cuttlefish-rs 0.0.1

Parallel, external-memory construction of uncolored and colored compacted de Bruijn graphs. The Cuttlefish 3 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
//! FASTA/FASTQ input discovery and zero-copy fragment parsing.
//!
//! Parsers split records at non-ACGT symbols. Borrowed callbacks are preferred
//! by the production partitioner so sequence data does not need to be copied.

use crate::dna::is_dna_ascii;
use crate::params::BuildParams;
use flate2::read::MultiGzDecoder;
use std::fs;
use std::io::{BufRead, BufReader, Read, Seek};
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SequenceFragment {
    pub source_id: u32,
    pub record_id: u64,
    pub offset: usize,
    pub seq: Vec<u8>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BorrowedSequenceFragment<'a> {
    pub source_id: u32,
    pub record_id: u64,
    pub offset: usize,
    pub seq: &'a [u8],
}

pub fn expand_input_paths(params: &BuildParams) -> Result<Vec<PathBuf>, InputError> {
    let mut paths = Vec::new();

    for path in &params.seqs {
        paths.push(PathBuf::from(path));
    }

    for list in &params.lists {
        let file = fs::File::open(list).map_err(|source| InputError::Io {
            path: PathBuf::from(list),
            source,
        })?;
        for line in BufReader::new(file).lines() {
            let line = line.map_err(|source| InputError::Io {
                path: PathBuf::from(list),
                source,
            })?;
            let trimmed = line.trim();
            if !trimmed.is_empty() {
                paths.push(PathBuf::from(trimmed));
            }
        }
    }

    for dir in &params.dirs {
        let mut entries = fs::read_dir(dir)
            .map_err(|source| InputError::Io {
                path: PathBuf::from(dir),
                source,
            })?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|source| InputError::Io {
                path: PathBuf::from(dir),
                source,
            })?;
        entries.sort_by_key(|entry| entry.path());
        for entry in entries {
            let path = entry.path();
            if path.is_file() {
                paths.push(path);
            }
        }
    }

    if paths.is_empty() {
        return Err(InputError::NoInput);
    }

    Ok(paths)
}

pub fn parse_fragments<P, F>(
    path: P,
    source_id: u32,
    min_len: usize,
    mut on_fragment: F,
) -> Result<u64, InputError>
where
    P: AsRef<Path>,
    F: FnMut(SequenceFragment) -> Result<(), InputError>,
{
    parse_fragments_borrowed(path, source_id, min_len, |fragment| {
        on_fragment(SequenceFragment {
            source_id: fragment.source_id,
            record_id: fragment.record_id,
            offset: fragment.offset,
            seq: normalized_fragment_seq(fragment.seq),
        })
    })
}

pub fn parse_fragments_borrowed<P, F>(
    path: P,
    source_id: u32,
    min_len: usize,
    on_fragment: F,
) -> Result<u64, InputError>
where
    P: AsRef<Path>,
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    parse_fragments_borrowed_with(path, source_id, min_len, 1, on_fragment)
}

/// As [`parse_fragments_borrowed`], but permitted to use `inflate_workers`
/// threads to decompress block-structured (BGZF) gzip input.
///
/// The decompressed byte stream is identical either way; only the work of
/// producing it is shared out. Plain gzip ignores the budget.
pub fn parse_fragments_borrowed_with<P, F>(
    path: P,
    source_id: u32,
    min_len: usize,
    inflate_workers: usize,
    mut on_fragment: F,
) -> Result<u64, InputError>
where
    P: AsRef<Path>,
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    let path = path.as_ref();
    let mut file = fs::File::open(path).map_err(|source| InputError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    let input: Box<dyn Read> = if path.extension().is_some_and(|ext| ext == "gz") {
        // BGZF concatenates independent gzip members, so its blocks can be
        // inflated concurrently. A plain member cannot be split, and falls back
        // to the serial decoder.
        let mut head = [0u8; crate::bgzf::PROBE_BYTES];
        let probed = read_probe(&mut file, &mut head).map_err(|source| InputError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        file.rewind().map_err(|source| InputError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        if inflate_workers > 1 && crate::bgzf::is_bgzf(&head[..probed]) {
            Box::new(crate::bgzf::ParallelBgzfReader::new(file, inflate_workers))
        } else {
            Box::new(MultiGzDecoder::new(file))
        }
    } else {
        Box::new(file)
    };
    let mut reader = BufReader::with_capacity(1024 * 1024, input);
    let first_line = next_non_empty_line(&mut reader, path)?;
    match first_line.first().copied() {
        Some(b'>') => parse_fasta_reader(first_line, reader, source_id, min_len, &mut on_fragment),
        Some(b'@') => parse_fastq_reader(first_line, reader, source_id, min_len, &mut on_fragment),
        Some(_) if first_line.iter().copied().all(is_dna_ascii) => {
            parse_plain_sequence_reader(first_line, reader, source_id, min_len, &mut on_fragment)
        }
        Some(_) => Err(InputError::UnknownFormat(path.to_path_buf())),
        None => Err(InputError::EmptyFile(path.to_path_buf())),
    }
}

/// Fills as much of `head` as the file provides, tolerating short reads.
fn read_probe<R: Read>(source: &mut R, head: &mut [u8]) -> std::io::Result<usize> {
    let mut filled = 0;
    while filled < head.len() {
        match source.read(&mut head[filled..]) {
            Ok(0) => break,
            Ok(n) => filled += n,
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(error) => return Err(error),
        }
    }
    Ok(filled)
}

/// Appends a payload line, dropping embedded whitespace.
///
/// The filtering form is a per-byte predicate and push, which cannot vectorize.
/// Sequence lines essentially never contain interior whitespace, so probe first
/// with a scan the compiler can vectorize and fall back only when needed. The
/// slow path is kept so that interior whitespace still joins a record rather
/// than splitting it into fragments.
#[inline]
fn append_sequence_line(seq: &mut Vec<u8>, line: &[u8]) {
    if line.iter().any(u8::is_ascii_whitespace) {
        seq.extend(line.iter().copied().filter(|b| !b.is_ascii_whitespace()));
    } else {
        seq.extend_from_slice(line);
    }
}

fn parse_plain_sequence_reader<R, F>(
    first_line: Vec<u8>,
    mut reader: R,
    source_id: u32,
    min_len: usize,
    on_fragment: &mut F,
) -> Result<u64, InputError>
where
    R: BufRead,
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    let mut seq = first_line;
    let mut line = Vec::new();
    loop {
        line.clear();
        if reader
            .read_until(b'\n', &mut line)
            .map_err(|source| InputError::Read { source })?
            == 0
        {
            break;
        }
        trim_ascii_line_in_place(&mut line);
        append_sequence_line(&mut seq, &line);
    }
    emit_actg_fragments(source_id, 1, &seq, min_len, on_fragment)?;
    Ok(1)
}

fn next_non_empty_line<R: BufRead>(reader: &mut R, path: &Path) -> Result<Vec<u8>, InputError> {
    let mut line = Vec::new();
    loop {
        line.clear();
        let n = reader
            .read_until(b'\n', &mut line)
            .map_err(|source| InputError::Io {
                path: path.to_path_buf(),
                source,
            })?;
        if n == 0 {
            return Err(InputError::EmptyFile(path.to_path_buf()));
        }
        trim_ascii_line_in_place(&mut line);
        if !line.is_empty() {
            return Ok(line);
        }
    }
}

fn parse_fasta_reader<R, F>(
    first_header: Vec<u8>,
    mut reader: R,
    source_id: u32,
    min_len: usize,
    on_fragment: &mut F,
) -> Result<u64, InputError>
where
    R: BufRead,
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    debug_assert!(first_header.starts_with(b">"));
    let mut records = 0u64;
    let mut record_id = 1u64;
    let mut seq = Vec::new();
    let mut line = Vec::new();

    loop {
        line.clear();
        let n = reader
            .read_until(b'\n', &mut line)
            .map_err(|source| InputError::Read { source })?;
        if n == 0 {
            break;
        }
        trim_ascii_line_in_place(&mut line);
        if line.starts_with(b">") {
            records += 1;
            emit_actg_fragments(source_id, record_id, &seq, min_len, on_fragment)?;
            seq.clear();
            record_id += 1;
        } else if !line.is_empty() {
            append_sequence_line(&mut seq, &line);
        }
    }

    records += 1;
    emit_actg_fragments(source_id, record_id, &seq, min_len, on_fragment)?;

    Ok(records)
}

fn parse_fastq_reader<R, F>(
    first_header: Vec<u8>,
    mut reader: R,
    source_id: u32,
    min_len: usize,
    on_fragment: &mut F,
) -> Result<u64, InputError>
where
    R: BufRead,
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    debug_assert!(first_header.starts_with(b"@"));
    let mut record_id = 0u64;
    let mut header = first_header;
    let mut seq = Vec::new();
    let mut line = Vec::new();

    loop {
        record_id += 1;
        if !header.starts_with(b"@") {
            return Err(InputError::MalformedFastq(record_id));
        }

        seq.clear();
        loop {
            line.clear();
            if reader
                .read_until(b'\n', &mut line)
                .map_err(|source| InputError::Read { source })?
                == 0
            {
                return Err(InputError::MalformedFastq(record_id));
            }
            trim_ascii_line_in_place(&mut line);
            if line.starts_with(b"+") {
                break;
            }
            append_sequence_line(&mut seq, &line);
        }

        let mut qual_len = 0usize;
        while qual_len < seq.len() {
            line.clear();
            if reader
                .read_until(b'\n', &mut line)
                .map_err(|source| InputError::Read { source })?
                == 0
            {
                return Err(InputError::MalformedFastq(record_id));
            }
            trim_ascii_line_in_place(&mut line);
            qual_len += line.len();
        }

        emit_actg_fragments(source_id, record_id, &seq, min_len, on_fragment)?;

        header.clear();
        let bytes = reader
            .read_until(b'\n', &mut header)
            .map_err(|source| InputError::Read { source })?;
        if bytes == 0 {
            break;
        }
        trim_ascii_line_in_place(&mut header);
        if header.is_empty() {
            return Err(InputError::MalformedFastq(record_id + 1));
        }
    }

    Ok(record_id)
}

#[cfg(test)]
fn parse_fasta_bytes<F>(
    bytes: &[u8],
    source_id: u32,
    min_len: usize,
    on_fragment: &mut F,
) -> Result<u64, InputError>
where
    F: FnMut(SequenceFragment) -> Result<(), InputError>,
{
    let mut reader = BufReader::new(bytes);
    let first = next_non_empty_line(&mut reader, Path::new("<memory>"))?;
    let mut on_borrowed = |fragment: BorrowedSequenceFragment<'_>| {
        on_fragment(SequenceFragment {
            source_id: fragment.source_id,
            record_id: fragment.record_id,
            offset: fragment.offset,
            seq: normalized_fragment_seq(fragment.seq),
        })
    };
    match first.first().copied() {
        Some(b'>') => parse_fasta_reader(first, reader, source_id, min_len, &mut on_borrowed),
        Some(_) => Err(InputError::UnknownFormat(PathBuf::from("<memory>"))),
        None => Err(InputError::EmptyFile(PathBuf::from("<memory>"))),
    }
}

#[cfg(test)]
fn parse_fastq_bytes<F>(
    bytes: &[u8],
    source_id: u32,
    min_len: usize,
    on_fragment: &mut F,
) -> Result<u64, InputError>
where
    F: FnMut(SequenceFragment) -> Result<(), InputError>,
{
    let mut reader = BufReader::new(bytes);
    let first = next_non_empty_line(&mut reader, Path::new("<memory>"))?;
    let mut on_borrowed = |fragment: BorrowedSequenceFragment<'_>| {
        on_fragment(SequenceFragment {
            source_id: fragment.source_id,
            record_id: fragment.record_id,
            offset: fragment.offset,
            seq: normalized_fragment_seq(fragment.seq),
        })
    };
    match first.first().copied() {
        Some(b'@') => parse_fastq_reader(first, reader, source_id, min_len, &mut on_borrowed),
        Some(_) => Err(InputError::UnknownFormat(PathBuf::from("<memory>"))),
        None => Err(InputError::EmptyFile(PathBuf::from("<memory>"))),
    }
}

fn emit_actg_fragments<F>(
    source_id: u32,
    record_id: u64,
    seq: &[u8],
    min_len: usize,
    on_fragment: &mut F,
) -> Result<(), InputError>
where
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    let mut start = None;
    for (idx, &base) in seq.iter().enumerate() {
        if is_dna_ascii(base) {
            start.get_or_insert(idx);
        } else if let Some(beg) = start.take() {
            emit_fragment(
                source_id,
                record_id,
                beg,
                &seq[beg..idx],
                min_len,
                on_fragment,
            )?;
        }
    }

    if let Some(beg) = start {
        emit_fragment(source_id, record_id, beg, &seq[beg..], min_len, on_fragment)?;
    }

    Ok(())
}

fn emit_fragment<F>(
    source_id: u32,
    record_id: u64,
    offset: usize,
    seq: &[u8],
    min_len: usize,
    on_fragment: &mut F,
) -> Result<(), InputError>
where
    F: for<'a> FnMut(BorrowedSequenceFragment<'a>) -> Result<(), InputError>,
{
    if seq.len() >= min_len {
        on_fragment(BorrowedSequenceFragment {
            source_id,
            record_id,
            offset,
            seq,
        })?;
    }
    Ok(())
}

pub fn normalized_fragment_seq(seq: &[u8]) -> Vec<u8> {
    if seq.iter().all(|&b| matches!(b, b'A' | b'C' | b'G' | b'T')) {
        seq.to_vec()
    } else {
        seq.iter().map(|b| b.to_ascii_uppercase()).collect()
    }
}

#[inline]
fn trim_ascii_line_in_place(line: &mut Vec<u8>) {
    while line.last().is_some_and(|b| b.is_ascii_whitespace()) {
        line.pop();
    }
}

#[derive(Debug)]
pub enum InputError {
    NoInput,
    EmptyFile(PathBuf),
    UnknownFormat(PathBuf),
    MalformedFastq(u64),
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    Read {
        source: std::io::Error,
    },
    Partition(crate::partition::PartitionError),
    Bucket(crate::buckets::BucketError),
}

impl std::fmt::Display for InputError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoInput => write!(f, "no input files resolved"),
            Self::EmptyFile(path) => write!(f, "input file is empty: {}", path.display()),
            Self::UnknownFormat(path) => {
                write!(f, "unknown FASTA/FASTQ format: {}", path.display())
            }
            Self::MalformedFastq(record) => write!(f, "malformed FASTQ record {record}"),
            Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
            Self::Read { source } => write!(f, "{source}"),
            Self::Partition(err) => write!(f, "{err}"),
            Self::Bucket(err) => write!(f, "{err}"),
        }
    }
}

impl std::error::Error for InputError {}

impl From<crate::buckets::BucketError> for InputError {
    fn from(value: crate::buckets::BucketError) -> Self {
        Self::Bucket(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write;

    #[test]
    fn fasta_splits_on_non_actg() {
        let mut fragments = Vec::new();
        let records = parse_fasta_bytes(b">r1\nAACNNttg\n>r2\nCC\n", 1, 2, &mut |frag| {
            fragments.push(frag);
            Ok(())
        })
        .unwrap();

        assert_eq!(records, 2);
        assert_eq!(
            fragments
                .iter()
                .map(|f| f.seq.as_slice())
                .collect::<Vec<_>>(),
            vec![b"AAC".as_slice(), b"TTG".as_slice(), b"CC".as_slice()]
        );
        assert_eq!(fragments[1].offset, 5);
    }

    #[test]
    fn fastq_splits_and_counts_records() {
        let mut fragments = Vec::new();
        let records = parse_fastq_bytes(b"@r1\nACNTA\n+\nIIIII\n", 7, 2, &mut |frag| {
            fragments.push(frag);
            Ok(())
        })
        .unwrap();

        assert_eq!(records, 1);
        assert_eq!(fragments.len(), 2);
        assert_eq!(fragments[0].source_id, 7);
        assert_eq!(fragments[0].seq, b"AC");
        assert_eq!(fragments[1].seq, b"TA");
    }

    #[test]
    fn parses_gzipped_fastq() {
        let path =
            std::env::temp_dir().join(format!("cf3rs-input-{}.fastq.gz", std::process::id()));
        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(b"@r1\nACGTNNTA\n+\nIIIIIIII\n").unwrap();
        fs::write(&path, encoder.finish().unwrap()).unwrap();

        let mut fragments = Vec::new();
        let records = parse_fragments(&path, 3, 2, |fragment| {
            fragments.push(fragment);
            Ok(())
        })
        .unwrap();

        assert_eq!(records, 1);
        assert_eq!(
            fragments
                .iter()
                .map(|fragment| fragment.seq.as_slice())
                .collect::<Vec<_>>(),
            vec![b"ACGT".as_slice(), b"TA".as_slice()]
        );

        let _ = fs::remove_file(path);
    }

    #[test]
    fn parses_headerless_wrapped_sequence() {
        let path =
            std::env::temp_dir().join(format!("cf3rs-input-{}-plain.fna.gz", std::process::id()));
        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(b"\nAACGT\nTNNACGT\n").unwrap();
        fs::write(&path, encoder.finish().unwrap()).unwrap();

        let mut fragments = Vec::new();
        let records = parse_fragments(&path, 9, 3, |fragment| {
            fragments.push(fragment);
            Ok(())
        })
        .unwrap();
        assert_eq!(records, 1);
        assert_eq!(fragments[0].seq, b"AACGTT");
        assert_eq!(fragments[1].seq, b"ACGT");
        assert_eq!(fragments[1].offset, 8);

        let _ = fs::remove_file(path);
    }
}