1. #复习seurat流程
    2. rm(list = ls())
    3. library(Seurat)
    4. library(SeuratData)
    5. library(ggplot2)
    6. library(patchwork)
    7. library(dplyr)
    8. # Load the PBMC dataset
    9. # https://cf.10xgenomics.com/samples/cell/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz
    10. pbmc.data <- Read10X(data.dir = "filtered_gene_bc_matrices/hg19/")
    11. # Initialize the Seurat object with the raw (non-normalized data).
    12. pbmc <- CreateSeuratObject(counts = pbmc.data,
    13. project = "pbmc3k",
    14. min.cells = 3,
    15. min.features = 200)
    16. pbmc
    17. pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc,
    18. pattern = "^MT-") #计算属于给定特性集的所有计数的百分比
    19. #将QC度量可视化为小提琴图
    20. VlnPlot(pbmc,
    21. features = c("nFeature_RNA", "nCount_RNA", "percent.mt"),
    22. ncol = 3)
    23. #FeatureScatter通常用于可视化特征-特征关系,但也可以用于对象计算的任何内容,例如对象元数据的列,PC分数等。
    24. plot1 <- FeatureScatter(pbmc,
    25. feature1 = "nCount_RNA",
    26. feature2 = "percent.mt") #单细胞数据的散点图
    27. plot2 <- FeatureScatter(pbmc,
    28. feature1 = "nCount_RNA",
    29. feature2 = "nFeature_RNA") #单细胞数据的散点图
    30. plot1 + plot2
    31. pbmc <- subset(pbmc,
    32. subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
    33. pbmc <- NormalizeData(pbmc,
    34. normalization.method = "LogNormalize",
    35. scale.factor = 1e4)
    36. pbmc <- FindVariableFeatures(pbmc,
    37. selection.method = 'vst',
    38. nfeatures = 2000)
    39. #找出10个最易突变的基因
    40. top10 <- head(VariableFeatures(pbmc), 10)
    41. #绘制带有或不带有标签的变量特征
    42. plot1 <- VariableFeaturePlot(pbmc)
    43. plot2 <- LabelPoints(plot = plot1,
    44. points = top10,
    45. repel = TRUE)
    46. plot1 + plot2
    47. pbmc <- ScaleData(pbmc,
    48. vars.to.regress = "percent.mt")
    49. pbmc <- RunPCA(pbmc,
    50. features = VariableFeatures(object = pbmc))
    51. pbmc <- FindNeighbors(pbmc, dims = 1:10)
    52. pbmc <- FindClusters(pbmc, resolution = 0.5)
    53. #查看前5个单元格的集群id
    54. head(Idents(pbmc), 5)
    55. table(pbmc$seurat_clusters)
    56. pbmc <- RunUMAP(pbmc, dims = 1:10)
    57. #可以设置' label = TRUE '或使用LabelClusters功能来帮助标记单个集群
    58. DimPlot(pbmc, reduction = 'umap')
    59. VlnPlot(pbmc,
    60. features = c("MS4A1", "CD79A"))
    61. FeaturePlot(pbmc,
    62. features = c("MS4A1", "GNLY", "CD3E",
    63. "CD14", "FCER1A", "FCGR3A",
    64. "LYZ", "PPBP", "CD8A"))
    65. new.cluster.ids <- c("Naive CD4 T", "CD14+ Mono", "Memory CD4 T",
    66. "B", "CD8 T", "FCGR3A+ Mono", "NK", "DC", "Platelet")
    67. names(new.cluster.ids) <- levels(pbmc)
    68. pbmc <- RenameIdents(pbmc, new.cluster.ids)
    69. DimPlot(pbmc,
    70. reduction = 'umap',
    71. label = TRUE,
    72. pt.size = 0.5) + NoLegend()
    73. VlnPlot(pbmc,
    74. features = c("MS4A1", "CD79A"))
    75. FeaturePlot(pbmc,
    76. features = c("MS4A1", "CD79A"))
    77. RidgePlot(pbmc,
    78. features = c("MS4A1", "CD79A"),
    79. ncol = 1)
    80. features= c('IL7R', 'CCR7','CD14', 'LYZ', 'IL7R', 'S100A4',"MS4A1", "CD8A",'FOXP3',
    81. 'FCGR3A', 'MS4A7', 'GNLY', 'NKG7',
    82. 'FCER1A', 'CST3','PPBP')
    83. DotPlot(pbmc,
    84. features = unique(features)) + RotatedAxis()
    85. DoHeatmap(subset(pbmc ),
    86. features = features,
    87. size = 3)
    88. DoHeatmap(subset(pbmc, downsample = 100),
    89. features = features,
    90. size = 3)
    91. save(pbmc,file = 'basic.sce.pbmc.Rdata')

    image.png