liana.ms.adata_to_views

Contents

liana.ms.adata_to_views#

liana.ms.adata_to_views(adata, groupby, sample_key, obs_keys=None, view_sep=':', keep_stats=False, verbose=False, psbulk_kwargs=None, filter_samples_kwargs=None, filter_by_expr_kwargs=None, filter_by_prop_kwargs=None)#

Converts an AnnData object to a MuData object with views that represent an aggregate for each entity in adata.obs[groupby].

Parameters:
adata AnnData

Annotated data object.

groupby str

Key to be used for grouping.

sample_key str

key in adata.obs to use for grouping by sample or context.

obs_keys list[str] | None (default: None)

Column names in adata.obs to merge with the MuData object

view_sep str (default: ':')

Separator to use when assigning adata.var_names to views

keep_stats bool (default: False)

If True, keep the pseudobulk statistics in mdata.uns['psbulk_stats']. Default is False.

verbose bool (default: False)

Verbosity flag.

psbulk_kwargs dict[str, object] | None (default: None)

Arguments to pass to dc.pp.pseudobulk for pseudobulking. See decoupler documentation for more details.

filter_samples_kwargs dict[str, object] | None (default: None)

Arguments to pass to dc.pp.filter_samples for filtering samples. See decoupler documentation for more details. If None, won’t filter.

filter_by_expr_kwargs dict[str, object] | None (default: None)

Optional mapping of arguments to pass to dc.pp.filter_by_expr for gene filtering by expression. If None, won’t filter.

filter_by_prop_kwargs dict[str, object] | None (default: None)

Optional mapping of arguments to pass to dc.pp.filter_by_prop for gene filtering by proportion of cells that express the gene. If None, won’t filter.

Return type:

MuData

Returns:

Returns a MuData object with views that represent an aggregate for each entity in adata.obs[groupby].

Examples

Each cell type becomes a view of pseudobulk profiles, with samples as the observations – the multicellular structure that MOFA is then fit on:

>>> import liana as li
>>> adata = li.ds.generate_toy_adata()
>>> mdata = li.ms.adata_to_views(
...     adata,
...     groupby="bulk_labels",
...     sample_key="sample",
...     obs_keys=["case"],
...     psbulk_kwargs={"raw": True, "skip_checks": True},
... )

Only the views that survive the expression filters are kept, each variable is prefixed with its view, and obs_keys are joined onto the sample-level .obs. Pass filter_by_expr_kwargs to tighten or relax those filters – they go straight to decoupler.