fastx-io 0.3.0

Fast, streaming FASTA/FASTQ reader and writer for bioinformatics pipelines
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
//! The [`Sequence`] record type.

use std::fmt;
use std::io::Write;
use std::ops::Range;

use crate::error::{Error, Result};
use crate::format::Format;
use crate::qual::{self, PHRED33};
use crate::seq::{self, Alphabet, BaseCounts};

/// One FASTA or FASTQ record.
///
/// A record with `quality == None` is a FASTA record; a record with quality is a
/// FASTQ record whose quality string always has the same length as `seq`.
///
/// Fields are public so that pipelines can build records without ceremony, but
/// the constructors and [`Sequence::validate`] exist to keep the length
/// invariant intact.
///
/// ```
/// use fastx::Sequence;
///
/// let read = Sequence::fastq("read1", b"ACGT", b"IIII")?;
/// assert_eq!(read.len(), 4);
/// assert_eq!(read.mean_quality(), Some(40.0));
/// # Ok::<(), fastx::Error>(())
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct Sequence {
    /// Identifier: the header up to the first whitespace, without `>`/`@`.
    pub id: String,
    /// Everything after the first whitespace in the header, if any.
    pub description: Option<String>,
    /// Residues as ASCII bytes, without line breaks.
    pub seq: Vec<u8>,
    /// Phred quality characters (not scores), FASTQ only.
    pub quality: Option<Vec<u8>>,
}

impl Sequence {
    /// A FASTA record.
    pub fn fasta<I: Into<String>>(id: I, seq: impl Into<Vec<u8>>) -> Sequence {
        Sequence {
            id: id.into(),
            description: None,
            seq: seq.into(),
            quality: None,
        }
    }

    /// A FASTQ record. Fails if sequence and quality lengths differ.
    pub fn fastq<I: Into<String>>(
        id: I,
        seq: impl Into<Vec<u8>>,
        quality: impl Into<Vec<u8>>,
    ) -> Result<Sequence> {
        let id = id.into();
        let seq = seq.into();
        let quality = quality.into();
        if seq.len() != quality.len() {
            return Err(Error::LengthMismatch {
                id,
                seq: seq.len(),
                quality: quality.len(),
            });
        }
        Ok(Sequence {
            id,
            description: None,
            seq,
            quality: Some(quality),
        })
    }

    /// Builder-style setter for the description.
    pub fn with_description<D: Into<String>>(mut self, description: D) -> Sequence {
        let description = description.into();
        self.description = if description.is_empty() {
            None
        } else {
            Some(description)
        };
        self
    }

    /// Number of residues.
    pub fn len(&self) -> usize {
        self.seq.len()
    }

    /// True when the record has no residues.
    pub fn is_empty(&self) -> bool {
        self.seq.is_empty()
    }

    /// True when the record carries quality scores.
    pub fn has_quality(&self) -> bool {
        self.quality.is_some()
    }

    /// The format this record can be written as losslessly.
    pub fn format(&self) -> Format {
        if self.has_quality() {
            Format::Fastq
        } else {
            Format::Fasta
        }
    }

    /// The header line without its leading `>`/`@`.
    ///
    /// ```
    /// # use fastx::Sequence;
    /// let s = Sequence::fasta("chr1", b"ACGT".to_vec()).with_description("human chromosome 1");
    /// assert_eq!(s.header(), "chr1 human chromosome 1");
    /// ```
    pub fn header(&self) -> String {
        match &self.description {
            Some(d) => format!("{} {}", self.id, d),
            None => self.id.clone(),
        }
    }

    /// The residues as `&str`, if they are valid UTF-8 (ASCII in practice).
    pub fn seq_str(&self) -> Result<&str> {
        std::str::from_utf8(&self.seq).map_err(|e| Error::InvalidByte {
            id: self.id.clone(),
            pos: e.valid_up_to(),
            byte: self.seq[e.valid_up_to()],
        })
    }

    /// Reset the record to an empty state while keeping its allocations.
    ///
    /// This is what makes [`crate::FastxReader::read_into`] allocation-free.
    pub fn clear(&mut self) {
        self.id.clear();
        self.description = None;
        self.seq.clear();
        if let Some(q) = self.quality.as_mut() {
            q.clear();
        }
    }

    /// Per-base counts.
    pub fn base_counts(&self) -> BaseCounts {
        BaseCounts::of(&self.seq)
    }

    /// GC fraction over unambiguous bases, `None` when there are none.
    pub fn gc_content(&self) -> Option<f64> {
        seq::gc_content(&self.seq)
    }

    /// Number of `N`/ambiguity bases.
    pub fn ambiguous_count(&self) -> u64 {
        self.base_counts().n
    }

    /// Reverse complement, reversing the quality string as well.
    ///
    /// ```
    /// # use fastx::Sequence;
    /// let read = Sequence::fastq("r", b"ACGT", b"ABCD")?.reverse_complement();
    /// assert_eq!(read.seq, b"ACGT");
    /// assert_eq!(read.quality.unwrap(), b"DCBA");
    /// # Ok::<(), fastx::Error>(())
    /// ```
    pub fn reverse_complement(&self) -> Sequence {
        Sequence {
            id: self.id.clone(),
            description: self.description.clone(),
            seq: seq::reverse_complement(&self.seq),
            quality: self
                .quality
                .as_ref()
                .map(|q| q.iter().rev().copied().collect()),
        }
    }

    /// Reverse complement in place.
    pub fn reverse_complement_in_place(&mut self) {
        seq::reverse_complement_in_place(&mut self.seq);
        if let Some(q) = self.quality.as_mut() {
            q.reverse();
        }
    }

    /// Uppercase the residues in place (undoes soft masking).
    pub fn make_uppercase(&mut self) {
        self.seq.make_ascii_uppercase();
    }

    /// A sub-record covering `range`, carrying the matching quality slice.
    ///
    /// Returns [`Error::OutOfBounds`] if the range does not fit.
    pub fn subseq(&self, range: Range<usize>) -> Result<Sequence> {
        if range.start > range.end || range.end > self.seq.len() {
            return Err(Error::OutOfBounds {
                id: self.id.clone(),
                start: range.start as u64,
                end: range.end as u64,
                length: self.seq.len() as u64,
            });
        }
        Ok(Sequence {
            id: self.id.clone(),
            description: self.description.clone(),
            seq: self.seq[range.clone()].to_vec(),
            quality: self.quality.as_ref().map(|q| q[range].to_vec()),
        })
    }

    /// Keep only `range`, discarding the rest, in place.
    pub fn trim_to(&mut self, range: Range<usize>) -> Result<()> {
        if range.start > range.end || range.end > self.seq.len() {
            return Err(Error::OutOfBounds {
                id: self.id.clone(),
                start: range.start as u64,
                end: range.end as u64,
                length: self.seq.len() as u64,
            });
        }
        self.seq.truncate(range.end);
        self.seq.drain(..range.start);
        if let Some(q) = self.quality.as_mut() {
            q.truncate(range.end);
            q.drain(..range.start);
        }
        Ok(())
    }

    /// Translate the residues into protein using the standard genetic code.
    pub fn translate(&self, frame: usize, stop_at_stop: bool) -> Sequence {
        Sequence {
            id: self.id.clone(),
            description: self.description.clone(),
            seq: seq::translate(&self.seq, frame, stop_at_stop),
            quality: None,
        }
    }

    /// Iterator over overlapping k-mers.
    pub fn kmers(&self, k: usize) -> impl Iterator<Item = &[u8]> {
        seq::kmers(&self.seq, k)
    }

    /// Decoded Phred scores assuming the Phred+33 offset.
    pub fn quality_scores(&self) -> Option<Vec<u8>> {
        self.quality.as_ref().map(|q| qual::scores(q, PHRED33))
    }

    /// Decoded Phred scores for an explicit offset.
    pub fn quality_scores_with(&self, offset: u8) -> Option<Vec<u8>> {
        self.quality.as_ref().map(|q| qual::scores(q, offset))
    }

    /// Error-probability-weighted mean quality (Phred+33).
    pub fn mean_quality(&self) -> Option<f64> {
        qual::mean_quality(self.quality.as_deref()?, PHRED33)
    }

    /// Expected number of sequencing errors in the read (Phred+33).
    pub fn expected_errors(&self) -> Option<f64> {
        Some(qual::expected_errors(self.quality.as_deref()?, PHRED33))
    }

    /// Convert Phred+64 quality to Phred+33 in place. No-op for FASTA records.
    pub fn convert_quality_offset(&mut self, from: u8, to: u8) {
        if let Some(q) = self.quality.as_mut() {
            for c in q.iter_mut() {
                *c = qual::encode(qual::score(*c, from), to);
            }
        }
    }

    /// Drop the quality string, turning a FASTQ record into a FASTA record.
    pub fn into_fasta(mut self) -> Sequence {
        self.quality = None;
        self
    }

    /// Check the invariants that the writers rely on.
    ///
    /// * the id is non-empty and free of whitespace/newlines
    /// * the description contains no newlines
    /// * residues are printable and non-whitespace, and in `alphabet`
    /// * quality, when present, has the same length as the sequence and is
    ///   printable ASCII
    pub fn validate(&self, alphabet: Alphabet) -> Result<()> {
        if self.id.is_empty() {
            return Err(Error::Parse {
                line: 0,
                kind: crate::error::ParseError::EmptyId,
            });
        }
        if let Some(pos) = self.id.bytes().position(|b| b.is_ascii_whitespace()) {
            return Err(Error::InvalidByte {
                id: self.id.clone(),
                pos,
                byte: self.id.as_bytes()[pos],
            });
        }
        if let Some(d) = &self.description {
            if let Some(pos) = d.bytes().position(|b| b == b'\n' || b == b'\r') {
                return Err(Error::InvalidByte {
                    id: self.id.clone(),
                    pos,
                    byte: d.as_bytes()[pos],
                });
            }
        }
        alphabet.validate_named(&self.seq, &self.id)?;
        if let Some(q) = &self.quality {
            if q.len() != self.seq.len() {
                return Err(Error::LengthMismatch {
                    id: self.id.clone(),
                    seq: self.seq.len(),
                    quality: q.len(),
                });
            }
            if let Some(pos) = q.iter().position(|&b| !(33..=126).contains(&b)) {
                return Err(Error::InvalidByte {
                    id: self.id.clone(),
                    pos,
                    byte: q[pos],
                });
            }
        }
        Ok(())
    }

    /// Write this record as FASTA, wrapping sequence lines at `line_width`
    /// (`None` writes the sequence on a single line).
    ///
    /// Fails with [`Error::InvalidByte`] if the sequence contains a byte FASTA
    /// cannot represent: `\n`, `\r` or `>`. None of the three survives a
    /// write/read cycle once line wrapping can move it, so writing one would
    /// silently corrupt the record.
    pub fn write_fasta<W: Write>(&self, out: &mut W, line_width: Option<usize>) -> Result<()> {
        check_writable_residues(&self.seq, &self.id)?;
        out.write_all(b">")?;
        self.write_header(out)?;
        match line_width.filter(|w| *w > 0) {
            None => {
                out.write_all(&self.seq)?;
                out.write_all(b"\n")?;
            }
            Some(width) => {
                for chunk in self.seq.chunks(width) {
                    out.write_all(chunk)?;
                    out.write_all(b"\n")?;
                }
                if self.seq.is_empty() {
                    out.write_all(b"\n")?;
                }
            }
        }
        Ok(())
    }

    /// Write this record as FASTQ. Fails when the record has no quality, when
    /// the lengths disagree, or when a byte cannot be represented.
    pub fn write_fastq<W: Write>(&self, out: &mut W) -> Result<()> {
        let quality = self.quality.as_ref().ok_or_else(|| Error::MissingQuality {
            id: self.id.clone(),
        })?;
        if quality.len() != self.seq.len() {
            return Err(Error::LengthMismatch {
                id: self.id.clone(),
                seq: self.seq.len(),
                quality: quality.len(),
            });
        }
        check_writable_residues(&self.seq, &self.id)?;
        check_writable_fastq_sequence(&self.seq, &self.id)?;
        check_writable_quality(quality, &self.id)?;
        out.write_all(b"@")?;
        self.write_header(out)?;
        out.write_all(&self.seq)?;
        out.write_all(b"\n+\n")?;
        out.write_all(quality)?;
        out.write_all(b"\n")?;
        Ok(())
    }

    fn write_header<W: Write>(&self, out: &mut W) -> Result<()> {
        out.write_all(self.id.as_bytes())?;
        if let Some(d) = &self.description {
            out.write_all(b" ")?;
            out.write_all(d.as_bytes())?;
        }
        out.write_all(b"\n")?;
        Ok(())
    }

    /// Render the record in its native format as a `String`.
    pub fn to_string_in(&self, format: Format) -> Result<String> {
        let mut buf = Vec::with_capacity(self.seq.len() * 2 + 64);
        match format {
            Format::Fasta => self.write_fasta(&mut buf, Some(60))?,
            Format::Fastq => self.write_fastq(&mut buf)?,
        }
        Ok(String::from_utf8_lossy(&buf).into_owned())
    }

    /// Split a raw header (without the `>`/`@`) into id and description, using
    /// `spare` as the description's buffer.
    ///
    /// Splitting happens on ASCII whitespace over the raw bytes. Doing it on
    /// `char::is_whitespace` over a `str` would be wrong twice over: a header
    /// containing, say, a non-breaking space would be split in a place other
    /// tools do not split, and slicing after a multi-byte whitespace character
    /// would land inside it and panic.
    ///
    /// The `spare` buffer exists for speed. `description` is an `Option<String>`,
    /// and an `Option` holding `None` cannot also hold a buffer — so this used to
    /// allocate a fresh `String` per record and free it in the next `clear()`.
    /// That malloc/free pair measured at a third of the parser's total time and
    /// made a nonsense of "no allocation in the hot loop". The reader now passes
    /// the previous record's buffer back in, so one allocation serves a whole
    /// file.
    pub(crate) fn set_header_reusing(&mut self, header: &[u8], spare: &mut String) {
        let (id, description) = split_header(header);
        push_lossy(&mut self.id, id);
        match description {
            None => self.description = None,
            Some(description) => {
                let mut buffer = std::mem::take(spare);
                buffer.clear();
                push_lossy(&mut buffer, description);
                self.description = Some(buffer);
            }
        }
    }
}

/// Append bytes to a `String`, replacing anything that is not UTF-8.
///
/// Headers are ASCII in practice, so the `from_utf8` path is the one that runs;
/// it borrows rather than allocating.
fn push_lossy(target: &mut String, bytes: &[u8]) {
    match std::str::from_utf8(bytes) {
        Ok(text) => target.push_str(text),
        Err(_) => target.push_str(&String::from_utf8_lossy(bytes)),
    }
}

/// Split a raw header into its identifier and description.
///
/// Shared by the owned and borrowed read paths so that the two cannot disagree
/// about where an id ends — the bug that appeared when the indexer and the reader
/// each had their own rule.
pub(crate) fn split_header(header: &[u8]) -> (&[u8], Option<&[u8]>) {
    let header = trim_ascii_end(header);
    let id = header_id(header);
    let description = trim_ascii_start(&header[id.len()..]);
    (
        id,
        if description.is_empty() {
            None
        } else {
            Some(description)
        },
    )
}

/// The identifier part of a raw header: everything up to the first ASCII
/// whitespace byte.
///
/// The FASTA indexer uses this too. Both must agree on where an id ends, or an
/// index will not contain the names the reader produces — which is precisely the
/// bug that appears when one of them splits on Unicode whitespace and the other
/// does not.
pub(crate) fn header_id(header: &[u8]) -> &[u8] {
    let end = header
        .iter()
        .position(|b| b.is_ascii_whitespace())
        .unwrap_or(header.len());
    &header[..end]
}

/// Reject sequence bytes that FASTA and FASTQ cannot represent.
///
/// Neither format has an escape mechanism, so some bytes simply cannot survive a
/// write/read cycle and writing them would silently corrupt data:
///
/// * `\n` ends a line, so a sequence containing one would be read back as two.
/// * `\r` is stripped when it precedes a line break, and line wrapping can move
///   any `\r` to the end of a line — so `"AC\r"` would come back as `"AC"`.
/// * `>` starts a header, and wrapping can move any `>` to the start of a line,
///   splitting one record into two.
///
/// The check is a single SIMD pass, so it costs almost nothing next to the write
/// itself. Refusing to write is the only honest option here: the alternative is
/// output that this crate cannot read back.
pub(crate) fn check_writable_residues(seq: &[u8], id: &str) -> Result<()> {
    match memchr::memchr3(b'\n', b'\r', b'>', seq) {
        None => Ok(()),
        Some(pos) => Err(Error::InvalidByte {
            id: id.to_string(),
            pos,
            byte: seq[pos],
        }),
    }
}

/// A FASTQ sequence may not begin with `+`, which would be read as the separator.
pub(crate) fn check_writable_fastq_sequence(seq: &[u8], id: &str) -> Result<()> {
    if seq.first() == Some(&b'+') {
        return Err(Error::InvalidByte {
            id: id.to_string(),
            pos: 0,
            byte: b'+',
        });
    }
    Ok(())
}

/// Quality strings are length-delimited, so only line breaks are unrepresentable.
pub(crate) fn check_writable_quality(quality: &[u8], id: &str) -> Result<()> {
    match memchr::memchr2(b'\n', b'\r', quality) {
        None => Ok(()),
        Some(pos) => Err(Error::InvalidByte {
            id: id.to_string(),
            pos,
            byte: quality[pos],
        }),
    }
}

/// `[u8]::trim_ascii_start` is stable only from Rust 1.80; our MSRV is 1.74.
fn trim_ascii_start(mut bytes: &[u8]) -> &[u8] {
    while let [first, rest @ ..] = bytes {
        if first.is_ascii_whitespace() {
            bytes = rest;
        } else {
            break;
        }
    }
    bytes
}

fn trim_ascii_end(mut bytes: &[u8]) -> &[u8] {
    while let [rest @ .., last] = bytes {
        if last.is_ascii_whitespace() {
            bytes = rest;
        } else {
            break;
        }
    }
    bytes
}

impl fmt::Display for Sequence {
    /// FASTA with 60-column wrapping, or FASTQ when quality is present.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut buf = Vec::new();
        let rendered = match self.format() {
            Format::Fasta => self.write_fasta(&mut buf, Some(60)),
            Format::Fastq => self.write_fastq(&mut buf),
        };
        rendered.map_err(|_| fmt::Error)?;
        f.write_str(&String::from_utf8_lossy(&buf))
    }
}

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

    #[test]
    fn fastq_requires_matching_lengths() {
        assert!(Sequence::fastq("r", b"ACGT", b"III").is_err());
        assert!(Sequence::fastq("r", b"ACGT", b"IIII").is_ok());
    }

    #[test]
    fn header_splitting() {
        let mut s = Sequence::default();
        s.set_header_reusing(b"chr1 human chromosome 1", &mut String::new());
        assert_eq!(s.id, "chr1");
        assert_eq!(s.description.as_deref(), Some("human chromosome 1"));

        let mut s = Sequence::default();
        s.set_header_reusing(b"lonely", &mut String::new());
        assert_eq!(s.id, "lonely");
        assert_eq!(s.description, None);

        let mut s = Sequence::default();
        s.set_header_reusing(b"tabbed\tdesc  with   spaces  ", &mut String::new());
        assert_eq!(s.id, "tabbed");
        assert_eq!(s.description.as_deref(), Some("desc  with   spaces"));
    }

    #[test]
    fn header_splitting_handles_non_ascii() {
        // A non-breaking space (U+00A0) is not a separator, and slicing around
        // it must not land inside the character.
        let mut s = Sequence::default();
        s.set_header_reusing("chr\u{a0}1 description".as_bytes(), &mut String::new());
        assert_eq!(s.id, "chr\u{a0}1");
        assert_eq!(s.description.as_deref(), Some("description"));

        // Invalid UTF-8 is replaced rather than rejected.
        let mut s = Sequence::default();
        s.set_header_reusing(&[b'i', b'd', 0xff, b' ', b'd'], &mut String::new());
        assert!(s.id.starts_with("id"));
        assert_eq!(s.description.as_deref(), Some("d"));

        // A header that is nothing but whitespace leaves an empty id, which the
        // reader turns into a parse error.
        let mut s = Sequence::default();
        s.set_header_reusing(b"   \t ", &mut String::new());
        assert!(s.id.is_empty());
    }

    #[test]
    fn fasta_wrapping() {
        let s = Sequence::fasta("x", b"AAAAACCCCC".to_vec());
        let mut out = Vec::new();
        s.write_fasta(&mut out, Some(5)).unwrap();
        assert_eq!(out, b">x\nAAAAA\nCCCCC\n");

        let mut out = Vec::new();
        s.write_fasta(&mut out, None).unwrap();
        assert_eq!(out, b">x\nAAAAACCCCC\n");

        // An empty sequence still gets a (blank) sequence line.
        let mut out = Vec::new();
        Sequence::fasta("empty", Vec::new())
            .write_fasta(&mut out, Some(60))
            .unwrap();
        assert_eq!(out, b">empty\n\n");
    }

    #[test]
    fn fastq_output_and_missing_quality() {
        let r = Sequence::fastq("r", b"ACGT", b"IIII")
            .unwrap()
            .with_description("d");
        let mut out = Vec::new();
        r.write_fastq(&mut out).unwrap();
        assert_eq!(out, b"@r d\nACGT\n+\nIIII\n");

        let mut out = Vec::new();
        assert!(matches!(
            Sequence::fasta("r", b"ACGT".to_vec()).write_fastq(&mut out),
            Err(Error::MissingQuality { .. })
        ));
    }

    #[test]
    fn trimming_keeps_quality_aligned() {
        let mut r = Sequence::fastq("r", b"AACCGGTT", b"01234567").unwrap();
        r.trim_to(2..6).unwrap();
        assert_eq!(r.seq, b"CCGG");
        assert_eq!(r.quality.as_deref(), Some(&b"2345"[..]));
        assert!(r.trim_to(0..99).is_err());
    }

    #[test]
    fn subseq_bounds() {
        let r = Sequence::fastq("r", b"AACCGG", b"012345").unwrap();
        let sub = r.subseq(1..3).unwrap();
        assert_eq!(sub.seq, b"AC");
        assert_eq!(sub.quality.as_deref(), Some(&b"12"[..]));
        #[allow(clippy::reversed_empty_ranges)] // deliberately backwards
        {
            assert!(r.subseq(4..2).is_err());
        }
        assert!(r.subseq(0..7).is_err());
    }

    #[test]
    fn validation_catches_bad_records() {
        let mut r = Sequence::fasta("ok", b"ACGT".to_vec());
        assert!(r.validate(Alphabet::Dna).is_ok());
        r.id = "has space".into();
        assert!(r.validate(Alphabet::Dna).is_err());
        r.id = String::new();
        assert!(r.validate(Alphabet::Dna).is_err());

        let mut r = Sequence::fastq("r", b"ACGT", b"IIII").unwrap();
        r.quality = Some(b"II".to_vec());
        assert!(matches!(
            r.validate(Alphabet::Dna),
            Err(Error::LengthMismatch { .. })
        ));
        r.quality = Some(b"II\nI".to_vec());
        assert!(matches!(
            r.validate(Alphabet::Dna),
            Err(Error::InvalidByte { .. })
        ));
    }

    #[test]
    fn refuses_to_write_unrepresentable_residues() {
        // Regression: the reader accepts a bare '\r' inside a sequence line, but
        // writing it would put the '\r' before a newline, where reading strips
        // it — so ">x\nAC\r\n" would come back as "AC". Refuse instead.
        let mut out = Vec::new();
        for bad in [&b"AC\r"[..], b"AC\nGT", b"AC>GT", b">AC"] {
            let record = Sequence::fasta("x", bad.to_vec());
            assert!(
                matches!(
                    record.write_fasta(&mut out, Some(60)),
                    Err(Error::InvalidByte { .. })
                ),
                "{:?} should be rejected",
                String::from_utf8_lossy(bad)
            );
        }
        // Ordinary residues, gaps and stops are all still fine.
        for good in [&b"ACGTN"[..], b"acgt-n.", b"MEEPQSDPSV*"] {
            let record = Sequence::fasta("x", good.to_vec());
            assert!(record.write_fasta(&mut out, Some(60)).is_ok());
        }
    }

    #[test]
    fn refuses_to_write_unrepresentable_fastq() {
        let mut out = Vec::new();

        // A leading '+' in the sequence would be read back as the separator.
        let record = Sequence::fastq("x", b"+CGT", b"IIII").unwrap();
        assert!(matches!(
            record.write_fastq(&mut out),
            Err(Error::InvalidByte {
                pos: 0,
                byte: b'+',
                ..
            })
        ));
        // '+' anywhere else is harmless.
        assert!(Sequence::fastq("x", b"A+GT", b"IIII")
            .unwrap()
            .write_fastq(&mut out)
            .is_ok());

        // A line break in the quality string breaks the record.
        let record = Sequence::fastq("x", b"ACGT", b"II\nI").unwrap();
        assert!(matches!(
            record.write_fastq(&mut out),
            Err(Error::InvalidByte { byte: b'\n', .. })
        ));
        // '@' in quality is legal (Q31) and must not be rejected.
        assert!(Sequence::fastq("x", b"ACGT", b"@@@@")
            .unwrap()
            .write_fastq(&mut out)
            .is_ok());
    }

    #[test]
    fn quality_offset_conversion() {
        let mut r = Sequence::fastq("r", b"ACGT", b"hhhh").unwrap();
        r.convert_quality_offset(crate::qual::PHRED64, PHRED33);
        assert_eq!(r.quality.as_deref(), Some(&b"IIII"[..]));
    }

    #[test]
    fn clear_keeps_capacity() {
        let mut r = Sequence::fastq("r", b"ACGT", b"IIII").unwrap();
        let cap = r.seq.capacity();
        r.clear();
        assert!(r.id.is_empty() && r.seq.is_empty());
        assert_eq!(r.quality.as_deref(), Some(&b""[..]));
        assert_eq!(r.seq.capacity(), cap);
    }
}