liana.ms.to_tensor_c2c

Contents

liana.ms.to_tensor_c2c#

liana.ms.to_tensor_c2c(adata=None, sample_key=None, score_key=None, liana_res=None, source_key='source', target_key='target', ligand_key='ligand_complex', receptor_key='receptor_complex', uns_key='liana_res', non_expressed_fill=None, inverse_fn=<function DefaultValues.inverse_fn>, non_negative=True, return_dict=False, **kwargs)#

Function to convert a LIANA result to a tensor for cell2cell analysis.

Parameters:
adata AnnData | None (default: None)

Annotated data object.

sample_key str | None (default: None)

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

score_key str | None (default: None)

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

liana_res DataFrame | None (default: None)

A dataframe with the LIANA results. If None, it will be taken from adata.uns[uns_key].

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.

uns_key str (default: 'liana_res')

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

non_expressed_fill float | None (default: None)

Value to fill for non-expressed ligand-receptor pairs.

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.

non_negative bool (default: True)

Whether to make the tensor non-negative.

return_dict bool (default: False)

Whether to return a dictionary of tensors.

**kwargs object

keyword arguments to pass to Tensor-cell2cell’s cell2cell.tensor.external_scores.dataframes_to_tensor function.

Return type:

InteractionTensor | dict[Hashable, DataFrame]

Returns:

Returns a tensor of shape (n_samples, n_senders, n_receivers, n_interactions) or a dictionary of tensors if return_dict is True.

Raises:
  • AssertionError – If uns_key is not provided when given an adata instance.

  • ValueError – If neither liana_res or adata are provided, or if any of the provided keys (sample_key, source_key, target_key, ligand_key or receptor_key) are not found in liana_res.uns[uns_key] or if input data contains duplicates.

Examples

Expects a by-sample ligand-receptor result, 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)
>>> tensor = li.ms.to_tensor_c2c(adata, sample_key="sample", score_key="specificity_rank")

The tensor can then be decomposed with Tensor-cell2cell.