data-beans 0.6.11

Sparse genomics data backends, QC, algorithms, and simulation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Feature-name kind + canonicalizer hooks for multi-file data
//! alignment.
//!
//! NOT to be confused with the sibling [`crate::aux::feature_rows`]. That module
//! defines the row-name **grammar** faba's producers emit
//! (`{unit}/{modality}/{subunit}/{channel}`); this one **canonicalizes** a name
//! that already exists, so the same gene or locus matches across files that
//! spell it differently. Reach for `feature_rows` to build or split a row, and
//! for this module to decide whether two spellings are the same feature.
//!
//! Loaders that union rows across multiple sparse backends
//! ([`crate::aux::data_loading::read_data_on_shared_rows`]) can opt into
//! generous matching by passing a [`FeatureNameKind`] — same row-name
//! canonicalization machinery used by `senna marker` /
//! `FeaturePairGraph::from_edge_list` (via
//! [`legume_numeric::matrix::membership::GeneIndexResolver`]) but plumbed at the
//! [`crate::sparse_io_vector::SparseIoVec`] level so the row
//! intersection itself sees aligned names.
//!
//! The two flavors cover what biology pipelines see in practice:
//!
//! - [`FeatureNameKind::Gene`] for gene rows in scRNA / spatial-RNA
//!   data, where the same gene shows up as `TGFB1`, `ENSG00000105329`,
//!   or `ENSG00000105329_TGFB1` across cohorts;
//! - [`FeatureNameKind::Locus`] for chromosome-coordinate rows in ATAC
//!   / chickpea-style data, where `chr1:1000-2000`, `chr1_1000_2000`,
//!   and `1:1000-2000` should all resolve to the same peak.

use std::sync::Arc;

use crate::sparse_io_vector::RowNameCanonicalizer;
use legume_numeric::matrix::membership::canon_locus;
use rustc_hash::FxHashMap as HashMap;

/// Per-name canonicalization rule for cross-backend row alignment.
/// Concrete strategy only — no "request" variants. Callers that want
/// auto-detection pass [`None`] (or whatever wrapping enum they choose)
/// and call [`FeatureNameKind::auto_detect`] once row names are in hand.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum FeatureNameKind {
    /// Strict string match — no canonicalization. Default.
    #[default]
    Exact,
    /// Gene-symbol rule: register every `delim`-split component as an
    /// alias of the full name. `ENSG00000105329_TGFB1` and `TGFB1`
    /// resolve to the same row.
    Gene { delim: char },
    /// Genomic-locus rule. Normalizes formats (`chr1:1000-2000`,
    /// `1:1000-2000`, `chr1_1000_2000` → `1_1000_2000`). If
    /// `merge_overlapping`, intervals that overlap on the same
    /// chromosome additionally collapse into one cluster
    /// (`chr1:1-20` ∪ `chr1:15-30` → `1_1_30`). Useful for ATAC peak
    /// sets called independently across datasets.
    Locus { merge_overlapping: bool },
    /// Heterogeneous axis: dispatch per row name. Names that parse as
    /// loci go through [`FeatureNameKind::Locus`] with overlap merging;
    /// names with `_` use [`FeatureNameKind::Gene`]; the rest pass
    /// through. Picked automatically when [`auto_detect`] finds both
    /// signatures in the same axis (e.g. paired RNA + ATAC union).
    Mixed,
}

impl FeatureNameKind {
    /// Canonicalize a single name under this kind's per-name rule.
    /// [`Locus { merge_overlapping: true }`] and [`Mixed`] only describe
    /// the per-name part here (format normalization for loci, last-token
    /// split for gene-style); the global cluster step lives in
    /// [`build_locus_overlap_canonical_map`] and is installed by
    /// [`build_canonicalizer`].
    pub fn canonicalize(&self, name: &str) -> Box<str> {
        match self {
            FeatureNameKind::Exact => name.into(),
            FeatureNameKind::Gene { delim } => gene_canonicalize(name, *delim),
            FeatureNameKind::Locus { .. } => canon_locus(name),
            FeatureNameKind::Mixed => {
                if parse_locus(name).is_some() {
                    canon_locus(name)
                } else if name.contains('_') {
                    gene_canonicalize(name, '_')
                } else {
                    name.into()
                }
            }
        }
    }

    /// True iff this kind is [`Exact`] — no canonicalizer needed.
    pub fn is_exact(&self) -> bool {
        matches!(self, FeatureNameKind::Exact)
    }

    /// True iff installing the canonicalizer requires peeking every row
    /// name across all backends first (to build the locus-overlap cluster
    /// map). Loaders branch on this.
    pub fn needs_global_pass(&self) -> bool {
        matches!(
            self,
            FeatureNameKind::Locus {
                merge_overlapping: true
            } | FeatureNameKind::Mixed
        )
    }

    /// Sniff `names` and pick the right kind. Tallies:
    /// `n_locus` = count where `parse_locus` matches; `n_gene_like` =
    /// count of remaining names that contain `_` (loci with `_`
    /// separators are not double-counted as gene-like). Decision:
    /// both ≥ 10% → [`Mixed`]; else loci ≥ 50% →
    /// `Locus { merge_overlapping: true }`; else gene-like ≥ 50% →
    /// `Gene { delim: '_' }`; else [`Exact`].
    pub fn auto_detect(names: &[Box<str>]) -> Self {
        let n = names.len();
        if n == 0 {
            return Self::Exact;
        }
        let mut n_locus = 0usize;
        let mut n_gene_like = 0usize;
        for name in names {
            if parse_locus(name).is_some() {
                n_locus += 1;
            } else if name.contains('_') {
                n_gene_like += 1;
            }
        }
        let pct_locus = n_locus as f32 / n as f32;
        let pct_gene = n_gene_like as f32 / n as f32;
        if pct_locus >= 0.10 && pct_gene >= 0.10 {
            Self::Mixed
        } else if pct_locus >= 0.50 {
            Self::Locus {
                merge_overlapping: true,
            }
        } else if pct_gene >= 0.50 {
            Self::Gene { delim: '_' }
        } else {
            Self::Exact
        }
    }

    /// The one kind to install for a set of files that were each sniffed
    /// with [`auto_detect`](Self::auto_detect) on their own.
    ///
    /// Sniffing the POOLED names does not work: the signature usually lives
    /// on one side only. A raw `ENSG_SYM` cohort pooled with a reference
    /// already on the bare-symbol axis (a carried `pb_reference`, a
    /// symbol-keyed panel) leaves the gene-like share under half, the pair
    /// sniffs as `Exact`, and every gene becomes two rows. Canonicalizing
    /// under `Gene` is a no-op for names lacking the delimiter, so adopting
    /// the informative side is safe for both.
    ///
    /// Gene-style and locus-style files together dispatch per name
    /// (`Mixed`), which is what `auto_detect` would pick on one axis holding
    /// both; `Mixed` anywhere stays `Mixed`; all-`Exact` stays `Exact`.
    #[must_use]
    pub fn reconcile(kinds: &[FeatureNameKind]) -> FeatureNameKind {
        if kinds.iter().any(|k| matches!(k, FeatureNameKind::Mixed)) {
            return FeatureNameKind::Mixed;
        }
        let gene = kinds
            .iter()
            .find(|k| matches!(k, FeatureNameKind::Gene { .. }));
        let locus = kinds
            .iter()
            .find(|k| matches!(k, FeatureNameKind::Locus { .. }));
        match (gene, locus) {
            (Some(_), Some(_)) => FeatureNameKind::Mixed,
            _ => gene.or(locus).cloned().unwrap_or(FeatureNameKind::Exact),
        }
    }

    /// Build a `RowNameCanonicalizer` suitable for
    /// [`SparseIoVec::with_row_canonicalizer`]. Returns `None` for
    /// [`FeatureNameKind::Exact`] so callers don't install a no-op
    /// closure. **Does not** handle the LocusOverlap global step — for
    /// that, use [`build_locus_overlap_canonicalizer`].
    pub fn into_canonicalizer(self) -> Option<RowNameCanonicalizer> {
        if self.is_exact() {
            return None;
        }
        Some(Arc::new(move |name: &str| self.canonicalize(name)))
    }
}

/// Parse a row name as `(chr, start, end)`. Accepts either
/// `chr1:1000-2000`, `1:1000-2000`, `chr1_1000_2000`, or `1_1000_2000`.
/// Returns `None` for anything that doesn't match — those names pass
/// through the overlap pass untouched.
pub fn parse_locus(name: &str) -> Option<(Box<str>, u64, u64)> {
    let lower = name.to_ascii_lowercase();
    let stripped = lower
        .strip_prefix("chr")
        .map(str::to_string)
        .unwrap_or(lower);
    // Try canonical `_` form first: chr_start_end → ["chr", "start", "end"].
    let parts: Vec<&str> = stripped.splitn(3, [':', '-', '_']).collect();
    if parts.len() != 3 {
        return None;
    }
    let chr = parts[0];
    let start: u64 = parts[1].parse().ok()?;
    let end: u64 = parts[2].parse().ok()?;
    if end < start {
        return None;
    }
    Some((chr.to_string().into_boxed_str(), start, end))
}

/// Build the overlap-merge canonical map from a flat list of row names
/// across all input backends. Names that parse as `(chr, start, end)`
/// are grouped per chromosome, sorted by start, and clustered by
/// transitive overlap (any interval whose start falls before the
/// running cluster's max end). The cluster canonical is
/// `_{chr}_{min_start}_{max_end}` so every member name maps to a single
/// well-defined string.
///
/// Names that fail to parse are not entered into the map; the caller
/// falls back to per-name `canon_locus` for those.
pub fn build_locus_overlap_canonical_map(names: &[Box<str>]) -> HashMap<Box<str>, Box<str>> {
    let n = names.len();
    let parsed: Vec<Option<(Box<str>, u64, u64)>> = names.iter().map(|n| parse_locus(n)).collect();

    // Bucket valid indices by chromosome.
    let mut by_chr: HashMap<Box<str>, Vec<usize>> = HashMap::default();
    for (i, p) in parsed.iter().enumerate() {
        if let Some((chr, _, _)) = p {
            by_chr.entry(chr.clone()).or_default().push(i);
        }
    }

    // Union-find with path compression.
    let mut parent: Vec<usize> = (0..n).collect();
    fn find(p: &mut [usize], mut x: usize) -> usize {
        while p[x] != x {
            let g = p[p[x]];
            p[x] = g;
            x = g;
        }
        x
    }

    // Per chr: sort by start, sweep, union anything overlapping the running cluster.
    let mut cluster_extent: HashMap<usize, (u64, u64)> = HashMap::default();
    for (_, mut idxs) in by_chr {
        idxs.sort_by_key(|&i| parsed[i].as_ref().map(|p| p.1).unwrap_or(0));
        let mut current_root: Option<usize> = None;
        let mut current_min_start: u64 = 0;
        let mut current_max_end: u64 = 0;
        for i in idxs {
            let (_, s, e) = parsed[i].as_ref().unwrap();
            match current_root {
                Some(root) if *s < current_max_end => {
                    let ra = find(&mut parent, root);
                    let rb = find(&mut parent, i);
                    if ra != rb {
                        parent[rb] = ra;
                    }
                    current_max_end = current_max_end.max(*e);
                    cluster_extent
                        .insert(find(&mut parent, i), (current_min_start, current_max_end));
                }
                _ => {
                    current_root = Some(i);
                    current_min_start = *s;
                    current_max_end = *e;
                    cluster_extent.insert(i, (*s, *e));
                }
            }
        }
    }

    // Build name → canonical map. Canonical = `_{chr}_{min_start}_{max_end}`
    // — matches canon_locus's `chr` strip + `_` separator convention.
    let mut out: HashMap<Box<str>, Box<str>> = HashMap::default();
    for (i, p) in parsed.iter().enumerate() {
        if let Some((chr, _, _)) = p {
            let root = find(&mut parent, i);
            let (mn, mx) = cluster_extent.get(&root).copied().unwrap_or((0, 0));
            let canonical = format!("{}_{}_{}", chr, mn, mx).into_boxed_str();
            out.insert(names[i].clone(), canonical);
        }
    }
    out
}

/// Build a `RowNameCanonicalizer` for [`FeatureNameKind::LocusOverlap`].
/// `names` should be the concatenation of every input backend's row
/// names (in any order). The returned canonicalizer does
/// `map.get(name).cloned()` first, falling back to per-name
/// `canon_locus` for names that didn't parse as a locus.
pub fn build_locus_overlap_canonicalizer(names: &[Box<str>]) -> RowNameCanonicalizer {
    let map = Arc::new(build_locus_overlap_canonical_map(names));
    Arc::new(move |name: &str| map.get(name).cloned().unwrap_or_else(|| canon_locus(name)))
}

/// Per-name dispatcher for **mixed-kind** axes (e.g. multiome with peaks
/// ∪ genes in one feature axis). For each name:
///   • parses as `(chr, start, end)` → LocusOverlap canonical
///     (cluster representative from `names`).
///   • contains `_` → gene rule: last token after the rightmost `_`.
///   • else → passthrough.
///
/// Use this when the auto-detector sees significant evidence of BOTH
/// loci and gene-style names in the same axis.
pub fn build_mixed_kind_canonicalizer(names: &[Box<str>]) -> RowNameCanonicalizer {
    let map = Arc::new(build_locus_overlap_canonical_map(names));
    Arc::new(move |name: &str| {
        if let Some(c) = map.get(name) {
            c.clone()
        } else if parse_locus(name).is_some() {
            canon_locus(name)
        } else if name.contains('_') {
            gene_canonicalize(name, '_')
        } else {
            name.into()
        }
    })
}

/// Gene-symbol canonicalization with Cell Ranger feature-type suffix
/// awareness. 10x Cell Ranger HDF5 row names commonly arrive as
/// `ENSG..._SYMBOL_<feature_type>` where the third component is a
/// sanitized `feature_type` tag (e.g. `Gene` for `Gene Expression`).
/// A naive `rsplit(delim).next()` would return that constant tag,
/// canonicalizing *every* row to the same string and collapsing the
/// row intersection to one global key. Strip the known tag suffix
/// first so the actual symbol becomes the rsplit target.
fn gene_canonicalize(name: &str, delim: char) -> Box<str> {
    let stripped = strip_feature_type_suffix(name, delim);
    stripped.rsplit(delim).next().unwrap_or(stripped).into()
}

/// Cell Ranger sanitizes `features/feature_type` into the row name as
/// the trailing component. Strip the known tags so the actual gene
/// symbol becomes the rsplit target. Conservative list — only the
/// shapes we've actually seen in the wild — so an unknown tag falls
/// through untouched rather than corrupting a real symbol.
fn strip_feature_type_suffix(name: &str, delim: char) -> &str {
    // Names come pre-sanitized in different ways depending on the
    // producer (Cell Ranger's own h5, scanpy/anndata exports, R-side
    // tools), so accept both `_Gene` and `_Gene_Expression` plus the
    // common companion tags.
    const TAGS: &[&str] = &[
        "Gene_Expression",
        "Gene",
        "Antibody_Capture",
        "CRISPR_Guide_Capture",
        "Multiplexing_Capture",
        "Custom",
        "Peaks",
    ];
    for tag in TAGS {
        // Only strip if the suffix sits behind `delim` (otherwise we'd
        // mangle a real symbol that happens to end in "Gene").
        let mut suffix = String::with_capacity(tag.len() + 1);
        suffix.push(delim);
        suffix.push_str(tag);
        if let Some(rest) = name.strip_suffix(suffix.as_str()) {
            return rest;
        }
    }
    name
}

/// Clap-facing spelling of [`FeatureNameKind`].
///
/// The rule and the flag that selects it belong together: every crate that
/// aligns feature names across files exposes the same `--feature-name-kind`
/// vocabulary, so `senna` and anything after it agree on what
/// `gene` or `locus` means without each inventing a local rule.
#[derive(clap::ValueEnum, Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FeatureNameKindArg {
    #[default]
    Auto,
    Exact,
    Gene,
    Locus,
    LocusOverlap,
    Mixed,
}

impl FeatureNameKindArg {
    /// Resolve to a concrete [`FeatureNameKind`], defaulting `Auto` to
    /// `Gene { delim: '_' }` — the standard for gene-keyed pre-train
    /// inputs (bge / fne / topic-family dictionaries).
    pub fn resolve_or_gene(&self) -> FeatureNameKind {
        Option::<FeatureNameKind>::from(self.clone())
            .unwrap_or(FeatureNameKind::Gene { delim: '_' })
    }
}

impl From<FeatureNameKindArg> for Option<FeatureNameKind> {
    fn from(arg: FeatureNameKindArg) -> Self {
        match arg {
            FeatureNameKindArg::Auto => None,
            FeatureNameKindArg::Exact => Some(FeatureNameKind::Exact),
            FeatureNameKindArg::Gene => Some(FeatureNameKind::Gene { delim: '_' }),
            FeatureNameKindArg::Locus => Some(FeatureNameKind::Locus {
                merge_overlapping: false,
            }),
            FeatureNameKindArg::LocusOverlap => Some(FeatureNameKind::Locus {
                merge_overlapping: true,
            }),
            FeatureNameKindArg::Mixed => Some(FeatureNameKind::Mixed),
        }
    }
}

#[cfg(test)]
#[path = "feature_names_tests.rs"]
mod feature_names_tests;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn exact_passthrough() {
        let k = FeatureNameKind::Exact;
        assert_eq!(
            k.canonicalize("ENSG00000000003_TSPAN6").as_ref(),
            "ENSG00000000003_TSPAN6"
        );
        assert!(k.is_exact());
        assert!(k.into_canonicalizer().is_none());
    }

    #[test]
    fn gene_takes_last_underscore_component() {
        let k = FeatureNameKind::Gene { delim: '_' };
        assert_eq!(k.canonicalize("ENSG00000000003_TSPAN6").as_ref(), "TSPAN6");
        // Symbol-only inputs survive unchanged.
        assert_eq!(k.canonicalize("TSPAN6").as_ref(), "TSPAN6");
        // Unknown trailing tokens still get rsplit — caller's responsibility
        // to pick a sensible delim if a non-feature-type trailing token matters.
        assert_eq!(k.canonicalize("A_B_C").as_ref(), "C");
        assert!(!k.is_exact());
        assert!(k.into_canonicalizer().is_some());
    }

    #[test]
    fn gene_strips_cell_ranger_feature_type_suffix() {
        let k = FeatureNameKind::Gene { delim: '_' };
        // 10x Cell Ranger HDF5: `ENSG..._SYMBOL_Gene`. Trailing `_Gene`
        // would otherwise collapse every gene to the literal "Gene".
        assert_eq!(
            k.canonicalize("ENSG00000187634_SAMD11_Gene").as_ref(),
            "SAMD11"
        );
        // Full `Gene_Expression` tag variant.
        assert_eq!(
            k.canonicalize("ENSG00000187634_SAMD11_Gene_Expression")
                .as_ref(),
            "SAMD11"
        );
        // A real gene whose name happens to end in "Gene" *without* the
        // delimiter shouldn't be stripped (no underscore in front of "Gene").
        assert_eq!(k.canonicalize("FakeGene").as_ref(), "FakeGene");
    }

    #[test]
    fn locus_strips_chr_and_folds_separators() {
        let k = FeatureNameKind::Locus {
            merge_overlapping: false,
        };
        assert_eq!(k.canonicalize("chr1:1000-2000").as_ref(), "1_1000_2000");
        assert_eq!(k.canonicalize("1_1000_2000").as_ref(), "1_1000_2000");
        assert_eq!(k.canonicalize("ChrX:5000-6000").as_ref(), "X_5000_6000");
    }

    // -- Genomic region parsing edge cases ----------------------------------

    #[test]
    fn parse_locus_accepts_common_formats() {
        // colon-dash, bare chromosome, underscore-separated, chrX caps.
        assert_eq!(
            parse_locus("chr1:1000-2000"),
            Some(("1".into(), 1000, 2000))
        );
        assert_eq!(parse_locus("1:1000-2000"), Some(("1".into(), 1000, 2000)));
        assert_eq!(
            parse_locus("chr1_1000_2000"),
            Some(("1".into(), 1000, 2000))
        );
        assert_eq!(
            parse_locus("CHR1:1000-2000"),
            Some(("1".into(), 1000, 2000))
        );
        assert_eq!(
            parse_locus("chrX:5000-6000"),
            Some(("x".into(), 5000, 6000))
        );
        assert_eq!(parse_locus("chrMT:1-100"), Some(("mt".into(), 1, 100)));
    }

    #[test]
    fn parse_locus_rejects_non_loci() {
        assert!(parse_locus("TGFB1").is_none()); // gene symbol
        assert!(parse_locus("ENSG00000105329").is_none()); // ensembl
        assert!(parse_locus("chr1:bad-2000").is_none()); // non-numeric start
        assert!(parse_locus("chr1:1000").is_none()); // missing end
        assert!(parse_locus("chr1:2000-1000").is_none()); // end < start
        assert!(parse_locus("").is_none()); // empty
        assert!(parse_locus("chr1").is_none()); // chr-only
    }

    #[test]
    fn overlap_map_merges_two_overlapping_intervals() {
        // User's motivating example: chr1:1-20 and chr1:15-30 → same cluster.
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "chr1:15-30".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        let c0 = map.get(&names[0]).unwrap();
        let c1 = map.get(&names[1]).unwrap();
        assert_eq!(c0, c1, "both inputs should map to the same canonical");
        assert_eq!(c0.as_ref(), "1_1_30"); // union-range canonical
    }

    #[test]
    fn overlap_map_keeps_non_overlapping_separate() {
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "chr1:100-200".to_string().into_boxed_str(),
            "chr2:1-20".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        assert_eq!(map.get(&names[0]).unwrap().as_ref(), "1_1_20");
        assert_eq!(map.get(&names[1]).unwrap().as_ref(), "1_100_200");
        // different chromosome — separate cluster even if start overlaps
        assert_eq!(map.get(&names[2]).unwrap().as_ref(), "2_1_20");
    }

    #[test]
    fn overlap_map_handles_transitive_chain() {
        // A overlaps B (1-20 vs 15-30), B overlaps C (15-30 vs 25-40),
        // A does NOT overlap C directly — but they should still cluster
        // via transitive closure through B.
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "chr1:15-30".to_string().into_boxed_str(),
            "chr1:25-40".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        let c0 = map.get(&names[0]).unwrap();
        let c1 = map.get(&names[1]).unwrap();
        let c2 = map.get(&names[2]).unwrap();
        assert_eq!(c0, c1);
        assert_eq!(c1, c2);
        assert_eq!(c0.as_ref(), "1_1_40"); // union of full chain
    }

    #[test]
    fn overlap_map_handles_full_containment() {
        // chr1:1-100 contains chr1:30-50 — should cluster.
        let names = vec![
            "chr1:1-100".to_string().into_boxed_str(),
            "chr1:30-50".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        let c0 = map.get(&names[0]).unwrap();
        let c1 = map.get(&names[1]).unwrap();
        assert_eq!(c0, c1);
        assert_eq!(c0.as_ref(), "1_1_100");
    }

    #[test]
    fn overlap_map_treats_adjacent_as_separate() {
        // chr1:1-20 and chr1:20-30 are *touching* but not overlapping
        // (end of first == start of second, exclusive end convention).
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "chr1:20-30".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        assert_ne!(map.get(&names[0]).unwrap(), map.get(&names[1]).unwrap());
    }

    #[test]
    fn overlap_map_normalizes_chr_prefix_within_cluster() {
        // chr1:1-20 and 1:15-30 (no chr prefix) should still cluster
        // because parse_locus normalizes both to chr="1".
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "1:15-30".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        let c0 = map.get(&names[0]).unwrap();
        let c1 = map.get(&names[1]).unwrap();
        assert_eq!(c0, c1);
        assert_eq!(c0.as_ref(), "1_1_30");
    }

    #[test]
    fn overlap_map_normalizes_separators_within_cluster() {
        // chr1:1-20 and chr1_15_30 (different separators) → same cluster.
        let names = vec![
            "chr1:1-20".to_string().into_boxed_str(),
            "chr1_15_30".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        assert_eq!(map.get(&names[0]).unwrap(), map.get(&names[1]).unwrap());
    }

    #[test]
    fn overlap_map_ignores_non_locus_names() {
        // Non-locus names should not appear in the map; caller falls
        // back to canon_locus (the default Locus rule).
        let names = vec![
            "TGFB1".to_string().into_boxed_str(),
            "chr1:1-20".to_string().into_boxed_str(),
        ];
        let map = build_locus_overlap_canonical_map(&names);
        assert!(!map.contains_key(&names[0]));
        assert!(map.contains_key(&names[1]));
    }

    #[test]
    fn overlap_map_handles_zero_length_interval() {
        // chr1:1000-1000 — degenerate but valid; should be its own cluster.
        let names = vec!["chr1:1000-1000".to_string().into_boxed_str()];
        let map = build_locus_overlap_canonical_map(&names);
        assert_eq!(map.get(&names[0]).unwrap().as_ref(), "1_1000_1000");
    }

    #[test]
    fn overlap_canonicalizer_falls_back_to_canon_locus_for_unmatched() {
        let names = vec!["chr1:1-20".to_string().into_boxed_str()];
        let canon = build_locus_overlap_canonicalizer(&names);
        // In-cluster name → cluster canonical.
        assert_eq!(canon("chr1:1-20").as_ref(), "1_1_20");
        // Unrelated locus not in the map → canon_locus normalization.
        assert_eq!(canon("chr2:500-600").as_ref(), "2_500_600");
        // Non-locus → canon_locus passes through unchanged.
        // (canon_locus for "TGFB1" likely strips no separators and returns
        // the lowercase form — accept whatever the helper returns.)
        let g = canon("TGFB1");
        assert!(!g.is_empty());
    }

    // -- Auto-detect & Mixed dispatcher -------------------------------------

    #[test]
    fn auto_detect_pure_locus_axis() {
        let names: Vec<Box<str>> = (0..100)
            .map(|i| format!("chr1:{}-{}", i * 100, i * 100 + 50).into_boxed_str())
            .collect();
        assert!(matches!(
            FeatureNameKind::auto_detect(&names),
            FeatureNameKind::Locus {
                merge_overlapping: true
            }
        ));
    }

    #[test]
    fn auto_detect_pure_gene_axis() {
        let names: Vec<Box<str>> = (0..100)
            .map(|i| format!("ENSG000_GENE{}", i).into_boxed_str())
            .collect();
        assert!(matches!(
            FeatureNameKind::auto_detect(&names),
            FeatureNameKind::Gene { delim: '_' }
        ));
    }

    #[test]
    fn auto_detect_mixed_axis() {
        // 80 loci + 20 gene-style → both fractions ≥ 10% → Mixed.
        let mut names: Vec<Box<str>> = (0..80)
            .map(|i| format!("chr1:{}-{}", i * 1000, i * 1000 + 500).into_boxed_str())
            .collect();
        names.extend((0..20).map(|i| format!("ENSG000_GENE{}", i).into_boxed_str()));
        assert!(matches!(
            FeatureNameKind::auto_detect(&names),
            FeatureNameKind::Mixed
        ));
    }

    #[test]
    fn auto_detect_empty_or_exact() {
        assert!(matches!(
            FeatureNameKind::auto_detect(&[]),
            FeatureNameKind::Exact
        ));
        let names = vec!["TGFB1".into(), "CD4".into(), "IL2".into(), "GAPDH".into()];
        assert!(matches!(
            FeatureNameKind::auto_detect(&names),
            FeatureNameKind::Exact
        ));
    }

    #[test]
    fn mixed_dispatcher_canonicalizes_each_name_by_kind() {
        let names: Vec<Box<str>> = vec![
            "chr1:1-20".into(),     // locus → cluster canonical
            "chr1:15-30".into(),    // locus, overlaps above → same cluster
            "ENSG000_TGFB1".into(), // gene-style → "TGFB1"
            "CD4".into(),           // plain symbol → passthrough
        ];
        let canon = build_mixed_kind_canonicalizer(&names);
        assert_eq!(canon("chr1:1-20").as_ref(), "1_1_30");
        assert_eq!(canon("chr1:15-30").as_ref(), "1_1_30");
        assert_eq!(canon("ENSG000_TGFB1").as_ref(), "TGFB1");
        assert_eq!(canon("CD4").as_ref(), "CD4");
    }
}