Contents

1 Introduction

anndataR works with Python AnnData objects through reticulate. You can load Python objects, apply Python functions to them, and convert them to Seurat or SingleCellExperiment objects.

message(
  "Python packages scanpy and mudata are required to run this vignette. Code chunks will not be evaluated."
)

1.1 Prerequisites

This vignette requires Python with the scanpy and mudata packages installed. If these are not available, the code chunks will not be evaluated but the examples remain visible.

2 Basic Integration with scanpy

Install required Python packages if needed:

reticulate::py_require("scanpy")
library(anndataR)
library(reticulate)
sc <- import("scanpy")

Load a dataset directly from scanpy:

adata <- sc$datasets$pbmc3k_processed()
adata
#> ReticulateAnnData object with n_obs × n_vars = 2638 × 1838
#>     obs: 'n_genes', 'percent_mito', 'n_counts', 'louvain'
#>     var: 'n_cells'
#>     uns: 'draw_graph', 'louvain', 'louvain_colors', 'neighbors', 'pca', 'rank_genes_groups'
#>     obsm: 'X_pca', 'X_tsne', 'X_umap', 'X_draw_graph_fr'
#>     varm: 'PCs'
#>     obsp: 'distances', 'connectivities'

Apply scanpy functions directly:

sc$pp$filter_cells(adata, min_genes = 200L)
sc$pp$normalize_total(adata, target_sum = 1e4)
sc$pp$log1p(adata)

3 Conversion to R objects

Convert to SingleCellExperiment (see vignette("usage_singlecellexperiment")):

sce_obj <- adata$as_SingleCellExperiment()
sce_obj
#> class: SingleCellExperiment 
#> dim: 1838 538 
#> metadata(7): draw_graph louvain ... rank_genes_groups log1p
#> assays(1): X
#> rownames(1838): TNFRSF4 CPSF3L ... S100B PRMT2
#> rowData names(1): n_cells
#> colnames(538): AAACATTGAGCTAC-1 AAACATTGATCAGC-1 ... TTTCGAACTCTCAT-1
#>   TTTCTACTGAGGCA-1
#> colData names(4): n_genes percent_mito n_counts louvain
#> reducedDimNames(4): X_pca X_tsne X_umap X_draw_graph_fr
#> mainExpName: NULL
#> altExpNames(0):

Convert to Seurat (see vignette("usage_seurat")):

seurat_obj <- adata$as_Seurat()
#> Warning: No "counts" or "data" layer found in `names(layers_mapping)`, this may lead to
#> unexpected results when using the resulting <Seurat> object.
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
seurat_obj
#> An object of class Seurat 
#> 1838 features across 538 samples within 1 assay 
#> Active assay: RNA (1838 features, 0 variable features)
#>  1 layer present: X
#>  4 dimensional reductions calculated: X_pca, X_tsne, X_umap, X_draw_graph_fr

4 Multi-modal data with mudata

Install required Python packages if needed:

reticulate::py_install("mudata")
md <- import("mudata")

Load a MuData object from file:

cache <- BiocFileCache::BiocFileCache(ask = FALSE)
h5mu_file <- BiocFileCache::bfcrpath(
  cache,
  "https://github.com/gtca/h5xx-datasets/raw/b1177ac8877c89d8bb355b072164384b4e9cc81d/datasets/minipbcite.h5mu"
)
#> adding rname 'https://github.com/gtca/h5xx-datasets/raw/b1177ac8877c89d8bb355b072164384b4e9cc81d/datasets/minipbcite.h5mu'

mdata <- md$read_h5mu(h5mu_file)

Access individual modalities and convert them:

rna_mod <- mdata$mod[["rna"]]

rna_seurat <- rna_mod$as_Seurat()
#> Warning: No "counts" or "data" layer found in `names(layers_mapping)`, this may lead to
#> unexpected results when using the resulting <Seurat> object.
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
print(rna_seurat)
#> An object of class Seurat 
#> 27 features across 411 samples within 1 assay 
#> Active assay: RNA (27 features, 0 variable features)
#>  1 layer present: X
#>  2 dimensional reductions calculated: X_pca, X_umap

rna_sce <- rna_mod$as_SingleCellExperiment()
print(rna_sce)
#> class: SingleCellExperiment 
#> dim: 27 411 
#> metadata(8): celltype_colors hvg ... rank_genes_groups umap
#> assays(1): X
#> rownames(27): NKG7 KLRC2 ... MS4A1 KLF4
#> rowData names(13): gene_ids feature_types ... mean std
#> colnames(411): CAGCCAGGTCTCGACG-1 TTCTTCCTCTCGGTAA-1 ...
#>   GACTCTCCAGCTCTGG-1 GAAATGACAAGCACCC-1
#> colData names(6): n_genes_by_counts total_counts ... leiden celltype
#> reducedDimNames(2): X_pca X_umap
#> mainExpName: NULL
#> altExpNames(0):

5 Session info

5.1 R

sessionInfo()
#> R version 4.5.1 Patched (2025-08-23 r88802)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [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       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] reticulate_1.43.0           anndataR_0.99.6            
#>  [3] SingleCellExperiment_1.31.1 SummarizedExperiment_1.39.2
#>  [5] Biobase_2.69.1              GenomicRanges_1.61.6       
#>  [7] Seqinfo_0.99.3              IRanges_2.43.5             
#>  [9] S4Vectors_0.47.4            BiocGenerics_0.55.4        
#> [11] generics_0.1.4              MatrixGenerics_1.21.0      
#> [13] matrixStats_1.5.0           SeuratObject_5.2.0         
#> [15] sp_2.2-0                    BiocStyle_2.37.1           
#> 
#> loaded via a namespace (and not attached):
#>   [1] RColorBrewer_1.1-3     jsonlite_2.0.0         magrittr_2.0.4        
#>   [4] spatstat.utils_3.2-0   farver_2.1.2           rmarkdown_2.30        
#>   [7] vctrs_0.6.5            ROCR_1.0-11            memoise_2.0.1         
#>  [10] spatstat.explore_3.5-3 htmltools_0.5.8.1      S4Arrays_1.9.1        
#>  [13] curl_7.0.0             Rhdf5lib_1.31.1        SparseArray_1.9.1     
#>  [16] rhdf5_2.53.6           sass_0.4.10            sctransform_0.4.2     
#>  [19] parallelly_1.45.1      KernSmooth_2.23-26     bslib_0.9.0           
#>  [22] htmlwidgets_1.6.4      ica_1.0-3              httr2_1.2.1           
#>  [25] plyr_1.8.9             plotly_4.11.0          zoo_1.8-14            
#>  [28] cachem_1.1.0           igraph_2.2.0           mime_0.13             
#>  [31] lifecycle_1.0.4        pkgconfig_2.0.3        Matrix_1.7-4          
#>  [34] R6_2.6.1               fastmap_1.2.0          fitdistrplus_1.2-4    
#>  [37] future_1.67.0          shiny_1.11.1           digest_0.6.37         
#>  [40] patchwork_1.3.2        Seurat_5.3.0           tensor_1.5.1          
#>  [43] RSpectra_0.16-2        irlba_2.3.5.1          RSQLite_2.4.3         
#>  [46] filelock_1.0.3         progressr_0.17.0       spatstat.sparse_3.1-0 
#>  [49] httr_1.4.7             polyclip_1.10-7        abind_1.4-8           
#>  [52] compiler_4.5.1         bit64_4.6.0-1          withr_3.0.2           
#>  [55] S7_0.2.0               DBI_1.2.3              fastDummies_1.7.5     
#>  [58] MASS_7.3-65            rappdirs_0.3.3         DelayedArray_0.35.3   
#>  [61] tools_4.5.1            lmtest_0.9-40          otel_0.2.0            
#>  [64] httpuv_1.6.16          future.apply_1.20.0    goftest_1.2-3         
#>  [67] glue_1.8.0             nlme_3.1-168           rhdf5filters_1.21.4   
#>  [70] promises_1.4.0         grid_4.5.1             Rtsne_0.17            
#>  [73] cluster_2.1.8.1        reshape2_1.4.4         gtable_0.3.6          
#>  [76] spatstat.data_3.1-9    tidyr_1.3.1            data.table_1.17.8     
#>  [79] XVector_0.49.1         spatstat.geom_3.6-0    RcppAnnoy_0.0.22      
#>  [82] ggrepel_0.9.6          RANN_2.6.2             pillar_1.11.1         
#>  [85] stringr_1.5.2          spam_2.11-1            RcppHNSW_0.6.0        
#>  [88] later_1.4.4            splines_4.5.1          dplyr_1.1.4           
#>  [91] BiocFileCache_2.99.6   lattice_0.22-7         bit_4.6.0             
#>  [94] survival_3.8-3         deldir_2.0-4           tidyselect_1.2.1      
#>  [97] miniUI_0.1.2           pbapply_1.7-4          knitr_1.50            
#> [100] gridExtra_2.3          bookdown_0.45          scattermore_1.2       
#> [103] xfun_0.53              stringi_1.8.7          lazyeval_0.2.2        
#> [106] yaml_2.3.10            evaluate_1.0.5         codetools_0.2-20      
#> [109] tibble_3.3.0           BiocManager_1.30.26    cli_3.6.5             
#> [112] uwot_0.2.3             xtable_1.8-4           jquerylib_0.1.4       
#> [115] dichromat_2.0-0.1      Rcpp_1.1.0             spatstat.random_3.4-2 
#> [118] globals_0.18.0         dbplyr_2.5.1           png_0.1-8             
#> [121] spatstat.univar_3.1-4  parallel_4.5.1         blob_1.2.4            
#> [124] ggplot2_4.0.0          dotCall64_1.2          listenv_0.9.1         
#> [127] viridisLite_0.4.2      scales_1.4.0           ggridges_0.5.7        
#> [130] purrr_1.1.0            crayon_1.5.3           rlang_1.1.6           
#> [133] cowplot_1.2.0

5.2 Python

reticulate::py_config()
#> python:         /home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE/bin/python3
#> libpython:      /home/biocbuild/.cache/R/reticulate/uv/python/cpython-3.11.13-linux-x86_64-gnu/lib/libpython3.11.so
#> pythonhome:     /home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE:/home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE
#> virtualenv:     /home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE/bin/activate_this.py
#> version:        3.11.13 (main, Jul 11 2025, 22:43:55) [Clang 20.1.4 ]
#> numpy:          /home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE/lib/python3.11/site-packages/numpy
#> numpy_version:  2.3.4
#> scanpy:         /home/biocbuild/.cache/R/reticulate/uv/cache/archive-v0/uZEkqzFaL9vSWU6B7tDTE/lib/python3.11/site-packages/scanpy
#> 
#> NOTE: Python version was forced by py_require()

reticulate::py_list_packages()
#>              package     version                  requirement
#> 1            anndata      0.12.3              anndata==0.12.3
#> 2   array-api-compat      1.12.0     array-api-compat==1.12.0
#> 3          contourpy       1.3.3             contourpy==1.3.3
#> 4             crc32c         2.8                  crc32c==2.8
#> 5             cycler      0.12.1               cycler==0.12.1
#> 6             donfig 0.8.1.post1          donfig==0.8.1.post1
#> 7          fonttools      4.60.1            fonttools==4.60.1
#> 8               h5py      3.15.1                 h5py==3.15.1
#> 9             joblib       1.5.2                joblib==1.5.2
#> 10        kiwisolver       1.4.9            kiwisolver==1.4.9
#> 11   legacy-api-wrap       1.4.1       legacy-api-wrap==1.4.1
#> 12          llvmlite      0.45.1             llvmlite==0.45.1
#> 13        matplotlib      3.10.7           matplotlib==3.10.7
#> 14            mudata       0.3.2                mudata==0.3.2
#> 15           natsort       8.4.0               natsort==8.4.0
#> 16          networkx         3.5                networkx==3.5
#> 17             numba      0.62.1                numba==0.62.1
#> 18         numcodecs      0.16.3            numcodecs==0.16.3
#> 19             numpy       2.3.4                 numpy==2.3.4
#> 20         packaging        25.0              packaging==25.0
#> 21            pandas       2.3.3                pandas==2.3.3
#> 22             patsy       1.0.2                 patsy==1.0.2
#> 23            pillow      12.0.0               pillow==12.0.0
#> 24       pynndescent      0.5.13          pynndescent==0.5.13
#> 25         pyparsing       3.2.5             pyparsing==3.2.5
#> 26   python-dateutil 2.9.0.post0 python-dateutil==2.9.0.post0
#> 27              pytz      2025.2                 pytz==2025.2
#> 28            PyYAML       6.0.3                PyYAML==6.0.3
#> 29            scanpy      1.11.5               scanpy==1.11.5
#> 30      scikit-learn       1.7.2          scikit-learn==1.7.2
#> 31             scipy      1.16.2                scipy==1.16.2
#> 32           seaborn      0.13.2              seaborn==0.13.2
#> 33     session-info2       0.2.3         session-info2==0.2.3
#> 34               six      1.17.0                  six==1.17.0
#> 35       statsmodels      0.14.5          statsmodels==0.14.5
#> 36     threadpoolctl       3.6.0         threadpoolctl==3.6.0
#> 37              tqdm      4.67.1                 tqdm==4.67.1
#> 38 typing_extensions      4.15.0    typing_extensions==4.15.0
#> 39            tzdata      2025.2               tzdata==2025.2
#> 40        umap-learn 0.5.9.post2      umap-learn==0.5.9.post2
#> 41              zarr       3.1.3                  zarr==3.1.3