美文网首页ggplot2高级绘图操作a1f594e7e302单细胞测序专题集合
10X空间转录组绘图分析之体现两种细胞类型的空间位置

10X空间转录组绘图分析之体现两种细胞类型的空间位置

作者: 单细胞空间交响乐 | 来源:发表于2022-01-28 11:34 被阅读0次

    过年了,有钱没钱,回家过年,但今天居然是工作日,太没有人道了,今天我们就来简单画画图吧,我们来绘制一下空间转录组两种细胞类型的空间位置分布图,下面的图应该都很常见了。

    图片.png

    要么是全部细胞类型的比例图,要么就是单个细胞类型或者单个基因的空间分布图,那如何展示两种细胞类型的空间关系分布图呢?注意看,我们来实现一下

    加载并读取矩阵,这里都已经处理好了,单细胞空间联合分析的结果也都在对象里面了,前面的处理过程大家自己做就可以了。
    suppressMessages({
    library(Seurat)
    library(argparse)
    library(dplyr)
    library(ggplot2)
    })
    cortex_sp = readRDS(spatial_rds)
    
    提取信息,单细胞空间联合分析的信息并对齐,添加到对象的meta中
    decon_mtrx = t(cortex_sp@assays$predictions@data)
    
    cell_types_all <- colnames(decon_mtrx)[which(colnames(decon_mtrx) != "max")]
    
    decon_df <- decon_mtrx %>%
      data.frame(check.names = F) %>%
      tibble::rownames_to_column("barcodes")
    
    cortex_sp@meta.data <- cortex_sp@meta.data %>%
      tibble::rownames_to_column("barcodes") %>%
      dplyr::left_join(decon_df, by = "barcodes") %>%
      tibble::column_to_rownames("barcodes")
    
    开始绘图
    slice <- names(cortex_sp@images)[1]
    metadata_ds <- data.frame(cortex_sp@meta.data)
    colnames(metadata_ds) <- colnames(cortex_sp@meta.data)
    cell_types_interest <- c('Cajal-Retzius-cell','Smooth-muscle-cell')
    
    metadata_ds <- metadata_ds %>% tibble::rownames_to_column("barcodeID") %>%
                dplyr::mutate(rsum = base::rowSums(.[, cell_types_interest,
                    drop = FALSE])) %>% dplyr::filter(rsum != 0) %>%
                dplyr::select("barcodeID") %>% dplyr::left_join(metadata_ds %>%
                tibble::rownames_to_column("barcodeID"), by = "barcodeID") %>%
                tibble::column_to_rownames("barcodeID")
    
    空间坐标,spatial_coord包含了空间坐标和单细胞空间联合的信息
    spatial_coord <- data.frame(cortex_sp@images[[slice]]@coordinates) %>%
            tibble::rownames_to_column("barcodeID") %>% dplyr::mutate(imagerow_scaled = imagerow *
            cortex_sp@images[[slice]]@scale.factors$lowres, imagecol_scaled = imagecol *
            cortex_sp@images[[slice]]@scale.factors$lowres) %>% dplyr::inner_join(metadata_ds %>%
            tibble::rownames_to_column("barcodeID"), by = "barcodeID")
    
    读取图片并绘图,我们先绘制单个细胞类型的
    img <- png::readPNG(img)
    img_grob <- grid::rasterGrob(img, interpolate = FALSE, width = grid::unit(1, 
            "npc"), height = grid::unit(1, "npc"))
    for (cell in cell_types_all){
    Max = max(spatial_coord[,cell])
    spatial_coord[,cell] = spatial_coord[,cell]/Max
    scatterpie_plt <- suppressMessages(ggplot2::ggplot() + ggplot2::annotation_custom(grob = img_grob, 
            xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) + 
            ggplot2::geom_point(data = spatial_coord, ggplot2::aes(x = imagecol_scaled, 
                y = imagerow_scaled,size = get(cell), color = '#FF4500') +
            ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img), 
            0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11, 
            rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1, 
            xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") +ggplot2::scale_size_continuous(range=c(0,2))+labs(size = cell))
    pdf(paste(outdir,paste(cell,'dot.pdf',sep = '.'),sep = '/'),width = 8,height = 7)
    print(scatterpie_plt)
    dev.off()
    }
    
    图片.png 图片.png

    好,我们把两种细胞类型画在一起

    cell = 'Cajal-Retzius-cell'
    cell1 = 'Smooth-muscle-cell'
    Max = max(spatial_coord[,cell])
    spatial_coord[,cell] = spatial_coord[,cell]/Max
    Max1 = max(spatial_coord[,cell1])
    spatial_coord[,cell1] = spatial_coord[,cell1]/Max1
    
    scatterpie_plt <- suppressMessages(ggplot2::ggplot() + ggplot2::annotation_custom(grob = img_grob,xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) +
    ggplot2::geom_point(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
    y = imagerow_scaled,size = get(cell),alpha = get(cell)), color = 'yellow') + ggplot2::geom_point(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
    y = imagerow_scaled,size = get(cell1),alpha = get(cell1)), color = 'blue') +
    ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img),
    0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11,
    rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1,
    xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") +ggplot2::scale_size_continuous(range=c(0,2))+ggplot2::scale_alpha_continuous(range=c(0,1))+labs(size = cell) + guides(alpha = "none"))
    
    
    图片.png

    看来两种细胞类型之间是有一定的距离的,这个方法用处很广,主要看两种甚至多种细胞类型之间的空间位置,以此来做下游的更重要的分析,比如空间细胞网络、细胞单元,前后细胞类型的空间位置变化等等等。

    生活很好,有你更好,祝大家新年快乐

    相关文章

      网友评论

        本文标题:10X空间转录组绘图分析之体现两种细胞类型的空间位置

        本文链接:https://www.haomeiwen.com/subject/shozhrtx.html