troutpy.pp.segmentation_free_sainsc

troutpy.pp.segmentation_free_sainsc#

troutpy.pp.segmentation_free_sainsc(sdata, binsize=3, celltype_key='leiden', background_filter=0.4, gaussian_kernel_key=2.5, n_threads=16, resolution=1000, return_sainsc=False, copy=False, default_cell_type='unknown', default_numeric=nan)#

Assign a per-bin cell type and uRNA probability via SAiNSC segmentation-free analysis.

Bins all transcripts on a regular grid using sainsc.LazyKDE, computes a cosine-similarity map between each bin’s local expression and cell-type signatures derived from sdata["table"], and assigns each bin to its closest cell type. A Bayesian probability of being extracellular (prob_is_urna) is then derived from the cosine-similarity distributions of bins overlapping vs. not overlapping segmented cells, and the per-bin results are mapped back onto every transcript.

Parameters:
  • sdata (spatialdata.SpatialData) – SpatialData object with a "transcripts" points layer ("gene", "x", "y", "overlaps_cell") and a "table" AnnData with cell-type annotations in obs[celltype_key].

  • binsize (float, optional) – Spatial bin size, in micrometres, used by SAiNSC. Defaults to 3.

  • celltype_key (str, optional) – Column in sdata["table"].obs containing cell-type labels used to build reference signatures. Defaults to "leiden".

  • background_filter (float, optional) – Total-mRNA KDE threshold below which bins are treated as background by sainsc.LazyKDE.filter_background(). Defaults to 0.4.

  • gaussian_kernel_key (float, optional) – Bandwidth (in micrometres) of the Gaussian kernel used to smooth total mRNA counts. Defaults to 2.5.

  • n_threads (int, optional) – Number of threads/processes used by SAiNSC and bin assignment. Defaults to 16.

  • resolution (int, optional) – Resolution (in nanometres per pixel) passed to sainsc.LazyKDE.from_dataframe(). Defaults to 1000.

  • return_sainsc (bool, optional) – If True, additionally return a dict with the underlying LazyKDE instance ("brain") and the per-bin results table ("output_df"). Defaults to False.

  • copy (bool, optional) – If True, return the modified SpatialData object. Otherwise modify sdata in place and return None. Ignored if return_sainsc=True. Defaults to False.

  • default_cell_type (str, optional) – Reserved for future use; currently has no effect. Defaults to "unknown".

  • default_numeric (float, optional) – Reserved for future use; currently has no effect. Defaults to numpy.nan.

Returns:

spatialdata.SpatialData or None

SpatialData with closest_cell_type, cosine_similarity, prob_is_urna, and extracellular columns added to the "transcripts" layer if copy=True; otherwise None.

tuple[spatialdata.SpatialData, dict]

If return_sainsc=True, returns (sdata, {"brain": LazyKDE, "output_df": pandas.DataFrame}) instead of the above.