troutpy.tl.adaptative_source_score

troutpy.tl.adaptative_source_score#

troutpy.tl.adaptative_source_score(sdata, max_dist=200, lmbda=0.1, max_k=15, ambient_floor=1.5, signal_threshold=3.0, residual=0.1, cell_type_col='leiden', copy=False)#

Score extracellular transcripts against nearby cells using an adaptive neighborhood size.

For each extracellular transcript, a context gene set is built from its k_adaptive nearest extracellular neighbors (where k_adaptive grows with the transcript’s "enrichment_over_random"), then candidate parent cells within max_dist are weighted by how well their expression matches that context and by exponential distance decay.

Parameters:
  • sdata (SpatialData) – SpatialData object containing a "table" AnnData and a "transcripts" points layer with "overlaps_cell", "enrichment_over_random", "gene", "x", and "y" columns.

  • max_dist (float (default: 200)) – Maximum distance (in coordinate units) to search for candidate parent cells.

  • lmbda (float (default: 0.1)) – Decay rate for the exponential distance weighting.

  • max_k (int (default: 15)) – Maximum number of extracellular neighbors used to build the context gene set.

  • ambient_floor (float (default: 1.5)) – "enrichment_over_random" value at or below which k_adaptive is 1.

  • signal_threshold (float (default: 3.0)) – "enrichment_over_random" value at or above which k_adaptive is max_k.

  • residual (float (default: 0.1)) – Constant added to the denominator when normalizing candidate weights, preventing division by zero and capping the maximum normalized score.

  • cell_type_col (str (default: 'leiden')) – Column in sdata["table"].obs containing cell-type annotations.

  • copy (bool (default: False)) – If True, return sdata. Otherwise modify in place and return None.

Return type:

SpatialData | None

Returns:

If copy=True, sdata. Otherwise None, modifying sdata in place.

Notes

Adds a "source_score" table to sdata; "urna_source_score" / "normalized_urna_source_score" columns to sdata["table"].obs; and "aggregated_source_score" / "mean_assignment_score" columns to sdata["xrna_metadata"].var. This is one of two overlapping source-scoring implementations in this module, alongside adaptative_source_score_optimized().