streampile 0.2.0

Forward-only pileups streamed from coordinate-sorted BAM records
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
//! The per-read and per-base work of `tabulate`: alleles, runs, normalization, and ledgers.

use std::collections::{BTreeMap, HashMap};

use noodles::bam;
use noodles::sam::alignment::record::Flags;
use noodles::sam::alignment::record::cigar::op::Kind;
use pyo3::exceptions::PyValueError;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::sync::PyOnceLock;
use pyo3::types::{PyDict, PyString, PyTuple};

use super::{Int, bridge};
use crate::DEFAULT_EXCLUDE_FLAGS;
use crate::builder::passes;
use crate::pileup::record_name;

const REFERENCE_PADDING: i64 = 10_000;

/// An allele's reference and alternate bases.
type AlleleKey = (Vec<u8>, Vec<u8>);

/// The reads of an allele on the forward and reverse strands.
type Strands = [i64; 2];

/// A normalized allele: its position and its reference and alternate bases.
type Normalized<T> = (i64, Vec<T>, Vec<T>);

static TABULATED_BASE: PyOnceLock<Py<PyAny>> = PyOnceLock::new();

/// Two sliding windows over one contig of an indexed FASTA, upper-cased.
///
/// A read with a long reference skip reads two far-apart stretches, so the window used last is
/// kept beside the other, and neither is fetched again for the next read.
struct Reference {
    fasta: Py<PyAny>,
    contig: Py<PyString>,
    length: i64,
    windows: Vec<(i64, i64, Vec<u8>)>,
}

impl Reference {
    fn new(fasta: &Bound<'_, PyAny>, contig: &Bound<'_, PyString>) -> PyResult<Self> {
        Ok(Self {
            fasta: fasta.clone().unbind(),
            contig: contig.clone().unbind(),
            length: fasta
                .call_method1("get_reference_length", (contig,))?
                .extract()?,
            windows: Vec::new(),
        })
    }

    /// The upper-cased bases from `start` to `end`, cut to the contig.
    fn get(&mut self, py: Python<'_>, start: i64, end: i64) -> PyResult<&[u8]> {
        let (start, end) = (start.max(0), end.min(self.length));
        let found = self
            .windows
            .iter()
            .position(|(low, high, _)| *low <= start && end <= *high);
        let index = if let Some(index) = found {
            if index > 0 {
                self.windows.reverse();
            }
            0
        } else {
            let low = (start - REFERENCE_PADDING).max(0);
            let high = (end.max(start) + 10 * REFERENCE_PADDING).min(self.length);
            let fetched = self
                .fasta
                .bind(py)
                .call_method1("fetch", (self.contig.bind(py), low, high))?;
            let mut bases = fetched.cast::<PyString>()?.to_str()?.as_bytes().to_vec();
            bases.make_ascii_uppercase();
            self.windows.truncate(1);
            self.windows.insert(0, (low, high, bases));
            0
        };
        let (low, _, bases) = &self.windows[index];
        Ok(slice(bases, start - low, end - low))
    }
}

/// A Python slice of bytes from `start` to `end`, with non-negative bounds cut to the bytes.
fn slice(bytes: &[u8], start: i64, end: i64) -> &[u8] {
    let length = bytes.len() as i64;
    let start = start.clamp(0, length) as usize;
    let end = end.clamp(0, length) as usize;
    &bytes[start..end.max(start)]
}

fn is_acgt(base: u8) -> bool {
    matches!(base, b'A' | b'C' | b'G' | b'T')
}

/// The counts of one territory span while reads are added to it, by strand.
struct Ledger {
    start: i64,
    end: i64,
    depth: Vec<i64>,
    ref_fwd: Vec<i64>,
    ref_rev: Vec<i64>,
    no_calls: Vec<i64>,
    alleles: HashMap<i64, HashMap<AlleleKey, Strands>>,
}

impl Ledger {
    fn new(start: i64, end: i64) -> Self {
        let length = (end - start).max(0) as usize;
        Self {
            start,
            end,
            depth: vec![0; length + 1],
            ref_fwd: vec![0; length + 1],
            ref_rev: vec![0; length + 1],
            no_calls: vec![0; length],
            alleles: HashMap::new(),
        }
    }

    fn add(&mut self, start: i64, end: i64, reference: bool, reverse: bool) {
        let (start, end) = (start.max(self.start), end.min(self.end));
        if start < end {
            let (from, to) = ((start - self.start) as usize, (end - self.start) as usize);
            self.depth[from] += 1;
            self.depth[to] -= 1;
            if reference {
                let counts = if reverse {
                    &mut self.ref_rev
                } else {
                    &mut self.ref_fwd
                };
                counts[from] += 1;
                counts[to] -= 1;
            }
        }
    }

    fn add_no_call(&mut self, pos: i64) {
        if self.start <= pos && pos < self.end {
            self.no_calls[(pos - self.start) as usize] += 1;
        }
    }

    fn add_allele(&mut self, allele: &Allele, reverse: bool) {
        if self.start <= allele.pos && allele.pos < self.end {
            let counts = self
                .alleles
                .entry(allele.pos)
                .or_default()
                .entry((allele.reference.clone(), allele.alternate.clone()))
                .or_default();
            counts[usize::from(reverse)] += 1;
        }
    }
}

/// The rows of a finished ledger, made one at a time as they are asked for.
struct Rows {
    ledger: Ledger,
    bases: Vec<u8>,
    offset: usize,
    depth: i64,
    ref_fwd: i64,
    ref_rev: i64,
}

impl Rows {
    fn new(py: Python<'_>, ledger: Ledger, reference: &mut Reference) -> PyResult<Self> {
        let bases = reference.get(py, ledger.start, ledger.end)?.to_vec();
        Ok(Self {
            ledger,
            bases,
            offset: 0,
            depth: 0,
            ref_fwd: 0,
            ref_rev: 0,
        })
    }

    fn next(
        &mut self,
        py: Python<'_>,
        contig: &Bound<'_, PyString>,
    ) -> PyResult<Option<Py<PyAny>>> {
        let Some(&base) = self.bases.get(self.offset) else {
            return Ok(None);
        };
        let offset = self.offset;
        self.offset += 1;
        let ledger = &mut self.ledger;
        self.depth += ledger.depth[offset];
        self.ref_fwd += ledger.ref_fwd[offset];
        self.ref_rev += ledger.ref_rev[offset];
        let pos = ledger.start + offset as i64;
        let fields = PyDict::new(py);
        fields.set_item(intern!(py, "contig"), contig)?;
        fields.set_item(intern!(py, "pos"), pos + 1)?;
        fields.set_item(intern!(py, "ref"), char::from(base))?;
        fields.set_item(intern!(py, "depth"), self.depth)?;
        fields.set_item(intern!(py, "no_calls"), ledger.no_calls[offset])?;
        fields.set_item(intern!(py, "ref_reads"), self.ref_fwd + self.ref_rev)?;
        fields.set_item(intern!(py, "ref_fwd"), self.ref_fwd)?;
        fields.set_item(intern!(py, "ref_rev"), self.ref_rev)?;
        if let Some(counts) = ledger.alleles.remove(&pos) {
            let mut alleles: Vec<(AlleleKey, Strands)> = counts.into_iter().collect();
            alleles.sort_by(|(a, [a_fwd, a_rev]), (b, [b_fwd, b_rev])| {
                (b_fwd + b_rev).cmp(&(a_fwd + a_rev)).then_with(|| a.cmp(b))
            });
            let text = |bytes: &[u8]| String::from_utf8_lossy(bytes).into_owned();
            let refs = alleles.iter().map(|((reference, _), _)| text(reference));
            let alts = alleles.iter().map(|((_, alternate), _)| text(alternate));
            let count = |pick: fn(&Strands) -> i64| -> PyResult<Bound<'_, PyTuple>> {
                PyTuple::new(py, alleles.iter().map(|(_, counts)| pick(counts)))
            };
            let refs = PyTuple::new(py, refs.collect::<Vec<_>>())?;
            fields.set_item(intern!(py, "alt_refs"), refs)?;
            let alts = PyTuple::new(py, alts.collect::<Vec<_>>())?;
            fields.set_item(intern!(py, "alts"), alts)?;
            fields.set_item(intern!(py, "alt_reads"), count(|[fwd, rev]| fwd + rev)?)?;
            fields.set_item(intern!(py, "alt_fwd"), count(|[fwd, _]| *fwd)?)?;
            fields.set_item(intern!(py, "alt_rev"), count(|[_, rev]| *rev)?)?;
        }
        let class = TABULATED_BASE.get_or_try_init(py, || {
            py.import("streampile._table")?
                .getattr("TabulatedBase")
                .map(Bound::unbind)
        })?;
        class
            .bind(py)
            .call((), Some(&fields))
            .map(|row| Some(row.unbind()))
    }
}

/// One CIGAR operator of a read: its kind, its reference start, its query start, and length.
#[derive(Clone, Copy)]
struct Segment {
    kind: SegmentKind,
    reference: i64,
    query: i64,
    length: i64,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum SegmentKind {
    Aligned,
    Inserted,
    Deleted,
    Skipped,
    Clipped,
}

/// A difference from the reference within one read, in reference and query coordinates.
#[derive(Clone, Copy)]
struct Event {
    ref_start: i64,
    ref_end: i64,
    query_start: i64,
    query_end: i64,
    is_indel: bool,
}

/// Adjacent differences of one read, and whether a matching aligned base borders each end.
///
/// `floor` is the lowest position the run's allele may be left-aligned to: the end of the read's
/// previous difference or reference skip, or else the read's first aligned base.
struct Run {
    events: Vec<Event>,
    anchored: bool,
    closed: bool,
    floor: i64,
}

/// Collects the runs of one read as its aligned bases and indels are walked in order.
struct Runs {
    finished: Vec<Run>,
    events: Vec<Event>,
    anchored: bool,
    floor: i64,
}

impl Runs {
    fn border(&mut self, aligned: bool) {
        if let Some(last) = self.events.last() {
            let floor = last.ref_end;
            self.finished.push(Run {
                events: std::mem::take(&mut self.events),
                anchored: self.anchored,
                closed: aligned,
                floor: self.floor,
            });
            self.floor = floor;
        }
        self.anchored = aligned;
    }
}

/// A normalized VCF allele seen in one read, and the reference stretch it accounts for.
struct Allele {
    pos: i64,
    reference: Vec<u8>,
    alternate: Vec<u8>,
    start: i64,
    end: i64,
}

/// What one read is counted for: its alleles, and the stretches of the alleles it is not
/// counted for and of those that spell the reference.
#[derive(Default)]
struct Counted {
    alleles: Vec<Allele>,
    dropped: Vec<(i64, i64)>,
    matched: Vec<(i64, i64)>,
}

/// One read, decoded as `tabulate` reads it.
#[derive(Default)]
struct Read {
    start: i64,
    reverse: bool,
    segments: Vec<Segment>,
    sequence: Vec<u8>,
    qualities: Vec<u8>,
}

impl Read {
    /// Decodes a record, reading each aligned base written as `=` as the reference base there.
    fn fill(
        &mut self,
        py: Python<'_>,
        record: &bam::Record,
        contig: &mut Reference,
    ) -> PyResult<()> {
        let invalid = |error: std::io::Error| PyValueError::new_err(error.to_string());
        self.start = record
            .alignment_start()
            .transpose()
            .map_err(invalid)?
            .map_or(-1, |start| usize::from(start) as i64 - 1);
        self.reverse = record.flags().is_reverse_complemented();
        self.sequence.clear();
        self.sequence.extend(record.sequence().iter());
        self.qualities.clear();
        self.qualities
            .extend_from_slice(record.quality_scores().as_bytes());
        self.segments.clear();
        let (mut reference, mut query) = (self.start, 0);
        for op in record.cigar().iter() {
            let op = op.map_err(invalid)?;
            let length = op.len() as i64;
            let segment = |kind| Segment {
                kind,
                reference,
                query,
                length,
            };
            match op.kind() {
                Kind::Match | Kind::SequenceMatch | Kind::SequenceMismatch => {
                    self.segments.push(segment(SegmentKind::Aligned));
                    reference += length;
                    query += length;
                }
                Kind::Insertion => {
                    self.segments.push(segment(SegmentKind::Inserted));
                    query += length;
                }
                Kind::Deletion => {
                    self.segments.push(segment(SegmentKind::Deleted));
                    reference += length;
                }
                Kind::Skip => {
                    self.segments.push(segment(SegmentKind::Skipped));
                    reference += length;
                }
                Kind::SoftClip => {
                    self.segments.push(segment(SegmentKind::Clipped));
                    query += length;
                }
                Kind::HardClip | Kind::Pad => {}
            }
        }
        if self.sequence.contains(&b'=') {
            for segment in &self.segments {
                if segment.kind != SegmentKind::Aligned {
                    continue;
                }
                let end = segment.reference + segment.length;
                let bases = contig.get(py, segment.reference, end)?;
                let from = segment.query.clamp(0, self.sequence.len() as i64) as usize;
                for (base, &reference_base) in self.sequence[from..].iter_mut().zip(bases) {
                    if *base == b'=' {
                        *base = reference_base;
                    }
                }
            }
        }
        Ok(())
    }

    /// The runs of adjacent differences from the reference, in alignment order.
    fn runs(&self, py: Python<'_>, reference: &mut Reference) -> PyResult<Vec<Run>> {
        let mut runs = Runs {
            finished: Vec::new(),
            events: Vec::new(),
            anchored: false,
            floor: self.start,
        };
        for segment in &self.segments {
            match segment.kind {
                SegmentKind::Aligned => {
                    let bases =
                        reference.get(py, segment.reference, segment.reference + segment.length)?;
                    let read = slice(
                        &self.sequence,
                        segment.query,
                        segment.query + segment.length,
                    );
                    let mut previous = -1;
                    if read != bases {
                        for (offset, (base, ref_base)) in read.iter().zip(bases).enumerate() {
                            if base != ref_base {
                                let offset = offset as i64;
                                if offset > previous + 1 {
                                    runs.border(true);
                                }
                                let at = segment.reference + offset;
                                let query_at = segment.query + offset;
                                runs.events.push(Event {
                                    ref_start: at,
                                    ref_end: at + 1,
                                    query_start: query_at,
                                    query_end: query_at + 1,
                                    is_indel: false,
                                });
                                previous = offset;
                            }
                        }
                    }
                    let aligned = bases.len() as i64;
                    if previous < aligned - 1 {
                        runs.border(true);
                    }
                    if aligned < segment.length {
                        runs.border(false);
                    }
                }
                SegmentKind::Inserted => runs.events.push(Event {
                    ref_start: segment.reference,
                    ref_end: segment.reference,
                    query_start: segment.query,
                    query_end: segment.query + segment.length,
                    is_indel: true,
                }),
                SegmentKind::Deleted => runs.events.push(Event {
                    ref_start: segment.reference,
                    ref_end: segment.reference + segment.length,
                    query_start: segment.query,
                    query_end: segment.query,
                    is_indel: true,
                }),
                SegmentKind::Skipped | SegmentKind::Clipped => {
                    runs.border(false);
                    if segment.kind == SegmentKind::Skipped {
                        runs.floor = segment.reference + segment.length;
                    }
                }
            }
        }
        runs.border(false);
        Ok(runs.finished)
    }
}

/// The options of a tabulation.
#[derive(Clone, Copy)]
struct Options {
    min_base_quality: u8,
    min_mapping_quality: u8,
    exclude_flags: u16,
}

impl Options {
    /// Whether a read passes the flag and mapping-quality filters a builder applies and has
    /// bases to count: a builder piles up a read with no stored bases (SEQ `*`), as htslib does,
    /// where its alleles cannot be counted. Tabulate has no proper-pair filter or read filter.
    fn accepts(self, record: &bam::Record) -> bool {
        passes(
            record.flags(),
            record.mapping_quality().map_or(255, u8::from),
            Flags::from_bits_retain(self.exclude_flags),
            self.min_mapping_quality,
        ) && !record.sequence().is_empty()
            && !record.cigar().is_empty()
    }

    /// The alleles a read is counted for, and the stretches of the alleles it is not counted
    /// for and of those that spell the reference.
    fn alleles(self, py: Python<'_>, read: &Read, reference: &mut Reference) -> PyResult<Counted> {
        let qualities =
            (self.min_base_quality > 0 && !read.qualities.is_empty()).then_some(&read.qualities);
        let mut counted = Counted::default();
        for run in read.runs(py, reference)? {
            let (first, last) = (run.events[0], run.events[run.events.len() - 1]);
            let (mut ref_start, mut query_start) = (first.ref_start, first.query_start);
            let (ref_end, query_end) = (last.ref_end, last.query_end);
            if first.is_indel {
                if !run.anchored {
                    counted.dropped.push((ref_start, ref_end));
                    continue;
                }
                ref_start -= 1;
                query_start -= 1;
            }
            let reference_bases = reference.get(py, ref_start, ref_end)?.to_vec();
            let alternate = slice(&read.sequence, query_start, query_end).to_vec();
            let opens_with_insertion = first.is_indel && first.ref_end == first.ref_start;
            let closes_with_deletion = last.is_indel && last.ref_end > last.ref_start;
            let judged_from = if opens_with_insertion {
                query_start
            } else {
                first.query_start
            };
            let judged_to = if closes_with_deletion {
                query_end + 1
            } else {
                query_end
            };
            let under_floor = qualities.is_some_and(|qualities| {
                slice(qualities, judged_from, judged_to)
                    .iter()
                    .min()
                    .is_some_and(|&lowest| lowest < self.min_base_quality)
            });
            if (closes_with_deletion && !run.closed)
                || under_floor
                || alternate.contains(&b'N')
                || !reference_bases.iter().copied().all(is_acgt)
            {
                counted.dropped.push((ref_start, ref_end));
                continue;
            }
            if reference_bases == alternate {
                counted.matched.push((ref_start, ref_end));
                continue;
            }
            let normalized = normalize(
                ref_start,
                reference_bases,
                alternate,
                |start, end| reference.get(py, start, end).map(<[u8]>::to_vec),
                run.floor,
            )?;
            let Some((pos, reference_bases, alternate)) = normalized else {
                counted.dropped.push((run.floor, ref_end));
                continue;
            };
            let start = ref_start.min(pos);
            let end = ref_end.max(pos + reference_bases.len() as i64);
            if !reference
                .get(py, start, ref_start)?
                .iter()
                .copied()
                .all(is_acgt)
            {
                counted.dropped.push((start, end));
                continue;
            }
            counted.alleles.push(Allele {
                pos,
                reference: reference_bases,
                alternate,
                start,
                end,
            });
        }
        Ok(counted)
    }

    /// The aligned positions of a read under the floor or over a non-ACGT base, and its `N`s.
    fn uninformative(
        self,
        py: Python<'_>,
        read: &Read,
        reference: &mut Reference,
    ) -> PyResult<(Vec<i64>, Vec<i64>)> {
        let floor = self.min_base_quality;
        let low = floor > 0 && !read.qualities.is_empty();
        let mut uninformative = Vec::new();
        let mut no_calls = Vec::new();
        for segment in &read.segments {
            if segment.kind != SegmentKind::Aligned {
                continue;
            }
            let end = segment.query + segment.length;
            let at = |offset: i64| segment.reference + offset;
            let from = segment.query.clamp(0, read.sequence.len() as i64);
            for (offset, &base) in slice(&read.sequence, segment.query, end).iter().enumerate() {
                if base == b'N' {
                    no_calls.push(at(from + offset as i64 - segment.query));
                }
            }
            if low {
                let from = segment.query.clamp(0, read.qualities.len() as i64);
                for (offset, &quality) in slice(&read.qualities, segment.query, end)
                    .iter()
                    .enumerate()
                {
                    if quality < floor {
                        uninformative.push(at(from + offset as i64 - segment.query));
                    }
                }
            }
            let bases = reference.get(py, segment.reference, segment.reference + segment.length)?;
            for (offset, &base) in bases.iter().enumerate() {
                if !is_acgt(base) {
                    uninformative.push(segment.reference + offset as i64);
                }
            }
        }
        Ok((uninformative, no_calls))
    }
}

/// Trims and left-aligns an allele as `bcftools norm` does, or returns `None` for an allele
/// that changes nothing or would move past `floor`.
fn normalize<T: Copy + Eq>(
    mut pos: i64,
    mut reference: Vec<T>,
    mut alternate: Vec<T>,
    mut bases: impl FnMut(i64, i64) -> PyResult<Vec<T>>,
    floor: i64,
) -> PyResult<Option<Normalized<T>>> {
    if reference == alternate {
        return Ok(None);
    }
    if reference.is_empty() || alternate.is_empty() {
        return Err(PyValueError::new_err(
            "An allele needs a reference and an alternate base.",
        ));
    }
    while reference.last() == alternate.last() {
        reference.pop();
        alternate.pop();
        if reference.is_empty() || alternate.is_empty() {
            if pos - 1 < floor {
                return Ok(None);
            }
            pos -= 1;
            let base = bases(pos, pos + 1)?;
            if base.is_empty() {
                return Err(PyValueError::new_err(format!(
                    "The reference has no base at position {pos}."
                )));
            }
            reference.splice(0..0, base.iter().copied());
            alternate.splice(0..0, base);
        }
    }
    let trimmed = reference
        .iter()
        .zip(&alternate)
        .take(reference.len().min(alternate.len()).saturating_sub(1))
        .take_while(|(a, b)| a == b)
        .count();
    reference.drain(..trimmed);
    alternate.drain(..trimmed);
    Ok(Some((pos + trimmed as i64, reference, alternate)))
}

/// Trim and left-align an allele as `bcftools norm` does.
#[pyfunction]
#[pyo3(signature = (pos, reference_bases, alternate_bases, reference, floor = 0))]
pub(crate) fn normalize_allele(
    pos: i64,
    reference_bases: &str,
    alternate_bases: &str,
    reference: &Bound<'_, PyAny>,
    floor: i64,
) -> PyResult<Option<(i64, String, String)>> {
    let normalized = normalize(
        pos,
        reference_bases.chars().collect(),
        alternate_bases.chars().collect(),
        |start, end| {
            let bases = reference.call1((start, end))?;
            Ok(bases.cast::<PyString>()?.to_str()?.chars().collect())
        },
        floor,
    )?;
    Ok(normalized.map(|(pos, reference, alternate)| {
        (
            pos,
            reference.into_iter().collect(),
            alternate.into_iter().collect(),
        )
    }))
}

/// The per-read and per-base work of a `Tabulator`, over one reference.
#[pyclass(module = "streampile._native")]
pub(crate) struct Tabulation {
    fasta: Py<PyAny>,
    options: Options,
    reference: Option<Reference>,
}

#[pymethods]
impl Tabulation {
    #[new]
    #[pyo3(signature = (
        reference,
        *,
        min_base_quality = Int::from(0),
        min_mapping_quality = Int::from(0),
        exclude_flags = Int::from(i64::from(DEFAULT_EXCLUDE_FLAGS.bits())),
    ))]
    #[pyo3(
        text_signature = "(reference, *, min_base_quality=0, min_mapping_quality=0, \
        exclude_flags=3840)"
    )]
    fn new(
        reference: &Bound<'_, PyAny>,
        min_base_quality: Int,
        min_mapping_quality: Int,
        exclude_flags: Int,
    ) -> PyResult<Self> {
        Ok(Self {
            fasta: reference.clone().unbind(),
            options: Options {
                min_base_quality: min_base_quality.of("min_base_quality", u8::MAX)?,
                min_mapping_quality: min_mapping_quality.of("min_mapping_quality", u8::MAX)?,
                exclude_flags: exclude_flags.of("exclude_flags", u16::MAX)?,
            },
            reference: None,
        })
    }

    /// The indexed reference the reads are aligned to.
    #[getter]
    fn reference(&self, py: Python<'_>) -> Py<PyAny> {
        self.fasta.clone_ref(py)
    }

    /// The lowest base quality of an informative base.
    #[getter]
    fn min_base_quality(&self) -> u8 {
        self.options.min_base_quality
    }

    /// The lowest mapping quality of a counted read.
    #[getter]
    fn min_mapping_quality(&self) -> u8 {
        self.options.min_mapping_quality
    }

    /// The SAM flags of reads that are not counted.
    #[getter]
    fn exclude_flags(&self) -> u16 {
        self.options.exclude_flags
    }

    /// Whether a read passes the flag and mapping-quality filters and has bases to count.
    fn accepts(&self, record: &Bound<'_, PyAny>) -> PyResult<bool> {
        let mut bam = bam::Record::default();
        bridge::read(record, &mut bam)?;
        Ok(self.options.accepts(&bam))
    }

    /// The alleles a read is counted for, as `(pos, ref, alt, start, end)`, and the stretches of
    /// those it is not counted for.
    #[allow(clippy::type_complexity)]
    fn alleles(
        &mut self,
        py: Python<'_>,
        record: &Bound<'_, PyAny>,
    ) -> PyResult<(Vec<(i64, String, String, i64, i64)>, Vec<(i64, i64)>)> {
        let mut bam = bam::Record::default();
        bridge::read(record, &mut bam)?;
        let contig = record.getattr("reference_name")?;
        if contig.is_none() || bam.flags().is_unmapped() {
            return Err(PyValueError::new_err(format!(
                "Read {} is not mapped.",
                record_name(&bam)
            )));
        }
        let contig = contig.cast_into::<PyString>()?;
        let reference = match self.reference.take() {
            Some(reference) if reference.contig.bind(py).to_cow()? == contig.to_cow()? => reference,
            _ => Reference::new(self.fasta.bind(py), &contig)?,
        };
        let reference = self.reference.insert(reference);
        let mut read = Read::default();
        read.fill(py, &bam, reference)?;
        let counted = self.options.alleles(py, &read, reference)?;
        let text = |bytes: Vec<u8>| String::from_utf8_lossy(&bytes).into_owned();
        Ok((
            counted
                .alleles
                .into_iter()
                .map(|allele| {
                    (
                        allele.pos,
                        text(allele.reference),
                        text(allele.alternate),
                        allele.start,
                        allele.end,
                    )
                })
                .collect(),
            counted.dropped,
        ))
    }

    /// The rows of every base of the spans of one contig, in order, from reads fetched span by
    /// span from an indexed alignment file.
    fn tabulate_contig(
        &self,
        py: Python<'_>,
        alignments: &Bound<'_, PyAny>,
        contig: &Bound<'_, PyString>,
        spans: Vec<(i64, i64)>,
    ) -> PyResult<ContigRows> {
        Ok(ContigRows {
            alignments: alignments.clone().unbind(),
            contig: contig.clone().unbind(),
            reference: Reference::new(self.fasta.bind(py), contig)?,
            options: self.options,
            starts: spans.iter().map(|&(start, _)| start).collect(),
            spans,
            ledgers: BTreeMap::new(),
            next_span: 0,
            fetched_to: -1,
            rows: None,
            record: bam::Record::default(),
            read: Read::default(),
        })
    }
}

/// The rows of every base of the spans of one contig, made one at a time.
#[pyclass(module = "streampile._native")]
pub(crate) struct ContigRows {
    alignments: Py<PyAny>,
    contig: Py<PyString>,
    reference: Reference,
    options: Options,
    spans: Vec<(i64, i64)>,
    starts: Vec<i64>,
    ledgers: BTreeMap<usize, Ledger>,
    next_span: usize,
    fetched_to: i64,
    rows: Option<Rows>,
    record: bam::Record,
    read: Read,
}

#[pymethods]
impl ContigRows {
    fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
        slf
    }

    fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
        loop {
            if let Some(rows) = self.rows.as_mut()
                && let Some(row) = rows.next(py, self.contig.bind(py))?
            {
                return Ok(Some(row));
            }
            let Some(&(start, end)) = self.spans.get(self.next_span) else {
                return Ok(None);
            };
            let index = self.next_span;
            self.next_span += 1;
            let records = self
                .alignments
                .bind(py)
                .call_method1("fetch", (self.contig.bind(py), start, end))?;
            for record in records.try_iter()? {
                let record = record?;
                if bridge::reference_start(&record)? < self.fetched_to {
                    continue;
                }
                bridge::read(&record, &mut self.record)?;
                self.count(py)?;
            }
            self.fetched_to = end;
            let ledger = self
                .ledgers
                .remove(&index)
                .unwrap_or_else(|| Ledger::new(start, end));
            self.rows = Some(Rows::new(py, ledger, &mut self.reference)?);
        }
    }
}

impl ContigRows {
    /// Counts the read last fetched, unless a filter leaves it out.
    fn count(&mut self, py: Python<'_>) -> PyResult<()> {
        let record = &self.record;
        if !self.options.accepts(record) {
            return Ok(());
        }
        let start = record
            .alignment_start()
            .transpose()
            .map_err(|error| PyValueError::new_err(error.to_string()))?
            .map_or(-1, |start| usize::from(start) as i64 - 1);
        let end = end_of(record, start);
        let touched = self.touch(start, end);
        if touched.is_empty() {
            return Ok(());
        }
        self.read.fill(py, &self.record, &mut self.reference)?;
        let read = &self.read;
        let reverse = read.reverse;
        let counted = self.options.alleles(py, read, &mut self.reference)?;
        let (uninformative, no_calls) =
            self.options.uninformative(py, read, &mut self.reference)?;
        let mut excluded = uninformative;
        excluded.extend_from_slice(&no_calls);
        let ledgers = &mut self.ledgers;
        let mut each = |apply: &mut dyn FnMut(&mut Ledger)| {
            for index in &touched {
                if let Some(ledger) = ledgers.get_mut(index) {
                    apply(ledger);
                }
            }
        };
        for &pos in &no_calls {
            each(&mut |ledger| ledger.add_no_call(pos));
        }
        for &(start, end) in &counted.dropped {
            excluded.extend(start..end);
        }
        for allele in &counted.alleles {
            let after = allele.pos + allele.reference.len() as i64;
            each(&mut |ledger| {
                ledger.add(allele.start, allele.pos, true, reverse);
                ledger.add(allele.pos, after, false, reverse);
                ledger.add(after, allele.end, true, reverse);
                ledger.add_allele(allele, reverse);
            });
            excluded.extend(allele.start..allele.end);
        }
        for &(start, end) in &counted.matched {
            each(&mut |ledger| ledger.add(start, end, true, reverse));
            excluded.extend(start..end);
        }
        excluded.sort_unstable();
        excluded.dedup();
        for segment in &read.segments {
            if segment.kind != SegmentKind::Aligned {
                continue;
            }
            let (block_start, block_end) = (segment.reference, segment.reference + segment.length);
            let from = excluded.partition_point(|&pos| pos < block_start);
            let to = excluded.partition_point(|&pos| pos < block_end);
            let mut cursor = block_start;
            for &pos in &excluded[from..to] {
                each(&mut |ledger| ledger.add(cursor, pos, true, reverse));
                cursor = pos + 1;
            }
            each(&mut |ledger| ledger.add(cursor, block_end, true, reverse));
        }
        Ok(())
    }

    /// The indices of the spans a read from `start` to `end` overlaps, with their ledgers opened
    /// as needed.
    fn touch(&mut self, start: i64, end: i64) -> Vec<usize> {
        let mut touched = Vec::new();
        let mut index = self
            .starts
            .partition_point(|&first| first <= start)
            .saturating_sub(1);
        while let Some(&(span_start, span_end)) = self.spans.get(index) {
            if span_start >= end {
                break;
            }
            if span_end > start {
                self.ledgers
                    .entry(index)
                    .or_insert_with(|| Ledger::new(span_start, span_end));
                touched.push(index);
            }
            index += 1;
        }
        touched
    }
}

/// Where a read ends on the reference, one past its start for one that spans no base, as pysam's
/// `reference_end` gives.
fn end_of(record: &bam::Record, start: i64) -> i64 {
    use noodles::sam::alignment::record::Cigar as _;
    let cigar = record.cigar();
    if record.flags().is_unmapped() || cigar.is_empty() {
        start
    } else {
        start + (cigar.alignment_span().unwrap_or(0) as i64).max(1)
    }
}