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. DimPlot(pbmc,
    11. reduction = 'umap',
    12. label = TRUE, pt.size = 0.5) + NoLegend()
    13. FeaturePlot(pbmc,
    14. features = c("CD4"))
    15. sce=pbmc
    16. Idents(sce)
    17. levels(sce)
    18. head(sce@meta.data)
    19. genes_to_check = c('PTPRC', 'CD3D', 'CD3E',
    20. 'CD4','IL7R','NKG7','CD8A')
    21. DotPlot(sce,
    22. group.by = 'seurat_clusters',
    23. features = unique(genes_to_check)) + RotatedAxis()
    24. DotPlot(sce, # group.by = 'seurat_clusters',
    25. features = unique(genes_to_check)) + RotatedAxis()
    26. p1=DimPlot(sce,
    27. reduction = 'umap',
    28. group.by = 'seurat_clusters',
    29. label = TRUE, pt.size = 0.5) + NoLegend()
    30. p2=DotPlot(sce,
    31. group.by = 'seurat_clusters',
    32. features = unique(genes_to_check)) + RotatedAxis()
    33. p1+p2
    34. #在R里面取子集方法:逻辑值,坐标,名字
    35. #取子集方法1:
    36. cd4_sce1 = sce[,sce@meta.data$seurat_clusters %in% c(0,2)]
    37. #取子集方法2:
    38. cd4_sce2 = sce[, Idents(sce) %in% c("Naive CD4 T","Memory CD4 T")]
    39. #subset 函数也可以,手写函数也行
    40. cd4_sce1
    41. cd4_sce2
    42. #代码不要变动
    43. sce=cd4_sce1
    44. sce <- NormalizeData(sce,
    45. normalization.method = "LogNormalize",
    46. scale.factor = 1e4)
    47. sce <- FindVariableFeatures(sce,
    48. selection.method = 'vst',
    49. nfeatures = 2000)
    50. sce <- ScaleData(sce,
    51. vars.to.regress = "percent.mt")
    52. sce <- RunPCA(sce,
    53. features = VariableFeatures(object = sce))
    54. sce <- FindNeighbors(sce,
    55. dims = 1:10)
    56. sce <- FindClusters(sce,
    57. resolution = 1 )
    58. # Look at cluster IDs of the first 5 cells
    59. head(Idents(sce), 5)
    60. table(sce$seurat_clusters)
    61. sce <- RunUMAP(sce,
    62. dims = 1:10)
    63. DimPlot(sce,
    64. reduction = 'umap')
    65. genes_to_check = c('PTPRC', 'CD3D', 'CD3E', 'FOXP3',
    66. 'CD4','IL7R','NKG7','CD8A')
    67. DotPlot(sce,
    68. group.by = 'seurat_clusters',
    69. features = unique(genes_to_check)) + RotatedAxis()
    70. # 亚群水平
    71. p1=DimPlot(sce,
    72. reduction = 'umap',
    73. group.by = 'seurat_clusters',
    74. label = TRUE, pt.size = 0.5) + NoLegend()
    75. p2=DotPlot(sce,
    76. group.by = 'seurat_clusters',
    77. features = unique(genes_to_check)) + RotatedAxis()
    78. p1+p2
    79. save(sce,file = 'sce.cd4.subset.Rdata')
    80. load(file = 'sce.cd4.subset.Rdata')
    81. #先执行不同resolution 下的分群
    82. library(Seurat)
    83. library(clustree)
    84. sce <- FindClusters(
    85. object = sce,
    86. resolution = c(seq(.1,1.6,.2))
    87. )
    88. clustree(sce@meta.data, prefix = "RNA_snn_res.")

    image.png
    image.png