Overview

This package provides a lightweight interface between the Bioconductor SingleCellExperiment data structure and the Python AnnData-based single-cell analysis environment. The idea is to enable users and developers to easily move data between these frameworks to construct a multi-language analysis pipeline across R/Bioconductor and Python.

Reading and writing H5AD files

The readH5AD() function can be used to read a SingleCellExperiment from a H5AD file. This can be manipulated in the usual way as described in the SingleCellExperiment documentation.

library(zellkonverter)

# Obtaining an example H5AD file.
example_h5ad <- system.file("extdata", "krumsiek11.h5ad",
                            package = "zellkonverter")
readH5AD(example_h5ad)
## class: SingleCellExperiment 
## dim: 11 640 
## metadata(2): highlights iroot
## assays(1): X
## rownames(11): Gata2 Gata1 ... EgrNab Gfi1
## rowData names(0):
## colnames(640): 0 1 ... 158-3 159-3
## colData names(1): cell_type
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):

We can also write a SingleCellExperiment to a H5AD file with the writeH5AD() function. This is demonstrated below on the classic Zeisel mouse brain dataset from the scRNAseq package. The resulting file can then be directly used in compatible Python-based analysis frameworks.

library(scRNAseq)

sce_zeisel <- ZeiselBrainData()
out_path <- tempfile(pattern = ".h5ad")
writeH5AD(sce_zeisel, file = out_path)

Converting between SingleCellExperiment and AnnData objects

Developers and power users who control their Python environments can directly convert between SingleCellExperiment and AnnData objects using the SCE2AnnData() and AnnData2SCE() utilities. These functions expect that reticulate has already been loaded along with an appropriate version of the anndata package. We suggest using the basilisk package to set up the Python environment before using these functions.

library(basilisk)
library(scRNAseq)

seger <- SegerstolpePancreasData()
roundtrip <- basiliskRun(fun = function(sce) {
     # Convert SCE to AnnData:
     adata <- SCE2AnnData(sce)

     # Maybe do some work in Python on 'adata':
     # BLAH BLAH BLAH

     # Convert back to an SCE:
     AnnData2SCE(adata)
}, env = zellkonverterAnnDataEnv, sce = seger)

Package developers can guarantee that they are using the same versions of Python packages as zellkonverter by using the .AnnDataDependencies variable to set up their Python environments.

.AnnDataDependencies
## [1] "anndata==0.7.6"  "h5py==3.2.1"     "hdf5==1.10.6"    "natsort==7.1.1" 
## [5] "numpy==1.20.2"   "packaging==20.9" "pandas==1.2.4"   "scipy==1.6.3"   
## [9] "sqlite==3.35.5"

Progress messages

By default the functions in zellkonverter don’t display any information about their progress but this can be turned on by setting the verbose = TRUE argument.

readH5AD(example_h5ad, verbose = TRUE)
## class: SingleCellExperiment 
## dim: 11 640 
## metadata(2): highlights iroot
## assays(1): X
## rownames(11): Gata2 Gata1 ... EgrNab Gfi1
## rowData names(0):
## colnames(640): 0 1 ... 158-3 159-3
## colData names(1): cell_type
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):

If you would like to see progress messages for all functions by default you can turn this on using the setZellkonverterVerbose() function.

# This is not run here 
setZellkonverterVerbose(TRUE)

Session information

## R Under development (unstable) (2021-10-28 r81109)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 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] stats    graphics utils    stats4   methods  base    
## 
## other attached packages:
##  [1] basilisk_1.7.0              scRNAseq_2.9.0             
##  [3] SingleCellExperiment_1.17.0 SummarizedExperiment_1.25.1
##  [5] Biobase_2.55.0              GenomicRanges_1.47.3       
##  [7] GenomeInfoDb_1.31.1         IRanges_2.29.0             
##  [9] S4Vectors_0.33.0            BiocGenerics_0.41.1        
## [11] MatrixGenerics_1.7.0        matrixStats_0.61.0         
## [13] zellkonverter_1.5.0         knitr_1.36                 
## [15] BiocStyle_2.23.0           
## 
## loaded via a namespace (and not attached):
##   [1] rjson_0.2.20                  ellipsis_0.3.2               
##   [3] rprojroot_2.0.2               XVector_0.35.0               
##   [5] fs_1.5.0                      bit64_4.0.5                  
##   [7] interactiveDisplayBase_1.33.0 AnnotationDbi_1.57.1         
##   [9] fansi_0.5.0                   xml2_1.3.2                   
##  [11] cachem_1.0.6                  jsonlite_1.7.2               
##  [13] Rsamtools_2.11.0              dbplyr_2.1.1                 
##  [15] png_0.1-7                     shiny_1.7.1                  
##  [17] BiocManager_1.30.16           compiler_4.2.0               
##  [19] httr_1.4.2                    lazyeval_0.2.2               
##  [21] assertthat_0.2.1              Matrix_1.3-4                 
##  [23] fastmap_1.1.0                 cli_3.1.0                    
##  [25] later_1.3.0                   htmltools_0.5.2              
##  [27] prettyunits_1.1.1             tools_4.2.0                  
##  [29] glue_1.4.2                    GenomeInfoDbData_1.2.7       
##  [31] dplyr_1.0.7                   grDevices_4.2.0              
##  [33] rappdirs_0.3.3                Rcpp_1.0.7                   
##  [35] jquerylib_0.1.4               pkgdown_1.9000.9000.9000     
##  [37] vctrs_0.3.8                   Biostrings_2.63.0            
##  [39] ExperimentHub_2.3.0           rtracklayer_1.55.0           
##  [41] xfun_0.27                     stringr_1.4.0                
##  [43] mime_0.12                     lifecycle_1.0.1              
##  [45] restfulr_0.0.13               ensembldb_2.19.0             
##  [47] XML_3.99-0.8                  AnnotationHub_3.3.0          
##  [49] zlibbioc_1.41.0               basilisk.utils_1.7.0         
##  [51] ProtGenerics_1.27.0           ragg_1.2.0                   
##  [53] hms_1.1.1                     promises_1.2.0.1             
##  [55] parallel_4.2.0                AnnotationFilter_1.19.0      
##  [57] yaml_2.2.1                    curl_4.3.2                   
##  [59] memoise_2.0.0                 reticulate_1.22              
##  [61] sass_0.4.0                    datasets_4.2.0               
##  [63] biomaRt_2.51.0                stringi_1.7.5                
##  [65] RSQLite_2.2.8                 BiocVersion_3.15.0           
##  [67] BiocIO_1.5.0                  desc_1.4.0                   
##  [69] GenomicFeatures_1.47.2        filelock_1.0.2               
##  [71] BiocParallel_1.29.0           rlang_0.4.12                 
##  [73] pkgconfig_2.0.3               systemfonts_1.0.3            
##  [75] bitops_1.0-7                  evaluate_0.14                
##  [77] lattice_0.20-45               purrr_0.3.4                  
##  [79] GenomicAlignments_1.31.0      bit_4.0.4                    
##  [81] tidyselect_1.1.1              here_1.0.1                   
##  [83] magrittr_2.0.1                bookdown_0.24                
##  [85] R6_2.5.1                      generics_0.1.1               
##  [87] DelayedArray_0.21.1           DBI_1.1.1                    
##  [89] withr_2.4.2                   pillar_1.6.4                 
##  [91] KEGGREST_1.35.0               RCurl_1.98-1.5               
##  [93] tibble_3.1.5                  dir.expiry_1.3.0             
##  [95] crayon_1.4.2                  utf8_1.2.2                   
##  [97] BiocFileCache_2.3.0           rmarkdown_2.11               
##  [99] progress_1.2.2                grid_4.2.0                   
## [101] blob_1.2.2                    digest_0.6.28                
## [103] xtable_1.8-4                  httpuv_1.6.3                 
## [105] textshaping_0.3.6             bslib_0.3.1