spoars 0.1.0

Faithful native-Rust reimplementation of the spoa partial order alignment 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
//! The scalar backtrack shared by every gap mode, extracted from [`super::sisd::SisdEngine`] so
//! upcoming SIMD kernels can destripe their vectorized fill's matrices into these same row-major
//! `i32` buffers and reuse this exact backtrack — guaranteeing bit-identical tie-breaks with the
//! scalar engine (spoa's SIMD engine vectorizes only the DP fill; it backtracks scalar-ly on the
//! fully-stored matrices, per `third_party/spoa/src/simd_alignment_engine_implementation.hpp`).
//!
//! Each `backtrack_*` function below is a VERBATIM port of the corresponding backtrack half of
//! `spoa::SisdAlignmentEngine::{Linear,Affine,Convex}` (`sisd_alignment_engine.cpp`), reading
//! from caller-supplied `&[i32]` buffer slices — instead of `self`/`Implementation` fields — plus
//! the fill's already-computed `(max_i, max_j, max_score)` start cell. This is a pure extraction:
//! the tie-break precedence (match/mismatch, then graph-axis deletion, then sequence-axis
//! insertion; in-edges scanned in insertion order with first-exact-score-match winning), the
//! affine `extend_left`/`extend_up` gap-run unwinds, and the convex two-phase `extend_up` `|=`
//! compounds are unchanged from [`super::sisd`].

use super::{Alignment, AlignmentType, Scoring};
use crate::graph::{EdgeId, Graph};

/// Backtrack step 1 (match/mismatch) — the highest-precedence predecessor test, shared verbatim
/// by [`backtrack_linear`], [`backtrack_affine`], and [`backtrack_convex`]
/// (`sisd_alignment_engine.cpp:395-420,577-601,805-829` — byte-identical in all three).
///
/// Returns `Some((prev_i, prev_j))` for the first in-edge whose match/mismatch transition
/// explains `h_ij`, scanning the first in-edge then `inedges[1..]` in insertion order and taking
/// the first exact-score equality; returns `None` if none does (including at the
/// `i == 0 || j == 0` boundary). Reads only `h`/`sequence_profile`/`graph`/`node_id_to_rank`.
/// **The in-edge iteration order and first-equality-wins semantics are the tie-break that keeps
/// consensus/MSA parity with spoa — they must not be reordered.**
#[inline]
fn backtrack_match_step(
    graph: &Graph,
    node_id_to_rank: &[u32],
    sequence_profile: &[i32],
    h: &[i32],
    matrix_width: usize,
    i: usize,
    j: usize,
) -> Option<(usize, usize)> {
    if i == 0 || j == 0 {
        return None;
    }
    let h_ij = h[i * matrix_width + j];
    let node = &graph.nodes[graph.rank_to_node[i - 1].0 as usize];
    let match_cost = sequence_profile[node.code as usize * matrix_width + j];
    let pred_row = |edge_id: EdgeId| -> usize {
        let tail = graph.edges[edge_id.0 as usize].tail;
        node_id_to_rank[tail.0 as usize] as usize + 1
    };
    let pred_first = if node.inedges.is_empty() {
        0
    } else {
        pred_row(node.inedges[0])
    };
    if h_ij == h[pred_first * matrix_width + (j - 1)] + match_cost {
        return Some((pred_first, j - 1));
    }
    for p in 1..node.inedges.len() {
        let pred_i = pred_row(node.inedges[p]);
        if h_ij == h[pred_i * matrix_width + (j - 1)] + match_cost {
            return Some((pred_i, j - 1));
        }
    }
    None
}

/// Backtracks the optimal alignment under a linear gap penalty from the fill's best cell.
///
/// Ports the backtrack half of `spoa::SisdAlignmentEngine::Linear`
/// (`sisd_alignment_engine.cpp:372-462`) VERBATIM. `(max_i, max_j, max_score)` are the fill's
/// already-selected best cell/score (per `alignment_type`'s per-type max-score rule); if
/// `max_i == 0 && max_j == 0` (no cell was ever selected — an empty graph or sequence), returns an
/// empty alignment immediately, matching `sisd_alignment_engine.cpp:365-367`. Otherwise
/// `h[max_i * matrix_width + max_j]` must equal `max_score` (checked via `debug_assert_eq!`, which
/// also doubles as a destripe sanity check for the upcoming SIMD fills: if a vectorized fill
/// destripes `H` incorrectly, this assertion is the first line of defense).
#[allow(clippy::too_many_arguments)]
#[inline]
pub(crate) fn backtrack_linear(
    graph: &Graph,
    node_id_to_rank: &[u32],
    sequence_profile: &[i32],
    h: &[i32],
    matrix_width: usize,
    alignment_type: AlignmentType,
    scoring: &Scoring,
    max_i: usize,
    max_j: usize,
    max_score: i32,
) -> Alignment {
    if max_i == 0 && max_j == 0 {
        return Vec::new();
    }
    debug_assert_eq!(
        h[max_i * matrix_width + max_j],
        max_score,
        "fill's max_score must match H at (max_i, max_j)"
    );
    let g = i32::from(scoring.g);

    // Rank (+1, i.e. the DP row) of an in-edge's tail node (its predecessor).
    let pred_row = |edge_id: EdgeId| -> usize {
        let tail = graph.edges[edge_id.0 as usize].tail;
        node_id_to_rank[tail.0 as usize] as usize + 1
    };

    let mut alignment: Alignment = Vec::new();
    let mut i = max_i;
    let mut j = max_j;

    loop {
        let keep_going = match alignment_type {
            AlignmentType::Local => h[i * matrix_width + j] != 0,
            AlignmentType::Global => !(i == 0 && j == 0),
            AlignmentType::Overlap => !(i == 0 || j == 0),
        };
        if !keep_going {
            break;
        }

        let h_ij = h[i * matrix_width + j];
        let mut prev_i = 0usize;
        let mut prev_j = 0usize;
        let mut predecessor_found = false;

        // 1. Match/mismatch (:395-420) — highest precedence.
        if let Some((pi, pj)) = backtrack_match_step(
            graph,
            node_id_to_rank,
            sequence_profile,
            h,
            matrix_width,
            i,
            j,
        ) {
            prev_i = pi;
            prev_j = pj;
            predecessor_found = true;
        }

        // 2. Deletion / gap along the graph axis (:422-445).
        if !predecessor_found && i != 0 {
            let node = &graph.nodes[graph.rank_to_node[i - 1].0 as usize];
            let pred_first = if node.inedges.is_empty() {
                0
            } else {
                pred_row(node.inedges[0])
            };
            if h_ij == h[pred_first * matrix_width + j] + g {
                prev_i = pred_first;
                prev_j = j;
                predecessor_found = true;
            } else {
                for p in 1..node.inedges.len() {
                    let pred_i = pred_row(node.inedges[p]);
                    if h_ij == h[pred_i * matrix_width + j] + g {
                        prev_i = pred_i;
                        prev_j = j;
                        predecessor_found = true;
                        break;
                    }
                }
            }
        }

        // 3. Insertion / gap along the sequence axis (:447-451) — lowest precedence. The
        // `j != 0` guard mirrors the affine/convex backtracks: at the left edge column 0 is
        // always reconstructed by the deletion step above, so this branch is unreachable
        // there for well-formed matrices, but the guard keeps `j - 1` from underflowing.
        if !predecessor_found && j != 0 && h_ij == h[i * matrix_width + (j - 1)] + g {
            prev_i = i;
            prev_j = j - 1;
        }

        let node_slot = if i == prev_i {
            -1
        } else {
            graph.rank_to_node[i - 1].0 as i32
        };
        let seq_slot = if j == prev_j { -1 } else { (j - 1) as i32 };
        alignment.push((node_slot, seq_slot));

        i = prev_i;
        j = prev_j;
    }

    alignment.reverse();
    alignment
}

/// Backtracks the optimal alignment under an affine gap penalty from the fill's best cell.
///
/// Ports the backtrack half of `spoa::SisdAlignmentEngine::Affine`
/// (`sisd_alignment_engine.cpp:553-677`) VERBATIM, additionally unwinding affine gap *runs* via
/// `extend_left` (walk the `e` insertion run leftward, `:645-653`) and `extend_up` (walk the `f`
/// deletion run upward across predecessors, `:654-673`). See [`backtrack_linear`] for the
/// `(max_i, max_j, max_score)` contract and the empty-alignment early return.
#[allow(clippy::too_many_arguments)]
#[inline]
pub(crate) fn backtrack_affine(
    graph: &Graph,
    node_id_to_rank: &[u32],
    sequence_profile: &[i32],
    h: &[i32],
    e: &[i32],
    f: &[i32],
    matrix_width: usize,
    alignment_type: AlignmentType,
    scoring: &Scoring,
    max_i: usize,
    max_j: usize,
    max_score: i32,
) -> Alignment {
    if max_i == 0 && max_j == 0 {
        return Vec::new();
    }
    debug_assert_eq!(
        h[max_i * matrix_width + max_j],
        max_score,
        "fill's max_score must match H at (max_i, max_j)"
    );
    let g = i32::from(scoring.g);
    let e_penalty = i32::from(scoring.e);

    // Rank (+1, i.e. the DP row) of an in-edge's tail node (its predecessor).
    let pred_row = |edge_id: EdgeId| -> usize {
        let tail = graph.edges[edge_id.0 as usize].tail;
        node_id_to_rank[tail.0 as usize] as usize + 1
    };

    let mut alignment: Alignment = Vec::new();
    let mut i = max_i;
    let mut j = max_j;

    loop {
        let keep_going = match alignment_type {
            AlignmentType::Local => h[i * matrix_width + j] != 0,
            AlignmentType::Global => !(i == 0 && j == 0),
            AlignmentType::Overlap => !(i == 0 || j == 0),
        };
        if !keep_going {
            break;
        }

        let h_ij = h[i * matrix_width + j];
        let mut prev_i = 0usize;
        let mut prev_j = 0usize;
        let mut predecessor_found = false;
        let mut extend_left = false;
        let mut extend_up = false;

        // 1. Match/mismatch (:577-601) — highest precedence.
        if let Some((pi, pj)) = backtrack_match_step(
            graph,
            node_id_to_rank,
            sequence_profile,
            h,
            matrix_width,
            i,
            j,
        ) {
            prev_i = pi;
            prev_j = pj;
            predecessor_found = true;
        }

        // 2. Deletion / gap along the graph axis (:603-627). Faithfully ports the C++
        // `(extend_up = A) || B` idiom: `extend_up` is set ONLY from the F-extend test `A`, and
        // the `||` short-circuits so `B` (the H-open test) is not evaluated when `A` holds.
        if !predecessor_found && i != 0 {
            let node = &graph.nodes[graph.rank_to_node[i - 1].0 as usize];
            let pred_first = if node.inedges.is_empty() {
                0
            } else {
                pred_row(node.inedges[0])
            };
            let a = h_ij == f[pred_first * matrix_width + j] + e_penalty;
            extend_up = a;
            if a || h_ij == h[pred_first * matrix_width + j] + g {
                prev_i = pred_first;
                prev_j = j;
                predecessor_found = true;
            } else {
                for p in 1..node.inedges.len() {
                    let pred_i = pred_row(node.inedges[p]);
                    let a = h_ij == f[pred_i * matrix_width + j] + e_penalty;
                    extend_up = a;
                    if a || h_ij == h[pred_i * matrix_width + j] + g {
                        prev_i = pred_i;
                        prev_j = j;
                        predecessor_found = true;
                        break;
                    }
                }
            }
        }

        // 3. Insertion / gap along the sequence axis (:629-636) — lowest precedence. Same
        // `(extend_left = A) || B` short-circuit idiom.
        if !predecessor_found && j != 0 {
            let a = h_ij == e[i * matrix_width + (j - 1)] + e_penalty;
            extend_left = a;
            if a || h_ij == h[i * matrix_width + (j - 1)] + g {
                prev_i = i;
                prev_j = j - 1;
            }
        }

        let node_slot = if i == prev_i {
            -1
        } else {
            graph.rank_to_node[i - 1].0 as i32
        };
        let seq_slot = if j == prev_j { -1 } else { (j - 1) as i32 };
        alignment.push((node_slot, seq_slot));

        i = prev_i;
        j = prev_j;

        // Gap-run unwinding (:645-674).
        if extend_left {
            // Walk the E insertion run leftward until the affine extension no longer holds.
            loop {
                alignment.push((-1, (j - 1) as i32));
                j -= 1;
                if e[i * matrix_width + j] + e_penalty != e[i * matrix_width + (j + 1)] {
                    break;
                }
            }
        } else if extend_up {
            // Walk the F deletion run upward. `stop` is set ONLY from the H-open (`+ g`) test, and
            // the loop stops on gap-open or on reaching the boundary row (`i == 0`).
            loop {
                let mut stop = false;
                prev_i = 0;
                for &edge_id in &graph.nodes[graph.rank_to_node[i - 1].0 as usize].inedges {
                    let pred_i = pred_row(edge_id);
                    let s = f[i * matrix_width + j] == h[pred_i * matrix_width + j] + g;
                    stop = s;
                    if s || f[i * matrix_width + j] == f[pred_i * matrix_width + j] + e_penalty {
                        prev_i = pred_i;
                        break;
                    }
                }
                alignment.push((graph.rank_to_node[i - 1].0 as i32, -1));
                i = prev_i;
                if stop || i == 0 {
                    break;
                }
            }
        }
    }

    alignment.reverse();
    alignment
}

/// Backtracks the optimal alignment under a convex (double-affine) gap penalty from the fill's
/// best cell.
///
/// Ports the backtrack half of `spoa::SisdAlignmentEngine::Convex`
/// (`sisd_alignment_engine.cpp:781-924`) VERBATIM. Its deletion/insertion steps use a four-term
/// compound condition testing *both* gap functions (`f`+`e` OR `h`+`g` OR `o`+`c` OR `h`+`q`, via
/// the upstream `(extend_* |= ...) || ...` short-circuit idiom, `:837-865`). The gap-run unwinds
/// mirror that: `extend_left` walks leftward while *either* the `e` or `q` run continues
/// (`:879-887`, breaking only when NEITHER does), and `extend_up` walks upward in two phases per
/// step — first try to continue an `f`/`o` extend across predecessors, and only if none continues
/// fall back to an `f`/`o` gap-open scan (`:888-921`). See [`backtrack_linear`] for the
/// `(max_i, max_j, max_score)` contract and the empty-alignment early return.
#[allow(clippy::too_many_arguments)]
#[inline]
pub(crate) fn backtrack_convex(
    graph: &Graph,
    node_id_to_rank: &[u32],
    sequence_profile: &[i32],
    h: &[i32],
    e: &[i32],
    f: &[i32],
    o: &[i32],
    q: &[i32],
    matrix_width: usize,
    alignment_type: AlignmentType,
    scoring: &Scoring,
    max_i: usize,
    max_j: usize,
    max_score: i32,
) -> Alignment {
    if max_i == 0 && max_j == 0 {
        return Vec::new();
    }
    debug_assert_eq!(
        h[max_i * matrix_width + max_j],
        max_score,
        "fill's max_score must match H at (max_i, max_j)"
    );
    let g = i32::from(scoring.g);
    let e_penalty = i32::from(scoring.e);
    let q_penalty = i32::from(scoring.q);
    let c_penalty = i32::from(scoring.c);

    // Rank (+1, i.e. the DP row) of an in-edge's tail node (its predecessor).
    let pred_row = |edge_id: EdgeId| -> usize {
        let tail = graph.edges[edge_id.0 as usize].tail;
        node_id_to_rank[tail.0 as usize] as usize + 1
    };

    let mut alignment: Alignment = Vec::new();
    let mut i = max_i;
    let mut j = max_j;

    loop {
        let keep_going = match alignment_type {
            AlignmentType::Local => h[i * matrix_width + j] != 0,
            AlignmentType::Global => !(i == 0 && j == 0),
            AlignmentType::Overlap => !(i == 0 || j == 0),
        };
        if !keep_going {
            break;
        }

        let h_ij = h[i * matrix_width + j];
        let mut prev_i = 0usize;
        let mut prev_j = 0usize;
        let mut predecessor_found = false;
        let mut extend_left = false;
        let mut extend_up = false;

        // 1. Match/mismatch (:805-829) — highest precedence.
        if let Some((pi, pj)) = backtrack_match_step(
            graph,
            node_id_to_rank,
            sequence_profile,
            h,
            matrix_width,
            i,
            j,
        ) {
            prev_i = pi;
            prev_j = pj;
            predecessor_found = true;
        }

        // 2. Deletion / gap along the graph axis (:831-859). Faithfully ports the four-term
        // `(extend_up |= F-extend) || H-open || (extend_up |= O-extend) || H-2nd-open` compound:
        // `extend_up` accumulates via `|=` ONLY from the two gap-EXTEND tests (F+e, O+c), and the
        // `||` short-circuits so a later test — including the second `extend_up |= ...` — is not
        // evaluated once an earlier term holds. Thus `extend_up` ends true iff a gap-extend matched
        // before any gap-open short-circuited the chain.
        if !predecessor_found && i != 0 {
            let node = &graph.nodes[graph.rank_to_node[i - 1].0 as usize];
            let pred_first = if node.inedges.is_empty() {
                0
            } else {
                pred_row(node.inedges[0])
            };
            let a = h_ij == f[pred_first * matrix_width + j] + e_penalty;
            extend_up |= a;
            let cond = a
                || h_ij == h[pred_first * matrix_width + j] + g
                || {
                    let b = h_ij == o[pred_first * matrix_width + j] + c_penalty;
                    extend_up |= b;
                    b
                }
                || h_ij == h[pred_first * matrix_width + j] + q_penalty;
            if cond {
                prev_i = pred_first;
                prev_j = j;
                predecessor_found = true;
            } else {
                for p in 1..node.inedges.len() {
                    let pred_i = pred_row(node.inedges[p]);
                    let a = h_ij == f[pred_i * matrix_width + j] + e_penalty;
                    extend_up |= a;
                    let cond = a
                        || h_ij == h[pred_i * matrix_width + j] + g
                        || {
                            let b = h_ij == o[pred_i * matrix_width + j] + c_penalty;
                            extend_up |= b;
                            b
                        }
                        || h_ij == h[pred_i * matrix_width + j] + q_penalty;
                    if cond {
                        prev_i = pred_i;
                        prev_j = j;
                        predecessor_found = true;
                        break;
                    }
                }
            }
        }

        // 3. Insertion / gap along the sequence axis (:861-870) — lowest precedence. Same
        // four-term `(extend_left |= E-extend) || H-open || (extend_left |= Q-extend) ||
        // H-2nd-open` short-circuit idiom.
        if !predecessor_found && j != 0 {
            let a = h_ij == e[i * matrix_width + (j - 1)] + e_penalty;
            extend_left |= a;
            let cond = a
                || h_ij == h[i * matrix_width + (j - 1)] + g
                || {
                    let b = h_ij == q[i * matrix_width + (j - 1)] + c_penalty;
                    extend_left |= b;
                    b
                }
                || h_ij == h[i * matrix_width + (j - 1)] + q_penalty;
            if cond {
                prev_i = i;
                prev_j = j - 1;
            }
        }

        let node_slot = if i == prev_i {
            -1
        } else {
            graph.rank_to_node[i - 1].0 as i32
        };
        let seq_slot = if j == prev_j { -1 } else { (j - 1) as i32 };
        alignment.push((node_slot, seq_slot));

        i = prev_i;
        j = prev_j;

        // Gap-run unwinding (:879-921).
        if extend_left {
            // Walk the E/Q insertion run leftward; stop only when NEITHER the E run nor the Q run
            // continues (`&&` in the break condition, :883-886).
            loop {
                alignment.push((-1, (j - 1) as i32));
                j -= 1;
                let e_stops = e[i * matrix_width + j] + e_penalty != e[i * matrix_width + (j + 1)];
                let q_stops = q[i * matrix_width + j] + c_penalty != q[i * matrix_width + (j + 1)];
                if e_stops && q_stops {
                    break;
                }
            }
        } else if extend_up {
            // Walk the F/O deletion run upward in two phases per step (:889-919). `stop` starts
            // true; Phase A clears it if an F- or O-extend continues across some predecessor;
            // Phase B (a gap-open scan) runs ONLY when no extend was found.
            loop {
                let mut stop = true;
                prev_i = 0;
                // Phase A: try to continue an F or O extend.
                for &edge_id in &graph.nodes[graph.rank_to_node[i - 1].0 as usize].inedges {
                    let pred_i = pred_row(edge_id);
                    if f[i * matrix_width + j] == f[pred_i * matrix_width + j] + e_penalty
                        || o[i * matrix_width + j] == o[pred_i * matrix_width + j] + c_penalty
                    {
                        prev_i = pred_i;
                        stop = false;
                        break;
                    }
                }
                // Phase B: fall back to an F or O gap-open scan.
                if stop {
                    for &edge_id in &graph.nodes[graph.rank_to_node[i - 1].0 as usize].inedges {
                        let pred_i = pred_row(edge_id);
                        if f[i * matrix_width + j] == h[pred_i * matrix_width + j] + g
                            || o[i * matrix_width + j] == h[pred_i * matrix_width + j] + q_penalty
                        {
                            prev_i = pred_i;
                            break;
                        }
                    }
                }
                alignment.push((graph.rank_to_node[i - 1].0 as i32, -1));
                i = prev_i;
                if stop || i == 0 {
                    break;
                }
            }
        }
    }

    alignment.reverse();
    alignment
}

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

    fn linear_scoring() -> Scoring {
        Scoring::new(5, -4, -8, -8, -8, -8).unwrap()
    }

    fn affine_scoring() -> Scoring {
        Scoring::new(5, -4, -8, -6, -8, -6).unwrap()
    }

    fn convex_scoring() -> Scoring {
        Scoring::new(5, -4, -8, -6, -10, -4).unwrap()
    }

    /// A single-node graph ("A") aligned globally against `"A"` under linear scoring: a trivial
    /// 2x2 matrix (`matrix_width = 2`) with one match cell. Hand-computed:
    /// `H = [[0, -8], [-8, 5]]` (row 0/col 0 are the NW boundary; `H[1][1] = 5` is the match).
    #[test]
    fn backtrack_linear_matches_single_node_on_hand_built_matrix() {
        let mut g = Graph::new();
        g.add_alignment_weight(&[], b"A", 1).unwrap();

        let scoring = linear_scoring();
        let node_id_to_rank = vec![0u32];
        // sequence_profile[code * matrix_width + j]; only one code ('A') is used here:
        // [j=0 boundary (unused), j=1 match].
        let sequence_profile = vec![0, 5];
        let matrix_width = 2;
        let h = vec![0, -8, -8, 5];

        let alignment = backtrack_linear(
            &g,
            &node_id_to_rank,
            &sequence_profile,
            &h,
            matrix_width,
            AlignmentType::Global,
            &scoring,
            1,
            1,
            5,
        );

        assert_eq!(alignment, vec![(0, 0)]);
    }

    /// A single-node graph ("A") aligned globally against a length-2 sequence under affine
    /// scoring (`g=-8, e=-6`), with hand-picked `H`/`E`/`F` buffers engineered so that
    /// `(max_i, max_j) = (1, 2)`'s only explanation is an `E`-run gap-open (forcing
    /// `extend_left`), which then unwinds one more step before the row-0 boundary is reached via
    /// a graph-axis deletion. This exercises [`backtrack_affine`]'s `extend_left` unwind loop
    /// independent of any real DP fill.
    #[test]
    fn backtrack_affine_unwinds_e_run_then_deletes() {
        use crate::align::sisd::NEG_INF;

        let mut g = Graph::new();
        g.add_alignment_weight(&[], b"A", 1).unwrap();
        let node_id = g.rank_to_node[0].0 as i32;

        let scoring = affine_scoring(); // g = -8, e = -6
        let node_id_to_rank = vec![0u32];
        // sequence_profile[code * matrix_width + j]; only code 0 ("A") is used. j=1 -> match (5).
        let sequence_profile = vec![0, 5, 5];
        let matrix_width = 3;

        // H[0][*] = NW boundary row (j * g): [0, -8, -16].
        // H[1][0] = 0 + g = -8 (deletion from the boundary column).
        // H[1][2] = -26, engineered to equal E[1][1] + e (-20 + -6) so the E-run-open test fires,
        // and to NOT equal the match-step or gap-open-deletion alternatives at (1, 2).
        let h = vec![0, -8, -16, -8, NEG_INF, -26];
        // E[1][0] and E[1][1] are set so the unwind's continuation check fails immediately after
        // one step: E[1][0] + e (0 + -6 = -6) != E[1][1] (-20).
        let e = vec![0, -8, -16, 0, -20, NEG_INF];
        let f = vec![0, NEG_INF, NEG_INF, NEG_INF, NEG_INF, NEG_INF];

        let alignment = backtrack_affine(
            &g,
            &node_id_to_rank,
            &sequence_profile,
            &h,
            &e,
            &f,
            matrix_width,
            AlignmentType::Global,
            &scoring,
            1,
            2,
            -26,
        );

        assert_eq!(alignment, vec![(node_id, -1), (-1, 0), (-1, 1)]);
    }

    /// A single-node graph ("A") aligned globally against `"A"` under convex scoring: identical
    /// shape to the linear hand case, since with no gaps neither `O`/`Q` matters for the traceback
    /// of the match step itself; asserts the convex signature/wiring by using non-trivial (but
    /// unreachable-during-this-trace) `O`/`Q` buffers.
    #[test]
    fn backtrack_convex_matches_single_node_on_hand_built_matrix() {
        use crate::align::sisd::NEG_INF;

        let mut g = Graph::new();
        g.add_alignment_weight(&[], b"A", 1).unwrap();

        let scoring = convex_scoring();
        let node_id_to_rank = vec![0u32];
        let sequence_profile = vec![0, 5];
        let matrix_width = 2;
        let h = vec![0, -8, -8, 5];
        let e = vec![0, -8, NEG_INF, NEG_INF];
        let f = vec![0, NEG_INF, NEG_INF, NEG_INF];
        let o = vec![0, NEG_INF, NEG_INF, NEG_INF];
        let q = vec![0, -10, NEG_INF, NEG_INF];

        let alignment = backtrack_convex(
            &g,
            &node_id_to_rank,
            &sequence_profile,
            &h,
            &e,
            &f,
            &o,
            &q,
            matrix_width,
            AlignmentType::Global,
            &scoring,
            1,
            1,
            5,
        );

        assert_eq!(alignment, vec![(0, 0)]);
    }
}