清明:天气晴朗,草木繁茂。
Seurat官网上详细的指导完全可以满足Seurat包初级使用。不过该网站是英文的,为了方便大家迅速上手,我来走一遍标准流程。我用的是Windows 10, R4.0。 我走的流程原网站地址
首先我们需要在自己的RStudio中安装Seurat包
install.packages('Seurat')
library('Seurat')
packageVersion("Seurat")
?Seurat
原参考页面中还使用了一些相关的R包,所以我们也需要一并安装上,如果你已经安装了这些包就跳过这一步
install.packages(c('dplyr','patchwork'))
安装好R包之后,我们要Load进来现在的工作环境
library(dplyr)
library(Seurat)
library(patchwork)
示例数据可以在官网下载:https://support.10xgenomics.com/ 读入的数据可以是一个矩阵,行代表基因,列代表细胞。
1.数据导入
list.files('pbmc3k_filtered_gene_bc_matrices/filtered_gene_bc_matrices/hg19')
pbmc.counts <- Read10X(data.dir = "pbmc3k_filtered_gene_bc_matrices/filtered_gene_bc_matrices/hg19")
创建Seurat对象
pbmc <- CreateSeuratObject(counts = pbmc.counts)
pbmc
str(pbmc)
数据集中测到的少于200个基因的细胞(min.features = 200)和少于3个细胞覆盖的基因(min.cells = 3)被过滤掉
pbmc <- CreateSeuratObject(counts = pbmc.counts, project = "pbmc3k", min.cells = 3, min.features = 200)
2.数据质控
质控的参数主要有两个: 1.每个细胞测到的unique feature数目(unique feature代表一个细胞检测到的基因的数目,可以根据数据的质量进行调整) 2.每个细胞检测到的线粒体基因的比例,理论上线粒体基因组与核基因组相比,只占很小一部分。所以线粒体基因表达比例过高的细胞会被过滤。
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
nFeature_RNA代表每个细胞测到的基因数目,nCount代表每个细胞测到所有基因的表达量之和,percent.mt代表测到的线粒体基因的比例。
# FeatureScatter is typically used to visualize feature-feature relationships, but can be used
# for anything calculated by the object, i.e. columns in object metadata, PC scores etc.
plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot1 + plot2
[图片上传失败...(image-5e2fdf-1615627815934)]
去除线粒体基因表达比例过高的细胞,和一些极值细胞。
pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
3.标准化
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
#鉴定细胞间表达量高变的基因(feature selection)
#这一步的目的是鉴定出细胞与细胞之间表达量相差很大的基因,用于后续鉴定细胞类型,
#我们使用默认参数,即“vst”方法选取2000个高变基因。
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
# Identify the 10 most highly variable genes
top10 <- head(VariableFeatures(pbmc), 10)
# plot variable features with and without labels
plot1 <- VariableFeaturePlot(pbmc)
plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)
plot1+plot2
4.细胞分类
1)分类前首先要对数据集进行降维
#Scaling the data
all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)
#Perform linear dimensional reduction
pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
#Examine and visualize PCA results a few different ways
print(pbmc[["pca"]], dims = 1:5, nfeatures = 5)
VizDimLoadings(pbmc, dims = 1:2, reduction = "pca")
DimPlot(pbmc, reduction = "pca")
DimHeatmap(pbmc, dims = 1, cells = 500, balanced = TRUE)
2)定义数据集的“维度”
这里我们需要选择出主成分的数目,用于后续细胞分类。这里定义的“维度”并不代表细胞类型的数目,而是对细胞分类时需要用到的一个参数。
#NOTE: This process can take a long time for big datasets, comment out for expediency.
#More approximate techniques such as those implemented in ElbowPlot() can be used to reduce computation time
pbmc <- JackStraw(pbmc, num.replicate = 100)
pbmc <- ScoreJackStraw(pbmc, dims = 1:20)
JackStrawPlot(pbmc, dims = 1:15)
ElbowPlot(pbmc)
JackStraw和Elbow都可以决定数据的“维度”。但是Elbow比较直观,我们选择Elbow结果进行解读。可以看到,主成分(PC)7到10之间,数据的标准差基本不在下降。所以我们需要在7到10之间进行选择,为了尊重官网的建议,我们选取10,即前10个主成分用于细胞的分类。
3) 细胞分类
选择不同的resolution值可以获得不同的cluster数目,值越大cluster数目越多,默认值是0.5.
pbmc <- FindNeighbors(pbmc, dims = 1:10)
pbmc <- FindClusters(pbmc, resolution = 0.5)
#这里我们设置了dims = 1:10 即选取前10个主成分来分类细胞。分类的结果如下,可以看到,细胞被分为9个类别。
#Look at cluster IDs of the first 5 cells
head(Idents(pbmc), 5)
4) 可视化分类结果
TSNE和UMAP两种方法经常被用于可视化细胞类别。
#UMAP
pbmc <- RunUMAP(pbmc, dims = 1:10, label = T)
head(pbmc@reductions$umap@cell.embeddings) # 提取UMAP坐标值。
#note that you can set `label = TRUE` or use the LabelClusters function to help label individual clusters
p1 <- DimPlot(pbmc, reduction = "umap")
#T-SNE
pbmc <- RunTSNE(pbmc, dims = 1:10)
head(pbmc@reductions$tsne@cell.embeddings)
p2 <- DimPlot(pbmc, reduction = "tsne")
p1 + p2
saveRDS(pbmc, file = "pbmc_tutorial.rds") #保存数据,用于后续个性化分析
5.提取各个细胞类型的marker gene
利用 FindMarkers 命令,可以找到找到各个细胞类型中与其他类别的差异表达基因,作为该细胞类型的生物学标记基因。其中ident.1参数设置待分析的细胞类别,min.pct表示该基因表达数目占该类细胞总数的比例
#find all markers of cluster 1
cluster1.markers <- FindMarkers(pbmc, ident.1 = 1, min.pct = 0.25)
head(cluster1.markers, n = 5)
#利用 DoHeatmap 命令可以可视化marker基因的表达
pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25)
?FindMarkers
top3 <- pbmc.markers %>% group_by(cluster) %>% top_n(n = 3, wt = avg_logFC)
DoHeatmap(pbmc, features = top3$gene) + NoLegend()
6.探索感兴趣的基因
Seurat提供了许多方法使我们能够方便的探索感兴趣的基因在各个细胞类型中的表达情况
VlnPlot(pbmc, features = c("MS4A1", "CD79A"))
#我们能够看到,MS4A1和CD79A两个基因在细胞群体3中特异性表达。
#you can plot raw counts as well
VlnPlot(pbmc, features = c("NKG7", "PF4"), slot = "counts", log = TRUE)
FeaturePlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A"))
#这种展示方法把基因表达量映射到UMAP结果中,同样可以直观的看到基因表达的特异性。
7.利用先验知识定义细胞类型
通过对比我们鉴定的marker gene与已发表的细胞类型特意的基因表达marker,可以定义我们划分出来的细胞类群。最后,给我们定义好的细胞类群加上名称
new.cluster.ids <- c("Naive CD4 T", "Memory CD4 T", "CD14+ Mono", "B", "CD8 T", "FCGR3A+ Mono", "NK", "DC", "Platelet")
names(new.cluster.ids) <- levels(pbmc)
pbmc <- RenameIdents(pbmc, new.cluster.ids)
DimPlot(pbmc, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()
参考原文
Seurat包------标准流程