Skip to content

Instantly share code, notes, and snippets.

@alinaselega
Last active December 14, 2023 15:26
Show Gist options
  • Save alinaselega/eb5d808430be5c63572253072af69ded to your computer and use it in GitHub Desktop.
Save alinaselega/eb5d808430be5c63572253072af69ded to your computer and use it in GitHub Desktop.
Convert Seurat object/SingleCellExperiment to AnnData in R
# run R in an activated Python environment that has AnnData installed
library(Seurat)
library(reticulate)
# check that the environment is found
reticulate::py_discover_config()
file_rds <- readRDS("file.rds")
# Option 1
# file.rds is an SCE object
library(zellkonverter)
converted_data <- SCE2AnnData(file_rds)
library(anndata)
write_h5ad(converted_data, "file.h5ad")
# Option 2
# file.rds is a Seurat object
sce <- as.SingleCellExperiment(file_rds)
converted_data <- SCE2AnnData(sce)
library(anndata)
write_h5ad(converted_data, "file.h5ad")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment