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

# 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.10.9" "h5py==3.12.1"    "hdf5==1.14.3"    "natsort==8.4.0"
    ## [5] "numpy==2.1.2"    "packaging==24.1" "pandas==2.2.3"   "python==3.12.7"
    ## [9] "scipy==1.14.1"

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"   "python==3.7.10"
    ##  [9] "scipy==1.6.3"    "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.4.1 (2024-06-14)
    ## Platform: x86_64-apple-darwin20
    ## Running under: macOS Monterey 12.7.6
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] basilisk_1.18.0             reticulate_1.39.0
    ##  [3] scRNAseq_2.19.1             SingleCellExperiment_1.28.0
    ##  [5] SummarizedExperiment_1.36.0 Biobase_2.66.0
    ##  [7] GenomicRanges_1.58.0        GenomeInfoDb_1.42.0
    ##  [9] IRanges_2.40.0              S4Vectors_0.44.0
    ## [11] BiocGenerics_0.52.0         MatrixGenerics_1.18.0
    ## [13] matrixStats_1.4.1           zellkonverter_1.16.0
    ## [15] knitr_1.48                  BiocStyle_2.34.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] DBI_1.2.3                bitops_1.0-9             httr2_1.0.5
    ##  [4] rlang_1.1.4              magrittr_2.0.3           gypsum_1.2.0
    ##  [7] compiler_4.4.1           RSQLite_2.3.7            GenomicFeatures_1.58.0
    ## [10] dir.expiry_1.14.0        png_0.1-8                vctrs_0.6.5
    ## [13] ProtGenerics_1.38.0      pkgconfig_2.0.3          crayon_1.5.3
    ## [16] fastmap_1.2.0            dbplyr_2.5.0             XVector_0.46.0
    ## [19] utf8_1.2.4               Rsamtools_2.22.0         rmarkdown_2.28
    ## [22] UCSC.utils_1.2.0         bit_4.5.0                xfun_0.48
    ## [25] zlibbioc_1.52.0          cachem_1.1.0             jsonlite_1.8.9
    ## [28] blob_1.2.4               rhdf5filters_1.18.0      DelayedArray_0.32.0
    ## [31] Rhdf5lib_1.28.0          BiocParallel_1.40.0      parallel_4.4.1
    ## [34] R6_2.5.1                 bslib_0.8.0              rtracklayer_1.66.0
    ## [37] jquerylib_0.1.4          Rcpp_1.0.13              bookdown_0.41
    ## [40] Matrix_1.7-1             tidyselect_1.2.1         abind_1.4-8
    ## [43] yaml_2.3.10              codetools_0.2-20         curl_5.2.3
    ## [46] alabaster.sce_1.6.0      lattice_0.22-6           tibble_3.2.1
    ## [49] basilisk.utils_1.18.0    withr_3.0.2              KEGGREST_1.46.0
    ## [52] evaluate_1.0.1           BiocFileCache_2.14.0     alabaster.schemas_1.6.0
    ## [55] ExperimentHub_2.14.0     Biostrings_2.74.0        pillar_1.9.0
    ## [58] BiocManager_1.30.25      filelock_1.0.3           generics_0.1.3
    ## [61] RCurl_1.98-1.16          ensembldb_2.30.0         BiocVersion_3.20.0
    ## [64] alabaster.base_1.6.0     alabaster.ranges_1.6.0   glue_1.8.0
    ## [67] lazyeval_0.2.2           alabaster.matrix_1.6.0   tools_4.4.1
    ## [70] AnnotationHub_3.14.0     BiocIO_1.16.0            GenomicAlignments_1.42.0
    ## [73] XML_3.99-0.17            rhdf5_2.50.0             grid_4.4.1
    ## [76] AnnotationDbi_1.68.0     GenomeInfoDbData_1.2.13  HDF5Array_1.34.0
    ## [79] restfulr_0.0.15          cli_3.6.3                rappdirs_0.3.3
    ## [82] fansi_1.0.6              S4Arrays_1.6.0           dplyr_1.1.4
    ## [85] AnnotationFilter_1.30.0  alabaster.se_1.6.0       sass_0.4.9
    ## [88] digest_0.6.37            SparseArray_1.6.0        rjson_0.2.23
    ## [91] memoise_2.0.1            htmltools_0.5.8.1        lifecycle_1.0.4
    ## [94] httr_1.4.7               bit64_4.5.2