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)
# install SeuratDisk from GitHub using the remotes package remotes::install_github(repo =
# 'mojaveazure/seurat-disk', ref = 'develop')
library(SeuratDisk)
library(SeuratData)
library(patchwork)

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.

# Use PBMC3K from SeuratData
InstallData("pbmc3k")
pbmc <- LoadData(ds = "pbmc3k", type = "pbmc3k.final")
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

Seurat also allows conversion from SingleCellExperiment objects to Seurat objects; we demonstrate this on some publicly available data downloaded from a repository maintained by Martin Hemberg’s group.

# download from 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

Converting to/from loom

The loom format is a file structure imposed on HDF5 files designed by Sten Linnarsson’s group. It is designed to efficiently hold large single-cell genomics datasets. The ability to save Seurat objects as loom files is implemented in SeuratDisk For more details about the loom format, please see the loom file format specification.

pbmc.loom <- as.loom(pbmc, filename = "../output/pbmc3k.loom", verbose = FALSE)
pbmc.loom
## Class: loom
## Filename: /seurat-private/output/pbmc3k.loom
## Access type: H5F_ACC_RDWR
## Listing:
##        name    obj_type dataset.dims dataset.type_class
##       attrs   H5I_GROUP         <NA>               <NA>
##   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>
# Always remember to close loom files when done
pbmc.loom$close_all()

Seurat can also read in loom files connected via SeuratDisk into a Seurat object; we demonstrate this on a subset of the Mouse Brain Atlas created by the Linnarsson lab.

# download from 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
## Class: loom
## Filename: /seurat-private/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>
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()

For more details about interacting with loom files in R and Seurat, please see loomR on GitHub.

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. Support for reading data from and saving data to AnnData files is provided by SeuratDisk; please see their vignette showcasing the interoperability.

Acknowledgments

Many thanks to Davis McCarthy and Alex Wolf for their help in drafting the conversion functions.

Session Info

## R version 4.1.0 (2021-05-18)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] patchwork_1.1.1               thp1.eccite.SeuratData_3.1.5 
##  [3] stxBrain.SeuratData_0.1.1     ssHippo.SeuratData_3.1.4     
##  [5] pbmcsca.SeuratData_3.0.0      pbmcMultiome.SeuratData_0.1.1
##  [7] pbmc3k.SeuratData_3.1.4       panc8.SeuratData_3.0.2       
##  [9] ifnb.SeuratData_3.1.0         hcabm40k.SeuratData_3.0.0    
## [11] bmcite.SeuratData_0.3.0       SeuratData_0.2.1             
## [13] SeuratDisk_0.0.0.9019         SeuratObject_4.0.4           
## [15] Seurat_4.0.6                  scater_1.20.1                
## [17] ggplot2_3.3.5                 scuttle_1.2.1                
## [19] SingleCellExperiment_1.14.1   SummarizedExperiment_1.22.0  
## [21] Biobase_2.52.0                GenomicRanges_1.44.0         
## [23] GenomeInfoDb_1.28.1           IRanges_2.26.0               
## [25] S4Vectors_0.30.0              BiocGenerics_0.38.0          
## [27] MatrixGenerics_1.4.2          matrixStats_0.61.0           
## 
## loaded via a namespace (and not attached):
##   [1] utf8_1.2.2                reticulate_1.22          
##   [3] tidyselect_1.1.1          htmlwidgets_1.5.4        
##   [5] grid_4.1.0                BiocParallel_1.26.1      
##   [7] Rtsne_0.15                munsell_0.5.0            
##   [9] ScaledMatrix_1.0.0        codetools_0.2-18         
##  [11] ragg_1.1.3                ica_1.0-2                
##  [13] future_1.23.0             miniUI_0.1.1.1           
##  [15] withr_2.4.3               colorspace_2.0-2         
##  [17] highr_0.9                 knitr_1.33               
##  [19] ROCR_1.0-11               tensor_1.5               
##  [21] listenv_0.8.0             labeling_0.4.2           
##  [23] GenomeInfoDbData_1.2.6    polyclip_1.10-0          
##  [25] farver_2.1.0              bit64_4.0.5              
##  [27] rprojroot_2.0.2           parallelly_1.30.0        
##  [29] vctrs_0.3.8               generics_0.1.1           
##  [31] xfun_0.25                 R6_2.5.1                 
##  [33] ggbeeswarm_0.6.0          rsvd_1.0.5               
##  [35] hdf5r_1.3.3               bitops_1.0-7             
##  [37] spatstat.utils_2.3-0      cachem_1.0.6             
##  [39] DelayedArray_0.18.0       assertthat_0.2.1         
##  [41] promises_1.2.0.1          scales_1.1.1             
##  [43] beeswarm_0.4.0            gtable_0.3.0             
##  [45] beachmat_2.8.0            globals_0.14.0           
##  [47] goftest_1.2-3             rlang_0.4.12             
##  [49] systemfonts_1.0.2         splines_4.1.0            
##  [51] lazyeval_0.2.2            spatstat.geom_2.3-1      
##  [53] yaml_2.2.1                reshape2_1.4.4           
##  [55] abind_1.4-5               httpuv_1.6.5             
##  [57] tools_4.1.0               ellipsis_0.3.2           
##  [59] spatstat.core_2.3-2       jquerylib_0.1.4          
##  [61] RColorBrewer_1.1-2        ggridges_0.5.3           
##  [63] Rcpp_1.0.7                plyr_1.8.6               
##  [65] sparseMatrixStats_1.4.2   zlibbioc_1.38.0          
##  [67] purrr_0.3.4               RCurl_1.98-1.3           
##  [69] rpart_4.1-15              deldir_1.0-6             
##  [71] pbapply_1.5-0             viridis_0.6.1            
##  [73] cowplot_1.1.1             zoo_1.8-9                
##  [75] ggrepel_0.9.1             cluster_2.1.2            
##  [77] fs_1.5.2                  magrittr_2.0.1           
##  [79] data.table_1.14.2         scattermore_0.7          
##  [81] lmtest_0.9-39             RANN_2.6.1               
##  [83] fitdistrplus_1.1-6        mime_0.12                
##  [85] evaluate_0.14             xtable_1.8-4             
##  [87] gridExtra_2.3             compiler_4.1.0           
##  [89] tibble_3.1.6              KernSmooth_2.23-20       
##  [91] crayon_1.4.2              htmltools_0.5.2          
##  [93] mgcv_1.8-35               later_1.3.0              
##  [95] tidyr_1.1.4               DBI_1.1.1                
##  [97] formatR_1.11              MASS_7.3-54              
##  [99] rappdirs_0.3.3            Matrix_1.3-3             
## [101] cli_3.1.0                 igraph_1.2.11            
## [103] pkgconfig_2.0.3           pkgdown_1.6.1            
## [105] plotly_4.10.0             spatstat.sparse_2.1-0    
## [107] vipor_0.4.5               bslib_0.3.1              
## [109] XVector_0.32.0            stringr_1.4.0            
## [111] digest_0.6.29             sctransform_0.3.2        
## [113] RcppAnnoy_0.0.19          spatstat.data_2.1-2      
## [115] rmarkdown_2.10            leiden_0.3.9             
## [117] uwot_0.1.11               DelayedMatrixStats_1.14.2
## [119] shiny_1.7.1               lifecycle_1.0.1          
## [121] nlme_3.1-152              jsonlite_1.7.2           
## [123] BiocNeighbors_1.10.0      desc_1.4.0               
## [125] viridisLite_0.4.0         fansi_1.0.0              
## [127] pillar_1.6.4              lattice_0.20-44          
## [129] fastmap_1.1.0             httr_1.4.2               
## [131] survival_3.2-11           glue_1.6.0               
## [133] png_0.1-7                 bit_4.0.4                
## [135] stringi_1.7.6             sass_0.4.0               
## [137] textshaping_0.3.5         BiocSingular_1.8.1       
## [139] memoise_2.0.0             dplyr_1.0.7              
## [141] irlba_2.3.5               future.apply_1.8.1