vampire.anno.tl.haplotype_neighbor

vampire.anno.tl.haplotype_neighbor#

vampire.anno.tl.haplotype_neighbor(adata, *, aligned_key='aligned', metrics=None, k=None, fusion_method='max', store_key='haplotype')[source]#

Build a fused kNN graph from aligned motif arrays for haplotype analysis.

Computes the requested pairwise distance matrices from aligned motif arrays, converts each into an adaptive kNN similarity graph, fuses the graphs, and stores the fused connectivities in adata.obsp.

Parameters:
  • adata (AnnData) – Annotated data with aligned motif arrays (from tl.sample_msa()).

  • aligned_key (str) – Key prefix for aligned arrays in adata.uns.

  • metrics (list[str | Callable[[Any], ndarray]] | None) – Distance metrics to compute and fuse. Supported values are "structural", "cnv" and "composition" and callable functions. Self-defined callables must take adata as input and return an (n, n) distance matrix aligned to adata.obs_names. If None, defaults to ["structural", "cnv", "composition"].

  • k (int | None) – Number of neighbours for the kNN graph. If None, set to max(5, min(15, int(sqrt(n_samples)))).

  • fusion_method (Literal['max', 'mean', 'min']) – Graph fusion method: "max" (default), "mean", "min". With "max", an edge is retained if it appears in any of the individual graphs, and its similarity weight is set to the maximum weight observed across those graphs. This strategy encourages cells to be grouped together whenever they are highly similar under at least one metric. Instead, with "min", the similarity weight is set to the minimum weight observed across the individual graphs. This strategy emphasizes consensus among metrics and encourages cells to be grouped together only when they are consistently similar in every aspect (e.g. strutural, composition and cnv).

  • store_key (str) – Key prefix for storing results.

Returns:

Updated in-place with:

  • obsp[f"{store_key}_structural_distance"] — if requested

  • obsp[f"{store_key}_cnv_distance"] — if requested

  • obsp[f"{store_key}_composition_distance"] — if requested

  • obsp[f"{store_key}_{func_name}_distance"] — for any custom metric provided as a callable

  • obsp[f"{store_key}_connectivities"] — fused graph

  • uns[store_key] — parameters and graph statistics

Return type:

AnnData

Examples

>>> import vampire as vp
>>> vp.anno.pl.set_default_plotstyle()
>>> adata = vp.datasets.wdr7_hprc()
>>> vp.anno.tl.sample_msa(adata)
>>> vp.anno.tl.haplotype_neighbor(adata, metrics=["structural", "composition"])