anndict.adata_dict.build_adata_dict#
- anndict.adata_dict.build_adata_dict(adata, strata_keys, *, desired_strata=None)[source]#
Build a dictionary of
AnnData
objects split by theobs
columns specified instrata_keys
.- Parameters:
- adata
AnnData
The input
AnnData
.- strata_keys
list
[str
] List of column names in
adata.obs
to use for stratification.- desired_strata
list
|dict
|None
(default:None
) List of desired strata tuples or a dictionary where keys are the col names supplied in
strata_keys
, and values are lists of desired strata. If None (Default), all combinations of categories inadata.obs[strata_keys]
will be used.
- adata
- Return type:
- Returns:
Flat dictionary of class
AdataDict
where keys are strata tuples and values are correspondingAnnData
subsets.- Raises:
ValueError – If desired_strata is neither a list nor a dictionary of lists.
Examples
Case 1: Take all groups
import pandas as pd from anndata import AnnData # Create an example AnnData object adata = AnnData(obs=pd.DataFrame({ "Donor": ["Donor1", "Donor1", "Donor2"], "Tissue": ["Tissue1", "Tissue2", "Tissue1"] })) strata_keys = ["Donor", "Tissue"] adata_dict = build_adata_dict(adata, strata_keys) print(adata_dict) > { > ("Donor1", "Tissue1"): adata_d1_t1, > ("Donor1", "Tissue2"): adata_d1_t2, > ("Donor2", "Tissue1"): adata_d2_t1, > }
Case 2: Take only some groups
import pandas as pd from anndata import AnnData # Create an example AnnData object adata = AnnData(obs=pd.DataFrame({ "Donor": ["Donor1", "Donor1", "Donor2"], "Tissue": ["Tissue1", "Tissue2", "Tissue1"] })) strata_keys = ["Donor", "Tissue"] desired_strata = { "Donor": ["Donor1"], #Take only Donor1 "Tissue": ["Tissue1", "Tissue2"] #Take Tissue1 and Tissue2 } adata_dict = build_adata_dict(adata, strata_keys, desired_strata=desired_strata) print(adata_dict) > { > ("Donor1", "Tissue1"): adata_d1_t1, > ("Donor1", "Tissue2"): adata_d1_t2 > }