liana.pp.expand_coordinates

liana.pp.expand_coordinates#

liana.pp.expand_coordinates(adata, sample_key, spatial_key='spatial', n_cols=None, margin=0.1)#

Lay out the spatial coordinates of multiple samples side-by-side on a grid.

Each sample is translated into its own cell of a regular grid so that samples no longer overlap in coordinate space (e.g. for joint plotting). Within a sample the transformation is a pure translation, so relative distances are preserved. The original coordinates are stored in obsm[f'{spatial_key}_original'].

Parameters:
adata AnnData

Annotated data object with spatial coordinates in obsm[spatial_key].

sample_key str

Column in adata.obs identifying the sample each observation belongs to.

spatial_key str (default: 'spatial')

Key in adata.obsm holding the (n_obs, 2) coordinate matrix.

n_cols int | None (default: None)

Number of columns in the grid. Defaults to a roughly square layout.

margin float (default: 0.1)

Fractional spacing between grid cells, relative to the largest sample extent. margin=0 packs cells edge-to-edge.

Return type:

AnnData

Returns:

A copy of adata with expanded coordinates in obsm[spatial_key] and the original coordinates preserved in obsm[f'{spatial_key}_original'].

Examples

Lays samples that all live in the same coordinate range out on a grid, so that they no longer overlap when plotted or when neighbours are computed:

>>> import numpy as np
>>> import liana as li
>>> adata = li.ds.generate_toy_spatial()
>>> rng = np.random.default_rng(0)
>>> adata.obs["sample"] = rng.choice(["A", "B", "C", "D"], size=adata.n_obs)
>>> expanded = li.pp.expand_coordinates(adata, sample_key="sample")