如果你做过单细胞 RNA-seq,应该很熟悉这种感觉:真正耗时间的,往往不是某一条 R 命令,而是把质控、归一化、降维、聚类、批次校正、细胞注释、marker 验证和结果导出完整地串起来。每一步都需要理解对象结构、参数设置和结果判断。
这次我们做了一个很直接的对照:拿同一份 PBMC 单细胞数据,一边用 R + Seurat/Harmony 手工分析,一边把分析目标直接用自然语言告诉 AI辅助分析平台。我们想看的不是“AI 会不会画图”,而是同一个分析意图,能不能从百行代码,压缩成两句话。
01|同样的数据,我们故意走了两条路
路线A是传统的手工R分析:使用Seurat完成归一化、PCA、邻居图、聚类、UMAP/tSNE,再按Sample使用Harmony做批次校正;随后结合marker表达与cluster结构进行手动细胞注释。
路线B是 LanxuBio(生信通平台)的AI辅助分析:上传同一份 RDS,不逐条写分析命令,而是直接说明“要做什么”“按什么字段去批次”“需要哪些细胞类型”“用哪些marker验证”。平台负责把自然语言要求转成可执行流程。
两条路线的目标细胞类型保持一致:Bcells、Plasmacells、Tcells、NKcells、Monocyte、Dendriticcells、Platelet,以及无法可靠归类的 Other / Ambiguous。
02|路线 A:手工 R 分析,百行代码并不夸张
手工分析真正费力的地方,不只是把函数写出来,而是你必须知道什么时候该做归一化、如何确定维度、怎样做Harmony、如何看marker,以及cluster到底应该被注释成哪一种细胞。
2.1|从预处理、降维、Harmony 到手工注释
###前期准备###
#加载需要的包
library(Seurat)
library(SeuratDisk)
library(harmony)
library(ggplot2)
library(dplyr)
library(patchwork)
library(Matrix)
library(DESeq2)
library(openxlsx)
#设置随机种子和工作路径
set.seed(123)
setwd("")
#读取RDS
PBMC_subset_Harmony <- readRDS("PBMC_subset_Harmony.rds")
#查看对象
PBMC_subset_Harmony
Reductions(PBMC_subset_Harmony)
#将assay重命名为RNA
PBMC_subset_Harmony <- RenameAssays(PBMC_subset_Harmony, originalexp = "RNA")
DefaultAssay(PBMC_subset_Harmony) <- "RNA"
PBMC_subset_Harmony
#saveRDS(PBMC_subset_Harmony, "PBMC_Demo.rds")
#数据规范化
PBMC_subset_Harmony <- NormalizeData(PBMC_subset_Harmony, normalization.method = "LogNormalize", scale.factor = 10000)
#识别高度可变的特征(特征选择)
PBMC_subset_Harmony <- FindVariableFeatures(PBMC_subset_Harmony, selection.method = "vst",nfeatures = 2000)
#确定高表达的前十个基因
top10 <- head(VariableFeatures(PBMC_subset_Harmony),10)
plot1 <- VariableFeaturePlot(PBMC_subset_Harmony)
plot2 <- LabelPoints(plot = plot1,points = top10,repel = TRUE)
#plot1+plot2
#缩放数据
all.genes <- rownames(PBMC_subset_Harmony)
PBMC_subset_Harmony <- ScaleData(PBMC_subset_Harmony,features = all.genes)
#线性降维
PBMC_subset_Harmony <- RunPCA(PBMC_subset_Harmony, features = VariableFeatures(object=PBMC_subset_Harmony))
print(PBMC_subset_Harmony[["pca"]],dims = 1:5,nfeatures = 5)
#Seurat提供可视化细胞和定义PCA,包括功能的几种有用的方法ViziDimReduction(),DimPlot()和DimHeatmap()
VizDimLoadings(PBMC_subset_Harmony,dims = 1:2,reduction = "pca")
DimPlot(PBMC_subset_Harmony,reduction = "pca")
DimHeatmap(PBMC_subset_Harmony,dims = 1,cells = 500,balanced = TRUE)
DimHeatmap(PBMC_subset_Harmony,dims = 1:15,cells=500,balanced = TRUE)
#确定数据集的维度
PBMC_subset_Harmony<-JackStraw(PBMC_subset_Harmony,num.replicate = 100)
PBMC_subset_Harmony<-ScoreJackStraw(PBMC_subset_Harmony,dims = 1:20)
JackStrawPlot(PBMC_subset_Harmony,dims = 1:15)
ElbowPlot(PBMC_subset_Harmony)
PBMC_subset_Harmony <-FindNeighbors(PBMC_subset_Harmony,dims = 1:20)
PBMC_subset_Harmony<-FindClusters(PBMC_subset_Harmony,resolution = 0.5)
head(Idents(PBMC_subset_Harmony),5)
#运行非线性降维(UMAP/tSNE)
PBMC_subset_Harmony<-RunUMAP(PBMC_subset_Harmony,dims = 1:20)
DimPlot(PBMC_subset_Harmony,reduction = "umap")
PBMC_subset_Harmony<-RunTSNE(PBMC_subset_Harmony,dims = 1:20)
DimPlot(PBMC_subset_Harmony,reduction = "tsne")
p1 <- DimPlot(PBMC_subset_Harmony, reduction = "umap", pt.size=0.5, label = F,repel = TRUE)
p2 <- DimPlot(PBMC_subset_Harmony, reduction = "umap",group.by = "Sample", pt.size=0.5, label = F,repel = TRUE)
p1 + p2
gc()
#harmony去除批次效应
PBMC_subset_Harmony_1 = PBMC_subset_Harmony %>% RunHarmony("Sample", plot_convergence = TRUE)
PBMC_subset_Harmony_1
#标准流程,参数不变
PBMC_subset_Harmony_1 <- PBMC_subset_Harmony_1 %>% RunUMAP(reduction = "harmony", dims = 1:20) %>% FindNeighbors(reduction = "harmony", dims = 1:20) %>% FindClusters(resolution = 0.5) %>%
identity()
p1 <- DimPlot(PBMC_subset_Harmony_1, reduction = "umap", pt.size=0.5, label = F,repel = TRUE)
p2 <- DimPlot(PBMC_subset_Harmony_1, reduction = "umap",group.by = "Sample", pt.size=0.5, label = F,repel = TRUE)
p3 <- DimPlot(PBMC_subset_Harmony_1, reduction = "harmony", pt.size=0.5, label = F,repel = TRUE)
p4 <- DimPlot(PBMC_subset_Harmony_1, reduction = "harmony",group.by = "Sample", pt.size=0.5, label = F,repel = TRUE)
p1 + p2 + p3 + p4
DimPlot(PBMC_subset_Harmony_1, reduction = "umap", group.by = "CellType_level1", pt.size=0.5, label = F,repel = TRUE)
DimPlot(PBMC_subset_Harmony_1, reduction = "umap", group.by = "CellType_level2", pt.size=0.5, label = F,repel = TRUE)
#细胞注释
#PBMC常用marker
markers <- list(
"B cell"=c("MS4A1","CD79A","CD74","CD37"),
"Plasma cell"=c("MZB1","JCHAIN","SDC1","DERL3"),
"CD4 T"=c("CD4","IL7R","CCR7","TCF7"),
"CD8 T"=c("CD8A","CD8B","CCL5","CTSW"),
"Treg"=c("FOXP3","IL2RA","CTLA4","TIGIT"),
"Gamma-delta T"=c("TRDC","TRGC1","TRGC2","CD3D"),
"NK"=c("NKG7","GNLY","KLRD1","PRF1"),
"CD14 Mono"=c("LST1","FCN1","S100A8","S100A9"),
"CD16 Mono"=c("FCGR3A","MS4A7","IFITM3","LILRB1"),
"DC"=c("CD1C","FCER1A","CLEC10A","HLA-DPA1"),
"pDC"=c("GZMB","CLEC4C","IL3RA","TCF4"),
"Platelet"=c("PPBP","PF4","GP9","GNG11"),
"Neutrophil"=c("FCGR3B","CSF3R","CEACAM8","CXCR2"),
"Erythrocyte"=c("HBB","HBA1","HBA2","AHSP"),
"Cycling"=c("MKI67","TOP2A","STMN1","TUBA1B")
)
#仅保留数据中存在的marker
markers <- lapply(markers, \(x) x[x %in% rownames(PBMC_subset_Harmony_1)])
markers <- markers[lengths(markers)>0]
#marker点图
DotPlot(PBMC_subset_Harmony_1, features=markers, group.by="seurat_clusters") + RotatedAxis() + theme(axis.title=element_blank())
#查看cluster
print(table(PBMC_subset_Harmony_1$seurat_clusters))
#手动注释cluster
cluster_to_celltype <- c(
"0"="Monocyte","1"="Tcells","2"="Tcells","3"="Monocyte","4"="NKcells",
"5"="NKcells","6"="Tcells","7"="NKcells","8"="Bcells","9"="Monocyte",
"10"="Bcells","11"="Dendriticcells","12"="Platelet","13"="NKcells",
"14"="Dendriticcells","15"="NKcells","16"="Monocyte","17"="Plasmacells","18"="Other"
)
#写入手动注释
PBMC_subset_Harmony_1$CellType_manual <- unname(cluster_to_celltype[as.character(PBMC_subset_Harmony_1$seurat_clusters)])
#查看cluster与注释对应关系
print(table(PBMC_subset_Harmony_1$seurat_clusters, PBMC_subset_Harmony_1$CellType_manual))
#可视化手动注释
DimPlot(PBMC_subset_Harmony_1, reduction="umap", group.by="CellType_manual", label=TRUE, repel=TRUE, pt.size=0.5)
#列出每种细胞类型的细胞数
print(table(PBMC_subset_Harmony_1$CellType_manual))
从 UMAP 可以看到,Tcells、NKcells、Monocyte、Bcells、Platelet、Plasmacells 等主要群体形成了相对清晰的结构。Dendriticcells 与 Monocyte 邻近,而少量 Other 细胞位于边界区域。
2.2|注释不是“看颜色”,还要回到 marker
手工注释完成后,还需要用经典 marker 去验证结果是否符合预期。下面这段代码定义了我们最终用于展示的 7 类细胞 marker,并按手工注释结果绘制 DotPlot。
#DotPlot可视化marker基因
markers_manual <- list(
"Bcells"=c("MS4A1","CD79A","CD74","CD37"),
"Plasmacells"=c("MZB1","JCHAIN","SDC1","DERL3"),
"Tcells"=c("CD3D","IL7R","CCR7","TCF7"),
"NKcells"=c("NKG7","GNLY","KLRD1","PRF1"),
"Monocyte"=c("LST1","MS4A7","IFITM3","LILRB1"),
"Dendriticcells"=c("CD1C","FCER1A","CLEC10A","HLA-DPA1"),
"Platelet"=c("PPBP","PF4","GP9","GNG11")
)
#仅保留数据中存在的marker
markers_manual <- lapply(markers_manual, \(x) x[x %in% rownames(PBMC_subset_Harmony_1)]); markers_manual <- markers_manual[lengths(markers_manual)>0]
#按照细胞数量从多到少排序
cell_order <- names(sort(table(PBMC_subset_Harmony_1$CellType_manual), decreasing=TRUE))
PBMC_subset_Harmony_1$CellType_manual <- factor(PBMC_subset_Harmony_1$CellType_manual, levels=cell_order)
markers_manual <- markers_manual[cell_order[cell_order %in% names(markers_manual)]]
#查看细胞数量和排序
print(sort(table(PBMC_subset_Harmony_1$CellType_manual), decreasing=TRUE))
#marker点图
DotPlot(PBMC_subset_Harmony_1, features=markers_manual, group.by="CellType_manual") + RotatedAxis() + theme(axis.title=element_blank())
saveRDS(PBMC_subset_Harmony_1, "PBMC_subset_Harmony_1.rds")
这张图把“细胞类型”重新落回到基因表达证据上:例如 Tcells 的 CD3D / IL7R / CCR7 / TCF7,NKcells 的 NKG7 / GNLY / KLRD1 / PRF1,Bcells 的 MS4A1 / CD79A / CD74 / CD37,以及 Platelet 的 PPBP / PF4 / GP9 / GNG11,都能在对应群体中看到预期的表达模式。
03|路线 B:把同样的分析意图,改成两句话
到了平台端,我们没有把上面的 R 代码重新写一遍,而是把目标直接写成自然语言。第一句话负责质控、降维和批次效应处理;第二句话负责细胞注释、marker 指定和点图可视化。
3.1|第一句话:完成质控、降维和按 Sample 去批次
“这是我的单细胞 RNA-seq 数据 uploads/PBMC_Demo.rds,请帮我进行质控和降维分析,并按照 Sample 进行批次效应的去除。”
平台会先把自然语言要求拆解成分析步骤,再逐步执行。截图中可以看到,流程被组织为加载数据、QC、去 doublet、过滤与归一化、PCA/聚类/UMAP,以及按 Sample 使用 Harmony 进行批次校正。
3.2|第二句话:指定细胞类型和 marker,完成自动注释
“请帮我进行细胞注释,我只需要 Bcells、Plasmacells、Tcells、NKcells、Monocyte、Dendriticcells、Platelet,其余细胞注释为 Other,然后用点图可视化这些细胞的 marker 基因。 Bcells = MS4A1/CD79A/CD74/CD37;Plasmacells = MZB1/JCHAIN/SDC1/DERL3;Tcells = CD3D/IL7R/CCR7/TCF7;NKcells = NKG7/GNLY/KLRD1/PRF1;Monocyte = LST1/MS4A7/IFITM3/LILRB1;Dendriticcells = CD1C/FCER1A/CLEC10A/HLA-DPA1;Platelet = PPBP/PF4/GP9/GNG11。”
这一句话相当于把“我要哪些细胞类型、用哪些 marker 判断、怎么展示结果”一次性说明。平台随后自动完成注释并输出相应结果。
3.3|平台自动注释结果
第二句话执行后,平台首先给出自动细胞注释 UMAP:
值得注意的是,平台没有强行把所有细胞都塞进某个既定类型,而是把证据不足的部分细胞保留为 Ambiguous。实际分析时,这类细胞可以根据项目规则继续复核、细分,或者最终统一归入 Other。
同一套 marker 也会继续用于表达验证:
自动注释后的 DotPlot 与手工结果关注的是同一件事:注释是否有 marker 表达支持。Bcells、Plasmacells、Tcells、NKcells、Monocyte、Dendriticcells 和 Platelet 的代表性 marker 在对应群体中均呈现出清晰的表达模式。
04|两套结果到底有多像?不要只看 UMAP,要逐细胞比
UMAP 的坐标方向并不是绝对位置。同一份数据在不同随机种子、邻居图或参数下,可能发生旋转、镜像,甚至出现局部形变。因此,“两个 UMAP 长得像不像”并不是判断注释是否一致的可靠方法。
我们采用更直接的比较方式:提取两个 Seurat 对象中共同存在的 cell barcode,把手工注释 CellType_manual 作为参考,与平台 auto_cell_type 逐细胞对应,然后统计每个手工细胞群在 LanxuBio 结果中的完整去向。
4.1|逐细胞注释对应关系的计算代码
#读取RDS
PBMC_subset_LanxuBio <- readRDS("auto_annotation_seurat.rds")
DimPlot(PBMC_subset_LanxuBio , reduction = "umap",group.by = "auto_cell_type", pt.size=0.5, label = F,repel = TRUE)
DimPlot(PBMC_subset_Harmony_1 , reduction = "umap",group.by = "CellType_manual", pt.size=0.5, label = F,repel = TRUE)
#提取两个对象共同存在的细胞
common_cells<-intersect(Cells(PBMC_subset_Harmony_1),Cells(PBMC_subset_LanxuBio))
#查看共同细胞数量
cat("共同细胞数:",length(common_cells),"\n")
#提取共同细胞的两种注释,Harmony作为基准
compare_df<-data.frame(Cell=common_cells,Harmony=as.character(PBMC_subset_Harmony_1[[]][common_cells,"CellType_manual"]),LanxuBio=as.character(PBMC_subset_LanxuBio[[]][common_cells,"auto_cell_type"]),stringsAsFactors=FALSE)
#去掉注释缺失的细胞
compare_df<-compare_df%>%dplyr::filter(!is.na(Harmony),!is.na(LanxuBio),Harmony!="",LanxuBio!="")
#判断每个细胞的两种注释是否一致
compare_df<-compare_df%>%dplyr::mutate(Same=Harmony==LanxuBio)
#检查Same列
table(compare_df$Same,useNA="ifany")
#总体一致和不一致数量及比例
overall_summary<-compare_df%>%dplyr::count(Same,name="Cell_number")%>%dplyr::mutate(Percent=round(Cell_number/sum(Cell_number)*100,2))
#查看总体结果
overall_summary
#直接输出总体一致率和不一致率
cat("比较细胞数:",nrow(compare_df),"\n一致细胞:",sum(compare_df$Same),"(",round(mean(compare_df$Same)*100,2),"%)\n不一致细胞:",sum(!compare_df$Same),"(",round(mean(!compare_df$Same)*100,2),"%)\n")
#计算Harmony每个细胞群的一致率和不一致率
agreement_by_celltype<-compare_df%>%dplyr::group_by(Harmony)%>%dplyr::summarise(Total=n(),Same_number=sum(Same),Different_number=sum(!Same),Same_percent=round(Same_number/Total*100,2),Different_percent=round(Different_number/Total*100,2),.groups="drop")%>%dplyr::arrange(dplyr::desc(Total))
#查看每个Harmony细胞群的一致率
agreement_by_celltype
#计算Harmony每个细胞群在LanxuBio中的完整注释去向,分母为该Harmony群全部共同细胞
mapping_all<-compare_df%>%dplyr::count(Harmony,LanxuBio,name="Cell_number")%>%dplyr::group_by(Harmony)%>%dplyr::mutate(Harmony_total=sum(Cell_number),Percent=round(Cell_number/Harmony_total*100,2))%>%dplyr::ungroup()%>%dplyr::arrange(Harmony,dplyr::desc(Percent))
#查看Harmony每个细胞群在LanxuBio中的完整注释去向
mapping_all
#提取两个方法注释不一致的细胞
mismatch_df<-compare_df%>%dplyr::filter(!Same)
#计算每个Harmony细胞群中不一致细胞被LanxuBio注释成其他什么群,分母为该Harmony群所有不一致细胞
mismatch_mapping<-mismatch_df%>%dplyr::count(Harmony,LanxuBio,name="Cell_number")%>%dplyr::group_by(Harmony)%>%dplyr::mutate(Total_mismatch=sum(Cell_number),Percent_of_mismatch=round(Cell_number/Total_mismatch*100,2))%>%dplyr::ungroup()%>%dplyr::arrange(Harmony,dplyr::desc(Percent_of_mismatch))
#查看不一致细胞被注释成其他细胞群的比例
mismatch_mapping
#建立细胞数量混淆矩阵,行为Harmony,列为LanxuBio
confusion_matrix<-table(compare_df$Harmony,compare_df$LanxuBio)
#查看细胞数量混淆矩阵
confusion_matrix
#建立百分比混淆矩阵,每一行Harmony细胞群加起来为100%
confusion_percent<-round(prop.table(confusion_matrix,margin=1)*100,2)
#查看百分比混淆矩阵
confusion_percent
#绘制完整注释对应关系热图,每一行Harmony细胞群的比例加起来为100%
ggplot(mapping_all,aes(x=LanxuBio,y=Harmony,fill=Percent)) + geom_tile(color="white",linewidth=0.3) + geom_text(aes(label=ifelse(Percent>=1,paste0(Percent,"%"),"")),size=3) + scale_fill_gradient(low="white",high="red") + labs(x="LanxuBio annotation",y="Harmony annotation",fill="Percent",title="Annotation comparison of common cells") + theme_classic() + theme(axis.text.x=element_text(angle=45,hjust=1))
4.2|多个主要细胞群呈现较高对应度
从热图的行比例来看,Bcells → Bcells 为 99.95%,Plasmacells → Plasmacells 为 100%,Platelet → Platelet 为 95.41%,Tcells → Tcells 为 93.52%,NKcells → NKcells 为 85.79%,Dendriticcells → Dendriticcells 为 79.81%,Monocyte → Monocyte 为 71.35%。这些数字表示“每个手工注释细胞群内部,有多少细胞被平台注释到对应类型”,并不是把所有细胞合并后的总体准确率。
差异同样值得看。Monocyte 与 Dendriticcells 的边界更复杂,手工标记为 Other 的细胞在自动结果中也分散到多个标签。这个结果反而说明:自动分析不应该被理解为“按一下按钮就无需判断”,边界群体仍然需要回到 marker、cluster 构成和具体生物学背景中复核。
05|所以,AI 真正替代的是什么?
我们更愿意把它叫做 AI 辅助分析,而不是“AI 替代生信”。这次对照里,AI 真正压缩的是大量重复、工程化、容易出错的执行工作:对象读写、标准流程串联、批次校正、重复绘图、文件整理,以及把自然语言需求转换成可执行步骤。
但有些事情仍然必须由人判断:QC 阈值是否合理、marker 是否适用于当前组织和疾病背景、边界群体如何解释、参数变化会不会影响结论,以及最终的生物学解释是否站得住。
对熟悉 R 的人,平台更像一个“可复现的分析执行助手”,能把大量重复操作自动化;对不熟悉 R 的人,它降低了从问题到第一版结果的门槛,但依然需要理解结果,而不是只看一张漂亮的图。
06|写在最后:代码依然重要,但它不一定要成为入口
过去做一套单细胞分析,学习 R、Seurat、对象结构和各类函数几乎是绕不开的第一道门槛。AI 辅助分析提供了另一种入口:先把问题、目标和约束说清楚,让系统帮助完成标准化、可追踪的执行流程,再把人的时间留给更有价值的部分——检查数据、理解差异、提出假设和解释生物学。
这并不意味着所有单细胞数据都可以用固定的两句话得到正确答案。数据质量、实验设计和生物学复杂度不同,最终分析仍然需要复核。但至少在这份 PBMC 数据上,我们看到一个很具体的变化:原本需要百行 R 代码和多轮手工操作的分析意图,可以被压缩成两条自然语言指令,而多个主要细胞类型仍保持较高的逐细胞对应度。
注:自动分析结果仍建议结合 QC、marker、cluster 结构与具体生物学背景进行复核。