How to Convert AnnData (.h5ad) to a Seurat Object in R

Updated

A collaborator, a CELLxGENE download, or a Scanpy pipeline gave you an .h5ad file, and your analysis is in Seurat. Here are the reliable ways to get an AnnData file into a Seurat object (and save it as .rds), with the checks that keep counts and normalized data from being mixed up.

Short answer: obj <- anndataR::read_h5ad("file.h5ad", as = "Seurat"), then saveRDS(obj, "file.rds"). No R setup? Use the online h5ad → rds converter.

Step 0: check what's in the file

Before converting, find out where the raw counts are. In Python:

import anndata as ad
adata = ad.read_h5ad("file.h5ad", backed="r")
print(adata)                    # layers, obsm, uns keys
print(adata.X[:5, :5])          # integers = counts, decimals = normalized
print(adata.raw)                # raw counts are sometimes here

Option 1: anndataR (recommended)

BiocManager::install("anndataR")   # or: remotes::install_github("scverse/anndataR")
library(anndataR)
library(Seurat)

obj <- read_h5ad("file.h5ad", as = "Seurat")
obj
saveRDS(obj, "file.rds")

anndataR maps .obs to meta.data, .var to feature metadata, .layers to assay layers, and .obsm embeddings (X_pca, X_umap) to DimReducs, so DimPlot(obj, reduction = "umap") works right away. If the file is very large, you can use as = "HDF5AnnData" to inspect it on disk before loading everything into memory.

Option 2: zellkonverter → SingleCellExperiment → Seurat

BiocManager::install("zellkonverter")
library(zellkonverter)
library(Seurat)

sce <- readH5AD("file.h5ad")
# If X is normalized and counts live in layers["counts"]:
obj <- as.Seurat(sce, counts = "counts", data = "X")
# If X holds raw counts:
# obj <- as.Seurat(sce, counts = "X", data = NULL)

Option 3: SeuratDisk (legacy)

library(SeuratDisk)
Convert("file.h5ad", dest = "h5seurat", overwrite = TRUE)
obj <- LoadH5Seurat("file.h5seurat")

This still appears in many tutorials, but it fails on files written by anndata 0.8 or newer (most files today). See SeuratDisk errors and fixes.

Option 4: online converter (no code)

SingleCellConvert runs the anndataR conversion on a server: upload the .h5ad (up to 20 GB) and download a Seurat .rds. It's useful when you don't have R and Python environments that agree, or when the file is too big for your laptop's RAM. Spatial h5ad files (Visium obsm["spatial"] plus images) are mapped back into the Seurat object. Conversions start at $2.50 per file.

What maps where

AnnDataSeurat
adata.obsobj@meta.data
adata.varAssay feature metadata
adata.X, adata.layersAssay layers (counts, data)
adata.obsm["X_umap"]obj[["umap"]]
adata.obspGraphs
adata.unsobj@misc

Common problems

Normalized data loaded as counts

If adata.X is log-normalized and becomes Seurat's counts, NormalizeData() will normalize twice and SCTransform() will complain about non-integer values. Map the real counts layer explicitly (Step 0 above).

Gene IDs instead of symbols

CELLxGENE files use Ensembl IDs as var_names and keep symbols in var["feature_name"]. Rename features before converting if you want to plot by gene symbol.

Out of memory

Dense .X matrices expand dramatically in R. Make sure the matrix is sparse in Python (scipy.sparse.csr_matrix) before writing, or convert on a machine with more RAM.

Frequently asked questions

How do I read an h5ad file in R?

Install anndataR and run anndataR::read_h5ad("file.h5ad", as = "Seurat") to get a Seurat object, or as = "SingleCellExperiment" for Bioconductor workflows.

How do I convert h5ad to rds?

Load the h5ad into a Seurat object (for example with anndataR::read_h5ad(..., as = "Seurat")) and save it with saveRDS(obj, "file.rds"). An .rds file is just a serialized R object, so the conversion is really h5ad → Seurat object.

Why does SeuratDisk fail with "Missing required datasets 'levels' and 'values'"?

Newer versions of anndata (0.8+) store categorical columns in a format SeuratDisk does not understand. Use anndataR or zellkonverter instead, or convert categoricals to strings in Python before writing the file.

Should adata.X be counts or normalized data in Seurat?

It depends on the file. Many h5ad files keep log-normalized values in .X and raw counts in .layers['counts'] or .raw. Check which is which in Python before converting, and map raw counts to Seurat's counts layer.

Related guides