dnacomb 1.0.0

Count the occurances of structured sequence reads and compare to an expected library
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
//! Compiled expected-sequence libraries and lookup logic.
//!
//! This module defines:
//! - [`Library`], which coordinates lookup across one or more independent
//!   sublibraries,
//! - [`SubLibrary`], which stores expected sequences for a specific subset of
//!   variable regions,
//! - and the distance-based matching logic used to compare observed region
//!   sequences to those expected libraries.
//!
//! A single library TSV defines one `SubLibrary`. Multiple TSVs can be combined
//! into a top-level `Library` to represent combinatorial designs where each TSV
//! constrains one independent subset of regions.
use bio::alignment::distance;
use bio::bio_types::sequence::Sequence;
use clap::ValueEnum;
use csv::ReaderBuilder;
use itertools::Itertools;
use std::cmp;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use crate::errors::LibraryError;
use crate::interning::{
    LibraryID, RegionID, SeqHandle, library_id_from_str, region_id_from_str, seq_from_bytes,
    seq_to_bytes,
};
use crate::lib_spec::LibrarySpec;

/// Compiled expected-sequence library spanning one or more sublibraries.
///
/// A `Library` dispatches region lookups to the appropriate [`SubLibrary`] based
/// on region ID. This allows independent library TSVs to define separate parts of
/// a combinatorial design while presenting a single lookup interface.
#[derive(Debug, Clone)]
pub struct Library {
    pub regions: HashMap<RegionID, usize>,
    pub sublibraries: Vec<SubLibrary>,
}

impl Library {
    /// Construct a top-level library from compiled sublibraries.
    ///
    /// Each region may belong to at most one sublibrary. An error is returned if
    /// the same region appears in multiple sublibraries.
    pub fn new(sublibraries: Vec<SubLibrary>) -> Result<Self, LibraryError> {
        let mut regions = HashMap::new();

        for (i, lib) in sublibraries.iter().enumerate() {
            let new_regions = lib.regions();

            for r in new_regions {
                if regions.insert(r.clone(), i).is_some() {
                    return Err(LibraryError::DuplicateSubLibraryRegion { id: r.clone() });
                }
            }
        }

        Ok(Self {
            regions,
            sublibraries,
        })
    }

    /// Return true if the library contains no SubLibraries
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.sublibraries.is_empty()
    }

    /// Return the sublibrary index responsible for a given region ID.
    pub fn get_sublibrary_index(&self, region: &RegionID) -> Result<usize, LibraryError> {
        match self.regions.get(region) {
            Some(x) => Ok(*x),
            None => Err(LibraryError::MissingRegion { id: region.clone() }),
        }
    }

    /// Look up an observed sequence against the expected library for one region.
    ///
    /// The region ID determines which sublibrary should be queried. Matching is
    /// then delegated to that sublibrary using the requested distance metric and
    /// partial-matching mode.
    ///
    /// Returns:
    /// - `Ok(Some(...))` for one or more best matches within the allowed distance,
    /// - `Ok(None)` if no acceptable match is found,
    /// - `Err(...)` if the region is not represented in the library.
    pub fn lookup(
        &self,
        region: &RegionID,
        seq: &SeqHandle,
        metric: DistanceMetric,
        partial: PartialMatching,
    ) -> Result<Option<LibraryMatch>, LibraryError> {
        self.sublibraries[self.get_sublibrary_index(region)?].lookup(region, seq, metric, partial)
    }

    /// Build a top-level library from one or more library TSV files.
    ///
    /// Each TSV becomes one independent [`SubLibrary`]. Together these define a
    /// potentially combinatorial expected design, where each region must appear in
    /// at most one input file.
    pub fn from_files(
        paths: &[String],
        lib_spec: &LibrarySpec,
        default_max_distance: u64,
    ) -> Result<Library, LibraryError> {
        let libs: Result<Vec<SubLibrary>, LibraryError> = paths
            .iter()
            .enumerate()
            .map(|(i, x)| {
                SubLibrary::from_file_with_lib_spec(
                    x,
                    lib_spec,
                    default_max_distance,
                    Some(format!("lib{i}")),
                )
            })
            .collect();

        Library::new(libs?)
    }
}

/// Compiled expected-sequence library for a specific subset of variable regions.
///
/// A `SubLibrary` stores the expected sequence combinations from one library TSV
/// and supports efficient lookup of observed sequences against the unique
/// sequences present in each region.
#[derive(Debug, Clone)]
pub struct SubLibrary {
    /// Full sequences for each member of the library, divided into region vectors. The full nth
    /// sequence contains the nth sequence from each region vector
    pub library: HashMap<RegionID, Vec<Arc<LibraryRegion>>>,

    /// Unique sequences for each region, mapping back to which full combinations they are part
    /// of by index plus a copy of the real sequence to avoid hitting hte interning during the hot loop
    regions: HashMap<RegionID, Vec<LibrarySequence>>,

    /// Library member IDs
    pub ids: Vec<LibraryID>,

    /// HashMap of exact hits to Library regions for quick initial lookup and
    /// exact matching
    exact_matches: HashMap<RegionID, HashMap<SeqHandle, Arc<LibraryRegion>>>,

    /// Max distance to consider for each region
    region_max_distance: HashMap<RegionID, u64>,

    /// Default max distance to consider
    default_max_distance: u64,
}

/// A LibraryRegion and it's paired real sequence
///
/// Storing the sequence raw avoids hitting the interner during the hot loop
#[derive(Debug, Eq, PartialEq, Clone)]
struct LibrarySequence {
    region: Arc<LibraryRegion>,
    sequence: Sequence,
}

impl LibrarySequence {
    fn from_region(region: Arc<LibraryRegion>) -> Self {
        Self {
            sequence: seq_to_bytes(&region.sequence).to_vec(),
            region,
        }
    }
}

/// One unique expected sequence for a region within a compiled sublibrary.
///
/// A `LibraryRegion` stores the canonical sequence plus the set of library-member
/// indices and IDs that contain that sequence. This allows repeated identical
/// region sequences across multiple library members to be represented once.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct LibraryRegion {
    /// The sequence
    pub sequence: SeqHandle,

    /// The indeces of library members that contain this sequence
    pub inds: HashSet<usize>,

    /// The library members that contain this sequence
    pub ids: HashSet<LibraryID>,
}

impl Hash for LibraryRegion {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.sequence.hash(state);
        let mut i = self.inds.iter().copied().collect::<Vec<usize>>();

        i.sort_unstable();

        i.hash(state);
    }
}

/// Best-match result for a region lookup.
///
/// Contains all equally good best matches and the distance shared by those matches.
#[derive(Debug)]
pub struct LibraryMatch {
    pub matches: Vec<Arc<LibraryRegion>>,
    pub distance: u64,
}

/// Intersect two partial region-match results.
///
/// This is used when a region is observed only as two partial pieces, for example
/// from opposite ends of a read pair. Only library-region matches consistent with
/// both partial observations are retained.
///
/// Distances are summed across the two partial matches. This is appropriate when
/// the partial observations cover distinct parts of the region, but may
/// overcount if they overlap.
pub fn merge_matches(x: Option<LibraryMatch>, y: Option<LibraryMatch>) -> Option<LibraryMatch> {
    match (x, y) {
        (None, _) | (_, None) => None,
        (Some(x), Some(y)) => {
            let matches: Vec<Arc<LibraryRegion>> = x
                .matches
                .iter()
                .filter(|m| y.matches.contains(m))
                .cloned()
                .collect();

            if matches.is_empty() {
                None
            } else {
                Some(LibraryMatch {
                    matches,
                    distance: x.distance + y.distance,
                })
            }
        }
    }
}

impl SubLibrary {
    /// Compile a sublibrary from per-region expected sequences.
    ///
    /// The input `library` maps each region ID to the full column of expected
    /// sequences from one library TSV. All region vectors must have the same
    /// length, representing the same ordered library members.
    ///
    /// During compilation this:
    /// - validates dimensions and IDs,
    /// - assigns library-member IDs,
    /// - deduplicates identical region sequences,
    /// - builds exact-match lookup maps,
    /// - and reconstructs full per-member region assignments.
    pub fn new(
        library: HashMap<RegionID, Vec<Sequence>>,
        ids: Option<Vec<String>>,
        region_max_distance: HashMap<RegionID, u64>,
        default_max_distance: u64,
        default_id: Option<String>,
    ) -> Result<SubLibrary, LibraryError> {
        let mut regions: HashMap<RegionID, Vec<LibrarySequence>> = HashMap::new();

        let mut exp_len: usize = 0;

        // This allows an empty library
        if !library.is_empty() {
            exp_len = library
                .values()
                .next()
                .expect("Just checked library has at least 1 elements")
                .len();

            if !library.values().all(|x| x.len() == exp_len) {
                return Err(LibraryError::Library {
                    desc: "Library must contain the same number of sequences for each region"
                        .to_string(),
                });
            }

            if let Some(i) = &ids
                && i.len() != exp_len
            {
                return Err(LibraryError::Library {
                    desc: "Library must have as many IDs as the number of elements".to_string(),
                });
            }
        }

        let baseid = default_id.unwrap_or("seq".to_string());
        let lib_ids: Vec<LibraryID> = match ids {
            Some(ids) => ids.iter().map(|x| library_id_from_str(x)).collect(),
            None => (0..exp_len)
                .map(|x| library_id_from_str(&format!("{baseid}_{x}")))
                .collect(),
        };

        // Check if Libary IDs are unique
        if lib_ids.iter().collect::<HashSet<_>>().len() != exp_len {
            return Err(LibraryError::Library {
                desc: "Library IDs are not unique".to_string(),
            });
        }

        for key in library.keys() {
            let mut reg_map: HashMap<SeqHandle, HashSet<usize>> = HashMap::new();
            let seqs = match library.get(key) {
                Some(x) => x,
                None => {
                    return Err(LibraryError::Library {
                        desc: "Library sequence vec missing unexpectedly during compilation"
                            .to_string(),
                    });
                }
            };

            for (ind, seq) in seqs.iter().enumerate() {
                match reg_map.get_mut(&seq_from_bytes(seq)) {
                    Some(x) => {
                        x.insert(ind);
                    }
                    None => {
                        reg_map.insert(seq_from_bytes(seq), HashSet::from([ind]));
                    }
                }
            }

            if regions
                .insert(
                    key.clone(),
                    Vec::from_iter(
                        reg_map
                            .into_iter()
                            .map(|x| {
                                LibrarySequence::from_region(Arc::new(LibraryRegion {
                                    sequence: x.0,
                                    ids: x.1.iter().map(|x| lib_ids[*x].clone()).collect(),
                                    inds: x.1,
                                }))
                            })
                            .sorted_by_key(|x| x.sequence.clone()),
                    ),
                )
                .is_some()
            {
                return Err(LibraryError::DuplicateRegion { id: key.clone() });
            }
        }

        // Compile Exact matches HashMap
        let mut exact_matches: HashMap<RegionID, HashMap<SeqHandle, Arc<LibraryRegion>>> =
            HashMap::new();
        for key in regions.keys() {
            exact_matches.insert(key.clone(), HashMap::new());
            for reg in regions
                .get(key)
                .expect("Key known to be in regions HashMap")
            {
                exact_matches
                    .get_mut(key)
                    .expect("Key just added to exact_matchs")
                    .insert(reg.region.sequence.clone(), reg.region.clone());
            }
        }

        // Reconstruct library with links to correct region Rcs
        let mut library_compiled = HashMap::new();

        for (key, seqs) in library {
            let mut rc_vec: Vec<Option<Arc<LibraryRegion>>> = vec![None; seqs.len()];

            for reg in regions
                .get(&key)
                .expect("regions should contain key as just inserted")
            {
                for ind in &reg.region.inds {
                    rc_vec[*ind] = Some(reg.region.clone());
                }
            }

            library_compiled.insert(
                key,
                rc_vec.into_iter().map(
                    |x| x.expect("All library members should have been assigned Some(Arc<LibraryRegion>) by construction")).collect()
            );
        }

        Ok(SubLibrary {
            library: library_compiled,
            regions,
            ids: lib_ids,
            exact_matches,
            region_max_distance,
            default_max_distance,
        })
    }

    /// Number of library members in this sublibrary.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        if self.library.is_empty() {
            return 0;
        }

        self.library
            .values()
            .next()
            .expect("Returned previously if library empty")
            .len()
    }

    /// Return `true` if this sublibrary contains no library members.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.library.is_empty()
    }

    /// Return the region IDs represented in this sublibrary.
    pub fn regions(&self) -> Vec<&RegionID> {
        self.regions.keys().collect()
    }

    /// Look up an observed sequence against one region of this sublibrary.
    ///
    /// Matching proceeds in two stages:
    /// 1. exact-match lookup is attempted first,
    /// 2. if needed, the requested distance metric is used to find the best
    ///    sequence(s) within the configured maximum distance.
    ///
    /// `partial` controls whether the query must match the full library sequence
    /// or only one end of it. This is used for incompletely observed regions,
    /// such as truncation at the 5' or 3' end of a read.
    ///
    /// Returns the best match set and its distance, or `None` if no match lies
    /// within the allowed threshold.
    ///
    /// `DistanceMetric::BoundedLevenshtein` uses the configured maximum distance
    /// as an upper bound and is generally equivalent to Levenshtein for accepted
    /// matches while often being faster.
    ///
    /// The implementation dispatches to the appropriate lookup funciton based on distance matric
    /// and partial match type. We use the SIMD optimised versions of Hamming and Levenshtein
    /// distance if AVX2 and SSE4.1 are available at compile time. The Rust Bio SIMD distance
    /// metrics fall back to standard versions if SIMD isn't available so this is just a minor
    /// optimisation and either version should be portable without undefined behaviour. Bounded
    /// levenshtein is only available as a SIMD implementation with fallback so we rely on
    /// the Rust Bio and editdistancek authors for the check.
    pub fn lookup(
        &self,
        region: &RegionID,
        seq: &SeqHandle,
        metric: DistanceMetric,
        partial: PartialMatching,
    ) -> Result<Option<LibraryMatch>, LibraryError> {
        // Try exact matching first - short circuit if we find the region
        if let Some(exact) = self.exact_matches.get(region)
            && let Some(hit) = exact.get(seq)
        {
            return Ok(Some(LibraryMatch {
                matches: vec![hit.clone()],
                distance: 0,
            }));
        }

        // Else try lookup
        let regions: &Vec<LibrarySequence> = match self.regions.get(region) {
            Some(x) => x,
            None => {
                return Err(LibraryError::MissingRegion { id: region.clone() });
            }
        };

        let max_dist = match self.region_max_distance.get(region) {
            None => self.default_max_distance,
            Some(x) => *x,
        };

        let (hits, best_dist) = match (metric, partial) {
            (DistanceMetric::Exact, PartialMatching::Full) => {
                // Already checked, if reached here no exact match
                return Ok(None);
            }
            (DistanceMetric::Exact, PartialMatching::FivePrimeOnly) => {
                // Exact FivePrimeOnly is the same as Hamming lookup on 5' end with 0 dist
                Self::lookup_hamming_5prime(seq_to_bytes(seq).as_ref(), regions, 0)
            }
            (DistanceMetric::Exact, PartialMatching::ThreePrimeOnly) => {
                // Exact ThreePrimeOnly is the same as Hamming lookup on 3' end with 0 dist
                Self::lookup_hamming_3prime(seq_to_bytes(seq).as_ref(), regions, 0)
            }

            (DistanceMetric::Hamming, PartialMatching::Full) => {
                Self::lookup_hamming(seq_to_bytes(seq).as_ref(), regions, max_dist)
            }
            (DistanceMetric::Hamming, PartialMatching::FivePrimeOnly) => {
                Self::lookup_hamming_5prime(seq_to_bytes(seq).as_ref(), regions, max_dist)
            }
            (DistanceMetric::Hamming, PartialMatching::ThreePrimeOnly) => {
                Self::lookup_hamming_3prime(seq_to_bytes(seq).as_ref(), regions, max_dist)
            }

            (DistanceMetric::Levenshtein, PartialMatching::Full) => {
                Self::lookup_levenshtein(seq_to_bytes(seq).as_ref(), regions, max_dist as u32)
            }
            (DistanceMetric::Levenshtein, PartialMatching::FivePrimeOnly) => {
                Self::lookup_levenshtein_5prime(
                    seq_to_bytes(seq).as_ref(),
                    regions,
                    max_dist as u32,
                )
            }
            (DistanceMetric::Levenshtein, PartialMatching::ThreePrimeOnly) => {
                Self::lookup_levenshtein_3prime(
                    seq_to_bytes(seq).as_ref(),
                    regions,
                    max_dist as u32,
                )
            }

            (DistanceMetric::BoundedLevenshtein, PartialMatching::Full) => {
                Self::lookup_bounded_levenshtein(
                    seq_to_bytes(seq).as_ref(),
                    regions,
                    max_dist as u32,
                )
            }
            (DistanceMetric::BoundedLevenshtein, PartialMatching::FivePrimeOnly) => {
                Self::lookup_bounded_levenshtein_5prime(
                    seq_to_bytes(seq).as_ref(),
                    regions,
                    max_dist as u32,
                )
            }
            (DistanceMetric::BoundedLevenshtein, PartialMatching::ThreePrimeOnly) => {
                Self::lookup_bounded_levenshtein_3prime(
                    seq_to_bytes(seq).as_ref(),
                    regions,
                    max_dist as u32,
                )
            }
        };

        if hits.is_empty() {
            return Ok(None);
        }

        Ok(Some(LibraryMatch {
            matches: hits,
            distance: best_dist,
        }))
    }

    /// Compare an observed sequence to the library via Hamming distance
    fn lookup_hamming(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u64,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u64;
        let mut best_dist: u64 = u64::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();

        for reg in regions.iter() {
            // Hamming distance only applicaple for matching length, ignore
            // non-matching lengths
            if seq.len() != reg.sequence.len() {
                continue;
            }

            // Use appropriate hamming implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::hamming(seq, &reg.sequence);
            } else {
                dist = distance::hamming(seq, &reg.sequence);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }

            if best_dist == 0 {
                break;
            }
        }

        (hits, best_dist)
    }

    /// Compare an observed sequence to the library via Hamming distance at the library sequences
    /// 5 prime end
    fn lookup_hamming_5prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u64,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u64;
        let mut best_dist: u64 = u64::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            // Hamming distance only defined for equal length - if query is
            // longer than region discard
            if query_len > reg.sequence.len() {
                continue;
            }

            // Use appropriate hamming implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::hamming(seq, &reg.sequence[0..query_len]);
            } else {
                dist = distance::hamming(seq, &reg.sequence[0..query_len]);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }
        }

        (hits, best_dist)
    }

    /// Compare an observed sequence to the library via Hamming distance at the library sequences
    /// 3 prime end
    fn lookup_hamming_3prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u64,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u64;
        let mut best_dist: u64 = u64::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            let end = reg.sequence.len();
            let start = end.saturating_sub(query_len);

            // Hamming distance only defined for equal length - if query is
            // different length than region discard
            if end - start != seq.len() {
                continue;
            }

            // Use appropriate hamming implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::hamming(seq, &reg.sequence[start..end]);
            } else {
                dist = distance::hamming(seq, &reg.sequence[start..end]);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }
        }

        (hits, best_dist)
    }

    /// Compare an observed sequence to the library via Levenshtein distance
    fn lookup_levenshtein(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u32;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();

        for reg in regions.iter() {
            // Use appropriate levenshtein implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::levenshtein(seq, &reg.sequence);
            } else {
                dist = distance::levenshtein(seq, &reg.sequence);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }

            if best_dist == 0 {
                break;
            }
        }

        (hits, best_dist as u64)
    }

    /// Compare an observed sequence to the library via Levenshtein distance at the library
    /// sequences 5 prime end
    fn lookup_levenshtein_5prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u32;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            let reg_end = cmp::min(query_len, reg.sequence.len());

            // Use appropriate levenshtein implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::levenshtein(seq, &reg.sequence[0..reg_end]);
            } else {
                dist = distance::levenshtein(seq, &reg.sequence[0..reg_end]);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }
        }

        (hits, best_dist as u64)
    }

    /// Compare an observed sequence to the library via Levenshtein distance at the library
    /// sequences 3 prime end
    fn lookup_levenshtein_3prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: u32;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            let end = reg.sequence.len();
            let start = end.saturating_sub(query_len);

            // Use appropriate levenshtein implemntation (other branch should be
            // pruned at compile time)
            if cfg!(all(target_feature = "avx2", target_feature = "sse4.1")) {
                dist = distance::simd::levenshtein(seq, &reg.sequence[start..end]);
            } else {
                dist = distance::levenshtein(seq, &reg.sequence[start..end]);
            }

            // Ignore too distant seqs - could make custom dist functions that short
            // circuit sooner to squeeze extra performance potentially
            if (dist > max_dist) || (dist > best_dist) {
                continue;
            } else if dist < best_dist {
                hits.clear();
                hits.push(reg.region.clone());
                best_dist = dist;
            } else if dist == best_dist {
                hits.push(reg.region.clone());
            }
        }

        (hits, best_dist as u64)
    }

    /// Compare an observed sequence to the library via Bounded Levenshtein distance
    fn lookup_bounded_levenshtein(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: Option<u32>;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();

        for reg in regions.iter() {
            dist = distance::simd::bounded_levenshtein(
                seq,
                &reg.sequence,
                cmp::min(best_dist, max_dist),
            );

            match dist {
                // Ignore cases where d is over k/max_dist
                None => continue,
                Some(d) if d < best_dist => {
                    hits.clear();
                    hits.push(reg.region.clone());
                    best_dist = d;
                }
                Some(d) if d == best_dist => hits.push(reg.region.clone()),
                Some(_) => continue,
            }

            if best_dist == 0 {
                break;
            }
        }

        (hits, best_dist as u64)
    }

    /// Compare an observed sequence to the library via Bounded Levenshtein distance at the library
    /// sequences 5 prime end
    fn lookup_bounded_levenshtein_5prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: Option<u32>;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            let reg_end = cmp::min(query_len, reg.sequence.len());

            dist = distance::simd::bounded_levenshtein(
                seq,
                &reg.sequence[0..reg_end],
                cmp::min(best_dist, max_dist),
            );

            match dist {
                // Ignore cases where d is over k/max_dist
                None => continue,
                Some(d) if d < best_dist => {
                    hits.clear();
                    hits.push(reg.region.clone());
                    best_dist = d;
                }
                Some(d) if d == best_dist => hits.push(reg.region.clone()),
                Some(_) => continue,
            }
        }

        (hits, best_dist as u64)
    }

    /// Compare an observed sequence to the library via Bounded Levenshtein distance at the library
    /// sequences 3 prime end
    fn lookup_bounded_levenshtein_3prime(
        seq: &[u8],
        regions: &[LibrarySequence],
        max_dist: u32,
    ) -> (Vec<Arc<LibraryRegion>>, u64) {
        let mut dist: Option<u32>;
        let mut best_dist: u32 = u32::MAX;
        let mut hits: Vec<Arc<LibraryRegion>> = Vec::new();
        let query_len = seq.len();

        for reg in regions.iter() {
            let end = reg.sequence.len();
            let start = end.saturating_sub(query_len);

            dist = distance::simd::bounded_levenshtein(
                seq,
                &reg.sequence[start..end],
                cmp::min(best_dist, max_dist),
            );

            match dist {
                // Ignore cases where d is over k/max_dist
                None => continue,
                Some(d) if d < best_dist => {
                    hits.clear();
                    hits.push(reg.region.clone());
                    best_dist = d;
                }
                Some(d) if d == best_dist => hits.push(reg.region.clone()),
                Some(_) => continue,
            }
        }

        (hits, best_dist as u64)
    }

    /// Build a sublibrary from a TSV file and validate it against a `LibrarySpec`.
    ///
    /// This checks that:
    /// - the reserved `_id` name is not used as a variable region in the spec,
    /// - all TSV region columns correspond to variable regions in the spec,
    /// - and per-region max-distance defaults are taken from the spec where present.
    pub fn from_file_with_lib_spec(
        path: &str,
        lib_spec: &LibrarySpec,
        default_max_distance: u64,
        default_id: Option<String>,
    ) -> Result<SubLibrary, LibraryError> {
        let spec_regions = lib_spec.variable_regions();
        let region_max_distance = lib_spec.get_max_distances();

        if lib_spec
            .variable_regions()
            .contains(&region_id_from_str("_id"))
        {
            return Err(LibraryError::Library {
                desc: "Region named '_id'. This is reserved for element names when doing library comparison".to_string(),
            });
        }

        let lib: SubLibrary =
            SubLibrary::from_file(path, region_max_distance, default_max_distance, default_id)?;

        if !lib.library.keys().all(|x| spec_regions.contains(x)) {
            return Err(LibraryError::Library {
                desc: "Library region ids don't match variable LibSpec region ids".to_string(),
            });
        }

        Ok(lib)
    }

    /// Build a sublibrary directly from a library TSV file.
    ///
    /// The TSV must contain one column per region and one row per library member.
    /// An optional `_id` column supplies library-member names; otherwise names are
    /// generated automatically from `default_id`.
    pub fn from_file(
        path: &str,
        region_max_distance: HashMap<RegionID, u64>,
        default_max_distance: u64,
        default_id: Option<String>,
    ) -> Result<SubLibrary, LibraryError> {
        let mut reader = ReaderBuilder::new()
            .delimiter(b'\t')
            .comment(Some(b'#'))
            .trim(csv::Trim::All)
            .from_path(path)?;

        // Prepare a HashMap to store column name to values
        let names = reader.headers()?.clone();
        let mut id_vec: Vec<String> = Vec::new();
        let mut regions: HashMap<RegionID, Vec<Sequence>> = HashMap::new();

        // Initialize empty Vec<Sequence> for each column
        for name in names.iter() {
            if name != "_id" {
                regions.insert(region_id_from_str(name), Vec::new());
            }
        }

        // Iterate through records, appending values to the respective columns
        for result in reader.records() {
            let record = result?;
            for (name, val) in names.iter().zip(record.iter()) {
                if name == "_id" {
                    id_vec.push(val.to_string());
                    continue;
                }

                match regions.get_mut(&region_id_from_str(name)) {
                    None => {
                        return Err(LibraryError::MissingRegion {
                            id: region_id_from_str(name),
                        });
                    }
                    Some(v) => v.push(val.as_bytes().to_vec()),
                }
            }
        }

        let ids = if !id_vec.is_empty() {
            Some(id_vec)
        } else {
            None
        };

        SubLibrary::new(
            regions,
            ids,
            region_max_distance,
            default_max_distance,
            default_id,
        )
    }
}

/// Distance metric used for library lookup.
#[derive(Clone, ValueEnum, Debug, Copy, PartialEq)]
pub enum DistanceMetric {
    /// Require exact sequence equality.
    Exact,

    /// Count substitutions only; lengths must match for full matching.
    Hamming,

    /// Full edit distance allowing substitutions, insertions, and deletions.
    Levenshtein,

    /// Edit distance capped at the configured maximum threshold.
    ///
    /// Usually equivalent to Levenshtein for accepted matches, but often faster.
    BoundedLevenshtein,
}

/// How an observed query sequence should be aligned against a library region.
///
/// Partial matching is used for truncated observed regions. The enum names refer
/// to which end of the *library sequence* must be matched by the query.
#[derive(Clone, Debug, Copy)]
pub enum PartialMatching {
    /// Require the full query to match the full library region.
    Full,

    /// Match the query to the 5' end of the library sequence.
    FivePrimeOnly,

    /// Match the query to the 3' end of the library sequence.
    ThreePrimeOnly,
}

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

    use crate::interning::{library_id_to_str, region_id_to_str};
    use crate::lib_spec::LibrarySpec;

    use std::collections::{HashMap, HashSet};
    use std::fs;
    use std::path::PathBuf;
    use std::str::FromStr;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn seq(s: &[u8]) -> SeqHandle {
        seq_from_bytes(s)
    }

    fn hit_seqs(hit: &LibraryMatch) -> HashSet<String> {
        hit.matches
            .iter()
            .map(|x| x.sequence.to_str_or_log())
            .collect()
    }

    /// Main synthetic sublibrary used for table-driven lookup tests.
    ///
    /// r1 rows:
    /// seq1 = AAAA
    /// seq2 = AAAT
    /// seq3 = TTAA
    /// seq4 = GGAA
    /// seq5 = CCCC
    ///
    /// r2 is included just to keep the sublibrary realistic but lookup tests
    /// below focus on r1.
    fn make_lookup_sublib(default_max_distance: u64) -> SubLibrary {
        let mut map: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        map.insert(
            region_id_from_str("r1"),
            vec![
                b"AAAA".to_vec(),
                b"AAAT".to_vec(),
                b"TTAA".to_vec(),
                b"GGAA".to_vec(),
                b"CCCC".to_vec(),
            ],
        );
        map.insert(
            region_id_from_str("r2"),
            vec![
                b"CCCC".to_vec(),
                b"CCCC".to_vec(),
                b"GGGG".to_vec(),
                b"TTTT".to_vec(),
                b"AAAA".to_vec(),
            ],
        );

        let ids = Some(vec![
            "seq1".to_string(),
            "seq2".to_string(),
            "seq3".to_string(),
            "seq4".to_string(),
            "seq5".to_string(),
        ]);

        SubLibrary::new(map, ids, HashMap::new(), default_max_distance, None).unwrap()
    }

    fn simple_libspec_json() -> String {
        r#"
{
  "id": "test",
  "forward_start_region": "fixed_left",
  "forward_read_length": 100,
  "reverse_start_region": "fixed_right",
  "reverse_read_length": 100,
  "regions": [
    { "id": "fixed_left", "seq_type": "Fixed", "seq": "AAAA" },
    { "id": "r1", "seq_type": "Library", "min_length": 4, "max_length": 4, "max_distance": 1 },
    { "id": "fixed_mid", "seq_type": "Fixed", "seq": "CCCC" },
    { "id": "r2", "seq_type": "Library", "min_length": 4, "max_length": 4 },
    { "id": "fixed_right", "seq_type": "Fixed", "seq": "GGGG" }
  ]
}
        "#
        .trim()
        .to_string()
    }

    fn temp_path(name: &str) -> PathBuf {
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "dnacomb_test_{}_{}_{}",
            std::process::id(),
            n,
            name
        ))
    }

    fn write_temp_file(name: &str, content: &str) -> PathBuf {
        let path = temp_path(name);
        fs::write(&path, content).unwrap();
        path
    }

    #[derive(Debug)]
    struct LookupCase {
        name: &'static str,
        metric: DistanceMetric,
        partial: PartialMatching,
        query: &'static [u8],
        max_dist: u64,
        expected: LookupExpected,
    }

    #[derive(Debug)]
    enum LookupExpected {
        None,
        Hit {
            distance: u64,
            seqs: &'static [&'static str],
        },
    }

    #[test]
    fn lookup_case_table() {
        let cases = vec![
            // ----- Exact full -----
            LookupCase {
                name: "exact full unique hit",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::Full,
                query: b"AAAA",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA"],
                },
            },
            LookupCase {
                name: "exact full miss",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::Full,
                query: b"AAAG",
                max_dist: 2,
                expected: LookupExpected::None,
            },
            // ----- Exact five-prime -----
            LookupCase {
                name: "exact five-prime multimatch on prefix AA",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AA",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "exact five-prime unique hit on prefix CC",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::FivePrimeOnly,
                query: b"CC",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["CCCC"],
                },
            },
            LookupCase {
                name: "exact five-prime miss",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AT",
                max_dist: 2,
                expected: LookupExpected::None,
            },
            // ----- Exact three-prime -----
            LookupCase {
                name: "exact three-prime multimatch on suffix AA",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AA",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "TTAA", "GGAA"],
                },
            },
            LookupCase {
                name: "exact three-prime unique hit on suffix AT",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AT",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAT"],
                },
            },
            LookupCase {
                name: "exact three-prime miss",
                metric: DistanceMetric::Exact,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"CG",
                max_dist: 2,
                expected: LookupExpected::None,
            },
            // ----- Hamming full -----
            LookupCase {
                name: "hamming full unique best hit",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::Full,
                query: b"AAAG",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 1,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "hamming full unique exact hit",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::Full,
                query: b"CCCC",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["CCCC"],
                },
            },
            LookupCase {
                name: "hamming full no hit beyond threshold",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::Full,
                query: b"GGGG",
                max_dist: 1,
                expected: LookupExpected::None,
            },
            // ----- Hamming five-prime -----
            LookupCase {
                name: "hamming five-prime prefix exact multimatch",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "hamming five-prime one-mismatch prefix hit",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AG",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 1,
                    seqs: &["AAAA", "GGAA", "AAAT"],
                },
            },
            LookupCase {
                name: "hamming five-prime query longer than region gives none",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AAAAA",
                max_dist: 1,
                expected: LookupExpected::None,
            },
            // ----- Hamming three-prime -----
            LookupCase {
                name: "hamming three-prime suffix exact multimatch",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "TTAA", "GGAA"],
                },
            },
            LookupCase {
                name: "hamming three-prime one-mismatch suffix hit",
                metric: DistanceMetric::Hamming,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"GA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 1,
                    seqs: &["AAAA", "TTAA", "GGAA"],
                },
            },
            // ----- Levenshtein full -----
            LookupCase {
                name: "levenshtein full deletion from AAAA to AAA",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::Full,
                query: b"AAA",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 1,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "levenshtein full exact hit",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::Full,
                query: b"AAAT",
                max_dist: 2,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAT"],
                },
            },
            LookupCase {
                name: "levenshtein full no hit beyond threshold",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::Full,
                query: b"GGGGGG",
                max_dist: 1,
                expected: LookupExpected::None,
            },
            // ----- Levenshtein five-prime -----
            LookupCase {
                name: "levenshtein five-prime exact short prefix",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "levenshtein five-prime insertion relative to prefix",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AAA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            // ----- Levenshtein three-prime -----
            LookupCase {
                name: "levenshtein three-prime exact short suffix",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "TTAA", "GGAA"],
                },
            },
            LookupCase {
                name: "levenshtein three-prime exact suffix AT",
                metric: DistanceMetric::Levenshtein,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AT",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAT"],
                },
            },
            // ----- Bounded Levenshtein full -----
            LookupCase {
                name: "bounded levenshtein full exact hit",
                metric: DistanceMetric::BoundedLevenshtein,
                partial: PartialMatching::Full,
                query: b"CCCC",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["CCCC"],
                },
            },
            LookupCase {
                name: "bounded levenshtein full one-edit hit",
                metric: DistanceMetric::BoundedLevenshtein,
                partial: PartialMatching::Full,
                query: b"AAA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 1,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            LookupCase {
                name: "bounded levenshtein full none beyond threshold",
                metric: DistanceMetric::BoundedLevenshtein,
                partial: PartialMatching::Full,
                query: b"GGGGGG",
                max_dist: 1,
                expected: LookupExpected::None,
            },
            // ----- Bounded Levenshtein five-prime -----
            LookupCase {
                name: "bounded levenshtein five-prime exact prefix",
                metric: DistanceMetric::BoundedLevenshtein,
                partial: PartialMatching::FivePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "AAAT"],
                },
            },
            // ----- Bounded Levenshtein three-prime -----
            LookupCase {
                name: "bounded levenshtein three-prime exact suffix",
                metric: DistanceMetric::BoundedLevenshtein,
                partial: PartialMatching::ThreePrimeOnly,
                query: b"AA",
                max_dist: 1,
                expected: LookupExpected::Hit {
                    distance: 0,
                    seqs: &["AAAA", "TTAA", "GGAA"],
                },
            },
        ];

        for case in cases {
            let sub = make_lookup_sublib(case.max_dist);

            let got = sub
                .lookup(
                    &region_id_from_str("r1"),
                    &seq(case.query),
                    case.metric,
                    case.partial,
                )
                .unwrap();

            match (case.expected, got) {
                (LookupExpected::None, None) => {}
                (LookupExpected::Hit { distance, seqs }, Some(hit)) => {
                    assert_eq!(hit.distance, distance, "distance mismatch: {}", case.name);

                    let got_seqs = hit_seqs(&hit);
                    let expected_seqs: HashSet<String> =
                        seqs.iter().map(|x| x.to_string()).collect();

                    assert_eq!(got_seqs, expected_seqs, "hit set mismatch: {}", case.name);
                }
                (exp, got) => {
                    panic!(
                        "unexpected lookup result for case {}\nexpected: {:?}\ngot: {:?}",
                        case.name, exp, got
                    );
                }
            }
        }
    }

    #[derive(Debug)]
    struct MergeCase {
        name: &'static str,
        left: Option<LibraryMatch>,
        right: Option<LibraryMatch>,
        expected: MergeExpected,
    }

    #[derive(Debug)]
    enum MergeExpected {
        None,
        Hit {
            distance: u64,
            seqs: &'static [&'static str],
        },
    }

    #[test]
    fn merge_matches_case_table() {
        let reg_a = Arc::new(LibraryRegion {
            sequence: seq(b"AAAA"),
            inds: HashSet::from([0usize]),
            ids: HashSet::from([library_id_from_str("seq1")]),
        });
        let reg_b = Arc::new(LibraryRegion {
            sequence: seq(b"AAAT"),
            inds: HashSet::from([1usize]),
            ids: HashSet::from([library_id_from_str("seq2")]),
        });
        let reg_c = Arc::new(LibraryRegion {
            sequence: seq(b"CCCC"),
            inds: HashSet::from([2usize]),
            ids: HashSet::from([library_id_from_str("seq3")]),
        });

        let cases = vec![
            MergeCase {
                name: "left none",
                left: None,
                right: Some(LibraryMatch {
                    matches: vec![reg_a.clone()],
                    distance: 1,
                }),
                expected: MergeExpected::None,
            },
            MergeCase {
                name: "right none",
                left: Some(LibraryMatch {
                    matches: vec![reg_a.clone()],
                    distance: 1,
                }),
                right: None,
                expected: MergeExpected::None,
            },
            MergeCase {
                name: "empty intersection",
                left: Some(LibraryMatch {
                    matches: vec![reg_a.clone()],
                    distance: 1,
                }),
                right: Some(LibraryMatch {
                    matches: vec![reg_c.clone()],
                    distance: 2,
                }),
                expected: MergeExpected::None,
            },
            MergeCase {
                name: "single overlapping hit",
                left: Some(LibraryMatch {
                    matches: vec![reg_a.clone(), reg_b.clone()],
                    distance: 1,
                }),
                right: Some(LibraryMatch {
                    matches: vec![reg_b.clone()],
                    distance: 2,
                }),
                expected: MergeExpected::Hit {
                    distance: 3,
                    seqs: &["AAAT"],
                },
            },
            MergeCase {
                name: "multiple overlapping hits",
                left: Some(LibraryMatch {
                    matches: vec![reg_a.clone(), reg_b.clone()],
                    distance: 2,
                }),
                right: Some(LibraryMatch {
                    matches: vec![reg_a.clone(), reg_b.clone(), reg_c.clone()],
                    distance: 1,
                }),
                expected: MergeExpected::Hit {
                    distance: 3,
                    seqs: &["AAAA", "AAAT"],
                },
            },
        ];

        for case in cases {
            let got = merge_matches(case.left, case.right);

            match (case.expected, got) {
                (MergeExpected::None, None) => {}
                (MergeExpected::Hit { distance, seqs }, Some(hit)) => {
                    assert_eq!(hit.distance, distance, "distance mismatch: {}", case.name);
                    let got_seqs = hit_seqs(&hit);
                    let expected_seqs: HashSet<String> =
                        seqs.iter().map(|x| x.to_string()).collect();
                    assert_eq!(got_seqs, expected_seqs, "hit set mismatch: {}", case.name);
                }
                (exp, got) => {
                    panic!(
                        "unexpected merge result for case {}\nexpected: {:?}\ngot: {:?}",
                        case.name, exp, got
                    );
                }
            }
        }
    }

    #[test]
    fn sublibrary_new_accepts_empty_library() {
        let sub = SubLibrary::new(HashMap::new(), None, HashMap::new(), 2, None).unwrap();
        assert!(sub.is_empty());
        assert_eq!(sub.len(), 0);
        assert!(sub.regions().is_empty());
    }

    #[test]
    fn sublibrary_new_rejects_mismatched_region_lengths() {
        let mut map: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        map.insert(
            region_id_from_str("r1"),
            vec![b"AAAA".to_vec(), b"CCCC".to_vec()],
        );
        map.insert(region_id_from_str("r2"), vec![b"GGGG".to_vec()]);

        let err = SubLibrary::new(map, None, HashMap::new(), 2, None).unwrap_err();
        assert!(err.to_string().contains("same number of sequences"));
    }

    #[test]
    fn sublibrary_new_rejects_duplicate_ids() {
        let mut map: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        map.insert(
            region_id_from_str("r1"),
            vec![b"AAAA".to_vec(), b"CCCC".to_vec()],
        );

        let err = SubLibrary::new(
            map,
            Some(vec!["x".to_string(), "x".to_string()]),
            HashMap::new(),
            2,
            None,
        )
        .unwrap_err();

        assert!(err.to_string().contains("IDs are not unique"));
    }

    #[test]
    fn library_new_rejects_duplicate_region_across_sublibraries() {
        let mut map1: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        map1.insert(region_id_from_str("r1"), vec![b"AAAA".to_vec()]);
        let sub1 = SubLibrary::new(map1, None, HashMap::new(), 2, Some("a".to_string())).unwrap();

        let mut map2: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        map2.insert(region_id_from_str("r1"), vec![b"CCCC".to_vec()]);
        let sub2 = SubLibrary::new(map2, None, HashMap::new(), 2, Some("b".to_string())).unwrap();

        let err = Library::new(vec![sub1, sub2]).unwrap_err();
        assert!(err.to_string().contains("multiple Libraries"));
    }

    #[test]
    fn library_dispatches_lookup_to_correct_sublibrary() {
        let mut left_map: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        left_map.insert(region_id_from_str("r1"), vec![b"AAAA".to_vec()]);
        let left = SubLibrary::new(
            left_map,
            Some(vec!["left1".to_string()]),
            HashMap::new(),
            1,
            None,
        )
        .unwrap();

        let mut right_map: HashMap<RegionID, Vec<Sequence>> = HashMap::new();
        right_map.insert(region_id_from_str("r2"), vec![b"TTTT".to_vec()]);
        let right = SubLibrary::new(
            right_map,
            Some(vec!["right1".to_string()]),
            HashMap::new(),
            1,
            None,
        )
        .unwrap();

        let lib = Library::new(vec![left, right]).unwrap();

        assert_eq!(
            lib.get_sublibrary_index(&region_id_from_str("r1")).unwrap(),
            0
        );
        assert_eq!(
            lib.get_sublibrary_index(&region_id_from_str("r2")).unwrap(),
            1
        );

        let hit1 = lib
            .lookup(
                &region_id_from_str("r1"),
                &seq(b"AAAA"),
                DistanceMetric::Exact,
                PartialMatching::Full,
            )
            .unwrap()
            .unwrap();

        let hit2 = lib
            .lookup(
                &region_id_from_str("r2"),
                &seq(b"TTTT"),
                DistanceMetric::Exact,
                PartialMatching::Full,
            )
            .unwrap()
            .unwrap();

        assert_eq!(hit1.matches[0].sequence.to_str_or_log(), "AAAA");
        assert_eq!(hit2.matches[0].sequence.to_str_or_log(), "TTTT");
    }

    #[test]
    fn library_lookup_errors_for_missing_region() {
        let sub = make_lookup_sublib(2);
        let lib = Library::new(vec![sub]).unwrap();

        let err = lib
            .lookup(
                &region_id_from_str("missing"),
                &seq(b"AAAA"),
                DistanceMetric::Exact,
                PartialMatching::Full,
            )
            .unwrap_err();

        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn from_file_reads_ids_and_sequences() {
        let path = write_temp_file(
            "library.tsv",
            "_id\tr1\tr2\nseq1\tAAAA\tCCCC\nseq2\tAAAT\tGGGG\n",
        );

        let sub = SubLibrary::from_file(
            path.to_str().unwrap(),
            HashMap::new(),
            2,
            Some("fallback".to_string()),
        )
        .unwrap();

        assert_eq!(sub.ids.len(), 2);
        assert_eq!(library_id_to_str(&sub.ids[0]).to_string(), "seq1");
        assert_eq!(library_id_to_str(&sub.ids[1]).to_string(), "seq2");

        let r1 = sub.library.get(&region_id_from_str("r1")).unwrap();
        assert_eq!(r1.len(), 2);
        assert_eq!(r1[0].sequence.to_str_or_log(), "AAAA");
        assert_eq!(r1[1].sequence.to_str_or_log(), "AAAT");

        fs::remove_file(path).ok();
    }

    #[test]
    fn from_file_generates_ids_when_missing() {
        let path = write_temp_file("library_no_ids.tsv", "r1\tr2\nAAAA\tCCCC\nAAAT\tGGGG\n");

        let sub = SubLibrary::from_file(
            path.to_str().unwrap(),
            HashMap::new(),
            2,
            Some("libx".to_string()),
        )
        .unwrap();

        let ids: Vec<String> = sub
            .ids
            .iter()
            .map(|x| library_id_to_str(x).to_string())
            .collect();

        assert_eq!(ids, vec!["libx_0".to_string(), "libx_1".to_string()]);

        fs::remove_file(path).ok();
    }

    #[test]
    fn from_file_with_lib_spec_accepts_valid_library() {
        let spec: LibrarySpec = LibrarySpec::from_str(&simple_libspec_json()).unwrap();

        let path = write_temp_file(
            "valid_library.tsv",
            "_id\tr1\tr2\nseq1\tAAAA\tCCCC\nseq2\tAAAT\tGGGG\n",
        );

        let sub = SubLibrary::from_file_with_lib_spec(
            path.to_str().unwrap(),
            &spec,
            2,
            Some("fallback".to_string()),
        )
        .unwrap();

        let regions: HashSet<String> = sub
            .regions()
            .into_iter()
            .map(|x| region_id_to_str(x).to_string())
            .collect();

        assert!(regions.contains("r1"));
        assert!(regions.contains("r2"));

        fs::remove_file(path).ok();
    }

    #[test]
    fn from_file_with_lib_spec_rejects_unknown_region_column() {
        let spec: LibrarySpec = LibrarySpec::from_str(&simple_libspec_json()).unwrap();

        let path = write_temp_file("invalid_library.tsv", "_id\tr1\tr3\nseq1\tAAAA\tCCCC\n");

        let err = SubLibrary::from_file_with_lib_spec(
            path.to_str().unwrap(),
            &spec,
            2,
            Some("fallback".to_string()),
        )
        .unwrap_err();

        assert!(
            err.to_string()
                .contains("don't match variable LibSpec region ids")
        );

        fs::remove_file(path).ok();
    }

    #[test]
    fn from_file_with_lib_spec_rejects_reserved_id_region_name() {
        let bad_spec = r#"
{
  "id": "test",
  "forward_start_region": "fixed_left",
  "forward_read_length": 100,
  "reverse_start_region": "fixed_right",
  "reverse_read_length": 100,
  "regions": [
    { "id": "fixed_left", "seq_type": "Fixed", "seq": "AAAA" },
    { "id": "_id", "seq_type": "Library", "min_length": 4, "max_length": 4 },
    { "id": "fixed_right", "seq_type": "Fixed", "seq": "GGGG" }
  ]
}
        "#;

        let spec: LibrarySpec = LibrarySpec::from_str(bad_spec).unwrap();
        let path = write_temp_file("reserved_id.tsv", "_id\t_id\nseq1\tAAAA\n");

        let err = SubLibrary::from_file_with_lib_spec(
            path.to_str().unwrap(),
            &spec,
            2,
            Some("fallback".to_string()),
        )
        .unwrap_err();

        assert!(err.to_string().contains("reserved"));

        fs::remove_file(path).ok();
    }

    // Test repeated hashing with the same library region is stable
    #[test]
    fn library_region_hashes() {
        let mut regs = HashSet::new();

        for _ in 0..1000 {
            let reg = LibraryRegion {
                sequence: seq_from_bytes(b"ACGT"),
                inds: HashSet::from_iter(vec![1usize, 2, 3].into_iter()),
                ids: HashSet::new(),
            };

            regs.insert(reg);
        }

        assert_eq!(regs.len(), 1, "Same LibraryRegion hash isn't consistent")
    }
}