Skip to contents

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.

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.

Basic Integration with scanpy

Install required Python packages if needed:

reticulate::py_install("scanpy")

Load a dataset directly from scanpy:

adata <- sc$datasets$pbmc3k_processed()
print(adata)

Apply scanpy functions directly:

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

Conversion to R objects

Convert to Seurat:

seurat_obj <- adata$as_Seurat()
print(seurat_obj)

Convert to SingleCellExperiment:

sce_obj <- adata$as_SingleCellExperiment()
print(sce_obj)

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"
)

mdata <- md$read_h5mu(h5mu_file)

Access individual modalities and convert them:

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

rna_seurat <- rna_mod$as_Seurat()
print(rna_seurat)

rna_sce <- rna_mod$as_SingleCellExperiment()
print(rna_sce)

Session info

Python

reticulate::py_config()

reticulate::py_list_packages()