Seurat是单细胞分析经常使用的分析包。seurat对象的处理是分析的一个难点,这里我根据我自己的理解整理了下常用的seurat对象处理的一些操作,有不足或者错误的地方希望大家指正~
首先是从10X数据或者其他数据生成一个seurat对象(这里直接拷贝的官网的教程https://satijalab.org/seurat/essential_commands.html)也可以是其他的代码。
pbmc.counts <- Read10X(data.dir = "~/Downloads/pbmc3k/filtered_gene_bc_matrices/hg19/")
pbmc <- CreateSeuratObject(counts = pbmc.counts)
首先在Rstudio中运行帮助?seurat
Each Seurat object has a number of slots which store information. Key slots to access are listed below.
Slots:
raw.data
The raw project data
data
The normalized expression matrix (log-scale)
scale.data
scaled (default is z-scoring each gene) expression matrix; used for dimmensional reduction and heatmap visualization
var.genes
Vector of genes exhibiting high variance across single cells
is.expr
Expression threshold to determine if a gene is expressed (0 by default)
ident
THe 'identity class' for each cell
meta.data
Contains meta-information about each cell, starting with number of genes detected (nGene) and the original identity class (orig.ident); more information is added usingAddMetaData
project.name
Name of the project (for record keeping)
dr
List of stored dimmensional reductions; named by technique
assay
List of additional assays for multimodal analysis; named by technique
hvg.info
The output of the mean/variability analysis for all genes
imputed
Matrix of imputed gene scores
cell.names
Names of all single cells (column names of the expression matrix)
cluster.tree
List where the first element is a phylo object containing the phylogenetic tree relating different identity classes
snn
Spare matrix object representation of the SNN graph
calc.params
Named list to store all calculation-related parameter choices
kmeans
Stores output of gene-based clustering from DoKMeans
spatial
Stores internal data and calculations for spatial mapping of single cells
misc
Miscellaneous spot to store any data alongisde the object (for example, gene lists)
version
Version of package used in object creation
但在实际的分析中没有这么多变量。大家可以用@
或者$
来获取有的变量。
上面我在后面分析用到的是
orig.ident
和group
还有seurat_clusters
变量,这里分别存储的是样本名,分组以及cluster信息。
1、基本信息获取
先来直接输出seurat对象看看:
> pbmc # 测试数据,进行了PCA和UMAP分析
An object of class Seurat
25540 features across 46636 samples within 2 assays
Active assay: integrated (2000 features, 2000 variable features)
1 other assay present: RNA
2 dimensional reductions calculated: pca, umap
一些可以查询和提取的基本信息:
colnames(x = pbmc) # 各个细胞的编号
Cells(pbmc) # 和上面的一样,各个细胞的编号
rownames(x = pbmc) # 基因名
ncol(x = pbmc) #列数
nrow(x = pbmc) #行数
dim(pbmc) # 行数和列数
# 获取细胞类型
Idents(object = pbmc)
levels(pbmc)
table(Idents(pbmc)) # 获取每个细胞类型的细胞数目表格
# 其他的一些细胞类型的处理
# Stash cell identity classes
pbmc[["old.ident"]] <- Idents(object = pbmc)
pbmc <- StashIdent(object = pbmc, save.name = "old.ident")
# Set identity classes
Idents(object = pbmc) <- "CD4 T cells"
Idents(object = pbmc, cells = 1:10) <- "CD4 T cells"
# Set identity classes to an existing column in meta data
Idents(object = pbmc, cells = 1:10) <- "orig.ident"
Idents(object = pbmc) <- "orig.ident"
# Rename identity classes
pbmc <- RenameIdents(object = pbmc, `CD4 T cells` = "T Helper cells")
我们可以直接根据
levels(pbmc)
获取所有的细胞类型2、subset函数筛选
# 筛选某一种或多种细胞类型
subset(x = pbmc, idents = "B cells")
subset(x = pbmc, idents = c("CD4 T cells", "CD8 T cells"), invert = TRUE)
# 还可以根据表达量的值来进行筛选
# Subset on the expression level of a gene/feature
subset(x = pbmc, subset = MS4A1 > 3)
# Subset on a combination of criteria
subset(x = pbmc, subset = MS4A1 > 3 & PC1 > 5)
subset(x = pbmc, subset = MS4A1 > 3, idents = "B cells")
# Subset on a value in the object meta data
subset(x = pbmc, subset = orig.ident == "Replicate1")
# Downsample the number of cells per identity class
subset(x = pbmc, downsample = 100)
#筛选基因
subset(x = pbmc_small, features = VariableFeatures(object = pbmc_small))
# 也可以使用数组的形式提取
pbmc_small_sub = pbmc_small[,pbmc_small@meta.data$seurat_clusters %in% c(0,2)]
pbmc_small_sub = pbmc_small[, Idents(pbmc_small) %in% c( "T cell" , "B cell" )] # 需要此时的pbmc_small数据Idents(pbmc_small)为细胞类型
3、数据获取
# 读取保存在@meta.data中的数据
# View metadata data frame, stored in object@meta.data
pbmc[[]]
# 提取某一类型的数据
# Retrieve specific values from the metadata
pbmc$nCount_RNA
pbmc[[c("percent.mito", "nFeature_RNA")]]
# 增加分组信息
# Add metadata, see ?AddMetaData
random_group_labels <- sample(x = c("g1", "g2"), size = ncol(x = pbmc), replace = TRUE)
pbmc$groups <- random_group_labels
# 使用GetAssayData函数获取'counts', 'data'和'scale.data'信息
# Retrieve or set data in an expression matrix ('counts', 'data', and 'scale.data')
GetAssayData(object = pbmc, slot = "counts")
pbmc <- SetAssayData(object = pbmc, slot = "scale.data", new.data = new.data)
# Get cell embeddings and feature loadings
Embeddings(object = pbmc, reduction = "pca")
Loadings(object = pbmc, reduction = "pca")
Loadings(object = pbmc, reduction = "pca", projected = TRUE)
# FetchData can pull anything from expression matrices, cell embeddings, or metadata
FetchData(object = pbmc, vars = c("PC_1", "percent.mito", "MS4A1"))
因为不同版本中的变量可能会有变化,这里的FetchData的前缀可以从Key(pbmc)
获取,比如
4、计算
# 获取平均表达量
Idents(scRNA_data) <- "seurat_clusters" # 这一步可以指定要计算哪一个分组的平均表达量,可以选择细胞类型("CellType")cluster("seurat_clusters")或者是样本类型("orig.ident"),要注意这里的变量名称不一定正确,要根据数据中的具体变量来指定
AverageExp <- AverageExpression(scRNA_data)
expr <- AverageExp$RNA
# 增加分组前缀,这里增加的是"Cluster"
for(i in 1:ncol(expr)){colnames(expr)[i] = paste("Cluster", colnames(expr)[i],sep = "")}
5、数据替换/修改
有时候需要对seurat对象的数据进行替换或修改
library(Seurat)
# 替换cell ID名称,-1改成_1
new_obj <- RenameCells(obj, new.names=gsub("-1", "_1", colnames(obj)))
# 如果有多个样本,筛选细胞
barcode_names <- obj$orig.ident
sampleA_barcode_name = attr(x[x=="sampleA"],"names")
一些参考资料:
1、https://satijalab.org/seurat/essential_commands.html
2、https://satijalab.org/seurat/v3.0/interaction_vignette.html
3、https://www.jianshu.com/p/d43f16bdfed9