# Converting single-cell data structures between Bioconductor and Python

Luke Zappia* and Aaron Lun**

*luke@lazappi.id.au
**infinite.monkeys.with.keyboards@gmail.com

#### Revised: 17 April 2022

#### Package

zellkonverter 1.6.5

# 1 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.

# 2 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)

# 3 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()` function to set up their Python environments.

    AnnDataDependencies()

    ##  [1] "anndata==0.8.0"  "h5py==3.6.0"     "hdf5==1.12.1"    "natsort==8.1.0"
    ##  [5] "numpy==1.22.3"   "packaging==21.3" "pandas==1.4.2"   "python==3.8.13"
    ##  [9] "scipy==1.7.3"    "sqlite==3.38.2"

This function can also be used to return dependencies for environments using older versions of anndata.

    AnnDataDependencies(version = "0.7.6")

    ## [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"

# 4 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)

# 5 Session information

    sessionInfo()

    ## R version 4.2.1 (2022-06-23 ucrt)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] basilisk_1.8.1              scRNAseq_2.10.0
    ##  [3] SingleCellExperiment_1.18.0 SummarizedExperiment_1.26.1
    ##  [5] Biobase_2.56.0              GenomicRanges_1.48.0
    ##  [7] GenomeInfoDb_1.32.4         IRanges_2.30.1
    ##  [9] S4Vectors_0.34.0            BiocGenerics_0.42.0
    ## [11] MatrixGenerics_1.8.1        matrixStats_0.62.0
    ## [13] zellkonverter_1.6.5         knitr_1.40
    ## [15] BiocStyle_2.24.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] ProtGenerics_1.28.0           bitops_1.0-7
    ##  [3] bit64_4.0.5                   filelock_1.0.2
    ##  [5] progress_1.2.2                httr_1.4.4
    ##  [7] rprojroot_2.0.3               tools_4.2.1
    ##  [9] bslib_0.4.0                   utf8_1.2.2
    ## [11] R6_2.5.1                      lazyeval_0.2.2
    ## [13] DBI_1.1.3                     prettyunits_1.1.1
    ## [15] tidyselect_1.1.2              bit_4.0.4
    ## [17] curl_4.3.2                    compiler_4.2.1
    ## [19] cli_3.4.0                     basilisk.utils_1.8.0
    ## [21] xml2_1.3.3                    DelayedArray_0.22.0
    ## [23] rtracklayer_1.56.1            bookdown_0.29
    ## [25] sass_0.4.2                    rappdirs_0.3.3
    ## [27] Rsamtools_2.12.0              stringr_1.4.1
    ## [29] digest_0.6.29                 rmarkdown_2.16
    ## [31] XVector_0.36.0                pkgconfig_2.0.3
    ## [33] htmltools_0.5.3               ensembldb_2.20.2
    ## [35] dbplyr_2.2.1                  fastmap_1.1.0
    ## [37] rlang_1.0.5                   RSQLite_2.2.17
    ## [39] shiny_1.7.2                   BiocIO_1.6.0
    ## [41] jquerylib_0.1.4               generics_0.1.3
    ## [43] jsonlite_1.8.0                BiocParallel_1.30.3
    ## [45] dplyr_1.0.10                  RCurl_1.98-1.8
    ## [47] magrittr_2.0.3                GenomeInfoDbData_1.2.8
    ## [49] Matrix_1.5-1                  Rcpp_1.0.9
    ## [51] fansi_1.0.3                   reticulate_1.26
    ## [53] lifecycle_1.0.2               stringi_1.7.8
    ## [55] yaml_2.3.5                    zlibbioc_1.42.0
    ## [57] BiocFileCache_2.4.0           AnnotationHub_3.4.0
    ## [59] grid_4.2.1                    blob_1.2.3
    ## [61] parallel_4.2.1                promises_1.2.0.1
    ## [63] ExperimentHub_2.4.0           crayon_1.5.1
    ## [65] dir.expiry_1.4.0              lattice_0.20-45
    ## [67] Biostrings_2.64.1             GenomicFeatures_1.48.3
    ## [69] hms_1.1.2                     KEGGREST_1.36.3
    ## [71] pillar_1.8.1                  rjson_0.2.21
    ## [73] codetools_0.2-18              biomaRt_2.52.0
    ## [75] XML_3.99-0.10                 glue_1.6.2
    ## [77] BiocVersion_3.15.2            evaluate_0.16
    ## [79] BiocManager_1.30.18           png_0.1-7
    ## [81] vctrs_0.4.1                   httpuv_1.6.6
    ## [83] purrr_0.3.4                   assertthat_0.2.1
    ## [85] cachem_1.0.6                  xfun_0.33
    ## [87] mime_0.12                     xtable_1.8-4
    ## [89] AnnotationFilter_1.20.0       restfulr_0.0.15
    ## [91] later_1.3.0                   tibble_3.1.8
    ## [93] GenomicAlignments_1.32.1      AnnotationDbi_1.58.0
    ## [95] memoise_2.0.1                 ellipsis_0.3.2
    ## [97] interactiveDisplayBase_1.34.0 here_1.0.1