zoe 0.0.30

A nightly library for viral genomics
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
use crate::alignment::{Alignment, AlignmentStates};
use std::{
    mem::MaybeUninit,
    simd::{Select, prelude::*},
};

/// Experimental backtracking matrix for alignment with affine gap scores.
#[derive(Clone, Debug)]
pub(crate) struct BacktrackMatrix {
    pub data: Vec<u8>,
    cols:     usize,
    cursor:   usize,
}

impl BacktrackMatrix {
    /// The bitflag used to represent a score that can be reached via opening a
    /// gap from the cell above it
    const UP: u8 = 1;

    /// The bitflag used to represent a score that can be reached via extending
    /// a gap from the cell above it
    const UP_EXTENDING: u8 = 2;

    /// The bitfag used to represent a score that can be reached via opening a
    /// gap from the cell left of it
    const LEFT: u8 = 4;

    /// The bitflag used to represent a score that can be reached via extending
    /// a gap from the cell left of it
    const LEFT_EXTENDING: u8 = 8;

    /// The bitflag used to indicate that an alignment starts at a given cell
    /// (i.e., the traceback should stop at that cell)
    const STOP: u8 = 16;

    /// Creates a new [`BacktrackMatrix`] with the specified number of rows and
    /// columns.
    ///
    /// The number of rows should be the length of the reference, and the number
    /// of columns should be the length of the query.
    pub(crate) fn new(rows: usize, cols: usize) -> Self {
        BacktrackMatrix {
            data: vec![0u8; rows * cols],
            cols,
            cursor: 0,
        }
    }

    /// Sets the [`UP`] flag for the current cell in the [`BacktrackMatrix`].
    ///
    /// [`UP`]: Self::UP
    #[inline]
    pub(crate) fn up(&mut self) {
        self.data[self.cursor] |= Self::UP;
    }

    /// Sets the [`LEFT`] flag for the current cell in the [`BacktrackMatrix`].
    ///
    /// [`LEFT`]: Self::LEFT
    #[inline]
    pub(crate) fn left(&mut self) {
        self.data[self.cursor] |= Self::LEFT;
    }

    /// Sets the [`UP_EXTENDING`] flag for the current cell in the
    /// [`BacktrackMatrix`].
    ///
    /// [`UP_EXTENDING`]: Self::UP_EXTENDING
    #[inline]
    pub(crate) fn up_extending(&mut self) {
        self.data[self.cursor] |= Self::UP_EXTENDING;
    }

    /// Sets the [`LEFT_EXTENDING`] flag for the current cell in the
    /// [`BacktrackMatrix`].
    ///
    /// [`LEFT_EXTENDING`]: Self::LEFT_EXTENDING
    #[inline]
    pub(crate) fn left_extending(&mut self) {
        self.data[self.cursor] |= Self::LEFT_EXTENDING;
    }

    /// Clears all flags then sets the [`STOP`] flag for the current cell in the
    /// [`BacktrackMatrix`].
    ///
    /// [`STOP`]: Self::STOP
    #[inline]
    pub(crate) fn stop(&mut self) {
        self.data[self.cursor] = Self::STOP;
    }
}

/// A backtrack matrix for the Striped Smith Waterman algorithm.
///
/// This stores the traceback information in a flattened vector of SIMD vectors
/// (each holding `N` entries in the DP table).
#[derive(Clone, Debug)]
pub(crate) struct BacktrackMatrixStriped<const N: usize> {
    pub data:  Vec<Simd<u8, N>>,
    num_vecs:  usize,
    v_cursor:  usize,
    curr_lane: usize,
}

impl<const N: usize> BacktrackMatrixStriped<N> {
    /// Allocates lazily-initialized backtrack data which can later be converted
    /// to a [`BacktrackMatrixStriped`].
    ///
    /// The total number of SIMD vectors is given by `size`.
    #[inline]
    #[must_use]
    pub(crate) fn make_uninit_data(size: usize) -> Vec<MaybeUninit<Simd<u8, N>>> {
        let mut data = Vec::with_capacity(size);
        data.resize_with(size, MaybeUninit::uninit);
        data
    }

    /// Wraps a vector of SIMD vectors in a [`BacktrackMatrixStriped`] to
    /// facilitate backtracking.
    #[inline]
    #[must_use]
    pub(crate) fn new(data: Vec<Simd<u8, N>>, num_vecs: usize) -> Self {
        assert!(data.len().is_multiple_of(num_vecs));
        BacktrackMatrixStriped {
            data,
            num_vecs,
            v_cursor: 0,
            curr_lane: 0,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn debug_cell(&self, r: usize, c: usize) -> String {
        let v = c % self.num_vecs;
        let lane = (c - v) / self.num_vecs;
        let v = self.data[self.num_vecs * r + v];
        v.debug_cell(lane)
    }

    #[allow(dead_code)]
    pub(crate) fn print_row(&self, r: usize) {
        let mut bt = self.clone();
        let nc = self.num_vecs * N;

        print!("{r:02}: x");
        for c in 0..nc {
            bt.move_to(r, c);
            if bt.is_stop() {
                print!("o");
            } else if bt.is_up() {
                print!("^");
            } else if bt.is_left() {
                print!("<");
            } else if bt.is_up_extending() {
                print!(":");
            } else if bt.is_left_extending() {
                print!("-");
            } else {
                print!("\\");
            }
        }
        println!();
    }
}

/// Trait for SIMD backtracking operations on packed u8 values
pub(crate) trait SimdBacktrackFlags<const N: usize> {
    const UP: Simd<u8, N>;
    const UP_EXTENDING: Simd<u8, N>;
    const LEFT: Simd<u8, N>;
    const LEFT_EXTENDING: Simd<u8, N>;
    const STOP: Simd<u8, N>;

    /// Creates a new match variable
    fn simd_match() -> Simd<u8, N> {
        Simd::splat(0)
    }

    /// Add Up direction to lanes selected by mask
    fn simd_up(&mut self, mask: Mask<i8, N>);

    /// Add Left direction to lanes selected by mask
    fn simd_left(&mut self, mask: Mask<i8, N>);

    fn simd_correct_and_set_left(&mut self, mask: Mask<i8, N>);

    /// Apply Up Extending direction to lanes selected by mask
    fn simd_up_extending(&mut self, mask: Mask<i8, N>);

    /// Add Left Extending direction to lanes selected by mask
    fn simd_left_extending(&mut self, mask: Mask<i8, N>);

    /// Set Stop state to lanes selected by mask
    fn simd_stop(&mut self, mask: Mask<i8, N>);

    /// Debug representation of a cell at the given lane
    fn debug_cell(&self, lane: usize) -> String;
}

impl<const N: usize> SimdBacktrackFlags<N> for Simd<u8, N> {
    const UP: Simd<u8, N> = Simd::splat(BacktrackMatrix::UP);
    const UP_EXTENDING: Simd<u8, N> = Simd::splat(BacktrackMatrix::UP_EXTENDING);
    const LEFT: Simd<u8, N> = Simd::splat(BacktrackMatrix::LEFT);
    const LEFT_EXTENDING: Simd<u8, N> = Simd::splat(BacktrackMatrix::LEFT_EXTENDING);
    const STOP: Simd<u8, N> = Simd::splat(BacktrackMatrix::STOP);

    #[inline]
    fn simd_up(&mut self, mask: Mask<i8, N>) {
        *self = mask.select(*self | Self::UP, *self);
    }

    #[inline]
    fn simd_left(&mut self, mask: Mask<i8, N>) {
        *self = mask.select(*self | Self::LEFT, *self);
    }

    #[inline]
    fn simd_correct_and_set_left(&mut self, mask: Mask<i8, N>) {
        *self = mask.select((*self & Self::UP_EXTENDING) | Self::LEFT, *self);
    }

    #[inline]
    fn simd_up_extending(&mut self, mask: Mask<i8, N>) {
        *self = mask.select(*self | Self::UP_EXTENDING, *self);
    }

    #[inline]
    fn simd_left_extending(&mut self, mask: Mask<i8, N>) {
        *self = mask.select(*self | Self::LEFT_EXTENDING, *self);
    }

    #[inline]
    fn simd_stop(&mut self, mask: Mask<i8, N>) {
        *self = mask.select(Self::STOP, *self);
    }

    fn debug_cell(&self, lane: usize) -> String {
        let cell = self[lane];

        let mut states = String::new();
        if cell & BacktrackMatrix::STOP > 0 {
            states.push('o');
        }

        if cell & BacktrackMatrix::UP > 0 {
            states.push('^');
        }

        if cell & BacktrackMatrix::LEFT > 0 {
            states.push('<');
        }

        if cell & BacktrackMatrix::UP_EXTENDING > 0 {
            states.push(':');
        }

        if cell & BacktrackMatrix::LEFT_EXTENDING > 0 {
            states.push('-');
        }

        if cell == 0 {
            states.push('\\');
        }

        states
    }
}

/// Trait for backtracking operations on a backtracking matrix
pub(crate) trait BackTrackable {
    /// Moves the cursor to the specified row and column
    fn move_to(&mut self, r: usize, c: usize);

    /// Checks if the current cell is in the **up** state
    fn is_up(&self) -> bool;

    /// Checks if the current cell is in the **left** state
    fn is_left(&self) -> bool;

    /// Checks if the current cell is in the **left extending** state
    fn is_left_extending(&self) -> bool;

    /// Checks if the current cell is in the **up extending** state
    fn is_up_extending(&self) -> bool;

    /// Checks if the current cell is in the **stop** state
    fn is_stop(&self) -> bool;

    /// Creates an [`Alignment`] from a backtrack matrix and other information.
    fn to_alignment<T>(
        &mut self, score: T, mut r_end: usize, mut c_end: usize, ref_len: usize, query_len: usize,
    ) -> Alignment<T> {
        let mut states = AlignmentStates::new();
        let mut op = 0;

        self.move_to(r_end, c_end); // 0-based move to max

        // 1-based as though we had a padded matrix
        r_end += 1;
        c_end += 1;

        let (mut r, mut c) = (r_end, c_end);

        // soft clip 3'
        states.soft_clip(query_len - c);

        while !self.is_stop() && r > 0 && c > 0 {
            if op == b'D' && self.is_up_extending() {
                op = b'D';
                r -= 1;
            } else if op == b'I' && self.is_left_extending() {
                op = b'I';
                c -= 1;
            } else if self.is_up() {
                op = b'D';
                r -= 1;
            } else if self.is_left() {
                op = b'I';
                c -= 1;
            } else {
                op = b'M';
                r -= 1;
                c -= 1;
            }
            states.add_state(op);
            self.move_to(r.saturating_sub(1), c.saturating_sub(1)); // 0-based next position
        }

        // soft clip 5'
        states.soft_clip(c);
        states.make_reverse();

        // r and c are decremented and becomes 0-based
        Alignment {
            score,
            ref_range: r..r_end,
            query_range: c..c_end,
            states,
            ref_len,
            query_len,
        }
    }

    /// Creates an [`Alignment`] representing a global alignment from a
    /// backtrack matrix.
    fn to_alignment_global<T>(&mut self, score: T, ref_len: usize, query_len: usize) -> Alignment<T> {
        let mut states = AlignmentStates::new();
        let mut op = 0;

        let Some(mut r) = ref_len.checked_sub(1) else {
            // reference is empty
            states.add_inc_op(query_len, b'I');
            return Alignment::new_global(score, states, ref_len, query_len);
        };

        let Some(mut c) = query_len.checked_sub(1) else {
            // query is empty
            states.add_inc_op(ref_len, b'D');
            return Alignment::new_global(score, states, ref_len, query_len);
        };

        self.move_to(r, c); // 0-based move to max

        // 1-based as though we had a padded matrix
        r += 1;
        c += 1;

        loop {
            if op == b'D' && self.is_up_extending() {
                op = b'D';
                r -= 1;
            } else if op == b'I' && self.is_left_extending() {
                op = b'I';
                c -= 1;
            } else if self.is_up() {
                op = b'D';
                r -= 1;
            } else if self.is_left() {
                op = b'I';
                c -= 1;
            } else {
                op = b'M';
                r -= 1;
                c -= 1;
            }
            states.add_state(op);

            if r == 0 {
                states.add_inc_op(c, b'I');
                states.make_reverse();
                return Alignment::new_global(score, states, ref_len, query_len);
            } else if c == 0 {
                states.add_inc_op(r, b'D');
                states.make_reverse();
                return Alignment::new_global(score, states, ref_len, query_len);
            }

            self.move_to(r - 1, c - 1); // 0-based next position
        }
    }

    /// Prints the backtracking matrix in a human-readable format
    #[allow(dead_code)]
    fn print(&self);
}

impl BackTrackable for BacktrackMatrix {
    #[inline]
    fn move_to(&mut self, r: usize, c: usize) {
        self.cursor = self.cols * r + c;
    }

    #[inline]
    fn is_up(&self) -> bool {
        self.data[self.cursor] & Self::UP > 0
    }

    #[inline]
    fn is_left(&self) -> bool {
        self.data[self.cursor] & Self::LEFT > 0
    }

    #[inline]
    fn is_left_extending(&self) -> bool {
        self.data[self.cursor] & Self::LEFT_EXTENDING > 0
    }

    #[inline]
    fn is_up_extending(&self) -> bool {
        self.data[self.cursor] & Self::UP_EXTENDING > 0
    }

    #[inline]
    fn is_stop(&self) -> bool {
        self.data[self.cursor] & Self::STOP > 0
    }

    fn print(&self) {
        let mut bt = self.clone();
        let nr = self.data.len() / self.cols;
        let nc = self.cols;

        print!("    ");
        for _ in 0..=nc {
            print!("x");
        }
        println!();
        for r in 0..nr {
            print!("{r:02}: x");
            for c in 0..nc {
                bt.move_to(r, c);
                if bt.is_stop() {
                    print!("o");
                } else if bt.is_up() {
                    print!("^");
                } else if bt.is_left() {
                    print!("<");
                } else if bt.is_up_extending() {
                    print!(":");
                } else if bt.is_left_extending() {
                    print!("-");
                } else {
                    print!("\\");
                }
            }
            println!();
        }
    }
}

impl<const N: usize> BackTrackable for BacktrackMatrixStriped<N> {
    #[inline]
    fn move_to(&mut self, r: usize, c: usize) {
        let v = c % self.num_vecs;
        self.curr_lane = (c - v) / self.num_vecs;
        self.v_cursor = self.num_vecs * r + v;
    }

    #[inline]
    fn is_up(&self) -> bool {
        (self.data[self.v_cursor][self.curr_lane] & BacktrackMatrix::UP) > 0
    }

    #[inline]
    fn is_left(&self) -> bool {
        (self.data[self.v_cursor][self.curr_lane] & BacktrackMatrix::LEFT) > 0
    }

    #[inline]
    fn is_left_extending(&self) -> bool {
        (self.data[self.v_cursor][self.curr_lane] & BacktrackMatrix::LEFT_EXTENDING) > 0
    }

    #[inline]
    fn is_up_extending(&self) -> bool {
        (self.data[self.v_cursor][self.curr_lane] & BacktrackMatrix::UP_EXTENDING) > 0
    }

    #[inline]
    fn is_stop(&self) -> bool {
        (self.data[self.v_cursor][self.curr_lane] & BacktrackMatrix::STOP) > 0
    }

    fn print(&self) {
        let mut bt = self.clone();
        let nr = self.data.len() / self.num_vecs;
        let nc = self.num_vecs * N;

        print!("    ");
        for _ in 0..=nc {
            print!("x");
        }
        println!();
        for r in 0..nr {
            print!("{r:02}: x");
            for c in 0..nc {
                bt.move_to(r, c);
                if bt.is_stop() {
                    print!("o");
                } else if bt.is_up() {
                    print!("^");
                } else if bt.is_left() {
                    print!("<");
                } else if bt.is_up_extending() {
                    print!(":");
                } else if bt.is_left_extending() {
                    print!("-");
                } else {
                    print!("\\");
                }
            }
            println!();
        }
    }
}

/// A backtrack matrix for the Banded Smith Waterman algorithm.
///
/// This stores the traceback information for the diagonal band in a flattened
/// vector of bytes (using one byte per DP table entry).
#[derive(Clone, Debug)]
pub(crate) struct BandedBacktrackMatrix {
    data:       Vec<u8>,
    band_width: usize,
    cursor:     usize,
}

impl BandedBacktrackMatrix {
    /// The bitflag used to represent a score that can be reached via opening a
    /// gap from the cell above it
    const UP: u8 = BacktrackMatrix::UP;

    /// The bitflag used to represent a score that can be reached via extending
    /// a gap from the cell above it
    const UP_EXTENDING: u8 = BacktrackMatrix::UP_EXTENDING;

    /// The bitfag used to represent a score that can be reached via opening a
    /// gap from the cell left of it
    const LEFT: u8 = BacktrackMatrix::LEFT;

    /// The bitflag used to represent a score that can be reached via extending
    /// a gap from the cell left of it
    const LEFT_EXTENDING: u8 = BacktrackMatrix::LEFT_EXTENDING;

    /// The bitflag used to indicate that an alignment starts at a given cell
    /// (i.e., the traceback should stop at that cell)
    const STOP: u8 = BacktrackMatrix::STOP;

    /// Create a new [`BandedBacktrackMatrix`] with the specified reference
    /// length and band width.
    ///
    /// The total size of the matrix will be `ref_len × (2 * band_width + 1)`.
    pub(crate) fn new(ref_len: usize, band_width: usize) -> Self {
        Self {
            data: vec![0u8; ref_len * (2 * band_width + 1)],
            band_width,
            cursor: 0,
        }
    }

    /// Sets the [`UP`] flag for the current cell in the
    /// [`BandedBacktrackMatrix`].
    ///
    /// [`UP`]: Self::UP
    #[inline]
    pub(crate) fn up(&mut self) {
        self.data[self.cursor] |= Self::UP;
    }

    /// Sets the [`LEFT`] flag for the current cell in the
    /// [`BandedBacktrackMatrix`].
    ///
    /// [`LEFT`]: Self::LEFT
    #[inline]
    pub(crate) fn left(&mut self) {
        self.data[self.cursor] |= Self::LEFT;
    }

    /// Sets the [`UP_EXTENDING`] flag for the current cell in the
    /// [`BandedBacktrackMatrix`].
    ///
    /// [`UP_EXTENDING`]: Self::UP_EXTENDING
    #[inline]
    pub(crate) fn up_extending(&mut self) {
        self.data[self.cursor] |= Self::UP_EXTENDING;
    }

    /// Sets the [`LEFT_EXTENDING`] flag for the current cell in the
    /// [`BandedBacktrackMatrix`].
    ///
    /// [`LEFT_EXTENDING`]: Self::LEFT_EXTENDING
    #[inline]
    pub(crate) fn left_extending(&mut self) {
        self.data[self.cursor] |= Self::LEFT_EXTENDING;
    }

    /// Clears all flags then sets the [`STOP`] flag for the current cell in the
    /// [`BandedBacktrackMatrix`].
    ///
    /// [`STOP`]: Self::STOP
    #[inline]
    pub(crate) fn stop(&mut self) {
        self.data[self.cursor] = Self::STOP;
    }
}

impl BackTrackable for BandedBacktrackMatrix {
    #[inline]
    fn move_to(&mut self, r: usize, c: usize) {
        let band_full_width = 2 * self.band_width + 1;
        let num_cols_skipped = r.saturating_sub(self.band_width);
        let band_col = c - num_cols_skipped;
        self.cursor = r * band_full_width + band_col;
    }

    #[inline]
    fn is_up(&self) -> bool {
        self.data[self.cursor] & Self::UP > 0
    }

    #[inline]
    fn is_left(&self) -> bool {
        self.data[self.cursor] & Self::LEFT > 0
    }

    #[inline]
    fn is_left_extending(&self) -> bool {
        self.data[self.cursor] & Self::LEFT_EXTENDING > 0
    }

    #[inline]
    fn is_up_extending(&self) -> bool {
        self.data[self.cursor] & Self::UP_EXTENDING > 0
    }

    #[inline]
    fn is_stop(&self) -> bool {
        self.data[self.cursor] & Self::STOP > 0
    }

    fn print(&self) {
        let mut bt = self.clone();
        let nr = self.data.len() / (2 * self.band_width + 1);
        let nc = nr + self.band_width;

        print!("    ");
        for _ in 0..=nc {
            print!("x");
        }
        println!();
        for r in 0..nr {
            print!("{r:02}: x");
            let start_col = r.saturating_sub(self.band_width);
            let end_col = r + self.band_width + 1;
            for _ in 0..start_col {
                print!(" ");
            }
            for c in start_col..end_col {
                bt.move_to(r, c);
                if bt.is_stop() {
                    print!("o");
                } else if bt.is_up() {
                    print!("^");
                } else if bt.is_left() {
                    print!("<");
                } else if bt.is_up_extending() {
                    print!(":");
                } else if bt.is_left_extending() {
                    print!("-");
                } else {
                    print!("\\");
                }
            }
            println!();
        }
    }
}