劉小澤寫于2020.7.18
為何取名叫“交響樂”?因為單細(xì)胞分析就像一個大樂團(tuán)克锣,需要各個流程的協(xié)同配合
單細(xì)胞交響樂1-常用的數(shù)據(jù)結(jié)構(gòu)SingleCellExperiment
單細(xì)胞交響樂2-scRNAseq從實驗到下游簡介
單細(xì)胞交響樂3-細(xì)胞質(zhì)控
單細(xì)胞交響樂4-歸一化
單細(xì)胞交響樂5-挑選高變化基因
單細(xì)胞交響樂6-降維
單細(xì)胞交響樂7-聚類分群
單細(xì)胞交響樂8-marker基因檢測
單細(xì)胞交響樂9-細(xì)胞類型注釋
單細(xì)胞交響樂9-細(xì)胞類型注釋
單細(xì)胞交響樂10-數(shù)據(jù)集整合后的批次矯正
單細(xì)胞交響樂11-多樣本間差異分析
單細(xì)胞交響樂12-檢測Doublet
單細(xì)胞交響樂13-細(xì)胞周期推斷
單細(xì)胞交響樂14-細(xì)胞軌跡推斷
單細(xì)胞交響樂15-scRNA與蛋白豐度信息結(jié)合
單細(xì)胞交響樂16-處理大型數(shù)據(jù)
1 前言
這部分內(nèi)容是來自Seurat:https://satijalab.org/seurat/v3.1/conversion_vignette.html
單細(xì)胞數(shù)據(jù)格式目前有這么幾大派:
- Bioconductor主導(dǎo)的SingleCellExperiment數(shù)據(jù)格式:例如scran、scater号醉、monocle(盡管它的對象不直接使用SingleCellExperiment夷狰,但靈感來源于SingleCellExperiment,并且操作也是類似的)
- Seurat:SeuratObject格式
- scanpy:AnnData格式
這么一來见擦,很多分析流程就被固定在某個包中了钉汗,比如使用Seurat會一用到底,也不會去學(xué)習(xí)scater或其他R包了鲤屡,但也許就錯過了其他R包好用的一些功能(比如我感覺scater
的uniquifyFeatureNames
就很好用)
既然有需求损痰,就有開發(fā)者添加功能 ,這里Davis McCarthy 和Alex Wolf就為Seurat添加了和其他數(shù)據(jù)類型轉(zhuǎn)換的函數(shù)
2 Seurat與SingleCellExperiment的相互轉(zhuǎn)換
library(scater)
# devtools::install_github(repo = "satijalab/seurat", ref = "loom")
library(loomR)
library(Seurat)
library(patchwork)
2.1 Seurat轉(zhuǎn)SingleCellExperiment
# 使用Seurat內(nèi)置數(shù)據(jù)
data("pbmc_small")
> pbmc_small
An object of class Seurat
230 features across 80 samples within 1 assay
Active assay: RNA (230 features)
2 dimensional reductions calculated: pca, tsne
# 一個函數(shù)即可
pbmc.sce <- as.SingleCellExperiment(pbmc_small)
> pbmc.sce
class: SingleCellExperiment
dim: 230 80
metadata(0):
assays(2): counts logcounts
rownames(230): MS4A1 CD79B ... SPON2 S100B
rowData names(5): vst.mean vst.variance
vst.variance.expected
vst.variance.standardized vst.variable
colnames(80): ATGCCAGAACGACT CATGGCCTGTGCAT ...
GGAACACTTCAGAC CTTGATTGATCTTC
colData names(8): orig.ident nCount_RNA ...
RNA_snn_res.1 ident
reducedDimNames(2): PCA TSNE
spikeNames(0):
altExpNames(0):
# 接下來就是scater的操作了
p1 <- plotExpression(pbmc.sce, features = "MS4A1", x = "ident") + theme(axis.text.x = element_text(angle = 45,
hjust = 1))
p2 <- plotPCA(pbmc.sce, colour_by = "ident")
p1 + p2
2.2 SingleCellExperiment轉(zhuǎn)Seurat
# 導(dǎo)入sce對象(https://scrnaseq-public-datasets.s3.amazonaws.com/scater-objects/manno_human.rds)
manno <- readRDS(file = "manno_human.rds")
> manno
class: SingleCellExperiment
dim: 20560 4029
metadata(0):
assays(2): counts logcounts
rownames(20560): 'MARC1' 'MARC2' ... ZZEF1 ZZZ3
rowData names(10): feature_symbol
is_feature_control ... total_counts
log10_total_counts
colnames(4029): 1772122_301_C02 1772122_180_E05
... 1772116-063_G02 1772099-259_H03
colData names(34): Species cell_type1 ...
pct_counts_ERCC is_cell_control
reducedDimNames(0):
altExpNames(0):
manno <- runPCA(manno)
# 轉(zhuǎn)為seurat對象
manno.seurat <- as.Seurat(manno, counts = "counts", data = "logcounts")
# 看下這個函數(shù)
# as.Seurat(
# x,
# counts = "counts",
# data = "logcounts",
# assay = "RNA",
# project = "SingleCellExperiment",
# ...
# )
# 既然有默認(rèn)參數(shù)酒来,因此直接按下面這么寫就可以:
manno.seurat <- as.Seurat(manno)
> manno.seurat
An object of class Seurat
20560 features across 4029 samples within 1 assay
Active assay: RNA (20560 features)
1 dimensional reduction calculated: PCA
Idents(manno.seurat) <- "cell_type1"
p1 <- DimPlot(manno.seurat, reduction = "PCA", group.by = "Source") + NoLegend()
p2 <- RidgePlot(manno.seurat, features = "ACTB", group.by = "Source")
p1 + p2
3 Seurat與loom的相互轉(zhuǎn)換
還記得上次在單細(xì)胞交響樂16-處理大型數(shù)據(jù)中說到:處理大型數(shù)據(jù)遇到內(nèi)存不足時卢未,可以使用這個HDF5Array
R包(類似的還有 bigmemory
, matter
),它會將底層數(shù)據(jù)做成HDF5格式堰汉,用硬盤空間來存儲數(shù)據(jù)辽社,必要時再調(diào)用一部分?jǐn)?shù)據(jù)到內(nèi)存。loom格式就是處理HDF5使用的
3.1 Seurat轉(zhuǎn)為loom
pbmc.loom <- as.loom(pbmc, filename = "pbmc3k.loom", verbose = FALSE)
pbmc.loom
## Class: loom
## Filename: /__w/1/s/output/pbmc3k.loom
## Access type: H5F_ACC_RDWR
## Attributes: version, chunks, LOOM_SPEC_VERSION, assay, last_modified
## Listing:
## name obj_type dataset.dims dataset.type_class
## col_attrs H5I_GROUP <NA> <NA>
## col_graphs H5I_GROUP <NA> <NA>
## layers H5I_GROUP <NA> <NA>
## matrix H5I_DATASET 2638 x 13714 H5T_FLOAT
## row_attrs H5I_GROUP <NA> <NA>
## row_graphs H5I_GROUP <NA> <NA>
# 最后使用完要記得關(guān)上loom對象
pbmc.loom$close_all()
3.2 loom轉(zhuǎn)為Seurat
首先讀惹萄肌:用 loomR 的connect
l6.immune <- connect(filename = "../data/l6_r1_immune_cells.loom", mode = "r")
l6.immune
## Class: loom
## Filename: /__w/1/s/data/l6_r1_immune_cells.loom
## Access type: H5F_ACC_RDONLY
## Attributes: CreationDate, last_modified
## Listing:
## name obj_type dataset.dims dataset.type_class
## col_attrs H5I_GROUP <NA> <NA>
## col_graphs H5I_GROUP <NA> <NA>
## layers H5I_GROUP <NA> <NA>
## matrix H5I_DATASET 14908 x 27998 H5T_FLOAT
## row_attrs H5I_GROUP <NA> <NA>
## row_graphs H5I_GROUP <NA> <NA>
然后轉(zhuǎn)換
l6.seurat <- as.Seurat(l6.immune)
VlnPlot(l6.seurat, features = c("Sparc", "Ftl1", "Junb", "Ccl4"), ncol = 2, pt.size = 0.1)
最后處理完滴铅,記得關(guān)閉loom文件
l6.immune$close_all()
3.3 補(bǔ)充
如果使用Seurat V2,還有一個自帶的函數(shù)Convert
data("pbmc_small")
pbmc_small
pfile <- Convert(from = pbmc_small, to = "loom", filename = "pbmc_small.loom",
display.progress = FALSE)
pfile
## Class: loom
## Filename: /home/paul/Documents/Satija/pbmc_small.loom
## Access type: H5F_ACC_RDWR
## Attributes: version, chunks
## Listing:
## name obj_type dataset.dims dataset.type_class
## col_attrs H5I_GROUP <NA> <NA>
## col_graphs H5I_GROUP <NA> <NA>
## layers H5I_GROUP <NA> <NA>
## matrix H5I_DATASET 80 x 230 H5T_FLOAT
## row_attrs H5I_GROUP <NA> <NA>
## row_graphs H5I_GROUP <NA> <NA>
4 Scanpy轉(zhuǎn)Seurat
Seurat有一個函數(shù)ReadH5AD
可以讀取AnnData的H5AD文件
pbmc3k <- ReadH5AD(file = "pbmc3k.h5ad")
# 利用Seurat操作
Idents(pbmc3k) <- "louvain"
p1 <- DimPlot(pbmc3k, label = TRUE)
p2 <- VlnPlot(pbmc3k, features = c("CST3", "NKG7", "PPBP"), combine = FALSE)
wrap_plots(c(list(p1), p2), ncol = 2) & NoLegend()
目前還不能直接將Seurat寫成H5AD文件就乓,因此不能之間將Seurat轉(zhuǎn)為Scanpy汉匙;但是可以將loom文件作為橋梁實現(xiàn)Seurat轉(zhuǎn)Scanpy,例如Scanpy
有一個函數(shù)scanpy.read_loom()
參考:https://scanpy.readthedocs.io/en/stable/api/scanpy.read_loom.html
歡迎關(guān)注我們的公眾號~_~
我們是兩個農(nóng)轉(zhuǎn)生信的小碩生蚁,打造生信星球噩翠,想讓它成為一個不拽術(shù)語、通俗易懂的生信知識平臺邦投。需要幫助或提出意見請后臺留言或發(fā)送郵件到jieandze1314@gmail.com