Build an AdataDict#
This tutorial demonstrates how to build an AdataDict.
There are two ways to build an AdataDict:
From an already loaded anndata object
Reading anndata into an AdataDict from disk
1. From an already loaded anndata#
The main function for this method is build_adata_dict()
.
import anndict as adt
import scanpy as sc
#load an anndata
adata = sc.datasets.pbmc3k_processed()
# Rename obs column (for legibility)
adata.obs['cell_type'] = adata.obs['louvain']
# Build an AdataDict from this anndata
adata_dict = adt.build_adata_dict(adata, strata_keys=['cell_type'])
# We can optionally select only certain groups when we make the adata_dict
# The desired_strata are specified as a list of tuples becaues the keys of an AdataDict are tuples
adata_dict = adt.build_adata_dict(adata, strata_keys=['cell_type'], desired_strata=[('CD4 T cells',), ('CD14+ Monocytes',)])
2. Reading anndata into an AdataDict from disk#
The main function used for this method is read_adata_dict_from_h5ad()
. Note that it can be used in several ways as outlined below.
adata_dict = adt.read_adata_dict_from_h5ad('path/to/anndata.h5ad') # To load anndata.h5ad into a single-element AdataDict
adata_dict = adt.read_adata_dict_from_h5ad('path/to/dir/with/several/anndata/') # To load all anndata.h5ad files in a directory into an AdataDict (1 element per h5ad file)
adata_dict = adt.read_adata_dict_from_h5ad(['path/to/anndata.h5ad', 'path/to/dir/with/several/anndata/']) # Loads everything in the list into an AdataDict
# You can also manuall specifiy the AdataDict keys when you load
adata_dict = adt.read_adata_dict_from_h5ad(['path/to/anndata1.h5ad', 'path/to/anndata2.h5ad'], keys=['ad1', 'ad2'])