liana.ms.lrs_to_views

Contents

liana.ms.lrs_to_views#

liana.ms.lrs_to_views(adata, score_key=None, inverse_fn=<function DefaultValues.inverse_fn>, obs_keys=None, lr_prop=0.5, lr_fill=nan, lrs_per_view=20, lrs_per_sample=10, samples_per_view=3, min_variance=0, min_var_nbatches=1, batch_key=None, lr_sep='^', cell_sep='&', var_sep=':', uns_key='liana_res', sample_key='sample', source_key='source', target_key='target', ligand_key='ligand_complex', receptor_key='receptor_complex', verbose=False)#

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

Parameters:
adata AnnData

Annotated data object.

score_key str | None (default: None)

Column name of the score in liana_res. If None, the score is inferred from the method.

inverse_fn Callable[[Series], Series] (default: <function DefaultValues.inverse_fn at 0x7e3fdb8699e0>)

Function applied to scores for which a lower value is the stronger one – p-values and aggregate ranks such as magnitude_rank – so that “higher is stronger” holds throughout. Defaults to -log10(x + eps). Which scores are inverted is decided by liana.mt.get_method_scores, so this is handled automatically for liana’s own scores.

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

List of keys in adata.obs that should be included in the MuData object. These columns should correspond to the number of samples in adata.obs[sample_key].

lr_prop float (default: 0.5)

Reflects the minimum required proportion of samples for an interaction to be considered for building the views.

lr_fill float (default: nan)

Value to fill in for interactions that are not present in a view. Default is np.nan.

lrs_per_view int (default: 20)

Reflects the minimum required number of interactions in a view to be considered for building the views.

lrs_per_sample int (default: 10)

Reflects the minimum required number of interactions in a sample to be considered when building a specific view.

samples_per_view int (default: 3)

Reflects the minimum required samples to keep a view.

min_variance int (default: 0)

Reflects the minimum required variance across samples for each interaction in each view. NaNs are ignored when computing the variance.

min_var_nbatches int (default: 1)

Reflect the minimum number of batches (>=) that must have a variance above min_variance for an interaction to be included in the view.

batch_key str | None (default: None)

Key in adata.obs that represents the batch information. Used solely when computing the variance. If batch_key is not None, the variance is computed per batch, and the ``

lr_sep str (default: '^')

Separator to use when joining ligand and receptor names into interactions.

cell_sep str (default: '&')

Separator to use for the cell names in the views.

var_sep str (default: ':')

Separator to use for the variable names in the views.

uns_key str (default: 'liana_res')

Key in adata.uns that contains the LIANA results. Default is 'liana_res'.

sample_key str (default: 'sample')

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

source_key str (default: 'source')

Column name of the sender/source cell types in liana_res.

target_key str (default: 'target')

Column name of the receiver/target cell types in liana_res.

ligand_key str (default: 'ligand_complex')

Column name of the ligand in liana_res.

receptor_key str (default: 'receptor_complex')

Column name of the receptor in liana_res.

verbose bool (default: False)

Verbosity flag.

Return type:

MuData

Returns:

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

Raises:

ValueError – If any of the provided keys are not found in the corresponding adata view.

Examples

Expects a by-sample ligand-receptor result in adata.uns, as written by any method’s .by_sample. A toy result stands in here:

>>> import liana as li
>>> adata = li.ds.generate_toy_adata()
>>> adata.uns["liana_res"] = li.ds.sample_lrs(by_sample=True)
>>> mdata = li.ms.lrs_to_views(
...     adata,
...     score_key="specificity_rank",
...     obs_keys=["case"],
...     lr_prop=0.1,
...     lrs_per_sample=0,
...     lrs_per_view=5,
...     samples_per_view=0,
...     min_variance=-1,
... )

Each source-target cell type pair becomes a view named 'source&target', of interactions by sample. The thresholds are relaxed below their defaults here only because the toy scores are random and no view would otherwise survive them – on real data the defaults are the sensible starting point.