- spac.spatial_analysis.calculate_nearest_neighbor(adata, annotation, spatial_associated_table='spatial', imageid=None, label='spatial_distance', verbose=True)[source]
Computes the shortest distance from each cell to the nearest cell of each phenotype (via scimap.tl.spatial_distance) and stores the resulting DataFrame in adata.obsm[label].
- Parameters:
adata (anndata.AnnData) – Annotated data matrix with spatial information.
annotation (str) – Column name in adata.obs containing cell annotationsi (i.e. phenotypes).
spatial_associated_table (str, optional) – Key in adata.obsm where spatial coordinates are stored. Default is ‘spatial’.
imageid (str, optional) – The column in adata.obs specifying image IDs. If None, a dummy image column is created temporarily. Spatial distances are computed across the entire dataseti as if it’s one image.
label (str, optional) – The key under which results are stored in adata.obsm. Default is ‘spatial_distance’.
verbose (bool, optional) – If True, prints progress messages. Default is True.
- Returns:
Modifies adata in place by storing a DataFrame of spatial distances in adata.obsm[label].
- Return type:
None
Example
For a dataset with two cells (CellA, CellB) both of the same phenotype “type1”, the output might look like:
>>> adata.obsm['spatial_distance'] type1 CellA 0.0 CellB 0.0
For a dataset with two phenotypes “type1” and “type2”, the output might look like:
>>> adata.obsm['spatial_distance'] type1 type2 CellA 0.00 1.414214 CellB 1.414214 0.00
- Input:
- adata.obs:
cell_type imageid type1 image1 type1 image1 type2 image1
- adata.obsm[‘spatial’]:
[[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]
- Output stored in adata.obsm[‘spatial_distance’]:
type1 type2
0 0.0 1.414 1 1.414 0.0 2 2.236 1.0
- Raises:
ValueError – If spatial_associated_table is not found in adata.obsm. If spatial coordinates are missing or invalid.