单细胞空间转录组单细胞测序空间转录组

10X空间转录组的画图操作(基础知识)

2021-06-24  本文已影响0人  单细胞空间交响乐

我们要实现下面这张图,这是一张10X空间转录组分析得到的图

图片.png

这张图可以展示基因的区域表达情况,也可以表示10X单细胞和10X空间转录组联合之后,每种细胞类型在各个区域的分布情况,我们今天就来实现这张图,我们采用的是10X空间转录组和10X单细胞分析后的结果,看其中一种细胞类型的空间分布。

首先读取数据,注意这里的数据必须包含单细胞空间联合分析的结果,至于联合分析的方法,我分享了很多,大家可以参考。

library(Seurat)
data = readRDS(seurat_obj)

注意对象的结构,我们是要10X单细胞和10X空间转录组联合分析的结果,assay为predictions

decon_mtrx = t(data@assays$predictions@data)
decon_mtrx[1:3,1:3]
                           GC        FB2         E8
AAACAACGAATAGTTC-1 0.07912982 0.04368710 0.01843825
AAACAAGTATCTCCCA-1 0.04709078 0.03477676 0.04923712
AAACAATCTACTAGCA-1 0.01996235 0.01988366 0.43659999

注意联合分析的数据结构,横坐标是Barcode,纵坐标是预测的细胞类型。

细胞类型的信息和数据整理

cell_types_all <- colnames(decon_mtrx)
library(dplyr)
decon_df <- decon_mtrx %>%
  data.frame() %>%
  tibble::rownames_to_column("barcodes")

data@meta.data <- data@meta.data %>%
  tibble::rownames_to_column("barcodes") %>%
  dplyr::left_join(decon_df, by = "barcodes") %>%
  tibble::column_to_rownames("barcodes")

接下来获取空间点的坐标和背景图片信息

slice <- names(data@images)[1]
metadata_ds <- data.frame(data@meta.data)
colnames(metadata_ds) <- colnames(data@meta.data)
cell_types_interest <- cell_types_all

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 <- 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"))

最终的绘图

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 = 'red') +  ###cell就是指定的细胞类型
        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"))
企业微信截图_16245251461799.png

可以分析细胞类型的区域分布,非常赞

生活很好,等你超越

上一篇下一篇

猜你喜欢

热点阅读