Seurat
is an R package designed forQC, analysis, and exploration of single-cell RNA-seq data
. Seurat aims to enable users to identify and interpret sources of heterogeneity from single-cell transcriptomic measurements, and to integrate diverse types of single-cell data.
Improved and expanded methods for single-cell integration. Seurat v3 implements new methods to identify ‘anchors’ across diverse single-cell data types, in order to construct harmonized references, or to transfer information across experiments.
Improved methods for normalization. Seurat v3 includes support for sctransform, a new modeling approach for the normalization of single-cell data, described in a second preprint. Compared to standard log-normalization, sctransform effectively removes technically-driven variation while preserving biological heterogeneity.
An efficiently restructured Seurat object, with an emphasis on multi-modal data. We have carefully re-designed the structure of the Seurat object, with clearer documentation, and a flexible framework to easily switch between RNA, protein, cell hashing, batch-corrected / integrated, or imputed data.
安装并加载所需的R包构建Seurat对象标准的数据预处理流程数据的归一化鉴定高可变基因(特征选择)数据的标准化进行PCA线性降维选择PCA降维的维数用于后续的分析细胞的聚类分群非线性降维可视化(UMAP/tSNE)鉴定不同类群之间的差异表达基因Marker基因的可视化对聚类后的不同类群进行注释
目前最新版的Seurat包都是基于3.0版本的,可以直接通过install.packages命令进行安装。
1# 设置R包安装镜像
2options(repos="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
3install.packages('Seurat')
4library(Seurat)
5library(dplyr)
6library(patchwork)
7# 查看Seurat包的版本信息
8packageVersion("Seurat")
如果想安装早期2.0版本的Seurat包可以通过devtools包进行安装
1# 先安装devtools包
2install.packages('devtools')
3# 使用devtools安装指定版本的Seurat包
4devtools::install_version(package = 'Seurat', version = package_version('2.3.0'))
5library(Seurat)
本教程使用的是来自10X Genomics平台测序的外周血单核细胞(PBMC)数据集,这个数据集是用Illumina NextSeq 500平台进行测序的,里面包含了2,700个细胞的RNA-seq数据。
这个原始数据是用CellRanger软件进行基因表达定量处理的,最终生成了一个UMI的count矩阵。矩阵里的列是不同barcode指示的细胞,行是测序到的不同基因。接下来,我们将使用这个count矩阵创建Seurat对象。
1# Load the PBMC dataset
2pbmc.data <- Read10X(data.dir = "/home/dongwei/scRNA-seq/data/pbmc3k/filtered_gene_bc_matrices/hg19/")
3# Initialize the Seurat object with the raw (non-normalized data).
4# 初步过滤:每个细胞中至少检测到200个基因,每个基因至少在3个细胞中表达
5pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
6pbmc
7## An object of class Seurat
8## 13714 features across 2700 samples within 1 assay
9## Active assay: RNA (13714 features)
查看原始count矩阵的信息
1# Lets examine a few genes in the first thirty cells
2pbmc.data[c("CD3D", "TCL1A", "MS4A1"), 1:30]
3## 3 x 30 sparse Matrix of class "dgCMatrix"
4##
5## CD3D 4 . 10 . . 1 2 3 1 . . 2 7 1 . . 1 3 . 2 3 . . . . . 3 4 1 5
6## TCL1A . . . . . . . . 1 . . . . . . . . . . . . 1 . . . . . . . .
7## MS4A1 . 6 . . . . . . 1 1 1 . . . . . . . . . 36 1 2 . . 2 . . . .
表达矩阵中的"."表示某一基因在某一细胞中没有检测到表达,因为scRNA-seq的表达矩阵中会存在很多表达量为0的数据,Seurat会使用稀疏矩阵进行数据的存储,可以有效减少存储的内存。
Seurat可以对原始count矩阵进行质量控制,选择特定的质控条件进行细胞和基因的过滤,质控的一般指标包括:
每个细胞中能检测到的基因数目:
1)低质量的细胞或空的droplets中能检测到的基因比较少
2)细胞doublets 或者 multiplets 中会检测到比较高数目的基因count数
每个细胞内能检测到的分子数
1)细胞内检测到的线粒体基因的比例
2)低质量/死细胞中通常会有比较高的线粒体污染
在Seurat中可以使用PercentageFeatureSet
函数计算每个细胞中线粒体的含量:在人类参考基因中线粒体基因是以“MT-”开头的,而在小鼠中是以“mt-”开头的。
1pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
2# Show QC metrics for the first 5 cells
3head(pbmc@meta.data, 5)
4orig.ident nCount_RNA nFeature_RNA percent.mt
5AAACATACAACCAC pbmc3k 2419 779 3.0177759
6AAACATTGAGCTAC pbmc3k 4903 1352 3.7935958
7AAACATTGATCAGC pbmc3k 3147 1129 0.8897363
8AAACCGTGCTTCCG pbmc3k 2639 960 1.7430845
9AAACCGTGTATGCG pbmc3k 980 521 1.2244898
可视化QC指标,并用它们来过滤细胞:
1)将unique基因count数超过2500,或者小于200的细胞过滤掉
2)把线粒体含量超过5%以上的细胞过滤掉
1# Visualize QC metrics as a violin plot
2VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
我们还可以使用FeatureScatter
函数来对不同特征-特征之间的关系进行可视化
1plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")
2plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
3plot1 + plot2
根据QC指标进行细胞和基因的过滤
1pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
默认情况下,Seurat使用global-scaling的归一化方法,称为“LogNormalize”,这种方法是利用总的表达量对每个细胞里的基因表达值进行归一化,乘以一个scale factor(默认值是10000),再用log转换一下。归一化后的数据存放在pbmc[["RNA"]]@data里。
1pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
Seurat使用FindVariableFeatures
函数鉴定高可变基因,这些基因在PBMC不同细胞之间的表达量差异很大(在一些细胞中高表达,在另一些细胞中低表达)。默认情况下,会返回2,000个高可变基因用于下游的分析,如PCA等。
1pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
2
3# Identify the 10 most highly variable genes
4top10 <- head(VariableFeatures(pbmc), 10)
5
6# plot variable features with and without labels
7plot1 <- VariableFeaturePlot(pbmc)
8plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)
9plot1 + plot2
Seurat使用ScaleData
函数对归一化后的count矩阵进行一个线性的变换(“scaling”),将数据进行标准化:
1)shifting每个基因的表达,使细胞间的平均表达为0
2)scaling每个基因的表达,使细胞间的差异为1
ScaleData默认对之前鉴定到的2000个高可变基因进行标准化,也可以通过vars.to.regress参数指定其他的变量对数据进行标准化,表达矩阵进行scaling后,其结果存储在pbmc[["RNA"]]@scale.data中。
1pbmc <- ScaleData(pbmc)
2all.genes <- rownames(pbmc)
3pbmc <- ScaleData(pbmc, features = all.genes)
4pbmc <- ScaleData(pbmc, vars.to.regress = "percent.mt")
Seurat使用RunPCA
函数对标准化后的表达矩阵进行PCA降维处理,默认情况下,只对前面选出的2000个高可变基因进行线性降维,也可以通过feature参数指定想要降维的数据集。
1pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
2# Examine and visualize PCA results a few different ways
3print(pbmc[["pca"]], dims = 1:5, nfeatures = 5)
4## PC_ 1
5## Positive: CST3, TYROBP, LST1, AIF1, FTL
6## Negative: MALAT1, LTB, IL32, IL7R, CD2
7## PC_ 2
8## Positive: CD79A, MS4A1, TCL1A, HLA-DQA1, HLA-DQB1
9## Negative: NKG7, PRF1, CST7, GZMB, GZMA
10## PC_ 3
11## Positive: HLA-DQA1, CD79A, CD79B, HLA-DQB1, HLA-DPB1
12## Negative: PPBP, PF4, SDPR, SPARC, GNG11
13## PC_ 4
14## Positive: HLA-DQA1, CD79B, CD79A, MS4A1, HLA-DQB1
15## Negative: VIM, IL7R, S100A6, IL32, S100A8
16## PC_ 5
17## Positive: GZMB, NKG7, S100A8, FGFBP2, GNLY
18## Negative: LTB, IL7R, CKB, VIM, MS4A7
Seurat可以使用VizDimReduction
, DimPlot
, 和DimHeatmap
函数对PCA的结果进行可视化
1VizDimLoadings(pbmc, dims = 1:2, reduction = "pca")
1DimPlot(pbmc, reduction = "pca")
1DimHeatmap(pbmc, dims = 1, cells = 500, balanced = TRUE)
1DimHeatmap(pbmc, dims = 1:15, cells = 500, balanced = TRUE)
Seurat可以使用两种方法确定PCA降维的维数用于后续的聚类分析:
使用JackStrawPlot函数
使用JackStraw
函数计算每个PC的P值的分布,显著的PC会有较低的p-value
1pbmc <- JackStraw(pbmc, num.replicate = 100)
2pbmc <- ScoreJackStraw(pbmc, dims = 1:20)
3# 使用JackStrawPlot函数进行可视化
4JackStrawPlot(pbmc, dims = 1:15)
5
使用ElbowPlot函数
使用ElbowPlot函数查看在哪一个PC处出现平滑的挂点:
1ElbowPlot(pbmc)
Seurat使用图聚类的方法对降维后的表达数据进行聚类分群。
Briefly, these methods embed cells in a graph structure - for example a K-nearest neighbor (KNN) graph, with edges drawn between cells with similar feature expression patterns, and then attempt to partition this graph into highly interconnected ‘quasi-cliques’ or ‘communities’.
1pbmc <- FindNeighbors(pbmc, dims = 1:10)
2pbmc <- FindClusters(pbmc, resolution = 0.5)
3## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
4##
5## Number of nodes: 2638
6## Number of edges: 96033
7##
8## Running Louvain algorithm...
9## Maximum modularity in 10 random starts: 0.8720
10## Number of communities: 9
11## Elapsed time: 0 seconds
12# Look at cluster IDs of the first 5 cells
13head(Idents(pbmc), 5)
14## AAACATACAACCAC AAACATTGAGCTAC AAACATTGATCAGC AAACCGTGCTTCCG AAACCGTGTATGCG
15## 1 3 1 2 6
16## Levels: 0 1 2 3 4 5 6 7 8
Seurat offers several non-linear dimensional reduction techniques, such as tSNE and UMAP, to visualize and explore these datasets. The goal of these algorithms is to learn the underlying manifold of the data in order to place similar cells together in low-dimensional space.
1# If you haven't installed UMAP, you can do so via reticulate::py_install(packages = 'umap-learn')
2# UMAP降维可视化
3pbmc <- RunUMAP(pbmc, dims = 1:10)
4# note that you can set `label = TRUE` or use the LabelClusters function to help label individual clusters
5DimPlot(pbmc, reduction = "umap")
1#tSNE降维可视化
2pbmc <- RunTSNE(pbmc, dims = 1:10)
3DimPlot(pbmc, reduction = "tsne", label = TRUE)
4
Seurat使用FindMarkers
和FindAllMarkers
函数进行差异表达基因的筛选,可以通过test.use参数指定不同的差异表达基因筛选的方法。
1# find all markers of cluster 1
2cluster1.markers <- FindMarkers(pbmc, ident.1 = 1, min.pct = 0.25)
3head(cluster1.markers, n = 5)
4p_val avg_logFC pct.1 pct.2 p_val_adj
5IL32 0 0.8373872 0.948 0.464 0
6LTB 0 0.8921170 0.981 0.642 0
7CD3D 0 0.6436286 0.919 0.431 0
8IL7R 0 0.8147082 0.747 0.325 0
9LDHB 0 0.6253110 0.950 0.613 0
10
11# find all markers distinguishing cluster 5 from clusters 0 and 3
12cluster5.markers <- FindMarkers(pbmc, ident.1 = 5, ident.2 = c(0, 3), min.pct = 0.25)
13head(cluster5.markers, n = 5)
14p_val avg_logFC pct.1 pct.2 p_val_adj
15FCGR3A 0 2.963144 0.975 0.037 0
16IFITM3 0 2.698187 0.975 0.046 0
17CFD 0 2.362381 0.938 0.037 0
18CD68 0 2.087366 0.926 0.036 0
19RP11-290F20.3 0 1.886288 0.840 0.016 0
20
21# find markers for every cluster compared to all remaining cells, report only the positive ones
22pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)
23pbmc.markers %>% group_by(cluster) %>% top_n(n = 2, wt = avg_logFC)
24p_val avg_logFC pct.1 pct.2 p_val_adj cluster gene
250 0.7300635 0.901 0.594 0 0 LDHB
260 0.9219135 0.436 0.110 0 0 CCR7
270 0.8921170 0.981 0.642 0 1 LTB
280 0.8586034 0.422 0.110 0 1 AQP3
290 3.8608733 0.996 0.215 0 2 S100A9
300 3.7966403 0.975 0.121 0 2 S100A8
310 2.9875833 0.936 0.041 0 3 CD79A
320 2.4894932 0.622 0.022 0 3 TCL1A
330 2.1220555 0.985 0.240 0 4 CCL5
340 2.0461687 0.587 0.059 0 4 GZMK
350 2.2954931 0.975 0.134 0 5 FCGR3A
360 2.1388125 1.000 0.315 0 5 LST1
370 3.3462278 0.961 0.068 0 6 GZMB
380 3.6898996 0.961 0.131 0 6 GNLY
390 2.6832771 0.812 0.011 0 7 FCER1A
400 1.9924275 1.000 0.513 0 7 HLA-DPB1
410 5.0207262 1.000 0.010 0 8 PF4
420 5.9443347 1.000 0.024 0 8 PPBP
43
Seurat可以使用VlnPlot,FeaturePlot,RidgePlot,DotPlot和DoHeatmap
等函数对marker基因的表达进行可视化
1VlnPlot(pbmc, features = c("MS4A1", "CD79A"))
1# you can plot raw counts as well
2VlnPlot(pbmc, features = c("NKG7", "PF4"), slot = "counts", log = TRUE)
1FeaturePlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A"))
RidgePlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A"))
DotPlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A"))
top10 <- pbmc.markers %>% group_by(cluster) %>% top_n(n = 10, wt = avg_logFC)
DoHeatmap(pbmc, features = top10$gene) + NoLegend()
我们可以基于已有的生物学知识,根据一些特异的marker基因不细胞类群进行注释
1Cluster ID Markers Cell Type
20 IL7R, CCR7 Naive CD4+ T
31 IL7R, S100A4 Memory CD4+
42 CD14, LYZ CD14+ Mono
53 MS4A1 B
64 CD8A CD8+ T
75 FCGR3A, MS4A7 FCGR3A+ Mono
86 GNLY, NKG7 NK
97 FCER1A, CST3 DC
108 PPBP Platelet
以上是根据PBMC中不同细胞类群的一些marker基因进行细胞分群注释的结果
1# 根据marker基因进行分群注释
2new.cluster.ids <- c("Naive CD4 T", "Memory CD4 T", "CD14+ Mono", "B", "CD8 T", "FCGR3A+ Mono", "NK", "DC", "Platelet")
3names(new.cluster.ids) <- levels(pbmc)
4# 细胞分群的重命名
5pbmc <- RenameIdents(pbmc, new.cluster.ids)
6DimPlot(pbmc, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()
1# 保存分析的结果
2saveRDS(pbmc, file = "../output/pbmc3k_final.rds")
1sessionInfo()
2R version 3.6.0 (2019-04-26)
3Platform: x86_64-redhat-linux-gnu (64-bit)
4Running under: CentOS Linux 7 (Core)
5
6Matrix products: default
7BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
8
9locale:
10 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
11 [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
12 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
13 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
14 [9] LC_ADDRESS=C LC_TELEPHONE=C
15[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
16
17attached base packages:
18 [1] splines parallel stats4 grid stats graphics grDevices
19 [8] utils datasets methods base
20
21other attached packages:
22 [1] loomR_0.2.1.9000 hdf5r_1.3.1
23 [3] R6_2.4.0 mclust_5.4.5
24 [5] garnett_0.1.16 org.Hs.eg.db_3.8.2
25 [7] AnnotationDbi_1.46.0 pagedown_0.9.1
26 [9] devtools_2.1.0 usethis_1.5.1
27[11] celaref_1.2.0 scran_1.12.1
28[13] scRNAseq_1.10.0 FLOWMAPR_1.2.0
29[15] Seurat_3.1.4.9902 sctransform_0.2.0
30[17] patchwork_0.0.1 cowplot_1.0.0
31[19] DT_0.12 RColorBrewer_1.1-2
32[21] shinydashboard_0.7.1 ggsci_2.9
33[23] shiny_1.3.2 stxBrain.SeuratData_0.1.1
34[25] pbmcsca.SeuratData_3.0.0 panc8.SeuratData_3.0.2
35[27] SeuratData_0.2.1 doParallel_1.0.14
36[29] iterators_1.0.12 binless_0.15.1
37[31] RcppEigen_0.3.3.5.0 foreach_1.4.7
38[33] scales_1.0.0 dplyr_0.8.3
39[35] pagoda2_0.1.0 harmony_1.0
40[37] Rcpp_1.0.2 conos_1.1.2
Seurat对象解读:
思维导图链接:https://mubu.com/doc/6kVetytOc27
参考来源:https://satijalab.org/seurat/v3.1/pbmc3k_tutorial.html