tfmindi.pp.extract_seqlets

tfmindi.pp.extract_seqlets#

tfmindi.pp.extract_seqlets(contrib, oh, method='recursive_q99_abs_smooth', motifs=None, **method_kwargs)#

Extract, scale, and process seqlets from saliency maps.

Seqlets are called from either the projected 1D attribution track (contrib * oh).sum(1) or, for "finemo_fit_contrib", directly from the raw contrib/oh/motifs, using the selected method; each seqlet’s contribution matrix is then normalized by its maximum absolute contribution value and sign-corrected.

Parameters:
  • contrib (ndarray) – Contribution scores array with shape (n_examples, 4, length)

  • oh (ndarray) – One-hot encoded sequences array with shape (n_examples, 4, length)

  • method (Literal['recursive_q99_abs_smooth', 'recursive_raw', 'hysteresis', 'local_contrast', 'wavelet_otsu', 'finemo_fit_contrib'] (default: 'recursive_q99_abs_smooth')) –

    Seqlet-calling algorithm to use. One of:

    • "recursive_q99_abs_smooth" (default): triangular-smooth, per-example q99 normalisation, then the recursive caller on abs(track). Accepts smooth_window (9), threshold (0.05), min_seqlet_len (4), max_seqlet_len (25), additional_flanks (3), n_bins (1000). If calling seqlets on data with a large dynamic range, consider increasing n_bins.

    • "recursive_raw": recursive caller on the raw signed track (reproduces the previous TF-MInDi default behaviour). Same knobs as above.

    • "hysteresis": two-threshold local caller. Accepts smooth_window, seed_z (2.5), grow_z (1.0), min_seqlet_len (4), max_seqlet_len (25), merge_gap (2).

    • "local_contrast": multi-scale sliding-window contrast caller. Accepts windows (10, 16, 24), smooth_window (9), seed_z (4.0), expand_z (1.25), …

    • "wavelet_otsu": wavelet-denoise + Otsu-threshold caller (needs PyWavelets). Accepts wavelet, threshold_scale (1.7), otsu_weight, min_seqlet_len, …

    • "finemo_fit_contrib": fits motifs (after IC scaling) directly against the raw contrib/oh arrays via the finemo package (GPU-optimized), instead of thresholding the projected 1D track. Requires motifs to be given. Accepts ic_trim_threshold (0.2), ic_background, pseudocount, include_rc, include_neg, compile_optimizer, batch_size.

  • motifs (dict[str, ndarray] | None (default: None)) – Mapping of motif_id -> PFM array of shape (4, W) in A, C, G, T order, each column summing to 1. Only used (and required) when method="finemo_fit_contrib".

  • **method_kwargs – Method-specific hyperparameters forwarded to the selected caller, overriding its defaults, e.g. threshold=0.1 for the recursive methods, seed_z=3.0 for hysteresis, threshold_scale=2.0 for wavelet_otsu. Passing a keyword the chosen caller does not accept raises a TypeError. See the per-method caller classes for the full parameter lists (rec_q99_smooth_abs, recursive_raw, hysteresis, local_contrast, wavelet_otsu, finemo_fit_contrib).

Return type:

tuple[DataFrame, list[ndarray]]

Returns:

  • DataFrame with seqlet coordinates and scores [example_idx, start, end, attribution, score]

  • List of processed seqlet contribution matrices

Examples

>>> seqlets_df, seqlet_matrices = extract_seqlets(contrib, oh)
>>> print(seqlets_df.columns.tolist())
['example_idx', 'start', 'end', 'attribution', 'score']
>>> # switch caller and tune it in one call
>>> seqlets_df, seqlet_matrices = extract_seqlets(contrib, oh, method="hysteresis", seed_z=3.0)
>>> # fit known motifs directly against the contribution scores via finemo
>>> seqlets_df, seqlet_matrices = extract_seqlets(contrib, oh, method="finemo_fit_contrib", motifs=motifs)