#多个单细胞亚群合并rm(list = ls())library(Seurat)# devtools::install_github('satijalab/seurat-data')library(SeuratData)library(ggplot2)library(patchwork)library(dplyr)load(file = 'basic.sce.pbmc.Rdata')levels(Idents(pbmc))DimPlot(pbmc, reduction = 'umap', label = TRUE, pt.size = 0.5) + NoLegend()sce=pbmc#为了合并亚群,将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群genes_to_check = c('PTPRC', 'CD3D', 'CD3E', 'PRF1' , 'NKG7', 'CD19', 'CD79A', 'MS4A1' , 'CD68', 'CD163', 'CD14', "FCER1A", "PPBP")DotPlot(sce, group.by = 'seurat_clusters', features = unique(genes_to_check)) + RotatedAxis()Idents(sce)levels(sce)head(sce@meta.data)#方法1.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群)new.cluster.ids <- c("T", "Mono", "T", "B", "T", "Mono", "T", "DC", "Platelet")names(new.cluster.ids) <- levels(sce)#合并后的scesce <- RenameIdents(sce, new.cluster.ids)#合并后作图DimPlot(sce, reduction = 'umap', label = TRUE, pt.size = 0.5) + NoLegend()#方法2.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群) cluster2celltype <- c("0"="T", "1"="Mono", "2"="T", "3"= "B", "4"= "T", "5"= "Mono", "6"= "T", "7"= "DC", "8"= "Platelet")sce[['cell_type']] = unname(cluster2celltype[sce@meta.data$seurat_clusters])#合并后作图DimPlot(sce, reduction = 'umap', group.by = 'cell_type', label = TRUE, pt.size = 0.5) + NoLegend()#方法3.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群) # 一个数据框 (n=length(unique(sce@meta.data$seurat_clusters)))celltype=data.frame(ClusterID=0:(n-1), celltype='unkown')celltype[celltype$ClusterID %in% c(0,2,4,6),2]='T' #第0,2,4,6亚群为T细胞celltype[celltype$ClusterID %in% c(3),2]='B' #第3亚群为B细胞celltype[celltype$ClusterID %in% c(1,5),2]='Mono' #第1,5亚群为Monocelltype[celltype$ClusterID %in% c(7),2]='DC' #第7亚群为DCcelltype[celltype$ClusterID %in% c(8),2]='Platelet' #第8亚群为Plateletsce@meta.data$celltype = "NA"for(i in 1:nrow(celltype)){ sce@meta.data[which(sce@meta.data$seurat_clusters == celltype$ClusterID[i]),'celltype'] <- celltype$celltype[i]}table(sce@meta.data$celltype)phe=sce@meta.data#合并后作图DimPlot(sce, reduction = 'umap', group.by = 'celltype', label = TRUE, pt.size = 0.5) + NoLegend()head(sce@meta.data)table(sce@meta.data$cell_type, sce@meta.data$celltype)p1=DimPlot(sce, reduction = 'umap', group.by = 'celltype', label = TRUE, pt.size = 0.5) + NoLegend()p2=DotPlot(sce, group.by = 'celltype', features = unique(genes_to_check)) + RotatedAxis()p1+p2
