varforge 0.2.0

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
//! BAM output writer for simulated reads.
//!
//! Converts internal [`Read`] and [`ReadPair`] records into noodles BAM
//! records and writes them to a bgzip-compressed, coordinate-sorted BAM file
//! with an accompanying BAI/CSI index.
use std::fs::File;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use noodles_bam as bam;
use noodles_bgzf as bgzf;
use noodles_core::Position;
use noodles_csi::binning_index::index::reference_sequence::bin::Chunk;
use noodles_csi::binning_index::Indexer as CsiIndexer;
use noodles_sam::{
    self as sam,
    alignment::{
        record::{
            cigar::{op::Kind, Op},
            data::field::Tag,
            Flags, MappingQuality,
        },
        record_buf::{data::field::Value, Cigar, Data, QualityScores, Sequence},
        RecordBuf,
    },
    header::record::value::{
        map::{
            self, header::Version, program::tag as pg_tag, Program, ReadGroup, ReferenceSequence,
        },
        Map,
    },
};

use crate::io::config::SampleConfig;

/// BAM writer that outputs aligned paired-end reads with proper SAM headers.
pub struct BamWriter {
    writer: bam::io::Writer<bgzf::io::Writer<File>>,
    header: sam::Header,
    sample_name: String,
    /// Mapping quality written to every record.
    mapq: u8,
    /// Path to the BAM file, used to write the `.bai` index after finishing.
    path: PathBuf,
}

impl BamWriter {
    /// Create a new BAM writer at `path`.
    ///
    /// The BAM header is constructed from:
    /// - `ref_sequences`: list of (name, length) pairs for @SQ lines
    /// - `sample_config`: used for @RG read group fields
    pub fn new(
        path: &Path,
        ref_sequences: &[(String, u64)],
        sample_config: &SampleConfig,
        mapq: u8,
    ) -> Result<Self> {
        let file = File::create(path)
            .with_context(|| format!("failed to create BAM file: {}", path.display()))?;

        // Normalise to uppercase so values like "illumina" satisfy GATK's
        // requirement that PL tags use uppercase platform names.
        let platform = sample_config
            .platform
            .as_deref()
            .unwrap_or("ILLUMINA")
            .to_uppercase();

        let sample_name = sample_config.name.clone();

        // Build the @HD line with VN:1.6 SO:coordinate
        let hd = {
            let mut hd = Map::<map::Header>::new(Version::new(1, 6));
            // Set sort order to coordinate via other_fields
            let so_tag =
                noodles_sam::header::record::value::map::tag::Other::try_from([b'S', b'O'])
                    .map_err(|_| anyhow::anyhow!("invalid SO tag"))?;
            hd.other_fields_mut().insert(so_tag, "coordinate".into());
            hd
        };

        // Build header with @SQ lines
        let mut builder = sam::Header::builder().set_header(hd);

        for (name, length) in ref_sequences {
            let len = NonZeroUsize::try_from(*length as usize)
                .with_context(|| format!("reference sequence length 0 for {}", name))?;
            builder =
                builder.add_reference_sequence(name.as_str(), Map::<ReferenceSequence>::new(len));
        }

        // Build @RG with SM, PL, LB fields via other_fields
        let mut rg = Map::<ReadGroup>::default();
        {
            let sm_tag =
                noodles_sam::header::record::value::map::tag::Other::try_from([b'S', b'M'])
                    .map_err(|_| anyhow::anyhow!("invalid SM tag"))?;
            rg.other_fields_mut()
                .insert(sm_tag, sample_name.as_str().into());

            let pl_tag =
                noodles_sam::header::record::value::map::tag::Other::try_from([b'P', b'L'])
                    .map_err(|_| anyhow::anyhow!("invalid PL tag"))?;
            rg.other_fields_mut()
                .insert(pl_tag, platform.as_str().into());

            let lb_tag =
                noodles_sam::header::record::value::map::tag::Other::try_from([b'L', b'B'])
                    .map_err(|_| anyhow::anyhow!("invalid LB tag"))?;
            rg.other_fields_mut()
                .insert(lb_tag, sample_name.as_str().into());
        }

        // Build @PG with PN and VN fields.
        let mut pg = Map::<Program>::default();
        pg.other_fields_mut()
            .insert(pg_tag::NAME, "varforge".into());
        pg.other_fields_mut()
            .insert(pg_tag::VERSION, env!("CARGO_PKG_VERSION").into());

        let header = builder
            .add_read_group(sample_name.as_str(), rg)
            .add_program("varforge", pg)
            .build();

        let mut writer = bam::io::Writer::new(file);
        writer
            .write_header(&header)
            .context("failed to write BAM header")?;

        Ok(Self {
            writer,
            header,
            sample_name,
            mapq,
            path: path.to_path_buf(),
        })
    }

    /// Write an aligned read pair to the BAM file.
    ///
    /// - `pair`: the [`ReadPair`] holding sequences and qualities
    /// - `ref_id`: 0-based reference sequence index (index into @SQ lines)
    /// - `pos`: 0-based alignment start position on the reference (converted to 1-based internally)
    /// - `cigar_r1`: CIGAR string for read 1 (e.g. `"150M"`)
    /// - `cigar_r2`: CIGAR string for read 2 (e.g. `"150M"`)
    pub fn write_pair(
        &mut self,
        pair: &crate::core::types::ReadPair,
        ref_id: usize,
        pos: u64,
        cigar_r1: &str,
        cigar_r2: &str,
    ) -> Result<()> {
        // Compute NM from read vs reference and CIGAR for each read.
        let nm_r1 = compute_nm(&pair.read1.seq, &pair.ref_seq_r1, cigar_r1);
        let r2_seq_rc = crate::seq_utils::reverse_complement(&pair.read2.seq);
        let ref_r2_rc = crate::seq_utils::reverse_complement(&pair.ref_seq_r2);
        let nm_r2 = compute_nm(&r2_seq_rc, &ref_r2_rc, cigar_r2);

        let data_r1: Data = [
            (Tag::READ_GROUP, Value::from(self.sample_name.as_str())),
            (Tag::EDIT_DISTANCE, Value::from(nm_r1 as i32)),
        ]
        .into_iter()
        .collect();
        let data_r2: Data = [
            (Tag::READ_GROUP, Value::from(self.sample_name.as_str())),
            (Tag::EDIT_DISTANCE, Value::from(nm_r2 as i32)),
        ]
        .into_iter()
        .collect();
        self.write_pair_inner(pair, ref_id, pos, cigar_r1, cigar_r2, data_r1, data_r2)
    }

    /// Write a single aligned read to the BAM file.
    ///
    /// Used for long-read platforms where each molecule produces one record.
    /// The record uses the pair QNAME and read1 sequence/quality. No mate
    /// information is set; flags indicate a primary, unpaired, mapped read.
    ///
    /// - `pair`: the [`ReadPair`] whose `read1` field is written
    /// - `ref_id`: 0-based reference sequence index
    /// - `pos`: 0-based alignment start position on the reference
    /// - `cigar`: CIGAR string for the read (e.g. `"15000M"`)
    pub fn write_single_read(
        &mut self,
        pair: &crate::core::types::ReadPair,
        ref_id: usize,
        pos: u64,
        cigar: &str,
    ) -> Result<()> {
        let pos1 = Position::new(pos as usize + 1)
            .ok_or_else(|| anyhow::anyhow!("invalid alignment position"))?;

        let mapq = MappingQuality::new(60).expect("60 is a valid mapping quality");

        let data: Data = [(Tag::READ_GROUP, Value::from(self.sample_name.as_str()))]
            .into_iter()
            .collect();

        // Flag 0: primary, mapped, single-segment (not paired).
        let flags = Flags::empty();

        let cigar_ops =
            parse_cigar(cigar).with_context(|| format!("failed to parse CIGAR: {cigar}"))?;

        let record = RecordBuf::builder()
            .set_name(pair.name.as_str())
            .set_flags(flags)
            .set_reference_sequence_id(ref_id)
            .set_alignment_start(pos1)
            .set_mapping_quality(mapq)
            .set_cigar(cigar_ops.into_iter().collect::<Cigar>())
            .set_sequence(Sequence::from(pair.read1.seq.as_slice()))
            .set_quality_scores(QualityScores::from(pair.read1.qual.clone()))
            .set_data(data)
            .build();

        self.emit(&record)
    }

    /// Write a read pair with UMI tags (RX and MI).
    ///
    /// - `umi`: UMI barcode sequence string (written as `RX:Z:...`)
    /// - `family_id`: molecular family identifier (written as `MI:i:...`)
    #[allow(clippy::too_many_arguments)]
    pub fn write_pair_with_umi(
        &mut self,
        pair: &crate::core::types::ReadPair,
        ref_id: usize,
        pos: u64,
        cigar_r1: &str,
        cigar_r2: &str,
        umi: &str,
        family_id: i32,
    ) -> Result<()> {
        // Compute NM from read vs reference and CIGAR for each read.
        let nm_r1 = compute_nm(&pair.read1.seq, &pair.ref_seq_r1, cigar_r1);
        let r2_seq_rc = crate::seq_utils::reverse_complement(&pair.read2.seq);
        let ref_r2_rc = crate::seq_utils::reverse_complement(&pair.ref_seq_r2);
        let nm_r2 = compute_nm(&r2_seq_rc, &ref_r2_rc, cigar_r2);

        let data_r1: Data = [
            (Tag::READ_GROUP, Value::from(self.sample_name.as_str())),
            (Tag::UMI_SEQUENCE, Value::from(umi)),
            (Tag::UMI_ID, Value::from(family_id)),
            (Tag::EDIT_DISTANCE, Value::from(nm_r1 as i32)),
        ]
        .into_iter()
        .collect();
        let data_r2: Data = [
            (Tag::READ_GROUP, Value::from(self.sample_name.as_str())),
            (Tag::UMI_SEQUENCE, Value::from(umi)),
            (Tag::UMI_ID, Value::from(family_id)),
            (Tag::EDIT_DISTANCE, Value::from(nm_r2 as i32)),
        ]
        .into_iter()
        .collect();
        self.write_pair_inner(pair, ref_id, pos, cigar_r1, cigar_r2, data_r1, data_r2)
    }

    /// Build and emit a paired-end read pair with caller-supplied per-read data fields.
    ///
    /// This is the single place that sets alignment positions, flags, CIGAR, sequence,
    /// and quality scores for a pair. Both `write_pair` and `write_pair_with_umi`
    /// delegate here, passing only the data fields that differ between them.
    #[allow(clippy::too_many_arguments)]
    fn write_pair_inner(
        &mut self,
        pair: &crate::core::types::ReadPair,
        ref_id: usize,
        pos: u64,
        cigar_r1: &str,
        cigar_r2: &str,
        data_r1: Data,
        data_r2: Data,
    ) -> Result<()> {
        // Positions in noodles are 1-based.
        let pos1 = Position::new(pos as usize + 1)
            .ok_or_else(|| anyhow::anyhow!("invalid alignment position"))?;

        // R2 starts at fragment_start + fragment_length - r2_read_length (0-based).
        // This is the correct paired-end position formula: R2 ends at the far end of
        // the fragment and reads back toward R1.
        let r2_len = pair.read2.seq.len();
        let pos2_zero = (pos as usize + pair.fragment_length).saturating_sub(r2_len);
        let pos2 = Position::new(pos2_zero + 1)
            .ok_or_else(|| anyhow::anyhow!("invalid mate alignment position"))?;

        let mapq = MappingQuality::new(self.mapq).ok_or_else(|| {
            anyhow::anyhow!(
                "invalid mapping quality {}: value 255 is reserved for unavailable",
                self.mapq
            )
        })?;
        // Use a saturating cast; fragment_length > i32::MAX is possible for long reads.
        let template_len = i32::try_from(pair.fragment_length).unwrap_or(i32::MAX);

        // Flags for R1: paired, properly segmented, mate reverse, first segment.
        let flags_r1 = Flags::SEGMENTED
            | Flags::PROPERLY_SEGMENTED
            | Flags::MATE_REVERSE_COMPLEMENTED
            | Flags::FIRST_SEGMENT;

        // Flags for R2: paired, properly segmented, reverse complemented, last segment.
        let flags_r2 = Flags::SEGMENTED
            | Flags::PROPERLY_SEGMENTED
            | Flags::REVERSE_COMPLEMENTED
            | Flags::LAST_SEGMENT;

        // Build CIGAR ops, prepending a soft-clip for any inline UMI prefix.
        let cigar_ops_r1 = {
            let mut ops = parse_cigar(cigar_r1)
                .with_context(|| format!("failed to parse CIGAR: {cigar_r1}"))?;
            if let Some(ref pfx) = pair.inline_prefix_r1 {
                ops.insert(0, Op::new(Kind::SoftClip, pfx.len()));
            }
            ops
        };
        let cigar_ops_r2 = {
            let mut ops = parse_cigar(cigar_r2)
                .with_context(|| format!("failed to parse CIGAR: {cigar_r2}"))?;
            if let Some(ref pfx) = pair.inline_prefix_r2 {
                // R2 is stored reverse-complemented in BAM, so the UMI prefix
                // (which was at the start of the original read) maps to the END
                // of the CIGAR after reversal.
                ops.push(Op::new(Kind::SoftClip, pfx.len()));
            }
            ops
        };

        // R2 sequence must be reverse-complemented: BAM stores the sequence as it
        // appears on the forward strand, but R2 aligns to the reverse strand.
        let r2_seq_rc = crate::seq_utils::reverse_complement(&pair.read2.seq);
        let mut r2_qual_rev = pair.read2.qual.clone();
        r2_qual_rev.reverse();

        // Build final sequences: prepend any inline UMI prefix to the template sequence.
        let r1_seq_final: Vec<u8>;
        let r1_qual_final: Vec<u8>;
        let r2_seq_final: Vec<u8>;
        let r2_qual_final: Vec<u8>;

        if let Some(ref pfx) = pair.inline_prefix_r1 {
            let pfx_qual = vec![37u8; pfx.len()];
            r1_seq_final = pfx.iter().chain(pair.read1.seq.iter()).copied().collect();
            r1_qual_final = pfx_qual
                .iter()
                .chain(pair.read1.qual.iter())
                .copied()
                .collect();
        } else {
            r1_seq_final = pair.read1.seq.clone();
            r1_qual_final = pair.read1.qual.clone();
        }

        if let Some(ref pfx) = pair.inline_prefix_r2 {
            let pfx_qual = vec![37u8; pfx.len()];
            // R2 is stored RC in BAM. Prepend prefix in forward orientation, then
            // the whole sequence (prefix + template) is reversed for storage.
            let seq_fwd: Vec<u8> = pfx.iter().chain(pair.read2.seq.iter()).copied().collect();
            let qual_fwd: Vec<u8> = pfx_qual
                .iter()
                .chain(pair.read2.qual.iter())
                .copied()
                .collect();
            r2_seq_final = crate::seq_utils::reverse_complement(&seq_fwd);
            r2_qual_final = qual_fwd.into_iter().rev().collect();
        } else {
            r2_seq_final = r2_seq_rc;
            r2_qual_final = r2_qual_rev;
        }

        // Compute MD strings. R1 compares forward read against forward reference.
        // R2 is stored as RC, so the reference must also be RC for comparison.
        // MD is computed from the template bases only (no UMI prefix in the reference).
        let md_r1 = build_md_string(&pair.read1.seq, &pair.ref_seq_r1);
        let ref_r2_rc = crate::seq_utils::reverse_complement(&pair.ref_seq_r2);
        let r2_seq_template_rc = crate::seq_utils::reverse_complement(&pair.read2.seq);
        let md_r2 = build_md_string(&r2_seq_template_rc, &ref_r2_rc);

        // Insert MD tags into the auxiliary data for each read.
        let mut data_r1 = data_r1;
        data_r1.insert(Tag::MISMATCHED_POSITIONS, Value::from(md_r1.as_str()));

        let mut data_r2 = data_r2;
        data_r2.insert(Tag::MISMATCHED_POSITIONS, Value::from(md_r2.as_str()));

        let r1 = build_record(
            &pair.name,
            flags_r1,
            ref_id,
            pos1,
            pos2,
            mapq,
            cigar_ops_r1,
            template_len,
            &r1_seq_final,
            r1_qual_final,
            data_r1,
        );

        let r2 = build_record(
            &pair.name,
            flags_r2,
            ref_id,
            pos2,
            pos1,
            mapq,
            cigar_ops_r2,
            -template_len,
            &r2_seq_final,
            r2_qual_final,
            data_r2,
        );

        self.emit(&r1)?;
        self.emit(&r2)?;

        Ok(())
    }

    /// Finalize and flush the BAM file.
    /// Finalise and flush the BAM file, then write a `.bai` index alongside it.
    ///
    /// The BAM must be coordinate-sorted (the header is written with `SO:coordinate`)
    /// so that the index is valid. After the bgzip stream is closed, the file is
    /// re-opened, scanned sequentially, and a BAI index is written to `<path>.bai`.
    pub fn finish(mut self) -> Result<()> {
        self.writer
            .try_finish()
            .context("failed to finalize BAM file")?;

        build_bai_index(&self.path, &self.header)
            .with_context(|| format!("failed to write BAI index for {}", self.path.display()))?;

        Ok(())
    }

    /// Return a reference to the SAM header.
    // Called only in tests; not yet needed by production callers.
    #[cfg(test)]
    pub fn header(&self) -> &sam::Header {
        &self.header
    }

    /// Write a single alignment record to the BAM stream.
    fn emit(&mut self, record: &RecordBuf) -> Result<()> {
        use noodles_sam::alignment::io::Write as _;
        // Split borrows: &mut self.writer and &self.header are disjoint.
        let writer = &mut self.writer;
        let header = &self.header;
        writer
            .write_alignment_record(header, record)
            .context("failed to write BAM record")
    }
}

/// Build and write a BAI index for the BAM file at `bam_path`.
///
/// Opens the BAM, reads all records in order, and uses
/// [`noodles_csi::binning_index::Indexer`] to build the index. Writes the
/// result to `<bam_path>.bai`.
///
/// The BAM must be coordinate-sorted. Records that lack an alignment position
/// or reference sequence are counted as unplaced unmapped.
fn build_bai_index(bam_path: &Path, header: &sam::Header) -> Result<()> {
    use noodles_sam::alignment::Record as _;

    let bai_path = bam_path.with_extension("bam.bai");

    let mut reader = File::open(bam_path)
        .map(bam::io::Reader::new)
        .with_context(|| format!("failed to open BAM for indexing: {}", bam_path.display()))?;

    // Read (and discard) the header. We already have it, but the reader must
    // advance past it before we can read records.
    reader
        .read_header()
        .context("failed to read BAM header during indexing")?;

    let mut record = bam::Record::default();
    let mut builder = CsiIndexer::default();
    let mut start_position = reader.get_ref().virtual_position();

    loop {
        let bytes_read = reader
            .read_record(&mut record)
            .context("failed to read BAM record during indexing")?;
        if bytes_read == 0 {
            break;
        }

        let end_position = reader.get_ref().virtual_position();
        let chunk = Chunk::new(start_position, end_position);

        let alignment_context = match (
            record.reference_sequence_id().transpose()?,
            record.alignment_start().transpose()?,
            record.alignment_end().transpose()?,
        ) {
            (Some(id), Some(start), Some(end)) => {
                let is_mapped = !record.flags().is_unmapped();
                Some((id, start, end, is_mapped))
            }
            _ => None,
        };

        builder
            .add_record(alignment_context, chunk)
            .context("failed to add record to BAI indexer")?;

        start_position = end_position;
    }

    let index = builder.build(header.reference_sequences().len());

    bam::bai::fs::write(&bai_path, &index)
        .with_context(|| format!("failed to write BAI index: {}", bai_path.display()))?;

    Ok(())
}

/// Build the MD tag string for a single read.
///
/// The MD string encodes positions where the read differs from the reference.
/// Matching bases are counted and emitted as integers. Each mismatch emits the
/// current match count (even if zero) followed by the reference base at that
/// position, then resets the count.
///
/// If `ref_seq` is empty, the function returns the read length as a decimal
/// string, indicating all bases match (no reference available).
///
/// # Examples
///
/// - All 150 bp match: `"150"`
/// - Mismatch at position 50 (ref base A): `"50A99"`
/// - Mismatch at position 0 (ref base T): `"0T149"`
pub fn build_md_string(read_seq: &[u8], ref_seq: &[u8]) -> String {
    if ref_seq.is_empty() {
        return read_seq.len().to_string();
    }

    let mut result = String::new();
    let mut match_count: usize = 0;

    for (&read_base, &ref_base) in read_seq.iter().zip(ref_seq.iter()) {
        // Compare uppercase to handle any lowercase reference bases.
        if read_base.eq_ignore_ascii_case(&ref_base) {
            match_count += 1;
        } else {
            // Emit accumulated match count, then the mismatched reference base.
            result.push_str(&match_count.to_string());
            result.push(ref_base.to_ascii_uppercase() as char);
            match_count = 0;
        }
    }

    // Emit any trailing matches.
    result.push_str(&match_count.to_string());
    result
}

/// Compute the NM (edit distance) value for a single read.
///
/// NM is defined per the SAM specification as the number of mismatches plus
/// the total length of all insertions and deletions relative to the reference.
///
/// The function walks the CIGAR operations, consuming bases from `read_seq`
/// and `ref_seq` as each operation dictates:
///
/// - Match (`M`): compare bases; count mismatches.
/// - Insertion (`I`): consume read bases; add the insertion length to NM.
/// - Deletion (`D`): consume reference bases; add the deletion length to NM.
/// - Soft clip (`S`): consume read bases; no contribution to NM.
/// - Hard clip (`H`), skip (`N`), pad (`P`): no bases consumed from either.
///
/// If `ref_seq` is empty (no reference available), NM falls back to zero.
///
/// # Arguments
///
/// - `read_seq`: the read sequence as written in the BAM record (forward strand
///   for R1; reverse-complemented for R2).
/// - `ref_seq`: the corresponding reference slice in the same orientation.
/// - `cigar_str`: the CIGAR string for this read.
pub fn compute_nm(read_seq: &[u8], ref_seq: &[u8], cigar_str: &str) -> usize {
    if ref_seq.is_empty() {
        return 0;
    }

    let ops = match parse_cigar(cigar_str) {
        Ok(ops) => ops,
        Err(_) => return 0,
    };

    let mut nm: usize = 0;
    let mut read_pos: usize = 0;
    let mut ref_pos: usize = 0;

    for op in &ops {
        let len = op.len();
        match op.kind() {
            Kind::Match | Kind::SequenceMatch | Kind::SequenceMismatch => {
                // Compare bases one by one and count mismatches.
                for i in 0..len {
                    let rp = read_pos + i;
                    let fp = ref_pos + i;
                    if rp < read_seq.len()
                        && fp < ref_seq.len()
                        && !read_seq[rp].eq_ignore_ascii_case(&ref_seq[fp])
                    {
                        nm += 1;
                    }
                }
                read_pos += len;
                ref_pos += len;
            }
            Kind::Insertion => {
                nm += len;
                read_pos += len;
            }
            Kind::Deletion => {
                nm += len;
                ref_pos += len;
            }
            // N (Skip) advances reference position but does not count as an edit.
            Kind::Skip => {
                ref_pos += len;
            }
            Kind::SoftClip => {
                read_pos += len;
            }
            // Hard clip and pad do not consume bases from either sequence.
            Kind::HardClip | Kind::Pad => {}
        }
    }

    nm
}

/// Parse a CIGAR string like `"150M"` or `"5M2I143M"` into a list of [`Op`]s.
pub fn parse_cigar(cigar_str: &str) -> Result<Vec<Op>> {
    let mut ops = Vec::new();
    let mut num_buf = String::new();

    for ch in cigar_str.chars() {
        if ch.is_ascii_digit() {
            num_buf.push(ch);
        } else {
            let len: usize = num_buf
                .parse()
                .with_context(|| format!("invalid CIGAR length before '{ch}'"))?;
            num_buf.clear();
            let kind = match ch {
                'M' => Kind::Match,
                'I' => Kind::Insertion,
                'D' => Kind::Deletion,
                'N' => Kind::Skip,
                'S' => Kind::SoftClip,
                'H' => Kind::HardClip,
                'P' => Kind::Pad,
                '=' => Kind::SequenceMatch,
                'X' => Kind::SequenceMismatch,
                other => anyhow::bail!("unknown CIGAR operation: '{other}'"),
            };
            ops.push(Op::new(kind, len));
        }
    }

    if !num_buf.is_empty() {
        anyhow::bail!("trailing digits in CIGAR string with no operation character");
    }

    Ok(ops)
}

/// Build a single SAM/BAM alignment record.
///
/// Centralises all record construction so that `write_pair_inner` does not
/// repeat the builder chain twice. The caller is responsible for choosing the
/// correct flags, positions, CIGAR ops, and data fields for each read in the
/// pair.
///
/// - `name`: read name (QNAME)
/// - `flags`: SAM flags for this read
/// - `ref_id`: 0-based reference sequence index
/// - `align_start`: 1-based alignment start position (noodles convention)
/// - `mate_start`: 1-based mate alignment start position
/// - `mapq`: mapping quality
/// - `cigar_ops`: parsed CIGAR operations
/// - `template_len`: signed template length (negative for R2)
/// - `seq`: read sequence as raw bytes
/// - `qual`: base quality scores
/// - `data`: auxiliary data fields (RG, RX, MI, …)
#[allow(clippy::too_many_arguments)]
fn build_record(
    name: &str,
    flags: Flags,
    ref_id: usize,
    align_start: Position,
    mate_start: Position,
    mapq: MappingQuality,
    cigar_ops: Vec<Op>,
    template_len: i32,
    seq: &[u8],
    qual: Vec<u8>,
    data: Data,
) -> RecordBuf {
    RecordBuf::builder()
        .set_name(name)
        .set_flags(flags)
        .set_reference_sequence_id(ref_id)
        .set_alignment_start(align_start)
        .set_mapping_quality(mapq)
        .set_cigar(cigar_ops.into_iter().collect::<Cigar>())
        .set_mate_reference_sequence_id(ref_id)
        .set_mate_alignment_start(mate_start)
        .set_template_length(template_len)
        .set_sequence(Sequence::from(seq))
        .set_quality_scores(QualityScores::from(qual))
        .set_data(data)
        .build()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::types::{Read, ReadPair};
    use crate::io::config::SampleConfig;
    use tempfile::NamedTempFile;

    fn make_sample_config(name: &str) -> SampleConfig {
        SampleConfig {
            name: name.to_string(),
            read_length: 150,
            coverage: 30.0,
            platform: Some("ILLUMINA".to_string()),
        }
    }

    fn make_sample_config_with_platform(name: &str, platform: &str) -> SampleConfig {
        SampleConfig {
            name: name.to_string(),
            read_length: 150,
            coverage: 30.0,
            platform: Some(platform.to_string()),
        }
    }

    fn make_read_pair(name: &str, len: usize) -> ReadPair {
        let seq = vec![b'A'; len];
        let qual = vec![30u8; len];
        ReadPair {
            name: name.to_string(),
            read1: Read::new(seq.clone(), qual.clone()),
            read2: Read::new(seq, qual),
            fragment_start: 100,
            fragment_length: len + 50,
            chrom: "chr1".to_string(),
            variant_tags: Vec::new(),
            ref_seq_r1: Vec::new(),
            ref_seq_r2: Vec::new(),
            inline_prefix_r1: None,
            inline_prefix_r2: None,
        }
    }

    fn ref_seqs() -> Vec<(String, u64)> {
        vec![
            ("chr1".to_string(), 248_956_422),
            ("chr2".to_string(), 242_193_529),
        ]
    }

    fn read_back_records(path: &Path, header: &sam::Header) -> Vec<RecordBuf> {
        let mut reader = bam::io::Reader::new(std::fs::File::open(path).unwrap());
        let _hdr = reader.read_header().unwrap();
        let mut records = Vec::new();
        for result in reader.record_bufs(header) {
            records.push(result.unwrap());
        }
        records
    }

    #[test]
    fn test_write_header() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        let writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        // @HD present
        assert!(header.header().is_some());

        // @SQ lines
        assert_eq!(header.reference_sequences().len(), 2);
        assert!(header.reference_sequences().contains_key(&b"chr1"[..]));
        assert!(header.reference_sequences().contains_key(&b"chr2"[..]));

        // @RG line
        assert_eq!(header.read_groups().len(), 1);
        assert!(header.read_groups().contains_key(&b"SAMPLE"[..]));
    }

    #[test]
    fn test_platform_normalised_to_uppercase() {
        // Passing "illumina" (lowercase) must produce "ILLUMINA" in the PL tag.
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config_with_platform("SAMPLE", "illumina");

        let writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let rg = header
            .read_groups()
            .get(&b"SAMPLE"[..])
            .expect("RG not found");

        let pl_tag =
            noodles_sam::header::record::value::map::tag::Other::try_from([b'P', b'L']).unwrap();
        let pl_value = rg
            .other_fields()
            .get(&pl_tag)
            .expect("PL field missing from @RG");
        let pl_bytes: &[u8] = pl_value.as_ref();
        assert_eq!(pl_bytes, b"ILLUMINA", "PL tag must be uppercase");
    }

    #[test]
    fn test_write_single_pair() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        let pair = make_read_pair("read1", 150);

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer.write_pair(&pair, 0, 1000, "150M", "150M").unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        assert_eq!(records.len(), 2, "expected two records (R1 and R2)");

        let r1 = &records[0];
        assert_eq!(r1.name().unwrap(), b"read1".as_ref());
        assert_eq!(r1.reference_sequence_id(), Some(0));
        // Position is 1-based in noodles: we wrote pos=1000 (0-based) => 1001
        assert_eq!(usize::from(r1.alignment_start().unwrap()), 1001);
        assert!(!r1.sequence().is_empty());
    }

    #[test]
    fn test_mate_flags() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        let pair = make_read_pair("flagtest", 100);

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer.write_pair(&pair, 0, 500, "100M", "100M").unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        let r1 = &records[0];
        let r2 = &records[1];

        // R1 flags
        assert!(r1.flags().is_segmented(), "R1 should be paired");
        assert!(
            r1.flags().is_properly_segmented(),
            "R1 should be proper pair"
        );
        assert!(
            r1.flags().is_mate_reverse_complemented(),
            "R1 mate should be reverse"
        );
        assert!(r1.flags().is_first_segment(), "R1 should be first in pair");
        assert!(
            !r1.flags().is_last_segment(),
            "R1 should not be last in pair"
        );

        // R2 flags
        assert!(r2.flags().is_segmented(), "R2 should be paired");
        assert!(
            r2.flags().is_properly_segmented(),
            "R2 should be proper pair"
        );
        assert!(
            r2.flags().is_reverse_complemented(),
            "R2 should be reverse complemented"
        );
        assert!(r2.flags().is_last_segment(), "R2 should be last in pair");
        assert!(
            !r2.flags().is_first_segment(),
            "R2 should not be first in pair"
        );
    }

    #[test]
    fn test_umi_tags() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        let pair = make_read_pair("umipair", 100);

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer
            .write_pair_with_umi(&pair, 0, 500, "100M", "100M", "ACGTACGT", 42)
            .unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        assert_eq!(records.len(), 2);

        for record in &records {
            let data = record.data();

            // RX tag (UMI sequence)
            let rx = data.get(&Tag::UMI_SEQUENCE).expect("RX tag missing");
            if let Value::String(s) = rx {
                let s_bytes: &[u8] = s.as_ref();
                assert_eq!(s_bytes, b"ACGTACGT");
            } else {
                panic!("RX tag should be a string");
            }

            // MI tag (family ID)
            let mi = data.get(&Tag::UMI_ID).expect("MI tag missing");
            assert_eq!(mi.as_int(), Some(42));
        }
    }

    #[test]
    fn test_cigar_preserved() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        // Use a more complex CIGAR: 5M2I143M
        let pair = make_read_pair("cigartest", 150);

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer
            .write_pair(&pair, 0, 200, "5M2I143M", "150M")
            .unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        let r1 = &records[0];

        let ops: Vec<Op> = r1.cigar().as_ref().to_vec();

        assert_eq!(ops.len(), 3);
        assert_eq!(ops[0].kind(), Kind::Match);
        assert_eq!(ops[0].len(), 5);
        assert_eq!(ops[1].kind(), Kind::Insertion);
        assert_eq!(ops[1].len(), 2);
        assert_eq!(ops[2].kind(), Kind::Match);
        assert_eq!(ops[2].len(), 143);
    }

    // --- MD tag unit tests ---

    #[test]
    fn test_md_all_match() {
        // 50 bp read where every base matches the reference.
        let seq = vec![b'A'; 50];
        let ref_seq = vec![b'A'; 50];
        assert_eq!(build_md_string(&seq, &ref_seq), "50");
    }

    #[test]
    fn test_md_single_snv() {
        // Read is all A's. Reference has T at position 5 (0-based).
        // MD should be: 5 matches, ref base T, 5 more matches → "5T5".
        let read = b"AAAAAAAAAAA";
        let mut ref_seq = b"AAAAAAAAAAA".to_vec();
        ref_seq[5] = b'T';
        assert_eq!(build_md_string(read, &ref_seq), "5T5");
    }

    #[test]
    fn test_md_empty_ref() {
        // Empty reference: return read length as all-match string.
        let read = vec![b'A'; 150];
        assert_eq!(build_md_string(&read, &[]), "150");
    }

    #[test]
    fn test_md_mismatch_at_start() {
        // Mismatch at position 0: "0" + ref_base + remaining_matches.
        let read = b"ACGT";
        let ref_seq = b"TCGT";
        assert_eq!(build_md_string(read, ref_seq), "0T3");
    }

    #[test]
    fn test_md_mismatch_at_end() {
        // Mismatch at last position.
        let read = b"ACGT";
        let ref_seq = b"ACGA";
        assert_eq!(build_md_string(read, ref_seq), "3A0");
    }

    #[test]
    fn test_md_multiple_mismatches() {
        // Mismatches at positions 1 and 3.
        // read:  A C G T
        // ref:   A T G A
        // MD:    1T1A0
        let read = b"ACGT";
        let ref_seq = b"ATGA";
        assert_eq!(build_md_string(read, ref_seq), "1T1A0");
    }

    // --- NM tag unit tests ---

    #[test]
    fn test_nm_all_match() {
        // 10 bp, all bases identical: NM = 0.
        let read = vec![b'A'; 10];
        let ref_seq = vec![b'A'; 10];
        assert_eq!(compute_nm(&read, &ref_seq, "10M"), 0);
    }

    #[test]
    fn test_nm_single_snv() {
        // One mismatch at position 5: NM = 1.
        let mut read = vec![b'A'; 10];
        read[5] = b'C';
        let ref_seq = vec![b'A'; 10];
        assert_eq!(compute_nm(&read, &ref_seq, "10M"), 1);
    }

    #[test]
    fn test_nm_insertion() {
        // 2 bp insertion in the read: NM = 2.
        // read: 5 matches + 2 inserted + 3 matches = 10 bp read, 8 bp ref.
        let read = vec![b'A'; 10];
        let ref_seq = vec![b'A'; 8];
        assert_eq!(compute_nm(&read, &ref_seq, "5M2I3M"), 2);
    }

    #[test]
    fn test_nm_deletion() {
        // 2 bp deletion from reference: NM = 2.
        // read: 5 matches + 3 matches = 8 bp read, 10 bp ref.
        let read = vec![b'A'; 8];
        let ref_seq = vec![b'A'; 10];
        assert_eq!(compute_nm(&read, &ref_seq, "5M2D3M"), 2);
    }

    #[test]
    fn test_nm_snv_and_indel() {
        // 1 mismatch + 2 bp deletion: NM = 3.
        // read:  ACCCAAA  (7 bp: pos 0 is mismatch, then 6 match)
        // ref:   AACCAAAAA (9 bp: pos 0 matches A, pos 1 matches A, 2-bp del, then 5 match)
        // CIGAR: 2M2D5M
        let mut read = vec![b'A'; 7];
        read[1] = b'C'; // mismatch at read pos 1 vs ref pos 1 (ref=A)
        let ref_seq = vec![b'A'; 9];
        assert_eq!(compute_nm(&read, &ref_seq, "2M2D5M"), 3);
    }

    #[test]
    fn test_nm_empty_ref() {
        // No reference available: NM = 0.
        let read = vec![b'A'; 10];
        assert_eq!(compute_nm(&read, &[], "10M"), 0);
    }

    #[test]
    fn test_nm_soft_clip_ignored() {
        // Soft clip does not contribute to NM.
        // CIGAR: 2S8M — only the 8M region is compared.
        let read = vec![b'A'; 10];
        let ref_seq = vec![b'A'; 8];
        assert_eq!(compute_nm(&read, &ref_seq, "2S8M"), 0);
    }

    #[test]
    fn test_nm_written_to_bam() {
        // End-to-end: write a pair with one SNV, read back NM tag.
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        // Build a read pair where read1 has one mismatch vs the reference.
        let mut seq1 = vec![b'A'; 100];
        seq1[10] = b'C'; // mismatch at position 10
        let ref1 = vec![b'A'; 100]; // reference is all A's

        let seq2 = vec![b'A'; 100];
        let ref2 = vec![b'A'; 100];

        let pair = ReadPair {
            name: "nmtest".to_string(),
            read1: Read::new(seq1, vec![30u8; 100]),
            read2: Read::new(seq2, vec![30u8; 100]),
            fragment_start: 0,
            fragment_length: 250,
            chrom: "chr1".to_string(),
            variant_tags: Vec::new(),
            ref_seq_r1: ref1,
            ref_seq_r2: ref2,
            inline_prefix_r1: None,
            inline_prefix_r2: None,
        };

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer.write_pair(&pair, 0, 0, "100M", "100M").unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        assert_eq!(records.len(), 2);

        // R1 has one mismatch: NM should be 1.
        let nm_r1 = records[0]
            .data()
            .get(&Tag::EDIT_DISTANCE)
            .expect("NM tag missing on R1");
        assert_eq!(nm_r1.as_int(), Some(1), "R1 NM should be 1");

        // R2 has no mismatches: NM should be 0.
        let nm_r2 = records[1]
            .data()
            .get(&Tag::EDIT_DISTANCE)
            .expect("NM tag missing on R2");
        assert_eq!(nm_r2.as_int(), Some(0), "R2 NM should be 0");
    }

    #[test]
    fn test_read_group() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("MY_SAMPLE");

        let pair = make_read_pair("rgtest", 100);

        let mut writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        writer.write_pair(&pair, 0, 0, "100M", "100M").unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        let records = read_back_records(tmp.path(), &header);
        assert_eq!(records.len(), 2);

        for record in &records {
            let data = record.data();
            let rg = data.get(&Tag::READ_GROUP).expect("RG tag missing");
            if let Value::String(s) = rg {
                let s_bytes: &[u8] = s.as_ref();
                assert_eq!(s_bytes, b"MY_SAMPLE");
            } else {
                panic!("RG tag should be a string, got {:?}", rg);
            }
        }

        // Also verify RG is in the header
        assert!(header.read_groups().contains_key(&b"MY_SAMPLE"[..]));
    }

    #[test]
    fn test_pg_record_in_header() {
        let tmp = NamedTempFile::new().unwrap();
        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");

        let writer = BamWriter::new(tmp.path(), &refs, &cfg, 60).unwrap();
        let header = writer.header().clone();
        writer.finish().unwrap();

        // @PG line with ID:varforge must be present.
        assert!(
            header.programs().as_ref().contains_key(&b"varforge"[..]),
            "@PG ID:varforge not found in header"
        );

        let pg = &header.programs().as_ref()[&b"varforge"[..]];

        // PN field
        let pn = pg
            .other_fields()
            .get(&pg_tag::NAME)
            .expect("PN field missing from @PG");
        assert_eq!(pn, "varforge");

        // VN field must be a non-empty string.
        let vn = pg
            .other_fields()
            .get(&pg_tag::VERSION)
            .expect("VN field missing from @PG");
        assert!(!vn.is_empty(), "VN field should not be empty");
    }

    #[test]
    fn test_bai_index_created() {
        // After finish(), a .bai index file must exist alongside the BAM.
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let bam_path = dir.path().join("test.bam");
        let bai_path = dir.path().join("test.bam.bai");

        let refs = ref_seqs();
        let cfg = make_sample_config("SAMPLE");
        let pair = make_read_pair("idx_test", 100);

        let mut writer = BamWriter::new(&bam_path, &refs, &cfg, 60).unwrap();
        writer.write_pair(&pair, 0, 1000, "100M", "100M").unwrap();
        writer.finish().unwrap();

        assert!(
            bai_path.exists(),
            ".bai index was not created at {:?}",
            bai_path
        );
        assert!(
            bai_path.metadata().unwrap().len() > 0,
            ".bai index file is empty"
        );
    }
}