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
//! Sequence utilities that operate on raw `&[u8]` slices.
//!
//! Everything here is case-preserving and allocation-free unless the return type
//! says otherwise, so these helpers can be used inside hot loops.

use crate::error::{Error, Result};

/// IUPAC pairs shared by the DNA and RNA complement tables.
const AMBIGUITY_PAIRS: &[(u8, u8)] = &[
    (b'Y', b'R'),
    (b'R', b'Y'),
    (b'S', b'S'),
    (b'W', b'W'),
    (b'K', b'M'),
    (b'M', b'K'),
    (b'B', b'V'),
    (b'V', b'B'),
    (b'D', b'H'),
    (b'H', b'D'),
    (b'N', b'N'),
];

/// Complement table for DNA output: `A` becomes `T`, and `U` becomes `A`.
static COMPLEMENT: [u8; 256] = build_complement_table(&[
    (b'A', b'T'),
    (b'T', b'A'),
    (b'U', b'A'),
    (b'G', b'C'),
    (b'C', b'G'),
]);

/// Complement table for RNA output: `A` becomes `U`, and both `T` and `U`
/// become `A`.
static COMPLEMENT_RNA: [u8; 256] = build_complement_table(&[
    (b'A', b'U'),
    (b'U', b'A'),
    (b'T', b'A'),
    (b'G', b'C'),
    (b'C', b'G'),
]);

const fn build_complement_table(bases: &[(u8, u8)]) -> [u8; 256] {
    // Bytes that are not nucleotide codes map to themselves.
    let mut table = [0u8; 256];
    let mut i = 0;
    while i < 256 {
        table[i] = i as u8;
        i += 1;
    }
    let mut p = 0;
    while p < bases.len() {
        let (from, to) = bases[p];
        table[from as usize] = to;
        table[(from + 32) as usize] = to + 32; // lowercase
        p += 1;
    }
    let mut p = 0;
    while p < AMBIGUITY_PAIRS.len() {
        let (from, to) = AMBIGUITY_PAIRS[p];
        table[from as usize] = to;
        table[(from + 32) as usize] = to + 32;
        p += 1;
    }
    table
}

/// Complement a single nucleotide as DNA, preserving case.
///
/// Bytes that are not nucleotide codes pass through unchanged, so gaps and
/// padding survive.
///
/// `U` complements to `A`, and `A` complements to `T` — so complementing twice
/// turns RNA into DNA rather than returning the original byte. That is the only
/// sensible reading of a DNA complement, but it does mean the operation is not
/// an involution on RNA. Use [`complement_rna`] to keep uracil.
///
/// ```
/// # use fastx::seq::complement;
/// assert_eq!(complement(b'A'), b'T');
/// assert_eq!(complement(b'g'), b'c');
/// assert_eq!(complement(b'-'), b'-');
/// assert_eq!(complement(b'U'), b'A');
/// assert_eq!(complement(complement(b'U')), b'T'); // not 'U'
/// ```
#[inline]
pub fn complement(base: u8) -> u8 {
    COMPLEMENT[base as usize]
}

/// Complement a single nucleotide as RNA: `A` becomes `U`, not `T`.
///
/// ```
/// # use fastx::seq::complement_rna;
/// assert_eq!(complement_rna(b'A'), b'U');
/// assert_eq!(complement_rna(b'U'), b'A');
/// assert_eq!(complement_rna(b'T'), b'A');
/// assert_eq!(complement_rna(complement_rna(b'u')), b'u');
/// ```
#[inline]
pub fn complement_rna(base: u8) -> u8 {
    COMPLEMENT_RNA[base as usize]
}

/// Reverse complement of a nucleotide sequence, as DNA.
///
/// ```
/// # use fastx::seq::reverse_complement;
/// assert_eq!(reverse_complement(b"ACGTn"), b"nACGT");
/// assert_eq!(reverse_complement(b"ACGU"), b"ACGT"); // uracil becomes thymine
/// ```
pub fn reverse_complement(seq: &[u8]) -> Vec<u8> {
    seq.iter().rev().map(|&b| complement(b)).collect()
}

/// Reverse complement of a nucleotide sequence, as RNA.
///
/// ```
/// # use fastx::seq::reverse_complement_rna;
/// assert_eq!(reverse_complement_rna(b"ACGU"), b"ACGU");
/// assert_eq!(reverse_complement_rna(b"AAGG"), b"CCUU");
/// ```
pub fn reverse_complement_rna(seq: &[u8]) -> Vec<u8> {
    seq.iter().rev().map(|&b| complement_rna(b)).collect()
}

/// Reverse complement into an existing buffer, which is cleared first.
///
/// Use this in loops to avoid one allocation per record.
pub fn reverse_complement_into(seq: &[u8], out: &mut Vec<u8>) {
    out.clear();
    out.reserve(seq.len());
    out.extend(seq.iter().rev().map(|&b| complement(b)));
}

/// Reverse complement a sequence in place.
pub fn reverse_complement_in_place(seq: &mut [u8]) {
    let n = seq.len();
    for i in 0..n / 2 {
        let j = n - 1 - i;
        let a = complement(seq[i]);
        let b = complement(seq[j]);
        seq[i] = b;
        seq[j] = a;
    }
    if n % 2 == 1 {
        seq[n / 2] = complement(seq[n / 2]);
    }
}

/// Per-base counts of a nucleotide sequence, case-insensitive.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BaseCounts {
    /// Adenine.
    pub a: u64,
    /// Cytosine.
    pub c: u64,
    /// Guanine.
    pub g: u64,
    /// Thymine, or uracil in RNA.
    pub t: u64,
    /// `N` and other IUPAC ambiguity codes.
    pub n: u64,
    /// Anything that is not a nucleotide code (gaps, `*`, ...).
    pub other: u64,
}

/// Class assigned to each byte by [`BASE_CLASS`].
const CLASS_A: u8 = 0;
const CLASS_C: u8 = 1;
const CLASS_G: u8 = 2;
const CLASS_T: u8 = 3;
const CLASS_AMBIGUOUS: u8 = 4;
const CLASS_OTHER: u8 = 5;

/// Maps every byte to a base class, so that counting is one table lookup and one
/// increment per base rather than a chain of comparisons.
static BASE_CLASS: [u8; 256] = build_base_class_table();

const fn build_base_class_table() -> [u8; 256] {
    let mut table = [CLASS_OTHER; 256];
    table[b'A' as usize] = CLASS_A;
    table[b'a' as usize] = CLASS_A;
    table[b'C' as usize] = CLASS_C;
    table[b'c' as usize] = CLASS_C;
    table[b'G' as usize] = CLASS_G;
    table[b'g' as usize] = CLASS_G;
    table[b'T' as usize] = CLASS_T;
    table[b't' as usize] = CLASS_T;
    table[b'U' as usize] = CLASS_T;
    table[b'u' as usize] = CLASS_T;

    let ambiguous = b"NRYSWKMBDHV";
    let mut i = 0;
    while i < ambiguous.len() {
        table[ambiguous[i] as usize] = CLASS_AMBIGUOUS;
        table[(ambiguous[i] + 32) as usize] = CLASS_AMBIGUOUS;
        i += 1;
    }
    table
}

impl BaseCounts {
    /// Count the bases of `seq`.
    pub fn of(seq: &[u8]) -> BaseCounts {
        // Eight slots rather than six so that the index is provably in range and
        // the bounds check disappears.
        let mut counts = [0u64; 8];
        for &b in seq {
            counts[(BASE_CLASS[b as usize] & 7) as usize] += 1;
        }
        BaseCounts {
            a: counts[CLASS_A as usize],
            c: counts[CLASS_C as usize],
            g: counts[CLASS_G as usize],
            t: counts[CLASS_T as usize],
            n: counts[CLASS_AMBIGUOUS as usize],
            other: counts[CLASS_OTHER as usize],
        }
    }

    /// Total number of counted bytes.
    pub fn total(&self) -> u64 {
        self.a + self.c + self.g + self.t + self.n + self.other
    }

    /// Unambiguous A/C/G/T bases.
    pub fn acgt(&self) -> u64 {
        self.a + self.c + self.g + self.t
    }

    /// GC fraction over unambiguous bases, or `None` when there are none.
    pub fn gc_content(&self) -> Option<f64> {
        let acgt = self.acgt();
        if acgt == 0 {
            None
        } else {
            Some((self.g + self.c) as f64 / acgt as f64)
        }
    }

    /// Add another set of counts.
    pub fn merge(&mut self, other: &BaseCounts) {
        self.a += other.a;
        self.c += other.c;
        self.g += other.g;
        self.t += other.t;
        self.n += other.n;
        self.other += other.other;
    }
}

/// GC fraction of a sequence, ignoring ambiguity codes.
///
/// Returns `None` when the sequence has no unambiguous bases.
///
/// ```
/// # use fastx::seq::gc_content;
/// assert_eq!(gc_content(b"GGCC"), Some(1.0));
/// assert_eq!(gc_content(b"ATAT"), Some(0.0));
/// assert_eq!(gc_content(b"GCATNNNN"), Some(0.5));
/// assert_eq!(gc_content(b"NNNN"), None);
/// ```
pub fn gc_content(seq: &[u8]) -> Option<f64> {
    BaseCounts::of(seq).gc_content()
}

/// A residue alphabet used for validation.
///
/// Marked `#[non_exhaustive]`: alphabets are a judgement call, not a closed set,
/// so adding one should not break callers. Alphabets are normally passed in
/// rather than matched on, which makes the cost of the annotation close to zero.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Alphabet {
    /// `ACGT` plus `N`, either case.
    Dna,
    /// `ACGU` plus `N`, either case.
    Rna,
    /// The full IUPAC nucleotide set plus `-` and `.` gaps.
    Iupac,
    /// The 20 amino acids plus `BXZJUO*-.`, either case.
    Protein,
    /// Any printable, non-whitespace byte.
    Any,
}

impl Alphabet {
    /// Whether `byte` is a member of the alphabet.
    pub fn contains(self, byte: u8) -> bool {
        let upper = byte.to_ascii_uppercase();
        match self {
            Alphabet::Dna => matches!(upper, b'A' | b'C' | b'G' | b'T' | b'N'),
            Alphabet::Rna => matches!(upper, b'A' | b'C' | b'G' | b'U' | b'N'),
            Alphabet::Iupac => {
                matches!(
                    upper,
                    b'A' | b'C'
                        | b'G'
                        | b'T'
                        | b'U'
                        | b'R'
                        | b'Y'
                        | b'S'
                        | b'W'
                        | b'K'
                        | b'M'
                        | b'B'
                        | b'D'
                        | b'H'
                        | b'V'
                        | b'N'
                        | b'-'
                        | b'.'
                )
            }
            Alphabet::Protein => upper.is_ascii_uppercase() || matches!(upper, b'*' | b'-' | b'.'),
            Alphabet::Any => byte.is_ascii_graphic(),
        }
    }

    /// Check every byte of `seq`, reporting the first violation.
    pub fn validate(self, seq: &[u8]) -> Result<()> {
        self.validate_named(seq, "")
    }

    /// Like [`Alphabet::validate`] but attaches a record id to the error.
    pub fn validate_named(self, seq: &[u8], id: &str) -> Result<()> {
        match seq.iter().position(|&b| !self.contains(b)) {
            None => Ok(()),
            Some(pos) => Err(Error::InvalidByte {
                id: id.to_string(),
                pos,
                byte: seq[pos],
            }),
        }
    }
}

/// Uppercase a sequence in place (soft-masked genomes use lowercase for repeats).
pub fn make_uppercase(seq: &mut [u8]) {
    seq.make_ascii_uppercase();
}

/// Replace `U`/`u` with `T`/`t`, turning RNA into DNA in place.
pub fn rna_to_dna(seq: &mut [u8]) {
    for b in seq.iter_mut() {
        match *b {
            b'U' => *b = b'T',
            b'u' => *b = b't',
            _ => {}
        }
    }
}

/// The standard genetic code (NCBI translation table 1), in `TCAG` codon order.
const CODON_TABLE: &[u8; 64] = b"FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG";

/// Sentinel stored in [`CODON_INDEX`] for bytes that are not `T`/`U`, `C`, `A` or `G`.
const NOT_A_BASE: u8 = u8::MAX;

/// Maps every byte to its position in the `TCAG` codon ordering.
static CODON_INDEX: [u8; 256] = build_codon_index_table();

const fn build_codon_index_table() -> [u8; 256] {
    let mut table = [NOT_A_BASE; 256];
    table[b'T' as usize] = 0;
    table[b't' as usize] = 0;
    table[b'U' as usize] = 0;
    table[b'u' as usize] = 0;
    table[b'C' as usize] = 1;
    table[b'c' as usize] = 1;
    table[b'A' as usize] = 2;
    table[b'a' as usize] = 2;
    table[b'G' as usize] = 3;
    table[b'g' as usize] = 3;
    table
}

#[inline]
fn base_index(base: u8) -> Option<usize> {
    match CODON_INDEX[base as usize] {
        NOT_A_BASE => None,
        index => Some(index as usize),
    }
}

/// Translate a single codon using the standard genetic code.
///
/// Stop codons become `*` and codons containing an ambiguous base become `X`.
///
/// ```
/// # use fastx::seq::translate_codon;
/// assert_eq!(translate_codon(b"ATG"), b'M');
/// assert_eq!(translate_codon(b"tga"), b'*');
/// assert_eq!(translate_codon(b"ANG"), b'X');
/// ```
pub fn translate_codon(codon: &[u8]) -> u8 {
    if codon.len() < 3 {
        return b'X';
    }
    match (
        base_index(codon[0]),
        base_index(codon[1]),
        base_index(codon[2]),
    ) {
        (Some(a), Some(b), Some(c)) => CODON_TABLE[a * 16 + b * 4 + c],
        _ => b'X',
    }
}

/// Translate a nucleotide sequence into a protein sequence.
///
/// `frame` is 0, 1 or 2 and skips that many leading bases. A trailing partial
/// codon is dropped. When `stop_at_stop` is true, translation ends before the
/// first stop codon.
///
/// ```
/// # use fastx::seq::translate;
/// assert_eq!(translate(b"ATGGCCTGA", 0, false), b"MA*");
/// assert_eq!(translate(b"ATGGCCTGA", 0, true), b"MA");
/// assert_eq!(translate(b"AATGGCC", 1, false), b"MA");
/// ```
pub fn translate(seq: &[u8], frame: usize, stop_at_stop: bool) -> Vec<u8> {
    let seq = if frame < seq.len() {
        &seq[frame..]
    } else {
        &[][..]
    };
    let mut out = Vec::with_capacity(seq.len() / 3);
    for codon in seq.chunks_exact(3) {
        let aa = translate_codon(codon);
        if stop_at_stop && aa == b'*' {
            break;
        }
        out.push(aa);
    }
    out
}

/// Iterator over the overlapping k-mers of a sequence.
///
/// ```
/// # use fastx::seq::kmers;
/// let all: Vec<&[u8]> = kmers(b"ACGTA", 3).collect();
/// assert_eq!(all, vec![&b"ACG"[..], &b"CGT"[..], &b"GTA"[..]]);
/// assert_eq!(kmers(b"AC", 3).count(), 0);
/// ```
pub fn kmers(seq: &[u8], k: usize) -> impl Iterator<Item = &[u8]> {
    let n = if k == 0 || seq.len() < k {
        0
    } else {
        seq.len() - k + 1
    };
    (0..n).map(move |i| &seq[i..i + k])
}

/// Canonical k-mer: the lexicographically smaller of a k-mer and its reverse
/// complement, so that both strands hash to the same value.
pub fn canonical_kmer(kmer: &[u8]) -> Vec<u8> {
    let rc = reverse_complement(kmer);
    if rc.as_slice() < kmer {
        rc
    } else {
        kmer.to_vec()
    }
}

/// Hamming distance between two equal-length sequences, case-insensitive.
///
/// Returns `None` if the lengths differ.
pub fn hamming_distance(a: &[u8], b: &[u8]) -> Option<usize> {
    if a.len() != b.len() {
        return None;
    }
    Some(
        a.iter()
            .zip(b)
            .filter(|(x, y)| !x.eq_ignore_ascii_case(y))
            .count(),
    )
}

/// N50 of a set of lengths: the length `L` such that contigs of at least `L`
/// cover half of the total assembly length.
///
/// ```
/// # use fastx::seq::n50;
/// assert_eq!(n50(&mut [2, 3, 4, 5, 6]), Some(5));
/// assert_eq!(n50(&mut []), None);
/// ```
pub fn n50(lengths: &mut [u64]) -> Option<u64> {
    nx(lengths, 0.5)
}

/// Generalised N-statistic: `nx(lengths, 0.9)` is the N90.
///
/// `fraction` is clamped to `0.0..=1.0`. The slice is sorted as a side effect.
pub fn nx(lengths: &mut [u64], fraction: f64) -> Option<u64> {
    if lengths.is_empty() {
        return None;
    }
    let total: u64 = lengths.iter().sum();
    if total == 0 {
        return Some(0);
    }
    lengths.sort_unstable_by(|a, b| b.cmp(a));
    let target = total as f64 * fraction.clamp(0.0, 1.0);
    let mut acc = 0u64;
    for &len in lengths.iter() {
        acc += len;
        if acc as f64 >= target {
            return Some(len);
        }
    }
    lengths.last().copied()
}

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

    #[test]
    fn complements_iupac_and_preserves_case() {
        assert_eq!(reverse_complement(b"ACGTacgt"), b"acgtACGT");
        assert_eq!(reverse_complement(b"RYKM"), b"KMRY");
        assert_eq!(reverse_complement(b""), b"");
        // Non-nucleotide bytes are preserved.
        assert_eq!(reverse_complement(b"AC-GT"), b"AC-GT");
    }

    #[test]
    fn in_place_matches_allocating() {
        for s in [&b""[..], b"A", b"AC", b"ACG", b"ACGTN", b"acgtRYn"] {
            let mut owned = s.to_vec();
            reverse_complement_in_place(&mut owned);
            assert_eq!(
                owned,
                reverse_complement(s),
                "{:?}",
                String::from_utf8_lossy(s)
            );
        }
    }

    #[test]
    fn counts_bases() {
        let c = BaseCounts::of(b"AACCGGTTNNxx");
        assert_eq!((c.a, c.c, c.g, c.t, c.n, c.other), (2, 2, 2, 2, 2, 2));
        assert_eq!(c.total(), 12);
        assert_eq!(c.acgt(), 8);
        assert_eq!(c.gc_content(), Some(0.5));
    }

    #[test]
    fn validates_alphabets() {
        assert!(Alphabet::Dna.validate(b"ACGTN").is_ok());
        assert!(Alphabet::Dna.validate(b"ACGU").is_err());
        assert!(Alphabet::Rna.validate(b"ACGU").is_ok());
        assert!(Alphabet::Iupac.validate(b"ACGTRYKM-").is_ok());
        assert!(Alphabet::Protein.validate(b"MEEPQSDPSV*").is_ok());
        assert!(Alphabet::Any.validate(b"anything!").is_ok());
        assert!(Alphabet::Any.validate(b"tab\there").is_err());

        match Alphabet::Dna.validate_named(b"ACG!T", "read1") {
            Err(Error::InvalidByte { id, pos, byte }) => {
                assert_eq!((id.as_str(), pos, byte), ("read1", 3, b'!'));
            }
            other => panic!("expected InvalidByte, got {other:?}"),
        }
    }

    #[test]
    fn translates_all_codons() {
        // Spot-check every position of the table via the classic ordering.
        assert_eq!(translate(b"TTTTTCTTATTG", 0, false), b"FFLL");
        assert_eq!(translate(b"ATGCATTAA", 0, false), b"MH*");
        assert_eq!(translate(b"AUGCAU", 0, false), b"MH"); // RNA input
                                                           // Trailing partial codon dropped.
        assert_eq!(translate(b"ATGCA", 0, false), b"M");
        assert_eq!(translate(b"AT", 0, false), b"");
        assert_eq!(translate(b"ATG", 5, false), b"");
    }

    #[test]
    fn kmers_and_canonical() {
        assert_eq!(kmers(b"AAAA", 4).count(), 1);
        assert_eq!(kmers(b"AAAA", 0).count(), 0);
        assert_eq!(canonical_kmer(b"TTT"), b"AAA");
        assert_eq!(canonical_kmer(b"AAA"), b"AAA");
    }

    #[test]
    fn hamming() {
        assert_eq!(hamming_distance(b"ACGT", b"acgt"), Some(0));
        assert_eq!(hamming_distance(b"ACGT", b"ACGA"), Some(1));
        assert_eq!(hamming_distance(b"ACGT", b"ACG"), None);
    }

    #[test]
    fn n_statistics() {
        // Total 100; sorted desc 50,30,15,5 -> 50 reaches 50.
        assert_eq!(n50(&mut [5, 50, 30, 15]), Some(50));
        assert_eq!(nx(&mut [5, 50, 30, 15], 0.9), Some(15));
        assert_eq!(nx(&mut [0, 0], 0.5), Some(0));
    }

    #[test]
    fn rna_dna_conversion() {
        let mut s = b"ACGUacgu".to_vec();
        rna_to_dna(&mut s);
        assert_eq!(s, b"ACGTacgt");
        make_uppercase(&mut s);
        assert_eq!(s, b"ACGTACGT");
    }
}