dnacomb 0.5.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
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
//! Counting the occurance of different reads in sequence files
//!
//! Contains methods for counting combinations of expected
//! regions in DNA sequence input.
//! Supports multiple approaches for extracting regions of interest
//! from the input sequence: alignment, pattern matching, inframe
//! position matching and full read counting.
use bio::alignment::AlignmentOperation;
use bio::alignment::distance::hamming;
use bio::alignment::pairwise::{Aligner, MatchFunc, Scoring};
use bio::alphabets::dna::revcomp;
use bio::bio_types::sequence::Sequence;
use clap::ValueEnum;
use itertools::izip;
use log::{debug, info};
use std::iter::{repeat_with, zip};
use std::sync::Once;

use crate::combination::CombinationKey;
use crate::combinations::{CacheHit, ObservedCombinations};
use crate::errors::{AlignmentInfo, LibSpecError, ReadCountError};
use crate::filters::FilterConfig;
use crate::lib_spec::{FlankingSequences, LibrarySpec};
use crate::logging::{Progress, ProgressStyle};
use crate::parsing::{ReadPairProducer, ThreadedReadPairParser};
use crate::region::{RegionCompleteness, RegionKey};
use crate::seqs::{ReadPair, SeqPair};
use crate::utils::mean_quality;

/// Position in an alignment where a region is found
///
/// Has the format (start, stop, RegionCompleteness status)
pub type AlignmentPosition = (usize, usize, RegionCompleteness);

/// Region sequence identified while searching an input sequence, containing
/// the found sequence, quality and whether it is complete. Convenience type
/// for counting functions that is quickly processed into region keys and the
/// proper combination structs.
type RegionMatch = (Sequence, Vec<u8>, RegionCompleteness);

static WARN_MERGE: Once = Once::new();

// Module consts
/// Message for logging region extraction progress
const PROG_MSG: &str = "Extracting regions:";
/// Message emitted at the end of region extraction
const FINAL_MSG: &str = "Extracted regions:";

/// Single end align mode message
const ALIGN_START_SINGLE_MSG: &str = "Extracting regions from single end reads by alignment";
/// Paired end align mode message
const ALIGN_START_PAIRED_MSG: &str = "Extracting regions from paired end reads by alignment";
/// Logging interval when aligning
#[cfg(debug_assertions)]
const ALIGN_LOG_INTERVAL: u64 = 1000;
#[cfg(not(debug_assertions))]
const ALIGN_LOG_INTERVAL: u64 = 250000;

/// Single end pattern match mode message
const PATTERN_START_SINGLE_MSG: &str =
    "Extracting regions from single end reads by pattern matching";
/// Paired end pattern match mode message
const PATTERN_START_PAIRED_MSG: &str =
    "Extracting regions from paired end reads by pattern matching";
/// Logging interval when pattern matching
#[cfg(debug_assertions)]
const PATTERN_LOG_INTERVAL: u64 = 1000;
#[cfg(not(debug_assertions))]
const PATTERN_LOG_INTERVAL: u64 = 2000000;

/// Single end inframe mode message
const INFRAME_START_SINGLE_MSG: &str = "Extracting regions from single end reads in-frame";
/// Paired end inframe mode message
const INFRAME_START_PAIRED_MSG: &str = "Extracting regions from paired end reads in-frame";
/// Logging interval in inframe mode
#[cfg(debug_assertions)]
const INFRAME_LOG_INTERVAL: u64 = 1000;
#[cfg(not(debug_assertions))]
const INFRAME_LOG_INTERVAL: u64 = 5000000;

/// Single end raw mode message
const RAW_START_SINGLE_MSG: &str = "Counting raw single end reads";
/// Paired end raw mode message
/// Logging interval in raw mode
#[cfg(debug_assertions)]
const RAW_LOG_INTERVAL: u64 = 1000;
#[cfg(not(debug_assertions))]
const RAW_LOG_INTERVAL: u64 = 10000000;

/// Customisable alignment scoring scheme allowing Ns
#[derive(Debug, Copy, Clone)]
pub struct AlignmentScorer {
    std_match: i32,
    n_match: i32,
    mismatch: i32,
    gap_open: i32,
    gap_extend: i32,
}

impl AlignmentScorer {
    pub fn new(
        std_match: i32,
        n_match: i32,
        mismatch: i32,
        gap_open: i32,
        gap_extend: i32,
    ) -> Self {
        Self {
            std_match,
            n_match,
            mismatch,
            gap_open,
            gap_extend,
        }
    }

    /// Generate a matching Scoring object to use with Rust Bio alignment
    pub fn get_scoring(self) -> Scoring<AlignmentScorer> {
        Scoring::new(self.gap_open, self.gap_extend, self)
    }
}

impl MatchFunc for AlignmentScorer {
    /// Alignment match scores allowing Ns
    ///
    /// Return a (mis)match score that allows alignment of anything against N
    /// with a moderate penalty. Penalty is greater than a mismatch but less
    /// than a gap to account for possible sequencing errors before variable
    /// regions, which otherwise get shunted into the N region
    fn score(&self, a: u8, b: u8) -> i32 {
        if a == b'N' || b == b'N' {
            self.n_match
        } else if a == b {
            self.std_match
        } else {
            self.mismatch
        }
    }
}

/// Extract the regions of a query sequence that match template sections by walking
/// an alignment path and region position vector together
///
/// Returns a vector of AlignmentPostions the same length as region_positions where each entry gives
/// the corresponding position in the query sequence plus a RegionCompleteness status
fn regions_from_alignment_path(
    region_positions: &[(usize, usize)],
    alignment_path: &[(usize, usize, AlignmentOperation)],
) -> Result<Vec<Option<AlignmentPosition>>, ReadCountError> {
    // Walk the alignment path and region list, adding each region when it completes
    let mut aln_idx: usize = 0;
    let mut seq_start: usize = 1; // AlignmentPath is 1 based
    let mut out_regions: Vec<Option<AlignmentPosition>> = vec![None; region_positions.len()];

    let mut reg_idx: usize = match region_positions
        .iter()
        .position(|x| x.1 >= alignment_path[0].1)
    {
        Some(x) => x,
        None => {
            // abort read if no region overlaps start of alignment, incrementing empty comb
            return Ok(out_regions);
        }
    };

    // If we start strictly inside a region the first is incomplete, otherwise complete
    let mut current_completeness = if alignment_path[0].1 > region_positions[reg_idx].0 {
        RegionCompleteness::Partial5Prime
    } else {
        RegionCompleteness::Complete
    };

    loop {
        // Operate based on current position in alignment/regions
        if alignment_path[aln_idx].1 < region_positions[reg_idx].0 {
            // Before current region
            aln_idx += 1;
            match alignment_path[aln_idx].2 {
                // Increment seq_start for match/subst
                AlignmentOperation::Match | AlignmentOperation::Subst => {
                    seq_start = alignment_path[aln_idx].0
                }
                // But not for del (it doesn't change) and ins (we want to include the start of
                // the insert in any putitive region)
                AlignmentOperation::Del | AlignmentOperation::Ins => {}
                AlignmentOperation::Xclip(_) | AlignmentOperation::Yclip(_) => {
                    return Err(ReadCountError::Error {
                        desc: "Unexpected end clipping in alignment".to_string(),
                    });
                }
            };
        } else if (alignment_path[aln_idx].1 >= region_positions[reg_idx].0)
            && (alignment_path[aln_idx].1 < region_positions[reg_idx].1)
        {
            // A del at the start of a region has a matching template position but preceeding
            // observed position => increment it
            if (alignment_path[aln_idx].1 == region_positions[reg_idx].0)
                && matches!(alignment_path[aln_idx].2, AlignmentOperation::Del)
            {
                seq_start += 1
            }
            // Inside current region
            aln_idx += 1;
        } else if alignment_path[aln_idx].1 >= region_positions[reg_idx].1 {
            // After current region
            if seq_start < alignment_path[aln_idx].0 {
                // If this isn't the case it indicates the whole region is
                // deleted in the alignment and so should be left as None
                out_regions[reg_idx] =
                    Some((seq_start, alignment_path[aln_idx].0, current_completeness));
            }
            current_completeness = RegionCompleteness::Complete;
            aln_idx += 1;
            reg_idx += 1;
        } else {
            return Err(ReadCountError::Error {
                desc: "Region/alignment out of sync while walking (this should \
                       be impossible...)"
                    .to_string(),
            });
        }

        // TODO - need to work out partial matches properly (both alignment end and region expected length?) and need to deal with different alignment opperations

        // Check break conditions
        if reg_idx == region_positions.len() {
            // Exhausted regions => break
            break;
        } else if aln_idx == alignment_path.len() - 1 {
            // Exhausted alignment with possibly open region =>
            // conclude possibly partial region and break
            if alignment_path[aln_idx].1 >= region_positions[reg_idx].0 {
                // Started current region
                if (alignment_path[aln_idx].1 - seq_start)
                    == (region_positions[reg_idx].1 - region_positions[reg_idx].0)
                {
                    // Length is correct, so complete
                    out_regions[reg_idx] = Some((
                        seq_start,
                        alignment_path[aln_idx].0,
                        RegionCompleteness::Complete,
                    ));
                } else {
                    // Shorter than expected to incomplete
                    out_regions[reg_idx] = Some((
                        seq_start,
                        alignment_path[aln_idx].0,
                        RegionCompleteness::Partial3Prime,
                    ));
                }
            }
            break;
        }
    }

    Ok(out_regions)
}

/// Merge a forward and reverse sequence in a region
///
/// Looks at each position in turn and takes the highest quality option, if any.
/// Args are tuples with sequence, quality vector and a completeness tag, plus the
/// expected region length. This only works for fixed length, will need something
/// more complete if there is variable overlap (plus should really warn to use a read
/// merger at that point).
fn merge_seqs(
    fwd: Option<RegionMatch>,
    rev: Option<RegionMatch>,
    len: usize,
) -> Result<Option<(Sequence, RegionCompleteness)>, anyhow::Error> {
    // Deal with simple cases first
    let (f_reg, r_reg) = match (fwd, rev) {
        // Neither present
        (None, None) => return Ok(None),

        // Only rev
        (None, Some(r)) => return Ok(Some((r.0, r.2))),

        // Only fwd
        (Some(f), None) => return Ok(Some((f.0, f.2))),

        // Both, must decide which or possibly combine
        (Some(f), Some(r)) => (f, r),
    };

    // Choose path forward based on completeness flags
    // Overlap/missing centre shouldn't occur here as they imply it has already been merged
    match (f_reg.2, r_reg.2) {
        // Error options
        (RegionCompleteness::MissingCenter { .. }, _)
        | (_, RegionCompleteness::MissingCenter { .. }) => Err(ReadCountError::Error {
            desc: "merge_seqs passed MissingCenter regions, implying already merged".to_string(),
        }
        .into()),

        (RegionCompleteness::Overlapping { .. }, _)
        | (_, RegionCompleteness::Overlapping { .. }) => Err(ReadCountError::Error {
            desc: "merge_seqs passed Overlapping regions, implying already merged".to_string(),
        }
        .into()),

        // Both complete - use highest quality
        (RegionCompleteness::Complete, RegionCompleteness::Complete) => {
            WARN_MERGE.call_once(|| {
                log::warn!(
                    "F/R reads overlap. Consider merging reads first (e.g. using Pear) as this will compare and combine them much more robustly than the simple approach used here."
                );
            });

            if mean_quality(&f_reg.1) >= mean_quality(&r_reg.1) {
                Ok(Some((f_reg.0, RegionCompleteness::Complete)))
            } else {
                Ok(Some((r_reg.0, RegionCompleteness::Complete)))
            }
        }

        // One complete - use the complete one
        (RegionCompleteness::Complete, _) => Ok(Some((f_reg.0, f_reg.2))),
        (_, RegionCompleteness::Complete) => Ok(Some((r_reg.0, r_reg.2))),

        // Both partial in same way - use highest quality
        // Weird situation but some read trimming could lead here potentially
        (RegionCompleteness::Partial5Prime, RegionCompleteness::Partial5Prime) => {
            WARN_MERGE.call_once(|| {
                log::warn!(
                    "F/R reads overlap. Consider merging reads first (e.g. using Pear) as this will compare and combine them much more robustly than the simple approach used here."
                );
            });

            if mean_quality(&f_reg.1) >= mean_quality(&r_reg.1) {
                Ok(Some((f_reg.0, RegionCompleteness::Partial5Prime)))
            } else {
                Ok(Some((r_reg.0, RegionCompleteness::Partial5Prime)))
            }
        }
        (RegionCompleteness::Partial3Prime, RegionCompleteness::Partial3Prime) => {
            WARN_MERGE.call_once(|| {
                log::warn!(
                    "F/R reads overlap. Consider merging reads first (e.g. using Pear) as this will compare and combine them much more robustly than the simple approach used here."
                );
            });

            if mean_quality(&f_reg.1) >= mean_quality(&r_reg.1) {
                Ok(Some((f_reg.0, RegionCompleteness::Partial3Prime)))
            } else {
                Ok(Some((r_reg.0, RegionCompleteness::Partial3Prime)))
            }
        }

        // Both partial with gap - missing centre or overlapping based simply on max region length
        (RegionCompleteness::Partial5Prime, RegionCompleteness::Partial3Prime) => {
            let f_end = f_reg.0.len(); // `f` covers [0..f_end)
            let r_start = len - r_reg.0.len(); // `r` covers [r_start..seq_len)

            let mut out_seq = Vec::with_capacity(f_reg.0.len() + 1 + r_reg.0.len());
            out_seq.extend_from_slice(&f_reg.0);
            out_seq.push(b'/');
            out_seq.extend_from_slice(&r_reg.0);

            if r_start >= f_end {
                // No overlap
                Ok(Some((
                    out_seq,
                    RegionCompleteness::MissingCenter {
                        split_ind: f_reg.0.len(),
                    },
                )))
            } else {
                // Overlap
                WARN_MERGE.call_once(|| {
                    log::warn!(
                        "F/R reads overlap. Consider merging reads first (e.g. using Pear) as this will compare and combine them much more robustly than the simple approach used here."
                    );
                });

                Ok(Some((
                    out_seq,
                    RegionCompleteness::Overlapping {
                        split_ind: f_reg.0.len(),
                    },
                )))
            }
        }
        (RegionCompleteness::Partial3Prime, RegionCompleteness::Partial5Prime) => {
            // Reversed, should rarely see this case but some odd trimming could create in theory
            // Basically the reverse of above
            let r_end = r_reg.0.len(); // `r` covers [0..r_end)
            let f_start = len - f_reg.0.len(); // `f` covers [f_start..seq_len)

            let mut out_seq = Vec::with_capacity(r_reg.0.len() + 1 + f_reg.0.len());
            out_seq.extend_from_slice(&r_reg.0);
            out_seq.push(b'/');
            out_seq.extend_from_slice(&f_reg.0);

            if f_start >= r_end {
                // No overlap
                Ok(Some((
                    out_seq,
                    RegionCompleteness::MissingCenter {
                        split_ind: r_reg.0.len(),
                    },
                )))
            } else {
                // Overlap
                WARN_MERGE.call_once(|| {
                    log::warn!(
                        "F/R reads overlap. Consider merging reads first (e.g. using Pear) as this will compare and combine them much more robustly than the simple approach used here."
                    );
                });

                Ok(Some((
                    out_seq,
                    RegionCompleteness::Overlapping {
                        split_ind: r_reg.0.len(),
                    },
                )))
            }
        }
    }
}

/// Find region matches by pattern
///
/// Search an input sequence for regions flanked by the input patterns, in the order
/// they occur. Not all regions need to be identified, but those found will be a
/// continuous subsequence. For instance, it may return hits for regions 2-4 but
/// be missing regions 1 and 5. Where the first/last flank sequence is None it is
/// considered to start/end at the sequence start/end. A mismatch tolerance allows
/// close flank matches to be considered hits to correct for errors, but this requires
/// care where multiple flank sequences have similar sequences.
///
/// Returns a vector of hits, one per input region with None if the region is missing or
/// Some((Sequence, Phred Quality, RegionCompleteness)) tuple
fn match_flank_patterns(
    seq: &[u8],
    qual: &[u8],
    flanks: &[FlankingSequences],
    tolerance: u64,
) -> Result<Vec<Option<RegionMatch>>, ReadCountError> {
    let mut out: Vec<Option<RegionMatch>> = repeat_with(|| None).take(flanks.len()).collect();

    // If no flanking regions then find nothing
    if flanks.is_empty() {
        return Ok(out);
    }

    // Check flank sequence is valid
    match LibrarySpec::validate_flank_seqs(flanks) {
        Ok(_) => {}
        Err(e) => {
            return Err(ReadCountError::Error {
                desc: format!("Invalid flanking sequences: {}", e),
            });
        }
    };

    // Initialise seach space
    let mut pos: usize = 0; // Position in sequence to search for match
    let mut end: usize; // end of current flank seq to match
    let mut reg: usize = 0; // region being matched
    let mut reg_start: usize = 0; // Start point of region seq
    let mut flank_seq: &Sequence; // Sequence being searched for
    let mut open: bool = false; // whether the region start is found
    let mut dist: u64; // Distance to region

    // Find opening region to assign start point
    let start_regs: Vec<Sequence> = flanks
        .iter()
        .map(|r| match r {
            FlankingSequences::Unflanked => unreachable!("Unflanked already checked"),
            FlankingSequences::OpenStart(end) => Ok(end.clone()),
            FlankingSequences::Internal(start, ..) => Ok(start.clone()),
            FlankingSequences::OpenEnd(start) => Ok(start.clone()),
        })
        .collect::<Result<Vec<Sequence>, ReadCountError>>()?;

    'outer: while pos < seq.len() {
        for (i, r) in start_regs.iter().enumerate() {
            end = pos + r.len();
            if end > seq.len() {
                continue;
            }

            dist = hamming(r, &seq[pos..end]);

            if dist <= tolerance {
                match flanks[i] {
                    FlankingSequences::Unflanked => unreachable!("Unflanked already checked"),
                    FlankingSequences::OpenStart(..) => {
                        out[i] = Some((
                            seq[0..pos].to_vec(),
                            qual[0..pos].to_vec(),
                            RegionCompleteness::Partial5Prime,
                        ));

                        reg = i + 1;
                        open = false;
                        pos = end;
                    }
                    FlankingSequences::Internal(..) => {
                        reg = i;
                        open = true;
                        reg_start = end;
                        pos = end;
                    }
                    FlankingSequences::OpenEnd(..) => {
                        out[i] = Some((
                            seq[end..seq.len()].to_vec(),
                            qual[end..seq.len()].to_vec(),
                            RegionCompleteness::Partial3Prime,
                        ));

                        // An open end region must be at the end (checked in validation)
                        // so directly return
                        return Ok(out);
                    }
                }
                break 'outer;
            }
        }
        pos += 1;
    }

    // Walk the remaining sequence and region list in parallel, adding each newly found region to output.
    // Before finding the first region must consider all options at each point
    'outer: while reg < flanks.len() && pos < seq.len() {
        // Get region sequence
        flank_seq = match (open, &flanks[reg]) {
            (_, FlankingSequences::Unflanked) => unreachable!("Unflanked already checked"),
            (_, FlankingSequences::OpenStart(..)) => {
                unreachable!("Open start can only be first and already processed")
            }
            (_, FlankingSequences::OpenEnd(start)) => start,
            (false, FlankingSequences::Internal(start, _)) => start,
            (true, FlankingSequences::Internal(_, end)) => end,
        };

        end = pos + flank_seq.len();

        // Loop forward to find then process
        'inner: while pos < seq.len() {
            // If seq runs off end without finding we've exhausted
            if end > seq.len() {
                break 'outer;
            }

            dist = hamming(flank_seq, &seq[pos..end]);

            // If not found, continue (do this way to save indent below)
            if dist > tolerance {
                pos += 1;
                end += 1;
                continue;
            }

            match (open, &flanks[reg]) {
                (_, FlankingSequences::Unflanked) => unreachable!("Unflanked already checked"),
                (_, FlankingSequences::OpenStart(..)) => {
                    unreachable!("Open start can only be first and already processed")
                }
                (true, FlankingSequences::OpenEnd(..)) => {
                    unreachable!("Open end only has a start and is then processed below")
                }
                (false, FlankingSequences::OpenEnd(..)) => {
                    out[reg] = Some((
                        seq[end..seq.len()].to_vec(),
                        qual[end..seq.len()].to_vec(),
                        RegionCompleteness::Partial3Prime,
                    ));

                    // Open end must finish the seq so break outer
                    break 'outer;
                }
                (true, FlankingSequences::Internal(..)) => {
                    // Found end - set out[reg] and move to next region
                    out[reg] = Some((
                        seq[reg_start..pos].to_vec(),
                        qual[reg_start..pos].to_vec(),
                        RegionCompleteness::Complete,
                    ));

                    pos = end;
                    open = false;
                    reg += 1;
                    break 'inner;
                }
                (false, FlankingSequences::Internal(..)) => {
                    // Found start - set open and search for end
                    pos = end;
                    open = true;
                    reg_start = end;
                    break 'inner;
                }
            }
        }
    }

    // If an Internal region is still open, close it as partial 3'
    if open && matches!(&flanks[reg], FlankingSequences::Internal(..)) {
        out[reg] = Some((
            seq[reg_start..seq.len()].to_vec(),
            qual[reg_start..seq.len()].to_vec(),
            RegionCompleteness::Partial3Prime,
        ));
    }

    Ok(out)
}

/// Extract ObservedCombinations from a worker thread JoinHandle
///
/// Returns the appropriate result or an error that can be raised via ?
fn join_observed_combinations(
    handle: Option<std::thread::JoinHandle<Result<ObservedCombinations, anyhow::Error>>>,
) -> Result<ObservedCombinations, anyhow::Error> {
    match handle {
        Some(j) => match j.join() {
            Ok(r) => r,
            Err(e) => {
                if let Some(msg) = e.downcast_ref::<&str>() {
                    Err(ReadCountError::Error {
                        desc: format!("Counting thread paniced: {msg}"),
                    }
                    .into())
                } else if let Some(msg) = e.downcast_ref::<String>() {
                    Err(ReadCountError::Error {
                        desc: format!("Counting thread paniced: {msg}"),
                    }
                    .into())
                } else {
                    Err(ReadCountError::Error {
                        desc: "Counting thread paniced with an unknown payload".to_string(),
                    }
                    .into())
                }
            }
        },
        None => Err(ReadCountError::Error {
            desc: "No counting threads returned objects".to_string(),
        }
        .into()),
    }
}

/// Count algorithm to apply
#[derive(Clone, ValueEnum, Debug, Copy)]
pub enum CountMode {
    FullRead,
    Inframe,
    Pattern,
    Align,
}

/// Count the occurance of query regions in sequencing reads
///
/// Dispatches counting to the appropriate implementation based on CountMode
pub fn count_reads<T: ReadPairProducer>(
    reads: T,
    lib_spec: &Option<LibrarySpec>,
    mode: CountMode,
    full_seq: bool,
    filter_config: FilterConfig,
    alignment_scorer: Option<AlignmentScorer>,
    pattern_length: Option<usize>,
    pattern_tolerance: Option<u64>,
    cache: bool,
    threads: usize,
    progress_style: Option<&ProgressStyle>,
) -> Result<ObservedCombinations, anyhow::Error> {
    let default_progress = ProgressStyle::new(None, false);
    let progress = progress_style.unwrap_or(&default_progress);

    match threads.cmp(&1) {
        std::cmp::Ordering::Less => Err(ReadCountError::Error {
            desc: "Threads must be >0".to_string(),
        }
        .into()),
        std::cmp::Ordering::Equal => {
            // Determine type of matching desired and despatch as appropriate
            match (
                reads.has_reverse(),
                lib_spec,
                mode,
                alignment_scorer,
                pattern_length,
                pattern_tolerance,
            ) {
                (_, _, CountMode::Align, None, _, _) => Err(ReadCountError::Error {
                    desc: "Mode is 'align' but no AlignmentScorer passed".to_string(),
                }
                .into()),
                (false, Some(lib_spec), CountMode::Align, Some(a), _, _) => {
                    count_single_align(reads, lib_spec, full_seq, filter_config, a, cache, progress)
                }
                (true, Some(lib_spec), CountMode::Align, Some(a), _, _) => {
                    count_paired_align(reads, lib_spec, full_seq, filter_config, a, cache, progress)
                }

                (_, _, CountMode::Pattern, _, None, _) | (_, _, CountMode::Pattern, _, _, None) => {
                    Err(ReadCountError::Error {
                        desc: "Mode is 'pattern' but pattern length and/or tolerance is missing"
                            .to_string(),
                    }
                    .into())
                }
                (false, Some(lib_spec), CountMode::Pattern, _, Some(len), Some(tol)) => {
                    count_single_pattern(
                        reads,
                        lib_spec,
                        full_seq,
                        filter_config,
                        len,
                        tol,
                        cache,
                        progress,
                    )
                }
                (true, Some(lib_spec), CountMode::Pattern, _, Some(len), Some(tol)) => {
                    count_paired_pattern(
                        reads,
                        lib_spec,
                        full_seq,
                        filter_config,
                        len,
                        tol,
                        cache,
                        progress,
                    )
                }

                (false, Some(lib_spec), CountMode::Inframe, _, _, _) => {
                    count_single_inframe(reads, lib_spec, full_seq, filter_config, cache, progress)
                }
                (true, Some(lib_spec), CountMode::Inframe, _, _, _) => {
                    count_paired_inframe(reads, lib_spec, full_seq, filter_config, cache, progress)
                }

                (_, Some(_), CountMode::FullRead, _, _, _) => {
                    count_raw(reads, filter_config, progress)
                }
                (_, None, _, _, _, _) => count_raw(reads, filter_config, progress),
            }
        }
        std::cmp::Ordering::Greater => {
            // Set up communication channel
            let (read_tx, read_rx) = crossbeam::channel::bounded(10 * threads);
            let mut handles = Vec::new();

            // Spin up worker threads ready to receive data
            for _ in 0..threads {
                // each thread gets its own Read Receiver and Count Sender clone
                let rx = read_rx.clone();
                let rev_reads = reads.has_reverse();
                let group = reads.group().clone();
                let max_reads = reads.max_reads();
                let lspec = lib_spec.as_ref().cloned();
                let fconf = filter_config.clone();
                let mut pstyle = progress_style.cloned();
                if let Some(ref mut p) = pstyle {
                    p.use_thread_id = true
                }

                handles.push(std::thread::spawn(move || {
                    let local_reads = ThreadedReadPairParser::new(rx, rev_reads, group, max_reads);
                    count_reads(
                        local_reads,
                        &lspec,
                        mode,
                        full_seq,
                        fconf,
                        alignment_scorer,
                        pattern_length,
                        pattern_tolerance,
                        cache,
                        1,
                        pstyle.as_ref(),
                    )
                }));
            }

            // Produce reads on main thread - as they are sent they will be processed on worker threads
            for read in reads {
                read_tx
                    .send(read)
                    .expect("Region extraction thread send failed");
            }
            drop(read_tx);
            drop(read_rx);

            // Unpack initial count object
            let mut final_counts: ObservedCombinations = join_observed_combinations(handles.pop())?;

            // Merge rest of the results
            for handle in handles {
                let new_counts = join_observed_combinations(Some(handle))?;
                final_counts.merge(new_counts)?
            }

            Ok(final_counts)
        }
    }
}

/// Count single end reads by aligning to the library template
fn count_single_align<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    alignment_scorer: AlignmentScorer,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", ALIGN_START_SINGLE_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        ALIGN_LOG_INTERVAL,
    );

    // Identify regions
    let regions = lib_spec.variable_regions();
    let region_positions: Vec<(usize, usize)> = regions
        .iter()
        .map(|x| lib_spec.template_position(x).expect("Region from LibSpec"))
        // Alignment path is 1 based, so offset originally 0 based positions
        .map(|x| (x.0 + 1, x.1 + 1))
        .collect();

    // Initialise counts
    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    // Initialise aligner
    let scoring = alignment_scorer.get_scoring();
    let mut aligner = Aligner::with_capacity_and_scoring(400, 150, scoring);
    let template = lib_spec.template_sequence();

    // Iterate over reads
    info!("Printing example alignments. Check these are reasonable to help diagnose problems");
    for (i, result) in reads.enumerate() {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let read_alignment = aligner.semiglobal(record.forward.seq(), &template);
        let alignment_path = read_alignment.path();

        // Filter reads with too low scores or otherwise unmatched
        match counts.filter_alignment(&record, &read_alignment, None, true) {
            None => {}
            Some(reason) => {
                if cache {
                    counts.cache(record.into_seqpair(), CacheHit::Filter(reason));
                }
                continue;
            }
        }

        // Extract positions
        let query_regions = regions_from_alignment_path(&region_positions, &alignment_path)?;

        if i < 10 {
            info!(
                "Alignment {}:\nScore: {}, Cigar: {}\n{}\nExtracted regions: {:?}",
                i + 1,
                read_alignment.score,
                read_alignment.cigar(false),
                read_alignment.pretty(record.forward.seq(), &template, 100),
                query_regions
            );
            debug!("{:?}", read_alignment.path())
        }

        let mut comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            Vec::with_capacity(query_regions.len()),
        );

        for (id, opt_pos) in zip(&regions, &query_regions) {
            if let Some(pos) = opt_pos {
                match record.forward.seq().get((pos.0 - 1)..(pos.1 - 1)) {
                    Some(s) => {
                        comb_key.regions.push(RegionKey::new(
                            id.to_string(),
                            // Offset seq lookup - rust vec 0 based and AlignmentPath 1 based
                            s.to_vec(),
                            pos.2,
                        ));
                    }
                    None => {
                        return Err(ReadCountError::BadAlignment {
                            alignment: Box::new(AlignmentInfo {
                                read_id: record.forward.id().to_string(),
                                read_number: i,
                                pretty_alignment: read_alignment.pretty(
                                    record.forward.seq(),
                                    &template,
                                    100,
                                ),
                                alignment: read_alignment,
                                region_ids: regions,
                                region_positions,
                                mapped_positions: query_regions,
                            }),
                        }
                        .into());
                    }
                }
            }
        }

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }
        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count paired end reads by aligning to the library template
fn count_paired_align<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    alignment_scorer: AlignmentScorer,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", ALIGN_START_PAIRED_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        ALIGN_LOG_INTERVAL,
    );

    // Identify regions
    let regions = lib_spec.variable_regions();

    let region_lengths: Vec<usize> = regions
        .iter()
        .map(|x| {
            lib_spec
                .get_region(x)
                .expect("Region should exist as comes from lib")
                .len()
        })
        .collect();

    let region_positions: Vec<(usize, usize)> = regions
        .iter()
        .map(|x| lib_spec.template_position(x).expect("Region from LibSpec"))
        // Alignment path is 1 based, so offset originally 0 based positions
        .map(|x| (x.0 + 1, x.1 + 1))
        .collect();

    // Initialise counts
    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    // Initialise aligner
    let scoring = alignment_scorer.get_scoring();
    let mut aligner = Aligner::with_capacity_and_scoring(400, 150, scoring);
    let template = lib_spec.template_sequence();

    // Iterate over reads
    info!("Printing example alignments. Check these are reasonable to help diagnose problems");
    for (i, result) in reads.enumerate() {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let f_read = record.forward.seq();
        let r_read = match &record.reverse {
            Some(x) => revcomp(x.seq()),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        let f_qual = record.forward.qual();
        let r_qual: Vec<u8> = match &record.reverse {
            Some(x) => x.qual().iter().rev().cloned().collect(),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        // Fwd Read
        let f_alignment = aligner.semiglobal(f_read, &template);
        let f_path = f_alignment.path();

        // Rev Read
        let r_alignment = aligner.semiglobal(&r_read, &template);
        let r_path = r_alignment.path();

        // Filter reads with too low scores or otherwise unmatched
        match counts.filter_alignment(&record, &f_alignment, Some(&r_alignment), true) {
            None => {}
            Some(reason) => {
                if cache {
                    counts.cache(record.into_seqpair(), CacheHit::Filter(reason));
                }
                continue;
            }
        }

        // Extract regions
        let f_regions = regions_from_alignment_path(&region_positions, &f_path)?;
        let r_regions = regions_from_alignment_path(&region_positions, &r_path)?;

        // Print first 10 alignments
        if i < 10 {
            info!(
                "Fwd Alignment {}:\nScore: {}, Cigar: {}\n{}\nExtracted regions: {:?}",
                i + 1,
                f_alignment.score,
                f_alignment.cigar(false),
                f_alignment.pretty(f_read, &template, 100),
                f_regions
            );
            debug!("{:?}", f_alignment.path());

            info!(
                "Rev Alignment {}:\nScore: {}, Cigar: {}\n{}\nExtracted regions: {:?}",
                i + 1,
                r_alignment.score,
                r_alignment.cigar(false),
                r_alignment.pretty(&r_read, &template, 100),
                r_regions
            );
            debug!("{:?}", r_alignment.path());
        }

        let mut comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            Vec::with_capacity(std::cmp::max(f_regions.len(), r_regions.len())),
        );

        for (id, len, f_pos, r_pos) in izip!(&regions, &region_lengths, &f_regions, &r_regions) {
            match (f_pos, r_pos) {
                (None, None) => continue,
                (None, Some(r)) => {
                    match r_read.get((r.0 - 1)..(r.1 - 1)) {
                        Some(s) => {
                            comb_key.regions.push(RegionKey::new(
                                id.to_string(),
                                // Offset seq lookup - rust vec 0 based and AlignmentPath 1 based
                                s.to_vec(),
                                r.2,
                            ));
                        }
                        None => {
                            return Err(ReadCountError::BadAlignment {
                                alignment: Box::new(AlignmentInfo {
                                    read_id: record
                                        .reverse
                                        .expect("Read known to be present")
                                        .id()
                                        .to_string(),
                                    read_number: i,
                                    pretty_alignment: r_alignment.pretty(&r_read, &template, 100),
                                    alignment: r_alignment,
                                    region_ids: regions,
                                    region_positions,
                                    mapped_positions: r_regions,
                                }),
                            }
                            .into());
                        }
                    }
                }
                (Some(f), None) => {
                    match f_read.get((f.0 - 1)..(f.1 - 1)) {
                        Some(s) => {
                            comb_key.regions.push(RegionKey::new(
                                id.to_string(),
                                // Offset seq lookup - rust vec 0 based and AlignmentPath 1 based
                                s.to_vec(),
                                f.2,
                            ));
                        }
                        None => {
                            return Err(ReadCountError::BadAlignment {
                                alignment: Box::new(AlignmentInfo {
                                    read_id: record.forward.id().to_string(),
                                    read_number: i,
                                    pretty_alignment: f_alignment.pretty(f_read, &template, 100),
                                    alignment: f_alignment,
                                    region_ids: regions,
                                    region_positions,
                                    mapped_positions: f_regions,
                                }),
                            }
                            .into());
                        }
                    }
                }
                (Some(f), Some(r)) => {
                    let f_reg_seq = match f_read.get((f.0 - 1)..(f.1 - 1)) {
                        Some(s) => s,
                        None => {
                            return Err(ReadCountError::BadAlignment {
                                alignment: Box::new(AlignmentInfo {
                                    read_id: record.forward.id().to_string(),
                                    read_number: i,
                                    pretty_alignment: f_alignment.pretty(f_read, &template, 100),
                                    alignment: f_alignment,
                                    region_ids: regions,
                                    region_positions,
                                    mapped_positions: f_regions,
                                }),
                            }
                            .into());
                        }
                    };

                    let f_reg_qual = f_qual
                        .get((f.0 - 1)..(f.1 - 1))
                        .expect("If seq extracted then qual should be too as the same length");

                    let r_reg_seq = match r_read.get((r.0 - 1)..(r.1 - 1)) {
                        Some(s) => s,
                        None => {
                            return Err(ReadCountError::BadAlignment {
                                alignment: Box::new(AlignmentInfo {
                                    read_id: record
                                        .reverse
                                        .expect("Read known to be present")
                                        .id()
                                        .to_string(),
                                    read_number: i,
                                    pretty_alignment: r_alignment.pretty(&r_read, &template, 100),
                                    alignment: r_alignment,
                                    region_ids: regions,
                                    region_positions,
                                    mapped_positions: r_regions,
                                }),
                            }
                            .into());
                        }
                    };

                    let r_reg_qual = r_qual
                        .get((r.0 - 1)..(r.1 - 1))
                        .expect("If seq extracted then qual should be too as the same length");

                    match merge_seqs(
                        Some((f_reg_seq.to_vec(), f_reg_qual.to_vec(), f.2)),
                        Some((r_reg_seq.to_vec(), r_reg_qual.to_vec(), r.2)),
                        *len,
                    )? {
                        Some((seq, comp)) => {
                            comb_key.regions.push(RegionKey::new(id.clone(), seq, comp))
                        }
                        None => continue,
                    };
                }
            }
        }

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }

        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count single end reads using surrounding patterns from the library template
fn count_single_pattern<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    pattern_length: usize,
    pattern_tolerance: u64,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", PATTERN_START_SINGLE_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        PATTERN_LOG_INTERVAL,
    );

    let regions = lib_spec.variable_regions();

    let flank_regions = lib_spec.get_all_flanking_regions(pattern_length)?;

    info!(
        "Extracting regions using flank seqs: {:?}",
        flank_regions
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
    );

    // Count reads
    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    for result in reads {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let region_matches = match_flank_patterns(
            record.forward.seq(),
            record.forward.qual(),
            &flank_regions,
            pattern_tolerance,
        )?;

        let comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            zip(&regions, region_matches)
                .filter_map(|(id, reg)| match reg {
                    Some(r) => Some(RegionKey::new(id.clone(), r.0, r.2)),
                    None => None,
                })
                .collect(),
        );

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }

        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count paired end reads using surrounding patterns from the library template
fn count_paired_pattern<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    pattern_length: usize,
    pattern_tolerance: u64,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", PATTERN_START_PAIRED_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        PATTERN_LOG_INTERVAL,
    );

    let regions = lib_spec.variable_regions();
    let n_regions = regions.len();

    let region_lengths: Vec<usize> = regions
        .iter()
        .map(|x| {
            lib_spec
                .get_region(x)
                .expect("Region should exist as comes from lib")
                .len()
        })
        .collect();

    let flank_regions = lib_spec.get_all_flanking_regions(pattern_length)?;

    info!(
        "Extracting regions using flank seqs: {:?}",
        flank_regions
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
    );

    // Count reads
    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    for (i, result) in reads.enumerate() {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let f_seq = record.forward.seq();
        let r_seq = match &record.reverse {
            Some(x) => revcomp(x.seq()),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        let f_qual = record.forward.qual();
        let r_qual: Vec<u8> = match &record.reverse {
            Some(x) => x.qual().iter().rev().cloned().collect(),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        let f_matches = match_flank_patterns(f_seq, f_qual, &flank_regions, pattern_tolerance)?;

        let r_matches = match_flank_patterns(&r_seq, &r_qual, &flank_regions, pattern_tolerance)?;

        let mut comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            Vec::with_capacity(n_regions),
        );

        for (id, len, fwd, rev) in izip!(&regions, &region_lengths, f_matches, r_matches) {
            if let Some(merged) = merge_seqs(fwd, rev, *len)? {
                comb_key
                    .regions
                    .push(RegionKey::new(id.clone(), merged.0, merged.1));
            }
        }

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }

        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count single end reads based on their in-frame position in the template
fn count_single_inframe<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", INFRAME_START_SINGLE_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        INFRAME_LOG_INTERVAL,
    );

    let regions = lib_spec.variable_regions();

    let offset = match lib_spec.template_position(&lib_spec.forward_start_region) {
        Ok(x) => x.0,
        Err(_) => {
            return Err(LibSpecError::LibSpec {
                desc: "Forward start region missing from LibSpec".to_string(),
            }
            .into());
        }
    };
    debug!("F offset: {}", offset);

    let region_positions: Vec<(usize, usize)> = regions
        .iter()
        .map(|x| {
            lib_spec
                .template_position(x)
                .expect("The region should be in LibSpec")
        })
        .map(|x| (x.0 + offset, x.1 + offset))
        .collect();
    debug!("F region positions: {:?}", region_positions);

    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    for result in reads {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let mut comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            Vec::with_capacity(regions.len()),
        );

        let seq_len = record.forward.seq().len();

        for (id, pos) in zip(&regions, &region_positions) {
            let reg_seq: Sequence;
            let complete: RegionCompleteness;

            if (pos.0 < seq_len) && (pos.1 < seq_len) {
                // Region fully within read
                reg_seq = record
                    .forward
                    .seq()
                    .get(pos.0..pos.1)
                    .expect("Seq should contain region as just tested")
                    .to_vec();
                complete = RegionCompleteness::Complete;
            } else if (pos.0 < seq_len) && (pos.1 > seq_len - 1) {
                // Region partially within read
                reg_seq = record
                    .forward
                    .seq()
                    .get(pos.0..seq_len)
                    .expect("Seq should contain region as just tested")
                    .to_vec();
                complete = RegionCompleteness::Partial3Prime;
            } else {
                // Region outside read
                // Regions are fetched in order so all remaining will be outside to => break
                break;
            }

            comb_key
                .regions
                .push(RegionKey::new(id.clone(), reg_seq, complete));
        }

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }

        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count paired end reads based on their in-frame position in the template
fn count_paired_inframe<T: ReadPairProducer>(
    reads: T,
    lib_spec: &LibrarySpec,
    full_seq: bool,
    filter_config: FilterConfig,
    cache: bool,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", INFRAME_START_PAIRED_MSG);

    let mut progress: Progress = Progress::from_style(
        progress_style,
        PROG_MSG,
        FINAL_MSG,
        None,
        INFRAME_LOG_INTERVAL,
    );

    let regions = lib_spec.variable_regions();

    let region_lengths: Vec<usize> = regions
        .iter()
        .map(|x| {
            lib_spec
                .get_region(x)
                .expect("Region should exist as comes from lib")
                .len()
        })
        .collect();

    let f_offset = match lib_spec.template_position(&lib_spec.forward_start_region) {
        Ok(x) => x.0,
        Err(_) => {
            return Err(LibSpecError::LibSpec {
                desc: "Forward start region missing from LibSpec".to_string(),
            }
            .into());
        }
    };
    debug!("F offset: {}", f_offset);

    let f_region_positions: Vec<(usize, usize)> = regions
        .iter()
        .map(|x| {
            lib_spec
                .template_position(x)
                .expect("The region should be in LibSpec")
        })
        .map(|x| (x.0 + f_offset, x.1 + f_offset))
        .collect();
    debug!("F region positions: {:?}", f_region_positions);

    let r_offset = match lib_spec.template_position(&lib_spec.reverse_start_region) {
        Ok(x) => x.1,
        Err(_) => {
            return Err(LibSpecError::LibSpec {
                desc: "Reverse start region missing from LibSpec".to_string(),
            }
            .into());
        }
    };
    debug!("R offset: {}", r_offset);

    let r_region_positions: Vec<(usize, usize)> = regions
        .iter()
        .map(|x| {
            lib_spec
                .template_position(x)
                .expect("The region should be in LibSpec")
        })
        .map(|x| (x.0.abs_diff(r_offset), x.1.abs_diff(r_offset)))
        .collect();
    debug!("R region positions: {:?}", r_region_positions);

    let mut counts = ObservedCombinations::new(regions.clone(), filter_config);

    for (i, result) in reads.enumerate() {
        let record: ReadPair = result?;

        // Check if read needs to be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        // Check if the read has been cached
        if cache && counts.check_cache(&record, true)?.is_some() {
            continue;
        }

        let mut comb_key: CombinationKey = CombinationKey::new(
            if full_seq {
                Some(SeqPair::from_readpair(&record))
            } else {
                None
            },
            Vec::with_capacity(regions.len()),
        );

        let f_read = record.forward.seq();
        let r_read = match &record.reverse {
            Some(x) => revcomp(x.seq()),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        let f_qual = record.forward.qual();
        let r_qual: Vec<u8> = match &record.reverse {
            Some(x) => x.qual().iter().rev().cloned().collect(),
            None => {
                return Err(ReadCountError::Error {
                    desc: format!("No reverse read found at read {i}"),
                }
                .into());
            }
        };

        let f_len = f_read.len();
        let r_len = r_read.len();

        for (id, len, f_pos, r_pos) in izip!(
            &regions,
            &region_lengths,
            &f_region_positions,
            &r_region_positions
        ) {
            let mut fwd: Option<RegionMatch> = None;
            let mut rev: Option<RegionMatch> = None;

            // Extract f_seq
            if (f_pos.0 < f_len) && (f_pos.1 < f_len) {
                // Region fully within read
                fwd = Some((
                    f_read
                        .get(f_pos.0..f_pos.1)
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    f_qual
                        .get(f_pos.0..f_pos.1)
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    RegionCompleteness::Complete,
                ));
            } else if (f_pos.0 < f_len) && (f_pos.1 > f_len - 1) {
                // Region partially within read
                fwd = Some((
                    f_read
                        .get(f_pos.0..f_len)
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    f_qual
                        .get(f_pos.0..f_len)
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    RegionCompleteness::Partial3Prime,
                ));
            }

            // Extract r_seq
            if (r_len > r_pos.0) && (r_len > r_pos.1) {
                // Region fully within read
                rev = Some((
                    r_read
                        .get((r_len - r_pos.0)..(r_len - r_pos.1))
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    r_qual
                        .get((r_len - r_pos.0)..(r_len - r_pos.1))
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    RegionCompleteness::Complete,
                ));
            } else if (r_len <= r_pos.0) && (r_len > r_pos.1) {
                // Region partially within read
                rev = Some((
                    r_read
                        .get(0..(r_len - r_pos.1))
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    r_qual
                        .get(0..(r_len - r_pos.1))
                        .expect("Seq should contain region as just tested")
                        .to_vec(),
                    RegionCompleteness::Partial5Prime,
                ));
            }

            // Determine which read to use
            match merge_seqs(fwd, rev, *len)? {
                Some((seq, comp)) => comb_key.regions.push(RegionKey::new(id.clone(), seq, comp)),
                None => continue,
            }
        }

        counts.add_or_increment_combination(&comb_key, record.group.clone())?;

        if cache {
            counts.cache(record.into_seqpair(), CacheHit::Comb(comb_key));
        }

        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

/// Count entire single end reads
fn count_raw<T: ReadPairProducer>(
    reads: T,
    filter_config: FilterConfig,
    progress_style: &ProgressStyle,
) -> Result<ObservedCombinations, anyhow::Error> {
    info!("{}", RAW_START_SINGLE_MSG);

    let mut progress: Progress =
        Progress::from_style(progress_style, PROG_MSG, FINAL_MSG, None, RAW_LOG_INTERVAL);

    let mut counts = ObservedCombinations::new(vec![], filter_config);

    for result in reads {
        let record: ReadPair = result?;

        // Check if read should be filtered
        if counts.filter_readpair(&record, true).is_some() {
            continue;
        }

        let comb_key: CombinationKey =
            CombinationKey::new(Some(SeqPair::from_readpair(&record)), vec![]);

        counts.add_or_increment_combination(&comb_key, record.group)?;
        progress.inc(1);
    }
    progress.finish();

    Ok(counts)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lib_spec::FlankingSequences;
    use crate::region::RegionCompleteness;

    #[test]
    fn test_perfect_flank_matching() {
        let _ = env_logger::try_init();

        let seq = b"CCCCAATTGGGCCGGAAAAGGCCGGTATAGGGGATATGGGCGCGTTTT";
        let qual = b"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
        let flanks = vec![
            FlankingSequences::OpenStart(b"AATT".to_vec()),
            FlankingSequences::Internal(b"CCGG".to_vec(), b"GGCC".to_vec()),
            FlankingSequences::Internal(b"TATA".to_vec(), b"ATAT".to_vec()),
            FlankingSequences::OpenEnd(b"CGCG".to_vec()),
        ];
        let tolerance: u64 = 0;

        let exp: Vec<Option<RegionMatch>> = vec![
            Some((
                b"CCCC".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Partial5Prime,
            )),
            Some((
                b"AAAA".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Complete,
            )),
            Some((
                b"GGGG".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Complete,
            )),
            Some((
                b"TTTT".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Partial3Prime,
            )),
        ];

        if let Ok(obs) = match_flank_patterns(seq, qual, &flanks, tolerance) {
            assert_eq!(obs, exp, "Observed regions don't match expected");
        } else {
            assert!(false, "match_flank_patterns returned Err(...)")
        }
    }

    #[test]
    fn test_flank_matching_with_mismatches() {
        let _ = env_logger::try_init();

        let seq = b"CCCCAATCGGGGCGGAAAAGGCCGGTATAGGGGATATAAACGTTTTTT";
        let qual = b"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
        let flanks = vec![
            FlankingSequences::OpenStart(b"AATT".to_vec()), // 1 mismatch: AATC
            FlankingSequences::Internal(b"CCGG".to_vec(), b"GGCC".to_vec()), // 1 mismatch: GCGG
            FlankingSequences::Internal(b"TATA".to_vec(), b"ATAT".to_vec()),
            FlankingSequences::OpenEnd(b"CGCG".to_vec()), // 2 mismatches: CGTT
        ];
        let tolerance: u64 = 1;

        let exp: Vec<Option<RegionMatch>> = vec![
            Some((
                b"CCCC".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Partial5Prime,
            )),
            Some((
                b"AAAA".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Complete,
            )),
            Some((
                b"GGGG".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Complete,
            )),
            None,
        ];

        let obs =
            match_flank_patterns(seq, qual, &flanks, tolerance).expect("Pattern match failed");
        assert_eq!(
            obs, exp,
            "Observed regions don't match expected with mismatch tolerance"
        );
    }

    #[test]
    fn test_flank_matching_partial_path() {
        let seq = b"GGGCCGGAAAAGGCCGGTATAGGGG"; // Starts at region 2
        let qual = b"FFFFFFFFFFFFFFFFFFFFFFFFF";
        let flanks = vec![
            FlankingSequences::OpenStart(b"AATT".to_vec()), // missing
            FlankingSequences::Internal(b"CCGG".to_vec(), b"GGCC".to_vec()),
            FlankingSequences::Internal(b"TATA".to_vec(), b"ATAT".to_vec()),
            FlankingSequences::OpenEnd(b"CGCG".to_vec()), // missing
        ];
        let tolerance: u64 = 0;

        let exp: Vec<Option<RegionMatch>> = vec![
            None,
            Some((
                b"AAAA".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Complete,
            )),
            Some((
                b"GGGG".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Partial3Prime,
            )),
            None,
        ];

        let obs =
            match_flank_patterns(seq, qual, &flanks, tolerance).expect("Pattern match failed");
        assert_eq!(obs, exp);
    }

    #[test]
    fn test_flank_matching_with_gap_stops_scan() {
        let seq = b"GGGGAATTGGGGGGGGCGCG"; // Region 2 is missing
        let qual = b"FFFFFFFFFFFFFFFFFFFF";
        let flanks = vec![
            FlankingSequences::OpenStart(b"AATT".to_vec()),
            FlankingSequences::Internal(b"CCGG".to_vec(), b"GGCC".to_vec()), // missing
            FlankingSequences::Internal(b"TATA".to_vec(), b"ATAT".to_vec()), // would match if not skipped
            FlankingSequences::OpenEnd(b"CGCG".to_vec()),
        ];
        let tolerance: u64 = 0;

        let exp: Vec<Option<RegionMatch>> = vec![
            Some((
                b"GGGG".to_vec(),
                b"FFFF".to_vec(),
                RegionCompleteness::Partial5Prime,
            )),
            None,
            None,
            None,
        ];

        let obs =
            match_flank_patterns(seq, qual, &flanks, tolerance).expect("Pattern match failed");
        assert_eq!(obs, exp);
    }

    #[test]
    fn test_empty_flank_list() {
        let seq = b"ACGTACGT";
        let qual = b"FFFFFFFF";
        let flanks: Vec<FlankingSequences> = vec![];
        let tolerance = 0;

        let obs =
            match_flank_patterns(seq, qual, &flanks, tolerance).expect("Pattern match failed");
        assert!(obs.is_empty());
    }
}