ferro-hgvs 1.0.0

HGVS variant normalizer - part of the ferro bioinformatics toolkit
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
//! Contig name aliases for different naming conventions.
//!
//! Handles mapping between different contig naming conventions:
//! - UCSC: chr1, chr2, ..., chrX, chrY, chrM
//! - RefSeq: NC_000001.10 (GRCh37), NC_000001.11 (GRCh38)
//! - Ensembl: 1, 2, ..., X, Y, MT

use crate::liftover::assembly_report::AssemblyReport;
use crate::reference::transcript::GenomeBuild;
use std::collections::HashMap;

/// Contig alias mappings.
#[derive(Debug, Clone, Default)]
pub struct ContigAliases {
    /// Maps (contig_name, build) -> canonical RefSeq name
    to_refseq: HashMap<(String, GenomeBuild), String>,
    /// Maps RefSeq name -> UCSC name
    refseq_to_ucsc: HashMap<String, String>,
    /// Maps RefSeq name -> Ensembl name
    refseq_to_ensembl: HashMap<String, String>,
}

impl ContigAliases {
    /// Create empty alias mapping.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create default human genome aliases.
    pub fn default_human() -> Self {
        let mut aliases = Self::new();

        // Autosomes
        for i in 1..=22 {
            let ucsc = format!("chr{}", i);
            let ensembl = format!("{}", i);
            let refseq_37 = format!("NC_{:06}.{}", i, Self::grch37_version(i));
            let refseq_38 = format!("NC_{:06}.{}", i, Self::grch38_version(i));

            // GRCh37 mappings
            aliases.add_alias(&ucsc, GenomeBuild::GRCh37, &refseq_37);
            aliases.add_alias(&ensembl, GenomeBuild::GRCh37, &refseq_37);
            aliases.add_alias(&refseq_37, GenomeBuild::GRCh37, &refseq_37);

            // GRCh38 mappings
            aliases.add_alias(&ucsc, GenomeBuild::GRCh38, &refseq_38);
            aliases.add_alias(&ensembl, GenomeBuild::GRCh38, &refseq_38);
            aliases.add_alias(&refseq_38, GenomeBuild::GRCh38, &refseq_38);

            // Reverse lookups
            aliases
                .refseq_to_ucsc
                .insert(refseq_37.clone(), ucsc.clone());
            aliases
                .refseq_to_ucsc
                .insert(refseq_38.clone(), ucsc.clone());
            aliases.refseq_to_ensembl.insert(refseq_37, ensembl.clone());
            aliases.refseq_to_ensembl.insert(refseq_38, ensembl);
        }

        // X chromosome
        aliases.add_alias("chrX", GenomeBuild::GRCh37, "NC_000023.10");
        aliases.add_alias("X", GenomeBuild::GRCh37, "NC_000023.10");
        aliases.add_alias("NC_000023.10", GenomeBuild::GRCh37, "NC_000023.10");
        aliases.add_alias("chrX", GenomeBuild::GRCh38, "NC_000023.11");
        aliases.add_alias("X", GenomeBuild::GRCh38, "NC_000023.11");
        aliases.add_alias("NC_000023.11", GenomeBuild::GRCh38, "NC_000023.11");
        aliases
            .refseq_to_ucsc
            .insert("NC_000023.10".to_string(), "chrX".to_string());
        aliases
            .refseq_to_ucsc
            .insert("NC_000023.11".to_string(), "chrX".to_string());
        aliases
            .refseq_to_ensembl
            .insert("NC_000023.10".to_string(), "X".to_string());
        aliases
            .refseq_to_ensembl
            .insert("NC_000023.11".to_string(), "X".to_string());

        // Y chromosome
        aliases.add_alias("chrY", GenomeBuild::GRCh37, "NC_000024.9");
        aliases.add_alias("Y", GenomeBuild::GRCh37, "NC_000024.9");
        aliases.add_alias("NC_000024.9", GenomeBuild::GRCh37, "NC_000024.9");
        aliases.add_alias("chrY", GenomeBuild::GRCh38, "NC_000024.10");
        aliases.add_alias("Y", GenomeBuild::GRCh38, "NC_000024.10");
        aliases.add_alias("NC_000024.10", GenomeBuild::GRCh38, "NC_000024.10");
        aliases
            .refseq_to_ucsc
            .insert("NC_000024.9".to_string(), "chrY".to_string());
        aliases
            .refseq_to_ucsc
            .insert("NC_000024.10".to_string(), "chrY".to_string());
        aliases
            .refseq_to_ensembl
            .insert("NC_000024.9".to_string(), "Y".to_string());
        aliases
            .refseq_to_ensembl
            .insert("NC_000024.10".to_string(), "Y".to_string());

        // Mitochondrial — NC_012920.1 is the canonical rCRS accession on
        // both GRCh37 and GRCh38 (mtDNA was not re-assembled between
        // builds), so register a self-alias under each build. Without
        // the GRCh38 self-alias `infer_genome_build_from_accession` would
        // misclassify NC_012920.1 inputs as GRCh37-only.
        aliases.add_alias("chrM", GenomeBuild::GRCh37, "NC_012920.1");
        aliases.add_alias("MT", GenomeBuild::GRCh37, "NC_012920.1");
        aliases.add_alias("NC_012920.1", GenomeBuild::GRCh37, "NC_012920.1");
        aliases.add_alias("chrM", GenomeBuild::GRCh38, "NC_012920.1");
        aliases.add_alias("MT", GenomeBuild::GRCh38, "NC_012920.1");
        aliases.add_alias("NC_012920.1", GenomeBuild::GRCh38, "NC_012920.1");
        aliases
            .refseq_to_ucsc
            .insert("NC_012920.1".to_string(), "chrM".to_string());
        aliases
            .refseq_to_ensembl
            .insert("NC_012920.1".to_string(), "MT".to_string());

        aliases
    }

    /// Build a contig-alias table from parsed NCBI assembly reports, one per
    /// genome build.
    ///
    /// Data-driven counterpart to [`default_human`](Self::default_human): the
    /// `(name, build) → RefSeq` and reverse maps are derived from the report's
    /// own `assembled-molecule` rows rather than a hardcoded per-chromosome
    /// version table (#716). For each such row the RefSeq accession is
    /// registered as a self-alias (so [`infer_genome_build_from_accession_with`]
    /// can classify it), alongside its UCSC-style name (column 9) and Ensembl
    /// name (the Assigned-Molecule, column 2). Rows whose UCSC name is the `na`
    /// placeholder contribute no UCSC alias.
    ///
    /// Multiple reports are merged, so passing both the GRCh37 and GRCh38
    /// reports yields one table covering both builds — an accession present in
    /// only one build resolves under that build alone, which is exactly the
    /// signal build inference needs.
    pub fn from_assembly_reports(reports: &[(GenomeBuild, &AssemblyReport)]) -> Self {
        let mut aliases = Self::new();
        for (build, report) in reports {
            for entry in report.assembled_molecules_with_refseq() {
                let refseq = entry.refseq_accession.as_str();
                let ensembl = entry.assigned_molecule.as_str();
                let ucsc = entry.ucsc_name.as_str();

                aliases.add_alias(refseq, *build, refseq);
                if !ensembl.is_empty() && ensembl != "na" {
                    aliases.add_alias(ensembl, *build, refseq);
                    aliases
                        .refseq_to_ensembl
                        .insert(refseq.to_string(), ensembl.to_string());
                }
                if !ucsc.is_empty() && ucsc != "na" {
                    aliases.add_alias(ucsc, *build, refseq);
                    aliases
                        .refseq_to_ucsc
                        .insert(refseq.to_string(), ucsc.to_string());
                }
            }
        }
        aliases
    }

    /// Get GRCh37 version number for a chromosome.
    fn grch37_version(chr: i32) -> i32 {
        match chr {
            1 => 10,
            2 => 11,
            3 => 11,
            4 => 11,
            5 => 9,
            6 => 11,
            7 => 13,
            8 => 10,
            9 => 11,
            10 => 10,
            11 => 9,
            12 => 11,
            13 => 10,
            14 => 8,
            15 => 9,
            16 => 9,
            17 => 10,
            18 => 9,
            19 => 9,
            20 => 10,
            21 => 8,
            22 => 10,
            _ => 1,
        }
    }

    /// Get GRCh38 version number for a chromosome.
    fn grch38_version(chr: i32) -> i32 {
        match chr {
            1 => 11,
            2 => 12,
            3 => 12,
            4 => 12,
            5 => 10,
            6 => 12,
            7 => 14,
            8 => 11,
            9 => 12,
            10 => 11,
            11 => 10,
            12 => 12,
            13 => 11,
            14 => 9,
            15 => 10,
            16 => 10,
            17 => 11,
            18 => 10,
            19 => 10,
            20 => 11,
            21 => 9,
            22 => 11,
            _ => 1,
        }
    }

    /// `true` when no `(name, build)` mappings are registered.
    pub fn is_empty(&self) -> bool {
        self.to_refseq.is_empty()
    }

    /// Add an alias mapping.
    pub fn add_alias(&mut self, name: &str, build: GenomeBuild, refseq: &str) {
        self.to_refseq
            .insert((name.to_string(), build), refseq.to_string());
    }

    /// Resolve a contig name to its canonical RefSeq name.
    pub fn resolve_to_refseq(&self, name: &str, build: GenomeBuild) -> Option<&str> {
        self.to_refseq
            .get(&(name.to_string(), build))
            .map(|s| s.as_str())
    }

    /// Resolve a RefSeq name to UCSC format.
    pub fn refseq_to_ucsc(&self, refseq: &str) -> Option<&str> {
        self.refseq_to_ucsc.get(refseq).map(|s| s.as_str())
    }

    /// Resolve a RefSeq name to Ensembl format.
    pub fn refseq_to_ensembl(&self, refseq: &str) -> Option<&str> {
        self.refseq_to_ensembl.get(refseq).map(|s| s.as_str())
    }

    /// Check if two contig names are equivalent for a given build.
    pub fn are_equivalent(&self, name1: &str, name2: &str, build: GenomeBuild) -> bool {
        let refseq1 = self.resolve_to_refseq(name1, build);
        let refseq2 = self.resolve_to_refseq(name2, build);
        match (refseq1, refseq2) {
            (Some(r1), Some(r2)) => r1 == r2,
            _ => name1 == name2,
        }
    }

    /// Normalize a contig name to the preferred format for a build.
    ///
    /// Returns the RefSeq accession if found, otherwise returns the input unchanged.
    pub fn normalize(&self, name: &str, build: GenomeBuild) -> String {
        self.resolve_to_refseq(name, build)
            .map(|s| s.to_string())
            .unwrap_or_else(|| name.to_string())
    }
}

/// Lazily-constructed singleton of [`ContigAliases::default_human`] so
/// repeated build inference (per projection) does not rebuild the
/// ~150-entry alias table every call.
///
/// `pub(crate)` so other modules that need the bundled human chromosome↔RefSeq
/// table (e.g. the VCF→HGVS converter resolving a chromosome name to its
/// build-appropriate `NC_` accession, #804) share this one cached instance
/// rather than constructing a second copy.
pub(crate) fn default_human_aliases() -> &'static ContigAliases {
    use std::sync::OnceLock;
    static ALIASES: OnceLock<ContigAliases> = OnceLock::new();
    ALIASES.get_or_init(ContigAliases::default_human)
}

/// Infer the canonical genome build name for a chromosome accession.
///
/// Sources of build information consulted, in order:
///   1. `accession.assembly` — assembly-style references like
///      `GRCh37(chr1):g.…` carry the build verbatim.
///   2. `NC_*` accessions are looked up in [`ContigAliases::default_human`].
///      `NC_*` carries a build-distinguishing version (e.g.
///      `NC_000017.10` is GRCh37 chr17, `NC_000017.11` is GRCh38). NC
///      accessions present under both builds (e.g. mtDNA `NC_012920.1`)
///      resolve to GRCh38, our default-when-ambiguous.
///   3. Everything else (`NG_*`, `LRG_*`, `NW_*`, unknown prefixes, NC
///      accessions outside the human alias table) returns `None` so the
///      caller falls back to the build-agnostic probe order.
///
/// Centralized here so call sites that need to disambiguate cdot lookups
/// against multi-build data (e.g. `MultiFastaProvider`, `VariantProjector`)
/// share one implementation rather than duplicating the GRCh37/GRCh38
/// inference logic.
pub fn infer_genome_build_from_accession(
    accession: &crate::hgvs::variant::Accession,
) -> Option<&'static str> {
    infer_genome_build_from_accession_with(default_human_aliases(), accession)
}

/// Like [`infer_genome_build_from_accession`] but consulting an explicitly
/// supplied [`ContigAliases`] table rather than the bundled
/// [`default_human`](ContigAliases::default_human) heuristic.
///
/// This is the data-driven entry point (#716): pass a table built from the
/// prepared reference's own `assembly_report.txt`
/// ([`ContigAliases::from_assembly_reports`]) and build inference becomes
/// authoritative — it classifies any `NC_` accession the report describes,
/// including versions absent from the hardcoded table. The `accession.assembly`
/// explicit-build field and the `NC_`-prefix gate are honored identically to
/// the bundled path, so the only behavioral difference is the source of the
/// accession→build mapping.
pub fn infer_genome_build_from_accession_with(
    aliases: &ContigAliases,
    accession: &crate::hgvs::variant::Accession,
) -> Option<&'static str> {
    // Assembly-style references (`GRCh37(chr1)`, `GRCh38(chrX)`) carry the
    // build name explicitly; honor it before the prefix check.
    if let Some(asm) = accession.assembly.as_deref() {
        match asm {
            "GRCh37" => return Some("GRCh37"),
            "GRCh38" => return Some("GRCh38"),
            _ => {}
        }
    }
    if &*accession.prefix != "NC" {
        return None;
    }
    let acc_str = accession.full();
    // GRCh38 is checked first so an accession present under both builds (e.g.
    // mtDNA `NC_012920.1`) resolves to GRCh38, ferro's default-when-ambiguous.
    if aliases
        .resolve_to_refseq(&acc_str, GenomeBuild::GRCh38)
        .is_some()
    {
        return Some("GRCh38");
    }
    if aliases
        .resolve_to_refseq(&acc_str, GenomeBuild::GRCh37)
        .is_some()
    {
        return Some("GRCh37");
    }
    None
}

/// Infer the genome build for an accession, consulting a data-driven
/// [`ContigAliases`] (from the prepared reference's own assembly report, #716)
/// before falling back to the bundled hardcoded table.
///
/// `table` is `None` when the prepared reference carries no assembly report
/// (old manifest, bare library use), in which case this is exactly
/// [`infer_genome_build_from_accession`]. When `Some`, the data-driven table is
/// authoritative for any accession it describes; accessions it omits still
/// classify via the hardcoded fallback. Returns the first build found, so the
/// data-driven layer wins on disagreement — safe because the two only overlap
/// on accessions identical across builds (e.g. mtDNA), which the GRCh38-first
/// check order in [`infer_genome_build_from_accession_with`] resolves the same
/// way either source would.
pub fn infer_genome_build_layered(
    table: Option<&ContigAliases>,
    accession: &crate::hgvs::variant::Accession,
) -> Option<&'static str> {
    if let Some(table) = table {
        if let Some(build) = infer_genome_build_from_accession_with(table, accession) {
            return Some(build);
        }
    }
    infer_genome_build_from_accession(accession)
}

/// Normalize a user-supplied assembly name to ferro's canonical build string
/// (`"GRCh37"` / `"GRCh38"`), or `None` if unrecognized (#715).
///
/// Accepts the common aliases (`hg19`/`b37`/`37` → `GRCh37`,
/// `hg38`/`b38`/`38` → `GRCh38`), case-insensitively. The canonical strings
/// returned here are the exact `&'static str` values
/// [`infer_genome_build_from_accession`] yields and that
/// `select_placement_for_build` / cdot's `get_transcript_on_build` compare
/// against, so callers should validate at the boundary (CLI flag, Python kwarg)
/// and only ever hand the canonical value to
/// [`crate::project::VariantProjector::with_assembly`].
pub fn normalize_assembly_name(name: &str) -> Option<&'static str> {
    match name.trim().to_ascii_lowercase().as_str() {
        "grch37" | "hg19" | "b37" | "37" => Some("GRCh37"),
        "grch38" | "hg38" | "b38" | "38" => Some("GRCh38"),
        _ => None,
    }
}

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

    #[test]
    fn test_normalize_assembly_name() {
        for name in ["GRCh37", "grch37", "hg19", "HG19", "b37", "37", " grch37 "] {
            assert_eq!(normalize_assembly_name(name), Some("GRCh37"), "{name:?}");
        }
        for name in ["GRCh38", "grch38", "hg38", "b38", "38"] {
            assert_eq!(normalize_assembly_name(name), Some("GRCh38"), "{name:?}");
        }
        for name in ["", "hg20", "GRCh39", "chm13", "t2t"] {
            assert_eq!(normalize_assembly_name(name), None, "{name:?}");
        }
    }

    #[test]
    fn test_default_human_autosomes() {
        let aliases = ContigAliases::default_human();

        // Chr1 GRCh37
        assert_eq!(
            aliases.resolve_to_refseq("chr1", GenomeBuild::GRCh37),
            Some("NC_000001.10")
        );
        assert_eq!(
            aliases.resolve_to_refseq("1", GenomeBuild::GRCh37),
            Some("NC_000001.10")
        );

        // Chr1 GRCh38
        assert_eq!(
            aliases.resolve_to_refseq("chr1", GenomeBuild::GRCh38),
            Some("NC_000001.11")
        );
        assert_eq!(
            aliases.resolve_to_refseq("1", GenomeBuild::GRCh38),
            Some("NC_000001.11")
        );
    }

    #[test]
    fn test_sex_chromosomes() {
        let aliases = ContigAliases::default_human();

        assert_eq!(
            aliases.resolve_to_refseq("chrX", GenomeBuild::GRCh37),
            Some("NC_000023.10")
        );
        assert_eq!(
            aliases.resolve_to_refseq("X", GenomeBuild::GRCh38),
            Some("NC_000023.11")
        );
        assert_eq!(
            aliases.resolve_to_refseq("chrY", GenomeBuild::GRCh37),
            Some("NC_000024.9")
        );
    }

    #[test]
    fn test_mitochondrial() {
        let aliases = ContigAliases::default_human();

        assert_eq!(
            aliases.resolve_to_refseq("chrM", GenomeBuild::GRCh37),
            Some("NC_012920.1")
        );
        assert_eq!(
            aliases.resolve_to_refseq("MT", GenomeBuild::GRCh38),
            Some("NC_012920.1")
        );
    }

    #[test]
    fn test_reverse_lookup() {
        let aliases = ContigAliases::default_human();

        assert_eq!(aliases.refseq_to_ucsc("NC_000001.10"), Some("chr1"));
        assert_eq!(aliases.refseq_to_ensembl("NC_000001.10"), Some("1"));
    }

    #[test]
    fn test_equivalence() {
        let aliases = ContigAliases::default_human();

        assert!(aliases.are_equivalent("chr1", "1", GenomeBuild::GRCh37));
        assert!(aliases.are_equivalent("chr1", "NC_000001.10", GenomeBuild::GRCh37));
        assert!(!aliases.are_equivalent("chr1", "chr2", GenomeBuild::GRCh37));
    }

    #[test]
    fn test_normalize() {
        let aliases = ContigAliases::default_human();

        assert_eq!(
            aliases.normalize("chr1", GenomeBuild::GRCh37),
            "NC_000001.10"
        );
        assert_eq!(aliases.normalize("unknown", GenomeBuild::GRCh37), "unknown");
    }

    // ----- #716: data-driven build inference from assembly reports -----

    use crate::hgvs::variant::Accession;
    use crate::liftover::assembly_report::parse_assembly_report;

    /// A minimal but format-faithful assembly_report for one or more
    /// chromosomes: `(ucsc, ensembl, refseq)` rows under the given name.
    fn synthetic_report(name: &str, rows: &[(&str, &str, &str)]) -> String {
        let mut s = format!("# Assembly name:  {name}\n");
        for (ucsc, ensembl, refseq) in rows {
            // cols: name role assigned type genbank rel refseq unit length ucsc
            s.push_str(&format!(
                "{ensembl}\tassembled-molecule\t{ensembl}\tChromosome\tCM000000.1\t=\t{refseq}\tPrimary Assembly\t1000\t{ucsc}\n"
            ));
        }
        s
    }

    #[test]
    fn from_assembly_reports_builds_resolvable_aliases() {
        let report = parse_assembly_report(&synthetic_report(
            "GRCh38.p14",
            &[("chr1", "1", "NC_000001.11"), ("chrX", "X", "NC_000023.11")],
        ));
        let aliases = ContigAliases::from_assembly_reports(&[(GenomeBuild::GRCh38, &report)]);

        assert_eq!(
            aliases.resolve_to_refseq("chr1", GenomeBuild::GRCh38),
            Some("NC_000001.11")
        );
        assert_eq!(
            aliases.resolve_to_refseq("1", GenomeBuild::GRCh38),
            Some("NC_000001.11")
        );
        // Self-alias powers build inference.
        assert_eq!(
            aliases.resolve_to_refseq("NC_000023.11", GenomeBuild::GRCh38),
            Some("NC_000023.11")
        );
        assert_eq!(aliases.refseq_to_ucsc("NC_000001.11"), Some("chr1"));
        assert_eq!(aliases.refseq_to_ensembl("NC_000023.11"), Some("X"));
    }

    #[test]
    fn infer_with_classifies_from_merged_reports() {
        let g38 = parse_assembly_report(&synthetic_report(
            "GRCh38.p14",
            &[("chr1", "1", "NC_000001.11")],
        ));
        let g37 = parse_assembly_report(&synthetic_report(
            "GRCh37.p13",
            &[("chr1", "1", "NC_000001.10")],
        ));
        let aliases = ContigAliases::from_assembly_reports(&[
            (GenomeBuild::GRCh38, &g38),
            (GenomeBuild::GRCh37, &g37),
        ]);

        assert_eq!(
            infer_genome_build_from_accession_with(
                &aliases,
                &Accession::new("NC", "000001", Some(11))
            ),
            Some("GRCh38")
        );
        assert_eq!(
            infer_genome_build_from_accession_with(
                &aliases,
                &Accession::new("NC", "000001", Some(10))
            ),
            Some("GRCh37")
        );
    }

    #[test]
    fn infer_with_hardens_beyond_the_hardcoded_table() {
        // A version the bundled table does not know (`NC_000001.12` is a
        // hypothetical future GRCh38 chr1 build). The hardcoded default
        // declines; a report-built table classifies it authoritatively.
        let unknown_to_default = Accession::new("NC", "000001", Some(12));
        assert_eq!(
            infer_genome_build_from_accession(&unknown_to_default),
            None,
            "the bundled version table must not know NC_000001.12"
        );

        let report = parse_assembly_report(&synthetic_report(
            "GRCh38.p15",
            &[("chr1", "1", "NC_000001.12")],
        ));
        let aliases = ContigAliases::from_assembly_reports(&[(GenomeBuild::GRCh38, &report)]);
        assert_eq!(
            infer_genome_build_from_accession_with(&aliases, &unknown_to_default),
            Some("GRCh38"),
            "report-derived aliases must classify accessions absent from the bundled table"
        );
    }

    #[test]
    fn infer_default_path_is_unchanged() {
        // The public no-arg entry point delegates to the bundled table; its
        // behavior must be identical to before the #716 refactor.
        assert_eq!(
            infer_genome_build_from_accession(&Accession::new("NC", "000017", Some(11))),
            Some("GRCh38")
        );
        assert_eq!(
            infer_genome_build_from_accession(&Accession::new("NC", "000017", Some(10))),
            Some("GRCh37")
        );
        // mtDNA is shared across builds and resolves to the GRCh38 default.
        assert_eq!(
            infer_genome_build_from_accession(&Accession::new("NC", "012920", Some(1))),
            Some("GRCh38")
        );
        // Non-NC prefixes still decline.
        assert_eq!(
            infer_genome_build_from_accession(&Accession::new("NG", "012337", Some(1))),
            None
        );
    }

    /// A GRCh38-only report whose chr17 row carries a version (`.99`) the
    /// hardcoded table does not know — proves the data-driven layer adds reach.
    const GRCH38_FUTURE_SAMPLE: &str = "\
# Assembly name:  GRCh38.future
# Sequence-Name\tSequence-Role\tAssigned-Molecule\tAssigned-Molecule-Location/Type\tGenBank-Accn\tRelationship\tRefSeq-Accn\tAssembly-Unit\tSequence-Length\tUCSC-style-name
17\tassembled-molecule\t17\tChromosome\tCM000679.9\t=\tNC_000017.99\tPrimary Assembly\t83257441\tchr17
";

    #[test]
    fn layered_classifies_report_only_version() {
        use crate::hgvs::variant::Accession;
        let report = parse_assembly_report(GRCH38_FUTURE_SAMPLE);
        let table = ContigAliases::from_assembly_reports(&[(GenomeBuild::GRCh38, &report)]);

        let future = Accession::new("NC", "000017", Some(99));
        // Hardcoded table has never seen .99 → None.
        assert_eq!(infer_genome_build_from_accession(&future), None);
        // Data-driven layer classifies it from the report.
        assert_eq!(
            infer_genome_build_layered(Some(&table), &future),
            Some("GRCh38")
        );
    }

    #[test]
    fn layered_falls_back_to_hardcoded_when_absent_from_report() {
        use crate::hgvs::variant::Accession;
        let report = parse_assembly_report(GRCH38_FUTURE_SAMPLE);
        let table = ContigAliases::from_assembly_reports(&[(GenomeBuild::GRCh38, &report)]);

        // GRCh37 chr1 (.10) is not in this GRCh38-only report → hardcoded fallback.
        let grch37_chr1 = Accession::new("NC", "000001", Some(10));
        assert_eq!(
            infer_genome_build_layered(Some(&table), &grch37_chr1),
            Some("GRCh37")
        );
    }

    #[test]
    fn layered_with_no_table_matches_hardcoded() {
        use crate::hgvs::variant::Accession;
        let grch38_chr17 = Accession::new("NC", "000017", Some(11));
        assert_eq!(
            infer_genome_build_layered(None, &grch38_chr17),
            infer_genome_build_from_accession(&grch38_chr17)
        );
    }
}