dnacomb 1.0.0

Count the occurances of structured sequence reads and compare to an expected library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
//! Unified parsing of single-end and paired-end FASTA/FASTQ inputs.
//!
//! This module normalises sequence-file input into a common `ReadPair` stream,
//! regardless of whether the source data are FASTA or FASTQ, compressed or
//! uncompressed, single-end or paired-end.
//!
//! FASTA input is converted into FASTQ-like records by assigning a default
//! quality score to each base so downstream code can operate on a single record type.
use bio::io::{fasta, fastq};
use clap::ValueEnum;
use crossbeam::channel::Receiver;
use flate2::read::MultiGzDecoder;
use log::debug;
use regex::Regex;
use std::fmt;
use std::fs::File;
use std::io::{self, BufReader};
use std::str;

use crate::errors::{FastaError, ReadPairError};
use crate::groups::ReadGroup;
use crate::seqs::ReadPair;

/// Parser for Fastq files
///
/// Creates an iterator that yields Fastq records
struct FastqParser<R>
where
    R: Iterator<Item = Result<fastq::Record, fastq::Error>>,
{
    reads: R,
}

impl<R> FastqParser<R>
where
    R: Iterator<Item = Result<fastq::Record, fastq::Error>>,
{
    fn new(reads: R) -> Self {
        FastqParser { reads }
    }
}

impl<R> Iterator for FastqParser<R>
where
    R: Iterator<Item = Result<fastq::Record, fastq::Error>>,
{
    type Item = Result<fastq::Record, FastaError>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reads.next() {
            Some(Ok(x)) => Some(Ok(x)),
            Some(Err(e)) => Some(Err(FastaError::Fastq(e))),
            None => None,
        }
    }
}

/// Parser for Fasta files
///
/// Creates an iterator yielding Fasta records
struct FastaParser<R>
where
    R: Iterator<Item = Result<fasta::Record, io::Error>>,
{
    reads: R,
    default_quality: u8,
}

impl<R> FastaParser<R>
where
    R: Iterator<Item = Result<fasta::Record, io::Error>>,
{
    fn new(reads: R, default_quality: u8) -> Self {
        FastaParser {
            reads,
            default_quality,
        }
    }
}

impl<R> Iterator for FastaParser<R>
where
    R: Iterator<Item = Result<fasta::Record, io::Error>>,
{
    type Item = Result<fastq::Record, FastaError>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reads.next() {
            Some(Ok(x)) => Some(Ok(fasta_to_fastq(x, self.default_quality))),
            Some(Err(e)) => Some(Err(FastaError::Fasta(e))),
            None => None,
        }
    }
}

/// Convert a FASTA record into a synthetic FASTQ record.
///
/// Because downstream processing expects FASTQ-style records, FASTA input is
/// normalised by assigning the same default quality byte to every base.
fn fasta_to_fastq(fasta_record: fasta::Record, default_quality: u8) -> fastq::Record {
    let id = fasta_record.id().to_string();
    let desc = fasta_record.desc().map(|d| d.to_string());
    let seq = fasta_record.seq().to_vec();
    let qual = vec![default_quality; seq.len()]; // Assign default quality for each base

    fastq::Record::with_attrs(&id, desc.as_deref(), &seq, &qual)
}

/// Common interface for sources that yield `ReadPair` records.
///
/// This trait abstracts over direct file parsing and threaded read delivery so
/// counting code can consume either through the same interface.
pub trait ReadPairProducer: Iterator<Item = Result<ReadPair, ReadPairError>> {
    /// Return `true` if produced records may include reverse reads.
    fn has_reverse(&self) -> bool;

    /// Return the grouping regex used to assign read groups, if any.
    fn group(&self) -> &Option<Regex>;

    /// Maximum number of reads configured for this producer, or `0` for no limit.
    fn max_reads(&self) -> u64;

    /// Number of reads yielded so far.
    fn read_count(&self) -> u64;
}

/// Parser that yields normalised `ReadPair` records from one or two sequence files.
///
/// Internally, forward and reverse inputs may be FASTA or FASTQ and may be
/// compressed or uncompressed. All records are converted into FASTQ-style
/// records before being assembled into `ReadPair`s.
///
/// For paired-end input, forward and reverse files are consumed in lockstep and
/// must remain synchronised. Grouping is derived from the forward read header only.
pub struct ReadPairParser {
    /// Forward parser
    forward: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>>,

    /// Reverse parser
    reverse: Option<Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>>>,

    /// Regex to process forward read names with to identify groups, for instance cells
    /// in single cell assays
    group: Option<Regex>,

    /// Maximum number of reads to process
    max_reads: u64,

    /// Number of reads processed
    read_count: u64,

    /// Implementation detail for repeated Regex search for read groups
    /// Instead of concating id/desc into a new string each time, reuse this
    /// buffer. id/desc are generally the same length so should quickly converge on
    /// a good capacity.
    group_haystack: String,
}

impl ReadPairParser {
    /// Construct a parser from already-initialised forward and optional reverse iterators.
    fn new(
        forward: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>>,
        reverse: Option<Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>>>,
        group: Option<Regex>,
        max_reads: u64,
    ) -> Self {
        ReadPairParser {
            forward,
            reverse,
            group,
            max_reads,
            read_count: 0,
            group_haystack: String::with_capacity(200),
        }
    }

    /// Construct a `ReadPairParser` from forward and optional reverse file paths.
    ///
    /// File format and compression may be explicitly specified or auto-detected
    /// from the path. FASTA input is converted into FASTQ-style records using
    /// `default_quality`.
    pub fn from_paths(
        forward: SeqPath,
        reverse: Option<SeqPath>,
        group: Option<Regex>,
        max_reads: u64,
        default_quality: u8,
    ) -> Result<Self, ReadPairError> {
        let f_records = forward.get_records(default_quality)?;
        let r_records = match reverse {
            Some(x) => Some(x.get_records(default_quality)?),
            None => None,
        };

        Ok(ReadPairParser::new(f_records, r_records, group, max_reads))
    }

    /// Assign a read group from the forward read header.
    ///
    /// If no grouping regex is configured, reads are assigned to the `ungrouped`
    /// sentinel group. If a regex is configured but no first capture group is
    /// found, reads are assigned to the `unmatched` sentinel group.
    fn read_group(&mut self, f_record: &fastq::Record) -> ReadGroup {
        let re = match &self.group {
            None => return ReadGroup::ungrouped(),
            Some(x) => x,
        };

        self.group_haystack.clear();
        self.group_haystack.push_str(f_record.id());
        match f_record.desc() {
            None => {}
            Some(x) => {
                self.group_haystack.push(' ');
                self.group_haystack.push_str(x);
            }
        }

        match re.captures(&self.group_haystack) {
            None => ReadGroup::unmatched(),
            Some(cap) => match cap.get(1) {
                None => ReadGroup::unmatched(),
                Some(x) => ReadGroup::grouped(x.as_str()),
            },
        }
    }
}

impl ReadPairProducer for ReadPairParser {
    fn has_reverse(&self) -> bool {
        self.reverse.is_some()
    }

    fn group(&self) -> &Option<Regex> {
        &self.group
    }

    fn max_reads(&self) -> u64 {
        self.max_reads
    }

    fn read_count(&self) -> u64 {
        self.read_count
    }
}

/// Yields one `ReadPair` at a time, enforcing paired-file synchronisation when
/// reverse reads are present.
impl Iterator for ReadPairParser {
    type Item = Result<ReadPair, ReadPairError>;

    fn next(&mut self) -> Option<Self::Item> {
        if (self.max_reads > 0) && (self.read_count == self.max_reads) {
            return None;
        }

        let f_record: Option<Result<fastq::Record, FastaError>> = self.forward.next();

        if f_record.is_some() {
            self.read_count += 1;
        }

        if self.reverse.is_none() {
            // Unpaired reads
            match f_record {
                Some(Ok(f)) => Some(Ok(ReadPair {
                    // Group first so can safely move f into ReadPair next
                    group: self.read_group(&f),
                    forward: f,
                    reverse: None,
                })),
                Some(Err(e)) => Some(Err(ReadPairError::ReadPair {
                    forward: Some(e),
                    reverse: None,
                })),
                None => None,
            }
        } else {
            // Paired reads
            let r_record: Option<Result<fastq::Record, FastaError>> =
                self.reverse.as_mut().unwrap().next();

            match (f_record, r_record) {
                // Expecgted read pair
                (Some(Ok(f)), Some(Ok(r))) => {
                    Some(Ok(ReadPair {
                        group: self.read_group(&f),
                        // Group first so can safely move f into ReadPair next
                        forward: f,
                        reverse: Some(r),
                    }))
                }

                // Files exhausted
                (None, None) => None,

                // Error combinations
                (Some(Ok(_)), Some(Err(r))) => Some(Err(ReadPairError::ReadPair {
                    forward: None,
                    reverse: Some(r),
                })),
                (Some(Err(f)), Some(Ok(_))) => Some(Err(ReadPairError::ReadPair {
                    forward: Some(f),
                    reverse: None,
                })),
                (Some(Err(f)), Some(Err(r))) => Some(Err(ReadPairError::ReadPair {
                    forward: Some(f),
                    reverse: Some(r),
                })),
                (Some(_), None) => Some(Err(ReadPairError::EarlyExhastion {
                    read: "Reverse".to_string(),
                })),
                (None, Some(_)) => Some(Err(ReadPairError::EarlyExhastion {
                    read: "Forward".to_string(),
                })),
            }
        }
    }
}

/// `ReadPairProducer` implementation backed by a channel from another thread.
///
/// This is used for multithreaded counting, where one thread produces parsed
/// reads and worker threads consume them through the same `ReadPairProducer`
/// interface as direct file parsing.
pub struct ThreadedReadPairParser {
    // Channel receiving new reads
    rx: Receiver<Result<ReadPair, ReadPairError>>,

    // Whether the incoming read pairs have reverse reads
    rev_reads: bool,

    // Regex used to parse grouping structure
    group: Option<Regex>,

    /// Maximum number of reads potentially coming from the channel
    max_reads: u64,

    /// Number of reads received on this channel
    read_count: u64,
}

impl ThreadedReadPairParser {
    /// Construct a threaded read producer from a receiving channel and parser metadata.
    pub fn new(
        rx: Receiver<Result<ReadPair, ReadPairError>>,
        rev_reads: bool,
        group: Option<Regex>,
        max_reads: u64,
    ) -> Self {
        ThreadedReadPairParser {
            rx,
            rev_reads,
            group,
            max_reads,
            read_count: 0,
        }
    }
}

impl ReadPairProducer for ThreadedReadPairParser {
    fn has_reverse(&self) -> bool {
        self.rev_reads
    }

    fn group(&self) -> &Option<Regex> {
        &self.group
    }

    fn max_reads(&self) -> u64 {
        self.max_reads
    }

    fn read_count(&self) -> u64 {
        self.read_count
    }
}

impl Iterator for ThreadedReadPairParser {
    type Item = Result<ReadPair, ReadPairError>;

    fn next(&mut self) -> Option<Self::Item> {
        let next = self.rx.recv().ok();

        if next.is_some() {
            self.read_count += 1;
        }

        next
    }
}

/// Path plus parsing metadata for a sequence file.
///
/// A `SeqPath` stores the file path together with either explicit or auto-detected
/// format/compression settings, and can open the file as a normalised record iterator.
#[derive(Debug)]
pub struct SeqPath {
    path: String,
    format: SeqFormat,
    gzip: Compression,
}

impl SeqPath {
    /// Construct a new sequence-file descriptor.
    pub fn new(path: String, format: SeqFormat, gzip: Compression) -> Self {
        SeqPath { path, format, gzip }
    }

    /// Open the sequence file and return an iterator over normalised FASTQ records.
    ///
    /// Format and compression are either taken directly from the stored settings
    /// or auto-detected from the file path. FASTA input is converted into FASTQ
    /// records using `default_quality`.
    fn get_records(
        &self,
        default_quality: u8,
    ) -> Result<Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>>, ReadPairError> {
        let fmt = match self.format {
            SeqFormat::Auto => detect_seq_format(&self.path)?,
            SeqFormat::Fasta | SeqFormat::Fastq => self.format,
        };

        let gzip = match self.gzip {
            Compression::Auto => detect_gzip(&self.path),
            Compression::Gzip | Compression::None => self.gzip,
        };
        debug!(
            "Using format {} and compression {} for {}",
            fmt, gzip, self.path
        );

        let reader: BufReader<File> = BufReader::new(File::open(&self.path)?);

        match (gzip, fmt) {
            (Compression::Auto, _) | (_, SeqFormat::Auto) => Err(ReadPairError::Format {
                desc: "Gzip::Auto or SeqFormat::Auto remained after parsing".to_string(),
            }),
            (Compression::None, SeqFormat::Fasta) => {
                let fasta_reader = fasta::Reader::from_bufread(reader);
                Ok(Box::new(FastaParser::new(
                    fasta_reader.records(),
                    default_quality,
                )))
            }
            (Compression::None, SeqFormat::Fastq) => {
                let fastq_reader = fastq::Reader::from_bufread(reader);
                Ok(Box::new(FastqParser::new(fastq_reader.records())))
            }
            (Compression::Gzip, SeqFormat::Fasta) => {
                // We use MultiGzDecoder here as in some cases seq files have multiple blocks and in others
                // a single one, this protects against this, although could give weird results if you abuse it
                // with an strange multi-file gzipped seq.
                let gz_decoder = BufReader::new(MultiGzDecoder::new(reader));
                let fasta_reader = fasta::Reader::from_bufread(gz_decoder);
                Ok(Box::new(FastaParser::new(
                    fasta_reader.records(),
                    default_quality,
                )))
            }
            (Compression::Gzip, SeqFormat::Fastq) => {
                let gz_decoder = BufReader::new(MultiGzDecoder::new(reader));
                let fastq_reader = fastq::Reader::from_bufread(gz_decoder);
                Ok(Box::new(FastqParser::new(fastq_reader.records())))
            }
        }
    }
}

impl fmt::Display for SeqPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({}, {})", self.path, self.format, self.gzip)
    }
}

/// Sequence file format.
#[derive(Clone, ValueEnum, Debug, Copy, PartialEq)]
pub enum SeqFormat {
    /// Detect format from the file extension.
    Auto,
    /// Parse as FASTA.
    Fasta,
    /// Parse as FASTQ.
    Fastq,
}

impl fmt::Display for SeqFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Auto => write!(f, "Auto Format"),
            Self::Fasta => write!(f, "Fasta"),
            Self::Fastq => write!(f, "Fastq"),
        }
    }
}

/// Detect file format from a string path
fn detect_seq_format(path: &str) -> Result<SeqFormat, ReadPairError> {
    if str::ends_with(path, ".fa")
        || str::ends_with(path, ".fa.gz")
        || str::ends_with(path, ".fasta")
        || str::ends_with(path, ".fasta.gz")
    {
        Ok(SeqFormat::Fasta)
    } else if str::ends_with(path, ".fq")
        || str::ends_with(path, ".fq.gz")
        || str::ends_with(path, ".fastq")
        || str::ends_with(path, ".fastq.gz")
    {
        Ok(SeqFormat::Fastq)
    } else {
        Err(ReadPairError::Format {
            desc: format!(
                "Can't auto-detect format of {path} (assumes .fa/.fasta \
             or .fq/fastq ending with optional .gz)"
            ),
        })
    }
}

/// Compression mode for sequence-file input.
#[derive(Clone, ValueEnum, Debug, Copy, PartialEq, Eq)]
pub enum Compression {
    /// Detect compression from the file extension.
    Auto,
    /// Treat the file as gzip-compressed.
    Gzip,
    /// Treat the file as uncompressed.
    None,
}

impl fmt::Display for Compression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Auto => write!(f, "Auto detect Compression"),
            Self::Gzip => write!(f, "Gzip"),
            Self::None => write!(f, "None"),
        }
    }
}

/// Detect compression format from the file extension.
///
/// Only Gzip with extension ".gz" ia supported currently.
fn detect_gzip(path: &str) -> Compression {
    if str::ends_with(path, ".gz") {
        Compression::Gzip
    } else {
        Compression::None
    }
}

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

    // Format detection
    #[test]
    fn test_seq_detection_fasta_variants() {
        assert_eq!(detect_seq_format("file.fa").unwrap(), SeqFormat::Fasta);
        assert_eq!(detect_seq_format("file.fasta").unwrap(), SeqFormat::Fasta);
        assert_eq!(detect_seq_format("file.fa.gz").unwrap(), SeqFormat::Fasta);
        assert_eq!(
            detect_seq_format("file.fasta.gz").unwrap(),
            SeqFormat::Fasta
        );
    }

    #[test]
    fn test_seq_detection_fastq_variants() {
        assert_eq!(detect_seq_format("file.fq").unwrap(), SeqFormat::Fastq);
        assert_eq!(detect_seq_format("file.fastq").unwrap(), SeqFormat::Fastq);
        assert_eq!(detect_seq_format("file.fq.gz").unwrap(), SeqFormat::Fastq);
        assert_eq!(
            detect_seq_format("file.fastq.gz").unwrap(),
            SeqFormat::Fastq
        );
    }

    #[test]
    fn test_seq_detection_with_paths() {
        assert_eq!(
            detect_seq_format("path/to/file.fa").unwrap(),
            SeqFormat::Fasta
        );
        assert_eq!(
            detect_seq_format("/absolute/path/file.fastq").unwrap(),
            SeqFormat::Fastq
        );
        assert_eq!(
            detect_seq_format("../relative/file.fq.gz").unwrap(),
            SeqFormat::Fastq
        );
    }

    #[test]
    fn test_seq_detection_case_sensitive() {
        // Extensions are case-sensitive
        assert!(detect_seq_format("file.FA").is_err());
        assert!(detect_seq_format("file.Fasta").is_err());
        assert!(detect_seq_format("file.FQ").is_err());
    }

    #[test]
    fn test_seq_detection_invalid_extensions() {
        assert!(detect_seq_format("file.txt").is_err());
        assert!(detect_seq_format("file.seq").is_err());
        assert!(detect_seq_format("file.gz").is_err());
        assert!(detect_seq_format("file").is_err());
    }

    #[test]
    fn test_seq_detection_multiple_dots() {
        assert_eq!(
            detect_seq_format("file.backup.fa.gz").unwrap(),
            SeqFormat::Fasta
        );
    }

    // Compression detection
    #[test]
    fn test_gzip_detection_gzip() {
        assert_eq!(detect_gzip("file.fa.gz"), Compression::Gzip);
        assert_eq!(detect_gzip("file.txt.gz"), Compression::Gzip);
    }

    #[test]
    fn test_gzip_detection_none() {
        assert_eq!(detect_gzip("file.fa"), Compression::None);
        assert_eq!(detect_gzip("file.txt"), Compression::None);
    }

    #[test]
    fn test_gzip_detection_case_sensitive() {
        assert_eq!(detect_gzip("file.GZ"), Compression::None);
    }

    #[test]
    fn test_gzip_detection_multiple_gz() {
        assert_eq!(detect_gzip("file.fa.gz.gz"), Compression::Gzip);
    }

    // SeqPath
    #[test]
    fn seqpath_new_auto_format_auto_compression() {
        let sp = SeqPath::new("test.fa.gz".to_string(), SeqFormat::Auto, Compression::Auto);
        assert_eq!(sp.format, SeqFormat::Auto);
        assert_eq!(sp.gzip, Compression::Auto);
        assert_eq!(sp.path, "test.fa.gz");
    }

    #[test]
    fn seqpath_new_explicit_format_and_compression() {
        let sp = SeqPath::new("test.txt".to_string(), SeqFormat::Fastq, Compression::Gzip);
        assert_eq!(sp.format, SeqFormat::Fastq);
        assert_eq!(sp.gzip, Compression::Gzip);
    }

    #[test]
    fn seqpath_display() {
        let sp = SeqPath::new(
            "test.fa.gz".to_string(),
            SeqFormat::Fasta,
            Compression::Gzip,
        );
        let display = format!("{}", sp);
        assert!(display.contains("test.fa.gz"));
        assert!(display.contains("Fasta"));
        assert!(display.contains("Gzip"));
    }

    // Fasta to Fastq
    #[test]
    fn fasta_to_fastq_basic() {
        let fasta = fasta::Record::with_attrs("seq1", None, b"ACGT");
        let fastq = fasta_to_fastq(fasta, b'I');

        assert_eq!(fastq.id(), "seq1");
        assert_eq!(fastq.seq(), b"ACGT");
        assert_eq!(fastq.qual(), b"IIII");
    }

    #[test]
    fn fasta_to_fastq_with_description() {
        let fasta = fasta::Record::with_attrs("seq1", Some("description text"), b"ACGT");
        let fastq = fasta_to_fastq(fasta, b'I');

        assert_eq!(fastq.id(), "seq1");
        assert_eq!(fastq.seq(), b"ACGT");
        assert_eq!(fastq.qual(), b"IIII");
    }

    #[test]
    fn fasta_to_fastq_empty_sequence() {
        let fasta = fasta::Record::with_attrs("empty", None, b"");
        let fastq = fasta_to_fastq(fasta, b'I');

        assert_eq!(fastq.id(), "empty");
        assert_eq!(fastq.seq(), b"");
        assert_eq!(fastq.qual(), b"");
    }

    #[test]
    fn fasta_to_fastq_different_quality_scores() {
        let fasta = fasta::Record::with_attrs("seq", None, b"ACGTACGT");
        let fastq_low = fasta_to_fastq(fasta.clone(), b'!');
        let fastq_mid = fasta_to_fastq(fasta.clone(), b'I');
        let fastq_high = fasta_to_fastq(fasta, b'~');

        assert_eq!(fastq_low.qual(), b"!!!!!!!!");
        assert_eq!(fastq_mid.qual(), b"IIIIIIII");
        assert_eq!(fastq_high.qual(), b"~~~~~~~~");
    }

    #[test]
    fn fasta_to_fastq_long_sequence() {
        let seq = vec![b'A'; 10_000];
        let fasta = fasta::Record::with_attrs("long", None, &seq);
        let fastq = fasta_to_fastq(fasta, b'I');

        assert_eq!(fastq.seq().len(), 10_000);
        assert_eq!(fastq.qual().len(), 10_000);
        assert!(fastq.qual().iter().all(|&q| q == b'I'));
    }

    // ReadPair parser properties
    #[test]
    fn readpair_parser_properties() {
        let fwd_iter = vec![Ok(fastq::Record::with_attrs("r1", None, b"ACGT", b"IIII"))];
        let fwd: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>> =
            Box::new(fwd_iter.into_iter());

        let parser = ReadPairParser::new(fwd, None, None, 100);
        assert!(!parser.has_reverse());
        assert_eq!(parser.max_reads(), 100);
        assert_eq!(parser.read_count(), 0);
        assert!(parser.group().is_none());
    }

    #[test]
    fn readpair_parser_with_grouping() {
        let re = Regex::new(r"([A-Z0-9]+)").unwrap();
        let fwd_iter = vec![Ok(fastq::Record::with_attrs(
            "GROUPX_read1",
            None,
            b"ACGT",
            b"IIII",
        ))];
        let fwd: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>> =
            Box::new(fwd_iter.into_iter());

        let parser = ReadPairParser::new(fwd, None, Some(re.clone()), 100);
        assert!(parser.group().is_some());
        assert_eq!(parser.group().as_ref().unwrap().as_str(), re.as_str());
    }

    // ReadPair parser grouping
    #[test]
    fn readpair_parser_group_ungrouped() {
        let mut parser = ReadPairParser::new(Box::new(std::iter::empty()), None, None, 0);

        let record = fastq::Record::with_attrs("read1", None, b"ACGT", b"IIII");
        let group = parser.read_group(&record);
        assert!(group.is_ungrouped());
    }

    #[test]
    fn readpair_parser_group_with_regex_match() {
        let re = Regex::new(r"([A-Z]+)_").unwrap();
        let mut parser = ReadPairParser::new(Box::new(std::iter::empty()), None, Some(re), 0);

        let record = fastq::Record::with_attrs("CELLTYPE_read1", None, b"ACGT", b"IIII");
        let group = parser.read_group(&record);
        assert!(group.is_match());
    }

    #[test]
    fn readpair_parser_group_with_regex_no_match() {
        let re = Regex::new(r"([0-9]+)").unwrap();
        let mut parser = ReadPairParser::new(Box::new(std::iter::empty()), None, Some(re), 0);

        let record = fastq::Record::with_attrs("readABC", None, b"ACGT", b"IIII");
        let group = parser.read_group(&record);
        assert!(group.is_unmatched());
    }

    #[test]
    fn readpair_parser_group_with_description() {
        let re = Regex::new(r"group=([A-Za-z0-9]+)").unwrap();
        let mut parser = ReadPairParser::new(Box::new(std::iter::empty()), None, Some(re), 0);

        let record =
            fastq::Record::with_attrs("read1", Some("group=mygroup extra"), b"ACGT", b"IIII");
        let group = parser.read_group(&record);
        assert!(group.is_match());
    }

    #[test]
    fn readpair_parser_group_haystack_reuse() {
        let re = Regex::new(r"([A-Z]+)").unwrap();
        let mut parser = ReadPairParser::new(Box::new(std::iter::empty()), None, Some(re), 0);

        let r1 = fastq::Record::with_attrs("ABC_read", None, b"ACGT", b"IIII");
        let r2 = fastq::Record::with_attrs("XYZ_read", None, b"ACGT", b"IIII");

        parser.read_group(&r1);
        let after_first = parser.group_haystack.capacity();
        parser.read_group(&r2);
        let after_second = parser.group_haystack.capacity();

        // Haystack capacity should stabilize after first use
        assert_eq!(after_first, after_second);
    }

    // ReadPair Parser iteration
    #[test]
    fn readpair_parser_max_reads_limit() {
        let fwd_iter = vec![
            Ok(fastq::Record::with_attrs("r1", None, b"A", b"I")),
            Ok(fastq::Record::with_attrs("r2", None, b"C", b"I")),
            Ok(fastq::Record::with_attrs("r3", None, b"G", b"I")),
        ];
        let fwd: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>> =
            Box::new(fwd_iter.into_iter());

        let mut parser = ReadPairParser::new(fwd, None, None, 2);

        assert!(parser.next().is_some());
        assert!(parser.next().is_some());
        assert!(parser.next().is_none(), "should stop at max_reads");
    }

    #[test]
    fn readpair_parser_zero_max_reads() {
        let fwd_iter = vec![Ok(fastq::Record::with_attrs("r1", None, b"A", b"I"))];
        let fwd: Box<dyn Iterator<Item = Result<fastq::Record, FastaError>>> =
            Box::new(fwd_iter.into_iter());

        let mut parser = ReadPairParser::new(fwd, None, None, 0);
        assert!(parser.next().is_some(), "0 means no limit");
    }

    // Format and compression display
    #[test]
    fn seqformat_display() {
        assert_eq!(format!("{}", SeqFormat::Auto), "Auto Format");
        assert_eq!(format!("{}", SeqFormat::Fasta), "Fasta");
        assert_eq!(format!("{}", SeqFormat::Fastq), "Fastq");
    }

    #[test]
    fn compression_display() {
        assert_eq!(format!("{}", Compression::Auto), "Auto detect Compression");
        assert_eq!(format!("{}", Compression::Gzip), "Gzip");
        assert_eq!(format!("{}", Compression::None), "None");
    }

    // Threaded parser properties
    #[test]
    fn threaded_readpair_parser_properties() {
        use crossbeam::channel::bounded;

        let (_tx, rx) = bounded::<Result<ReadPair, ReadPairError>>(10);
        let parser = ThreadedReadPairParser::new(rx, true, None, 1000);

        assert!(parser.has_reverse());
        assert_eq!(parser.max_reads(), 1000);
        assert_eq!(parser.read_count(), 0);
        assert!(parser.group().is_none());
    }

    #[test]
    fn threaded_readpair_parser_with_group() {
        use crossbeam::channel::bounded;

        let re = Regex::new(r"(\w+)").unwrap();
        let (_tx, rx) = bounded::<Result<ReadPair, ReadPairError>>(10);
        let parser = ThreadedReadPairParser::new(rx, false, Some(re.clone()), 500);

        assert!(!parser.has_reverse());
        assert!(parser.group().is_some());
        assert_eq!(parser.max_reads(), 500);
    }

    // Fastq parser
    #[test]
    fn fastq_parser_iter() {
        let records = vec![
            Ok(fastq::Record::with_attrs("r1", None, b"ACGT", b"IIII")),
            Ok(fastq::Record::with_attrs("r2", None, b"GGGG", b"!!!!")),
        ];
        let mut parser = FastqParser::new(records.into_iter());

        let r1 = parser.next().unwrap().unwrap();
        assert_eq!(r1.id(), "r1");
        let r2 = parser.next().unwrap().unwrap();
        assert_eq!(r2.id(), "r2");
        assert!(parser.next().is_none());
    }

    // Fasta parser
    #[test]
    fn fasta_parser_iter_with_quality() {
        let records = vec![
            Ok(fasta::Record::with_attrs("s1", None, b"ACGT")),
            Ok(fasta::Record::with_attrs("s2", None, b"GGGG")),
        ];
        let mut parser = FastaParser::new(records.into_iter(), b'#');

        let r1 = parser.next().unwrap().unwrap();
        assert_eq!(r1.id(), "s1");
        assert_eq!(r1.qual(), b"####");

        let r2 = parser.next().unwrap().unwrap();
        assert_eq!(r2.id(), "s2");
        assert_eq!(r2.qual(), b"####");
    }
}