varforge 0.1.1

Synthetic cancer sequencing test data generator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
//! Core BAM editing engine.
//!
//! Takes an existing BAM file, spikes in variants at specified positions with
//! stochastic VAF sampling, and writes a modified BAM file.
//!
//! The engine reads all records into memory, sorts them by
//! (reference_sequence_id, alignment_start), then processes them with a
//! single-pass sliding-window approach. Sorting up front makes the editor
//! robust to unsorted or partially sorted input, which the simulator does
//! not guarantee. This is acceptable because the BAM editor targets small,
//! targeted BAMs rather than whole-genome files.

use std::collections::HashMap;
use std::io::BufWriter;
use std::path::Path;

use anyhow::{Context, Result};
use noodles_bam as bam;
use noodles_sam::{
    self as sam,
    alignment::{record::data::field::Tag, RecordBuf},
};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

use crate::core::types::{MutationType, Variant};
use crate::editor::read_modifier::{apply_deletion, apply_insertion, apply_snv, ModifyResult};
use crate::variants::vaf::sample_alt_count;

/// Configuration for the BAM editor.
#[derive(Debug, Clone)]
pub struct EditConfig {
    /// Path to the input BAM file.
    pub input_bam: std::path::PathBuf,
    /// Path to the output BAM file.
    pub output_bam: std::path::PathBuf,
    /// Variants to spike in.
    pub variants: Vec<Variant>,
    /// Random seed for reproducibility.
    pub seed: u64,
    /// Tumour purity (0.0-1.0). Scales all VAFs.
    pub purity: f64,
    /// Optional path to write a truth VCF.
    pub truth_vcf: Option<std::path::PathBuf>,
    /// Sample name for output headers.
    pub sample_name: String,
}

/// Result of spiking a single variant into the BAM.
#[derive(Debug, Clone)]
pub struct SpikedVariant {
    pub variant: Variant,
    pub total_depth: u32,
    pub alt_count: u32,
    pub actual_vaf: f64,
}

/// Core BAM editing engine.
pub struct BamEditor {
    config: EditConfig,
    rng: StdRng,
}

impl BamEditor {
    /// Create a new `BamEditor` from the given config.
    pub fn new(config: EditConfig) -> Self {
        let rng = StdRng::seed_from_u64(config.seed);
        Self { config, rng }
    }

    /// Run the editing pipeline:
    ///
    /// 1. Read all records from the input BAM into memory.
    /// 2. Sort records by (reference_sequence_id, alignment_start).
    /// 3. Sort variants by (chrom_id, pos).
    /// 4. Process records in order with a sliding window for active variants.
    /// 5. Once all reads for a variant region are buffered, spike that variant.
    /// 6. Flush writes as soon as records are no longer needed by any variant.
    /// 7. Optionally write truth VCF.
    pub fn run(&mut self) -> Result<Vec<SpikedVariant>> {
        let input_path = self.config.input_bam.clone();
        let output_path = self.config.output_bam.clone();
        let variants = self.config.variants.clone();
        let purity = self.config.purity;

        // Open input BAM and read header.
        let file = std::fs::File::open(&input_path)
            .with_context(|| format!("failed to open BAM file: {}", input_path.display()))?;
        let mut reader = bam::io::Reader::new(file);
        let header = reader
            .read_header()
            .with_context(|| format!("failed to read BAM header: {}", input_path.display()))?;

        // Open output BAM writer.
        let out_file = std::fs::File::create(&output_path)
            .with_context(|| format!("failed to create BAM file: {}", output_path.display()))?;
        let mut writer = bam::io::Writer::new(out_file);
        writer
            .write_header(&header)
            .context("failed to write BAM header")?;

        // Sort variants by (chrom_id, pos). Variants on unknown chromosomes are
        // dropped with a warning.
        let mut sorted_variants: Vec<(usize, u64, Variant)> = Vec::new();
        for v in &variants {
            match header
                .reference_sequences()
                .get_index_of(v.chrom.as_bytes())
            {
                Some(rid) => {
                    sorted_variants.push((rid, v.pos(), v.clone()));
                }
                None => {
                    tracing::warn!(
                        "chromosome '{}' not found in BAM header, skipping variant at pos {}",
                        v.chrom,
                        v.pos()
                    );
                }
            }
        }
        sorted_variants.sort_by_key(|&(rid, pos, _)| (rid, pos));

        // Read all records into memory, then sort by (rid, alignment_start).
        // This makes the editor robust to unsorted input from the simulator.
        let mut all_records: Vec<RecordBuf> = Vec::new();
        for result in reader.record_bufs(&header) {
            let record = result.context("failed to read BAM record")?;
            all_records.push(record);
        }
        all_records.sort_by(|a, b| {
            let rid_a = a.reference_sequence_id();
            let rid_b = b.reference_sequence_id();
            let start_a = a.alignment_start().map(|p| usize::from(p) as u64);
            let start_b = b.alignment_start().map(|p| usize::from(p) as u64);
            rid_a.cmp(&rid_b).then(start_a.cmp(&start_b))
        });

        // Process sorted records with a sliding-window approach.
        let mut spiked: Vec<SpikedVariant> = Vec::new();
        let mut variant_idx = 0usize;

        // Buffer holds records until all variants that could overlap them have
        // been processed. Modifications happen in place.
        let mut buffer: Vec<RecordBuf> = Vec::new();

        // `min_active` returns the (rid, pos) of the earliest unprocessed variant.
        // Records that cannot overlap any remaining variant can be flushed.
        let min_active = |vi: usize, sorted: &[(usize, u64, Variant)]| -> Option<(usize, u64)> {
            sorted.get(vi).map(|(rid, pos, _)| (*rid, *pos))
        };

        use noodles_sam::alignment::io::Write as _;

        for record in all_records {
            // Determine this record's alignment span.
            let rec_rid = record.reference_sequence_id();
            let rec_start = record.alignment_start().map(|p| usize::from(p) as u64 - 1);

            // Push into buffer unconditionally. We will flush eligible records
            // after processing any variants that have become fully covered.
            buffer.push(record);

            // Process variants whose region is fully covered by the buffer. A
            // variant at (vrid, vpos) is fully covered when the most recently
            // read record has alignment start > vpos on the same or a later
            // chromosome. At that point, no future record can overlap vpos.
            loop {
                let Some((vrid, vpos, _)) = sorted_variants.get(variant_idx) else {
                    break;
                };
                let vrid = *vrid;
                let vpos = *vpos;

                // Check whether the current record is past this variant.
                let past = match (rec_rid, rec_start) {
                    (Some(rr), Some(rs)) => rr > vrid || (rr == vrid && rs > vpos),
                    _ => false,
                };

                if !past {
                    // The current record may still overlap this variant; stop
                    // processing variants for now.
                    break;
                }

                // All reads for this variant have been buffered. Spike it.
                let variant = sorted_variants[variant_idx].2.clone();
                let effective_vaf = variant.expected_vaf * purity;
                if let Some(sv) =
                    self.spike_variant_in_buffer(&mut buffer, &header, &variant, effective_vaf)?
                {
                    spiked.push(sv);
                }

                variant_idx += 1;
            }

            // Flush records that cannot overlap any remaining variant.
            let min = min_active(variant_idx, &sorted_variants);
            let flush_up_to = find_flush_boundary(&buffer, min);

            for record in buffer.drain(..flush_up_to) {
                writer
                    .write_alignment_record(&header, &record)
                    .context("failed to write BAM record")?;
            }
        }

        // End of input: process all remaining variants against the buffer.
        while variant_idx < sorted_variants.len() {
            let variant = sorted_variants[variant_idx].2.clone();
            let effective_vaf = variant.expected_vaf * purity;
            if let Some(sv) =
                self.spike_variant_in_buffer(&mut buffer, &header, &variant, effective_vaf)?
            {
                spiked.push(sv);
            }
            variant_idx += 1;
        }

        // Flush all remaining buffered records.
        for record in &buffer {
            writer
                .write_alignment_record(&header, record)
                .context("failed to write BAM record")?;
        }

        writer.try_finish().context("failed to finalise BAM file")?;

        // Write truth VCF if requested.
        if let Some(ref vcf_path) = self.config.truth_vcf.clone() {
            let contigs: Vec<(String, u64)> = header
                .reference_sequences()
                .iter()
                .map(|(name, map)| {
                    let name = String::from_utf8_lossy(name).into_owned();
                    let len = usize::from(map.length()) as u64;
                    (name, len)
                })
                .collect();

            write_truth_vcf(vcf_path, &self.config.sample_name, &contigs, &spiked)?;
        }

        Ok(spiked)
    }

    /// Spike a single variant into records held in the buffer.
    ///
    /// Only records that overlap the variant position are eligible. The method
    /// modifies records in place.
    fn spike_variant_in_buffer(
        &mut self,
        buffer: &mut [RecordBuf],
        header: &sam::Header,
        variant: &Variant,
        effective_vaf: f64,
    ) -> Result<Option<SpikedVariant>> {
        let var_pos = variant.pos();
        let chrom = &variant.chrom;

        let ref_id = match header.reference_sequences().get_index_of(chrom.as_bytes()) {
            Some(id) => id,
            None => {
                tracing::warn!("chromosome '{}' not found in BAM header", chrom);
                return Ok(None);
            }
        };

        // Collect indices of overlapping records in the buffer.
        let overlapping: Vec<usize> = buffer
            .iter()
            .enumerate()
            .filter_map(|(i, record)| {
                let rid = record.reference_sequence_id()?;
                if rid != ref_id {
                    return None;
                }
                let align_start = usize::from(record.alignment_start()?) as u64 - 1;
                let read_len = record.sequence().len() as u64;
                let align_end = align_start + read_len;

                // Skip MQ=0 reads.
                let mq = record.mapping_quality().map(u8::from).unwrap_or(0);
                if mq == 0 {
                    return None;
                }

                if var_pos >= align_start && var_pos < align_end {
                    Some(i)
                } else {
                    None
                }
            })
            .collect();

        let total_depth = overlapping.len() as u32;
        if total_depth == 0 {
            tracing::debug!("variant at {}:{} has zero depth, skipping", chrom, var_pos);
            return Ok(None);
        }

        // Sample alt count using binomial distribution (stochastic VAF).
        let alt_count = sample_alt_count(total_depth, effective_vaf, &mut self.rng);
        if alt_count == 0 {
            return Ok(Some(SpikedVariant {
                variant: variant.clone(),
                total_depth,
                alt_count: 0,
                actual_vaf: 0.0,
            }));
        }

        // Select which reads to modify, respecting strand balance.
        let to_modify =
            select_reads_for_modification(buffer, &overlapping, alt_count as usize, &mut self.rng);

        // Apply the modification to selected reads.
        let mut actual_modified: u32 = 0;
        for idx in to_modify {
            let record = &mut buffer[idx];
            let result = match &variant.mutation {
                MutationType::Snv { .. } => apply_snv(record, &variant.mutation),
                MutationType::Indel {
                    ref_seq, alt_seq, ..
                } => {
                    if alt_seq.len() > ref_seq.len() {
                        apply_insertion(record, &variant.mutation)
                    } else {
                        apply_deletion(record, &variant.mutation)
                    }
                }
                _ => ModifyResult::Unchanged,
            };
            if result == ModifyResult::Modified {
                actual_modified += 1;
            }
        }

        let actual_vaf = if total_depth > 0 {
            actual_modified as f64 / total_depth as f64
        } else {
            0.0
        };

        Ok(Some(SpikedVariant {
            variant: variant.clone(),
            total_depth,
            alt_count: actual_modified,
            actual_vaf,
        }))
    }
}

// ---------------------------------------------------------------------------
// Flush boundary helper
// ---------------------------------------------------------------------------

/// Return the number of leading buffer entries that can be safely flushed.
///
/// A record can be flushed if it cannot overlap any remaining variant. Given
/// the earliest remaining variant at `min_active` (rid, pos), a record is safe
/// to flush if:
///   - its reference id is less than min_active.rid, or
///   - its reference id equals min_active.rid and its alignment end <= min_active.pos.
///
/// When there are no remaining variants, all records are safe to flush.
fn find_flush_boundary(buffer: &[RecordBuf], min_active: Option<(usize, u64)>) -> usize {
    let Some((min_rid, min_pos)) = min_active else {
        // No variants left; flush everything.
        return buffer.len();
    };

    let mut count = 0;
    for record in buffer {
        let rid = match record.reference_sequence_id() {
            Some(r) => r,
            None => {
                // Unmapped record; safe to flush.
                count += 1;
                continue;
            }
        };
        let align_start = match record.alignment_start() {
            Some(p) => usize::from(p) as u64 - 1,
            None => {
                count += 1;
                continue;
            }
        };
        let read_len = record.sequence().len() as u64;
        let align_end = align_start + read_len;

        let safe = rid < min_rid || (rid == min_rid && align_end <= min_pos);
        if safe {
            count += 1;
        } else {
            // Records are ordered; once one is unsafe, all later ones may be too.
            break;
        }
    }
    count
}

// ---------------------------------------------------------------------------
// Read selection
// ---------------------------------------------------------------------------

/// Select `n` read indices to modify, respecting strand balance.
fn select_reads_for_modification(
    records: &[RecordBuf],
    overlapping: &[usize],
    n: usize,
    rng: &mut StdRng,
) -> Vec<usize> {
    if n == 0 || overlapping.is_empty() {
        return Vec::new();
    }

    let n = n.min(overlapping.len());

    // Separate forward and reverse reads.
    let mut fwd: Vec<usize> = Vec::new();
    let mut rev: Vec<usize> = Vec::new();
    for &idx in overlapping {
        if records[idx].flags().is_reverse_complemented() {
            rev.push(idx);
        } else {
            fwd.push(idx);
        }
    }

    // Proportion of forward/reverse reads.
    let total = overlapping.len();
    let n_fwd = ((n as f64 * fwd.len() as f64 / total as f64).round() as usize).min(fwd.len());
    let n_rev = (n - n_fwd).min(rev.len());
    let n_fwd = n - n_rev; // re-balance in case n_rev was capped

    let selected_fwd = reservoir_sample(&fwd, n_fwd, rng);
    let selected_rev = reservoir_sample(&rev, n_rev, rng);

    let mut selected: Vec<usize> = selected_fwd.into_iter().chain(selected_rev).collect();
    selected.sort_unstable();
    selected
}

/// Reservoir sampling without replacement.
fn reservoir_sample(items: &[usize], k: usize, rng: &mut StdRng) -> Vec<usize> {
    if k == 0 || items.is_empty() {
        return Vec::new();
    }
    let k = k.min(items.len());
    let mut result: Vec<usize> = items[..k].to_vec();
    for (i, &item) in items[k..].iter().enumerate() {
        let j = rng.random_range(0..=(i + k));
        if j < k {
            result[j] = item;
        }
    }
    result
}

// ---------------------------------------------------------------------------
// BAM I/O helpers (kept for tests)
// ---------------------------------------------------------------------------

/// Load all records from a BAM file into memory.
///
/// Used in tests. Production code uses the streaming path in `BamEditor::run`.
#[cfg(test)]
pub fn load_bam(path: &Path) -> Result<(sam::Header, Vec<RecordBuf>)> {
    let file = std::fs::File::open(path)
        .with_context(|| format!("failed to open BAM file: {}", path.display()))?;
    let mut reader = bam::io::Reader::new(file);
    let header = reader
        .read_header()
        .with_context(|| format!("failed to read BAM header: {}", path.display()))?;

    let mut records = Vec::new();
    for result in reader.record_bufs(&header) {
        let record = result.with_context(|| "failed to read BAM record")?;
        records.push(record);
    }

    Ok((header, records))
}

/// Write records to a BAM file.
#[cfg(test)]
pub fn write_bam(path: &Path, header: &sam::Header, records: &[RecordBuf]) -> Result<()> {
    use noodles_sam::alignment::io::Write as _;

    let file = std::fs::File::create(path)
        .with_context(|| format!("failed to create BAM file: {}", path.display()))?;
    let mut writer = bam::io::Writer::new(file);
    writer
        .write_header(header)
        .context("failed to write BAM header")?;

    for record in records {
        writer
            .write_alignment_record(header, record)
            .context("failed to write BAM record")?;
    }

    writer.try_finish().context("failed to finalize BAM file")?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Truth VCF writer
// ---------------------------------------------------------------------------

/// Write a truth VCF for the spiked variants.
fn write_truth_vcf(
    path: &Path,
    sample_name: &str,
    contigs: &[(String, u64)],
    spiked: &[SpikedVariant],
) -> Result<()> {
    use std::io::Write;

    let file = std::fs::File::create(path)
        .with_context(|| format!("failed to create truth VCF: {}", path.display()))?;
    let mut w = BufWriter::new(file);

    writeln!(w, "##fileformat=VCFv4.3")?;
    writeln!(w, "##source=VarForge-edit")?;
    writeln!(
        w,
        r#"##INFO=<ID=DP,Number=1,Type=Integer,Description="Total depth at variant site">"#
    )?;
    writeln!(
        w,
        r#"##INFO=<ID=AC,Number=1,Type=Integer,Description="Actual alt count after spike-in">"#
    )?;
    writeln!(
        w,
        r#"##INFO=<ID=AF,Number=1,Type=Float,Description="Actual allele frequency after spike-in">"#
    )?;
    writeln!(
        w,
        r#"##INFO=<ID=EXPECTED_VAF,Number=1,Type=Float,Description="Expected variant allele frequency">"#
    )?;
    writeln!(
        w,
        r#"##INFO=<ID=VARTYPE,Number=1,Type=String,Description="Variant type">"#
    )?;
    for (name, length) in contigs {
        writeln!(w, "##contig=<ID={name},length={length}>")?;
    }
    writeln!(
        w,
        r#"##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">"#
    )?;
    writeln!(
        w,
        "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t{sample_name}"
    )?;

    for sv in spiked {
        let v = &sv.variant;
        let (ref_allele, alt_allele) = variant_alleles(v);
        let pos_1based = v.pos() + 1;
        let vartype = v.vartype();
        let info = format!(
            "DP={dp};AC={ac};AF={af:.6};EXPECTED_VAF={evaf:.6};VARTYPE={vartype}",
            dp = sv.total_depth,
            ac = sv.alt_count,
            af = sv.actual_vaf,
            evaf = v.expected_vaf,
        );
        writeln!(
            w,
            "{chrom}\t{pos}\t.\t{ref}\t{alt}\t.\tPASS\t{info}\tGT\t0/1",
            chrom = v.chrom,
            pos = pos_1based,
            ref = String::from_utf8_lossy(&ref_allele),
            alt = String::from_utf8_lossy(&alt_allele),
        )?;
    }

    w.flush().context("failed to flush truth VCF")?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Variant helpers
// ---------------------------------------------------------------------------

fn variant_alleles(v: &Variant) -> (Vec<u8>, Vec<u8>) {
    match &v.mutation {
        MutationType::Snv {
            ref_base, alt_base, ..
        } => (vec![*ref_base], vec![*alt_base]),
        MutationType::Indel {
            ref_seq, alt_seq, ..
        } => (ref_seq.clone(), alt_seq.clone()),
        MutationType::Mnv {
            ref_seq, alt_seq, ..
        } => (ref_seq.clone(), alt_seq.clone()),
        MutationType::Sv { .. } => (b"N".to_vec(), b"<SV>".to_vec()),
    }
}

// ---------------------------------------------------------------------------
// UMI-aware editing helpers
// ---------------------------------------------------------------------------

/// Group record indices by UMI family (MI tag).
///
/// Returns a map from family_id to Vec<record_index>.
// Called only in tests; future UMI-collapse logic will use this in production.
#[allow(dead_code)]
pub fn group_by_umi_family(records: &[RecordBuf]) -> HashMap<i64, Vec<usize>> {
    let mut families: HashMap<i64, Vec<usize>> = HashMap::new();
    for (i, record) in records.iter().enumerate() {
        if let Some(mi) = record.data().get(&Tag::UMI_ID).and_then(|v| v.as_int()) {
            families.entry(mi).or_default().push(i);
        }
    }
    families
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use noodles_core::Position as NoodlesPosition;
    use noodles_sam::alignment::{
        record::Flags,
        record_buf::{Cigar, Data, QualityScores, Sequence},
        RecordBuf,
    };
    use noodles_sam::header::record::value::{map::ReferenceSequence, Map};
    use std::num::NonZeroUsize;
    use tempfile::NamedTempFile;

    // ------------------------------------------------------------------
    // Helpers to build minimal in-memory BAM data
    // ------------------------------------------------------------------

    fn build_header(refs: &[(&str, u64)]) -> sam::Header {
        let mut builder = sam::Header::builder();
        for (name, len) in refs {
            let nz_len = NonZeroUsize::new(*len as usize).unwrap();
            builder = builder.add_reference_sequence(*name, Map::<ReferenceSequence>::new(nz_len));
        }
        builder.build()
    }

    fn make_record(
        seq: &[u8],
        qual: &[u8],
        ref_id: usize,
        align_start: u64, // 0-based
        flags: Flags,
    ) -> RecordBuf {
        let cigar_str = format!("{}M", seq.len());
        let cigar_ops = crate::io::bam::parse_cigar(&cigar_str).unwrap();
        RecordBuf::builder()
            .set_flags(flags)
            .set_reference_sequence_id(ref_id)
            .set_alignment_start(NoodlesPosition::new(align_start as usize + 1).unwrap())
            .set_mapping_quality(noodles_sam::alignment::record::MappingQuality::new(60).unwrap())
            .set_cigar(cigar_ops.into_iter().collect::<Cigar>())
            .set_sequence(Sequence::from(seq))
            .set_quality_scores(QualityScores::from(qual.to_vec()))
            .build()
    }

    fn make_records_at_pos(
        n: usize,
        ref_id: usize,
        align_start: u64,
        seq: &[u8],
    ) -> Vec<RecordBuf> {
        (0..n)
            .map(|_| {
                make_record(
                    seq,
                    &vec![30u8; seq.len()],
                    ref_id,
                    align_start,
                    Flags::empty(),
                )
            })
            .collect()
    }

    // ------------------------------------------------------------------
    // Test 1: SNV spike-in, base changed, quality preserved
    // ------------------------------------------------------------------
    #[test]
    fn test_snv_spike_in_bam() {
        let header = build_header(&[("chr1", 1000)]);
        // 20 reads of "ACGTACGTACGTACGT" at position 100.
        let seq = b"ACGTACGTACGTACGTACGT";
        let records = make_records_at_pos(20, 0, 100, seq);

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 102,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 1.0, // 100% so all reads are modified
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        let spiked = editor.run().unwrap();

        assert_eq!(spiked.len(), 1);
        assert!(
            spiked[0].alt_count > 0,
            "at least some reads should be modified"
        );

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        // At least some reads should have 'T' at offset 2.
        let alt_count = out_records
            .iter()
            .filter(|r| {
                let s: Vec<u8> = r.sequence().as_ref().to_vec();
                s.get(2) == Some(&b'T')
            })
            .count();
        assert!(alt_count > 0, "SNV should be applied to at least one read");
    }

    // ------------------------------------------------------------------
    // Test 2: Stochastic VAF - 50% VAF at 1000x depth should be ~50%
    // ------------------------------------------------------------------
    #[test]
    fn test_snv_stochastic_vaf() {
        let header = build_header(&[("chr1", 10000)]);
        let seq = b"ACGTACGTACGTACGTACGT"; // 20 bases
        let records = make_records_at_pos(1000, 0, 100, seq);

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 102,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 0.5,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        let spiked = editor.run().unwrap();

        assert_eq!(spiked.len(), 1);
        let sv = &spiked[0];
        // Should be roughly 50% +/- 10%.
        assert!(
            sv.actual_vaf >= 0.40 && sv.actual_vaf <= 0.60,
            "VAF at 50% target should be in [0.40, 0.60], got {}",
            sv.actual_vaf
        );
    }

    // ------------------------------------------------------------------
    // Test 3: Insertion spike-in
    // ------------------------------------------------------------------
    #[test]
    fn test_indel_insertion_bam() {
        let header = build_header(&[("chr1", 1000)]);
        let seq = b"ACGTACGTACGTACGT";
        let records = make_records_at_pos(10, 0, 100, seq);

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Indel {
                pos: 102,
                ref_seq: b"G".to_vec(),
                alt_seq: b"GTT".to_vec(),
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        let spiked = editor.run().unwrap();

        assert!(spiked[0].alt_count > 0, "insertion should be applied");

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        let has_insertion = out_records.iter().any(|r| {
            let ops: Vec<_> = r.cigar().as_ref().to_vec();
            ops.iter()
                .any(|op| op.kind() == noodles_sam::alignment::record::cigar::op::Kind::Insertion)
        });
        assert!(
            has_insertion,
            "at least one record should have an Insertion CIGAR op"
        );
    }

    // ------------------------------------------------------------------
    // Test 4: Deletion spike-in
    // ------------------------------------------------------------------
    #[test]
    fn test_indel_deletion_bam() {
        let header = build_header(&[("chr1", 1000)]);
        let seq = b"ACGTACGTACGTACGT";
        let records = make_records_at_pos(10, 0, 100, seq);

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Indel {
                pos: 102,
                ref_seq: b"GT".to_vec(),
                alt_seq: b"G".to_vec(),
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        let spiked = editor.run().unwrap();
        assert!(spiked[0].alt_count > 0, "deletion should be applied");

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        let has_deletion = out_records.iter().any(|r| {
            let ops: Vec<_> = r.cigar().as_ref().to_vec();
            ops.iter()
                .any(|op| op.kind() == noodles_sam::alignment::record::cigar::op::Kind::Deletion)
        });
        assert!(
            has_deletion,
            "at least one record should have a Deletion CIGAR op"
        );
    }

    // ------------------------------------------------------------------
    // Test 5: NM tag updated
    // ------------------------------------------------------------------
    #[test]
    fn test_nm_tag_updated() {
        use noodles_sam::alignment::record::data::field::Tag;
        let header = build_header(&[("chr1", 1000)]);
        let seq = b"ACGTACGT";
        // Build records with NM=0.
        let records: Vec<RecordBuf> = (0..5)
            .map(|_| {
                let cigar_ops = crate::io::bam::parse_cigar("8M").unwrap();
                let mut data = Data::default();
                data.insert(
                    Tag::EDIT_DISTANCE,
                    noodles_sam::alignment::record_buf::data::field::Value::from(0i32),
                );
                RecordBuf::builder()
                    .set_flags(Flags::empty())
                    .set_reference_sequence_id(0)
                    .set_alignment_start(NoodlesPosition::new(101).unwrap())
                    .set_mapping_quality(
                        noodles_sam::alignment::record::MappingQuality::new(60).unwrap(),
                    )
                    .set_cigar(cigar_ops.into_iter().collect::<Cigar>())
                    .set_sequence(Sequence::from(seq.as_ref()))
                    .set_quality_scores(QualityScores::from(vec![30u8; seq.len()]))
                    .set_data(data)
                    .build()
            })
            .collect();

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 100,
                ref_base: b'A',
                alt_base: b'C',
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        editor.run().unwrap();

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        // Any modified record should have NM=1.
        let modified_nm: Vec<i64> = out_records
            .iter()
            .filter_map(|r| {
                let s: Vec<u8> = r.sequence().as_ref().to_vec();
                if s.first() == Some(&b'C') {
                    r.data().get(&Tag::EDIT_DISTANCE).and_then(|v| v.as_int())
                } else {
                    None
                }
            })
            .collect();

        for nm in &modified_nm {
            assert_eq!(
                *nm, 1,
                "NM should be 1 after one SNV modification, got {nm}"
            );
        }
        assert!(!modified_nm.is_empty(), "some records should be modified");
    }

    // ------------------------------------------------------------------
    // Test 6: Strand balance
    // ------------------------------------------------------------------
    #[test]
    fn test_strand_balance() {
        let header = build_header(&[("chr1", 1000)]);
        let seq = b"ACGTACGTACGTACGT";
        let mut records = Vec::new();

        // 50 forward, 50 reverse reads at the same position.
        for _ in 0..50 {
            records.push(make_record(
                seq,
                &vec![30u8; seq.len()],
                0,
                100,
                Flags::empty(),
            ));
        }
        for _ in 0..50 {
            records.push(make_record(
                seq,
                &vec![30u8; seq.len()],
                0,
                100,
                Flags::REVERSE_COMPLEMENTED,
            ));
        }

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 102,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 0.5, // 50 of 100 reads
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        editor.run().unwrap();

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();

        let mut fwd_alt = 0usize;
        let mut rev_alt = 0usize;
        for r in &out_records {
            let s: Vec<u8> = r.sequence().as_ref().to_vec();
            if s.get(2) == Some(&b'T') {
                if r.flags().is_reverse_complemented() {
                    rev_alt += 1;
                } else {
                    fwd_alt += 1;
                }
            }
        }

        // Both strands should have some alt reads.
        // With 50 of 100 reads modified at 50:50 strand ratio, we expect ~25 per strand.
        // Allow generous margin (at least 5 on each strand out of 50).
        assert!(
            fwd_alt >= 5,
            "forward strand should have some alt reads, got {fwd_alt}"
        );
        assert!(
            rev_alt >= 5,
            "reverse strand should have some alt reads, got {rev_alt}"
        );
    }

    // ------------------------------------------------------------------
    // Test 7: Quality preserved for SNV
    // ------------------------------------------------------------------
    #[test]
    fn test_quality_preserved() {
        let header = build_header(&[("chr1", 1000)]);
        let seq = b"ACGTACGT";
        let quals: Vec<u8> = (20u8..28).collect();
        let record = {
            let cigar_ops = crate::io::bam::parse_cigar("8M").unwrap();
            RecordBuf::builder()
                .set_flags(Flags::empty())
                .set_reference_sequence_id(0)
                .set_alignment_start(NoodlesPosition::new(101).unwrap())
                .set_mapping_quality(
                    noodles_sam::alignment::record::MappingQuality::new(60).unwrap(),
                )
                .set_cigar(cigar_ops.into_iter().collect::<Cigar>())
                .set_sequence(Sequence::from(seq.as_ref()))
                .set_quality_scores(QualityScores::from(quals.clone()))
                .build()
        };

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &[record]).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 102,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        editor.run().unwrap();

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        assert_eq!(out_records.len(), 1);
        let out_quals: Vec<u8> = out_records[0].quality_scores().as_ref().to_vec();
        assert_eq!(
            out_quals, quals,
            "quality scores should be identical after SNV"
        );
    }

    // ------------------------------------------------------------------
    // Test 8: Unmodified reads are byte-identical to input
    // ------------------------------------------------------------------
    #[test]
    fn test_unmodified_reads_unchanged() {
        let header = build_header(&[("chr1", 2000)]);
        let near_seq = b"ACGTACGT";
        let far_seq = b"TTTTTTTT";

        let near_record = make_record(near_seq, &[30u8; 8], 0, 100, Flags::empty());
        let far_record = make_record(far_seq, &[30u8; 8], 0, 500, Flags::empty()); // won't overlap pos 102

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &[near_record, far_record]).unwrap();

        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 102,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        editor.run().unwrap();

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        assert_eq!(out_records.len(), 2);

        // The far read (index 1) should be byte-for-byte identical.
        let out_far_seq: Vec<u8> = out_records[1].sequence().as_ref().to_vec();
        assert_eq!(
            out_far_seq,
            far_seq.to_vec(),
            "non-overlapping read must be unchanged"
        );
    }

    // ------------------------------------------------------------------
    // Test 9: Streaming - total record count preserved
    // ------------------------------------------------------------------
    #[test]
    fn test_streaming_record_count_preserved() {
        let header = build_header(&[("chr1", 5000)]);
        let seq = b"ACGTACGTACGTACGT"; // 16 bases

        // Records at varying positions to exercise the flush boundary logic.
        let mut records = Vec::new();
        for start in [0u64, 100, 500, 1000, 2000, 3000] {
            for _ in 0..5 {
                records.push(make_record(seq, &[30u8; 16], 0, start, Flags::empty()));
            }
        }
        let total = records.len();

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        // Spike a variant in the middle.
        let variant = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 502,
                ref_base: b'G',
                alt_base: b'T',
            },
            expected_vaf: 0.5,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![variant],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        editor.run().unwrap();

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        assert_eq!(
            out_records.len(),
            total,
            "output must contain exactly as many records as input"
        );
    }

    // ------------------------------------------------------------------
    // Test 10: Multiple variants on the same chromosome
    // ------------------------------------------------------------------
    #[test]
    fn test_multiple_variants_same_chrom() {
        let header = build_header(&[("chr1", 5000)]);
        let seq = b"AAAAAAAAAAAAAAAAAAAA"; // 20 As

        // Two groups of reads at different positions.
        let mut records = Vec::new();
        for _ in 0..10 {
            records.push(make_record(seq, &[30u8; 20], 0, 100, Flags::empty()));
        }
        for _ in 0..10 {
            records.push(make_record(seq, &[30u8; 20], 0, 1000, Flags::empty()));
        }

        let tmp_in = NamedTempFile::new().unwrap();
        let tmp_out = NamedTempFile::new().unwrap();
        write_bam(tmp_in.path(), &header, &records).unwrap();

        // Two variants, one at each group.
        let v1 = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 105,
                ref_base: b'A',
                alt_base: b'T',
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };
        let v2 = Variant {
            chrom: "chr1".to_string(),
            mutation: MutationType::Snv {
                pos: 1005,
                ref_base: b'A',
                alt_base: b'C',
            },
            expected_vaf: 1.0,
            clone_id: None,
            haplotype: None,
            ccf: None,
        };

        let config = EditConfig {
            input_bam: tmp_in.path().to_path_buf(),
            output_bam: tmp_out.path().to_path_buf(),
            variants: vec![v1, v2],
            seed: 42,
            purity: 1.0,
            truth_vcf: None,
            sample_name: "TEST".to_string(),
        };

        let mut editor = BamEditor::new(config);
        let spiked = editor.run().unwrap();

        assert_eq!(spiked.len(), 2, "both variants should be spiked");
        assert!(spiked[0].alt_count > 0, "first variant should modify reads");
        assert!(
            spiked[1].alt_count > 0,
            "second variant should modify reads"
        );

        let (_, out_records) = load_bam(tmp_out.path()).unwrap();
        assert_eq!(out_records.len(), 20, "record count must be preserved");

        // First group: base at offset 5 should be T.
        let first_group_alt = out_records[..10]
            .iter()
            .filter(|r| r.sequence().as_ref().get(5) == Some(&b'T'))
            .count();
        assert!(
            first_group_alt > 0,
            "first variant should modify first group"
        );

        // Second group: base at offset 5 should be C.
        let second_group_alt = out_records[10..]
            .iter()
            .filter(|r| r.sequence().as_ref().get(5) == Some(&b'C'))
            .count();
        assert!(
            second_group_alt > 0,
            "second variant should modify second group"
        );
    }
}