SeekSpace 空间转录组数据分析教程 (Seurat / 小鼠脑)
背景信息
本⽂档是关于寻因 SeekSpace 技术所得到的数据及其 Seurat 分析⽅法的说明。
寻因 SeekSpace 技术可以检测到单细胞精度的基因表达数据,同时也能定位出每个细胞在组织中的空间坐标。
SeekSpace 的数据分析简便易学,能够很方便地兼容常见单细胞转录组的分析软件,例如 Seurat 和 scanpy。
本数据为基于 SeekSpace 技术的⼩⿏脑的空间数据。数据中包含 32,758 个细胞的单细胞转录组矩阵,空间坐标矩阵,组织 DAPI 染⾊图⽚和 HE 图片。
⽂件说明
SeekSpace 技术的配套的基础数据处理的软件是 SeekSpace® Tools,该软件可以从测序文库中识别细胞的表达信息,同时可以识别出每个细胞在空间上的位置。
经过 SeekSpace® Tools 软件处理之后所得到的结果文件格式如下:
├── WTH1092_filtered_feature_bc_matrix # 表达矩阵⽬录,可以使⽤Seurat 的 Read10X 命令进⾏读取
│ ├── barcodes.tsv.gz
│ ├── features.tsv.gz
│ ├── matrix.mtx.gz
│ └── cell_locations.tsv.gz 为⼩⿏脑测序数据中细胞的空间坐标⽂件。第 1 列是 barcode,顺序与 matrix 中的 filtered_feature_bc_matrix/barcode 中细胞的顺序⼀致;第 2 列和第 3 列分别为该 barcode 所代表的细胞的空间位置(即空间芯⽚上的像素坐标)。
├── WTH1092_aligned_DAPI.png # 为⼩⿏脑组织切⽚的 DAPI 染⾊图⽚
├── WTH1092_aligned_HE.png
├── WTH1092_aligned_HE_TIMG.png
├── seekspace_of_Seurat.ipynb # 为使⽤Seurat 分析该⼩⿏脑空间数据的 jupyter 示例⽂件
└── seekspace_of_scanpy.ipynb # 为使⽤scanpy 分析该⼩⿏脑空间数据的 jupyter 示例⽂件
SeekSpace 技术中一个像素点的大小是约为 0.2653 微米,将像素点的坐标乘以 0.2653 即可转换计算细胞在真实空间上的距离。
library(Seurat)
library(tidyverse)
library(ggplot2)创建 Seurat 对象
读取 SeekSpace 的矩阵⽂件,这里直接使用 Seurat 常用的读入和创建矩阵的方法即可。
mouse_brain.data <- Read10X('./Outs/WTH1092_filtered_feature_bc_matrix')
mouse_brain <- CreateSeuratObject(counts=mouse_brain.data,project='mouse_brain')降维聚类
这里使用常见的默认参数进行降维聚类,实际分析的时候,可以按照样品的特征进行相应的修改。
mouse_brain <- NormalizeData(mouse_brain, normalization.method = "LogNormalize", scale.factor = 10000) %>%
FindVariableFeatures(selection.method = "vst", nfeatures = 2000) %>%
ScaleData() %>%
RunPCA() %>%
FindNeighbors(dims = 1:15) %>%
FindClusters(resolution = seq(0.2, 1.4, 0.3)) %>%
RunUMAP(dims = 1:15)
Idents(mouse_brain) <- 'RNA_snn_res.0.8'添加空间坐标
接下来我们使用 Seurat 的 CreateDimReducObject 函数,给 Seurat 对象中的每个细胞添加一个空间的坐标。
注意!!
CreateDimReducObject 函数读入空间坐标矩阵的细胞的顺序必须与 mouse_brain 里面的 meta.data 里面细胞的顺序相一致,否则在多样品整合分析的时候会出现细胞顺序错乱的情况。
这里我们使用了一个简单的一行代码对空间坐标矩阵进行了排序。
spatial_df <- read.table('./Outs/WTH1092_filtered_feature_bc_matrix/cell_locations.tsv.gz', row.names = 1, sep = '\t',header = T)
colnames(spatial_df) <- c("spatial_1","spatial_2")
spatial_matrix <- as.matrix(spatial_df)
spatial_matrix_sorted <- spatial_matrix[match(row.names(mouse_brain@meta.data),row.names(spatial_matrix)), ]
mouse_brain@reductions$spatial <- CreateDimReducObject(embeddings = spatial_matrix_sorted, key='spatial_', assay='RNA')head(mouse_brain@reductions$spatial@cell.embeddings)经过这步处理之后,我们可以在 Seurat 对象中看到一个新的坐标系”spatial“,这个坐标系即为每个细胞在空间位置上的坐标。
str(mouse_brain@reductions$spatial)添加组织图像信息
下面我们展示一下在空间数据上添加背景图片。
samplename = 'WTH1092'
size_x = 55128
size_y = 19906
#如果芯片编号的倒数第二个字母是A,则size_x=55128,size_y=19906;
#如果芯片编号的倒数第二个字母是B,则size_x=55050,size_y=19906;
#如果芯片编号的倒数第二个字母是C,则size_x=55050,size_y=19906.
#(可以在samplename_report.html质控报告的Summary Table中查看芯片编号)
mouse_brain@misc$info[[`samplename`]]$size_x = as.integer(size_x)
mouse_brain@misc$info[[`samplename`]]$size_y = as.integer(size_y)添加 DAPI 图片
img = './Outs/WTH1092_aligned_DAPI.png'
#base64格式
img_64 = base64enc::dataURI(file = img)
mouse_brain@misc$info[[`samplename`]]$img = img_64
#png格式
img_gg <- png::readPNG(img)
img_grob <- grid::rasterGrob(img_gg, interpolate = FALSE, width = grid::unit(1,"npc"), height = grid::unit(1, "npc"))
mouse_brain@misc$info[[`samplename`]]$img_gg = img_grob添加 HE 图片
img = './Outs/WTH1092_aligned_HE_TIMG.png'
#base64格式
img_64 = base64enc::dataURI(file = img)
mouse_brain@misc$info[[`samplename`]]$img_he = img_64
#png格式
img_gg <- png::readPNG(img)
img_grob <- grid::rasterGrob(img_gg, interpolate = FALSE, width = grid::unit(1,"npc"), height = grid::unit(1, "npc"))
mouse_brain@misc$info[[`samplename`]]$img_he_gg = img_grobstr(mouse_brain@misc$info)细胞注释结果
接下来我们再把细胞的注释结果添加到每个细胞上,这样我们就可以看不同的细胞类型在空间上的分布情况了。
SeekSpace 细胞注释的过程,跟普通的单细胞注释的过程完全一致。
我们之前已经对 demo 数据进行了大群和亚群的注释,注释的结果文件放在了 anntation.csv 文件中。
接下来我们简单的读入处理一下。
anno <- read.csv("./annotation.csv",row.names = 1,header = TRUE)mouse_brain <- AddMetaData(mouse_brain, anno)head(mouse_brain)saveRDS(mouse_brain,"WTH1092_demo_mouse_brain.rds")结果可视化
经过 Seurat 处理后的数据保存为 rds,我们可以继续使用 Seurat 包默认的一系列函数进行绘图
空间坐标绘图
#dimplot umap
options(repr.plot.height=7, repr.plot.width=7)
DimPlot(mouse_brain, reduction = 'umap')options(repr.plot.height=10, repr.plot.width=10)
FeaturePlot(mouse_brain, reduction = 'umap', features=c('Mbp','Mobp', 'Olig1','Plp1'), pt.size = 1)当我们把 DimPlot 函数中的 reduction 参数设置为 "spatial" 的时候,我们即可将细胞绘制在空间水平上。
options(repr.plot.height=7, repr.plot.width=15)
DimPlot(mouse_brain, reduction = 'spatial',pt.size = 1)options(repr.plot.height=7, repr.plot.width=15)
DimPlot(mouse_brain, reduction = 'spatial',pt.size = 1,group.by = "Sub_CellType")同样,我们可以在使用 FeaturePlot 函数时,将 reduction 参数设置为"spatial",来查看基因在空间位置上的表达情况。
options(repr.plot.height=10, repr.plot.width=20)
FeaturePlot(mouse_brain, reduction = 'spatial', features = c('Mbp','Mobp', 'Olig1','Plp1'),pt.size = 0.7)SeekSpace 数据对 Seurat 的兼容性比较好,取子集等等函数均可运行。
options(repr.plot.height=7, repr.plot.width=15)
sub_mouse_brain <- subset(mouse_brain, Main_CellType == 'Ext')
DimPlot(sub_mouse_brain, reduction = 'spatial',pt.size = 1.5, group.by = "Main_CellType")带有 DAPI/HE 的图片绘制
自定义了 ImageSpacePlot 和 FeatureSpacePlot 两个函数绘制细胞在空间上的分群信息和细胞连续变量的指标
######################绘图细胞分群函数
ImageSpacePlot = function(obj, group_by, type="DAPI", sample=names(obj@misc$info)[1], size=1, alpha=1,color=MYCOLOR){
MYCOLOR=c(
"#6394ce", "#2a4c87", "#eed500", "#ed5858",
"#f6cbc2", "#f5a2a2", "#3ca676", "#6cc9d8",
"#ef4db0", "#992269", "#bcb34a", "#74acf3",
"#3e275b", "#fbec7e", "#ec4d3d", "#ee807e",
"#f7bdb5", "#dbdde6", "#f591e1", "#51678c",
"#2fbcd3", "#80cfc3", "#fbefd1", "#edb8b5",
"#5678a8", "#2fb290", "#a6b5cd", "#90d1c1",
"#a4e0ea", "#837fd3", "#5dce8b", "#c5cdd9",
"#f9e2d6", "#c64ea4", "#b2dfd6", "#dbdfe7",
"#dff2ec", "#cce8f3", "#e74d51", "#f7c9c4",
"#f29c81", "#c9e6e0", "#c1c5de", "#750000"
)
raster_type <- switch(type,
HE = "img_he_gg",
DAPI = "img_gg",
stop("Invalid type. Must be 'HE' or 'DAPI'.")
)
spatial_coord1 <- as.data.frame(obj[[group_by]])
colnames(spatial_coord1) <- group_by
spatial_coord2 <- as.data.frame(obj@reductions$spatial@cell.embeddings)
spatial_coord <-cbind(spatial_coord2,spatial_coord1)
ImageSpacePlot <- ggplot2::ggplot() + ggplot2::annotation_custom(grob = obj@misc$info[[sample]][[raster_type]],
xmin = 0, xmax = obj@misc$info[[sample]]$size_x,
ymin = 0, ymax = obj@misc$info[[sample]]$size_y) +
ggplot2::geom_point(data = spatial_coord, ggplot2::aes(x = spatial_1,y = spatial_2, color = !!sym(group_by),
fill = !!sym(group_by)), size=size, alpha=alpha)+
labs(size = group_by) + guides(alpha = "none")+
ggplot2::theme_classic()+
scale_color_manual(values = color)+ coord_fixed()
return(ImageSpacePlot)
}
###################绘图基因表达函数
FeatureSpacePlot = function(obj, feature, type="DAPI", sample=names(obj@misc$info)[1], size=1, alpha=c(1,1),color=c("lightgrey","blue")){
raster_type <- switch(type,
HE = "img_he_gg",
DAPI = "img_gg",
stop("Invalid type. Must be 'HE' or 'DAPI'.")
)
spatial_coord1 <- as.data.frame(obj@reductions$spatial@cell.embeddings)
spatial_coord2 <- FetchData(obj,feature)
colnames(spatial_coord2) <- feature
spatial_coord <-cbind(spatial_coord1,spatial_coord2)
FeatureSpacePlot <-ggplot2::ggplot() + ggplot2::annotation_custom(grob = obj@misc$info[[sample]][[raster_type]],
xmin = 0, xmax = obj@misc$info[[sample]]$size_x, ymin = 0, ymax = obj@misc$info[[sample]]$size_y) +
ggplot2::geom_point(data = spatial_coord, ggplot2::aes(x = spatial_1, y = spatial_2,color = !!sym(feature),alpha = !!sym(feature)), size=size)+
labs(color = feature)+
guides(alpha = "none")+
ggplot2::theme_classic()+
ggplot2::scale_alpha_continuous(range=alpha)+
scale_color_gradient(low=color[1],high = color[2])+ coord_fixed()
return(FeatureSpacePlot)
}# DAPI背景的细胞分群图
options(repr.plot.height=7, repr.plot.width=15)
ImageSpacePlot(obj=mouse_brain, group_by = "Sub_CellType",type="DAPI",size=0.7)# HE背景的细胞分群图
options(repr.plot.height=7, repr.plot.width=15)
ImageSpacePlot(obj=mouse_brain, group_by = "Sub_CellType",type="HE")# DAPI背景的基因表达图
options(repr.plot.height=7, repr.plot.width=15)
FeatureSpacePlot(obj=mouse_brain, feature="Hpca",type="DAPI")# HE背景的基因表达图
options(repr.plot.height=7, repr.plot.width=15)
FeatureSpacePlot(obj=mouse_brain, feature="Hpca",type="HE")