Seurat官网:https://satijalab.org/seurat/articles/conversion_vignette.html
Seurat objects, SingleCellExperiment objects和anndata objects之间的转换。
library(scater)
library(Seurat)
library(SeuratData)
#remotes::install_github("mojaveazure/seurat-disk")
library(SeuratDisk)
library(patchwork)
1. Converting to/from SingleCellExperiment
- 从Seurat对象转换为SingleCellExperiment对象
# 数据下载自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对象转换为Seurat对象
# 演示数据下载自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对象转换为loom对象
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对象转换为Seurat对象
# 数据下载自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()
更多loom和Seurat之间的相互转换,见:https://github.com/mojaveazure/loomR
3. Converting to/from AnnData
AnnData 是Scanpy包的数据存储格式, Seurat对象和AnnData之间的转换由SeuratDisk完成。
参考 :https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html
- 从Seurat对象转换到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对象转换为AnnData file需要两步。
1. 将Seurat对象保存为h5Seurat文件。
2. 将h5Seurat文件转换成Scanpy的AnnData文件。
SaveH5Seurat(pbmc3k.final, filename = "pbmc3k.h5Seurat")
Convert("pbmc3k.h5Seurat", dest = "h5ad")
运行完后,工作目录下的到2个文件
随后就可以使用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转换到Seurat对象(使用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