本笔记来源于B站@生信技能树-jimmy;学习视频链接: 「生信技能树」单细胞数据挖掘
- cell annotation
# 对肿瘤细胞来说,分群后的细胞亚群注释是不可行的
# 这里仅仅是演示 SingleR 做 cell annotation的流程
library(SingleR)
??singleR
library(Seurat)
load("../../scRNA.Rdata")
refdata <- get(load("../../rawdata/HumanPrimaryCellAtlasData.Rdata"))
assay(refdata)[1:4,1:4]
head(refdata@colData)
head(refdata)
ref <- HumanPrimaryCellAtlasData() #下载参考数据库,等待时间较长。建议下载后储存为Rdata,方便以后使用。
testdata <- GetAssayData(scRNA, slot="data")
clusters <- scRNA@meta.data$seurat_clusters
cellpred <- SingleR(test = testdata, ref = refdata, labels = refdata$label.fine,
# label.fine耗时比较长一点
method = "cluster", clusters = clusters,
assay.type.test = "logcounts", assay.type.ref = "logcounts")
save(cellpred,file = "../../cellpred.Rdata")
load("../../tmp/cellpred.Rdata")
rm(refdata, HumanPrimaryCellAtlasData, testdata) #珍惜内存
table(cellpred$labels)
celltype = data.frame(ClusterID=rownames(cellpred), celltype=cellpred$labels, stringsAsFactors = F)
table(celltype$ClusterID,celltype$celltype)
SingleCell8_1.jpg
# 结合上述结果,给scRNA增添celltype注释信息
# 先新增列celltype,值均为NA,然后利用下一行代码循环填充
scRNA@meta.data$celltype = "NA"
for(i in 1:nrow(celltype)){
scRNA@meta.data[which(scRNA@meta.data$seurat_clusters == celltype$ClusterID[i]),'celltype'] <- celltype$celltype[i]}
# 绘图
p1 <- DimPlot(scRNA, group.by="celltype", label=F , reduction='tsne')
p1
ggsave("../../out/3.3celltype_anno.pdf", plot = p1, width = 18, height = 12)
Fig2_1.png
网友评论