1. #多个单细胞亚群合并
    2. rm(list = ls())
    3. library(Seurat)
    4. # devtools::install_github('satijalab/seurat-data')
    5. library(SeuratData)
    6. library(ggplot2)
    7. library(patchwork)
    8. library(dplyr)
    9. load(file = 'basic.sce.pbmc.Rdata')
    10. levels(Idents(pbmc))
    11. DimPlot(pbmc,
    12. reduction = 'umap',
    13. label = TRUE,
    14. pt.size = 0.5) + NoLegend()
    15. sce=pbmc
    16. #为了合并亚群,将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群
    17. genes_to_check = c('PTPRC', 'CD3D', 'CD3E', 'PRF1' , 'NKG7',
    18. 'CD19', 'CD79A', 'MS4A1' ,
    19. 'CD68', 'CD163', 'CD14',
    20. "FCER1A", "PPBP")
    21. DotPlot(sce,
    22. group.by = 'seurat_clusters',
    23. features = unique(genes_to_check)) + RotatedAxis()
    24. Idents(sce)
    25. levels(sce)
    26. head(sce@meta.data)
    27. #方法1.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群)
    28. new.cluster.ids <- c("T", "Mono", "T",
    29. "B", "T", "Mono",
    30. "T", "DC", "Platelet")
    31. names(new.cluster.ids) <- levels(sce)
    32. #合并后的sce
    33. sce <- RenameIdents(sce,
    34. new.cluster.ids)
    35. #合并后作图
    36. DimPlot(sce,
    37. reduction = 'umap',
    38. label = TRUE, pt.size = 0.5) + NoLegend()
    39. #方法2.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群)
    40. cluster2celltype <- c("0"="T",
    41. "1"="Mono",
    42. "2"="T",
    43. "3"= "B",
    44. "4"= "T",
    45. "5"= "Mono",
    46. "6"= "T",
    47. "7"= "DC",
    48. "8"= "Platelet")
    49. sce[['cell_type']] = unname(cluster2celltype[sce@meta.data$seurat_clusters])
    50. #合并后作图
    51. DimPlot(sce,
    52. reduction = 'umap',
    53. group.by = 'cell_type',
    54. label = TRUE, pt.size = 0.5) + NoLegend()
    55. #方法3.(将NK、CD8T、CD4T合并为一个亚群,将DC、Mono合并为Mono亚群)
    56. # 一个数据框
    57. (n=length(unique(sce@meta.data$seurat_clusters)))
    58. celltype=data.frame(ClusterID=0:(n-1),
    59. celltype='unkown')
    60. celltype[celltype$ClusterID %in% c(0,2,4,6),2]='T' #第0,2,4,6亚群为T细胞
    61. celltype[celltype$ClusterID %in% c(3),2]='B' #第3亚群为B细胞
    62. celltype[celltype$ClusterID %in% c(1,5),2]='Mono' #第1,5亚群为Mono
    63. celltype[celltype$ClusterID %in% c(7),2]='DC' #第7亚群为DC
    64. celltype[celltype$ClusterID %in% c(8),2]='Platelet' #第8亚群为Platelet
    65. sce@meta.data$celltype = "NA"
    66. for(i in 1:nrow(celltype)){
    67. sce@meta.data[which(sce@meta.data$seurat_clusters == celltype$ClusterID[i]),'celltype'] <- celltype$celltype[i]}
    68. table(sce@meta.data$celltype)
    69. phe=sce@meta.data
    70. #合并后作图
    71. DimPlot(sce,
    72. reduction = 'umap',
    73. group.by = 'celltype',
    74. label = TRUE, pt.size = 0.5) + NoLegend()
    75. head(sce@meta.data)
    76. table(sce@meta.data$cell_type,
    77. sce@meta.data$celltype)
    78. p1=DimPlot(sce,
    79. reduction = 'umap',
    80. group.by = 'celltype',
    81. label = TRUE, pt.size = 0.5) + NoLegend()
    82. p2=DotPlot(sce,
    83. group.by = 'celltype',
    84. features = unique(genes_to_check)) + RotatedAxis()
    85. p1+p2

    image.png