前面介绍过整合scRNA-seq和空间转录组数据主要有(1)映射(Mapping)和(2)去卷积(Deconvolution)两种方法。前面空转 | 结合scRNA完成空转spot注释(Seurat Mapping) & 彩蛋(封面的空转主图代码)介绍了使用Seurat Mapping的方式进行spot注释,本文介绍一种经典的解卷积方法-SPOTlight。
一 载入R包,数据
直接使用前面Seurat空转得到的空转和单细胞转录组的结果。
library(SPOTlight)
library(Seurat)
library(ggplot2)
library(SingleCellExperiment)
library(scater)
library(scran)
#load data
load("Brain_ST_scRNA.sBio.Rdata")
head(Brain_scRNA)
head(Brain_ST)
准备工作详见前面,单细胞数据最好完成了注释。
二 SPOTlight 分析
1,数据处理
先将数据转为SingleCellExperiment 对象,然后按照官网文档https://github.com/MarcElosua/SPOTlight/blob/HEAD/vignettes/SPOTlight_kidney.Rmd 进行处理
sce <- as.SingleCellExperiment(Brain_scRNA)
### Feature selection
sce <- logNormCounts(sce)
### Variance modelling
# 去掉核糖体和线粒体基因
genes <- !grepl(pattern = "^RP[L|S]|MT", x = rownames(sce))
dec <- modelGeneVar(sce , subset.row = genes)
# 计算高变基因
hvg <- getTopHVGs(dec, n = 3000)
# 加上细胞注释信息
colLabels(sce) <- colData(sce)$celltype
# Compute marker genes
mgs <- scoreMarkers(sce, subset.row = genes)
# 保留最相关的marker基因
mgs_fil <- lapply(names(mgs), function(i) {
x <- mgs[[i]]
# Filter and keep relevant marker genes, those with AUC > 0.8
x <- x[x$mean.AUC > 0.8, ]
# Sort the genes from highest to lowest weight
x <- x[order(x$mean.AUC, decreasing = TRUE), ]
# Add gene and cluster id to the dataframe
x$gene <- rownames(x)
x$cluster <- i
data.frame(x)
})
mgs_df <- do.call(rbind, mgs_fil)
使用lapply函数批量得到每种celltype的marker 基因,这里是根据AUC的阈值(0.8)进行筛选,其中0.8 可以根据需要自行更改。
2,SPOTlight分析
使用SPOTlight主函数进行分析,注新版本的是SPOTlight函数,而不是spotlight_deconvolution函数了。
### Deconvolution
res <- SPOTlight(
x = sce,
y = Brain_ST,
as.character(sce$celltype), # 也可以是cluster, =
mgs = mgs_df,
hvg = hvg,
weight_id = "mean.AUC",
"cluster", =
gene_id = "gene")
#Extract data from `SPOTlight`:
decon_mtrx <- res$mat
#这里重命名,非必要
<- paste(colnames(decon_mtrx),"Spotlight",sep = "_")
结果得到的是每个spot的 各个celltype的占比,这里重命名是为了区分(比如后面使用其他方法进行注释)。
和Seurat一致,也可以2种保存方式
(1)添加至metadata.
(2)虽然res 不是SeuratObject ,但是也可以构建为新的slot 。
#meta 方式添加
Brain_ST@meta.data <- cbind(Brain_ST@meta.data, decon_mtrx)
head(Brain_ST)
#构建新的 slot
Brain_ST[["SPOTlight"]] <- CreateAssayObject(t(res$mat))
DefaultAssay(Brain_ST) <- "SPOTlight"
三 SPOTlight 结果可视化
1,提取SPOTlight结果
## 提取结果
head(mat <- res$mat)[, seq_len(length(unique(sce$celltype)))]
mod <- res$NMF
res.data <- (mat <- res$mat)[, seq_len(length(unique(sce$celltype)))]
2,topic对细胞类型的拟合情况
使用plotTopicProfiles 函数绘制拟合情况图
## 检查topic对细胞类型的拟合情况
plotTopicProfiles(
x = mod,
y = sce$celltype, #
facet = FALSE,
min_prop = 0.01,
ncol = 1) +
theme(aspect.ratio = 1)
3,Correlation Matrix 和 Co-localization
## Spatial Correlation Matrix
p1 <- plotCorrelationMatrix(mat)
## Co-localization
p2 <- plotInteractions(mat, "heatmap")
plotInteractions(mat, "network")
4,Scatterpie 饼图
这时SPOTlight 注释spot后的核心图,将每个spot中的各celltype比例绘制为饼图,可以绘制到切片tiff背景上(左图),也可以同样的形状绘制在白板上(右图)。
ct <- colnames(mat)
< 0.1] <- 0
#自定义颜色
paletteMartin <- c(
"#004949", "#009292", "#ff6db6", "#ffb6db",
"#490092", "#006ddb", "#b66dff", "#6db6ff", "#b6dbff",
"#924900", "#db6d00", "#24ff24", "#ffff6d")
pal <- colorRampPalette(paletteMartin)(length(ct))
names(pal) <- ct
p3 <- plotSpatialScatterpie(
x = Brain_ST,
y = mat,
cell_types = colnames(mat),
T, #以tiff为背景 =
scatterpie_alpha = 1,
pie_scale = 0.4,
# Rotate the image 90 degrees counterclockwise
degrees = -90,
# Pivot the image on its x axis
axis = "h") +
scale_fill_manual(
values = pal,
breaks = names(pal))
p4 <- plotSpatialScatterpie(
x = Brain_ST,
y = mat,
cell_types = colnames(mat),
img = FALSE,
scatterpie_alpha = 1,
pie_scale = 0.4) +
scale_fill_manual(
values = pal,
breaks = names(pal))
p3 + p4
注意:(1)可以通过img 是否添加背景 ;
(2)pal 自定义颜色,注意长度 与celltype个数一致 ;
(3)degrees 调整翻转角度 和 tiff图片一致
5,批量绘制解析后的结果
前面Seurat的 四 彩蛋- 空转主图可视化 部分了介绍了lapply 得到list然后自定义拼图的方式,这里介绍一下SpatialFeaturePlot进行绘制的方式。
注意要用 & 而不是 + ,否则后续的theme等设置只会对最后一张图有效果。
celltypes = rownames(Brain_ST)
SpatialFeaturePlot(Brain_ST, features = celltypes,
pt.size.factor = 1.6,
ncol = 4,
crop = TRUE) &
DarkTheme() &
theme(text=element_text(size=14)) &
theme(text=element_text(face = "bold")) &
theme(legend.text=element_text(size=7))
https://github.com/satijalab/seurat/issues/3698 That is because of differences between patchwork and ggplot2. Simply use patchwork theming & instead of ggplot2 +.
参考资料:
https://github.com/MarcElosua/SPOTlight/blob/HEAD/vignettes/SPOTlight_kidney.Rmd
◆ ◆ ◆ ◆ ◆
精心整理(含图PLUS版)|R语言生信分析,可视化(R统计,ggplot2绘图,生信图形可视化汇总)