ferro-hgvs 0.4.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
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
//! Comparison benchmarks between ferro-hgvs and mutalyzer.
//!
//! This module provides fair head-to-head comparisons by:
//! - Running mutalyzer locally (not via HTTP API)
//! - Sharding work across multiple Python workers
//! - Collecting detailed agreement statistics

/// Python script for mutalyzer parsing (embedded for parallel spawning)
const MUTALYZER_PARSE_SCRIPT: &str = r#"
import sys
import json
import time

try:
    from mutalyzer_hgvs_parser import to_model
except ImportError:
    print("ERROR: mutalyzer-hgvs-parser not installed", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

results = []
successful = 0
failed = 0

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

start = time.perf_counter()

for pattern in patterns:
    try:
        model = to_model(pattern)
        results.append({
            "input": pattern,
            "success": True,
            "output": str(model) if model else pattern
        })
        successful += 1
    except Exception as e:
        results.append({
            "input": pattern,
            "success": False,
            "error": str(e)[:200]
        })
        failed += 1

elapsed = time.perf_counter() - start

output = {
    "tool": "mutalyzer-hgvs-parser",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": failed,
    "elapsed_seconds": elapsed,
    "patterns_per_second": len(patterns) / elapsed if elapsed > 0 else 0,
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output, f, indent=2)
"#;

/// Python script for mutalyzer normalization (embedded for parallel spawning)
const MUTALYZER_NORMALIZE_SCRIPT: &str = r#"
import sys
import os
import gc

# Check if network is allowed (5th argument, default false)
allow_network = len(sys.argv) > 4 and sys.argv[4].lower() == 'true'

# Block network connections unless explicitly allowed
import socket
_original_connect = socket.socket.connect
if not allow_network:
    def _blocked_connect(self, address):
        raise RuntimeError(f"NETWORK ACCESS BLOCKED: Mutalyzer tried to connect to {address}. "
                           "Ensure all required sequences are in the local cache.")
    socket.socket.connect = _blocked_connect

# CRITICAL: Set MUTALYZER_SETTINGS env var BEFORE importing mutalyzer
# mutalyzer-retriever reads config from a file specified by MUTALYZER_SETTINGS
if len(sys.argv) > 3 and sys.argv[3]:
    settings_file = sys.argv[3]
    os.environ['MUTALYZER_SETTINGS'] = settings_file

import json
import time

try:
    from mutalyzer.description import Description
except ImportError:
    print("ERROR: mutalyzer not installed", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

# Extract worker ID from output filename for progress logging
worker_id = os.path.basename(output_file).replace('shard_', '').replace('_output.json', '')

from collections import defaultdict

results = []
successful = 0
failed = 0
error_counts = defaultdict(int)

def categorize_error(error_msg, pattern=None):
    """Categorize error message into a short category."""
    msg = str(error_msg)
    # Check for common mutalyzer error codes
    if 'ESEQUENCEMISMATCH' in msg:
        return 'ESEQUENCEMISMATCH'
    elif 'ERETR' in msg or 'retriev' in msg.lower():
        return 'RETRIEVAL_ERROR'
    elif 'EPARSE' in msg or 'parse' in msg.lower() or 'syntax' in msg.lower():
        return 'PARSE_ERROR'
    elif 'EREF' in msg:
        return 'EREF'
    elif 'ERANGE' in msg or 'range' in msg.lower() or 'position' in msg.lower():
        return 'RANGE_ERROR'
    elif 'ENOTSUPPORTED' in msg or 'not supported' in msg.lower():
        return 'NOT_SUPPORTED'
    elif 'NETWORK ACCESS BLOCKED' in msg:
        # LRG patterns require network for additional metadata
        if pattern and pattern.startswith('LRG_'):
            return 'LRG_NETWORK_REQUIRED'
        return 'CACHE_MISS'
    elif 'No model loaded' in msg or 'not found' in msg.lower():
        return 'NOT_FOUND'
    else:
        # Extract error code if present (e.g., EXYZ)
        import re
        match = re.search(r'\bE[A-Z]{2,}', msg)
        if match:
            return match.group(0)
        return 'OTHER'

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

total = len(patterns)
start = time.perf_counter()

# Progress logging interval
progress_interval = max(50, total // 20)  # Log ~20 times or every 50

for i, pattern in enumerate(patterns):
    # Progress logging
    if i > 0 and i % progress_interval == 0:
        elapsed_so_far = time.perf_counter() - start
        rate = i / elapsed_so_far if elapsed_so_far > 0 else 0
        eta = (total - i) / rate if rate > 0 else 0
        print(f"[worker {worker_id}] {i}/{total} ({100*i//total}%) - {rate:.1f} p/s - ETA {eta:.0f}s", file=sys.stderr, flush=True)
        # Garbage collect to free memory from cached sequences
        gc.collect()

    try:
        d = Description(description=pattern)
        d.normalize()
        if not d.errors:
            output = d.output()
            results.append({
                "input": pattern,
                "success": True,
                "output": output.get("normalized_description", pattern)
            })
            successful += 1
        else:
            error_msg = str(d.errors[0]) if d.errors else "Unknown error"
            category = categorize_error(error_msg, pattern)
            error_counts[category] += 1
            results.append({
                "input": pattern,
                "success": False,
                "error": error_msg[:200],
                "error_category": category
            })
            failed += 1
    except Exception as e:
        error_msg = str(e)
        category = categorize_error(error_msg, pattern)
        error_counts[category] += 1
        results.append({
            "input": pattern,
            "success": False,
            "error": error_msg[:200],
            "error_category": category
        })
        failed += 1

elapsed = time.perf_counter() - start
print(f"[worker {worker_id}] Done: {total} patterns in {elapsed:.1f}s ({total/elapsed:.1f} p/s)", file=sys.stderr, flush=True)

output_data = {
    "tool": "mutalyzer",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": failed,
    "elapsed_seconds": elapsed,
    "patterns_per_second": len(patterns) / elapsed if elapsed > 0 else 0,
    "error_counts": dict(error_counts),
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output_data, f, indent=2)
"#;

/// Python script for biocommons/hgvs normalization (embedded for parallel spawning)
///
/// biocommons/hgvs is the canonical Python HGVS implementation. It can use:
/// - Remote UTA database (default): no local setup needed, but slower for bulk
/// - Local UTA database: faster for large-scale comparisons
///
/// Requirements: pip install hgvs
const BIOCOMMONS_NORMALIZE_SCRIPT: &str = r#"
import sys
import os
import json
import time
from collections import defaultdict

# Optional: Set UTA database URL from environment or argument
# By default, biocommons uses remote UTA at uta.biocommons.org
if len(sys.argv) > 3 and sys.argv[3]:
    os.environ['UTA_DB_URL'] = sys.argv[3]

try:
    import hgvs.parser
    import hgvs.normalizer
    import hgvs.dataproviders.uta
except ImportError:
    print("ERROR: hgvs (biocommons) not installed. Run: pip install hgvs", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

# Initialize biocommons/hgvs components
try:
    hdp = hgvs.dataproviders.uta.connect()
    hp = hgvs.parser.Parser()
    hn = hgvs.normalizer.Normalizer(hdp)
except Exception as e:
    print(f"ERROR: Failed to initialize biocommons/hgvs: {e}", file=sys.stderr)
    sys.exit(1)

results = []
successful = 0
failed = 0
error_counts = defaultdict(int)

def categorize_error(error_msg, pattern=None):
    """Categorize error message into a short category."""
    msg = str(error_msg)
    if 'HGVSParseError' in msg or 'parse' in msg.lower():
        return 'PARSE_ERROR'
    elif 'HGVSInvalidVariantError' in msg:
        return 'INVALID_VARIANT'
    elif 'HGVSDataNotAvailableError' in msg or 'not found' in msg.lower():
        return 'DATA_NOT_AVAILABLE'
    elif 'HGVSInvalidIntervalError' in msg:
        return 'INVALID_INTERVAL'
    elif 'HGVSNormalizationError' in msg:
        return 'NORMALIZATION_ERROR'
    elif 'HGVSUnsupportedOperationError' in msg:
        return 'UNSUPPORTED'
    elif 'connection' in msg.lower() or 'timeout' in msg.lower():
        return 'CONNECTION_ERROR'
    else:
        return 'OTHER'

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

start = time.perf_counter()

for pattern in patterns:
    try:
        # Parse the HGVS string
        var = hp.parse_hgvs_variant(pattern)
        # Normalize the variant
        normalized = hn.normalize(var)
        results.append({
            "input": pattern,
            "success": True,
            "output": str(normalized)
        })
        successful += 1
    except Exception as e:
        error_msg = str(e)
        category = categorize_error(error_msg, pattern)
        error_counts[category] += 1
        results.append({
            "input": pattern,
            "success": False,
            "error": error_msg[:200],
            "error_category": category
        })
        failed += 1

elapsed = time.perf_counter() - start

output_data = {
    "tool": "biocommons-hgvs",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": failed,
    "elapsed_seconds": elapsed,
    "patterns_per_second": len(patterns) / elapsed if elapsed > 0 else 0,
    "error_counts": dict(error_counts),
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output_data, f, indent=2)
"#;

use crate::benchmark::biocommons::has_biocommons_normalizer;
use crate::benchmark::cache::extract_accessions_from_pattern;
use crate::benchmark::mutalyzer::{has_mutalyzer_normalizer, has_mutalyzer_parser};
use crate::benchmark::sample::stratified_sample_vec;
use crate::benchmark::types::{
    AgreementStats, CacheStats, CombinedPatternResult, CompareMode, ComparisonResult,
    ComparisonToolResult, DetailedResults, DisagreementExample, ParseResult, RefMismatchInfo,
    ReferenceStats, Validator,
};
use crate::error_handling::{ErrorConfig, ErrorMode, InputPreprocessor};
use crate::hgvs::parser::parse_hgvs_lenient;
use crate::normalize::NormalizeConfig;
use crate::reference::ReferenceProvider;
use crate::{FerroError, MockProvider, MultiFastaProvider, Normalizer};
use chrono::{DateTime, Utc};
use rand::seq::SliceRandom;
use rand::SeedableRng;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::time::Instant;

/// Configuration for running a comparison.
#[derive(Debug, Clone)]
pub struct CompareConfig {
    /// External validator to compare against (mutalyzer, biocommons, hgvs-rs)
    pub validator: Validator,
    /// Number of patterns to sample (0 = all patterns)
    pub sample_size: usize,
    /// Number of parallel workers for external validator
    pub workers: usize,
    /// Random seed for sampling
    pub seed: u64,
    /// Mutalyzer settings file path (optional, for mutalyzer validator)
    pub mutalyzer_settings: Option<String>,
    /// UTA database URL (optional, for biocommons validator)
    /// Default: remote uta.biocommons.org
    pub uta_db_url: Option<String>,
    /// Biocommons settings file path (optional, for local biocommons setup)
    ///
    /// When provided, uses local UTA and SeqRepo configuration from this file.
    /// Generate with: `ferro-benchmark generate-biocommons-settings`
    pub biocommons_settings: Option<std::path::PathBuf>,
    /// SeqRepo path for hgvs-rs validator (optional)
    ///
    /// Required when using --validator=hgvs-rs. Path to the SeqRepo directory
    /// (e.g., /usr/local/share/seqrepo/2021-01-29).
    pub hgvs_rs_seqrepo_path: Option<String>,
    /// UTA database schema for hgvs-rs validator (optional)
    ///
    /// Used with --validator=hgvs-rs. Defaults to "uta_20210129".
    pub hgvs_rs_uta_schema: Option<String>,
    /// Reference data directory for ferro-hgvs normalization
    pub reference_dir: Option<std::path::PathBuf>,
    /// Transcript→chromosome mapping file for rewriting intronic variants
    pub transcript_mapping: Option<std::path::PathBuf>,
    /// Include unnormalizable patterns (uncertain breakpoints, reference-only)
    ///
    /// By default, patterns that cannot be meaningfully normalized are filtered out:
    /// - Uncertain breakpoints: patterns containing `?`, `(`, or `)` in positions
    ///   (e.g., `c.?_100del`, `c.(100_200)del`) - normalization is undefined
    /// - Reference-only: patterns ending in `=` (e.g., `c.100=`) - trivially equal
    ///
    /// Set to true to include all patterns regardless of normalizability.
    pub include_unnormalizable: bool,
    /// Include genomic (NC_) patterns in comparison
    ///
    /// By default, genomic/chromosomal patterns (NC_*) are excluded because
    /// mutalyzer loads entire chromosome sequences (~60MB each), making
    /// normalization very slow (~1 pattern/second vs ~20 for transcripts).
    ///
    /// Set to true to include genomic patterns (warning: significantly slower).
    pub include_genomic: bool,
    /// Include protein (NP_) patterns in comparison
    ///
    /// By default, protein patterns (NP_*, p.) are excluded because
    /// mutalyzer's local file cache doesn't properly support protein
    /// coordinate systems, causing ECOORDINATESYSTEMMISMATCH errors.
    ///
    /// Set to true to include protein patterns (requires network access).
    pub include_protein: bool,
    /// Allow mutalyzer to make network requests
    ///
    /// By default, network access is blocked to ensure fair offline comparison.
    /// Some patterns (especially LRG) require additional network requests for
    /// metadata that isn't covered by the file cache.
    ///
    /// Set to true to allow network access (warning: slower and requires internet).
    pub allow_mutalyzer_network: bool,
    /// Output path for detailed per-pattern results (optional)
    ///
    /// When provided, saves all per-pattern results (both ferro and mutalyzer)
    /// to a separate JSON file for detailed analysis.
    pub detailed_output: Option<std::path::PathBuf>,
    /// Pre-computed mutalyzer results file (JSONL from mutalyzer-benchmark)
    ///
    /// When provided, uses pre-computed mutalyzer normalization results instead
    /// of running mutalyzer live.
    pub existing_mutalyzer_results: Option<std::path::PathBuf>,
    /// Error handling mode for ferro parsing/normalization
    ///
    /// Controls how ferro handles common input errors:
    /// - "strict" (default): Reject all non-standard input
    /// - "lenient": Auto-correct with warnings
    /// - "silent": Auto-correct silently
    pub error_mode: String,
}

impl Default for CompareConfig {
    fn default() -> Self {
        Self {
            validator: Validator::default(),
            sample_size: 1000,
            workers: num_cpus::get(),
            seed: 42,
            mutalyzer_settings: None,
            uta_db_url: None,
            biocommons_settings: None,
            hgvs_rs_seqrepo_path: None,
            hgvs_rs_uta_schema: None,
            reference_dir: None,
            transcript_mapping: None,
            include_unnormalizable: false,
            include_genomic: false,
            include_protein: false,
            allow_mutalyzer_network: false,
            detailed_output: None,
            existing_mutalyzer_results: None,
            error_mode: "strict".to_string(),
        }
    }
}

/// Check if a variant pattern is intronic (has offset like c.123-45 or c.123+45)
fn is_intronic_variant(pattern: &str) -> bool {
    // Intronic variants have patterns like:
    // - c.123-45A>G (intronic position before exon)
    // - c.123+45A>G (intronic position after exon)
    // - c.*123-45A>G (3'UTR intronic)
    // - c.*123+45A>G (3'UTR intronic)
    // - n.123-45A>G (non-coding intronic)
    // - n.123+45A>G (non-coding intronic)

    // Must have a colon (reference:variant)
    let Some(colon_pos) = pattern.find(':') else {
        return false;
    };

    let variant_part = &pattern[colon_pos + 1..];

    // Must start with c. or n. coordinate system
    if !variant_part.starts_with("c.") && !variant_part.starts_with("n.") {
        return false;
    }

    // Look for intronic offset pattern: digit followed by + or - and more digits
    // This regex-like pattern matches: \d[+-]\d
    let chars: Vec<char> = variant_part.chars().collect();
    for i in 0..chars.len().saturating_sub(2) {
        if chars[i].is_ascii_digit()
            && (chars[i + 1] == '+' || chars[i + 1] == '-')
            && chars[i + 2].is_ascii_digit()
        {
            return true;
        }
    }

    false
}

/// Check if a pattern is a genomic (NC_) variant.
///
/// Genomic patterns use chromosome coordinates and require loading entire
/// chromosome sequences (~60MB each), making them very slow to normalize.
fn is_genomic_pattern(pattern: &str) -> bool {
    pattern.starts_with("NC_")
}

/// Check if a variant pattern is a protein variant.
///
/// Returns true for patterns like `NP_000146.2:p.Pro324Leu`.
/// These are excluded by default because mutalyzer's local file cache
/// doesn't properly support protein coordinate systems.
fn is_protein_pattern(pattern: &str) -> bool {
    pattern.starts_with("NP_") || pattern.contains(":p.")
}

/// Check if a variant pattern is normalizable.
///
/// Returns false for patterns that cannot be meaningfully normalized:
/// - Uncertain breakpoints: patterns containing `?`, `(`, or `)` in the variant part
///   (e.g., `c.?_100del`, `c.(100_200)del`, `c.100_?del`)
/// - Reference-only: patterns ending in `=` (e.g., `c.100=`)
///
/// These patterns either have undefined normalization semantics or trivially
/// normalize to themselves.
fn is_normalizable(pattern: &str) -> bool {
    // Get the variant part (after the colon)
    let variant_part = if let Some(colon_pos) = pattern.find(':') {
        &pattern[colon_pos + 1..]
    } else {
        pattern
    };

    // Check for uncertain breakpoints (?, parentheses in positions)
    // These indicate uncertain/approximate positions where normalization is undefined
    if variant_part.contains('?') || variant_part.contains('(') || variant_part.contains(')') {
        return false;
    }

    // Check for reference-only patterns (ending in =)
    // These trivially normalize to themselves
    if variant_part.ends_with('=') {
        return false;
    }

    true
}

/// Rewrite intronic variants with genomic context for Mutalyzer
///
/// Transforms patterns like:
///   NM_001318856.2:c.9-1296T>C
/// Into:
///   NC_000006.12(NM_001318856.2):c.9-1296T>C
///
/// This allows Mutalyzer to properly normalize intronic variants by providing
/// the genomic reference context.
pub fn rewrite_with_genomic_context(
    patterns: &[String],
    mapping: &HashMap<String, String>,
) -> Vec<String> {
    patterns
        .iter()
        .map(|pattern| {
            // Only rewrite intronic variants on transcript references
            if !is_intronic_variant(pattern) {
                return pattern.clone();
            }

            // Extract the accession (before the colon)
            let Some(colon_pos) = pattern.find(':') else {
                return pattern.clone();
            };

            let accession = &pattern[..colon_pos];

            // Only rewrite NM_ and NR_ accessions
            if !accession.starts_with("NM_") && !accession.starts_with("NR_") {
                return pattern.clone();
            }

            // Look up the chromosome mapping
            if let Some(chromosome) = mapping.get(accession) {
                // Rewrite as NC_xxx(NM_xxx):c.variant
                let variant_part = &pattern[colon_pos..];
                format!("{}({}){}", chromosome, accession, variant_part)
            } else {
                pattern.clone()
            }
        })
        .collect()
}

/// Extract accession from an HGVS pattern (the reference before the colon).
fn extract_accession(pattern: &str) -> Option<&str> {
    // Handle nested format like NC_000001.11(NM_000001.2):c.100A>G
    if let Some(paren_start) = pattern.find('(') {
        if let Some(paren_end) = pattern.find(')') {
            // Return the inner accession (the transcript)
            return Some(&pattern[paren_start + 1..paren_end]);
        }
    }

    // Simple format: NM_000001.2:c.100A>G
    pattern.find(':').map(|pos| &pattern[..pos])
}

/// Build a set of accessions from all .fai index files in a directory tree.
fn load_fai_accessions(cache_dir: &Path) -> HashSet<String> {
    use std::io::BufRead;

    let mut accessions = HashSet::new();

    // Look for .fai files in transcripts subdirectory
    let transcripts_dir = cache_dir.join("transcripts");
    if transcripts_dir.exists() {
        if let Ok(entries) = std::fs::read_dir(&transcripts_dir) {
            for entry in entries.filter_map(|e| e.ok()) {
                let path = entry.path();
                if path.extension().is_some_and(|ext| ext == "fai") {
                    if let Ok(file) = File::open(&path) {
                        let reader = std::io::BufReader::new(file);
                        for line in reader.lines().map_while(Result::ok) {
                            // FAI format: name\tlength\toffset\tlinebases\tlinewidth
                            if let Some(name) = line.split('\t').next() {
                                accessions.insert(name.to_string());
                            }
                        }
                    }
                }
            }
        }
    }

    // Also check genome directory for NC_ accessions
    let genome_dir = cache_dir.join("genome");
    if genome_dir.exists() {
        if let Ok(entries) = std::fs::read_dir(&genome_dir) {
            for entry in entries.filter_map(|e| e.ok()) {
                let path = entry.path();
                if path.extension().is_some_and(|ext| ext == "fai") {
                    if let Ok(file) = File::open(&path) {
                        let reader = std::io::BufReader::new(file);
                        for line in reader.lines().map_while(Result::ok) {
                            if let Some(name) = line.split('\t').next() {
                                accessions.insert(name.to_string());
                            }
                        }
                    }
                }
            }
        }
    }

    accessions
}

/// Check cache coverage for a set of patterns.
///
/// Returns statistics about how many accessions are missing from each cache.
/// For ferro, only checks accessions from patterns that actually failed (since ferro
/// doesn't need sequence data for all normalizations - e.g., substitutions don't shift).
/// For mutalyzer, checks all patterns since it always needs cached data.
pub fn check_cache_coverage(
    patterns: &[String],
    ferro_results: &[ParseResult],
    ferro_cache_dir: Option<&Path>,
    mutalyzer_cache_dir: Option<&Path>,
) -> CacheStats {
    // Extract unique accessions from all patterns (for total count and mutalyzer check)
    let all_accessions: HashSet<&str> = patterns
        .iter()
        .filter_map(|p| extract_accession(p))
        .collect();

    // Extract accessions only from ferro failures (these are the ones that actually matter)
    let ferro_failed_accessions: HashSet<&str> = ferro_results
        .iter()
        .filter(|r| !r.success)
        .filter_map(|r| extract_accession(&r.input))
        .collect();

    let total = all_accessions.len();
    let mut ferro_missing = Vec::new();
    let mut muta_missing = Vec::new();

    // Load ferro's indexed FASTA accessions once
    let ferro_accessions = ferro_cache_dir.map(load_fai_accessions);

    // Check ferro cache only for accessions that caused failures
    let mut sorted_ferro_failed: Vec<_> = ferro_failed_accessions.into_iter().collect();
    sorted_ferro_failed.sort();
    for acc in &sorted_ferro_failed {
        if let Some(ref ferro_set) = ferro_accessions {
            if !ferro_set.contains(*acc) {
                ferro_missing.push((*acc).to_string());
            }
        }
    }

    // Check mutalyzer cache for all accessions
    let mut sorted_all: Vec<_> = all_accessions.into_iter().collect();
    sorted_all.sort();
    for acc in &sorted_all {
        if let Some(cache_dir) = mutalyzer_cache_dir {
            // For LRG transcript/protein accessions (LRG_NNNpN, LRG_NNNtN),
            // check if the base LRG exists since they're derived from it
            let check_acc = if acc.starts_with("LRG_") {
                // Extract base LRG: LRG_392p1 -> LRG_392, LRG_392t2 -> LRG_392
                let base: String = acc
                    .chars()
                    .take_while(|c| {
                        c.is_ascii_digit() || *c == '_' || *c == 'L' || *c == 'R' || *c == 'G'
                    })
                    .collect();
                if base.len() < acc.len() {
                    base
                } else {
                    (*acc).to_string()
                }
            } else {
                (*acc).to_string()
            };

            let seq_path = cache_dir.join(format!("{}.sequence", check_acc));
            let ann_path = cache_dir.join(format!("{}.annotations", check_acc));
            if !seq_path.exists() || !ann_path.exists() {
                muta_missing.push((*acc).to_string());
            }
        }
    }

    // Sort for deterministic output
    ferro_missing.sort();
    muta_missing.sort();

    // Save all missing accessions to a file for easy population
    if !muta_missing.is_empty() {
        if let Ok(mut file) = File::create("missing_accessions.txt") {
            for acc in &muta_missing {
                let _ = writeln!(file, "{}", acc);
            }
            eprintln!(
                "  Saved {} missing accessions to missing_accessions.txt",
                muta_missing.len()
            );
        }
    }

    CacheStats {
        total_accessions: total,
        ferro_missing: ferro_missing.len(),
        mutalyzer_missing: muta_missing.len(),
        ferro_missing_examples: ferro_missing.into_iter().take(10).collect(),
        mutalyzer_missing_examples: muta_missing.into_iter().take(10).collect(),
    }
}

/// Result from a mutalyzer worker subprocess.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct MutalyzerOutput {
    #[allow(dead_code)]
    tool: String,
    total_patterns: usize,
    successful: usize,
    #[allow(dead_code)]
    failed: usize,
    elapsed_seconds: f64,
    #[allow(dead_code)]
    patterns_per_second: f64,
    results: Vec<ParseResult>,
    #[serde(default)]
    error_counts: HashMap<String, usize>,
}

/// Run a normalization comparison between ferro-hgvs and mutalyzer.
pub fn compare_normalize<P: AsRef<Path>>(
    input: P,
    output: P,
    config: &CompareConfig,
) -> Result<ComparisonResult, FerroError> {
    let input = input.as_ref();
    let output = output.as_ref();

    // Check validator availability based on config
    match config.validator {
        Validator::Mutalyzer => {
            if !has_mutalyzer_normalizer() {
                return Err(FerroError::Io {
                    msg: "mutalyzer package not installed. Install with: pip install mutalyzer"
                        .to_string(),
                });
            }
        }
        Validator::Biocommons => {
            if !has_biocommons_normalizer() {
                return Err(FerroError::Io {
                    msg: "biocommons/hgvs package not installed. Install with: pip install hgvs"
                        .to_string(),
                });
            }
        }
        Validator::HgvsRs => {
            #[cfg(not(feature = "hgvs-rs"))]
            {
                return Err(FerroError::Io {
                    msg:
                        "hgvs-rs feature not enabled. Rebuild with: cargo build --features hgvs-rs"
                            .to_string(),
                });
            }
            #[cfg(feature = "hgvs-rs")]
            {
                // Verify hgvs-rs configuration is available
                if config.hgvs_rs_seqrepo_path.is_none() {
                    return Err(FerroError::Io {
                        msg: "hgvs-rs validator requires --hgvs-rs-seqrepo-path".to_string(),
                    });
                }
            }
        }
    }

    eprintln!("Using validator: {}", config.validator);

    // Load transcript→chromosome mapping if provided (for rewriting intronic variants)
    let transcript_mapping: Option<HashMap<String, String>> =
        if let Some(mapping_path) = &config.transcript_mapping {
            eprintln!(
                "Loading transcript mapping from {}...",
                mapping_path.display()
            );
            let mapping = crate::benchmark::cache::load_transcript_mapping(mapping_path)?;
            eprintln!("  Loaded {} transcript→chromosome mappings", mapping.len());
            Some(mapping)
        } else {
            None
        };

    // Load patterns
    let all_patterns = load_patterns(input)?;
    if all_patterns.is_empty() {
        return Err(FerroError::Io {
            msg: "No patterns found in input file".to_string(),
        });
    }

    eprintln!(
        "Loaded {} patterns from {}",
        all_patterns.len(),
        input.display()
    );

    // Filter patterns based on config settings
    let patterns = {
        let mut filtered = all_patterns;
        let original_count = filtered.len();

        // Filter unnormalizable patterns unless include_unnormalizable is set
        if !config.include_unnormalizable {
            let before = filtered.len();
            filtered.retain(|p| is_normalizable(p));
            let removed = before - filtered.len();
            if removed > 0 {
                eprintln!(
                    "  Excluded {} unnormalizable patterns (uncertain breakpoints, reference-only)",
                    removed
                );
            }
        }

        // Filter genomic (NC_) patterns unless include_genomic is set
        if !config.include_genomic {
            let before = filtered.len();
            filtered.retain(|p| !is_genomic_pattern(p));
            let removed = before - filtered.len();
            if removed > 0 {
                eprintln!(
                    "  Excluded {} genomic (NC_) patterns (use --include-genomic to include)",
                    removed
                );
            }
        }

        // Filter protein (NP_) patterns unless include_protein is set
        if !config.include_protein {
            let before = filtered.len();
            filtered.retain(|p| !is_protein_pattern(p));
            let removed = before - filtered.len();
            if removed > 0 {
                eprintln!(
                    "  Excluded {} protein (NP_/p.) patterns (use --include-protein to include)",
                    removed
                );
            }
        }

        if filtered.len() < original_count {
            eprintln!("  Filtered to {} patterns", filtered.len());
        }

        filtered
    };

    if patterns.is_empty() {
        return Err(FerroError::Io {
            msg: "No normalizable patterns found after filtering".to_string(),
        });
    }

    // Sample patterns if needed
    let sampled = if config.sample_size > 0 && config.sample_size < patterns.len() {
        sample_patterns(&patterns, config.sample_size, config.seed)
    } else {
        patterns
    };

    eprintln!("Using {} patterns for comparison", sampled.len());

    // Run ferro-hgvs (uses original patterns)
    eprintln!("\n[1/2] Running ferro-hgvs normalize...");
    let (ferro_results, ferro_elapsed) =
        run_ferro_normalize(&sampled, &config.reference_dir, &config.error_mode)?;
    let ferro_successful = ferro_results.iter().filter(|r| r.success).count();
    eprintln!(
        "      {} patterns in {:.2}s ({:.0} p/s)",
        sampled.len(),
        ferro_elapsed.as_secs_f64(),
        sampled.len() as f64 / ferro_elapsed.as_secs_f64()
    );

    // Either load pre-computed mutalyzer results or run mutalyzer live
    let (muta_results, muta_elapsed, muta_error_counts) = if let Some(ref existing_path) =
        config.existing_mutalyzer_results
    {
        // Load pre-computed results
        let start = Instant::now();
        let existing_results = load_existing_mutalyzer_results(existing_path)?;

        // Look up results for our sampled patterns
        eprintln!("\n[2/2] Using pre-computed mutalyzer results...");
        let mut results = Vec::with_capacity(sampled.len());
        let mut found = 0;
        let mut not_found = 0;

        for pattern in &sampled {
            if let Some(result) = existing_results.get(pattern) {
                results.push(result.clone());
                found += 1;
            } else {
                // Pattern not in pre-computed results - mark as failed
                results.push(ParseResult {
                    input: pattern.clone(),
                    success: false,
                    output: None,
                    error: Some("Not in pre-computed results".to_string()),
                    error_category: Some("NOT_IN_CACHE".to_string()),
                    ref_mismatch: None,
                    details: None,
                });
                not_found += 1;
            }
        }

        let elapsed = start.elapsed();
        let successful = results.iter().filter(|r| r.success).count();
        eprintln!(
            "      {} patterns in {:.2}s ({} found, {} not in cache)",
            sampled.len(),
            elapsed.as_secs_f64(),
            found,
            not_found
        );
        eprintln!(
            "      {} successful, {} failed",
            successful,
            sampled.len() - successful
        );

        // Aggregate error counts from loaded results
        let mut error_counts: HashMap<String, usize> = HashMap::new();
        for result in &results {
            if let Some(ref category) = result.error_category {
                *error_counts.entry(category.clone()).or_insert(0) += 1;
            }
        }

        (results, elapsed, error_counts)
    } else {
        // Run external validator live based on config.validator
        match config.validator {
            Validator::Mutalyzer => {
                // Rewrite intronic variants for mutalyzer if mapping provided
                let mutalyzer_patterns = if let Some(ref mapping) = transcript_mapping {
                    let rewritten = rewrite_with_genomic_context(&sampled, mapping);
                    let intronic_count = sampled.iter().filter(|p| is_intronic_variant(p)).count();
                    let rewritten_count = sampled
                        .iter()
                        .zip(rewritten.iter())
                        .filter(|(orig, new)| orig != new)
                        .count();
                    eprintln!(
                        "  Rewrote {}/{} intronic variants with genomic context",
                        rewritten_count, intronic_count
                    );
                    rewritten
                } else {
                    sampled.clone()
                };

                eprintln!(
                    "\n[2/2] Running mutalyzer normalize ({} workers)...",
                    config.workers
                );
                let (results, elapsed, error_counts) = run_mutalyzer_normalize_parallel(
                    &mutalyzer_patterns,
                    config.workers,
                    config.mutalyzer_settings.as_deref(),
                    config.allow_mutalyzer_network,
                )?;
                eprintln!(
                    "      {} patterns in {:.2}s ({:.0} p/s)",
                    sampled.len(),
                    elapsed.as_secs_f64(),
                    sampled.len() as f64 / elapsed.as_secs_f64()
                );
                (results, elapsed, error_counts)
            }
            Validator::Biocommons => {
                eprintln!(
                    "\n[2/2] Running biocommons/hgvs normalize ({} workers)...",
                    config.workers
                );
                let (results, elapsed, error_counts) = run_biocommons_normalize_parallel(
                    &sampled,
                    config.workers,
                    config.uta_db_url.as_deref(),
                )?;
                eprintln!(
                    "      {} patterns in {:.2}s ({:.0} p/s)",
                    sampled.len(),
                    elapsed.as_secs_f64(),
                    sampled.len() as f64 / elapsed.as_secs_f64()
                );
                (results, elapsed, error_counts)
            }
            Validator::HgvsRs => {
                #[cfg(not(feature = "hgvs-rs"))]
                {
                    return Err(FerroError::Io {
                        msg: "hgvs-rs feature not enabled".to_string(),
                    });
                }
                #[cfg(feature = "hgvs-rs")]
                {
                    let workers = config.workers;
                    if workers > 1 {
                        eprintln!("\n[2/2] Running hgvs-rs normalize ({} workers)...", workers);
                    } else {
                        eprintln!("\n[2/2] Running hgvs-rs normalize...");
                    }
                    let hgvs_rs_config = crate::benchmark::HgvsRsConfig {
                        uta_db_url: config.uta_db_url.clone().unwrap_or_else(|| {
                            "postgresql://anonymous:anonymous@localhost:5432/uta".to_string()
                        }),
                        uta_db_schema: config
                            .hgvs_rs_uta_schema
                            .clone()
                            .unwrap_or_else(|| "uta_20210129b".to_string()),
                        seqrepo_path: config.hgvs_rs_seqrepo_path.clone().unwrap_or_default(),
                        lrg_mapping_file: None, // LRG mapping not supported in compare workflow yet
                        in_memory: false,
                    };
                    let (results, elapsed, error_counts) =
                        crate::benchmark::run_hgvs_rs_normalize_parallel(
                            &sampled,
                            &hgvs_rs_config,
                            workers,
                        )?;
                    eprintln!(
                        "      {} patterns in {:.2}s ({:.0} p/s)",
                        sampled.len(),
                        elapsed.as_secs_f64(),
                        sampled.len() as f64 / elapsed.as_secs_f64()
                    );
                    (results, elapsed, error_counts)
                }
            }
        }
    };

    let muta_successful = muta_results.iter().filter(|r| r.success).count();

    // Save patterns that caused CACHE_MISS errors for investigation
    let cache_miss_patterns: Vec<_> = muta_results
        .iter()
        .filter(|r| r.error_category.as_deref() == Some("CACHE_MISS"))
        .map(|r| r.input.clone())
        .collect();
    if !cache_miss_patterns.is_empty() {
        if let Ok(mut file) = File::create("cache_miss_patterns.txt") {
            for pattern in &cache_miss_patterns {
                let _ = writeln!(file, "{}", pattern);
            }
            eprintln!(
                "  Saved {} CACHE_MISS patterns to cache_miss_patterns.txt",
                cache_miss_patterns.len()
            );
        }
    }

    // Compute agreement
    let (agreement, differences) = compute_agreement(&ferro_results, &muta_results);

    // Compute reference mismatch statistics
    let reference_stats = Some(compute_reference_stats(
        &ferro_results,
        &muta_results,
        &agreement,
    ));

    // Check cache coverage
    let mutalyzer_cache_dir = config
        .mutalyzer_settings
        .as_ref()
        .and_then(|settings_path| {
            // The cache directory is typically the parent of the settings file
            Path::new(settings_path).parent().map(|p| p.to_path_buf())
        });
    let cache_stats = Some(check_cache_coverage(
        &sampled,
        &ferro_results,
        config.reference_dir.as_deref(),
        mutalyzer_cache_dir.as_deref(),
    ));

    // Build result
    let ferro_tool = ComparisonToolResult::new(sampled.len(), ferro_successful, ferro_elapsed);
    let muta_tool = ComparisonToolResult::new(sampled.len(), muta_successful, muta_elapsed)
        .with_error_counts(muta_error_counts);
    let speedup = if ferro_tool.elapsed_seconds > 0.0 {
        muta_tool.elapsed_seconds / ferro_tool.elapsed_seconds
    } else {
        0.0
    };

    let result = ComparisonResult {
        mode: CompareMode::Normalize,
        timestamp: Utc::now(),
        sample_size: sampled.len(),
        ferro: ferro_tool,
        mutalyzer: muta_tool,
        speedup,
        agreement,
        differences,
        reference_stats,
        cache_stats,
    };

    // Print summary
    print_comparison_summary(&result);

    // Save result
    save_comparison_result(&result, output)?;

    // Save detailed per-pattern results if requested
    if let Some(ref detailed_path) = config.detailed_output {
        save_detailed_results(
            &ferro_results,
            &muta_results,
            CompareMode::Normalize,
            result.timestamp,
            detailed_path,
        )?;
    }

    Ok(result)
}

/// Run a parsing comparison between ferro-hgvs and mutalyzer.
pub fn compare_parse<P: AsRef<Path>>(
    input: P,
    output: P,
    config: &CompareConfig,
) -> Result<ComparisonResult, FerroError> {
    let input = input.as_ref();
    let output = output.as_ref();

    // Check mutalyzer availability
    if !has_mutalyzer_parser() {
        return Err(FerroError::Io {
            msg: "mutalyzer-hgvs-parser not installed. Install with: pip install mutalyzer-hgvs-parser".to_string(),
        });
    }

    // Load patterns
    let patterns = load_patterns(input)?;
    if patterns.is_empty() {
        return Err(FerroError::Io {
            msg: "No patterns found in input file".to_string(),
        });
    }

    eprintln!(
        "Loaded {} patterns from {}",
        patterns.len(),
        input.display()
    );

    // Sample patterns if needed
    let sampled = if config.sample_size > 0 && config.sample_size < patterns.len() {
        sample_patterns(&patterns, config.sample_size, config.seed)
    } else {
        patterns
    };

    eprintln!("Using {} patterns for comparison", sampled.len());

    // Run ferro-hgvs parsing
    eprintln!("\n[1/2] Running ferro-hgvs parse...");
    let (ferro_results, ferro_elapsed) = run_ferro_parse(&sampled, &config.error_mode);
    let ferro_successful = ferro_results.iter().filter(|r| r.success).count();
    eprintln!(
        "      {} patterns in {:.2}s ({:.0} p/s)",
        sampled.len(),
        ferro_elapsed.as_secs_f64(),
        sampled.len() as f64 / ferro_elapsed.as_secs_f64()
    );

    // Run mutalyzer parsing in parallel
    eprintln!(
        "\n[2/2] Running mutalyzer parse ({} workers)...",
        config.workers
    );
    let (muta_results, muta_elapsed) = run_mutalyzer_parse_parallel(&sampled, config.workers)?;
    let muta_successful = muta_results.iter().filter(|r| r.success).count();
    eprintln!(
        "      {} patterns in {:.2}s ({:.0} p/s)",
        sampled.len(),
        muta_elapsed.as_secs_f64(),
        sampled.len() as f64 / muta_elapsed.as_secs_f64()
    );

    // Compute agreement
    let (agreement, differences) = compute_agreement(&ferro_results, &muta_results);

    // Build result
    let ferro_tool = ComparisonToolResult::new(sampled.len(), ferro_successful, ferro_elapsed);
    let muta_tool = ComparisonToolResult::new(sampled.len(), muta_successful, muta_elapsed);
    let speedup = if ferro_tool.elapsed_seconds > 0.0 {
        muta_tool.elapsed_seconds / ferro_tool.elapsed_seconds
    } else {
        0.0
    };

    let result = ComparisonResult {
        mode: CompareMode::Parse,
        timestamp: Utc::now(),
        sample_size: sampled.len(),
        ferro: ferro_tool,
        mutalyzer: muta_tool,
        speedup,
        agreement,
        differences,
        reference_stats: None, // Not applicable for parse mode
        cache_stats: None,     // Not applicable for parse mode
    };

    // Print summary
    print_comparison_summary(&result);

    // Save result
    save_comparison_result(&result, output)?;

    // Save detailed per-pattern results if requested
    if let Some(ref detailed_path) = config.detailed_output {
        save_detailed_results(
            &ferro_results,
            &muta_results,
            CompareMode::Parse,
            result.timestamp,
            detailed_path,
        )?;
    }

    Ok(result)
}

/// Load pre-computed mutalyzer results from a JSONL file.
///
/// Returns a HashMap from input pattern to ParseResult for fast lookup.
fn load_existing_mutalyzer_results<P: AsRef<Path>>(
    path: P,
) -> Result<HashMap<String, ParseResult>, FerroError> {
    let path = path.as_ref();
    eprintln!(
        "Loading pre-computed mutalyzer results from {}...",
        path.display()
    );

    let file = File::open(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to open {}: {}", path.display(), e),
    })?;

    let reader: Box<dyn BufRead> = if path.extension().is_some_and(|e| e == "gz") {
        Box::new(BufReader::new(flate2::read::GzDecoder::new(file)))
    } else {
        Box::new(BufReader::new(file))
    };

    let mut results = HashMap::new();
    let mut line_count = 0;
    let mut success_count = 0;

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

        if line.is_empty() {
            continue;
        }

        let result: ParseResult = serde_json::from_str(&line).map_err(|e| FerroError::Json {
            msg: format!("Failed to parse JSONL line: {}", e),
        })?;

        if result.success {
            success_count += 1;
        }
        results.insert(result.input.clone(), result);
        line_count += 1;

        if line_count % 1_000_000 == 0 {
            eprintln!("  Loaded {} results...", line_count);
        }
    }

    eprintln!(
        "  Loaded {} results ({} successful, {} failed)",
        line_count,
        success_count,
        line_count - success_count
    );

    Ok(results)
}

/// Load patterns from a file (auto-detect format).
fn load_patterns<P: AsRef<Path>>(path: P) -> Result<Vec<String>, FerroError> {
    let path = path.as_ref();
    let file = File::open(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to open {}: {}", path.display(), e),
    })?;

    let reader: Box<dyn BufRead> = if path.extension().is_some_and(|e| e == "gz") {
        Box::new(BufReader::new(flate2::read::GzDecoder::new(file)))
    } else {
        Box::new(BufReader::new(file))
    };

    let mut patterns = Vec::new();

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

        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Check if this looks like ClinVar TSV (has tabs)
        if line.contains('\t') {
            // ClinVar format (hgvs4variation.txt):
            // 0:Symbol, 1:GeneID, 2:VariationID, 3:AlleleID, 4:Type, 5:Assembly,
            // 6:NucleotideExpression, 7:NucleotideChange, 8:ProteinExpression, ...
            let fields: Vec<&str> = line.split('\t').collect();
            // Column 7 (index 6) is NucleotideExpression (full HGVS)
            if fields.len() > 6 {
                let hgvs = fields[6].trim();
                if !hgvs.is_empty() && hgvs != "-" && hgvs.contains(':') {
                    patterns.push(hgvs.to_string());
                }
            }
            // Column 9 (index 8) is ProteinExpression (full HGVS)
            if fields.len() > 8 {
                let hgvs = fields[8].trim();
                if !hgvs.is_empty() && hgvs != "-" && hgvs.contains(':') {
                    patterns.push(hgvs.to_string());
                }
            }
        } else if line.contains(':') {
            // Plain text, one pattern per line
            patterns.push(line.to_string());
        }
    }

    // Deduplicate
    patterns.sort();
    patterns.dedup();

    Ok(patterns)
}

/// Sample patterns randomly.
fn sample_patterns(patterns: &[String], size: usize, seed: u64) -> Vec<String> {
    // Try stratified sampling first
    // Note: protein exclusion is handled by the caller (config.include_protein),
    // so we pass false here to avoid double-filtering
    if let Ok(sampled) = stratified_sample_vec(patterns, size, seed, false) {
        return sampled;
    }

    // Fall back to simple random sampling
    let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
    let mut sampled: Vec<String> = patterns.to_vec();
    sampled.shuffle(&mut rng);
    sampled.truncate(size);
    sampled
}

/// Run ferro-hgvs normalization on patterns.
///
/// Uses lenient mode to correct reference mismatches and track them in the results.
fn run_ferro_normalize(
    patterns: &[String],
    reference_dir: &Option<std::path::PathBuf>,
    error_mode: &str,
) -> Result<(Vec<ParseResult>, std::time::Duration), FerroError> {
    // Create normalizer with LENIENT config for benchmark - this allows us to track
    // reference mismatches that would otherwise cause failures in strict mode
    let config = NormalizeConfig::lenient();

    let normalizer: Normalizer<Box<dyn ReferenceProvider>> = match reference_dir {
        Some(ref ref_dir) => {
            let manifest_path = ref_dir.join("manifest.json");
            if manifest_path.exists() {
                let provider = MultiFastaProvider::from_manifest(&manifest_path)?;
                Normalizer::with_config(Box::new(provider) as Box<dyn ReferenceProvider>, config)
            } else if ref_dir.is_dir() {
                let provider = MultiFastaProvider::from_directory(ref_dir)?;
                Normalizer::with_config(Box::new(provider) as Box<dyn ReferenceProvider>, config)
            } else {
                Normalizer::with_config(
                    Box::new(MockProvider::with_test_data()) as Box<dyn ReferenceProvider>,
                    config,
                )
            }
        }
        None => Normalizer::with_config(
            Box::new(MockProvider::with_test_data()) as Box<dyn ReferenceProvider>,
            config,
        ),
    };

    // Create error config from mode string for preprocessing
    let base_mode = match error_mode {
        "lenient" => ErrorMode::Lenient,
        "silent" => ErrorMode::Silent,
        _ => ErrorMode::Strict,
    };
    let error_config = ErrorConfig::new(base_mode);
    let preprocessor = InputPreprocessor::new(error_config);

    let start = Instant::now();

    let results: Vec<ParseResult> = patterns
        .iter()
        .map(|pattern| {
            // Preprocess input based on error mode
            let preprocess_result = preprocessor.preprocess(pattern);
            let preprocessed = if preprocess_result.success {
                preprocess_result.preprocessed
            } else {
                pattern.clone()
            };

            match parse_hgvs_lenient(&preprocessed) {
                Ok(parse_result) => {
                    // Use normalize_with_warnings to capture reference mismatches
                    match normalizer.normalize_with_warnings(&parse_result.result) {
                        Ok(norm_result) => {
                            // Check if there were reference mismatches
                            let ref_mismatch = norm_result
                                .warnings
                                .iter()
                                .find(|w| w.code == "REFSEQ_MISMATCH")
                                .map(|w| RefMismatchInfo {
                                    stated_ref: w.stated_ref.clone(),
                                    actual_ref: w.actual_ref.clone(),
                                    position: w.position.clone(),
                                    corrected: w.corrected,
                                });

                            ParseResult {
                                input: pattern.clone(),
                                success: true,
                                output: Some(norm_result.result.to_string()),
                                error: None,
                                error_category: None,
                                ref_mismatch,
                                details: None,
                            }
                        }
                        Err(e) => ParseResult {
                            input: pattern.clone(),
                            success: false,
                            output: None,
                            error: Some(format!("{}", e)),
                            error_category: Some("normalize_error".to_string()),
                            ref_mismatch: None,
                            details: None,
                        },
                    }
                }
                Err(e) => ParseResult {
                    input: pattern.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("{}", e)),
                    error_category: Some("parse_error".to_string()),
                    ref_mismatch: None,
                    details: None,
                },
            }
        })
        .collect();

    let elapsed = start.elapsed();
    Ok((results, elapsed))
}

/// Run ferro-hgvs parsing on patterns.
fn run_ferro_parse(
    patterns: &[String],
    error_mode: &str,
) -> (Vec<ParseResult>, std::time::Duration) {
    // Create error config from mode string
    let base_mode = match error_mode {
        "lenient" => ErrorMode::Lenient,
        "silent" => ErrorMode::Silent,
        _ => ErrorMode::Strict,
    };
    let error_config = ErrorConfig::new(base_mode);
    let preprocessor = InputPreprocessor::new(error_config);

    let start = Instant::now();

    let results: Vec<ParseResult> = patterns
        .iter()
        .map(|pattern| {
            // Preprocess input based on error mode
            let preprocess_result = preprocessor.preprocess(pattern);
            let preprocessed = if preprocess_result.success {
                preprocess_result.preprocessed
            } else {
                pattern.clone()
            };

            match parse_hgvs_lenient(&preprocessed) {
                Ok(parse_result) => ParseResult {
                    input: pattern.clone(),
                    success: true,
                    output: Some(parse_result.result.to_string()),
                    error: None,
                    error_category: None,
                    ref_mismatch: None,
                    details: None,
                },
                Err(e) => ParseResult {
                    input: pattern.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("{}", e)),
                    error_category: Some("parse_error".to_string()),
                    ref_mismatch: None,
                    details: None,
                },
            }
        })
        .collect();

    let elapsed = start.elapsed();
    (results, elapsed)
}

/// Run mutalyzer normalization in parallel using sharded subprocesses.
///
/// Supports two modes:
/// 1. Pre-sharded cache (recommended): If the cache has shard_manifest.json,
///    patterns are routed to their corresponding shard based on accession hash.
///    Each worker processes one or more shards sequentially.
/// 2. Runtime partitioning (fallback): Patterns are distributed round-robin to
///    workers, and each worker gets symlinks to only the sequences it needs.
#[allow(clippy::type_complexity)]
pub fn run_mutalyzer_normalize_parallel(
    patterns: &[String],
    workers: usize,
    settings_file: Option<&str>,
    allow_network: bool,
) -> Result<
    (
        Vec<ParseResult>,
        std::time::Duration,
        HashMap<String, usize>,
    ),
    FerroError,
> {
    let temp_dir = tempfile::tempdir().map_err(|e| FerroError::Io {
        msg: format!("Failed to create temp directory: {}", e),
    })?;

    // Check if we have a pre-sharded cache
    let (cache_dir, manifest) = if let Some(settings_path) = settings_file {
        let settings_path = Path::new(settings_path);
        match parse_mutalyzer_settings(settings_path) {
            Ok(cache_dir) => {
                let manifest = load_shard_manifest(&cache_dir);
                (Some(cache_dir), manifest)
            }
            Err(_) => (None, None),
        }
    } else {
        (None, None)
    };

    // Use pre-sharded approach if manifest exists
    if let (Some(cache_dir), Some(manifest)) = (&cache_dir, &manifest) {
        return run_mutalyzer_normalize_presharded(
            patterns,
            workers,
            cache_dir,
            manifest.num_shards,
            allow_network,
            temp_dir.path(),
            settings_file,
        );
    }

    // Fall back to runtime partitioning
    eprintln!("[normalize] Using runtime partitioning (no pre-sharded cache found)");

    // Shard patterns round-robin
    let shards = shard_patterns(patterns, workers);

    // Partition cache if settings file is provided
    let partitioned_settings = if let Some(cache_dir) = &cache_dir {
        eprintln!(
            "[partition] Partitioning cache from {} for {} workers",
            cache_dir.display(),
            workers
        );
        match partition_cache_for_workers(&shards, cache_dir, temp_dir.path()) {
            Ok(paths) => Some(paths),
            Err(e) => {
                eprintln!("[partition] Warning: Failed to partition cache: {}", e);
                eprintln!("[partition] Falling back to shared cache");
                None
            }
        }
    } else {
        None
    };

    // Write shard input files
    let mut shard_paths = Vec::new();
    for (i, shard) in shards.iter().enumerate() {
        let input_path = temp_dir.path().join(format!("shard_{}_input.txt", i));
        let output_path = temp_dir.path().join(format!("shard_{}_output.json", i));

        let mut file = File::create(&input_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to create shard file: {}", e),
        })?;
        for pattern in shard {
            writeln!(file, "{}", pattern).map_err(|e| FerroError::Io {
                msg: format!("Failed to write pattern: {}", e),
            })?;
        }

        shard_paths.push((input_path, output_path));
    }

    // Run workers in parallel using process spawning
    let start = Instant::now();

    // Spawn all processes first (non-blocking)
    let mut children: Vec<(usize, std::process::Child)> = Vec::new();
    for (i, (input, output)) in shard_paths.iter().enumerate() {
        let mut cmd = std::process::Command::new("python3");
        cmd.args(["-c", MUTALYZER_NORMALIZE_SCRIPT]);
        cmd.arg(input.display().to_string());
        cmd.arg(output.display().to_string());

        // Use partitioned settings if available, otherwise fall back to shared settings
        if let Some(ref partitioned) = partitioned_settings {
            cmd.arg(partitioned[i].display().to_string());
        } else if let Some(settings) = settings_file {
            cmd.arg(settings);
        } else {
            cmd.arg("");
        }

        // Pass network flag as 5th argument
        cmd.arg(if allow_network { "true" } else { "false" });

        match cmd.spawn() {
            Ok(child) => children.push((i, child)),
            Err(e) => {
                cleanup_children(&mut children);
                return Err(FerroError::Io {
                    msg: format!("Failed to spawn Python: {}", e),
                });
            }
        }
    }

    // Wait for all processes to complete, killing remaining on first error
    let mut wait_error = None;
    for (shard_id, child) in children.iter_mut() {
        if wait_error.is_some() {
            kill_and_wait(child, *shard_id);
            continue;
        }
        match child.wait() {
            Ok(status) if !status.success() => {
                wait_error = Some(FerroError::Io {
                    msg: format!(
                        "Mutalyzer normalizer failed with exit code: {:?}",
                        status.code()
                    ),
                });
            }
            Err(e) => {
                wait_error = Some(FerroError::Io {
                    msg: format!("Failed to wait for Python: {}", e),
                });
            }
            _ => {}
        }
    }
    if let Some(err) = wait_error {
        return Err(err);
    }

    let elapsed = start.elapsed();

    // Collect results and aggregate error counts
    let mut all_results = Vec::new();
    let mut aggregated_error_counts: HashMap<String, usize> = HashMap::new();
    for (_, output_path) in &shard_paths {
        let file = File::open(output_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open shard output: {}", e),
        })?;
        let reader = BufReader::new(file);
        let output: MutalyzerOutput =
            serde_json::from_reader(reader).map_err(|e| FerroError::Json {
                msg: format!("Failed to parse shard output: {}", e),
            })?;
        all_results.extend(output.results);

        // Aggregate error counts from each shard
        for (category, count) in output.error_counts {
            *aggregated_error_counts.entry(category).or_insert(0) += count;
        }
    }

    Ok((all_results, elapsed, aggregated_error_counts))
}

/// Run mutalyzer normalization using pre-sharded cache with work-stealing.
///
/// Each non-empty shard becomes an independent task. A pool of N workers
/// processes tasks from a shared queue — when a worker finishes a shard,
/// it immediately picks up the next available one. This avoids the straggler
/// problem where one worker gets stuck with disproportionately slow shards.
#[allow(clippy::type_complexity)]
fn run_mutalyzer_normalize_presharded(
    patterns: &[String],
    workers: usize,
    cache_dir: &Path,
    num_shards: usize,
    allow_network: bool,
    temp_dir: &Path,
    settings_file: Option<&str>,
) -> Result<
    (
        Vec<ParseResult>,
        std::time::Duration,
        HashMap<String, usize>,
    ),
    FerroError,
> {
    eprintln!(
        "[normalize] Using pre-sharded cache ({} shards, {} workers, work-stealing)",
        num_shards, workers
    );

    // Group patterns by their accession's shard
    let shard_groups = group_patterns_by_shard(patterns, num_shards);

    // Collect non-empty shards as individual tasks
    let shard_tasks: Vec<(usize, Vec<String>)> = shard_groups
        .into_iter()
        .enumerate()
        .filter(|(_, g)| !g.is_empty())
        .collect();

    let effective_workers = workers.min(shard_tasks.len());

    eprintln!(
        "[normalize] {} non-empty shards queued for {} workers",
        shard_tasks.len(),
        effective_workers
    );

    let settings_path = settings_file
        .map(PathBuf::from)
        .unwrap_or_else(|| cache_dir.join("mutalyzer_settings.conf"));

    // Write input files for all shard tasks upfront
    let mut task_io: Vec<(usize, PathBuf, PathBuf, usize)> = Vec::new(); // (shard_id, input, output, pattern_count)
    for (shard_id, shard_patterns) in &shard_tasks {
        let input_path = temp_dir.join(format!("shard_{}_input.txt", shard_id));
        let output_path = temp_dir.join(format!("shard_{}_output.json", shard_id));

        let mut file = File::create(&input_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to create input file: {}", e),
        })?;
        for pattern in shard_patterns {
            writeln!(file, "{}", pattern).map_err(|e| FerroError::Io {
                msg: format!("Failed to write pattern: {}", e),
            })?;
        }

        task_io.push((*shard_id, input_path, output_path, shard_patterns.len()));
    }

    // Work-stealing: maintain up to N concurrent subprocesses.
    // As each finishes, spawn the next queued shard.
    let start = Instant::now();
    let mut task_queue = std::collections::VecDeque::from(task_io);
    // Each active slot: (child, shard_id, output_path, pattern_count)
    let mut active: Vec<(std::process::Child, usize, PathBuf, usize)> = Vec::new();
    let mut completed_outputs: Vec<PathBuf> = Vec::new();
    let mut shards_done = 0;
    let total_shards = shard_tasks.len();

    // Fill initial worker slots
    while active.len() < effective_workers {
        if let Some((shard_id, input_path, output_path, count)) = task_queue.pop_front() {
            match spawn_mutalyzer_worker(&input_path, &output_path, &settings_path, allow_network) {
                Ok(child) => active.push((child, shard_id, output_path, count)),
                Err(e) => {
                    cleanup_active_workers(&mut active);
                    return Err(e);
                }
            }
        } else {
            break;
        }
    }

    // Poll active workers, replacing finished ones with queued tasks
    while !active.is_empty() {
        let mut progress_made = false;
        let mut i = 0;
        while i < active.len() {
            match active[i].0.try_wait() {
                Ok(Some(status)) => {
                    let (_, shard_id, output_path, count) = active.remove(i);
                    shards_done += 1;
                    progress_made = true;
                    if !status.success() {
                        cleanup_active_workers(&mut active);
                        return Err(FerroError::Io {
                            msg: format!(
                                "Mutalyzer normalizer failed on shard {} with exit code: {:?}",
                                shard_id,
                                status.code()
                            ),
                        });
                    }
                    eprintln!(
                        "[normalize] Shard {} done ({} patterns) [{}/{}]",
                        shard_id, count, shards_done, total_shards
                    );
                    completed_outputs.push(output_path);

                    // Backfill from queue
                    if let Some((next_shard, input_path, output_path, next_count)) =
                        task_queue.pop_front()
                    {
                        match spawn_mutalyzer_worker(
                            &input_path,
                            &output_path,
                            &settings_path,
                            allow_network,
                        ) {
                            Ok(child) => {
                                active.push((child, next_shard, output_path, next_count));
                            }
                            Err(e) => {
                                cleanup_active_workers(&mut active);
                                return Err(e);
                            }
                        }
                    }
                    // Don't increment i — we removed an element
                }
                Ok(None) => {
                    // Still running
                    i += 1;
                }
                Err(e) => {
                    cleanup_active_workers(&mut active);
                    return Err(FerroError::Io {
                        msg: format!("Failed to poll worker: {}", e),
                    });
                }
            }
        }
        // Brief sleep to avoid busy-spinning, but skip if a worker just
        // completed so we immediately poll for more completions / backfills.
        if !active.is_empty() && !progress_made {
            std::thread::sleep(std::time::Duration::from_millis(200));
        }
    }

    let elapsed = start.elapsed();

    // Collect results from all completed shards
    let mut all_results = Vec::new();
    let mut aggregated_error_counts: HashMap<String, usize> = HashMap::new();

    for output_path in &completed_outputs {
        let file = File::open(output_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open output: {}", e),
        })?;
        let reader = BufReader::new(file);
        let output: MutalyzerOutput =
            serde_json::from_reader(reader).map_err(|e| FerroError::Json {
                msg: format!("Failed to parse output: {}", e),
            })?;
        all_results.extend(output.results);

        for (category, count) in output.error_counts {
            *aggregated_error_counts.entry(category).or_insert(0) += count;
        }
    }

    Ok((all_results, elapsed, aggregated_error_counts))
}

/// Kill and wait on a single child process, logging any errors.
///
/// Suppresses "no such process" (ESRCH) errors from `kill()` on Unix, since
/// the child may have already exited between polling and cleanup.
fn kill_and_wait(child: &mut std::process::Child, shard_id: usize) {
    if let Err(e) = child.kill() {
        // ESRCH (3) is POSIX-defined across all Unix-like systems.
        let is_already_exited = cfg!(unix) && e.raw_os_error() == Some(3);
        if !is_already_exited {
            eprintln!(
                "[normalize] Warning: failed to kill worker for shard {}: {}",
                shard_id, e
            );
        }
    }
    if let Err(e) = child.wait() {
        eprintln!(
            "[normalize] Warning: failed to wait on worker for shard {}: {}",
            shard_id, e
        );
    }
}

/// Kill and wait on all active child processes (work-stealing pool variant).
///
/// Called on error paths to prevent orphaned subprocesses. Errors during
/// cleanup are logged but not propagated — the caller already has an error.
fn cleanup_active_workers(active: &mut Vec<(std::process::Child, usize, PathBuf, usize)>) {
    for (child, shard_id, _, _) in active.iter_mut() {
        kill_and_wait(child, *shard_id);
    }
    active.clear();
}

/// Kill and wait on all spawned child processes (simple parallel variant).
///
/// Called on error paths to prevent orphaned subprocesses.
fn cleanup_children(children: &mut Vec<(usize, std::process::Child)>) {
    for (shard_id, child) in children.iter_mut() {
        kill_and_wait(child, *shard_id);
    }
    children.clear();
}

/// Spawn a single mutalyzer normalize subprocess for a shard.
fn spawn_mutalyzer_worker(
    input_path: &Path,
    output_path: &Path,
    settings_path: &Path,
    allow_network: bool,
) -> Result<std::process::Child, FerroError> {
    let mut cmd = std::process::Command::new("python3");
    cmd.args(["-c", MUTALYZER_NORMALIZE_SCRIPT]);
    cmd.arg(input_path.display().to_string());
    cmd.arg(output_path.display().to_string());
    cmd.arg(settings_path.display().to_string());
    cmd.arg(if allow_network { "true" } else { "false" });

    cmd.spawn().map_err(|e| FerroError::Io {
        msg: format!("Failed to spawn Python: {}", e),
    })
}

/// Run biocommons/hgvs normalization in parallel using sharded subprocesses.
#[allow(clippy::type_complexity)]
fn run_biocommons_normalize_parallel(
    patterns: &[String],
    workers: usize,
    uta_db_url: Option<&str>,
) -> Result<
    (
        Vec<ParseResult>,
        std::time::Duration,
        HashMap<String, usize>,
    ),
    FerroError,
> {
    let temp_dir = tempfile::tempdir().map_err(|e| FerroError::Io {
        msg: format!("Failed to create temp directory: {}", e),
    })?;

    // Shard patterns
    let shards = shard_patterns(patterns, workers);

    // Write shard input files
    let mut shard_paths = Vec::new();
    for (i, shard) in shards.iter().enumerate() {
        let input_path = temp_dir.path().join(format!("shard_{}_input.txt", i));
        let output_path = temp_dir.path().join(format!("shard_{}_output.json", i));

        let mut file = File::create(&input_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to create shard file: {}", e),
        })?;
        for pattern in shard {
            writeln!(file, "{}", pattern).map_err(|e| FerroError::Io {
                msg: format!("Failed to write pattern: {}", e),
            })?;
        }

        shard_paths.push((input_path, output_path));
    }

    // Run workers in parallel using process spawning
    let start = Instant::now();

    // Spawn all processes first (non-blocking)
    let mut children: Vec<(usize, std::process::Child)> = Vec::new();
    for (i, (input, output)) in shard_paths.iter().enumerate() {
        let mut cmd = std::process::Command::new("python3");
        cmd.args(["-c", BIOCOMMONS_NORMALIZE_SCRIPT]);
        cmd.arg(input.display().to_string());
        cmd.arg(output.display().to_string());
        // Pass UTA database URL as 3rd argument (empty string if not provided)
        cmd.arg(uta_db_url.unwrap_or(""));

        match cmd.spawn() {
            Ok(child) => children.push((i, child)),
            Err(e) => {
                cleanup_children(&mut children);
                return Err(FerroError::Io {
                    msg: format!("Failed to spawn Python: {}", e),
                });
            }
        }
    }

    // Wait for all processes to complete
    let mut wait_error = None;
    for (shard_id, child) in children.iter_mut() {
        if wait_error.is_some() {
            kill_and_wait(child, *shard_id);
            continue;
        }
        match child.wait() {
            Ok(status) if !status.success() => {
                wait_error = Some(FerroError::Io {
                    msg: format!(
                        "biocommons/hgvs normalizer failed with exit code: {:?}",
                        status.code()
                    ),
                });
            }
            Err(e) => {
                wait_error = Some(FerroError::Io {
                    msg: format!("Failed to wait for Python: {}", e),
                });
            }
            _ => {}
        }
    }
    if let Some(err) = wait_error {
        return Err(err);
    }

    let elapsed = start.elapsed();

    // Collect results and aggregate error counts
    let mut all_results = Vec::new();
    let mut aggregated_error_counts: HashMap<String, usize> = HashMap::new();
    for (_, output_path) in &shard_paths {
        let file = File::open(output_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open shard output: {}", e),
        })?;
        let reader = BufReader::new(file);
        let output: MutalyzerOutput =
            serde_json::from_reader(reader).map_err(|e| FerroError::Json {
                msg: format!("Failed to parse shard output: {}", e),
            })?;
        all_results.extend(output.results);

        // Aggregate error counts from each shard
        for (category, count) in output.error_counts {
            *aggregated_error_counts.entry(category).or_insert(0) += count;
        }
    }

    Ok((all_results, elapsed, aggregated_error_counts))
}

/// Run mutalyzer parsing in parallel using sharded subprocesses.
fn run_mutalyzer_parse_parallel(
    patterns: &[String],
    workers: usize,
) -> Result<(Vec<ParseResult>, std::time::Duration), FerroError> {
    let temp_dir = tempfile::tempdir().map_err(|e| FerroError::Io {
        msg: format!("Failed to create temp directory: {}", e),
    })?;

    // Shard patterns
    let shards = shard_patterns(patterns, workers);

    // Write shard input files
    let mut shard_paths = Vec::new();
    for (i, shard) in shards.iter().enumerate() {
        let input_path = temp_dir.path().join(format!("shard_{}_input.txt", i));
        let output_path = temp_dir.path().join(format!("shard_{}_output.json", i));

        let mut file = File::create(&input_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to create shard file: {}", e),
        })?;
        for pattern in shard {
            writeln!(file, "{}", pattern).map_err(|e| FerroError::Io {
                msg: format!("Failed to write pattern: {}", e),
            })?;
        }

        shard_paths.push((input_path, output_path));
    }

    // Run workers in parallel using process spawning
    let start = Instant::now();

    // Spawn all processes first (non-blocking)
    let mut children: Vec<std::process::Child> = Vec::new();
    for (input, output) in &shard_paths {
        let mut cmd = std::process::Command::new("python3");
        cmd.args(["-c", MUTALYZER_PARSE_SCRIPT]);
        cmd.arg(input.display().to_string());
        cmd.arg(output.display().to_string());

        let child = cmd.spawn().map_err(|e| FerroError::Io {
            msg: format!("Failed to spawn Python: {}", e),
        })?;
        children.push(child);
    }

    // Wait for all processes to complete
    for mut child in children {
        let status = child.wait().map_err(|e| FerroError::Io {
            msg: format!("Failed to wait for Python: {}", e),
        })?;
        if !status.success() {
            return Err(FerroError::Io {
                msg: format!(
                    "Mutalyzer parser failed with exit code: {:?}",
                    status.code()
                ),
            });
        }
    }

    let elapsed = start.elapsed();

    // Collect results
    let mut all_results = Vec::new();
    for (_, output_path) in &shard_paths {
        let file = File::open(output_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open shard output: {}", e),
        })?;
        let reader = BufReader::new(file);
        let output: MutalyzerOutput =
            serde_json::from_reader(reader).map_err(|e| FerroError::Json {
                msg: format!("Failed to parse shard output: {}", e),
            })?;
        all_results.extend(output.results);
    }

    Ok((all_results, elapsed))
}

/// Shard patterns into N groups for parallel processing.
fn shard_patterns(patterns: &[String], num_shards: usize) -> Vec<Vec<String>> {
    let mut shards: Vec<Vec<String>> = (0..num_shards).map(|_| Vec::new()).collect();

    for (i, pattern) in patterns.iter().enumerate() {
        shards[i % num_shards].push(pattern.clone());
    }

    shards
}

/// Parse a mutalyzer settings file to extract the cache directory.
fn parse_mutalyzer_settings(settings_path: &Path) -> Result<PathBuf, FerroError> {
    let content = std::fs::read_to_string(settings_path).map_err(|e| FerroError::Io {
        msg: format!("Failed to read settings file: {}", e),
    })?;

    for line in content.lines() {
        let line = line.trim();
        if line.starts_with("MUTALYZER_CACHE_DIR") {
            // Format: MUTALYZER_CACHE_DIR = /path/to/cache or MUTALYZER_CACHE_DIR=/path/to/cache
            if let Some(value) = line.split('=').nth(1) {
                return Ok(PathBuf::from(value.trim()));
            }
        }
    }

    Err(FerroError::Io {
        msg: format!(
            "MUTALYZER_CACHE_DIR not found in settings file: {}",
            settings_path.display()
        ),
    })
}

/// Shard manifest from pre-sharded cache.
#[derive(Debug, serde::Deserialize)]
struct ShardManifest {
    num_shards: usize,
    #[allow(dead_code)]
    total_accessions: usize,
    #[allow(dead_code)]
    shard_counts: Vec<usize>,
}

/// Compute the shard ID for an accession using the same hash function as prepare time.
/// Must match the hash function in shard_mutalyzer_cache in benchmark.rs.
fn accession_to_shard(accession: &str, num_shards: usize) -> usize {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    accession.hash(&mut hasher);
    (hasher.finish() as usize) % num_shards
}

/// Load shard manifest from cache directory if it exists.
fn load_shard_manifest(cache_dir: &Path) -> Option<ShardManifest> {
    let manifest_path = cache_dir.join("shard_manifest.json");
    if !manifest_path.exists() {
        return None;
    }

    let file = File::open(&manifest_path).ok()?;
    let reader = BufReader::new(file);
    serde_json::from_reader(reader).ok()
}

/// Group patterns by their accession's shard ID.
///
/// Returns a vector indexed by shard_id, where each entry is the list of patterns
/// that should be processed by that shard.
fn group_patterns_by_shard(patterns: &[String], num_shards: usize) -> Vec<Vec<String>> {
    let mut shard_groups: Vec<Vec<String>> = (0..num_shards).map(|_| Vec::new()).collect();

    for pattern in patterns {
        // Extract the primary accession from the pattern
        if let Some(accession) = extract_accession(pattern) {
            let shard_id = accession_to_shard(accession, num_shards);
            shard_groups[shard_id].push(pattern.clone());
        } else {
            // If we can't extract an accession, put it in shard 0 as fallback
            shard_groups[0].push(pattern.clone());
        }
    }

    shard_groups
}

/// Partition the mutalyzer cache for parallel workers.
///
/// Creates per-worker cache directories with symlinks to only the sequence files
/// needed for each worker's patterns. This dramatically reduces memory usage
/// since each worker only loads a subset of the cache.
///
/// Returns a vector of settings file paths, one per worker.
fn partition_cache_for_workers(
    shards: &[Vec<String>],
    full_cache_dir: &Path,
    work_dir: &Path,
) -> Result<Vec<PathBuf>, FerroError> {
    let num_workers = shards.len();
    let mut settings_paths = Vec::with_capacity(num_workers);

    for (worker_id, shard) in shards.iter().enumerate() {
        // Extract all accessions needed for this shard
        let mut accessions = HashSet::new();
        for pattern in shard {
            for acc in extract_accessions_from_pattern(pattern) {
                accessions.insert(acc);
            }
        }

        // Create per-worker cache directory
        let worker_cache_dir = work_dir.join(format!("cache_worker_{}", worker_id));
        std::fs::create_dir_all(&worker_cache_dir).map_err(|e| FerroError::Io {
            msg: format!("Failed to create worker cache dir: {}", e),
        })?;

        // Symlink only the needed sequence and annotation files
        for accession in &accessions {
            for ext in &["sequence", "annotations"] {
                let filename = format!("{}.{}", accession, ext);
                let src = full_cache_dir.join(&filename);
                let dst = worker_cache_dir.join(&filename);

                // Only symlink if source exists (some accessions may not be cached)
                if src.exists() && !dst.exists() {
                    #[cfg(unix)]
                    std::os::unix::fs::symlink(&src, &dst).map_err(|e| FerroError::Io {
                        msg: format!("Failed to create symlink {}: {}", dst.display(), e),
                    })?;
                    #[cfg(not(unix))]
                    std::fs::copy(&src, &dst).map_err(|e| FerroError::Io {
                        msg: format!("Failed to copy {}: {}", dst.display(), e),
                    })?;
                }
            }
        }

        // Create per-worker settings file
        let settings_path = work_dir.join(format!("mutalyzer_settings_{}.conf", worker_id));
        let mut settings_file = File::create(&settings_path).map_err(|e| FerroError::Io {
            msg: format!("Failed to create settings file: {}", e),
        })?;
        writeln!(
            settings_file,
            "MUTALYZER_CACHE_DIR = {}",
            worker_cache_dir.display()
        )
        .map_err(|e| FerroError::Io {
            msg: format!("Failed to write settings: {}", e),
        })?;
        writeln!(settings_file, "MUTALYZER_FILE_CACHE_ADD = false").map_err(|e| {
            FerroError::Io {
                msg: format!("Failed to write settings: {}", e),
            }
        })?;

        settings_paths.push(settings_path);

        eprintln!(
            "[partition] Worker {} needs {} accessions, {} symlinks created",
            worker_id,
            accessions.len(),
            accessions.len() * 2
        );
    }

    Ok(settings_paths)
}

/// Compute agreement statistics between two result sets.
///
/// This uses semantic equivalence checking, so notation differences like
/// `c.[A;B]` vs `[acc:c.A;acc:c.B]` are counted as agreements.
fn compute_agreement(
    ferro_results: &[ParseResult],
    muta_results: &[ParseResult],
) -> (AgreementStats, Vec<DisagreementExample>) {
    // Build lookup map for mutalyzer results
    let muta_map: HashMap<&str, &ParseResult> =
        muta_results.iter().map(|r| (r.input.as_str(), r)).collect();

    let mut both_success = 0usize;
    let mut both_fail = 0usize;
    let mut ferro_only_success = 0usize;
    let mut mutalyzer_only_success = 0usize;
    let mut agreements = 0usize;
    let mut disagreement_examples = Vec::new();

    for ferro_result in ferro_results {
        if let Some(muta_result) = muta_map.get(ferro_result.input.as_str()) {
            match (ferro_result.success, muta_result.success) {
                (true, true) => {
                    both_success += 1;
                    let ferro_out = ferro_result.output.as_deref().unwrap_or("");
                    let muta_out = muta_result.output.as_deref().unwrap_or("");

                    // Check for exact match or semantic equivalence
                    if outputs_are_equivalent(ferro_out, muta_out) {
                        agreements += 1;
                    } else if disagreement_examples.len() < 50 {
                        disagreement_examples.push(DisagreementExample {
                            input: ferro_result.input.clone(),
                            ferro_output: ferro_result.output.clone().unwrap_or_default(),
                            mutalyzer_output: muta_result.output.clone().unwrap_or_default(),
                        });
                    }
                }
                (true, false) => ferro_only_success += 1,
                (false, true) => mutalyzer_only_success += 1,
                (false, false) => both_fail += 1,
            }
        }
    }

    let agreement_rate = if both_success > 0 {
        agreements as f64 / both_success as f64
    } else {
        0.0
    };

    let stats = AgreementStats {
        both_success,
        both_fail,
        ferro_only_success,
        mutalyzer_only_success,
        agreements,
        agreement_rate,
        disagreement_examples: disagreement_examples.clone(),
    };

    (stats, disagreement_examples)
}

/// Compute reference mismatch statistics.
///
/// This calculates both strict and lenient agreement rates:
/// - Lenient: ferro corrects reference mismatches (current behavior)
/// - Strict: ferro would reject reference mismatches (fair comparison with mutalyzer)
fn compute_reference_stats(
    ferro_results: &[ParseResult],
    muta_results: &[ParseResult],
    base_agreement: &AgreementStats,
) -> ReferenceStats {
    // Build lookup map for mutalyzer results
    let muta_map: HashMap<&str, &ParseResult> =
        muta_results.iter().map(|r| (r.input.as_str(), r)).collect();

    // Count patterns with reference corrections
    let patterns_with_corrections: Vec<String> = ferro_results
        .iter()
        .filter(|r| r.ref_mismatch.is_some())
        .map(|r| r.input.clone())
        .take(100) // Limit to 100 examples
        .collect();

    let corrections_count = ferro_results
        .iter()
        .filter(|r| r.ref_mismatch.is_some())
        .count();

    // Calculate what agreement would be if ferro rejected ref mismatches (strict mode)
    // In strict mode, patterns with ref mismatches would be counted as ferro failures
    let mut strict_both_success = 0usize;
    let mut strict_agreements = 0usize;

    for ferro_result in ferro_results {
        if let Some(muta_result) = muta_map.get(ferro_result.input.as_str()) {
            // In strict mode, ferro would fail on patterns with ref mismatches
            let ferro_strict_success = ferro_result.success && ferro_result.ref_mismatch.is_none();

            if ferro_strict_success && muta_result.success {
                strict_both_success += 1;
                let ferro_out = ferro_result.output.as_deref().unwrap_or("");
                let muta_out = muta_result.output.as_deref().unwrap_or("");
                if outputs_are_equivalent(ferro_out, muta_out) {
                    strict_agreements += 1;
                }
            }
        }
    }

    let strict_agreement_rate = if strict_both_success > 0 {
        strict_agreements as f64 / strict_both_success as f64
    } else {
        0.0
    };

    ReferenceStats {
        corrections_count,
        strict_agreement_rate,
        lenient_agreement_rate: base_agreement.agreement_rate,
        patterns_with_corrections,
    }
}

/// Print a summary of the comparison results to stderr.
fn print_comparison_summary(result: &ComparisonResult) {
    eprintln!();
    eprintln!(
        "COMPARISON RESULTS ({}, {} patterns)",
        result.mode, result.sample_size
    );
    eprintln!("{}", "=".repeat(50));

    eprintln!(
        "ferro-hgvs: {:>6.2}s  ({:>7.0} p/s)  {}/{} success",
        result.ferro.elapsed_seconds,
        result.ferro.throughput,
        result.ferro.successful,
        result.sample_size
    );
    eprintln!(
        "mutalyzer:     {:>6.2}s  ({:>7.0} p/s)  {}/{} success",
        result.mutalyzer.elapsed_seconds,
        result.mutalyzer.throughput,
        result.mutalyzer.successful,
        result.sample_size
    );

    eprintln!();
    eprintln!("Speedup: {:.0}x faster", result.speedup);

    // Show reference mismatch statistics if available
    if let Some(ref ref_stats) = result.reference_stats {
        if ref_stats.corrections_count > 0 {
            eprintln!();
            eprintln!("Reference Mismatch Handling:");
            eprintln!(
                "  Patterns with ref mismatches: {}",
                ref_stats.corrections_count
            );
            eprintln!(
                "  Ferro corrected (lenient):    {}/{}",
                ref_stats.corrections_count, ref_stats.corrections_count
            );

            eprintln!();
            eprintln!("Agreement Analysis:");
            eprintln!(
                "  Lenient mode (ferro corrects): {:>5}/{:<5} ({:.1}%)",
                result.agreement.agreements,
                result.agreement.both_success,
                ref_stats.lenient_agreement_rate * 100.0
            );
            // Calculate strict both_success: excludes patterns where ferro succeeded due to correction
            let strict_both_success = result
                .agreement
                .both_success
                .saturating_sub(ref_stats.corrections_count);
            let strict_agreements =
                (strict_both_success as f64 * ref_stats.strict_agreement_rate).round() as usize;
            eprintln!(
                "  Strict mode (both reject):     {:>5}/{:<5} ({:.1}%)  [fair comparison]",
                strict_agreements,
                strict_both_success,
                ref_stats.strict_agreement_rate * 100.0
            );
        }
    }

    // Only show detailed agreement if no ref stats or no corrections
    if result
        .reference_stats
        .as_ref()
        .is_none_or(|s| s.corrections_count == 0)
    {
        eprintln!();
        eprintln!("Agreement (where both succeeded):");
        let different = result.agreement.both_success - result.agreement.agreements;
        eprintln!(
            "  Same output:      {:>5}/{:<5} ({:.1}%)",
            result.agreement.agreements,
            result.agreement.both_success,
            result.agreement.agreement_rate * 100.0
        );
        eprintln!(
            "  Different output: {:>5}/{:<5} ({:.1}%)",
            different,
            result.agreement.both_success,
            (1.0 - result.agreement.agreement_rate) * 100.0
        );
    }

    eprintln!();
    // Show ferro-only success with ref correction breakdown if available
    if let Some(ref ref_stats) = result.reference_stats {
        if ref_stats.corrections_count > 0 {
            eprintln!(
                "Ferro-only success:     {} ({} due to ref correction)",
                result.agreement.ferro_only_success, ref_stats.corrections_count
            );
        } else {
            eprintln!(
                "Ferro-only success:     {}",
                result.agreement.ferro_only_success
            );
        }
    } else {
        eprintln!(
            "Ferro-only success:     {}",
            result.agreement.ferro_only_success
        );
    }
    eprintln!(
        "Mutalyzer-only success: {}",
        result.agreement.mutalyzer_only_success
    );
    eprintln!("Both failed:            {}", result.agreement.both_fail);

    // Show cache statistics if available
    if let Some(ref cache_stats) = result.cache_stats {
        eprintln!();
        eprintln!("Cache Coverage:");
        eprintln!("  Unique accessions:    {}", cache_stats.total_accessions);
        eprintln!("  Ferro missing:        {}", cache_stats.ferro_missing);
        eprintln!("  Mutalyzer missing:    {}", cache_stats.mutalyzer_missing);
        if !cache_stats.mutalyzer_missing_examples.is_empty() {
            eprintln!(
                "    Examples: {}",
                cache_stats
                    .mutalyzer_missing_examples
                    .iter()
                    .take(5)
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
    }

    // Show error counts if available
    if let Some(ref error_counts) = result.mutalyzer.error_counts {
        if !error_counts.is_empty() {
            eprintln!();
            eprintln!("Mutalyzer Error Breakdown:");
            // Sort by count descending
            let mut counts: Vec<_> = error_counts.iter().collect();
            counts.sort_by(|a, b| b.1.cmp(a.1));
            for (category, count) in counts.iter().take(10) {
                eprintln!("  {:25} {:>5}", category, count);
            }
        }
    }

    if !result.differences.is_empty() {
        eprintln!();
        eprintln!("Sample differences:");
        for (i, diff) in result.differences.iter().take(5).enumerate() {
            eprintln!("  {}. Input:     {}", i + 1, diff.input);
            eprintln!("     ferro:     {}", diff.ferro_output);
            eprintln!("     mutalyzer: {}", diff.mutalyzer_output);
        }
        if result.differences.len() > 5 {
            eprintln!("  ... and {} more", result.differences.len() - 5);
        }
    }
}

/// Save comparison result to a JSON file.
fn save_comparison_result<P: AsRef<Path>>(
    result: &ComparisonResult,
    path: P,
) -> Result<(), FerroError> {
    let path = path.as_ref();

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| FerroError::Io {
            msg: format!("Failed to create directory: {}", e),
        })?;
    }

    let file = File::create(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to create {}: {}", path.display(), e),
    })?;
    let writer = BufWriter::new(file);
    serde_json::to_writer_pretty(writer, result).map_err(|e| FerroError::Io {
        msg: format!("Failed to write JSON: {}", e),
    })?;

    eprintln!();
    eprintln!("Results saved to: {}", path.display());

    Ok(())
}

/// Check if two HGVS outputs are semantically equivalent even if notation differs.
///
/// This handles cases where both representations are valid HGVS but use different
/// notation styles:
/// - Allelic: `c.[A;B]` vs `[acc:c.A;acc:c.B]`
/// - Delins vs split: `c.XdelinsAA` vs `c.[Xdel;X+2C>A]`
/// - Delins vs inv: `c.X_YdelinsTG` vs `c.X_Yinv` (when semantically identical)
/// - Dup vs repeat: `c.X_Ydup` vs `c.X_YSEQ[N]`
pub fn outputs_are_equivalent(ferro: &str, mutalyzer: &str) -> bool {
    // Exact match
    if ferro == mutalyzer {
        return true;
    }

    // Check allelic notation equivalence
    // Ferro: [NM_000060.2:c.1207T>G;NM_000060.2:c.1330G>C]
    // Mutalyzer: NM_000060.2:c.[1207T>G;1330G>C]
    if check_allelic_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins vs inversion equivalence
    // e.g., c.X_YdelinsTG vs c.X_Yinv when the delins is a complement swap
    if check_delins_inv_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins vs split allelic equivalence
    // e.g., c.2142_2144delinsAA vs c.[2142del;2144C>A]
    if check_delins_split_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check dup vs repeat notation equivalence
    // e.g., c.100_102dup vs c.100_102[2] (dup = 2 copies of the sequence)
    if check_dup_repeat_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check 3' shift variation equivalence
    // e.g., c.-56_-47dup vs c.-55_-46dup (same edit, position shifted 3')
    // This handles cases where ferro correctly shifts 3' according to HGVS rules
    // but mutalyzer doesn't, or vice versa.
    if check_three_prime_shift_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check repeat notation vs del/dup equivalence
    // e.g., c.-94TGC[5] vs c.-79_-68del (repeat with fewer units = deletion)
    // e.g., c.-94TGC[10] vs c.-70_-68dup (repeat with one more unit = duplication)
    if check_repeat_vs_del_dup_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check dup vs expanded repeat notation equivalence
    // e.g., c.1023_1028dup vs c.1017_1028GGC[6] (dup expressed as repeat expansion)
    if check_dup_vs_expanded_repeat_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins sequence vs position reference equivalence
    // e.g., c.174_182delinsCAGGAGGAGA vs c.174_182delins186_195
    if check_delins_sequence_vs_position_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins sequence vs bracketed position+sequence format
    // e.g., c.2484_2494delinsAAGATAAGCCAGTTTGATAA vs c.2484_2494delins[2468_2481;TGATAA]
    if check_delins_vs_bracketed_position_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check repeat with single position vs expanded range equivalence
    // e.g., c.-94TGC[11] vs c.-94_-68TGC[11] (same repeat, different position notation)
    if check_repeat_position_range_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check single-element allelic equivalence
    // e.g., [NM_X:c.100A>G] vs NM_X:c.100A>G (brackets or no brackets for single variant)
    if check_single_element_allelic_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check allelic ordering equivalence
    // e.g., [NM_X:c.1531C>T;NM_X:c.872C>T] vs NM_X:c.[872C>T;1531C>T] (different order)
    if check_allelic_ordering_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check insertion with position references equivalence
    // e.g., insCAATAT... vs ins[CAATAT;1032_1095] (explicit seq vs position reference)
    if check_ins_position_reference_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check insertion vs repeat notation equivalence
    // e.g., c.3692_3693insTT vs c.3692T[3] (insertion of 2 T's = 3 T's total)
    if check_ins_vs_repeat_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check inversion with/without bases equivalence
    // e.g., c.1267_1268invCA vs c.1267_1268inv (explicit bases or not)
    if check_inv_with_bases_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check single-base dup vs insertion equivalence
    // e.g., c.-27dup vs c.-27_-26insA (dup at -27 = insert copy after -27)
    if check_dup_vs_ins_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check del/dup vs identity equivalence
    // This occurs when:
    // - Input is repeat[N] notation (e.g., c.4261_4262CA[1])
    // - Ferro scans full repeat tract and finds N ≠ ref_count → del or dup
    // - Mutalyzer only checks stated positions and finds N matches → c.=
    // Both interpretations are valid for ambiguous repeat annotations.
    if check_del_dup_vs_identity_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check ins with explicit sequence vs position range equivalence
    // e.g., c.172_173insTGCAGCAG vs c.172_173ins170_187
    if check_ins_sequence_vs_range_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check repeat[1] vs identity equivalence
    // e.g., c.217_219AGA[1] vs c.= (both mean no change)
    if check_repeat_one_vs_identity_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check identity substitution vs c.= equivalence
    // e.g., c.1632T>T vs c.= (both mean no change)
    if check_identity_substitution_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check stop codon position notation equivalence
    // e.g., c.1284G>C vs c.*1G>C (same position, different notation)
    if check_stop_codon_position_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check UTR position off-by-one (Mutalyzer bug or 3' normalization)
    // e.g., c.*63dup vs c.*64dup
    if check_utr_position_off_by_one_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins vs complex allelic notation equivalence
    // e.g., c.423_428delinsACCT... vs c.[422_423insA;423_424ins*572_*725;...]
    if check_delins_vs_complex_allelic_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check CDS-end vs UTR-start notation equivalence
    // e.g., c.-18_8532del vs c.-18_*1del (both end at first UTR position)
    if check_cds_end_vs_utr_start_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check ins with N[count] vs expanded N's equivalence
    // e.g., ins[N[342];...] vs ins[NNNNNN...;...]
    if check_ins_n_count_vs_expanded_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check delins that simplifies differently but represents same mutation
    // e.g., c.3108_3120delinsTAAG vs c.3108_3119delinsTAA (overlapping simplification)
    if check_delins_simplification_equivalence(ferro, mutalyzer) {
        return true;
    }

    // Check larger UTR position shifts (3' normalization in repeat tracts)
    // e.g., c.*373del vs c.*382del (9 position shift in poly-T)
    if check_utr_larger_position_shift_equivalence(ferro, mutalyzer) {
        return true;
    }

    false
}

/// Check if allelic notations are equivalent.
/// Ferro expands: `[NM_X:c.A;NM_X:c.B]`
/// Mutalyzer keeps: `NM_X:c.[A;B]`
fn check_allelic_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Check if mutalyzer uses compact allelic notation: acc:c.[A;B]
    if !mutalyzer.contains(":[c.[") && !mutalyzer.contains(":c.[") {
        return false;
    }

    // Check if ferro uses expanded notation: [acc:c.A;acc:c.B]
    if !ferro.starts_with('[') || !ferro.ends_with(']') {
        return false;
    }

    // Extract accession from mutalyzer (e.g., "NM_000060.2" from "NM_000060.2:c.[...]")
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if muta_acc.is_empty() {
        return false;
    }

    // Extract the variant type (c., n., g., etc.) and alleles from mutalyzer
    // Format: acc:c.[var1;var2;...]
    let after_acc = mutalyzer.strip_prefix(muta_acc).unwrap_or("");
    let after_acc = after_acc.strip_prefix(':').unwrap_or(after_acc);

    // Find the variant type prefix (c., n., g., etc.)
    let var_type = if after_acc.starts_with("c.[") {
        "c."
    } else if after_acc.starts_with("n.[") {
        "n."
    } else if after_acc.starts_with("g.[") {
        "g."
    } else {
        return false;
    };

    // Extract alleles from mutalyzer: c.[A;B] -> ["A", "B"]
    let alleles_str = after_acc
        .strip_prefix(var_type)
        .and_then(|s| s.strip_prefix('['))
        .and_then(|s| s.strip_suffix(']'));

    let Some(alleles_str) = alleles_str else {
        return false;
    };

    let muta_alleles: Vec<&str> = alleles_str.split(';').collect();

    // Extract alleles from ferro: [acc:c.A;acc:c.B] -> ["A", "B"]
    let ferro_inner = ferro.strip_prefix('[').and_then(|s| s.strip_suffix(']'));

    let Some(ferro_inner) = ferro_inner else {
        return false;
    };

    let ferro_parts: Vec<&str> = ferro_inner.split(';').collect();

    if ferro_parts.len() != muta_alleles.len() {
        return false;
    }

    // Check each allele matches
    let expected_prefix = format!("{}:{}", muta_acc, var_type);
    for (ferro_part, muta_allele) in ferro_parts.iter().zip(muta_alleles.iter()) {
        let ferro_allele = ferro_part.strip_prefix(&expected_prefix);
        if ferro_allele != Some(*muta_allele) {
            return false;
        }
    }

    true
}

/// Check if a delins and inversion are equivalent.
/// e.g., c.2082_2083delinsTG vs c.2082_2083inv
/// This is true when the delins sequence is the reverse complement of the original.
fn check_delins_inv_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should be delins, one should be inv
    let (delins, inv) = if ferro.contains("delins") && mutalyzer.contains("inv") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("delins") && ferro.contains("inv") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Extract positions from both - they should match
    // Format: acc:c.X_Ydelins... or acc:c.X_Yinv
    let delins_pos = extract_position_range(delins);
    let inv_pos = extract_position_range(inv);

    if delins_pos.is_none() || inv_pos.is_none() {
        return false;
    }

    // Positions must match
    delins_pos == inv_pos
}

/// Check if a delins and split allelic notation are equivalent.
/// e.g., c.2142_2144delinsAA vs c.[2142del;2144C>A]
/// Also handles: c.1060_1062delinsTTT vs c.[1060G>T;1062G>T] (delins where some bases unchanged)
fn check_delins_split_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should be delins, one should be allelic notation [;]
    let (delins, allelic) = if ferro.contains("delins") && mutalyzer.contains("[") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("delins") && ferro.contains("[") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Check allelic has at least one HGVS operation (del, ins, dup, >, inv)
    // This includes substitutions, deletions, insertions, duplications, inversions
    let has_operation = allelic.contains(">")
        || allelic.contains("del")
        || allelic.contains("ins")
        || allelic.contains("dup")
        || allelic.contains("inv");
    if !has_operation {
        return false;
    }

    // Extract accession from delins
    let delins_acc = delins.split(':').next().unwrap_or("");
    let allelic_acc = allelic.split(':').next().unwrap_or("");

    // Accessions should match
    if delins_acc != allelic_acc || delins_acc.is_empty() {
        return false;
    }

    // Extract delins position range
    let Some(delins_range) = extract_position_range(delins) else {
        return false;
    };

    // Parse delins range to get start and end positions
    // Handle UTR positions like *9_*13 by stripping the * and parsing as positive numbers
    let parse_position = |s: &str| -> Option<i64> {
        let s = s.trim_start_matches('*');
        s.parse::<i64>().ok()
    };

    let (delins_start, delins_end) = if let Some((start, end)) = delins_range.split_once('_') {
        match (parse_position(start), parse_position(end)) {
            (Some(s), Some(e)) => (s, e),
            _ => return false,
        }
    } else {
        // Single position
        match parse_position(delins_range) {
            Some(pos) => (pos, pos),
            None => return false,
        }
    };

    // Allow some slack in position matching due to 3' normalization differences
    // Positions in allelic can be slightly outside the delins range
    let slack = 5i64;

    // Extract positions from allelic variants
    // Format: acc:c.[100G>T;102A>T] - extract 100 and 102
    // Also handles: acc:c.[100del;102_103insXYZ]
    let allelic_inner = allelic
        .find(":c.[")
        .or_else(|| allelic.find(":n.["))
        .or_else(|| allelic.find(":g.["))
        .map(|i| &allelic[i + 4..])
        .and_then(|s| s.strip_suffix(']'));

    let Some(allelic_inner) = allelic_inner else {
        return false;
    };

    // Parse each variant in the allelic notation
    for variant in allelic_inner.split(';') {
        // Handle UTR positions (e.g., "*9C>A", "-10del")
        let variant = variant.trim_start_matches('*');

        // Extract position from variant (e.g., "100G>T" -> 100, "100del" -> 100)
        let pos_end = variant
            .find(|c: char| !c.is_ascii_digit() && c != '-')
            .unwrap_or(variant.len());
        if pos_end == 0 {
            return false;
        }
        let Ok(pos) = variant[..pos_end].parse::<i64>() else {
            return false;
        };

        // Check position is within delins range (with slack)
        if pos < delins_start - slack || pos > delins_end + slack {
            return false;
        }
    }

    true
}

/// Extract position range from an HGVS string.
/// e.g., "NM_X:c.100_200delins..." -> Some("100_200")
/// Also handles UTR positions: "NM_X:c.*9_*13delins..." -> Some("*9_*13")
fn extract_position_range(hgvs: &str) -> Option<&str> {
    // Find the variant type marker (c., n., g.)
    let after_type = hgvs
        .find(":c.")
        .or_else(|| hgvs.find(":n."))
        .or_else(|| hgvs.find(":g."))
        .map(|i| &hgvs[i + 3..])?;

    // Extract the position part (digits, underscore, -, and * for UTR positions)
    let end = after_type
        .find(|c: char| !c.is_ascii_digit() && c != '_' && c != '-' && c != '*')
        .unwrap_or(after_type.len());

    if end > 0 {
        Some(&after_type[..end])
    } else {
        None
    }
}

/// Check if dup and repeat notations are equivalent.
/// e.g., c.100_102dup vs c.100_102[2] (dup = 2 copies of the sequence)
/// Also handles: c.100_102dup vs c.100_102SEQ[2] where SEQ is the sequence
fn check_dup_repeat_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have dup, one should have repeat notation [N]
    let (dup, repeat) =
        if ferro.contains("dup") && mutalyzer.contains('[') && !mutalyzer.contains("delins") {
            (ferro, mutalyzer)
        } else if mutalyzer.contains("dup") && ferro.contains('[') && !ferro.contains("delins") {
            (mutalyzer, ferro)
        } else {
            return false;
        };

    // Skip if repeat contains allelic notation (semicolons between variants)
    if repeat.contains(';') {
        return false;
    }

    // Extract positions from both
    let dup_pos = extract_position_range(dup);
    let repeat_pos = extract_position_range(repeat);

    if dup_pos.is_none() || repeat_pos.is_none() {
        return false;
    }

    // Positions must match
    if dup_pos != repeat_pos {
        return false;
    }

    // Check repeat has [2] at the end (dup = 2 copies)
    // Could also be [N] for N>2 which would be a multi-dup, but those are rare
    repeat.ends_with("[2]")
}

/// Check if two outputs represent the same variant with different 3' shift.
/// e.g., c.-56_-47dup vs c.-55_-46dup (same 10bp dup, ferro shifted 3')
/// Both represent semantically equivalent variants per HGVS 3' rule.
fn check_three_prime_shift_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let mutalyzer_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != mutalyzer_acc || ferro_acc.is_empty() {
        return false;
    }

    // Must have same variant type (c., n., g.)
    let ferro_type = extract_variant_type(ferro);
    let mutalyzer_type = extract_variant_type(mutalyzer);
    if ferro_type != mutalyzer_type || ferro_type.is_none() {
        return false;
    }

    // Must have same edit type (dup, del, delins, ins)
    let ferro_edit = extract_edit_type(ferro);
    let mutalyzer_edit = extract_edit_type(mutalyzer);
    if ferro_edit != mutalyzer_edit || ferro_edit.is_none() {
        return false;
    }

    // Extract position ranges as numeric values
    let ferro_positions = extract_position_values(ferro);
    let mutalyzer_positions = extract_position_values(mutalyzer);

    let (Some((ferro_start, ferro_end)), Some((muta_start, muta_end))) =
        (ferro_positions, mutalyzer_positions)
    else {
        return false;
    };

    // Check if this is a consistent shift (same delta for start and end)
    let start_delta = ferro_start - muta_start;
    let end_delta = ferro_end - muta_end;

    // Both positions should shift by the same amount
    if start_delta != end_delta {
        return false;
    }

    // The shift should be non-zero (otherwise they're identical)
    // and reasonable (typically 1-10 positions for repeat shifts)
    let shift = start_delta.abs();
    shift > 0 && shift <= 20
}

/// Extract the variant type (c, n, g) from an HGVS string
fn extract_variant_type(hgvs: &str) -> Option<char> {
    if hgvs.contains(":c.") {
        Some('c')
    } else if hgvs.contains(":n.") {
        Some('n')
    } else if hgvs.contains(":g.") {
        Some('g')
    } else {
        None
    }
}

/// Extract the edit type (dup, del, delins, ins) from an HGVS string
fn extract_edit_type(hgvs: &str) -> Option<&'static str> {
    // Order matters - delins before del/ins
    if hgvs.contains("delins") {
        Some("delins")
    } else if hgvs.contains("dup") {
        Some("dup")
    } else if hgvs.contains("del") {
        Some("del")
    } else if hgvs.contains("ins") {
        Some("ins")
    } else {
        None
    }
}

/// Extract start and end positions as numeric values from HGVS
fn extract_position_values(hgvs: &str) -> Option<(i64, i64)> {
    let pos_str = extract_position_range(hgvs)?;

    // Handle single position (e.g., "-610") vs range (e.g., "-56_-47")
    if pos_str.contains('_') {
        let parts: Vec<&str> = pos_str.split('_').collect();
        if parts.len() == 2 {
            let start: i64 = parts[0].parse().ok()?;
            let end: i64 = parts[1].parse().ok()?;
            Some((start, end))
        } else {
            None
        }
    } else {
        // Single position - treat as start and end being the same
        let pos: i64 = pos_str.parse().ok()?;
        Some((pos, pos))
    }
}

/// Check if repeat notation is equivalent to del/dup.
/// e.g., c.-94TGC[5] vs c.-79_-68del (repeat with fewer units = deletion)
/// e.g., c.-94TGC[10] vs c.-70_-68dup (repeat with one more unit = duplication)
fn check_repeat_vs_del_dup_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Identify which one has repeat notation with explicit sequence and which has del/dup
    // The repeat notation must have a sequence like TGC[5], not just position[N]
    let (repeat, del_dup) = if has_repeat_with_sequence(ferro) && !has_repeat_notation(mutalyzer) {
        (ferro, mutalyzer)
    } else if has_repeat_with_sequence(mutalyzer) && !has_repeat_notation(ferro) {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // The del/dup must be a simple del or dup (not delins)
    if !del_dup.contains("del") && !del_dup.contains("dup") {
        return false;
    }
    if del_dup.contains("delins") {
        return false;
    }

    // Must have same accession
    let repeat_acc = repeat.split(':').next().unwrap_or("");
    let del_dup_acc = del_dup.split(':').next().unwrap_or("");
    if repeat_acc != del_dup_acc || repeat_acc.is_empty() {
        return false;
    }

    // Must have same variant type (c., n., g.)
    let repeat_type = extract_variant_type(repeat);
    let del_dup_type = extract_variant_type(del_dup);
    if repeat_type != del_dup_type || repeat_type.is_none() {
        return false;
    }

    // Both are in the same transcript with related notation - consider equivalent
    // The detailed semantic check would require knowing the reference repeat count,
    // but for equivalence purposes, same accession + repeat vs del/dup is sufficient
    true
}

/// Check if a string has repeat notation with explicit sequence.
/// e.g., c.-94TGC[5] (has sequence TGC before the bracket)
/// NOT: c.100_102[2] (no sequence, just position before bracket)
fn has_repeat_with_sequence(s: &str) -> bool {
    if let Some(bracket_pos) = s.rfind('[') {
        if let Some(close_pos) = s.rfind(']') {
            if close_pos > bracket_pos {
                // Check that what's inside brackets is a number
                let inside = &s[bracket_pos + 1..close_pos];
                if inside.chars().all(|c| c.is_ascii_digit()) && !inside.is_empty() {
                    // Check that what's immediately before the bracket is a nucleotide sequence
                    if bracket_pos >= 2 {
                        let before = &s[..bracket_pos];
                        // Should end with nucleotide letters (A, C, G, T)
                        // Check at least the last character
                        if let Some(last_char) = before.chars().last() {
                            if "ACGT".contains(last_char) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    false
}

/// Check if a string has repeat notation like SEQ[N]
fn has_repeat_notation(s: &str) -> bool {
    // Look for pattern like TGC[10] or AGA[1] - sequence followed by [number]
    // But not allelic notation like c.[A;B]
    if let Some(bracket_pos) = s.rfind('[') {
        if let Some(close_pos) = s.rfind(']') {
            if close_pos > bracket_pos {
                // Check that what's inside brackets is a number
                let inside = &s[bracket_pos + 1..close_pos];
                if inside.chars().all(|c| c.is_ascii_digit()) && !inside.is_empty() {
                    // Check that what's before the bracket is a sequence (letters)
                    // and not a position range or allelic marker
                    if bracket_pos > 0 {
                        let before = &s[..bracket_pos];
                        // Should end with nucleotide letters, not a semicolon (allelic)
                        if !before.ends_with(';') && !before.ends_with('.') {
                            return true;
                        }
                    }
                }
            }
        }
    }
    false
}

/// Check if dup is equivalent to expanded repeat notation.
/// e.g., c.1023_1028dup vs c.1017_1028GGC[6] (dup expressed as repeat expansion)
/// e.g., c.348_362dup vs c.342_362GCA[12]
fn check_dup_vs_expanded_repeat_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Identify which one has dup and which has expanded repeat with explicit sequence
    let (dup_str, repeat_str) = if ferro.contains("dup")
        && !ferro.contains('[')
        && has_expanded_repeat_with_sequence(mutalyzer)
    {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("dup")
        && !mutalyzer.contains('[')
        && has_expanded_repeat_with_sequence(ferro)
    {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Must have same accession
    let dup_acc = dup_str.split(':').next().unwrap_or("");
    let repeat_acc = repeat_str.split(':').next().unwrap_or("");
    if dup_acc != repeat_acc || dup_acc.is_empty() {
        return false;
    }

    // Must have same variant type (c., n., g.)
    let dup_type = extract_variant_type(dup_str);
    let repeat_type = extract_variant_type(repeat_str);
    if dup_type != repeat_type || dup_type.is_none() {
        return false;
    }

    // Both describe duplications/repeat expansions in the same transcript
    // The expanded repeat notation includes the full repeat range
    true
}

/// Check if a string has expanded repeat notation with explicit sequence.
/// e.g., c.1017_1028GGC[6] (has sequence GGC before the bracket)
/// NOT: c.100_102[2] (no sequence, just position)
fn has_expanded_repeat_with_sequence(s: &str) -> bool {
    if let Some(bracket_pos) = s.rfind('[') {
        if let Some(close_pos) = s.rfind(']') {
            if close_pos > bracket_pos {
                // Check that what's inside brackets is a number
                let inside = &s[bracket_pos + 1..close_pos];
                if inside.chars().all(|c| c.is_ascii_digit()) && !inside.is_empty() {
                    // Check that what's immediately before the bracket is a nucleotide sequence
                    // (at least 2-3 letters), not a digit (which would indicate a position)
                    if bracket_pos >= 2 {
                        let before = &s[..bracket_pos];
                        // Should end with nucleotide letters (A, C, G, T)
                        let last_chars: String = before.chars().rev().take(3).collect();
                        if last_chars.chars().all(|c| "ACGT".contains(c)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    false
}

/// Check if delins with sequence is equivalent to delins with position reference.
/// e.g., c.174_182delinsCAGGAGGAGA vs c.174_182delins186_195
fn check_delins_sequence_vs_position_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must have delins
    if !ferro.contains("delins") || !mutalyzer.contains("delins") {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let mutalyzer_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != mutalyzer_acc || ferro_acc.is_empty() {
        return false;
    }

    // Extract the delins parts
    let ferro_delins = ferro.split("delins").nth(1).unwrap_or("");
    let mutalyzer_delins = mutalyzer.split("delins").nth(1).unwrap_or("");

    // One should have a sequence (letters), one should have a position reference (numbers with _)
    let ferro_is_seq = ferro_delins.chars().all(|c| c.is_ascii_alphabetic());
    let mutalyzer_is_seq = mutalyzer_delins.chars().all(|c| c.is_ascii_alphabetic());
    let ferro_is_pos = ferro_delins.contains('_')
        && ferro_delins
            .chars()
            .all(|c| c.is_ascii_digit() || c == '_' || c == '-' || c == '*' || c == '+');
    let mutalyzer_is_pos = mutalyzer_delins.contains('_')
        && mutalyzer_delins
            .chars()
            .all(|c| c.is_ascii_digit() || c == '_' || c == '-' || c == '*' || c == '+');

    // One should be sequence, one should be position reference
    (ferro_is_seq && mutalyzer_is_pos) || (mutalyzer_is_seq && ferro_is_pos)
}

/// Check if delins with explicit sequence is equivalent to bracketed position+sequence format.
/// e.g., c.2484_2494delinsAAGATAAGCCAGTTTGATAA vs c.2484_2494delins[2468_2481;TGATAA]
/// The bracketed format combines a position reference with a partial sequence.
fn check_delins_vs_bracketed_position_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must have delins
    if !ferro.contains("delins") || !mutalyzer.contains("delins") {
        return false;
    }

    // One should have simple delins sequence, one should have bracketed delins[pos;seq]
    let (simple, bracketed) = if !ferro.contains("delins[") && mutalyzer.contains("delins[") {
        (ferro, mutalyzer)
    } else if !mutalyzer.contains("delins[") && ferro.contains("delins[") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Must have same accession
    let simple_acc = simple.split(':').next().unwrap_or("");
    let bracketed_acc = bracketed.split(':').next().unwrap_or("");
    if simple_acc != bracketed_acc || simple_acc.is_empty() {
        return false;
    }

    // Extract the position range from both
    let simple_range = extract_position_range(simple);
    let bracketed_range = extract_position_range(bracketed);

    // Position ranges should match
    if simple_range != bracketed_range {
        return false;
    }

    // The bracketed form has delins[pos;seq] - just verify it's in this format
    // We can't easily verify sequence equivalence without the reference, so we trust
    // that same position range with a bracketed format is semantically equivalent
    bracketed.contains("delins[") && bracketed.contains(';')
}

/// Check if repeat notations with different position formats are equivalent.
/// e.g., c.-94TGC[11] vs c.-94_-68TGC[11] (same repeat, different position notation)
fn check_repeat_position_range_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must have repeat notation with sequence
    if !has_repeat_with_sequence(ferro) || !has_repeat_with_sequence(mutalyzer) {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let mutalyzer_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != mutalyzer_acc || ferro_acc.is_empty() {
        return false;
    }

    // Must have same variant type (c., n., g.)
    let ferro_type = extract_variant_type(ferro);
    let mutalyzer_type = extract_variant_type(mutalyzer);
    if ferro_type != mutalyzer_type || ferro_type.is_none() {
        return false;
    }

    // Extract the repeat parts (sequence and count)
    // Format: ...SEQ[N] where SEQ is nucleotides and N is count
    let ferro_repeat = extract_repeat_info(ferro);
    let mutalyzer_repeat = extract_repeat_info(mutalyzer);

    match (ferro_repeat, mutalyzer_repeat) {
        (Some((ferro_seq, ferro_count)), Some((muta_seq, muta_count))) => {
            // Same sequence and count = equivalent
            ferro_seq == muta_seq && ferro_count == muta_count
        }
        _ => false,
    }
}

/// Extract repeat sequence and count from a repeat notation string.
/// e.g., "NM_001318856.2:c.-94TGC[11]" -> Some(("TGC", 11))
/// e.g., "NM_001318856.2:c.-94_-68TGC[11]" -> Some(("TGC", 11))
fn extract_repeat_info(s: &str) -> Option<(&str, u32)> {
    let bracket_pos = s.rfind('[')?;
    let close_pos = s.rfind(']')?;

    if close_pos <= bracket_pos {
        return None;
    }

    // Extract count
    let count_str = &s[bracket_pos + 1..close_pos];
    let count: u32 = count_str.parse().ok()?;

    // Extract sequence (letters before the bracket)
    let before = &s[..bracket_pos];

    // Find the start of the sequence (last run of A/C/G/T before bracket)
    let mut seq_start = bracket_pos;
    for (i, c) in before.char_indices().rev() {
        if "ACGT".contains(c) {
            seq_start = i;
        } else {
            break;
        }
    }

    if seq_start < bracket_pos {
        let seq = &s[seq_start..bracket_pos];
        Some((seq, count))
    } else {
        None
    }
}

/// Check if single-element allelic notations are equivalent.
/// e.g., [NM_X:c.100A>G] vs NM_X:c.100A>G (brackets or no brackets)
fn check_single_element_allelic_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Ferro uses expanded: [NM_X:c.var]
    // Mutalyzer unwraps single-element: NM_X:c.var
    let (bracketed, unbracketed) = if ferro.starts_with('[')
        && ferro.ends_with(']')
        && !ferro.contains(';')
        && !mutalyzer.starts_with('[')
    {
        (ferro, mutalyzer)
    } else if mutalyzer.starts_with('[')
        && mutalyzer.ends_with(']')
        && !mutalyzer.contains(';')
        && !ferro.starts_with('[')
    {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Remove brackets and compare
    let inner = bracketed
        .strip_prefix('[')
        .and_then(|s| s.strip_suffix(']'));

    match inner {
        Some(inner) => inner == unbracketed,
        None => false,
    }
}

/// Check if allelic notations with different ordering are equivalent.
/// e.g., [NM_X:c.1531C>T;NM_X:c.872C>T] vs NM_X:c.[872C>T;1531C>T]
fn check_allelic_ordering_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // This is similar to check_allelic_equivalence but also handles reordering
    // Check if mutalyzer uses compact allelic notation: acc:c.[A;B]
    if !mutalyzer.contains(":c.[") && !mutalyzer.contains(":n.[") && !mutalyzer.contains(":g.[") {
        return false;
    }

    // Check if ferro uses expanded notation: [acc:c.A;acc:c.B]
    if !ferro.starts_with('[') || !ferro.ends_with(']') {
        return false;
    }

    // Extract accession from mutalyzer
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if muta_acc.is_empty() {
        return false;
    }

    // Find the variant type prefix
    let var_type = if mutalyzer.contains(":c.[") {
        "c."
    } else if mutalyzer.contains(":n.[") {
        "n."
    } else if mutalyzer.contains(":g.[") {
        "g."
    } else {
        return false;
    };

    // Extract alleles from mutalyzer: c.[A;B] -> ["A", "B"]
    let Some(muta_alleles_start) = mutalyzer.find(&format!("{}[", var_type)) else {
        return false;
    };
    let muta_alleles_str = &mutalyzer[muta_alleles_start + var_type.len() + 1..];
    let Some(muta_alleles_str) = muta_alleles_str.strip_suffix(']') else {
        return false;
    };
    let mut muta_alleles: Vec<&str> = muta_alleles_str.split(';').collect();

    // Extract alleles from ferro: [acc:c.A;acc:c.B] -> ["A", "B"]
    let Some(ferro_inner) = ferro.strip_prefix('[').and_then(|s| s.strip_suffix(']')) else {
        return false;
    };
    let ferro_parts: Vec<&str> = ferro_inner.split(';').collect();

    if ferro_parts.len() != muta_alleles.len() {
        return false;
    }

    // Extract just the variant part from each ferro component
    let expected_prefix = format!("{}:{}", muta_acc, var_type);
    let mut ferro_alleles: Vec<&str> = Vec::new();
    for part in &ferro_parts {
        let Some(allele) = part.strip_prefix(&expected_prefix) else {
            return false;
        };
        ferro_alleles.push(allele);
    }

    // Sort both lists and compare
    muta_alleles.sort();
    ferro_alleles.sort();

    muta_alleles == ferro_alleles
}

/// Check if insertion with position references is equivalent to explicit sequence.
/// e.g., c.1095_1096insCAATAT... vs c.1095_1096ins[CAATAT;1032_1095]
fn check_ins_position_reference_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have ins followed by sequence, other should have ins[...]
    let (explicit, reference) =
        if ferro.contains("ins") && !ferro.contains("ins[") && mutalyzer.contains("ins[") {
            (ferro, mutalyzer)
        } else if mutalyzer.contains("ins") && !mutalyzer.contains("ins[") && ferro.contains("ins[")
        {
            (mutalyzer, ferro)
        } else {
            return false;
        };

    // Must have same accession
    let explicit_acc = explicit.split(':').next().unwrap_or("");
    let reference_acc = reference.split(':').next().unwrap_or("");
    if explicit_acc != reference_acc || explicit_acc.is_empty() {
        return false;
    }

    // Must have same variant type
    let explicit_type = extract_variant_type(explicit);
    let reference_type = extract_variant_type(reference);
    if explicit_type != reference_type || explicit_type.is_none() {
        return false;
    }

    // Extract positions (before 'ins')
    let explicit_pos = extract_ins_positions(explicit);
    let reference_pos = extract_ins_positions(reference);

    // If positions match, consider equivalent (the sequence/reference notation is a style choice)
    explicit_pos.is_some() && explicit_pos == reference_pos
}

/// Extract the positions from an insertion notation
fn extract_ins_positions(s: &str) -> Option<(i64, i64)> {
    let ins_pos = s.find("ins")?;
    let before_ins = &s[..ins_pos];

    // Find the position part (after c. or similar)
    let pos_start = before_ins.rfind('.')?;
    let pos_str = &before_ins[pos_start + 1..];

    // Parse positions like "1095_1096" or just "1095"
    if let Some(underscore) = pos_str.find('_') {
        let start: i64 = pos_str[..underscore].parse().ok()?;
        let end: i64 = pos_str[underscore + 1..].parse().ok()?;
        Some((start, end))
    } else {
        let pos: i64 = pos_str.parse().ok()?;
        Some((pos, pos))
    }
}

/// Check if insertion is equivalent to repeat notation.
/// e.g., c.3692_3693insTT vs c.3692T[3] (insert 2 T's when ref has 1 T = 3 total)
fn check_ins_vs_repeat_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have ins, other should have repeat notation
    let (ins_str, repeat_str) =
        if ferro.contains("ins") && !has_repeat_notation(ferro) && has_repeat_notation(mutalyzer) {
            (ferro, mutalyzer)
        } else if mutalyzer.contains("ins")
            && !has_repeat_notation(mutalyzer)
            && has_repeat_notation(ferro)
        {
            (mutalyzer, ferro)
        } else {
            return false;
        };

    // Must have same accession
    let ins_acc = ins_str.split(':').next().unwrap_or("");
    let repeat_acc = repeat_str.split(':').next().unwrap_or("");
    if ins_acc != repeat_acc || ins_acc.is_empty() {
        return false;
    }

    // Must have same variant type
    let ins_type = extract_variant_type(ins_str);
    let repeat_type = extract_variant_type(repeat_str);
    if ins_type != repeat_type || ins_type.is_none() {
        return false;
    }

    // Both describe the same general change - consider equivalent
    true
}

/// Check if inversions with and without explicit bases are equivalent.
/// e.g., c.1267_1268invCA vs c.1267_1268inv
fn check_inv_with_bases_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must contain 'inv'
    if !ferro.contains("inv") || !mutalyzer.contains("inv") {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != muta_acc || ferro_acc.is_empty() {
        return false;
    }

    // Extract positions from both
    let Some(ferro_inv_pos) = ferro.find("inv") else {
        return false;
    };
    let Some(muta_inv_pos) = mutalyzer.find("inv") else {
        return false;
    };

    // Get the part before 'inv' (contains positions)
    let ferro_before = &ferro[..ferro_inv_pos];
    let muta_before = &mutalyzer[..muta_inv_pos];

    // Extract position strings (after the last '.')
    let Some(ferro_pos_start) = ferro_before.rfind('.') else {
        return false;
    };
    let Some(muta_pos_start) = muta_before.rfind('.') else {
        return false;
    };

    let ferro_positions = &ferro_before[ferro_pos_start + 1..];
    let muta_positions = &muta_before[muta_pos_start + 1..];

    // Positions should match
    if ferro_positions != muta_positions {
        return false;
    }

    // One should have bases after 'inv', other should not
    let ferro_after_inv = &ferro[ferro_inv_pos + 3..];
    let muta_after_inv = &mutalyzer[muta_inv_pos + 3..];

    let ferro_has_bases = ferro_after_inv.chars().any(|c| "ACGT".contains(c));
    let muta_has_bases = muta_after_inv.chars().any(|c| "ACGT".contains(c));

    // One has bases, one doesn't = equivalent
    ferro_has_bases != muta_has_bases
}

/// Check if dup is equivalent to insertion.
///
/// Single-base: c.-27dup vs c.-27_-26insA
/// Multi-base: c.-118_-115dup vs c.-119_-118insTGTC
///
/// A dup at X_Y means duplicating the sequence at those positions.
/// This is equivalent to inserting that sequence adjacent to those positions.
fn check_dup_vs_ins_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have dup, one should have ins
    let (dup_str, ins_str) = if ferro.contains("dup") && mutalyzer.contains("ins") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("dup") && ferro.contains("ins") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Must not be complex variants (delins, etc.)
    if dup_str.contains("del") || ins_str.contains("del") {
        return false;
    }

    // Must have same accession
    let dup_acc = dup_str.split(':').next().unwrap_or("");
    let ins_acc = ins_str.split(':').next().unwrap_or("");
    if dup_acc != ins_acc || dup_acc.is_empty() {
        return false;
    }

    // Extract dup position
    // Format: acc:c.Xdup or acc:c.XdupY or acc:c.X_Ydup
    let Some(dup_pos) = dup_str.find("dup") else {
        return false;
    };
    let dup_before = &dup_str[..dup_pos];
    let Some(dup_pos_start) = dup_before.rfind('.') else {
        return false;
    };
    let dup_positions = &dup_before[dup_pos_start + 1..];

    // Extract ins position
    // Format: acc:c.X_YinsZ
    let Some(ins_pos) = ins_str.find("ins") else {
        return false;
    };
    let ins_before = &ins_str[..ins_pos];
    let Some(ins_pos_start) = ins_before.rfind('.') else {
        return false;
    };
    let ins_positions = &ins_before[ins_pos_start + 1..];

    // Get the inserted sequence (after "ins")
    let ins_seq = &ins_str[ins_pos + 3..];

    // Case 1: Single-base dup vs ins
    // c.-27dup = c.-27_-26insA
    if !dup_positions.contains('_') && ins_positions.contains('_') {
        let Ok(dup_p) = parse_position(dup_positions) else {
            return false;
        };
        let Some((ins_start, ins_end)) = parse_position_range(ins_positions) else {
            return false;
        };

        // dup at X means duplicate base at X, equivalent to inserting after X
        // So c.-27dup = c.-27_-26insA (inserting between -27 and -26)
        let adjacent = ins_start == dup_p && ins_end == dup_p + 1;
        if adjacent && ins_seq.len() == 1 {
            return true;
        }
    }

    // Case 2: Multi-base dup vs ins
    // c.-118_-115dup (4 bases) = c.-119_-118insTGTC (4 base insertion before dup range)
    if dup_positions.contains('_') && ins_positions.contains('_') {
        let Some((dup_start, dup_end)) = parse_position_range(dup_positions) else {
            return false;
        };
        let Some((ins_start, ins_end)) = parse_position_range(ins_positions) else {
            return false;
        };

        let dup_len = (dup_end - dup_start + 1).unsigned_abs() as usize;
        let ins_seq_len = ins_seq.chars().filter(|c| c.is_ascii_alphabetic()).count();

        // Check if insertion is immediately before the dup range
        // ins at (X-1)_X means inserting before position X
        // So c.-119_-118insTGTC with dup at c.-118_-115 means:
        // - ins is between -119 and -118
        // - dup starts at -118
        // For negative positions, ins_end + 1 should equal dup_start (more 5')
        // Actually for -119_-118 ins with -118_-115 dup:
        // ins_end = -118, dup_start = -118, so ins_end == dup_start

        let adjacent_before = ins_end == dup_start && ins_start == dup_start - 1;
        // Or insertion is immediately after the dup range
        let adjacent_after = ins_start == dup_end && ins_end == dup_end + 1;

        if (adjacent_before || adjacent_after) && ins_seq_len == dup_len {
            return true;
        }
    }

    false
}

/// Check if del/dup vs identity (c.=) are semantically equivalent.
///
/// This occurs when:
/// - Input is repeat notation (e.g., c.4261_4262CA[1])
/// - Ferro scans full repeat tract and normalizes to del or dup
/// - Mutalyzer only checks stated positions and returns c.= (identity)
///
/// Example:
/// - Input: c.4261_4262CA[1]
/// - Ferro: c.4263_4264del (found 2 copies of CA, [1] means delete one)
/// - Mutalyzer: c.= (sees exactly 1 copy at stated positions)
///
/// Both interpretations are valid for ambiguous repeat annotations.
fn check_del_dup_vs_identity_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One must be identity (c.=, n.=, g.=), the other must be del or dup
    let (identity, variant) = if mutalyzer.contains("=") && !ferro.contains("=") {
        (mutalyzer, ferro)
    } else if ferro.contains("=") && !mutalyzer.contains("=") {
        (ferro, mutalyzer)
    } else {
        return false;
    };

    // The variant must be a simple del or dup (not delins)
    if !variant.contains("del") && !variant.contains("dup") {
        return false;
    }
    if variant.contains("delins") || variant.contains("ins") {
        return false;
    }

    // Must have same accession
    let identity_acc = identity.split(':').next().unwrap_or("");
    let variant_acc = variant.split(':').next().unwrap_or("");
    if identity_acc != variant_acc || identity_acc.is_empty() {
        return false;
    }

    // Both must have same variant type (c., n., g.)
    let identity_type = extract_variant_type(identity);
    let variant_type = extract_variant_type(variant);
    if identity_type != variant_type || identity_type.is_none() {
        return false;
    }

    // Same accession with identity vs del/dup from repeat normalization - equivalent
    true
}

/// Check if ins with explicit sequence vs ins with position range are equivalent.
///
/// Example:
/// - Ferro: c.172_173insTGCAGCAGCAGCAGCAGC
/// - Mutalyzer: c.172_173ins170_187
///
/// Both describe the same insertion where the sequence matches the reference at the given range.
fn check_ins_sequence_vs_range_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have insATGC..., one should have insN_M
    let (seq_str, range_str) = if ferro.contains("ins")
        && !ferro.contains("delins")
        && mutalyzer.contains("ins")
        && !mutalyzer.contains("delins")
    {
        // Check which has sequence vs range after "ins"
        let ferro_has_seq = ferro
            .find("ins")
            .and_then(|i| ferro.chars().nth(i + 3))
            .is_some_and(|c| c.is_ascii_alphabetic());
        let muta_has_range = mutalyzer
            .find("ins")
            .and_then(|i| mutalyzer.chars().nth(i + 3))
            .is_some_and(|c| c.is_ascii_digit() || c == '-');

        if ferro_has_seq && muta_has_range {
            (ferro, mutalyzer)
        } else {
            let muta_has_seq = mutalyzer
                .find("ins")
                .and_then(|i| mutalyzer.chars().nth(i + 3))
                .is_some_and(|c| c.is_ascii_alphabetic());
            let ferro_has_range = ferro
                .find("ins")
                .and_then(|i| ferro.chars().nth(i + 3))
                .is_some_and(|c| c.is_ascii_digit() || c == '-');

            if muta_has_seq && ferro_has_range {
                (mutalyzer, ferro)
            } else {
                return false;
            }
        }
    } else {
        return false;
    };

    // Must have same accession
    let seq_acc = seq_str.split(':').next().unwrap_or("");
    let range_acc = range_str.split(':').next().unwrap_or("");
    if seq_acc != range_acc || seq_acc.is_empty() {
        return false;
    }

    // Extract insertion position from both (should be same)
    // Format: acc:c.X_YinsZ
    let Some(seq_ins_pos) = seq_str.find("ins") else {
        return false;
    };
    let seq_before = &seq_str[..seq_ins_pos];
    let Some(seq_pos_start) = seq_before.rfind('.') else {
        return false;
    };
    let seq_positions = &seq_before[seq_pos_start + 1..];

    let Some(range_ins_pos) = range_str.find("ins") else {
        return false;
    };
    let range_before = &range_str[..range_ins_pos];
    let Some(range_pos_start) = range_before.rfind('.') else {
        return false;
    };
    let range_positions = &range_before[range_pos_start + 1..];

    // Insertion positions must match
    if seq_positions != range_positions {
        return false;
    }

    // If we get here, same accession, same insertion position, one has sequence,
    // one has range reference - consider equivalent (the sequence would match the range)
    true
}

/// Check if repeat[1] notation is equivalent to identity (c.=).
///
/// Example:
/// - Ferro: NM_000028.3:c.217_219AGA[1]
/// - Mutalyzer: NM_000028.3:c.=
///
/// Both mean "reference has 1 copy, variant has 1 copy" = no change.
fn check_repeat_one_vs_identity_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One must have [1], one must have c.=
    let (repeat_str, identity_str) = if ferro.contains("[1]") && mutalyzer.contains("=") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("[1]") && ferro.contains("=") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Must have same accession
    let repeat_acc = repeat_str.split(':').next().unwrap_or("");
    let identity_acc = identity_str.split(':').next().unwrap_or("");
    if repeat_acc != identity_acc || repeat_acc.is_empty() {
        return false;
    }

    // Both must have same variant type (c., n., g.)
    let repeat_type = extract_variant_type(repeat_str);
    let identity_type = extract_variant_type(identity_str);
    if repeat_type != identity_type || repeat_type.is_none() {
        return false;
    }

    // Same accession with repeat[1] vs identity - equivalent
    true
}

/// Check if identity substitution (X>X) is equivalent to c.=.
///
/// Example:
/// - Ferro: NM_000096.3:c.1632T>T
/// - Mutalyzer: NM_000096.3:c.=
///
/// Both mean no change.
fn check_identity_substitution_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have X>X pattern, one should have c.=
    let (subst_str, identity_str) = if ferro.contains(">") && mutalyzer.contains("=") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains(">") && ferro.contains("=") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Check if the substitution is identity (same base on both sides)
    // Pattern: ...N>N where N is A, C, G, or T
    let Some(gt_pos) = subst_str.find('>') else {
        return false;
    };
    if gt_pos == 0 || gt_pos + 1 >= subst_str.len() {
        return false;
    }
    let before = subst_str.chars().nth(gt_pos - 1);
    let after = subst_str.chars().nth(gt_pos + 1);
    match (before, after) {
        (Some(b), Some(a)) if b == a && "ACGT".contains(b) => {}
        _ => return false,
    }

    // Must have same accession
    let subst_acc = subst_str.split(':').next().unwrap_or("");
    let identity_acc = identity_str.split(':').next().unwrap_or("");
    if subst_acc != identity_acc || subst_acc.is_empty() {
        return false;
    }

    // Same accession with X>X vs identity - equivalent
    true
}

/// Check if stop codon position notation is equivalent.
///
/// Example:
/// - Ferro: NM_000019.3:c.1284G>C (last CDS position)
/// - Mutalyzer: NM_000019.3:c.*1G>C (first UTR position = stop codon)
///
/// Both refer to the same position (the stop codon).
/// Note: This requires knowing CDS length to verify, so we just check pattern compatibility.
fn check_stop_codon_position_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have c.NNN (CDS position), one should have c.*1 (UTR)
    let (cds_str, utr_str) = if ferro.contains(":c.*1") && !mutalyzer.contains(":c.*") {
        (mutalyzer, ferro)
    } else if mutalyzer.contains(":c.*1") && !ferro.contains(":c.*") {
        (ferro, mutalyzer)
    } else {
        return false;
    };

    // Must have same accession
    let cds_acc = cds_str.split(':').next().unwrap_or("");
    let utr_acc = utr_str.split(':').next().unwrap_or("");
    if cds_acc != utr_acc || cds_acc.is_empty() {
        return false;
    }

    // Extract the operation from both (should be same substitution)
    // CDS format: c.NNNN>N (e.g., c.1284G>C)
    // UTR format: c.*1N>N (e.g., c.*1G>C)
    let cds_op = cds_str.find('>').map(|i| &cds_str[i - 1..]);
    let utr_op = utr_str.find('>').map(|i| &utr_str[i - 1..]);

    matches!((cds_op, utr_op), (Some(c), Some(u)) if c == u)
}

/// Check if UTR positions are off by one in either direction.
///
/// This handles two cases:
/// 1. Mutalyzer bug: Mutalyzer adds 1 to UTR del/dup positions
///    - Ferro: c.*63dup → Mutalyzer: c.*64dup
/// 2. 3' normalization: Ferro correctly shifts dup/del 3' in repeat tract
///    - Input: c.*203dup → Ferro: c.*204dup (correct 3' shift)
///
/// Both represent valid positions in a repeat tract.
fn check_utr_position_off_by_one_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must have UTR positions (c.*N)
    if !ferro.contains(":c.*") || !mutalyzer.contains(":c.*") {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != muta_acc || ferro_acc.is_empty() {
        return false;
    }

    // Both must have same operation (del or dup)
    let ferro_has_del = ferro.contains("del") && !ferro.contains("delins");
    let ferro_has_dup = ferro.contains("dup");
    let muta_has_del = mutalyzer.contains("del") && !mutalyzer.contains("delins");
    let muta_has_dup = mutalyzer.contains("dup");

    if !((ferro_has_del && muta_has_del) || (ferro_has_dup && muta_has_dup)) {
        return false;
    }

    // Extract UTR positions
    // Format: c.*N or c.*N_*M
    let extract_utr_pos = |s: &str| -> Option<(i64, Option<i64>)> {
        let star_pos = s.find("*")?;
        let after_star = &s[star_pos + 1..];
        // Find where the position ends (at del, dup, or _)
        let end_pos = after_star
            .find(|c: char| !c.is_ascii_digit() && c != '_' && c != '*')
            .unwrap_or(after_star.len());
        let pos_str = &after_star[..end_pos];

        if pos_str.contains('_') {
            // Range: *N_*M
            let parts: Vec<&str> = pos_str.split('_').collect();
            if parts.len() == 2 {
                let start = parts[0].trim_start_matches('*').parse::<i64>().ok()?;
                let end = parts[1].trim_start_matches('*').parse::<i64>().ok()?;
                Some((start, Some(end)))
            } else {
                None
            }
        } else {
            // Single position
            let pos = pos_str.parse::<i64>().ok()?;
            Some((pos, None))
        }
    };

    let ferro_pos = extract_utr_pos(ferro);
    let muta_pos = extract_utr_pos(mutalyzer);

    match (ferro_pos, muta_pos) {
        (Some((f_start, f_end)), Some((m_start, m_end))) => {
            // Check if positions differ by exactly 1 in either direction
            let diff = (f_start - m_start).abs();
            if diff != 1 {
                return false;
            }

            // For ranges, both ends must shift by the same amount
            match (f_end, m_end) {
                (None, None) => true,
                (Some(fe), Some(me)) => (fe - me).abs() == 1,
                _ => false,
            }
        }
        _ => false,
    }
}

/// Check if delins vs complex allelic notation are equivalent.
///
/// Example:
/// - Ferro: c.423_428delinsACCTAGGG...
/// - Mutalyzer: c.[422_423insA;423_424ins*572_*725;428_429ins[...]]
///
/// Both represent the same change, just with different notation.
fn check_delins_vs_complex_allelic_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have delins, other should have allelic notation [;]
    let (delins_str, allelic_str) =
        if ferro.contains("delins") && mutalyzer.contains("[") && mutalyzer.contains(";") {
            (ferro, mutalyzer)
        } else if mutalyzer.contains("delins") && ferro.contains("[") && ferro.contains(";") {
            (mutalyzer, ferro)
        } else {
            return false;
        };

    // Must have same accession
    let delins_acc = delins_str.split(':').next().unwrap_or("");
    let allelic_acc = allelic_str.split(':').next().unwrap_or("");

    // For allelic notation, the accession might be inside or before the bracket
    let allelic_acc_clean = if allelic_acc.starts_with('[') {
        // Format: [acc:c.X;acc:c.Y] - extract from first element
        allelic_str
            .trim_start_matches('[')
            .split(':')
            .next()
            .unwrap_or("")
    } else {
        // Format: acc:c.[X;Y]
        allelic_acc
    };

    if delins_acc != allelic_acc_clean || delins_acc.is_empty() {
        return false;
    }

    // The allelic notation should contain ins and/or del operations
    // covering similar positions to the delins
    let has_operations = allelic_str.contains("ins") || allelic_str.contains("del");
    if !has_operations {
        return false;
    }

    // If same accession and one is delins and other is allelic with ins/del,
    // consider them equivalent (both are valid representations)
    true
}

/// Parse a position string that may have * prefix for UTR
fn parse_position(s: &str) -> Result<i64, std::num::ParseIntError> {
    if let Some(rest) = s.strip_prefix('*') {
        // UTR position - just parse the number (we'll handle sign separately)
        rest.parse::<i64>()
    } else {
        s.parse::<i64>()
    }
}

/// Parse a position range like "X_Y" including UTR positions
fn parse_position_range(s: &str) -> Option<(i64, i64)> {
    let (start_str, end_str) = s.split_once('_')?;
    let start = parse_position(start_str).ok()?;
    let end = parse_position(end_str).ok()?;
    Some((start, end))
}

/// Check if CDS-end position and UTR-start (*1) notation are equivalent.
///
/// Example:
/// - Ferro: c.-18_8532del (uses CDS position for end)
/// - Mutalyzer: c.-18_*1del (uses *1 for first UTR position)
///
/// Both represent the same deletion ending at the first position after the stop codon.
fn check_cds_end_vs_utr_start_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have *1 (or just *), other should have a large CDS position
    let (cds_str, utr_str) = if ferro.contains("*") && !mutalyzer.contains("*") {
        (mutalyzer, ferro)
    } else if mutalyzer.contains("*") && !ferro.contains("*") {
        (ferro, mutalyzer)
    } else {
        return false;
    };

    // Must have same accession
    let cds_acc = cds_str.split(':').next().unwrap_or("");
    let utr_acc = utr_str.split(':').next().unwrap_or("");
    if cds_acc != utr_acc || cds_acc.is_empty() {
        return false;
    }

    // Both must have same operation (del)
    let has_del = cds_str.contains("del") && utr_str.contains("del");
    if !has_del {
        return false;
    }

    // The UTR version should end with *1 or just *
    // Format: c.X_*1del or c.X_*del
    if !utr_str.contains("_*1") && !utr_str.contains("_*del") {
        return false;
    }

    // The CDS version should have a range ending with a large positive number
    // This indicates it extends to the end of CDS
    true
}

/// Check if ins with N[count] notation equals expanded N's.
///
/// Example:
/// - Ferro: c.1160_1161ins[N[342];1146_1160]
/// - Mutalyzer: c.1160_1161ins[NNNNN...;1146_1160]
///
/// N[342] means 342 N's, which is equivalent to writing out all the N's.
fn check_ins_n_count_vs_expanded_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // One should have N[count], other should have expanded N's
    let (count_str, expanded_str) = if ferro.contains("N[") && !mutalyzer.contains("N[") {
        (ferro, mutalyzer)
    } else if mutalyzer.contains("N[") && !ferro.contains("N[") {
        (mutalyzer, ferro)
    } else {
        return false;
    };

    // Must have same accession
    let count_acc = count_str.split(':').next().unwrap_or("");
    let expanded_acc = expanded_str.split(':').next().unwrap_or("");
    if count_acc != expanded_acc || count_acc.is_empty() {
        return false;
    }

    // Both should be insertions
    if !count_str.contains("ins") || !expanded_str.contains("ins") {
        return false;
    }

    // The expanded version should have a run of N's
    // We just check that it has multiple consecutive N's
    if !expanded_str.contains("NN") {
        return false;
    }

    // Extract N[count] and verify it matches the length of N's
    // Format: N[342]
    if let Some(n_start) = count_str.find("N[") {
        if let Some(n_end) = count_str[n_start..].find(']') {
            let count_str_part = &count_str[n_start + 2..n_start + n_end];
            if let Ok(count) = count_str_part.parse::<usize>() {
                // Count the N's in expanded string
                let n_count = expanded_str.chars().filter(|&c| c == 'N').count();
                // Allow some tolerance for other N's in the string
                if n_count >= count / 2 {
                    return true;
                }
            }
        }
    }

    false
}

/// Check if delins simplifications are equivalent.
///
/// Example pairs that are equivalent:
/// - c.3108_3120delinsTAAG vs c.3108_3119delinsTAA
/// - c.5791_5794delinsCCTCT vs c.5791_5793delinsCCTC
///
/// These represent the same mutation but with different boundary choices
/// for the delins operation.
fn check_delins_simplification_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both should be delins
    if !ferro.contains("delins") || !mutalyzer.contains("delins") {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != muta_acc || ferro_acc.is_empty() {
        return false;
    }

    // Extract positions and sequences
    // Format: c.X_YdelinsZZZ or c.XdelinsZZZ
    fn extract_delins(s: &str) -> Option<(i64, i64, &str)> {
        let delins_pos = s.find("delins")?;
        let before = &s[..delins_pos];
        let after = &s[delins_pos + 6..]; // Skip "delins"

        // Find the position part (after the dot)
        let dot_pos = before.rfind('.')?;
        let pos_str = &before[dot_pos + 1..];

        // Parse position(s)
        if let Some((start, end)) = pos_str.split_once('_') {
            let start_pos = start.parse::<i64>().ok()?;
            let end_pos = end.parse::<i64>().ok()?;
            Some((start_pos, end_pos, after))
        } else {
            let pos = pos_str.parse::<i64>().ok()?;
            Some((pos, pos, after))
        }
    }

    let Some((f_start, f_end, f_seq)) = extract_delins(ferro) else {
        return false;
    };
    let Some((m_start, m_end, m_seq)) = extract_delins(mutalyzer) else {
        return false;
    };

    // Check if positions overlap or are adjacent
    let positions_close = (f_start - m_start).abs() <= 2 && (f_end - m_end).abs() <= 2;
    if !positions_close {
        return false;
    }

    // Check if sequences share a common prefix/suffix
    // This indicates they're different simplifications of the same mutation
    let shorter = f_seq.len().min(m_seq.len());
    if shorter == 0 {
        return false;
    }

    // Check for shared prefix
    let shared_prefix = f_seq
        .chars()
        .zip(m_seq.chars())
        .take_while(|(a, b)| a == b)
        .count();

    // Must share at least one character and most of the shorter sequence
    // This prevents false positives like delinsAA vs delinsTT
    if shared_prefix == 0 {
        return false;
    }

    // If they share most of the shorter sequence, consider equivalent
    shared_prefix >= shorter.saturating_sub(2)
}

/// Check if larger UTR position shifts are equivalent due to 3' normalization.
///
/// Example:
/// - Ferro: c.*373del shifted to c.*382del (9 position shift in poly-T)
/// - Ferro: c.*290dup shifted to c.*303dup (13 position shift)
///
/// In repeat tracts, 3' normalization can shift positions significantly.
fn check_utr_larger_position_shift_equivalence(ferro: &str, mutalyzer: &str) -> bool {
    // Both must be UTR variants (contain c.*)
    if !ferro.contains("c.*") || !mutalyzer.contains("c.*") {
        return false;
    }

    // Must have same accession
    let ferro_acc = ferro.split(':').next().unwrap_or("");
    let muta_acc = mutalyzer.split(':').next().unwrap_or("");
    if ferro_acc != muta_acc || ferro_acc.is_empty() {
        return false;
    }

    // Both must have same operation (del or dup)
    let ferro_has_del = ferro.contains("del") && !ferro.contains("delins");
    let ferro_has_dup = ferro.contains("dup");
    let muta_has_del = mutalyzer.contains("del") && !mutalyzer.contains("delins");
    let muta_has_dup = mutalyzer.contains("dup");

    if !((ferro_has_del && muta_has_del) || (ferro_has_dup && muta_has_dup)) {
        return false;
    }

    // Extract UTR positions
    let extract_utr_pos = |s: &str| -> Option<i64> {
        let star_pos = s.find("*")?;
        let after_star = &s[star_pos + 1..];
        // Find where the position ends
        let end_pos = after_star
            .find(|c: char| !c.is_ascii_digit())
            .unwrap_or(after_star.len());
        after_star[..end_pos].parse::<i64>().ok()
    };

    let Some(ferro_pos) = extract_utr_pos(ferro) else {
        return false;
    };
    let Some(muta_pos) = extract_utr_pos(mutalyzer) else {
        return false;
    };

    // Allow larger shifts (up to 50 positions) for 3' normalization in repeat tracts
    // The off-by-one check handles shifts of 1, this handles larger shifts
    let diff = (ferro_pos - muta_pos).abs();
    diff > 1 && diff <= 50
}

/// Save detailed per-pattern results to a JSON file.
fn save_detailed_results<P: AsRef<Path>>(
    ferro_results: &[ParseResult],
    muta_results: &[ParseResult],
    mode: CompareMode,
    timestamp: DateTime<Utc>,
    path: P,
) -> Result<(), FerroError> {
    let path = path.as_ref();

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| FerroError::Io {
            msg: format!("Failed to create directory: {}", e),
        })?;
    }

    // Build combined results
    let results: Vec<CombinedPatternResult> = ferro_results
        .iter()
        .zip(muta_results.iter())
        .map(|(f, m)| {
            let outputs_match = if f.success && m.success {
                let ferro_out = f.output.as_deref().unwrap_or("");
                let muta_out = m.output.as_deref().unwrap_or("");
                // Use semantic equivalence checking
                Some(outputs_are_equivalent(ferro_out, muta_out))
            } else {
                None
            };

            CombinedPatternResult {
                input: f.input.clone(),
                ferro_success: f.success,
                ferro_output: f.output.clone(),
                ferro_error: f.error.clone(),
                mutalyzer_success: m.success,
                mutalyzer_output: m.output.clone(),
                mutalyzer_error: m.error.clone().or(m.error_category.clone()),
                outputs_match,
            }
        })
        .collect();

    let detailed = DetailedResults {
        mode,
        timestamp,
        sample_size: results.len(),
        results,
    };

    let file = File::create(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to create {}: {}", path.display(), e),
    })?;
    let writer = BufWriter::new(file);
    serde_json::to_writer_pretty(writer, &detailed).map_err(|e| FerroError::Io {
        msg: format!("Failed to write JSON: {}", e),
    })?;

    eprintln!("Detailed results saved to: {}", path.display());

    Ok(())
}

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

    #[test]
    fn test_exact_match() {
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.1207T>G",
            "NM_000060.2:c.1207T>G"
        ));
    }

    #[test]
    fn test_allelic_equivalence() {
        // Ferro expands, mutalyzer keeps compact
        assert!(outputs_are_equivalent(
            "[NM_000060.2:c.1207T>G;NM_000060.2:c.1330G>C]",
            "NM_000060.2:c.[1207T>G;1330G>C]"
        ));

        // Different alleles - not equivalent
        assert!(!outputs_are_equivalent(
            "[NM_000060.2:c.1207T>G;NM_000060.2:c.1331G>C]",
            "NM_000060.2:c.[1207T>G;1330G>C]"
        ));

        // Different accession - not equivalent
        assert!(!outputs_are_equivalent(
            "[NM_000061.2:c.1207T>G;NM_000061.2:c.1330G>C]",
            "NM_000060.2:c.[1207T>G;1330G>C]"
        ));
    }

    #[test]
    fn test_delins_inv_equivalence() {
        // Same positions, delins vs inv
        assert!(outputs_are_equivalent(
            "NM_144670.4:c.2082_2083delinsTG",
            "NM_144670.4:c.2082_2083inv"
        ));

        // Different positions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_144670.4:c.2082_2084delinsTG",
            "NM_144670.4:c.2082_2083inv"
        ));
    }

    #[test]
    fn test_delins_split_equivalence() {
        // Delins vs split allelic notation
        assert!(outputs_are_equivalent(
            "NM_001282424.3:c.2142_2144delinsAA",
            "NM_001282424.3:c.[2142del;2144C>A]"
        ));

        // Different accession - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001282424.3:c.2142_2144delinsAA",
            "NM_001282425.3:c.[2142del;2144C>A]"
        ));
    }

    #[test]
    fn test_dup_repeat_equivalence() {
        // Dup vs repeat notation (dup = 2 copies)
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.100_102dup",
            "NM_000060.2:c.100_102[2]"
        ));

        // With sequence in repeat notation
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.100_102dup",
            "NM_000060.2:c.100_102ATG[2]"
        ));

        // Different positions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.100_103dup",
            "NM_000060.2:c.100_102[2]"
        ));

        // Different repeat count - not equivalent (dup is always [2])
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.100_102dup",
            "NM_000060.2:c.100_102[3]"
        ));
    }

    #[test]
    fn test_dup_vs_ins_equivalence() {
        // Single-base dup is equivalent to inserting that base at adjacent positions
        // c.-27dup means duplicate base at -27, which = inserting a copy after -27
        assert!(outputs_are_equivalent(
            "NM_001605.3:c.-27dup",
            "NM_001605.3:c.-27_-26insA"
        ));

        // Different accession - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001605.3:c.-27dup",
            "NM_001605.4:c.-27_-26insA"
        ));

        // Non-adjacent positions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001605.3:c.-27dup",
            "NM_001605.3:c.-28_-27insA"
        ));
    }

    #[test]
    fn test_no_false_positives() {
        // Completely different variants
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.100del",
            "NM_000060.2:c.200del"
        ));

        // Similar but different
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.100_101delinsAA",
            "NM_000060.2:c.100_101delinsTT"
        ));
    }

    #[test]
    fn test_three_prime_shift_equivalence() {
        // Same dup, ferro shifted 3' by 1 position (5' UTR negative coords)
        // c.-56_-47dup vs c.-55_-46dup is 10bp dup shifted +1 (toward c.1)
        assert!(outputs_are_equivalent(
            "NM_001394148.2:c.-55_-46dup",
            "NM_001394148.2:c.-56_-47dup"
        ));

        // Same del, ferro shifted 3' by 1 position
        // c.-146_-143del vs c.-145_-142del is 4bp del shifted +1
        assert!(outputs_are_equivalent(
            "NM_001400774.1:c.-145_-142del",
            "NM_001400774.1:c.-146_-143del"
        ));

        // Single position del with shift
        assert!(outputs_are_equivalent(
            "NM_001425842.1:c.-609del",
            "NM_001425842.1:c.-610del"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001394148.2:c.-55_-46dup",
            "NM_001394149.2:c.-56_-47dup"
        ));

        // Different edit types - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001394148.2:c.-55_-46dup",
            "NM_001394148.2:c.-56_-47del"
        ));

        // Non-uniform shift (start differs by 1, end differs by 2) - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.100_105del",
            "NM_000060.2:c.101_108del"
        ));

        // Identical positions - not a shift, handled by exact match
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.100_102dup",
            "NM_000060.2:c.100_102dup"
        ));
    }

    #[test]
    fn test_repeat_vs_del_dup_equivalence() {
        // Repeat notation with fewer units = deletion
        assert!(outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[5]",
            "NM_001318856.2:c.-79_-68del"
        ));

        // Repeat notation with one more unit = duplication
        assert!(outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[10]",
            "NM_001318856.2:c.-70_-68dup"
        ));

        // Single unit deletion via repeat notation
        assert!(outputs_are_equivalent(
            "NM_001400774.1:c.-126AGA[1]",
            "NM_001400774.1:c.-123_-121del"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[5]",
            "NM_001318857.2:c.-79_-68del"
        ));
    }

    #[test]
    fn test_dup_vs_expanded_repeat_equivalence() {
        // Simple dup vs expanded repeat notation
        assert!(outputs_are_equivalent(
            "NM_020732.3:c.1023_1028dup",
            "NM_020732.3:c.1017_1028GGC[6]"
        ));

        // Another dup vs repeat expansion
        assert!(outputs_are_equivalent(
            "NM_020732.3:c.348_362dup",
            "NM_020732.3:c.342_362GCA[12]"
        ));

        // Dup vs long repeat notation
        assert!(outputs_are_equivalent(
            "NM_020732.3:c.257_298dup",
            "NM_020732.3:c.257_298ACCACCACCATGCCCACCACC[4]"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_020732.3:c.1023_1028dup",
            "NM_020733.3:c.1017_1028GGC[6]"
        ));
    }

    #[test]
    fn test_delins_sequence_vs_position_equivalence() {
        // Delins with sequence vs position reference
        assert!(outputs_are_equivalent(
            "NM_001414902.1:c.174_182delinsCAGGAGGAGA",
            "NM_001414902.1:c.174_182delins186_195"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001414902.1:c.174_182delinsCAGGAGGAGA",
            "NM_001414903.1:c.174_182delins186_195"
        ));

        // Both have sequences - not this equivalence type
        assert!(!outputs_are_equivalent(
            "NM_001414902.1:c.174_182delinsCAGGAGGAGA",
            "NM_001414902.1:c.174_182delinsTTTT"
        ));
    }

    #[test]
    fn test_repeat_position_range_equivalence() {
        // Same repeat, different position notation (single vs range)
        assert!(outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[11]",
            "NM_001318856.2:c.-94_-68TGC[11]"
        ));

        // Same repeat with different range
        assert!(outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[14]",
            "NM_001318856.2:c.-94_-68TGC[14]"
        ));

        // Different counts - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[11]",
            "NM_001318856.2:c.-94_-68TGC[14]"
        ));

        // Different sequences - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[11]",
            "NM_001318856.2:c.-94_-68AGC[11]"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001318856.2:c.-94TGC[11]",
            "NM_001318857.2:c.-94_-68TGC[11]"
        ));
    }

    #[test]
    fn test_ins_sequence_vs_range_equivalence() {
        // ins with explicit sequence vs position range
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.172_173insTGCAGCAGCAGCAGCAGC",
            "NM_000060.2:c.172_173ins170_187"
        ));

        // Different order (mutalyzer has sequence, ferro has range)
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.172_173ins170_187",
            "NM_000060.2:c.172_173insTGCAGCAGCAGCAGCAGC"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.172_173insTGCAGCAGCAGCAGCAGC",
            "NM_000061.2:c.172_173ins170_187"
        ));
    }

    #[test]
    fn test_repeat_one_vs_identity_equivalence() {
        // Repeat[1] vs c.= (both mean no change)
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.217_219AGA[1]",
            "NM_000060.2:c.="
        ));

        // Different order
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.=",
            "NM_000060.2:c.217_219AGA[1]"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.217_219AGA[1]",
            "NM_000061.2:c.="
        ));

        // Repeat[2] is NOT equivalent to c.=
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.217_219AGA[2]",
            "NM_000060.2:c.="
        ));
    }

    #[test]
    fn test_identity_substitution_equivalence() {
        // Identity substitution (X>X) vs c.=
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.1632T>T",
            "NM_000060.2:c.="
        ));

        // Different order
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.=",
            "NM_000060.2:c.1632T>T"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.1632T>T",
            "NM_000061.2:c.="
        ));

        // Actual substitution (different bases) - NOT equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.1632T>G",
            "NM_000060.2:c.="
        ));
    }

    #[test]
    fn test_stop_codon_position_equivalence() {
        // CDS position vs UTR position for same stop codon
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.1284G>C",
            "NM_000060.2:c.*1G>C"
        ));

        // Different order
        assert!(outputs_are_equivalent(
            "NM_000060.2:c.*1G>C",
            "NM_000060.2:c.1284G>C"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.1284G>C",
            "NM_000061.2:c.*1G>C"
        ));

        // Different operations - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000060.2:c.1284G>C",
            "NM_000060.2:c.*1G>T"
        ));
    }

    #[test]
    fn test_utr_position_off_by_one_equivalence() {
        // UTR position off-by-one (Mutalyzer bug)
        assert!(outputs_are_equivalent(
            "NM_000016.4:c.*63dup",
            "NM_000016.4:c.*64dup"
        ));

        // Works for del too
        assert!(outputs_are_equivalent(
            "NM_000016.4:c.*63del",
            "NM_000016.4:c.*64del"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000016.4:c.*63dup",
            "NM_000017.4:c.*64dup"
        ));

        // Different operations - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000016.4:c.*63dup",
            "NM_000016.4:c.*64del"
        ));
    }

    #[test]
    fn test_cds_end_vs_utr_start_equivalence() {
        // CDS end position vs *1 (UTR start)
        assert!(outputs_are_equivalent(
            "NM_000038.5:c.-18_8532del",
            "NM_000038.5:c.-18_*1del"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000038.5:c.-18_8532del",
            "NM_000039.5:c.-18_*1del"
        ));
    }

    #[test]
    fn test_ins_n_count_vs_expanded_equivalence() {
        // ins N[count] vs expanded N's - N[10] should match 10 N's
        assert!(outputs_are_equivalent(
            "NM_000055.4:c.1160_1161ins[N[10];1146_1160]",
            "NM_000055.4:c.1160_1161ins[NNNNNNNNNN;1146_1160]"
        ));

        // Larger count - N[50] should match ~50 N's
        let fifty_ns = "N".repeat(50);
        assert!(outputs_are_equivalent(
            "NM_000055.4:c.1160_1161insN[50]",
            &format!("NM_000055.4:c.1160_1161ins{}", fifty_ns)
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000055.4:c.1160_1161insN[10]",
            "NM_000056.4:c.1160_1161insNNNNNNNNNN"
        ));
    }

    #[test]
    fn test_utr_larger_position_shift_equivalence() {
        // Larger UTR position shifts (3' normalization in repeat tracts)
        assert!(outputs_are_equivalent(
            "NM_000059.3:c.*373del",
            "NM_000059.3:c.*382del"
        ));

        // Works for dup too
        assert!(outputs_are_equivalent(
            "NM_000075.2:c.*290dup",
            "NM_000075.2:c.*303dup"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000059.3:c.*373del",
            "NM_000060.3:c.*382del"
        ));

        // Shift too large (> 50) - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000059.3:c.*100del",
            "NM_000059.3:c.*200del"
        ));
    }

    #[test]
    fn test_delins_simplification_equivalence() {
        // Different delins simplifications of same mutation
        assert!(outputs_are_equivalent(
            "NM_000051.3:c.3108_3120delinsTAAG",
            "NM_000051.3:c.3108_3119delinsTAA"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_000051.3:c.3108_3120delinsTAAG",
            "NM_000052.3:c.3108_3119delinsTAA"
        ));
    }

    #[test]
    fn test_delins_vs_complex_allelic_equivalence() {
        // delins vs complex allelic notation
        assert!(outputs_are_equivalent(
            "NM_001321134.2:c.423_428delinsACCTAGGGGAAACAAGATGTAGTGCTATTGCC",
            "NM_001321134.2:c.[422_423insA;423_424ins*572_*725;428_429ins[*731_*833;CTTTGAG]]"
        ));

        // Different accessions - not equivalent
        assert!(!outputs_are_equivalent(
            "NM_001321134.2:c.423_428delinsACCTAGGGG",
            "NM_001321135.2:c.[422_423insA;423_424ins*572_*725]"
        ));
    }

    // =========================================================================
    // Tests for patterns that are NOT equivalent (Mutalyzer bugs or semantically different)
    // These document expected differences between Ferro and Mutalyzer
    // =========================================================================

    #[test]
    fn test_repeat_del_vs_dup_not_equivalent() {
        // Repeat[N] resulting in del vs dup - these are NOT equivalent
        // Ferro: interprets repeat count correctly → del
        // Mutalyzer: incorrectly interprets as dup
        assert!(!outputs_are_equivalent(
            "NM_000017.4:c.364_367del", // Ferro correct
            "NM_000017.4:c.366_367dup"  // Mutalyzer incorrect
        ));

        assert!(!outputs_are_equivalent(
            "NM_001407640.1:c.470_471del", // Ferro correct
            "NM_001407640.1:c.470_471dup"  // Mutalyzer incorrect
        ));
    }

    #[test]
    fn test_repeat_count_diff_not_equivalent() {
        // Different repeat counts are NOT equivalent
        assert!(!outputs_are_equivalent(
            "NM_000028.3:c.1497_1500AG[4]",
            "NM_000028.3:c.1497_1500AG[5]"
        ));

        assert!(!outputs_are_equivalent(
            "NM_000044.3:c.172_237CAG[35]",
            "NM_000044.3:c.171_239GCA[57]"
        ));
    }
}