前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >R tips:celltrek细胞共定位分析

R tips:celltrek细胞共定位分析

作者头像
生信菜鸟团
发布2025-03-17 20:53:18
发布2025-03-17 20:53:18
700
举报
文章被收录于专栏:生信菜鸟团生信菜鸟团

CellTrek是一个空间分析工具包,可以做单细胞和空间的整合分析、空间细胞共定位和空间基因表达分析。

它的共定位分析可以找到空间上距离相互靠近的细胞,并以网络图的形式呈现。

官方教程中,共定位分析需要依赖整合单细胞和空间数据之后的celltrek对象,本文则只使用空间数据进行共定位分析。同时官方提供了一个shiny程序用于展示共定位网络结构,交互性很好,但是无法导出图片,本文则使用igraph从头绘制共定位网络图。

仍以10X的xenium breast官方数据为例,下载后解压。

读取数据,构造用于celltrek共定位分析的对象

代码语言:txt
复制
# read xenium gene expression data 
xenium_expr <- Read10X_h5("Xenium_V1_FFPE_Human_Breast_IDC/cell_feature_matrix.h5") 
xenium <- CreateSeuratObject( 
    xenium_expr$`Gene Expression`, 
    min.cells = 10, 
    min.features = 10 
 ) %>% 
 RenameCells(add.cell.id = "cell") 
 
# read cell coords data 
# 3 column should exists to run CellTrek::scoloc analysis 
# coord_x, coord_y, id_new 
coords_xenium <- arrow::read_parquet("Xenium_V1_FFPE_Human_Breast_IDC/cells.parquet") 
meta_coords <- 
  coords_xenium %>% 
  dplyr::select( 
    cell_id, 
    coord_x = x_centroid, 
    coord_y = y_centroid 
 ) %>% 
  mutate(cell_id = str_c("cell", cell_id, sep = "_")) %>% 
  mutate(id_new = cell_id) %>% 
  column_to_rownames("cell_id") 
 
xenium <- xenium %>% AddMetaData(meta_coords) 

构造模拟的组别和细胞注释,这里只是示意,正常情况下,group是分析组别,annotation是注释好的细胞类型。

代码语言:txt
复制
set.seed(1234) 
xenium$group <- sample(LETTERS[1:3], size = ncol(xenium), replace = TRUE) 
xenium$annotation <- sample(str_c("Cell", LETTERS[1:10], sep = "_"), size = ncol(xenium), replace = TRUE) 

展示一下此时的数据,为了提高绘图速度,只是用10%的细胞绘制空间原位图:

代码语言:txt
复制
# spatial plot 
xenium[[]] %>% 
  dplyr::slice_sample(prop = 0.1) %>% 
  ggplot(aes(x = coord_x, y = coord_y)) + 
  geom_tile(aes(fill = group), height = 20, width = 20) + 
  coord_equal() + 
  ggsci::scale_fill_aaas() + 
  theme_void() 
 
xenium[[]] %>% 
  dplyr::slice_sample(prop = 0.1) %>% 
  ggplot(aes(x = coord_x, y = coord_y)) + 
  geom_tile(aes(fill = annotation), height = 20, width = 20) + 
  coord_equal() + 
  ggsci::scale_fill_igv() + 
  theme_void() 

空间共定位

代码语言:txt
复制
# run CellTrek analysis sequentially for the three groups 
all_data_grp_ls <- xenium %>% SplitObject(split.by = "group") 
 
# run celltrek 
all_celltrek_ls <- 
  all_data_grp_ls %>% 
  map(function(sc){ # browser() 
    graph_KL <- 
 CellTrek::scoloc( 
        sc, 
        col_cell   ='annotation', 
        use_method ='KL', 
        h   = 10, 
        eps = 1e-50 
 ) 
 
    cells <- sc$annotation %>% unique() 
    names(cells) <- cells %>% make.names() 
 
 ## We extract the minimum spanning tree (MST) result from the graph 
    graph_KL_mst_cons <- graph_KL$mst_cons 
    rownames(graph_KL_mst_cons) <- 
      colnames(graph_KL_mst_cons) <- 
      cells[colnames(graph_KL_mst_cons)] 
 
 ## We then extract the metadata (including cell types and their frequencies) 
    cell_class <- sc[[]] %>% 
      group_by(annotation) %>% 
      summarise(n = n()) %>% 
 as.data.frame() %>% 
      dplyr::rename(id = annotation) 
 
    list( 
      mst = graph_KL_mst_cons, 
      cell_class = cell_class 
 ) 
 }) 
 
# celltrek network can be visualized by scoloc_vis 
# group A: 
CellTrek::scoloc_vis(all_celltrek_lsAmst, meta_data = all_data_grp_ls

共定位网络图绘制

代码语言:txt
复制
# plot celltrek network by igraph 
 
cell_colors <- ggsci::pal_igv()(10) 
 
p_all_celltrek_ls <- 
  all_celltrek_ls %>% 
  map(function(ct){ 
    graph <- 
      ct$mst %>% 
 as.data.frame() %>% 
      rownames_to_column('from') %>%  
      pivot_longer(-from, names_to = "to", values_to = "weight") %>% 
      dplyr::filter(weight > 0.2) %>% 
      igraph::graph_from_data_frame() %>% 
      tidygraph::as_tbl_graph()  
 
 
 set.seed(12345) 
    p <- 
      graph %>% 
      tidygraph::activate(nodes) %>% 
      left_join(ct$cell_class, by = c(name = 'id')) %>% 
      ggraph::ggraph(layout = 'fr') + #, weights = weight) + 
      ggraph::geom_edge_link(aes(width = weight), color = 'gray', show.legend = FALSE) + 
      ggraph::geom_node_point(aes(size = n, color = name), show.legend = FALSE) + 
      ggraph::geom_node_text(aes(label = name), color = 'gray10', nudge_y= -0.2) + 
      ggraph::scale_edge_width(range = c(1, 4)) + 
      scale_size(range = c(4, 12)) + 
 # ggsci::scale_color_igv() + 
      scale_color_manual(values = cell_colors) + 
      coord_fixed(clip = 'off') + 
      theme_void() 
    p 
 }) 

celltrek网络图入下:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-03-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信菜鸟团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 读取数据,构造用于celltrek共定位分析的对象
  • 空间共定位
  • 共定位网络图绘制
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档