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: /__w/2/s/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: /__w/2/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>
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.2           
## [15] Seurat_4.0.3                  scater_1.20.0                
## [17] ggplot2_3.3.3                 scuttle_1.2.0                
## [19] SingleCellExperiment_1.14.1   SummarizedExperiment_1.22.0  
## [21] Biobase_2.52.0                GenomicRanges_1.44.0         
## [23] GenomeInfoDb_1.28.0           IRanges_2.26.0               
## [25] S4Vectors_0.30.0              BiocGenerics_0.38.0          
## [27] MatrixGenerics_1.4.0          matrixStats_0.59.0           
## 
## loaded via a namespace (and not attached):
##   [1] utf8_1.2.1                reticulate_1.20          
##   [3] tidyselect_1.1.1          htmlwidgets_1.5.3        
##   [5] grid_4.1.0                BiocParallel_1.26.0      
##   [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.21.0             miniUI_0.1.1.1           
##  [15] withr_2.4.2               colorspace_2.0-1         
##  [17] highr_0.9                 knitr_1.33               
##  [19] rstudioapi_0.13           ROCR_1.0-11              
##  [21] tensor_1.5                listenv_0.8.0            
##  [23] labeling_0.4.2            GenomeInfoDbData_1.2.6   
##  [25] polyclip_1.10-0           farver_2.1.0             
##  [27] bit64_4.0.5               rprojroot_2.0.2          
##  [29] parallelly_1.26.0         vctrs_0.3.8              
##  [31] generics_0.1.0            xfun_0.23                
##  [33] R6_2.5.0                  ggbeeswarm_0.6.0         
##  [35] rsvd_1.0.5                hdf5r_1.3.3              
##  [37] bitops_1.0-7              spatstat.utils_2.1-0     
##  [39] cachem_1.0.5              DelayedArray_0.18.0      
##  [41] assertthat_0.2.1          promises_1.2.0.1         
##  [43] scales_1.1.1              beeswarm_0.4.0           
##  [45] gtable_0.3.0              beachmat_2.8.0           
##  [47] globals_0.14.0            goftest_1.2-2            
##  [49] rlang_0.4.11              systemfonts_1.0.2        
##  [51] splines_4.1.0             lazyeval_0.2.2           
##  [53] spatstat.geom_2.1-0       yaml_2.2.1               
##  [55] reshape2_1.4.4            abind_1.4-5              
##  [57] httpuv_1.6.1              tools_4.1.0              
##  [59] ellipsis_0.3.2            spatstat.core_2.1-2      
##  [61] jquerylib_0.1.4           RColorBrewer_1.1-2       
##  [63] ggridges_0.5.3            Rcpp_1.0.6               
##  [65] plyr_1.8.6                sparseMatrixStats_1.4.0  
##  [67] zlibbioc_1.38.0           purrr_0.3.4              
##  [69] RCurl_1.98-1.3            ps_1.6.0                 
##  [71] rpart_4.1-15              deldir_0.2-10            
##  [73] pbapply_1.4-3             viridis_0.6.1            
##  [75] cowplot_1.1.1             zoo_1.8-9                
##  [77] ggrepel_0.9.1             cluster_2.1.2            
##  [79] fs_1.5.0                  magrittr_2.0.1           
##  [81] data.table_1.14.0         scattermore_0.7          
##  [83] lmtest_0.9-38             RANN_2.6.1               
##  [85] fitdistrplus_1.1-5        mime_0.10                
##  [87] evaluate_0.14             xtable_1.8-4             
##  [89] gridExtra_2.3             compiler_4.1.0           
##  [91] tibble_3.1.2              KernSmooth_2.23-20       
##  [93] crayon_1.4.1              htmltools_0.5.1.1        
##  [95] mgcv_1.8-35               later_1.2.0              
##  [97] tidyr_1.1.3               DBI_1.1.1                
##  [99] formatR_1.11              MASS_7.3-54              
## [101] rappdirs_0.3.3            Matrix_1.3-3             
## [103] cli_2.5.0                 igraph_1.2.6             
## [105] pkgconfig_2.0.3           pkgdown_1.6.1            
## [107] plotly_4.9.4              spatstat.sparse_2.0-0    
## [109] vipor_0.4.5               bslib_0.2.5.1            
## [111] XVector_0.32.0            stringr_1.4.0            
## [113] digest_0.6.27             sctransform_0.3.2        
## [115] RcppAnnoy_0.0.18          spatstat.data_2.1-0      
## [117] rmarkdown_2.8             leiden_0.3.8             
## [119] uwot_0.1.10               DelayedMatrixStats_1.14.0
## [121] shiny_1.6.0               lifecycle_1.0.0          
## [123] nlme_3.1-152              jsonlite_1.7.2           
## [125] BiocNeighbors_1.10.0      desc_1.3.0               
## [127] viridisLite_0.4.0         fansi_0.5.0              
## [129] pillar_1.6.1              lattice_0.20-44          
## [131] fastmap_1.1.0             httr_1.4.2               
## [133] survival_3.2-11           glue_1.4.2               
## [135] png_0.1-7                 bit_4.0.4                
## [137] stringi_1.6.2             sass_0.4.0               
## [139] textshaping_0.3.5         BiocSingular_1.8.1       
## [141] memoise_2.0.0             dplyr_1.0.6              
## [143] irlba_2.3.3               future.apply_1.7.0