rasusa 5.1.0

Randomly subsample reads or alignments
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
use crate::alignment::{is_query_grouped, program_entry};
use crate::fastx::{Fastx, FastxError, OnePassStats};
use crate::format::OutputEncoding;
use crate::subsampler::seeded_rng;
use crate::threading::build_alignment_reader;
use anyhow::Result;
use noodles::sam::alignment::record::Flags;
use noodles::sam::alignment::Record;
use rand::prelude::*;
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};

pub trait RecordSource {
    fn read_lengths(&self) -> Result<Vec<u32>, FastxError>;
    /// Returns the number of reads (or read templates, for paired alignment data) in the source,
    /// without materializing their individual lengths.
    fn count(&self) -> Result<usize, FastxError>;
    fn filter_reads_into(
        &self,
        reads_to_keep: &[bool],
        nb_reads_keep: usize,
        write_to: &mut dyn Write,
        encoding: OutputEncoding,
    ) -> Result<usize, FastxError>;
}

pub struct AlignmentSource {
    path: PathBuf,
    threads: NonZeroUsize,
}

impl AlignmentSource {
    pub fn new(path: &Path, threads: NonZeroUsize) -> Self {
        Self {
            path: path.to_path_buf(),
            threads,
        }
    }
}

/// Extracts a record's name as an owned byte vector (empty if unset), for storage across
/// iterations. Shared by every place in this module that groups alignment records by name
/// ([`TemplateIndexer`], [`check_name_grouped`], [`TemplateGrouper`]).
fn record_name_bytes(record: &dyn Record) -> Vec<u8> {
    record
        .name()
        .map(|n| (n.as_ref() as &[u8]).to_vec())
        .unwrap_or_default()
}

/// How many records the name-grouped guard scans, when the header doesn't already declare
/// grouping/sorting, before giving up and accepting the input. This is a documented limitation
/// (see the `one_pass` field doc on [`crate::reads::Reads`]), not a guarantee: input that breaks
/// grouping only after this many records is not caught.
const NAME_GROUP_SCAN_LIMIT: usize = 50;

/// Guards one-pass SAM/BAM/CRAM streaming against silently splitting a template's records apart.
///
/// One-pass groups a template by comparing each record only to the one immediately before it -
/// correct for name-grouped input (which unaligned data off a sequencer, and collated output,
/// both are), but silently wrong on anything else. This is accepted outright when the header
/// declares grouping or name-sorting ([`is_query_grouped`]); otherwise the first
/// [`NAME_GROUP_SCAN_LIMIT`] records are scanned for positive evidence of grouping: a run of
/// consecutive records sharing a name, with no name reappearing after a different one. Input
/// with no segmented records needs no grouping and is accepted without inspecting names at all.
///
/// Only names and flags are read here - never a whole record - so peak memory doesn't grow with
/// read length or input size for SAM/BAM, whose readers decode a record's fields lazily on
/// demand. CRAM has no such lazy representation in the reader this uses (its block-based
/// compression means decoding any field forces the whole record, and often the rest of its
/// containing slice, to be decoded) - the scan is still correct for CRAM, just not
/// constant-memory in the same way.
///
/// The reader used for the scan is discarded afterwards; the caller re-opens the file for the
/// actual one-pass streaming read.
fn check_name_grouped(path: &Path, threads: NonZeroUsize) -> Result<(), FastxError> {
    let mut reader = build_alignment_reader(path, threads)
        .map_err(|source| FastxError::AlignmentReadError { source })?;
    let header = reader
        .read_header()
        .map_err(|source| FastxError::AlignmentReadError { source })?;

    if is_query_grouped(&header) {
        return Ok(());
    }

    let mut last_name: Option<Vec<u8>> = None;
    let mut closed_names: HashSet<Vec<u8>> = HashSet::new();

    for result in reader.records(&header).take(NAME_GROUP_SCAN_LIMIT) {
        let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
        let flags = record
            .flags()
            .unwrap_or(noodles::sam::alignment::record::Flags::empty());

        if !flags.is_segmented() {
            continue;
        }

        let name = record_name_bytes(&*record);

        if let Some(last) = &last_name {
            if last != &name {
                closed_names.insert(last.clone());
            }
        }
        if closed_names.contains(&name) {
            return Err(FastxError::UngroupedAlignmentInput);
        }
        last_name = Some(name);
    }

    Ok(())
}

impl AlignmentSource {
    /// Runs the name-grouped guard (see [`check_name_grouped`]) against a throwaway reader.
    ///
    /// Callers driving output through a file (rather than an in-memory sink) should run this
    /// *before* opening/truncating that file, so a rejection doesn't leave a truncated output
    /// file behind; [`Reads::run_one_pass`](crate::reads::Reads) does so.
    pub fn check_name_grouped(&self) -> Result<(), FastxError> {
        check_name_grouped(&self.path, self.threads)
    }

    /// Streams a single-pass, probabilistic fraction subsample of unaligned SAM/BAM/CRAM input:
    /// reads the input exactly once, keeping each template (a segmented read's records, grouped
    /// by comparing each record only to the one immediately before it - see
    /// [`check_name_grouped`]) independently with probability `fraction`, writing kept records
    /// immediately. Unsegmented records are each their own template.
    ///
    /// Does not itself run [`check_name_grouped`] - callers that haven't already run
    /// [`AlignmentSource::check_name_grouped`] (e.g. tests) get an unguarded stream.
    pub fn subsample_one_pass(
        &self,
        fraction: f32,
        seed: Option<u64>,
        write_to: &mut dyn Write,
        encoding: OutputEncoding,
    ) -> Result<OnePassStats, FastxError> {
        let mut reader = build_alignment_reader(&self.path, self.threads)
            .map_err(|source| FastxError::AlignmentReadError { source })?;
        let mut header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut rng = seeded_rng(seed);
        let mut stats = OnePassStats::default();
        let mut grouper = TemplateGrouper::default();

        if let OutputEncoding::Alignment(format) = encoding {
            let (pg_id, pg_map) = program_entry(&header);
            header.programs_mut().as_mut().insert(pg_id.into(), pg_map);

            {
                let mut writer = noodles_util::alignment::io::writer::Builder::default()
                    .set_format(format)
                    .build_from_writer(&mut *write_to)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;
                writer
                    .write_header(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;

                for result in reader.records(&header) {
                    let record =
                        result.map_err(|source| FastxError::AlignmentReadError { source })?;
                    let (is_new_template, decision) =
                        grouper.decide(&*record, &mut rng, fraction)?;
                    if is_new_template {
                        stats.reads_seen += 1;
                        if decision {
                            stats.reads_kept += 1;
                        }
                    }
                    if decision {
                        writer
                            .write_record(&header, &record)
                            .map_err(|source| FastxError::AlignmentReadError { source })?;
                    }
                }
                writer
                    .finish(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;
            }
            write_to.flush().map_err(|source| FastxError::WriteError {
                source: anyhow::Error::from(source),
            })?;
        } else {
            let is_fasta = matches!(encoding, OutputEncoding::Fastx { fasta: true });

            for result in reader.records(&header) {
                let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
                let (is_new_template, decision) = grouper.decide(&*record, &mut rng, fraction)?;
                if is_new_template {
                    stats.reads_seen += 1;
                    if decision {
                        stats.reads_kept += 1;
                    }
                }
                if decision {
                    let name = record.name().map(|n| n.as_ref()).unwrap_or(&b"*"[..]);
                    let seq: Vec<u8> = record.sequence().iter().collect();
                    let qual: Vec<u8> = record
                        .quality_scores()
                        .iter()
                        .map(|q| q.map(|score| score + 33))
                        .collect::<Result<Vec<u8>, _>>()
                        .map_err(|source| FastxError::AlignmentReadError { source })?;
                    let qual = if qual.is_empty() {
                        None
                    } else {
                        Some(&qual[..])
                    };
                    crate::record::write_fastx_record(write_to, name, &seq, qual, is_fasta, b"\n")?;
                }
            }
        }

        Ok(stats)
    }
}

/// Groups consecutive alignment records into templates by comparing each record only to the one
/// immediately before it - the same constant-memory grouping [`check_name_grouped`] guards - and
/// carries one keep/drop decision per template forward across the records that share it.
#[derive(Default)]
struct TemplateGrouper {
    current_name: Option<Vec<u8>>,
    current_decision: bool,
}

impl TemplateGrouper {
    /// Decides whether `record` belongs to a new template (relative to the previous call's
    /// record) and, if so, draws a fresh keep/drop decision; otherwise reuses the previous
    /// decision so every record of a template is kept or dropped together. Also rejects mapped
    /// reads, matching existing (two-pass) behaviour.
    ///
    /// Returns `(is_new_template, decision)`.
    fn decide(
        &mut self,
        record: &dyn Record,
        rng: &mut impl Rng,
        fraction: f32,
    ) -> Result<(bool, bool), FastxError> {
        let flags = record
            .flags()
            .map_err(|source| FastxError::AlignmentReadError { source })?;
        if !flags.is_unmapped() {
            return Err(FastxError::MappedReadDetected);
        }

        let name = flags.is_segmented().then(|| record_name_bytes(record));

        let is_new_template = match (&name, self.current_name.as_ref()) {
            (Some(n), Some(cur)) => n != cur,
            _ => true,
        };

        if is_new_template {
            self.current_decision = rng.random_bool(fraction as f64);
        }
        self.current_name = name;

        Ok((is_new_template, self.current_decision))
    }
}

/// Assigns a stable per-template index to alignment records, coalescing segmented (paired)
/// records that share a QNAME onto the same index the way SAM template grouping expects.
/// Indices are handed out sequentially (`0, 1, 2, ...`) in first-seen order, so `next_idx` at any
/// point also equals the number of distinct templates seen so far.
///
/// Every alignment-format scan in this module (`read_lengths`, `count`, `filter_reads_into`)
/// needs exactly this grouping, so it's centralised here rather than reimplemented per scan.
#[derive(Default)]
struct TemplateIndexer {
    next_idx: usize,
    qname_to_idx: HashMap<Vec<u8>, usize>,
}

impl TemplateIndexer {
    fn index_for(&mut self, record: &dyn Record, flags: Flags) -> usize {
        if !flags.is_segmented() {
            let idx = self.next_idx;
            self.next_idx += 1;
            return idx;
        }

        let name = record_name_bytes(record);

        if let Some(&idx) = self.qname_to_idx.get(&name) {
            return idx;
        }
        let idx = self.next_idx;
        self.next_idx += 1;
        self.qname_to_idx.insert(name, idx);
        idx
    }
}

impl RecordSource for AlignmentSource {
    fn read_lengths(&self) -> Result<Vec<u32>, FastxError> {
        let mut reader = build_alignment_reader(&self.path, self.threads)
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut read_lengths: Vec<u32> = vec![];
        let mut indexer = TemplateIndexer::default();

        for result in reader.records(&header) {
            let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
            let flags = record
                .flags()
                .unwrap_or(noodles::sam::alignment::record::Flags::empty());

            if !flags.is_unmapped() {
                return Err(FastxError::MappedReadDetected);
            }

            let rlen = record.sequence().len() as u32;
            let idx = indexer.index_for(&*record, flags);
            if idx == read_lengths.len() {
                read_lengths.push(rlen);
            } else {
                read_lengths[idx] += rlen;
            }
        }

        Ok(read_lengths)
    }

    fn count(&self) -> Result<usize, FastxError> {
        let mut reader = build_alignment_reader(&self.path, self.threads)
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut indexer = TemplateIndexer::default();

        for result in reader.records(&header) {
            let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
            let flags = record
                .flags()
                .unwrap_or(noodles::sam::alignment::record::Flags::empty());

            if !flags.is_unmapped() {
                return Err(FastxError::MappedReadDetected);
            }

            indexer.index_for(&*record, flags);
        }

        // indices are assigned sequentially in first-seen order, so the number of templates seen
        // equals the count of indices handed out.
        Ok(indexer.next_idx)
    }

    fn filter_reads_into(
        &self,
        reads_to_keep: &[bool],
        nb_reads_keep: usize,
        write_to: &mut dyn Write,
        encoding: OutputEncoding,
    ) -> Result<usize, FastxError> {
        let mut reader = build_alignment_reader(&self.path, self.threads)
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut header = reader
            .read_header()
            .map_err(|source| FastxError::AlignmentReadError { source })?;

        let mut total_len = 0;
        let mut written_templates: HashSet<usize> = HashSet::new();
        let mut indexer = TemplateIndexer::default();

        // If the output format is an alignment format (SAM/BAM/CRAM), we write alignment records
        if let OutputEncoding::Alignment(format) = encoding {
            {
                // add rasusa program command line to header
                let (pg_id, pg_map) = program_entry(&header);
                header.programs_mut().as_mut().insert(pg_id.into(), pg_map);

                // `write_to` is a borrowed `&mut dyn Write`, not an owned `Send` sink, so it can't
                // be wrapped in the multithreaded BGZF encoder from `crate::threading` (that
                // needs to own the writer for its background compression threads). BAM output
                // written through `reads` is therefore always single-threaded; `--threads` only
                // speeds up BAM *reading* here.
                let mut writer = noodles_util::alignment::io::writer::Builder::default()
                    .set_format(format)
                    .build_from_writer(&mut *write_to)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;

                writer
                    .write_header(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;

                for result in reader.records(&header) {
                    let record =
                        result.map_err(|source| FastxError::AlignmentReadError { source })?;
                    let flags = record
                        .flags()
                        .unwrap_or(noodles::sam::alignment::record::Flags::empty());
                    let current_idx = indexer.index_for(&*record, flags);

                    if current_idx < reads_to_keep.len() && reads_to_keep[current_idx] {
                        total_len += record.sequence().len();
                        writer
                            .write_record(&header, &record)
                            .map_err(|source| FastxError::AlignmentReadError { source })?;
                        written_templates.insert(current_idx);
                    }
                }
                writer
                    .finish(&header)
                    .map_err(|source| FastxError::AlignmentReadError { source })?;
            }
            write_to.flush().map_err(|source| FastxError::WriteError {
                source: anyhow::Error::from(source),
            })?;
        } else {
            let is_fasta = matches!(encoding, OutputEncoding::Fastx { fasta: true });
            // Otherwise, we output as FASTQ (or FASTA)
            for result in reader.records(&header) {
                let record = result.map_err(|source| FastxError::AlignmentReadError { source })?;
                let flags = record
                    .flags()
                    .unwrap_or(noodles::sam::alignment::record::Flags::empty());
                let current_idx = indexer.index_for(&*record, flags);

                if current_idx < reads_to_keep.len() && reads_to_keep[current_idx] {
                    total_len += record.sequence().len();

                    let name = record.name().map(|n| n.as_ref()).unwrap_or(&b"*"[..]);
                    let seq: Vec<u8> = record.sequence().iter().collect();
                    let qual: Vec<u8> = record
                        .quality_scores()
                        .iter()
                        .map(|q| q.map(|score| score + 33))
                        .collect::<Result<Vec<u8>, _>>()
                        .map_err(|source| FastxError::AlignmentReadError { source })?;
                    let qual = if qual.is_empty() {
                        None
                    } else {
                        Some(&qual[..])
                    };

                    crate::record::write_fastx_record(write_to, name, &seq, qual, is_fasta, b"\n")?;

                    written_templates.insert(current_idx);
                }
            }
        }

        if written_templates.len() == nb_reads_keep {
            Ok(total_len)
        } else {
            Err(FastxError::IndicesNotFound)
        }
    }
}

pub fn determine_record_source(path: &Path, threads: NonZeroUsize) -> Box<dyn RecordSource> {
    match path.extension().and_then(|ext| ext.to_str()) {
        Some("sam") | Some("bam") | Some("cram") => Box::new(AlignmentSource::new(path, threads)),
        _ => Box::new(Fastx::from_path(path)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use noodles_util::alignment::io::Format;
    use std::fs::File;
    use std::path::Path;

    fn create_test_sam(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t4\tref\t10\t255\t10M\t*\t0\t0\tACTGACTGAW\t*\n\
                        r02\t4\tref\t20\t255\t10M\t*\t0\t0\tGCTGACTGAC\t*\n";
        file.write_all(content).unwrap();
    }

    fn create_test_paired_sam(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t77\tref\t10\t255\t10M\t=\t20\t20\tACTGACTGAC\t*\n\
                        r01\t141\tref\t20\t255\t10M\t=\t10\t-20\tGCTGACTGAC\t*\n\
                        r02\t77\tref\t30\t255\t10M\t=\t40\t20\tACTGACTGAC\t*\n\
                        r02\t141\tref\t40\t255\t10M\t=\t30\t-20\tGCTGACTGAC\t*\n";
        file.write_all(content).unwrap();
    }

    fn create_test_sam_with_quality(path: &Path) {
        let mut file = File::create(path).unwrap();
        let content = b"@HD\tVN:1.6\tSO:coordinate\n\
                        @SQ\tSN:ref\tLN:1000\n\
                        r01\t4\tref\t10\t255\t10M\t*\t0\t0\tACTGACTGAC\tIIIIIIIIII\n\
                        r02\t4\tref\t20\t255\t10M\t*\t0\t0\tGCTGACTGAC\tJJJJJJJJJJ\n";
        file.write_all(content).unwrap();
    }

    #[test]
    fn test_determine_record_source_alignment() {
        let path = Path::new("test.bam");
        let _source = determine_record_source(path, NonZeroUsize::new(1).unwrap());
    }

    #[test]
    fn test_alignment_source_read_lengths() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test.sam");
        create_test_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let actual = source.read_lengths().unwrap();
        assert_eq!(actual.len(), 2);
        assert_eq!(actual[0], 10);
        assert_eq!(actual[1], 10);
    }

    #[test]
    fn test_alignment_source_read_lengths_paired() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let actual = source.read_lengths().unwrap();
        // 2 templates, each length 10 + 10 = 20.
        assert_eq!(actual.len(), 2);
        assert_eq!(actual[0], 20);
        assert_eq!(actual[1], 20);
    }

    #[test]
    fn test_alignment_source_count() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test.sam");
        create_test_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        assert_eq!(source.count().unwrap(), 2);
    }

    #[test]
    fn test_alignment_source_count_paired_dedups_by_template() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        // 4 records but 2 templates (read pairs)
        assert_eq!(source.count().unwrap(), 2);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_bam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test.sam");
        create_test_sam(&input_path);

        let source = AlignmentSource::new(&input_path, NonZeroUsize::new(1).unwrap());
        let reads_to_keep = vec![true, false];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            OutputEncoding::Alignment(Format::Bam),
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        // Verify the output is a valid BAM and has 1 record
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_reader(&buffer[..])
            .unwrap();
        let header = reader.read_header().unwrap();
        let mut count = 0;
        for result in reader.records(&header) {
            let record = result.unwrap();
            assert_eq!(record.name().map(|n| n.as_ref()), Some(&b"r01"[..]));
            count += 1;
        }
        assert_eq!(count, 1);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_fasta() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test.sam");
        create_test_sam(&input_path);

        let source = AlignmentSource::new(&input_path, NonZeroUsize::new(1).unwrap());
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            OutputEncoding::Fastx { fasta: true },
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        assert_eq!(output.lines().next().unwrap(), ">r02");
        assert_eq!(output.lines().count(), 2);
        assert!(output.contains("GCTGACTGAC"));
    }

    #[test]
    fn test_alignment_source_filter_reads_into_sam_to_fastq_with_quality() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test_qual.sam");
        create_test_sam_with_quality(&input_path);

        let source = AlignmentSource::new(&input_path, NonZeroUsize::new(1).unwrap());
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            OutputEncoding::Fastx { fasta: false },
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines[0], "@r02");
        assert_eq!(lines[1], "GCTGACTGAC");
        assert_eq!(lines[2], "+");
        assert_eq!(lines[3], "JJJJJJJJJJ");
    }

    #[test]
    fn test_alignment_source_filter_reads_into_paired_sam_to_sam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let input_path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&input_path);

        let source = AlignmentSource::new(&input_path, NonZeroUsize::new(1).unwrap());
        // 2 templates. Keep first.
        let reads_to_keep = vec![true, false];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            OutputEncoding::Alignment(Format::Sam),
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 20);

        // Verify the output has 2 records (1 pair)
        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_reader(&buffer[..])
            .unwrap();
        let header = reader.read_header().unwrap();
        let mut count = 0;
        for result in reader.records(&header) {
            let record = result.unwrap();
            assert_eq!(record.name().map(|n| n.as_ref()), Some(&b"r01"[..]));
            count += 1;
        }
        assert_eq!(count, 2);
    }

    #[test]
    fn test_alignment_source_filter_reads_into_bam_to_sam() {
        let temp_dir = tempfile::tempdir().unwrap();
        let sam_path = temp_dir.path().join("input.sam");
        create_test_sam_with_quality(&sam_path);

        // Convert SAM to BAM first
        let bam_path = temp_dir.path().join("input.bam");
        {
            let source = AlignmentSource::new(&sam_path, NonZeroUsize::new(1).unwrap());
            let mut bam_file = File::create(&bam_path).unwrap();
            source
                .filter_reads_into(
                    &[true, true],
                    2,
                    &mut bam_file,
                    OutputEncoding::Alignment(Format::Bam),
                )
                .unwrap();
        }

        let source = AlignmentSource::new(&bam_path, NonZeroUsize::new(1).unwrap());
        let reads_to_keep = vec![false, true];
        let nb_reads_keep = 1;
        let mut buffer = Vec::new();

        let result = source.filter_reads_into(
            &reads_to_keep,
            nb_reads_keep,
            &mut buffer,
            OutputEncoding::Alignment(Format::Sam),
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 10);

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("r02"));
        assert!(output.contains("GCTGACTGAC"));
        assert!(output.contains("JJJJJJJJJJ"));
    }

    fn create_test_sam_with_header(path: &Path, header_extra: &str) {
        let mut file = File::create(path).unwrap();
        let content = format!(
            "@HD\tVN:1.6\t{header_extra}\n\
             @SQ\tSN:ref\tLN:1000\n\
             r01\t77\tref\t10\t255\t10M\t=\t20\t20\tACTGACTGAC\t*\n\
             r02\t77\tref\t30\t255\t10M\t=\t40\t20\tACTGACTGAC\t*\n\
             r01\t141\tref\t20\t255\t10M\t=\t10\t-20\tGCTGACTGAC\t*\n\
             r02\t141\tref\t40\t255\t10M\t=\t30\t-20\tGCTGACTGAC\t*\n"
        );
        file.write_all(content.as_bytes()).unwrap();
    }

    #[test]
    fn check_name_grouped_accepts_adjacent_pairs() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("grouped.sam");
        create_test_paired_sam(&path);

        assert!(check_name_grouped(&path, NonZeroUsize::new(1).unwrap()).is_ok());
    }

    #[test]
    fn check_name_grouped_accepts_unsegmented_records() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("unsegmented.sam");
        create_test_sam(&path);

        assert!(check_name_grouped(&path, NonZeroUsize::new(1).unwrap()).is_ok());
    }

    #[test]
    fn check_name_grouped_rejects_non_adjacent_pairs() {
        // r01's two records are split apart by r02's - not name-grouped.
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("non_adjacent.sam");
        create_test_sam_with_header(&path, "SO:coordinate");

        let err = check_name_grouped(&path, NonZeroUsize::new(1).unwrap())
            .expect_err("non-adjacent mates should be rejected");
        assert!(matches!(err, FastxError::UngroupedAlignmentInput));
    }

    #[test]
    fn check_name_grouped_trusts_go_query_header_without_scanning() {
        // Same non-adjacent layout that `check_name_grouped_rejects_non_adjacent_pairs` rejects,
        // but a `GO:query` header short-circuits the scan entirely.
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("trusted.sam");
        create_test_sam_with_header(&path, "GO:query");

        assert!(check_name_grouped(&path, NonZeroUsize::new(1).unwrap()).is_ok());
    }

    #[test]
    fn check_name_grouped_trusts_so_queryname_header_without_scanning() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("trusted.sam");
        create_test_sam_with_header(&path, "SO:queryname");

        assert!(check_name_grouped(&path, NonZeroUsize::new(1).unwrap()).is_ok());
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_fraction_one_keeps_every_record() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test.sam");
        create_test_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let mut out = Vec::new();
        let stats = source
            .subsample_one_pass(
                1.0,
                Some(1),
                &mut out,
                OutputEncoding::Fastx { fasta: false },
            )
            .unwrap();

        assert_eq!(stats.reads_seen, 2);
        assert_eq!(stats.reads_kept, 2);
        let output = String::from_utf8(out).unwrap();
        assert!(output.contains("r01"));
        assert!(output.contains("r02"));
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_fraction_zero_keeps_nothing() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test.sam");
        create_test_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let mut out = Vec::new();
        let stats = source
            .subsample_one_pass(
                0.0,
                Some(1),
                &mut out,
                OutputEncoding::Fastx { fasta: false },
            )
            .unwrap();

        assert_eq!(stats.reads_seen, 2);
        assert_eq!(stats.reads_kept, 0);
        assert!(out.is_empty());
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_keeps_mates_together() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let mut out = Vec::new();
        let stats = source
            .subsample_one_pass(
                1.0,
                Some(1),
                &mut out,
                OutputEncoding::Fastx { fasta: false },
            )
            .unwrap();

        // 2 templates (r01, r02), each contributing 2 records.
        assert_eq!(stats.reads_seen, 2);
        assert_eq!(stats.reads_kept, 2);
        let output = String::from_utf8(out).unwrap();
        assert_eq!(output.matches("r01").count(), 2);
        assert_eq!(output.matches("r02").count(), 2);
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_rejects_mapped_reads() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("mapped.sam");
        let mut file = File::create(&path).unwrap();
        file.write_all(
            b"@HD\tVN:1.6\tSO:coordinate\n\
              @SQ\tSN:ref\tLN:1000\n\
              r01\t0\tref\t10\t255\t10M\t*\t0\t0\tACTGACTGAC\t*\n",
        )
        .unwrap();

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let mut out = Vec::new();
        let result = source.subsample_one_pass(
            1.0,
            Some(1),
            &mut out,
            OutputEncoding::Fastx { fasta: false },
        );

        assert!(matches!(result, Err(FastxError::MappedReadDetected)));
    }

    #[test]
    fn test_alignment_source_check_name_grouped_rejects_ungrouped_input() {
        // subsample_one_pass doesn't run the guard itself (see its doc comment) - callers run
        // AlignmentSource::check_name_grouped first, as Reads::run_one_pass does, so a rejection
        // happens before the output file is created/truncated.
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("non_adjacent.sam");
        create_test_sam_with_header(&path, "SO:coordinate");

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let result = source.check_name_grouped();

        assert!(matches!(result, Err(FastxError::UngroupedAlignmentInput)));
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_same_seed_gives_same_output() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());

        let mut out1 = Vec::new();
        source
            .subsample_one_pass(
                0.5,
                Some(7),
                &mut out1,
                OutputEncoding::Fastx { fasta: false },
            )
            .unwrap();

        let mut out2 = Vec::new();
        source
            .subsample_one_pass(
                0.5,
                Some(7),
                &mut out2,
                OutputEncoding::Fastx { fasta: false },
            )
            .unwrap();

        assert_eq!(out1, out2);
    }

    #[test]
    fn test_alignment_source_subsample_one_pass_alignment_output_round_trips() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("test_paired.sam");
        create_test_paired_sam(&path);

        let source = AlignmentSource::new(&path, NonZeroUsize::new(1).unwrap());
        let mut out = Vec::new();
        let stats = source
            .subsample_one_pass(
                1.0,
                Some(1),
                &mut out,
                OutputEncoding::Alignment(Format::Sam),
            )
            .unwrap();
        assert_eq!(stats.reads_kept, 2);

        let mut reader = noodles_util::alignment::io::reader::Builder::default()
            .build_from_reader(&out[..])
            .unwrap();
        let header = reader.read_header().unwrap();
        let count = reader.records(&header).count();
        assert_eq!(count, 4);
    }
}