我們要實(shí)現(xiàn)下面這張圖,這是一張10X空間轉(zhuǎn)錄組分析得到的圖
圖片.png
這張圖可以展示基因的區(qū)域表達(dá)情況,也可以表示10X單細(xì)胞和10X空間轉(zhuǎn)錄組聯(lián)合之后锤躁,每種細(xì)胞類型在各個(gè)區(qū)域的分布情況,我們今天就來(lái)實(shí)現(xiàn)這張圖,我們采用的是10X空間轉(zhuǎn)錄組和10X單細(xì)胞分析后的結(jié)果盏浙,看其中一種細(xì)胞類型的空間分布眉睹。
首先讀取數(shù)據(jù),注意這里的數(shù)據(jù)必須包含單細(xì)胞空間聯(lián)合分析的結(jié)果废膘,至于聯(lián)合分析的方法竹海,我分享了很多,大家可以參考丐黄。
library(Seurat)
data = readRDS(seurat_obj)
注意對(duì)象的結(jié)構(gòu)斋配,我們是要10X單細(xì)胞和10X空間轉(zhuǎn)錄組聯(lián)合分析的結(jié)果,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
注意聯(lián)合分析的數(shù)據(jù)結(jié)構(gòu)灌闺,橫坐標(biāo)是Barcode艰争,縱坐標(biāo)是預(yù)測(cè)的細(xì)胞類型。
細(xì)胞類型的信息和數(shù)據(jù)整理
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")
接下來(lái)獲取空間點(diǎn)的坐標(biāo)和背景圖片信息
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")
###空間點(diǎn)的坐標(biāo)
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就是指定的細(xì)胞類型
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"))
企業(yè)微信截圖_16245251461799.png
可以分析細(xì)胞類型的區(qū)域分布桂对,非常贊
生活很好甩卓,等你超越