twitcher 0.1.10

Find template switch mutations in genomic data
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
use std::ops::{Deref, DerefMut};

use anyhow::{Context as _, bail};
use lib_tsalign::a_star_aligner::{
    alignment_result::alignment::Alignment,
    template_switch_distance::{
        AlignmentType, EqualCostRange, TemplateSwitchDirection, TemplateSwitchPrimary,
        TemplateSwitchSecondary,
    },
};

use crate::common::aligner::result::TwitcherAlignmentWithStatistics;

/// Wrapper type for the forward alignment of a sequence.
/// With this type, we always describe the entire sequence alignment, including padding around a cluster.
/// So, for example, for the VCF case (assuming padding 200), this will almost always start and end with 200M.
/// It is also restricted to only "Primary..." alignments, aka forward alignments.
#[derive(Clone, Debug)]
pub struct ForwardAlignment(pub Alignment<AlignmentType>);

impl Deref for ForwardAlignment {
    type Target = Alignment<AlignmentType>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ForwardAlignment {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Alignment<AlignmentType>> for ForwardAlignment {
    fn from(value: Alignment<AlignmentType>) -> Self {
        Self(value)
    }
}

impl ForwardAlignment {
    pub fn insert_ts_alignment(
        &self,
        ts_alignment: &TwitcherAlignmentWithStatistics,
    ) -> anyhow::Result<Alignment<AlignmentType>> {
        let mut forward_flat_iter = self.0.iter_flat_cloned();
        let mut result = Alignment::new();

        let mut roff = ts_alignment.stats.reference_offset();
        let mut qoff = ts_alignment.stats.query_offset();

        // padding: count down offset
        while roff > 0 || qoff > 0 {
            let Some(next) = forward_flat_iter.next() else {
                bail!("Incomplete forward alignment");
            };
            roff -= usize::try_from(consumed_reference(1, &next, None)?)?;
            qoff -= usize::try_from(consumed_query(1, &next, None)?)?;
            result.push(next);
        }

        // ts alignment
        let mut ts = None;
        let mut rlen = 0isize;
        let mut qlen = 0isize;
        for (n, ty) in ts_alignment.alignment.alignment.iter_compact_cloned() {
            rlen += consumed_reference(n, &ty, ts)?;
            qlen += consumed_query(n, &ty, ts)?;
            match &ty {
                AlignmentType::TemplateSwitchEntrance { primary, .. } => {
                    ts = Some(*primary);
                }
                AlignmentType::TemplateSwitchExit { .. } => {
                    ts.take();
                }
                _ => {}
            }
            result.push_n(n, ty);
        }

        // skip forward alignment of cluster
        while rlen > 0 || qlen > 0 {
            let Some(next) = forward_flat_iter.next() else {
                bail!("Incomplete forward alignment");
            };
            rlen -= consumed_reference(1, &next, None)?;
            qlen -= consumed_query(1, &next, None)?;
        }

        // and add the trailing padding
        for ty in forward_flat_iter {
            result.push(ty);
        }

        Ok(result)
    }

    pub fn crop_to_ts_region(
        &self,
        ts_alignment: &TwitcherAlignmentWithStatistics,
    ) -> anyhow::Result<Alignment<AlignmentType>> {
        let mut ref_remaining = ts_alignment.stats.reference_offset();
        let mut query_remaining = ts_alignment.stats.query_offset();
        let mut forward_flat_iter = self.0.iter_flat_cloned();
        let mut result = Alignment::new();

        // Skip padding by consuming exactly reference_offset ref bases and query_offset query
        // bases. Using both counters handles insertions that sit at the boundary ref position:
        // they consume 0 ref but belong to the context, not the cluster.
        while ref_remaining > 0 || query_remaining > 0 {
            let Some(next) = forward_flat_iter.next() else {
                bail!("Incomplete forward alignment");
            };
            ref_remaining =
                ref_remaining.saturating_sub(usize::try_from(consumed_reference(1, &next, None)?)?);
            query_remaining =
                query_remaining.saturating_sub(usize::try_from(consumed_query(1, &next, None)?)?);
        }

        // get length of ts alignment
        let mut ts = None;
        let mut ref_length = 0isize;
        for (n, ty) in ts_alignment.alignment.alignment.iter_compact_cloned() {
            ref_length += match &ty {
                AlignmentType::TemplateSwitchEntrance { primary, .. } => {
                    ts = Some(*primary);
                    consumed_reference(n, &ty, None)? // None because the entrance is not yet in the TS
                }
                AlignmentType::TemplateSwitchExit { .. } => {
                    let ts = ts.take();
                    consumed_reference(n, &ty, ts)?
                }
                ty => consumed_reference(n, ty, ts)?,
            };
        }

        // add forward alignment of cluster
        while ref_length > 0 {
            let Some(next) = forward_flat_iter.next() else {
                bail!("Incomplete forward alignment");
            };
            ref_length -= consumed_reference(1, &next, None)?;
            result.push(next);
        }

        Ok(result)
    }
}

pub fn consumed_reference(
    n: usize,
    ty: &AlignmentType,
    ts: Option<TemplateSwitchPrimary>,
) -> anyhow::Result<isize> {
    Ok(consumes(n, ty, ts)?.0)
}

pub fn consumed_query(
    n: usize,
    ty: &AlignmentType,
    ts: Option<TemplateSwitchPrimary>,
) -> anyhow::Result<isize> {
    Ok(consumes(n, ty, ts)?.1)
}

fn consumes(
    n: usize,
    ty: &AlignmentType,
    ts: Option<TemplateSwitchPrimary>,
) -> anyhow::Result<(isize, isize)> {
    let n = isize::try_from(n)?;

    let (r, q) = if let Some(primary) = ts {
        let (mut r, mut q) = match ty {
            AlignmentType::PrimaryInsertion
            | AlignmentType::PrimaryDeletion
            | AlignmentType::PrimarySubstitution
            | AlignmentType::PrimaryMatch
            | AlignmentType::PrimaryFlankInsertion
            | AlignmentType::PrimaryFlankDeletion
            | AlignmentType::PrimaryFlankSubstitution
            | AlignmentType::PrimaryFlankMatch
            | AlignmentType::Root
            | AlignmentType::PrimaryReentry
            | AlignmentType::PrimaryShortcut { .. } => {
                bail!("Primary alignments cannot happen inside a template switch")
            }
            AlignmentType::TemplateSwitchEntrance { .. } => {
                bail!("A new template switch cannot start inside a template switch")
            }
            // Assume primary == Ref, flip later if not
            //
            // These consume the primary sequence
            AlignmentType::SecondaryInsertion
            | AlignmentType::SecondarySubstitution
            | AlignmentType::SecondaryMatch => (n, 0),
            // These do not
            AlignmentType::SecondaryDeletion | AlignmentType::SecondaryRoot => (0, 0),
            // Exit consumes Anti-Primary
            AlignmentType::TemplateSwitchExit { anti_primary_gap } => (0, *anti_primary_gap),
        };
        if primary != TemplateSwitchPrimary::Reference {
            std::mem::swap(&mut r, &mut q);
        }
        (r, q)
    } else {
        match ty {
            // insertion
            AlignmentType::PrimaryInsertion | AlignmentType::PrimaryFlankInsertion => (0, n),
            // deletion
            AlignmentType::PrimaryDeletion | AlignmentType::PrimaryFlankDeletion => (n, 0),
            // (mis)match
            AlignmentType::PrimarySubstitution
            | AlignmentType::PrimaryFlankSubstitution
            | AlignmentType::PrimaryMatch
            | AlignmentType::PrimaryFlankMatch => (n, n),
            // ts entrance does not consume itself, neither do root nodes
            AlignmentType::TemplateSwitchEntrance { .. }
            | AlignmentType::Root
            | AlignmentType::PrimaryReentry => (0, 0),
            // shortcut should never occur, but if it does, this is the spec
            AlignmentType::PrimaryShortcut {
                delta_reference,
                delta_query,
            } => (*delta_reference, *delta_query),
            // this needs a TS value to judge.
            AlignmentType::SecondaryInsertion
            | AlignmentType::SecondaryDeletion
            | AlignmentType::SecondarySubstitution
            | AlignmentType::SecondaryMatch
            | AlignmentType::SecondaryRoot
            | AlignmentType::TemplateSwitchExit { .. } => {
                bail!("Secondary alignments require a preceding template switch entrance")
            }
        }
    };
    Ok((r, q))
}

#[allow(clippy::too_many_lines)]
pub fn cigar_to_alignment<S: AsRef<str>>(cigar: &S) -> anyhow::Result<Alignment<AlignmentType>> {
    let s = cigar.as_ref();
    let mut alignment = Alignment::new();
    let mut chars = s.char_indices().peekable();
    let mut inside_ts = false;

    while let Some((i, c)) = chars.peek().copied() {
        match c {
            '0'..='9' => {
                let start = i;
                while chars.peek().is_some_and(|(_, c)| c.is_ascii_digit()) {
                    chars.next();
                }
                let end = chars.peek().map_or(s.len(), |(i, _)| *i);
                let amount: usize = s[start..end]
                    .parse()
                    .with_context(|| format!("Invalid repeat count at offset {start}"))?;

                let (_, type_char) = chars.next().with_context(|| {
                    format!("Expected alignment type char after count at offset {end}")
                })?;

                let atype = parse_repeatable_char(type_char, inside_ts).with_context(|| {
                    format!("Unknown alignment type char '{type_char}' at offset {end}")
                })?;

                alignment.push_n(amount, atype);
            }

            'I' | 'D' | 'X' | '=' => {
                chars.next();
                let atype = parse_repeatable_char(c, inside_ts).unwrap();
                alignment.push(atype);
            }

            '[' => {
                chars.next();
                let (_, next) = chars.next().context("Unexpected end after '['")?;

                match next {
                    'T' => {
                        if inside_ts {
                            bail!("Nested TemplateSwitchEntrance is not allowed");
                        }

                        expect_char(&mut chars, 'S').context("Expected 'S' in '[TS...'")?;

                        let (_, p) = chars.next().context("Expected primary char")?;
                        let primary = parse_ts_primary(p)
                            .with_context(|| format!("Invalid TemplateSwitchPrimary '{p}'"))?;

                        let (_, s_char) = chars.next().context("Expected secondary char")?;
                        let secondary = parse_ts_secondary(s_char).with_context(|| {
                            format!("Invalid TemplateSwitchSecondary '{s_char}'")
                        })?;

                        let (_, d) = chars.next().context("Expected direction char")?;
                        let direction = parse_ts_direction(d)
                            .with_context(|| format!("Invalid TemplateSwitchDirection '{d}'"))?;

                        expect_char(&mut chars, ':').context("Expected ':' after direction")?;

                        let equal_cost_range = parse_equal_cost_range(&mut chars)
                            .context("Failed to parse EqualCostRange")?;

                        expect_char(&mut chars, ':')
                            .context("Expected ':' after EqualCostRange")?;

                        let first_offset = parse_isize(&mut chars, &[':', ']'])
                            .context("Failed to parse first_offset")?;

                        expect_char(&mut chars, ':')
                            .context("Expected trailing ':' after first_offset")?;

                        inside_ts = true;
                        alignment.push(AlignmentType::TemplateSwitchEntrance {
                            primary,
                            secondary,
                            direction,
                            equal_cost_range,
                            first_offset,
                        });
                    }

                    'P' => {
                        expect_char(&mut chars, 'S').context("Expected 'S' in '[PS...'")?;
                        expect_char(&mut chars, ':').context("Expected ':' in '[PS:...'")?;
                        expect_char(&mut chars, 'R')
                            .context("Expected 'R' before delta_reference")?;

                        let delta_reference = parse_isize(&mut chars, &['Q'])
                            .context("Failed to parse delta_reference")?;

                        expect_char(&mut chars, 'Q').context("Expected 'Q' before delta_query")?;

                        let delta_query = parse_isize(&mut chars, &[']'])
                            .context("Failed to parse delta_query")?;

                        expect_char(&mut chars, ']').context("Expected ']' to close '[PS...'")?;

                        alignment.push(AlignmentType::PrimaryShortcut {
                            delta_reference,
                            delta_query,
                        });
                    }

                    other => bail!("Unknown bracket expression starting with '[{other}'"),
                }
            }

            ':' => {
                if !inside_ts {
                    bail!(
                        "TemplateSwitchExit at offset {i} without a preceding TemplateSwitchEntrance"
                    );
                }
                chars.next();

                let anti_primary_gap =
                    parse_isize(&mut chars, &[']']).context("Failed to parse anti_primary_gap")?;

                expect_char(&mut chars, ']').context("Expected ']' to close TemplateSwitchExit")?;

                inside_ts = false;
                alignment.push(AlignmentType::TemplateSwitchExit { anti_primary_gap });
            }

            other => bail!("Unexpected character '{other}' at offset {i}"),
        }
    }

    if inside_ts {
        bail!("Unexpected end of input: unclosed TemplateSwitchEntrance");
    }

    Ok(alignment)
}

// --- helpers ---

fn parse_repeatable_char(c: char, inside_ts: bool) -> Option<AlignmentType> {
    match (c, inside_ts) {
        ('I', false) => Some(AlignmentType::PrimaryInsertion),
        ('D', false) => Some(AlignmentType::PrimaryDeletion),
        ('X', false) => Some(AlignmentType::PrimarySubstitution),
        ('=', false) => Some(AlignmentType::PrimaryMatch),
        ('I', true) => Some(AlignmentType::SecondaryInsertion),
        ('D', true) => Some(AlignmentType::SecondaryDeletion),
        ('X', true) => Some(AlignmentType::SecondarySubstitution),
        ('=', true) => Some(AlignmentType::SecondaryMatch),
        _ => None,
    }
}

fn parse_ts_primary(c: char) -> Option<TemplateSwitchPrimary> {
    match c {
        'R' => Some(TemplateSwitchPrimary::Reference),
        'Q' => Some(TemplateSwitchPrimary::Query),
        _ => None,
    }
}

fn parse_ts_secondary(c: char) -> Option<TemplateSwitchSecondary> {
    match c {
        'R' => Some(TemplateSwitchSecondary::Reference),
        'Q' => Some(TemplateSwitchSecondary::Query),
        _ => None,
    }
}

fn parse_ts_direction(c: char) -> Option<TemplateSwitchDirection> {
    match c {
        'F' => Some(TemplateSwitchDirection::Forward),
        'R' => Some(TemplateSwitchDirection::Reverse),
        _ => None,
    }
}

/// Parse a signed integer terminated by any char in `terminators` (not consumed).
fn parse_isize(
    chars: &mut std::iter::Peekable<impl Iterator<Item = (usize, char)>>,
    terminators: &[char],
) -> anyhow::Result<isize> {
    let mut buf = String::new();
    if chars.peek().is_some_and(|(_, c)| *c == '-') {
        buf.push('-');
        chars.next();
    }
    while let Some((_, c)) = chars.peek() {
        if terminators.contains(c) {
            break;
        }
        buf.push(*c);
        chars.next();
    }
    buf.parse::<isize>()
        .with_context(|| format!("Invalid integer '{buf}'"))
}

/// Consume and assert the next char equals `expected`.
fn expect_char(
    chars: &mut std::iter::Peekable<impl Iterator<Item = (usize, char)>>,
    expected: char,
) -> anyhow::Result<()> {
    match chars.next() {
        Some((_, c)) if c == expected => Ok(()),
        Some((i, c)) => bail!("Expected '{expected}' at offset {i}, got '{c}'"),
        None => bail!("Expected '{expected}' but got end of input"),
    }
}

/// Parse `EqualCostRange` in format `[{min_start},{max_start}]:[{min_end},{max_end}]`
/// or `[-]:[-]` for the invalid case.
fn parse_equal_cost_range(
    chars: &mut std::iter::Peekable<impl Iterator<Item = (usize, char)> + Clone>,
) -> anyhow::Result<EqualCostRange> {
    expect_char(chars, '[')?;

    // Distinguish [-] (invalid marker) from [-N,...] (negative min_start).
    // We do this by checking if the char after '-' is ']'.
    let is_invalid = {
        let mut tmp = chars.clone();
        tmp.next().is_some_and(|(_, c)| c == '-') && tmp.next().is_some_and(|(_, c)| c == ']')
    };

    if is_invalid {
        chars.next(); // consume '-'
        expect_char(chars, ']')?;
        expect_char(chars, ':')?;
        expect_char(chars, '[')?;
        expect_char(chars, '-')?;
        expect_char(chars, ']')?;
        return Ok(EqualCostRange::new_invalid());
    }

    let min_start = parse_isize(chars, &[',']).context("min_start")?;
    expect_char(chars, ',')?;
    let max_start = parse_isize(chars, &[']']).context("max_start")?;
    expect_char(chars, ']')?;
    expect_char(chars, ':')?;
    expect_char(chars, '[')?;
    let min_end = parse_isize(chars, &[',']).context("min_end")?;
    expect_char(chars, ',')?;
    let max_end = parse_isize(chars, &[']']).context("max_end")?;
    expect_char(chars, ']')?;

    Ok(EqualCostRange {
        min_start: min_start.try_into()?,
        max_start: max_start.try_into()?,
        min_end: min_end.try_into()?,
        max_end: max_end.try_into()?,
    })
}

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

    fn round_trip(alignment: &Alignment<AlignmentType>) -> Alignment<AlignmentType> {
        let cigar = alignment.cigar();
        cigar_to_alignment(&cigar).unwrap()
    }

    fn ecr(min_start: i8, max_start: i8, min_end: i8, max_end: i8) -> EqualCostRange {
        EqualCostRange {
            min_start,
            max_start,
            min_end,
            max_end,
        }
    }

    // --- Simple primary-only alignments ---

    #[test]
    fn test_empty() {
        let alignment = Alignment::new();
        assert_eq!(round_trip(&alignment), Alignment::new());
    }

    #[test]
    fn test_single_match() {
        let mut a = Alignment::new();
        a.push_n(1, AlignmentType::PrimaryMatch);
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_primary_only() {
        let mut a = Alignment::new();
        a.push_n(5, AlignmentType::PrimaryMatch);
        a.push_n(2, AlignmentType::PrimaryInsertion);
        a.push_n(3, AlignmentType::PrimaryDeletion);
        a.push_n(1, AlignmentType::PrimarySubstitution);
        a.push_n(4, AlignmentType::PrimaryMatch);
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_large_counts() {
        let mut a = Alignment::new();
        a.push_n(10_000, AlignmentType::PrimaryMatch);
        a.push_n(9_999, AlignmentType::PrimaryDeletion);
        assert_eq!(round_trip(&a), a);
    }

    // --- Template switch ---

    fn make_ts_entrance(
        primary: TemplateSwitchPrimary,
        secondary: TemplateSwitchSecondary,
        direction: TemplateSwitchDirection,
        equal_cost_range: EqualCostRange,
        first_offset: isize,
    ) -> AlignmentType {
        AlignmentType::TemplateSwitchEntrance {
            primary,
            secondary,
            direction,
            equal_cost_range,
            first_offset,
        }
    }

    fn push_simple_ts(
        a: &mut Alignment<AlignmentType>,
        primary: TemplateSwitchPrimary,
        secondary: TemplateSwitchSecondary,
        direction: TemplateSwitchDirection,
        equal_cost_range: EqualCostRange,
        first_offset: isize,
        anti_primary_gap: isize,
    ) {
        a.push(make_ts_entrance(
            primary,
            secondary,
            direction,
            equal_cost_range,
            first_offset,
        ));
        a.push_n(5, AlignmentType::SecondaryMatch);
        a.push(AlignmentType::TemplateSwitchExit { anti_primary_gap });
    }

    #[test]
    fn test_ts_empty_body() {
        // A TS with no secondary operations inside
        let mut a = Alignment::new();
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Reference,
            TemplateSwitchSecondary::Query,
            TemplateSwitchDirection::Forward,
            ecr(0, 0, 0, 0),
            0,
            0,
        );
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_ts_with_secondary_ops() {
        let mut a = Alignment::new();
        a.push(make_ts_entrance(
            TemplateSwitchPrimary::Query,
            TemplateSwitchSecondary::Reference,
            TemplateSwitchDirection::Reverse,
            ecr(-1, 2, -3, 4),
            -5,
        ));
        a.push_n(3, AlignmentType::SecondaryMatch);
        a.push_n(1, AlignmentType::SecondarySubstitution);
        a.push_n(2, AlignmentType::SecondaryInsertion);
        a.push_n(1, AlignmentType::SecondaryDeletion);
        a.push(AlignmentType::TemplateSwitchExit {
            anti_primary_gap: -3,
        });
        dbg!(a.cigar());
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_ts_negative_offsets() {
        let mut a = Alignment::new();
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Reference,
            TemplateSwitchSecondary::Reference,
            TemplateSwitchDirection::Reverse,
            ecr(-5, 0, -2, 0),
            -10,
            -7,
        );
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_ts_invalid_equal_cost_range() {
        let mut a = Alignment::new();
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Reference,
            TemplateSwitchSecondary::Query,
            TemplateSwitchDirection::Forward,
            EqualCostRange::new_invalid(),
            0,
            0,
        );
        assert_eq!(round_trip(&a), a);
    }

    // --- TS embedded in primary alignment ---

    #[test]
    fn test_primary_ts_primary() {
        let mut a = Alignment::new();
        a.push_n(10, AlignmentType::PrimaryMatch);
        a.push_n(2, AlignmentType::PrimaryDeletion);
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Reference,
            TemplateSwitchSecondary::Query,
            TemplateSwitchDirection::Forward,
            ecr(-1, 1, -1, 1),
            3,
            2,
        );
        a.push_n(5, AlignmentType::PrimaryMatch);
        assert_eq!(round_trip(&a), a);
    }

    #[test]
    fn test_multiple_ts() {
        let mut a = Alignment::new();
        a.push_n(4, AlignmentType::PrimaryMatch);
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Reference,
            TemplateSwitchSecondary::Query,
            TemplateSwitchDirection::Forward,
            ecr(0, 1, -1, 0),
            1,
            0,
        );
        a.push_n(3, AlignmentType::PrimarySubstitution);
        push_simple_ts(
            &mut a,
            TemplateSwitchPrimary::Query,
            TemplateSwitchSecondary::Reference,
            TemplateSwitchDirection::Reverse,
            ecr(-2, 0, 0, 3),
            -1,
            4,
        );
        a.push_n(6, AlignmentType::PrimaryMatch);
        assert_eq!(round_trip(&a), a);
    }

    // --- crop_to_ts_region ---

    // ref_len: how many reference bases the cluster TS alignment spans (determines how far crop reads).
    fn make_stats(
        ref_offset: usize,
        query_offset: usize,
        ref_len: usize,
    ) -> TwitcherAlignmentWithStatistics {
        use std::time::Duration;

        use generic_a_star::cost::AStarCost as _;
        use lib_tsalign::a_star_aligner::alignment_geometry::{
            AlignmentCoordinates, AlignmentRange,
        };
        use lib_tsalign::costs::U64Cost;

        use crate::common::aligner::fpa::FpaAlignmentStatistics;
        use crate::common::aligner::result::{AlignmentWithCost, TwitcherAlignmentStatistics};

        let mut cluster_aln = Alignment::new();
        cluster_aln.push_n(ref_len, AlignmentType::PrimaryMatch);

        TwitcherAlignmentWithStatistics {
            alignment: AlignmentWithCost::new(cluster_aln, U64Cost::from_primitive(0)),
            stats: TwitcherAlignmentStatistics::FPAStats(Box::new(FpaAlignmentStatistics {
                duration: Duration::ZERO,
                ranges: AlignmentRange::new_offset_limit(
                    AlignmentCoordinates::new(ref_offset, query_offset),
                    AlignmentCoordinates::new(ref_offset + ref_len, query_offset + ref_len),
                ),
            })),
        }
    }

    fn fw(cigar: &str) -> ForwardAlignment {
        cigar_to_alignment(&cigar.to_owned())
            .map(ForwardAlignment)
            .unwrap()
    }

    // Reproduce the bug: an insertion sitting exactly at the boundary reference position must be
    // consumed as padding, not included in the cropped cluster alignment.
    // Forward cigar: 20=1I108=1I70=1I9=1I200=  (ref=407, query=411)
    // Cluster starts at ref_offset=128, query_offset=130.
    // The 1I after the 108= sits at reference position 128 (query 129) and belongs to context.
    // Before the fix, crop_to_ts_region stopped at ref=0 and left that 1I in the iterator,
    // producing a leading 1I in the result that caused an out-of-bounds panic on cost computation.
    #[test]
    fn test_crop_skips_insertion_at_boundary() {
        // fw: 20=1I108=1I70=1I9=1I200= (ref=407, query=411)
        // Cluster starts at ref_offset=128, query_offset=130.
        // The 1I after 108= is at ref pos 128 (query 129) and belongs to the context.
        // Before the fix, that 1I leaked into the cropped result.
        let cropped = fw("20=1I108=1I70=1I9=1I200=")
            .crop_to_ts_region(&make_stats(128, 130, 279))
            .unwrap();
        assert_eq!(cropped.cigar(), "70=1I9=1I200=");
    }

    // Sanity check: when the offset is zero, the full alignment is returned.
    #[test]
    fn test_crop_zero_offset_returns_full() {
        let cropped = fw("10=2I5=")
            .crop_to_ts_region(&make_stats(0, 0, 15))
            .unwrap();
        assert_eq!(cropped.cigar(), "10=2I5=");
    }

    // --- Error cases ---

    #[test]
    fn test_error_unclosed_ts() {
        let cigar = "[TSRQF:[0,0]:[0,0]:0:";
        assert!(cigar_to_alignment(&cigar).is_err());
    }

    #[test]
    fn test_error_exit_without_entrance() {
        let cigar = ":0]";
        assert!(cigar_to_alignment(&cigar).is_err());
    }

    #[test]
    fn test_error_nested_ts() {
        // Construct a string that has two entrances before an exit
        let cigar = "[TSRQF:[0,0]:[0,0]:0:[TSRQF:[0,0]:[0,0]:0::0]:0]";
        assert!(cigar_to_alignment(&cigar).is_err());
    }

    #[test]
    fn test_error_invalid_char() {
        assert!(cigar_to_alignment(&"5Z").is_err());
    }

    #[test]
    fn test_error_truncated_ts_entrance() {
        assert!(cigar_to_alignment(&"[TSR").is_err());
    }
}