ferro-hgvs 0.5.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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
//! Registry of all error and warning codes with documentation.
//!
//! This module provides a comprehensive registry of all error (E-codes) and
//! warning (W-codes) in ferro-hgvs, enabling the `ferro explain` command.

use super::codes::{CodeCategory, CodeInfo, ModeBehavior};
use std::collections::HashMap;
use std::sync::OnceLock;

/// Static registry of all error and warning codes.
static CODE_REGISTRY: OnceLock<HashMap<&'static str, CodeInfo>> = OnceLock::new();

/// Get the code registry, initializing it if needed.
fn get_registry() -> &'static HashMap<&'static str, CodeInfo> {
    CODE_REGISTRY.get_or_init(build_registry)
}

/// Build the code registry.
fn build_registry() -> HashMap<&'static str, CodeInfo> {
    let mut map = HashMap::new();

    // =========================================================================
    // ERROR CODES (E-prefix)
    // =========================================================================

    // --- Parse Errors (E1xxx) ---

    map.insert(
        "E1001",
        CodeInfo {
            code: "E1001",
            name: "InvalidAccession",
            summary: "The accession identifier does not match any recognized format.",
            explanation: "HGVS expressions require a valid reference sequence accession number. \
                Valid prefixes include NM_ (mRNA), NP_ (protein), NC_ (chromosome), \
                NG_ (genomic region), NR_ (non-coding RNA), and LRG_ (Locus Reference Genomic).",
            category: CodeCategory::Parse,
            bad_examples: &["XY_000088.3:c.459A>G", "000088.3:c.459A>G"],
            good_examples: &["NM_000088.3:c.459A>G", "NC_000001.11:g.12345A>G"],
            mode_behavior: None,
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["E1002", "W1003"],
        },
    );

    map.insert(
        "E1002",
        CodeInfo {
            code: "E1002",
            name: "UnknownVariantType",
            summary: "Unknown variant type prefix.",
            explanation: "HGVS expressions must specify a coordinate type prefix: \
                g. (genomic), c. (coding DNA), n. (non-coding DNA), r. (RNA), \
                p. (protein), m. (mitochondrial), or o. (circular).",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:x.459A>G", "NM_000088.3:459A>G"],
            good_examples: &["NM_000088.3:c.459A>G", "NC_000001.11:g.12345A>G"],
            mode_behavior: None,
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["E1001", "W3006"],
        },
    );

    map.insert(
        "E1003",
        CodeInfo {
            code: "E1003",
            name: "InvalidPosition",
            summary: "The position format is invalid.",
            explanation: "Positions must be valid integers or offset positions (for intronic variants). \
                Position 0 is never valid in HGVS. Negative positions indicate upstream of the start codon, \
                and *N positions indicate downstream of the stop codon.",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:c.0A>G", "NM_000088.3:c.A>G"],
            good_examples: &["NM_000088.3:c.1A>G", "NM_000088.3:c.-10A>G"],
            mode_behavior: None,
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/DNA/numbering/"),
            related_codes: &["E3001", "W4001", "W4002"],
        },
    );

    map.insert(
        "E1004",
        CodeInfo {
            code: "E1004",
            name: "InvalidEdit",
            summary: "The edit type or format is invalid.",
            explanation: "Valid edit types include substitutions (A>G), deletions (del), \
                insertions (ins), duplications (dup), inversions (inv), and \
                deletion-insertions (delins). The edit format must match HGVS syntax.",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:c.459A->G", "NM_000088.3:c.459mutation"],
            good_examples: &["NM_000088.3:c.459A>G", "NM_000088.3:c.459del"],
            mode_behavior: None,
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/DNA/variant/",
            ),
            related_codes: &["E1005", "E1007"],
        },
    );

    map.insert(
        "E1005",
        CodeInfo {
            code: "E1005",
            name: "UnexpectedEnd",
            summary: "Unexpected end of input.",
            explanation: "The HGVS expression ended before it was complete. \
                This usually means a required component is missing.",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:c.", "NM_000088.3:c.459"],
            good_examples: &["NM_000088.3:c.459A>G", "NM_000088.3:c.459del"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E1004", "E1006"],
        },
    );

    map.insert(
        "E1006",
        CodeInfo {
            code: "E1006",
            name: "UnexpectedChar",
            summary: "Unexpected character in input.",
            explanation: "The parser encountered a character that is not valid at this position. \
                Check for typos, extra characters, or incorrect syntax.",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:c.459A>>G", "NM_000088.3:c.459A>G;extra"],
            good_examples: &["NM_000088.3:c.459A>G"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E1005", "W2004"],
        },
    );

    map.insert(
        "E1007",
        CodeInfo {
            code: "E1007",
            name: "InvalidBase",
            summary: "Invalid nucleotide base.",
            explanation: "Nucleotide bases must be one of A, C, G, T (or U for RNA). \
                For protein variants, use three-letter amino acid codes.",
            category: CodeCategory::Parse,
            bad_examples: &["NM_000088.3:c.459X>G", "NM_000088.3:c.459A>X"],
            good_examples: &["NM_000088.3:c.459A>G", "NM_000088.3:c.459A>T"],
            mode_behavior: None,
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/DNA/variant/substitution/",
            ),
            related_codes: &["E1008"],
        },
    );

    map.insert(
        "E1008",
        CodeInfo {
            code: "E1008",
            name: "InvalidAminoAcid",
            summary: "Invalid amino acid code.",
            explanation:
                "Amino acids must be specified using three-letter codes (e.g., Val, Glu, Ala). \
                Single-letter codes may be accepted in lenient mode.",
            category: CodeCategory::Parse,
            bad_examples: &["NP_000079.2:p.Xxx600Yyy", "NP_000079.2:p.Val600Xxx"],
            good_examples: &["NP_000079.2:p.Val600Glu", "NP_000079.2:p.Arg342Ter"],
            mode_behavior: None,
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/",
            ),
            related_codes: &["W1001", "W1002"],
        },
    );

    // --- Reference Errors (E2xxx) ---

    map.insert(
        "E2001",
        CodeInfo {
            code: "E2001",
            name: "ReferenceNotFound",
            summary: "Reference sequence or transcript not found.",
            explanation: "The specified accession could not be found in the reference data. \
                Ensure the accession is correct and that reference data has been loaded.",
            category: CodeCategory::Reference,
            bad_examples: &["NM_999999.1:c.100A>G"],
            good_examples: &["NM_000088.3:c.100A>G"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E2002", "E2003"],
        },
    );

    map.insert(
        "E2002",
        CodeInfo {
            code: "E2002",
            name: "SequenceNotFound",
            summary: "Sequence data not available.",
            explanation: "The sequence for this accession is not available. \
                This may occur when transcript metadata exists but sequence data is missing.",
            category: CodeCategory::Reference,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E2001"],
        },
    );

    map.insert(
        "E2003",
        CodeInfo {
            code: "E2003",
            name: "ChromosomeNotFound",
            summary: "Chromosome or contig not found.",
            explanation:
                "The specified chromosome or contig is not available in the reference data. \
                Check that the genome build matches your reference data.",
            category: CodeCategory::Reference,
            bad_examples: &["NC_999999.1:g.100A>G"],
            good_examples: &["NC_000001.11:g.100A>G"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E2001"],
        },
    );

    // --- Validation Errors (E3xxx) ---

    map.insert(
        "E3001",
        CodeInfo {
            code: "E3001",
            name: "PositionOutOfBounds",
            summary: "Position is outside the valid range.",
            explanation: "The specified position is beyond the length of the reference sequence. \
                Check that the position is correct for this transcript or chromosome.",
            category: CodeCategory::Validation,
            bad_examples: &["NM_000088.3:c.99999999A>G"],
            good_examples: &["NM_000088.3:c.459A>G"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E1003", "E3003"],
        },
    );

    map.insert(
        "E3002",
        CodeInfo {
            code: "E3002",
            name: "ReferenceMismatch",
            summary: "Reference sequence mismatch.",
            explanation: "The reference base(s) stated in the HGVS expression do not match \
                the actual reference sequence at that position. This may indicate a wrong \
                transcript version or a typo in the variant description.",
            category: CodeCategory::Validation,
            bad_examples: &["NM_000088.3:c.459G>A (when ref is actually A)"],
            good_examples: &["NM_000088.3:c.459A>G"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["W5001", "W3001"],
        },
    );

    map.insert(
        "E3003",
        CodeInfo {
            code: "E3003",
            name: "InvalidRange",
            summary: "Invalid coordinate range.",
            explanation: "The start position is greater than the end position, \
                or the range is otherwise invalid.",
            category: CodeCategory::Validation,
            bad_examples: &["NM_000088.3:c.200_100del"],
            good_examples: &["NM_000088.3:c.100_200del"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["W4001"],
        },
    );

    map.insert(
        "E3004",
        CodeInfo {
            code: "E3004",
            name: "ExonIntronBoundary",
            summary: "Variant spans exon-intron boundary.",
            explanation:
                "The variant spans an exon-intron junction, which cannot be properly normalized. \
                Split into separate exonic and intronic components if needed.",
            category: CodeCategory::Validation,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E3005", "E4001"],
        },
    );

    map.insert(
        "E3005",
        CodeInfo {
            code: "E3005",
            name: "UtrCdsBoundary",
            summary: "Variant spans UTR-CDS boundary.",
            explanation: "The variant spans the boundary between UTR and coding sequence. \
                This requires special handling for normalization.",
            category: CodeCategory::Validation,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E3004"],
        },
    );

    map.insert(
        "E3006",
        CodeInfo {
            code: "E3006",
            name: "SelfCancellingAllele",
            summary: "Self-cancelling allele (overlapping del + dup or equivalent).",
            explanation: "HGVS does not allow descriptions that remove part of a reference \
                sequence and re-insert (part of) the same sequence in the same allele. \
                For example, `c.[762_768del;767_774dup]` deletes [762..=768] and re-introduces \
                bases [767..=774] from the reference, partially undoing the deletion. \
                Drop the redundant edit (or rewrite as a single `delins`).",
            category: CodeCategory::Validation,
            bad_examples: &["NM_004006.2:c.[762_768del;767_774dup]"],
            good_examples: &["NM_004006.2:c.[100_110del;200_210dup]"],
            mode_behavior: None,
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["E3003", "W3003"],
        },
    );

    // --- Normalization Errors (E4xxx) ---

    map.insert(
        "E4001",
        CodeInfo {
            code: "E4001",
            name: "IntronicVariant",
            summary: "Intronic variant cannot be normalized.",
            explanation: "Intronic variants (those with +/- offset positions) cannot be normalized \
                without genomic context. Use genomic coordinates for intronic variant normalization.",
            category: CodeCategory::Normalization,
            bad_examples: &["NM_000088.3:c.459+10del"],
            good_examples: &["NC_000017.11:g.12345del"],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E3004"],
        },
    );

    map.insert(
        "E4002",
        CodeInfo {
            code: "E4002",
            name: "UnsupportedVariant",
            summary: "Unsupported variant type for this operation.",
            explanation: "This variant type is not supported for the requested operation. \
                Not all HGVS variant types can be normalized or converted.",
            category: CodeCategory::Normalization,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &[],
        },
    );

    // --- Conversion Errors (E5xxx) ---

    map.insert(
        "E5001",
        CodeInfo {
            code: "E5001",
            name: "ConversionFailed",
            summary: "Coordinate conversion failed.",
            explanation: "The coordinate conversion between reference sequences failed. \
                This may occur when converting between coding and genomic coordinates.",
            category: CodeCategory::Conversion,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E5002"],
        },
    );

    map.insert(
        "E5002",
        CodeInfo {
            code: "E5002",
            name: "NoOverlappingTranscript",
            summary: "No overlapping transcript found.",
            explanation:
                "No transcript was found that overlaps with the specified genomic position. \
                The position may be intergenic or the transcript data may be incomplete.",
            category: CodeCategory::Conversion,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E5001", "E2001"],
        },
    );

    // --- IO Errors (E9xxx) ---

    map.insert(
        "E9001",
        CodeInfo {
            code: "E9001",
            name: "IoError",
            summary: "File I/O error.",
            explanation: "An error occurred while reading or writing a file. \
                Check that the file exists and you have the necessary permissions.",
            category: CodeCategory::Io,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E9002"],
        },
    );

    map.insert(
        "E9002",
        CodeInfo {
            code: "E9002",
            name: "JsonError",
            summary: "JSON parsing error.",
            explanation: "An error occurred while parsing JSON data. \
                Check that the JSON is valid and matches the expected format.",
            category: CodeCategory::Io,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: None,
            hgvs_spec_url: None,
            related_codes: &["E9001"],
        },
    );

    // =========================================================================
    // WARNING CODES (W-prefix)
    // =========================================================================

    // --- Case/Capitalization Warnings (W1xxx) ---

    map.insert(
        "W1001",
        CodeInfo {
            code: "W1001",
            name: "LowercaseAminoAcid",
            summary: "Lowercase amino acid code.",
            explanation: "Three-letter amino acid codes should use standard capitalization \
                with the first letter uppercase (e.g., Val, Glu, Ala). Lowercase codes like \
                'val' or 'glu' are auto-corrected in lenient/silent modes.",
            category: CodeCategory::Case,
            bad_examples: &["p.val600glu", "p.VAL600GLU"],
            good_examples: &["p.Val600Glu"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/",
            ),
            related_codes: &["W1002", "E1008"],
        },
    );

    map.insert(
        "W1002",
        CodeInfo {
            code: "W1002",
            name: "SingleLetterAminoAcid",
            summary: "Single-letter amino acid code.",
            explanation: "HGVS recommends three-letter amino acid codes. Single-letter codes \
                like 'V' for Valine may be auto-expanded in lenient/silent modes.",
            category: CodeCategory::Case,
            bad_examples: &["p.V600E"],
            good_examples: &["p.Val600Glu"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/",
            ),
            related_codes: &["W1001"],
        },
    );

    map.insert(
        "W1003",
        CodeInfo {
            code: "W1003",
            name: "LowercaseAccessionPrefix",
            summary: "Lowercase accession prefix.",
            explanation: "Accession prefixes should be uppercase (NM_, NC_, NP_, etc.). \
                Lowercase prefixes like 'nm_' are auto-corrected in lenient/silent modes.",
            category: CodeCategory::Case,
            bad_examples: &["nm_000088.3:c.459A>G", "nc_000001.11:g.12345A>G"],
            good_examples: &["NM_000088.3:c.459A>G", "NC_000001.11:g.12345A>G"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["E1001"],
        },
    );

    map.insert(
        "W1004",
        CodeInfo {
            code: "W1004",
            name: "MixedCaseEditType",
            summary: "Mixed case in edit type keyword.",
            explanation: "Edit type keywords should be lowercase (del, ins, dup, etc.). \
                Mixed case like 'Del' or 'INS' is auto-corrected in lenient/silent modes.",
            category: CodeCategory::Case,
            bad_examples: &["c.100Del", "c.100_101INS"],
            good_examples: &["c.100del", "c.100_101ins"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &[],
        },
    );

    // --- Character Warnings (W2xxx) ---

    map.insert(
        "W2001",
        CodeInfo {
            code: "W2001",
            name: "WrongDashCharacter",
            summary: "Wrong dash character (en-dash or em-dash).",
            explanation: "HGVS requires ASCII hyphen-minus (-). Word processors often \
                substitute en-dash (–) or em-dash (—). These are auto-corrected in \
                lenient/silent modes.",
            category: CodeCategory::Character,
            bad_examples: &["c.100–200del", "c.100—200del"],
            good_examples: &["c.100-200del"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["W2002", "W2004"],
        },
    );

    map.insert(
        "W2002",
        CodeInfo {
            code: "W2002",
            name: "WrongQuoteCharacter",
            summary: "Smart quotes instead of ASCII quotes.",
            explanation: "HGVS requires ASCII quotes. Word processors often substitute \
                curly/smart quotes. These are auto-corrected in lenient/silent modes.",
            category: CodeCategory::Character,
            bad_examples: &["c.100_101ins\u{201C}ATG\u{201D}"],
            good_examples: &["c.100_101ins\"ATG\""],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["W2001", "W2004"],
        },
    );

    map.insert(
        "W2003",
        CodeInfo {
            code: "W2003",
            name: "ExtraWhitespace",
            summary: "Extra whitespace in description.",
            explanation: "HGVS expressions should not contain spaces (the spec is explicit \
                that the format `reference:description` has spaces \"added for clarity only\"). \
                Leading, trailing, and embedded whitespace — and zero-width invisible \
                characters (U+200B ZERO WIDTH SPACE, U+200C ZERO WIDTH NON-JOINER, \
                U+200D ZERO WIDTH JOINER, U+FEFF BOM/ZWNBSP) — are stripped in \
                lenient/silent modes.",
            category: CodeCategory::Character,
            bad_examples: &[
                "c.100 A>G",
                "NM_000088.3 : c.459A>G",
                "c.[100A>G ; 200T>C]",
                "NM_000088.3\u{200B}:c.100A>G",
            ],
            good_examples: &["c.100A>G", "NM_000088.3:c.459A>G"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["W2001", "W2004"],
        },
    );

    map.insert(
        "W2004",
        CodeInfo {
            code: "W2004",
            name: "InvalidUnicodeCharacter",
            summary: "Non-ASCII Unicode character.",
            explanation: "HGVS expressions should use ASCII characters only. \
                Common Unicode look-alikes are auto-corrected in lenient/silent modes.",
            category: CodeCategory::Character,
            bad_examples: &[],
            good_examples: &[],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["W2001", "W2002"],
        },
    );

    // --- Format/Syntax Warnings (W3xxx) ---

    map.insert(
        "W3001",
        CodeInfo {
            code: "W3001",
            name: "MissingVersion",
            summary: "Missing transcript version.",
            explanation: "Reference sequence accessions should include a version number \
                (e.g., NM_000088.3 not NM_000088). Without a version, the correct reference \
                sequence cannot be determined. In lenient/silent modes, parsing succeeds but \
                the variant is flagged for downstream handling.",
            category: CodeCategory::Format,
            bad_examples: &["NM_000088:c.459A>G"],
            good_examples: &["NM_000088.3:c.459A>G"],
            mode_behavior: Some(ModeBehavior::warn_accept()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["E3002"],
        },
    );

    map.insert(
        "W3002",
        CodeInfo {
            code: "W3002",
            name: "ProteinSubstitutionArrow",
            summary: "Arrow in protein substitution.",
            explanation: "Protein substitutions should not use '>' between amino acids. \
                Use the format 'p.Val600Glu' not 'p.Val600>Glu'.",
            category: CodeCategory::Format,
            bad_examples: &["p.Val600>Glu"],
            good_examples: &["p.Val600Glu"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/protein/variant/substitution/"),
            related_codes: &[],
        },
    );

    map.insert(
        "W3003",
        CodeInfo {
            code: "W3003",
            name: "OldSubstitutionSyntax",
            summary: "Old-style multi-base substitution syntax.",
            explanation: "Multi-base substitutions should use 'delins' not '>'. \
                For example, 'c.100_102delinsATG' not 'c.100_102>ATG'.",
            category: CodeCategory::Format,
            bad_examples: &["c.100_102>ATG"],
            good_examples: &["c.100_102delinsATG"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &[],
        },
    );

    map.insert(
        "W3004",
        CodeInfo {
            code: "W3004",
            name: "OldAlleleFormat",
            summary: "Old/deprecated allele bracket format.",
            explanation: "In compound allele notation, the coordinate type should appear before \
                the brackets. Use 'NM_000088.3:c.[100A>G;200C>T]' not 'NM_000088.3:[c.100A>G;c.200C>T]'.",
            category: CodeCategory::Format,
            bad_examples: &["NM_000088.3:[c.100A>G;c.200C>T]"],
            good_examples: &["NM_000088.3:c.[100A>G;200C>T]"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &[],
        },
    );

    map.insert(
        "W3005",
        CodeInfo {
            code: "W3005",
            name: "TrailingAnnotation",
            summary: "Trailing protein annotation.",
            explanation: "ClinVar and other databases often append protein consequence annotations \
                to HGVS expressions (e.g., '(p.Lys236=)'). These are stripped in lenient/silent modes.",
            category: CodeCategory::Format,
            bad_examples: &["NM_000088.3:c.459A>G (p.Lys153=)", "NM_000088.3:c.100del (p.Val34fs)"],
            good_examples: &["NM_000088.3:c.459A>G", "NM_000088.3:c.100del"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &[],
        },
    );

    map.insert(
        "W3006",
        CodeInfo {
            code: "W3006",
            name: "MissingCoordinatePrefix",
            summary: "Missing coordinate type prefix.",
            explanation: "HGVS expressions require a coordinate type prefix (g., c., p., etc.). \
                For chromosomal accessions (NC_), 'g.' can be inferred in lenient/silent modes.",
            category: CodeCategory::Format,
            bad_examples: &["NC_000001.11:12345A>G"],
            good_examples: &["NC_000001.11:g.12345A>G"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["E1002"],
        },
    );

    map.insert(
        "W3007",
        CodeInfo {
            code: "W3007",
            name: "DeprecatedStopCodonStar",
            summary: "Deprecated '*' for stop codon in protein substitution.",
            explanation: "The HGVS checklist permits both 'Ter' and '*' to indicate a \
                translation stop codon, but the three-letter 'Ter' is the preferred form. \
                In lenient/silent modes 'p.Arg97*' is rewritten to 'p.Arg97Ter'.",
            category: CodeCategory::Format,
            bad_examples: &["NP_000079.2:p.Arg97*", "NP_000079.2:p.Trp10*"],
            good_examples: &["NP_000079.2:p.Arg97Ter", "NP_000079.2:p.Trp10Ter"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/checklist/"),
            related_codes: &["W3008", "W3009", "W3010"],
        },
    );

    map.insert(
        "W3008",
        CodeInfo {
            code: "W3008",
            name: "DeprecatedStopCodonX",
            summary: "Deprecated 'X' for stop codon in protein substitution.",
            explanation: "Per the HGVS checklist, 'the X should not be used' to indicate a \
                translation stop codon. The single-letter 'X' is also reserved for the \
                'any amino acid' symbol Xaa, making 'p.Arg97X' ambiguous. In lenient/silent \
                modes the input is rewritten to 'p.Arg97Ter'.",
            category: CodeCategory::Format,
            bad_examples: &["NP_000079.2:p.Arg97X", "NP_000079.2:p.Trp10X"],
            good_examples: &["NP_000079.2:p.Arg97Ter", "NP_000079.2:p.Trp10Ter"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/substitution/",
            ),
            related_codes: &["W3007", "W3009", "W3010"],
        },
    );

    map.insert(
        "W3009",
        CodeInfo {
            code: "W3009",
            name: "DeprecatedFrameshiftStar",
            summary: "Deprecated 'fs*N' frameshift termination notation.",
            explanation: "The canonical HGVS frameshift form uses 'fsTerN', e.g. \
                'p.Arg123LysfsTer34'. The 'fs*N' alternative is permitted but discouraged. \
                In lenient/silent modes 'p.Arg97fs*23' is rewritten to 'p.Arg97fsTer23'.",
            category: CodeCategory::Format,
            bad_examples: &["NP_000079.2:p.Arg97fs*23", "NP_000079.2:p.Ser539Alafs*110"],
            good_examples: &[
                "NP_000079.2:p.Arg97fsTer23",
                "NP_000079.2:p.Ser539AlafsTer110",
            ],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/frameshift/",
            ),
            related_codes: &["W3007", "W3008", "W3010"],
        },
    );

    map.insert(
        "W3010",
        CodeInfo {
            code: "W3010",
            name: "DeprecatedFrameshiftX",
            summary: "Deprecated 'fsXN' frameshift termination notation.",
            explanation: "Neither 'X' nor 'Xaa' is used in HGVS frameshift termination; the \
                canonical form is 'fsTerN'. The strict parser rejects 'fsXN' outright; in \
                lenient/silent modes the preprocessor rewrites 'p.Arg97fsX23' to \
                'p.Arg97fsTer23' and emits this warning.",
            category: CodeCategory::Format,
            bad_examples: &["NP_000079.2:p.Arg97fsX23"],
            good_examples: &["NP_000079.2:p.Arg97fsTer23"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/protein/variant/frameshift/",
            ),
            related_codes: &["W3007", "W3008", "W3009"],
        },
    );

    map.insert(
        "W3011",
        CodeInfo {
            code: "W3011",
            name: "DelSizeSuffix",
            summary: "Deletion described with a size-count suffix instead of a position range.",
            explanation:
                "HGVS recommends naming both endpoints of a multi-residue deletion. The legacy \
                form `g.123del6` (size 6 starting at position 123) is non-canonical; the canonical \
                form lists the first and last residue deleted, e.g. `g.123_128del`. ferro cannot \
                auto-synthesize the end position safely (it depends on offset/intronic semantics), \
                so this warning is emitted but not auto-corrected.",
            category: CodeCategory::Format,
            bad_examples: &["NG_012232.1:g.123del6"],
            good_examples: &["NG_012232.1:g.123_128del"],
            mode_behavior: Some(ModeBehavior::warn_accept()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/DNA/deletion/",
            ),
            related_codes: &["W4003"],
        },
    );

    map.insert(
        "W3012",
        CodeInfo {
            code: "W3012",
            name: "EmptyDelinsInsert",
            summary: "Deletion-insertion with empty inserted sequence.",
            explanation:
                "A `delins` with no inserted sequence is semantically equivalent to a plain \
                deletion. Per HGVS spec the canonical form is `del`. In lenient/silent modes, \
                ferro rewrites `delins` to `del` and warns once.",
            category: CodeCategory::Format,
            bad_examples: &["NC_000001.11:g.100_102delins"],
            good_examples: &["NC_000001.11:g.100_102del"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/DNA/delins/"),
            related_codes: &[],
        },
    );

    map.insert(
        "W3013",
        CodeInfo {
            code: "W3013",
            name: "RedundantRepeatLabel",
            summary: "Repeat description with redundant base label.",
            explanation:
                "HGVS spec discourages including the repeat-unit base label when the positions \
                already define the unit, e.g. `r.-125_-123cug[4]` should be written as \
                `r.-125_-123[4]`. In lenient/silent modes, ferro strips the redundant base \
                segment and warns once.",
            category: CodeCategory::Format,
            bad_examples: &["NM_000088.3:r.100_102cug[4]"],
            good_examples: &["NM_000088.3:r.100_102[4]"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/RNA/repeated/",
            ),
            related_codes: &[],
        },
    );

    map.insert(
        "W3014",
        CodeInfo {
            code: "W3014",
            name: "DeprecatedIvsNotation",
            summary: "Retracted c.IVS intronic notation.",
            explanation: "The c.IVSn+offset / c.IVSn-offset notation has been retracted by \
                HGVS because the IVS number is not unique across transcripts and cannot be \
                resolved to a unique genomic position without metadata. Use the canonical \
                intronic-offset form, e.g. c.88+2T>G instead of c.IVS2+2T>G. ferro cannot \
                auto-rewrite this form without genomic context, so all modes reject the input \
                and emit an actionable hint.",
            category: CodeCategory::Format,
            bad_examples: &["c.IVS2+2T>G", "c.IVS5-1G>T"],
            good_examples: &["c.88+2T>G", "c.123-1G>T"],
            mode_behavior: Some(ModeBehavior::always_reject()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/background/numbering/"),
            related_codes: &["E1006", "E4001"],
        },
    );

    map.insert(
        "W3015",
        CodeInfo {
            code: "W3015",
            name: "DeprecatedConSyntax",
            summary: "Deprecated 'con' (sequence conversion) edit syntax.",
            explanation:
                "The HGVS spec retired the `con` edit type in favour of `delins`. Conversions \
                (a range of nucleotides replaced by a sequence from elsewhere) should be \
                described as deletion-insertions: `c.100_200conNM_001.1:c.5_105` becomes \
                `c.100_200delinsNM_001.1:c.5_105`. In lenient/silent modes ferro auto-rewrites; \
                in strict mode the input is rejected.",
            category: CodeCategory::Format,
            bad_examples: &["c.100_200conNM_001.1:c.5_105"],
            good_examples: &["c.100_200delinsNM_001.1:c.5_105"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/DNA/delins/"),
            related_codes: &["W3003"],
        },
    );

    // --- Position/Range Warnings (W4xxx) ---

    map.insert(
        "W4001",
        CodeInfo {
            code: "W4001",
            name: "SwappedPositions",
            summary: "Swapped/inverted range positions.",
            explanation: "In a range, the start position should be less than the end position. \
                Ranges like 'c.200_100del' are corrected to 'c.100_200del' in lenient/silent modes.",
            category: CodeCategory::Position,
            bad_examples: &["c.200_100del", "g.50000_10000dup"],
            good_examples: &["c.100_200del", "g.10000_50000dup"],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: None,
            related_codes: &["E3003"],
        },
    );

    map.insert(
        "W4002",
        CodeInfo {
            code: "W4002",
            name: "PositionZero",
            summary: "Invalid position zero.",
            explanation: "Position 0 does not exist in HGVS notation. Coding sequences start at \
                position 1 (ATG start codon), with upstream positions numbered as -1, -2, etc. \
                This error cannot be auto-corrected.",
            category: CodeCategory::Position,
            bad_examples: &["c.0A>G", "g.0del"],
            good_examples: &["c.1A>G", "c.-1A>G"],
            mode_behavior: Some(ModeBehavior::always_reject()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/DNA/numbering/",
            ),
            related_codes: &["E1003"],
        },
    );

    map.insert(
        "W4003",
        CodeInfo {
            code: "W4003",
            name: "SinglePositionRange",
            summary: "Single-position range used where a single position is canonical.",
            explanation: "HGVS spec requires `position(s)_deleted` (and the analogous \
                `positions_duplicated` / `positions_inverted`) to contain two different positions. \
                Forms like `c.123_123del`, `c.123_123dup`, and `c.100_100inv` should be written \
                with the single-position form `c.123del` / `c.123dup` / `c.100inv` respectively. \
                In lenient/silent modes, ferro collapses the redundant range and warns once.",
            category: CodeCategory::Position,
            bad_examples: &[
                "NM_000088.3:c.123_123del",
                "NM_000088.3:c.123_123dup",
                "NM_000088.3:c.100_100inv",
            ],
            good_examples: &[
                "NM_000088.3:c.123del",
                "NM_000088.3:c.123dup",
                "NM_000088.3:c.100inv",
            ],
            mode_behavior: Some(ModeBehavior::standard_correctable()),
            hgvs_spec_url: Some(
                "https://hgvs-nomenclature.org/stable/recommendations/DNA/deletion/",
            ),
            related_codes: &["W3011"],
        },
    );

    // --- Semantic Warnings (W5xxx) ---

    map.insert(
        "W5001",
        CodeInfo {
            code: "W5001",
            name: "RefSeqMismatch",
            summary: "Reference sequence mismatch.",
            explanation: "The reference base(s) stated in the HGVS expression do not match \
                the actual reference sequence. In lenient/silent modes, normalization proceeds \
                using the actual reference sequence, but a warning is always emitted.",
            category: CodeCategory::Semantic,
            bad_examples: &["c.100G>A (when reference is T at position 100)"],
            good_examples: &["c.100T>A (matches actual reference)"],
            mode_behavior: Some(ModeBehavior::always_warn_if_not_rejected()),
            hgvs_spec_url: None,
            related_codes: &["E3002", "W3001"],
        },
    );

    map.insert(
        "W5002",
        CodeInfo {
            code: "W5002",
            name: "OverlapConflictingEdits",
            summary: "Two or more cis-allele edits share identical reference bounds.",
            explanation: "Two or more edits in a cis allele point at the same single \
                base or range with identical (start, end) bounds. The HGVS spec does \
                not define a canonical form for this case; ferro preserves the input \
                verbatim and emits this warning so callers can decide whether to flag \
                or reject. If the HGVS Variant Working Group rules these inputs invalid, \
                change the mode_behavior here to promote this code to an error in Strict.",
            category: CodeCategory::Semantic,
            bad_examples: &[
                "g.[100G>A;100A>C]",
                "g.[100del;100A>C]",
                "c.[100_103del;100_103inv]",
            ],
            good_examples: &["g.[100A>C;101A>C]", "c.[762_768del;767_774dup]"],
            mode_behavior: Some(ModeBehavior::always_warn_if_not_rejected()),
            hgvs_spec_url: Some("https://hgvs-nomenclature.org/stable/recommendations/general/"),
            related_codes: &["W5001"],
        },
    );

    map
}

/// Get information about a specific code.
/// Accepts both uppercase and lowercase codes (e.g., "W1001" or "w1001").
pub fn get_code_info(code: &str) -> Option<&'static CodeInfo> {
    let uppercase = code.to_uppercase();
    get_registry().get(uppercase.as_str())
}

/// List all codes.
pub fn list_all_codes() -> Vec<&'static CodeInfo> {
    let mut codes: Vec<_> = get_registry().values().collect();
    codes.sort_by_key(|c| c.code);
    codes
}

/// List all error codes (E-prefix).
pub fn list_error_codes() -> Vec<&'static CodeInfo> {
    let mut codes: Vec<_> = get_registry().values().filter(|c| c.is_error()).collect();
    codes.sort_by_key(|c| c.code);
    codes
}

/// List all warning codes (W-prefix).
pub fn list_warning_codes() -> Vec<&'static CodeInfo> {
    let mut codes: Vec<_> = get_registry().values().filter(|c| c.is_warning()).collect();
    codes.sort_by_key(|c| c.code);
    codes
}

/// List codes by category.
pub fn list_codes_by_category(category: CodeCategory) -> Vec<&'static CodeInfo> {
    let mut codes: Vec<_> = get_registry()
        .values()
        .filter(|c| c.category == category)
        .collect();
    codes.sort_by_key(|c| c.code);
    codes
}

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

    #[test]
    fn test_registry_has_codes() {
        assert!(!get_registry().is_empty());
    }

    #[test]
    fn test_get_code_info() {
        let e1001 = get_code_info("E1001");
        assert!(e1001.is_some());
        assert_eq!(e1001.unwrap().name, "InvalidAccession");

        let w1001 = get_code_info("W1001");
        assert!(w1001.is_some());
        assert_eq!(w1001.unwrap().name, "LowercaseAminoAcid");

        assert!(get_code_info("NOTEXIST").is_none());
    }

    #[test]
    fn test_list_error_codes() {
        let errors = list_error_codes();
        assert!(!errors.is_empty());
        for code in errors {
            assert!(code.code.starts_with('E'));
        }
    }

    #[test]
    fn test_list_warning_codes() {
        let warnings = list_warning_codes();
        assert!(!warnings.is_empty());
        for code in warnings {
            assert!(code.code.starts_with('W'));
        }
    }

    #[test]
    fn test_list_all_codes_sorted() {
        let codes = list_all_codes();
        let sorted: Vec<_> = codes.iter().map(|c| c.code).collect();
        let mut check = sorted.clone();
        check.sort();
        assert_eq!(sorted, check);
    }

    #[test]
    fn test_warning_codes_have_mode_behavior() {
        let warnings = list_warning_codes();
        for code in warnings {
            assert!(
                code.mode_behavior.is_some(),
                "Warning {} should have mode_behavior",
                code.code
            );
        }
    }

    #[test]
    fn test_error_codes_no_mode_behavior() {
        let errors = list_error_codes();
        for code in errors {
            assert!(
                code.mode_behavior.is_none(),
                "Error {} should not have mode_behavior",
                code.code
            );
        }
    }

    #[test]
    fn test_format_terminal() {
        let code = get_code_info("W1001").unwrap();
        let output = code.format_terminal(false);
        assert!(output.contains("W1001"));
        assert!(output.contains("LowercaseAminoAcid"));
        assert!(output.contains("ferro parse --ignore"));
    }

    #[test]
    fn test_format_json() {
        let code = get_code_info("E1001").unwrap();
        let json = code.format_json();
        assert!(json.contains("\"code\":\"E1001\""));
        assert!(json.contains("\"name\":\"InvalidAccession\""));
    }

    #[test]
    fn test_format_markdown() {
        let code = get_code_info("W1001").unwrap();
        let md = code.format_markdown();
        assert!(md.contains("## W1001:"));
        assert!(md.contains("| Mode | Action |"));
    }

    #[test]
    fn test_list_codes_by_category() {
        let parse_codes = list_codes_by_category(CodeCategory::Parse);
        assert!(!parse_codes.is_empty());
        for code in parse_codes {
            assert_eq!(code.category, CodeCategory::Parse);
        }
    }
}