In this vignette, we demonstrate the ability to convert between Seurat objects, SingleCellExperiment objects, and anndata objects.
# install scater
# https://bioconductor.org/packages/release/bioc/html/scater.html
library(scater)
library(Seurat)
library(cowplot)
Converting to/from SingleCellExperiment
SingleCellExperiment
is a class for storing single-cell experiment data, created by Davide Risso, Aaron Lun, and Keegan Korthauer, and is used by many Bioconductor analysis packages. Here we demonstrate converting the Seurat object produced in our 3k PBMC tutorial to SingleCellExperiment for use with Davis McCarthy’s
scater
package.
# download from satija lab
# https://www.dropbox.com/s/kwd3kcxkmpzqg6w/pbmc3k_final.rds?dl=0
pbmc <- readRDS("~/Downloads/pbmc3k_final.rds")
pbmc_sce <- Convert(from = pbmc, to = "sce")
p1 <- plotExpression(object = pbmc_sce, features = "MS4A1", x = "ident") + theme(axis.text.x = element_text(angle = 45,
hjust = 1))
p2 <- plotPCA(object = pbmc_sce, colour_by = "ident")
plot_grid(p1, p2)
We demonstrate this on some publicly available data downloaded from a repository maintained by
Martin Hemberg’s group
.
# downloaded from hemberg lab
# https://scrnaseq-public-datasets.s3.amazonaws.com/scater-objects/manno_human.rds
manno <- readRDS("~/Downloads/manno_human.rds")
manno <- runPCA(manno)
manno_seurat <- Convert(from = manno, to = "seurat")
manno_seurat <- SetAllIdent(object = manno_seurat, id = "cell_type1")
p1 <- DimPlot(object = manno_seurat, reduction.use = "PCA", dim.1 = 1, dim.2 = 2,
group.by = "Source", do.return = TRUE)
p2 <- RidgePlot(object = manno_seurat, features.plot = "ACTB", y.lab.rot = TRUE,
group.by = "Source", do.return = TRUE)
plot_grid(p1, p2)
Converting to/from anndata
anndata
provides a python class, created by Alex Wolf and Philipp Angerer, that can be used to store single-cell data. This data format is also use for storage in their
scanpy
package for which we now support interoperability. In order to access the conversion function, first read the data into R using the
reticulate package
to import the anndata module.
# download from satija lab
# https://www.dropbox.com/s/ngs3p8n2i8y33hj/pbmc3k.h5ad?dl=0
library(reticulate)
ad <- import("anndata", convert = FALSE)
pbmc_ad <- ad$read_h5ad("/Users/abutler/Downloads/pbmc3k.h5ad")
pbmc3k <- Convert(pbmc_ad, to = "seurat")
p1 <- TSNEPlot(pbmc3k, group.by = "louvain", do.return = TRUE)
p2 <- VlnPlot(pbmc3k, c("CST3", "NKG7", "PPBP"), group.by = "louvain", do.return = TRUE)
plot_grid(p1, p2)
We support conversion from Seurat objects to anndata as well.
pbmc_ad <- Convert(from = pbmc, to = "anndata", filename = "/Users/abutler/Projects/test/pbmc3k.h5ad")
pbmc_ad
## AnnData object with n_obs × n_vars = 2638 × 13714
## obs: 'ClusterNames_0_6', 'n_counts', 'n_genes', 'orig_ident', 'percent_mito', 'res_0_6', 'res_0_8'
## var: 'gene.dispersion', 'gene.dispersion.scaled', 'gene.mean'
## obsm: 'X_pca', 'X_tsne'
Acknowledgments
Many thanks to
Davis McCarthy
and
Alex Wolf
for their help in drafting the conversion functions.