ferro-hgvs 0.6.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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
//! Transcript database loading
//!
//! This module provides functionality to load transcript annotations from
//! standard file formats (GFF3, GTF) commonly used for genome annotations.

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

use crate::error::FerroError;
use crate::reference::transcript::{GenomeBuild, ManeStatus, Transcript};

/// Statistics about MANE transcript coverage in the database
#[derive(Debug, Clone, Default)]
pub struct ManeStats {
    /// Total number of unique genes
    pub total_genes: usize,
    /// Number of genes with MANE Select transcript
    pub genes_with_mane_select: usize,
    /// Number of genes with MANE Plus Clinical transcripts
    pub genes_with_mane_plus_clinical: usize,
    /// Total number of MANE Plus Clinical transcripts
    pub total_mane_plus_clinical_transcripts: usize,
}

impl ManeStats {
    /// Get MANE Select coverage percentage
    pub fn mane_select_coverage(&self) -> f64 {
        if self.total_genes == 0 {
            0.0
        } else {
            (self.genes_with_mane_select as f64 / self.total_genes as f64) * 100.0
        }
    }
}

/// A database of transcripts indexed for efficient lookup
#[derive(Debug, Default)]
pub struct TranscriptDb {
    /// Transcripts indexed by accession ID
    transcripts: HashMap<String, Transcript>,
    /// Index from gene symbol to transcript IDs
    gene_index: HashMap<String, Vec<String>>,
    /// Index from chromosome:region to transcript IDs for overlap queries
    region_index: HashMap<String, Vec<(u64, u64, String)>>,
    /// Index from gene symbol to MANE Select transcript ID
    mane_select_index: HashMap<String, String>,
    /// Index from gene symbol to MANE Plus Clinical transcript IDs
    mane_plus_clinical_index: HashMap<String, Vec<String>>,
    /// Genome build
    pub genome_build: GenomeBuild,
}

impl TranscriptDb {
    /// Create a new empty transcript database
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new database with a specified genome build
    pub fn with_build(build: GenomeBuild) -> Self {
        Self {
            genome_build: build,
            ..Default::default()
        }
    }

    /// Add a transcript to the database
    pub fn add(&mut self, transcript: Transcript) {
        let id = transcript.id.clone();

        // Index by gene symbol
        if let Some(ref gene) = transcript.gene_symbol {
            self.gene_index
                .entry(gene.clone())
                .or_default()
                .push(id.clone());

            // Index MANE transcripts
            match transcript.mane_status {
                ManeStatus::Select => {
                    self.mane_select_index.insert(gene.clone(), id.clone());
                }
                ManeStatus::PlusClinical => {
                    self.mane_plus_clinical_index
                        .entry(gene.clone())
                        .or_default()
                        .push(id.clone());
                }
                ManeStatus::None => {}
            }
        }

        // Index by genomic region
        if let (Some(ref chrom), Some(start), Some(end)) = (
            &transcript.chromosome,
            transcript.genomic_start,
            transcript.genomic_end,
        ) {
            self.region_index
                .entry(chrom.clone())
                .or_default()
                .push((start, end, id.clone()));
        }

        self.transcripts.insert(id, transcript);
    }

    /// Get a transcript by its accession ID
    pub fn get(&self, id: &str) -> Option<&Transcript> {
        self.transcripts.get(id)
    }

    /// Get all transcripts for a gene
    pub fn get_by_gene(&self, gene: &str) -> Vec<&Transcript> {
        self.gene_index
            .get(gene)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.transcripts.get(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get all transcripts overlapping a genomic position
    pub fn get_by_position(&self, chrom: &str, pos: u64) -> Vec<&Transcript> {
        self.region_index
            .get(chrom)
            .map(|regions| {
                regions
                    .iter()
                    .filter(|(start, end, _)| pos >= *start && pos <= *end)
                    .filter_map(|(_, _, id)| self.transcripts.get(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get all transcripts overlapping a genomic region
    pub fn get_by_region(&self, chrom: &str, start: u64, end: u64) -> Vec<&Transcript> {
        self.region_index
            .get(chrom)
            .map(|regions| {
                regions
                    .iter()
                    .filter(|(tx_start, tx_end, _)| {
                        // Check for overlap
                        *tx_start <= end && *tx_end >= start
                    })
                    .filter_map(|(_, _, id)| self.transcripts.get(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get the MANE Select transcript for a gene
    ///
    /// Returns the single representative transcript designated by NCBI/EBI
    /// for this gene, if available.
    pub fn get_mane_select(&self, gene: &str) -> Option<&Transcript> {
        self.mane_select_index
            .get(gene)
            .and_then(|id| self.transcripts.get(id))
    }

    /// Get all MANE Plus Clinical transcripts for a gene
    ///
    /// Returns additional clinically relevant transcripts beyond MANE Select.
    pub fn get_mane_plus_clinical(&self, gene: &str) -> Vec<&Transcript> {
        self.mane_plus_clinical_index
            .get(gene)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.transcripts.get(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get all MANE transcripts for a gene (Select + Plus Clinical)
    pub fn get_mane_transcripts(&self, gene: &str) -> Vec<&Transcript> {
        let mut result = Vec::new();

        if let Some(select) = self.get_mane_select(gene) {
            result.push(select);
        }

        result.extend(self.get_mane_plus_clinical(gene));
        result
    }

    /// Get the best transcript for a gene, preferring MANE Select
    ///
    /// Priority order:
    /// 1. MANE Select
    /// 2. MANE Plus Clinical (first one)
    /// 3. Any coding transcript (first one)
    /// 4. Any transcript (first one)
    pub fn get_preferred_transcript(&self, gene: &str) -> Option<&Transcript> {
        // Try MANE Select first
        if let Some(tx) = self.get_mane_select(gene) {
            return Some(tx);
        }

        // Try MANE Plus Clinical
        let plus_clinical = self.get_mane_plus_clinical(gene);
        if !plus_clinical.is_empty() {
            return Some(plus_clinical[0]);
        }

        // Fall back to gene transcripts
        let transcripts = self.get_by_gene(gene);

        // Prefer coding transcripts
        if let Some(coding) = transcripts.iter().find(|tx| tx.is_coding()) {
            return Some(coding);
        }

        // Any transcript
        transcripts.first().copied()
    }

    /// Check if a gene has a MANE Select transcript
    pub fn has_mane_select(&self, gene: &str) -> bool {
        self.mane_select_index.contains_key(gene)
    }

    /// Get statistics about MANE coverage
    pub fn mane_stats(&self) -> ManeStats {
        let total_genes = self.gene_index.len();
        let genes_with_mane_select = self.mane_select_index.len();
        let genes_with_mane_plus = self.mane_plus_clinical_index.len();
        let total_mane_plus_transcripts: usize = self
            .mane_plus_clinical_index
            .values()
            .map(|v| v.len())
            .sum();

        ManeStats {
            total_genes,
            genes_with_mane_select,
            genes_with_mane_plus_clinical: genes_with_mane_plus,
            total_mane_plus_clinical_transcripts: total_mane_plus_transcripts,
        }
    }

    /// Get the number of transcripts in the database
    pub fn len(&self) -> usize {
        self.transcripts.len()
    }

    /// Check if the database is empty
    pub fn is_empty(&self) -> bool {
        self.transcripts.is_empty()
    }

    /// Get all transcript IDs
    pub fn ids(&self) -> impl Iterator<Item = &String> {
        self.transcripts.keys()
    }

    /// Iterate over all transcripts
    pub fn iter(&self) -> impl Iterator<Item = (&String, &Transcript)> {
        self.transcripts.iter()
    }
}

/// Detect genome build from file header or content
pub fn detect_genome_build<P: AsRef<Path>>(path: P) -> Result<GenomeBuild, FerroError> {
    let file = File::open(path.as_ref()).map_err(|e| FerroError::Io {
        msg: format!("Failed to open file: {}", e),
    })?;
    let reader = BufReader::new(file);

    for line in reader.lines().take(100) {
        let line = line.map_err(|e| FerroError::Io {
            msg: format!("Failed to read line: {}", e),
        })?;

        // Check for genome build indicators
        let line_lower = line.to_lowercase();
        if line_lower.contains("grch37") || line_lower.contains("hg19") {
            return Ok(GenomeBuild::GRCh37);
        }
        if line_lower.contains("grch38") || line_lower.contains("hg38") {
            return Ok(GenomeBuild::GRCh38);
        }
    }

    Ok(GenomeBuild::Unknown)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reference::transcript::{Exon, Strand};
    use std::sync::OnceLock;

    #[test]
    fn test_transcript_db_new() {
        let db = TranscriptDb::new();
        assert!(db.is_empty());
    }

    #[test]
    fn test_transcript_db_add_and_get() {
        let mut db = TranscriptDb::new();

        let tx = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::default(),
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx);

        assert_eq!(db.len(), 1);
        assert!(db.get("NM_000088.3").is_some());
    }

    #[test]
    fn test_transcript_db_get_by_gene() {
        let mut db = TranscriptDb::new();

        let tx1 = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::Select,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx2 = Transcript {
            id: "NM_000089.4".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx1);
        db.add(tx2);

        let transcripts = db.get_by_gene("COL1A1");
        assert_eq!(transcripts.len(), 2);
    }

    #[test]
    fn test_transcript_db_get_by_position() {
        let mut db = TranscriptDb::new();

        let tx = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::default(),
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx);

        // Position within range
        let matches = db.get_by_position("chr1", 1500);
        assert_eq!(matches.len(), 1);

        // Position outside range
        let matches = db.get_by_position("chr1", 500);
        assert_eq!(matches.len(), 0);

        // Different chromosome
        let matches = db.get_by_position("chr2", 1500);
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_mane_select_lookup() {
        let mut db = TranscriptDb::new();

        let tx_mane = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::Select,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx_other = Transcript {
            id: "NM_000089.4".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx_mane);
        db.add(tx_other);

        // Should find MANE Select
        assert!(db.has_mane_select("COL1A1"));
        let mane = db.get_mane_select("COL1A1");
        assert!(mane.is_some());
        assert_eq!(mane.unwrap().id, "NM_000088.3");

        // Preferred transcript should be MANE Select
        let preferred = db.get_preferred_transcript("COL1A1");
        assert!(preferred.is_some());
        assert_eq!(preferred.unwrap().id, "NM_000088.3");
    }

    #[test]
    fn test_mane_stats() {
        let mut db = TranscriptDb::new();

        let tx1 = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::Select,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx2 = Transcript {
            id: "NM_000099.1".to_string(),
            gene_symbol: Some("BRCA1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx1);
        db.add(tx2);

        let stats = db.mane_stats();
        assert_eq!(stats.total_genes, 2);
        assert_eq!(stats.genes_with_mane_select, 1);
        assert!((stats.mane_select_coverage() - 50.0).abs() < 0.01);
    }

    // ===== ManeStats Tests =====

    #[test]
    fn test_mane_stats_default() {
        let stats = ManeStats::default();
        assert_eq!(stats.total_genes, 0);
        assert_eq!(stats.genes_with_mane_select, 0);
        assert_eq!(stats.genes_with_mane_plus_clinical, 0);
        assert_eq!(stats.total_mane_plus_clinical_transcripts, 0);
    }

    #[test]
    fn test_mane_stats_coverage_empty() {
        let stats = ManeStats::default();
        assert_eq!(stats.mane_select_coverage(), 0.0);
    }

    #[test]
    fn test_mane_stats_coverage_full() {
        let stats = ManeStats {
            total_genes: 100,
            genes_with_mane_select: 100,
            genes_with_mane_plus_clinical: 50,
            total_mane_plus_clinical_transcripts: 60,
        };
        assert!((stats.mane_select_coverage() - 100.0).abs() < 0.01);
    }

    // ===== TranscriptDb Extended Tests =====

    #[test]
    fn test_transcript_db_with_build() {
        let db = TranscriptDb::with_build(GenomeBuild::GRCh37);
        assert!(matches!(db.genome_build, GenomeBuild::GRCh37));
    }

    #[test]
    fn test_transcript_db_get_by_region() {
        let mut db = TranscriptDb::new();

        let tx = Transcript {
            id: "NM_000088.3".to_string(),
            gene_symbol: Some("COL1A1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::default(),
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx);

        // Overlapping region
        let matches = db.get_by_region("chr1", 1500, 1600);
        assert_eq!(matches.len(), 1);

        // Adjacent region (no overlap)
        let matches = db.get_by_region("chr1", 500, 999);
        assert_eq!(matches.len(), 0);

        // Fully containing region
        let matches = db.get_by_region("chr1", 900, 2100);
        assert_eq!(matches.len(), 1);

        // Different chromosome
        let matches = db.get_by_region("chr2", 1000, 2000);
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_transcript_db_mane_plus_clinical() {
        let mut db = TranscriptDb::new();

        let tx_plus1 = Transcript {
            id: "NM_000100.1".to_string(),
            gene_symbol: Some("BRCA1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::PlusClinical,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx_plus2 = Transcript {
            id: "NM_000101.1".to_string(),
            gene_symbol: Some("BRCA1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::PlusClinical,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx_plus1);
        db.add(tx_plus2);

        let plus_clinical = db.get_mane_plus_clinical("BRCA1");
        assert_eq!(plus_clinical.len(), 2);

        // No MANE Select for this gene
        assert!(db.get_mane_select("BRCA1").is_none());

        // Get all MANE transcripts
        let all_mane = db.get_mane_transcripts("BRCA1");
        assert_eq!(all_mane.len(), 2);
    }

    #[test]
    fn test_transcript_db_preferred_transcript_fallback() {
        let mut db = TranscriptDb::new();

        // Non-MANE, non-coding transcript
        let tx_noncoding = Transcript {
            id: "NR_000001.1".to_string(),
            gene_symbol: Some("TEST".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: None,
            cds_end: None,
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        // Coding transcript (preferred over non-coding)
        let tx_coding = Transcript {
            id: "NM_000001.1".to_string(),
            gene_symbol: Some("TEST".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx_noncoding);
        db.add(tx_coding);

        // Should prefer coding transcript
        let preferred = db.get_preferred_transcript("TEST");
        assert!(preferred.is_some());
        assert!(preferred.unwrap().is_coding());
    }

    #[test]
    fn test_transcript_db_ids_iter() {
        let mut db = TranscriptDb::new();

        let tx1 = Transcript {
            id: "NM_000001.1".to_string(),
            gene_symbol: Some("GENE1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx2 = Transcript {
            id: "NM_000002.1".to_string(),
            gene_symbol: Some("GENE2".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx1);
        db.add(tx2);

        let ids: Vec<_> = db.ids().collect();
        assert_eq!(ids.len(), 2);

        let iter_count = db.iter().count();
        assert_eq!(iter_count, 2);
    }

    // ===== Gene Lookup Edge Cases =====

    #[test]
    fn test_transcript_db_nonexistent_gene() {
        let db = TranscriptDb::new();

        assert!(db.get_by_gene("NONEXISTENT").is_empty());
        assert!(db.get_mane_select("NONEXISTENT").is_none());
        assert!(db.get_mane_plus_clinical("NONEXISTENT").is_empty());
        assert!(db.get_mane_transcripts("NONEXISTENT").is_empty());
        assert!(db.get_preferred_transcript("NONEXISTENT").is_none());
        assert!(!db.has_mane_select("NONEXISTENT"));
    }

    #[test]
    fn test_transcript_db_no_gene_symbol() {
        let mut db = TranscriptDb::new();

        // Transcript without gene symbol
        let tx = Transcript {
            id: "NM_000001.1".to_string(),
            gene_symbol: None,
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: Some("chr1".to_string()),
            genomic_start: Some(1000),
            genomic_end: Some(2000),
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx);

        // Should still be findable by ID and position
        assert!(db.get("NM_000001.1").is_some());
        assert!(!db.get_by_position("chr1", 1500).is_empty());
    }

    // ===== Additional TranscriptDb Tests =====

    #[test]
    fn test_get_mane_transcripts() {
        let mut db = TranscriptDb::new();

        let tx1 = Transcript {
            id: "NM_000001.1".to_string(),
            gene_symbol: Some("BRCA1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::Select,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        let tx2 = Transcript {
            id: "NM_000002.1".to_string(),
            gene_symbol: Some("BRCA1".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::PlusClinical,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx1);
        db.add(tx2);

        let mane_txs = db.get_mane_transcripts("BRCA1");
        assert_eq!(mane_txs.len(), 2); // Both MANE Select and Plus Clinical
    }

    #[test]
    fn test_get_preferred_transcript_priority() {
        let mut db = TranscriptDb::new();

        // Add transcripts in reverse priority order
        let tx_none = Transcript {
            id: "NM_000003.1".to_string(),
            gene_symbol: Some("TEST".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::None,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };

        db.add(tx_none);

        // Without MANE, should return the coding transcript
        let preferred = db.get_preferred_transcript("TEST").unwrap();
        assert_eq!(preferred.id, "NM_000003.1");

        // Add MANE Plus Clinical
        let tx_plus = Transcript {
            id: "NM_000002.1".to_string(),
            gene_symbol: Some("TEST".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::PlusClinical,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };
        db.add(tx_plus);

        // Should prefer MANE Plus Clinical
        let preferred = db.get_preferred_transcript("TEST").unwrap();
        assert_eq!(preferred.id, "NM_000002.1");

        // Add MANE Select
        let tx_select = Transcript {
            id: "NM_000001.1".to_string(),
            gene_symbol: Some("TEST".to_string()),
            strand: Strand::Plus,
            sequence: Some("ATGC".to_string()),
            cds_start: Some(1),
            cds_end: Some(4),
            exons: vec![Exon::new(1, 1, 4)],
            chromosome: None,
            genomic_start: None,
            genomic_end: None,
            genome_build: GenomeBuild::GRCh38,
            mane_status: ManeStatus::Select,
            refseq_match: None,
            ensembl_match: None,
            exon_cigars: Vec::new(),
            cached_introns: OnceLock::new(),
        };
        db.add(tx_select);

        // Should prefer MANE Select
        let preferred = db.get_preferred_transcript("TEST").unwrap();
        assert_eq!(preferred.id, "NM_000001.1");
    }
}