liana.mt.df_to_lr

Contents

liana.mt.df_to_lr#

liana.mt.df_to_lr(adata, dea_df, groupby, stat_keys, resource_name='consensus', resource=None, interactions=None, groupby_pairs=None, layer=None, use_raw=False, expr_prop=0.1, min_cells=5, complex_col=None, return_all_lrs=False, source_labels=None, target_labels=None, lr_sep='^', verbose=False)#

Convert DEA results to ligand-receptor pairs.

Parameters:
adata AnnData

Annotated data object.

dea_df DataFrame

DEA results. Index must match adata.var_names

groupby str

Key to be used for grouping.

stat_keys list[str]

List of statistics to be used for ligand-receptor pairs

resource_name str (default: 'consensus')

Name of the resource to be used for ligand-receptor inference. See li.rs.show_resources() for available resources.

resource DataFrame | None (default: None)

A pandas dataframe with [ligand, receptor] columns. If provided will overrule the resource requested via resource_name

interactions list[tuple[str, str]] | None (default: None)

List of tuples with ligand-receptor pairs [(ligand, receptor), ...] to be used for the analysis. If passed, it will overrule the resource requested via resource and resource_name.

groupby_pairs DataFrame | None (default: None)

A DataFrame with columns source and target to be used to subset the possible combinations of interacting cell types. If None, all possible combinations are used.

layer str | None (default: None)

Layer in anndata.AnnData.layers to use. If None, use anndata.AnnData.X.

use_raw bool (default: False)

Whether to use the .raw attribute of adata. Defaults to False (uses .X).

expr_prop float (default: 0.1)

Minimum expression proportion for the ligands and receptors (+ their subunits) in the corresponding cell identities. Set to 0 to return unfiltered results.

min_cells int (default: 5)

Minimum cells (per cell identity if grouped by groupby) to be considered for downstream analysis.

complex_col str | None (default: None)

Column in dea_df to use for complex expression. Default is None. If None, will use mean expression (‘expr’) calculated per group in groupby.

return_all_lrs bool (default: False)

Bool whether to return all ligand-receptor pairs, or only those that surpass the expr_prop threshold. Ligand-receptor pairs that do not pass the expr_prop threshold will be assigned to the worst score of the ones that do. False by default.

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

List of labels to use as source, the rest are filtered out.

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

List of labels to use as target, the rest are filtered out.

lr_sep str (default: '^')

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

verbose bool (default: False)

Verbosity flag.

Return type:

DataFrame

Returns:

Returns a pd.DataFrame with joined ligand-receptor pairs and statistics.

Raises:
  • ValueError – If the groupby value is not in adata or dea_df, if dea_df indexes do not match adata.var_names or if complex_col does not match one of the computed stats.

  • AssertionError – If there’s no match when grouping-by between adata.obs and dea_df.

Examples

dea_df holds per-cell-type differential expression statistics, indexed by gene. It normally comes from a tool such as pydeseq2 or scanpy; a stand-in is built here so the example stays offline:

>>> import numpy as np
>>> import pandas as pd
>>> import liana as li
>>> adata = li.ds.generate_toy_adata()
>>> groups = adata.obs["bulk_labels"].cat.categories
>>> rng = np.random.default_rng(1337)
>>> dea_df = pd.DataFrame(
...     {"bulk_labels": np.repeat(groups, adata.n_vars), "stat": rng.normal(size=len(groups) * adata.n_vars)},
...     index=np.tile(adata.var_names, len(groups)),
... )
>>> lr_res = li.mt.df_to_lr(adata, dea_df=dea_df, groupby="bulk_labels", stat_keys=["stat"])

Each statistic named in stat_keys is carried over to both sides of every interaction – as ligand_stat and receptor_stat – and averaged into an interaction_stat column.