Seurat官網(wǎng):https://satijalab.org/seurat/articles/conversion_vignette.html
Seurat objects, SingleCellExperiment objects和anndata objects之間的轉(zhuǎn)換集漾。
library(scater)
library(Seurat)
library(SeuratData)
#remotes::install_github("mojaveazure/seurat-disk")
library(SeuratDisk)
library(patchwork)
1. Converting to/from SingleCellExperiment
- 從Seurat對(duì)象轉(zhuǎn)換為SingleCellExperiment對(duì)象
# 數(shù)據(jù)下載自satija lab
#下載地址:https://www.dropbox.com/s/kwd3kcxkmpzqg6w/pbmc3k_final.rds?dl=0
pbmc <- readRDS(file = "../data/pbmc3k_final.rds")
pbmc.sce <- as.SingleCellExperiment(pbmc)
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
- 從SingleCellExperiment對(duì)象轉(zhuǎn)換為Seurat對(duì)象
# 演示數(shù)據(jù)下載自hemberg lab
# 下載地址:https://scrnaseq-public-datasets.s3.amazonaws.com/scater-objects/manno_human.rds
manno <- readRDS(file = "../data/manno_human.rds")
manno <- runPCA(manno)
manno.seurat <- as.Seurat(manno, counts = "counts", data = "logcounts")
# gives the same results; but omits defaults provided in the last line
manno.seurat <- as.Seurat(manno)
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
2. Converting to/from loom
- 從Seurat對(duì)象轉(zhuǎn)換為loom對(duì)象
pbmc.loom <- as.loom(pbmc, filename = "../pbmc3k.loom", verbose = FALSE)
pbmc.loom
# Always remember to close loom files when done
pbmc.loom$close_all()
- 從loom對(duì)象轉(zhuǎn)換為Seurat對(duì)象
# 數(shù)據(jù)下載自linnarsson lab
# 下載地址:https://storage.googleapis.com/linnarsson-lab-loom/l6_r1_immune_cells.loom
l6.immune <- Connect(filename = "../data/l6_r1_immune_cells.loom", mode = "r")
l6.immune
l6.seurat <- as.Seurat(l6.immune)
Idents(l6.seurat) <- "ClusterName"
VlnPlot(l6.seurat, features = c("Sparc", "Ftl1", "Junb", "Ccl4"), ncol = 2)
# Always remember to close loom files when done
l6.immune$close_all()
更多l(xiāng)oom和Seurat之間的相互轉(zhuǎn)換缓屠,見(jiàn):https://github.com/mojaveazure/loomR
3. Converting to/from AnnData
AnnData 是Scanpy包的數(shù)據(jù)存儲(chǔ)格式唉韭, Seurat對(duì)象和AnnData之間的轉(zhuǎn)換由SeuratDisk完成。
參考 :https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html
- 從Seurat對(duì)象轉(zhuǎn)換到AnnData(使用h5Seurat)
pbmc3k <- readRDS("pbmc.rds")
pbmc3k.final
#> An object of class Seurat
#> 13714 features across 2638 samples within 1 assay
#> Active assay: RNA (13714 features, 2000 variable features)
#> 2 dimensional reductions calculated: pca, umap
將Seurat對(duì)象轉(zhuǎn)換為AnnData file需要兩步内列。
1. 將Seurat對(duì)象保存為h5Seurat文件。
2. 將h5Seurat文件轉(zhuǎn)換成Scanpy的AnnData文件背率。
SaveH5Seurat(pbmc3k.final, filename = "pbmc3k.h5Seurat")
Convert("pbmc3k.h5Seurat", dest = "h5ad")
運(yùn)行完后话瞧,工作目錄下的到2個(gè)文件
隨后就可以使用Scanpy的read_h5ad功能查看AnnData文件。
python3
import scanpy
adata = scanpy.read_h5ad("pbmc3k.h5ad")
adata
#> AnnData object with n_obs × n_vars = 2638 × 13714
#> obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA', 'percent.mt', 'RNA_snn_res.0.5', 'seurat_clusters', 'cell_type'
#> var: 'vst.mean', 'vst.variance', 'vst.variance.expected', 'vst.variance.standardized', 'vst.variable'
#> uns: 'neighbors'
#> obsm: 'X_pca', 'X_umap'
#> varm: 'PCs'
#> obsp: 'distances'
- 從AnnData轉(zhuǎn)換到Seurat對(duì)象(使用h5Seurat)
url <- "https://seurat.nygenome.org/pbmc3k_final.h5ad"
curl::curl_download(url, basename(url))
Convert("pbmc3k_final.h5ad", dest = "h5seurat", overwrite = TRUE)
pbmc3k <- LoadH5Seurat("pbmc3k_final.h5seurat")
pbmc3k
#> An object of class Seurat
#> 13714 features across 2638 samples within 1 assay
#> Active assay: RNA (13714 features, 0 variable features)
#> 2 dimensional reductions calculated: pca, umap