通过识别跨数据集的anchors(“锚点”,这些锚点代表不同数据集中被推测为来自相同生物学状态的细胞对/细胞群,随后用于校正数据集之间批次效应,使得数据可以整合在一起进行分析。
具体来说,该函数执行以下步骤:
- 特征选择:通过FindVariableFeatures找到的高变基因(默认是2000个)作为输入。
- 降维:对每个数据集进行PCA降维,使用共同的高变基因。然后,将这些PCA降维结果投射到一个共享的低维空间(通常使用CCA典型相关分析或PCA的相互最近邻方法)。
- 寻找锚点:在低维空间中,对每个数据集中的细胞,寻找跨数据集的最近邻作为候选锚点。然后,通过过滤步骤(包括评估共享最近邻的强度、锚点对之间的相似性等)来筛选出可靠的锚点。
- 评分锚点:对每个锚点对进行评分,评估它们代表相同生物学状态的可能性,过滤分数较低的锚点。
anchors <- FindIntegrationAnchors(
seurat_list,
dims = 1:20, # 减少维度数(从30减到20)
reduction = "rpca", # 使用更快的RPCA方法替代CCA
k.anchor = 5, # 减少锚点数量(默认20)
k.filter = 50, # 减少过滤阈值(默认200)
verbose = TRUE # 查看进度
)
anchors <- FindIntegrationAnchors(
seurat_list,
reference = c(1, 2), # 指定参考样本的索引
dims = 1:30
)
seurat_list <- lapply(seurat_list, function(x){
x <- subset(x,
subset = nFeature_RNA > 500 & nFeature_RNA < 5000 &
percent.mt < 20)
NormalizeData(x) %>%
FindVariableFeatures(selection.method="vst", nfeatures=2000)
})
anchors <- FindIntegrationAnchors(
seurat_list,
normalization.method = "SCT", # 使用SCTransform标准化
reduction = "rpca", # 使用鲁棒PCA
dims = 1:30
)
system.time({
anchors <- FindIntegrationAnchors(seurat_list, dims=1:30)
})
- 使用Harmony或Scanorama:这些工具通常比Seurat整合更快
- 分批处理:将样本分组整合,然后再整合结果
- 不要过度减少dims:维度太少可能导致整合效果不佳
- 保持足够的锚点:太少的锚点可能无法正确对齐细胞类型
- 监控整合质量:整合后检查不同样本的细胞是否混合良好
- 整合步骤虽然耗时,但对后续分析至关重要,因为它可以去除批次效应,允许跨样本比较,提高细胞类型识别的准确性。
############################################################
#### PART4. PCA
############################################################
obj_pca <- RunPCA(obj_scl,
features = VariableFeatures(obj_scl),
npcs = 50, # 先算50个,足够选拐点
verbose = FALSE)
##### 定量确定 PC 数量
pct <- obj_pca[["pca"]]@stdev / sum(obj_pca[["pca"]]@stdev) * 100
cumu <- cumsum(pct)
co1 <- which(cumu >= 90 & pct < 5)[1] # 累计解释方差 >=90% 且单个PC <5%
co2 <- sort(which(diff(pct) < -0.1), decreasing = TRUE)[1] + 1 # 更稳定的拐点检测方式
pc.use <- min(co1, co2, na.rm = TRUE)
if(is.na(pc.use)) pc.use <- 20 # 防止极端情况NA,备用值
print(paste("Suggested number of PCs:", pc.use)) # 你已经看到是22
##### 自定义 ElbowPlot(修正颜色名称)
Elbowplot <- ElbowPlot(obj_pca, ndims = 50)$data %>%
ggplot(aes(x = dims, y = stdev)) +
geom_point(size = 1.5) +
geom_vline(xintercept = pc.use, color = "darkred", linetype = "dashed", size = 1) +
theme_bw(base_size = 14) +
labs(title = "Elbow plot: quantitative approach",
x = "Principal Components",
y = "Standard Deviation")
##### 保存两张图(确保 outdir 已定义)
save_plot("P4.1_PCA_plot.png", DimPlot(obj_pca, reduction = "pca", dims = 1:2))
save_plot("P4.2_ElbowPlot.png", Elbowplot)
##### 可选:再保存一个带PC数的标注版本
Elbowplot_annotated <- Elbowplot +
annotate("text", x = pc.use + 3, y = max(obj_pca[["pca"]]@stdev)*0.9,
label = paste("Use", pc.use, "PCs"), color = "darkred", size = 5)
save_plot("P4.2_ElbowPlot_annotated.png", Elbowplot_annotated)
“Clustering resolutions were selected using adjusted Rand index (ARI) stability analysis. Main and sub-clustering were visualized explicitly using resolution-specific metadata columns to avoid identity reuse artifacts.”
改动原因1:FAN版本通过切换 Idents进行绘图,但这一操作是“全局状态修改”,后续 DEG / Marker / CellType 注释可能会受影响。为了不污染Idents,改为显式指定 group.by。
改动原因2:在 Seurat v4 / v5 中:FindClusters()没有 group.suffix,使用的是:cluster.name = "自定义列名",官方推荐方式是 显式指定列名,而不是后缀。
自定义 P5_clustering_umap_module 函数模块,已修正在 Seurat v4 / v5 都安全的标准函数。 主流程中调用方法如下:
P5_res <- P5_clustering_umap_module(
seurat_obj = obj_pca,#obj_pca即需要处理的数据自定义名
pc.use = pc.use, #pc.use已经在前面自义了,不用再改动
save_prefix = "P5"
)
obj_pca <- P5_res$seurat_obj