dino-seq 0.1.0

Low-allocation FASTQ and FASTA parser core
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
use std::fs::File;
use std::io::{Read, Write};
use std::ops::Range;
use std::path::PathBuf;

use dino_seq::{
    FastaIndex, FastaPartitionConfig, FastqStats, IndexedFastaReader, Result, build_fasta_index,
    open_fasta, open_fastq, plan_fasta_partitions,
};

#[cfg(feature = "bgzf")]
use dino_seq::{
    BgzfIndexedFastaReader, DetectedInputKind, build_bgzf_index_strict, build_fasta_index_bgzf,
    detect_file_input_kind,
};

fn main() {
    if let Err(err) = run() {
        eprintln!("{err}");
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let mut args = std::env::args().skip(1);
    let Some(command) = args.next() else {
        print_help();
        return Ok(());
    };
    match command.as_str() {
        "stats" => stats(args.collect()),
        "checksum" => checksum(args.collect()),
        "fasta-index" => fasta_index(args.collect()),
        "fasta-fetch" => fasta_fetch(args.collect()),
        "fasta-partitions" => fasta_partitions(args.collect()),
        "fasta-chunks" => fasta_chunks(args.collect()),
        "verify-bgzf" => verify_bgzf(args.collect()),
        "--help" | "-h" | "help" => {
            print_help();
            Ok(())
        }
        _ => Err(dino_seq::FastqError::Format(format!(
            "unknown command: {command}"
        ))),
    }
}

fn stats(args: Vec<String>) -> Result<()> {
    let mut format = String::from("fastq");
    let mut path = None;
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--format" => {
                format = iter.next().ok_or_else(|| {
                    dino_seq::FastqError::Format("--format requires a value".into())
                })?;
            }
            "--help" | "-h" => {
                print_help();
                return Ok(());
            }
            _ if path.is_none() => path = Some(PathBuf::from(arg)),
            _ => {
                return Err(dino_seq::FastqError::Format(format!(
                    "unexpected stats argument: {arg}"
                )));
            }
        }
    }
    let path = required_path(path, "stats")?;
    match format.as_str() {
        "fastq" => {
            let mut reader = open_fastq(&path)?;
            let stats = StreamStats::from(reader.count_records()?);
            print_stream_stats(&stats);
        }
        "fasta" => {
            let mut reader = open_fasta(&path)?;
            let stats = consume_fasta(&mut reader)?;
            print_stream_stats(&stats);
        }
        _ => {
            return Err(dino_seq::FastqError::Format(format!(
                "unsupported stats format: {format}"
            )));
        }
    }
    Ok(())
}

fn checksum(args: Vec<String>) -> Result<()> {
    let mut format = None;
    let mut path = None;
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--format" => format = Some(required_value(&mut iter, "--format")?),
            "--help" | "-h" => {
                print_help();
                return Ok(());
            }
            _ if path.is_none() => path = Some(arg),
            _ => {
                return Err(dino_seq::FastqError::Format(format!(
                    "unexpected checksum argument: {arg}"
                )));
            }
        }
    }
    let format =
        format.ok_or_else(|| dino_seq::FastqError::Format("checksum requires --format".into()))?;
    let reader = input_reader(path.as_deref())?;
    let stats = match format.as_str() {
        "fastq" => {
            let mut reader = dino_seq::FastqReader::new(reader);
            StreamStats::from(reader.count_records()?)
        }
        "fasta" => {
            let mut reader = dino_seq::FastaReader::new(reader);
            consume_fasta(&mut reader)?
        }
        _ => {
            return Err(dino_seq::FastqError::Format(format!(
                "unsupported checksum format: {format}"
            )));
        }
    };
    print_stream_stats(&stats);
    Ok(())
}

fn input_reader(path: Option<&str>) -> Result<Box<dyn Read>> {
    match path {
        None | Some("-") => Ok(Box::new(std::io::stdin())),
        Some(path) => Ok(Box::new(File::open(path)?)),
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct StreamStats {
    records: u64,
    bases: u64,
    qualities: u64,
    name_bytes: u64,
    checksum: u64,
}

impl StreamStats {
    fn observe_record(&mut self, name: &[u8], seq: &[u8], qual: &[u8]) {
        self.records += 1;
        self.bases += seq.len() as u64;
        self.qualities += qual.len() as u64;
        self.name_bytes += name.len() as u64;
        self.checksum = self
            .checksum
            .wrapping_add(seq.first().copied().unwrap_or_default() as u64)
            .wrapping_mul(1_099_511_628_211)
            .wrapping_add(seq.len() as u64);
    }
}

impl From<FastqStats> for StreamStats {
    fn from(stats: FastqStats) -> Self {
        Self {
            records: stats.records,
            bases: stats.bases,
            qualities: stats.qualities,
            name_bytes: stats.name_bytes,
            checksum: stats.checksum,
        }
    }
}

fn consume_fasta<R: Read>(reader: &mut dino_seq::FastaReader<R>) -> Result<StreamStats> {
    let mut stats = StreamStats::default();
    while let Some(batch) = reader.next_batch()? {
        for record in batch.records() {
            stats.observe_record(record.name(), record.seq(), b"");
        }
    }
    Ok(stats)
}

fn fasta_index(args: Vec<String>) -> Result<()> {
    let path = one_path(args, "fasta-index")?;
    #[cfg(feature = "bgzf")]
    let index = if detect_file_input_kind(&path)? == DetectedInputKind::Bgzf {
        build_fasta_index_bgzf(File::open(&path)?)?
    } else {
        build_fasta_index(File::open(&path)?)?
    };
    #[cfg(not(feature = "bgzf"))]
    let index = build_fasta_index(File::open(&path)?)?;
    print!("{}", index.to_fai_string());
    Ok(())
}

fn fasta_fetch(args: Vec<String>) -> Result<()> {
    let mut path = None;
    let mut fai = None;
    let mut name = None;
    let mut start = None;
    let mut end = None;
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--fai" => fai = Some(PathBuf::from(required_value(&mut iter, "--fai")?)),
            "--name" => name = Some(required_value(&mut iter, "--name")?),
            "--start" => start = Some(parse_u64_value(&mut iter, "--start")?),
            "--end" => end = Some(parse_u64_value(&mut iter, "--end")?),
            "--help" | "-h" => {
                print_help();
                return Ok(());
            }
            _ if path.is_none() => path = Some(PathBuf::from(arg)),
            _ => {
                return Err(dino_seq::FastqError::Format(format!(
                    "unexpected fasta-fetch argument: {arg}"
                )));
            }
        }
    }
    let path = required_path(path, "fasta-fetch")?;
    let fai = required_path(fai, "fasta-fetch --fai")?;
    let name =
        name.ok_or_else(|| dino_seq::FastqError::Format("fasta-fetch requires --name".into()))?;
    let range = Range {
        start: start
            .ok_or_else(|| dino_seq::FastqError::Format("fasta-fetch requires --start".into()))?,
        end: end
            .ok_or_else(|| dino_seq::FastqError::Format("fasta-fetch requires --end".into()))?,
    };
    let index = FastaIndex::from_fai_read(File::open(fai)?)?;

    #[cfg(feature = "bgzf")]
    let seq = if detect_file_input_kind(&path)? == DetectedInputKind::Bgzf {
        let bgzf_index = build_bgzf_index_strict(File::open(&path)?)?;
        let mut reader = BgzfIndexedFastaReader::new(File::open(&path)?, index, bgzf_index);
        reader.fetch(name.as_bytes(), range)?
    } else {
        let mut reader = IndexedFastaReader::new(File::open(&path)?, index);
        reader.fetch(name.as_bytes(), range)?
    };
    #[cfg(not(feature = "bgzf"))]
    let seq = {
        let mut reader = IndexedFastaReader::new(File::open(&path)?, index);
        reader.fetch(name.as_bytes(), range)?
    };

    std::io::stdout().write_all(&seq)?;
    std::io::stdout().write_all(b"\n")?;
    Ok(())
}

fn fasta_partitions(args: Vec<String>) -> Result<()> {
    let mut path = None;
    let mut fai = None;
    let mut parts = None;
    let mut overlap = 0_u64;
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--fai" => fai = Some(PathBuf::from(required_value(&mut iter, "--fai")?)),
            "--parts" => parts = Some(parse_usize_value(&mut iter, "--parts")?),
            "--overlap" => overlap = parse_u64_value(&mut iter, "--overlap")?,
            "--help" | "-h" => {
                print_help();
                return Ok(());
            }
            _ if path.is_none() => path = Some(PathBuf::from(arg)),
            _ => {
                return Err(dino_seq::FastqError::Format(format!(
                    "unexpected fasta-partitions argument: {arg}"
                )));
            }
        }
    }
    let _path = required_path(path, "fasta-partitions")?;
    let fai = required_path(fai, "fasta-partitions --fai")?;
    let parts = parts
        .ok_or_else(|| dino_seq::FastqError::Format("fasta-partitions requires --parts".into()))?;
    let index = FastaIndex::from_fai_read(File::open(fai)?)?;
    for partition in plan_fasta_partitions(&index, FastaPartitionConfig::new(parts, overlap))? {
        println!(
            "{}\t{}\t{}\t{}\t{}\t{}\t{}",
            partition.partition_index,
            String::from_utf8_lossy(&partition.name),
            partition.core.start,
            partition.core.end,
            partition.fetch.start,
            partition.fetch.end,
            partition.core_offset_in_fetch()
        );
    }
    Ok(())
}

fn fasta_chunks(args: Vec<String>) -> Result<()> {
    let mut path = None;
    let mut fai = None;
    let mut name = None;
    let mut start = None;
    let mut end = None;
    let mut chunk_bases = None;
    let mut iter = args.into_iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--fai" => fai = Some(PathBuf::from(required_value(&mut iter, "--fai")?)),
            "--name" => name = Some(required_value(&mut iter, "--name")?),
            "--start" => start = Some(parse_u64_value(&mut iter, "--start")?),
            "--end" => end = Some(parse_u64_value(&mut iter, "--end")?),
            "--chunk-bases" => chunk_bases = Some(parse_u64_value(&mut iter, "--chunk-bases")?),
            "--help" | "-h" => {
                print_help();
                return Ok(());
            }
            _ if path.is_none() => path = Some(PathBuf::from(arg)),
            _ => {
                return Err(dino_seq::FastqError::Format(format!(
                    "unexpected fasta-chunks argument: {arg}"
                )));
            }
        }
    }
    let path = required_path(path, "fasta-chunks")?;
    let fai = required_path(fai, "fasta-chunks --fai")?;
    let name =
        name.ok_or_else(|| dino_seq::FastqError::Format("fasta-chunks requires --name".into()))?;
    let range = Range {
        start: start
            .ok_or_else(|| dino_seq::FastqError::Format("fasta-chunks requires --start".into()))?,
        end: end
            .ok_or_else(|| dino_seq::FastqError::Format("fasta-chunks requires --end".into()))?,
    };
    let chunk_bases = chunk_bases.ok_or_else(|| {
        dino_seq::FastqError::Format("fasta-chunks requires --chunk-bases".into())
    })?;
    let index = FastaIndex::from_fai_read(File::open(fai)?)?;

    #[cfg(feature = "bgzf")]
    {
        if detect_file_input_kind(&path)? == DetectedInputKind::Bgzf {
            let bgzf_index = build_bgzf_index_strict(File::open(&path)?)?;
            let mut reader = BgzfIndexedFastaReader::new(File::open(&path)?, index, bgzf_index);
            for chunk in reader.reference_chunks(name.as_bytes(), range, chunk_bases)? {
                print_reference_chunk(&chunk?);
            }
            return Ok(());
        }
    }

    let mut reader = IndexedFastaReader::new(File::open(&path)?, index);
    for chunk in reader.reference_chunks(name.as_bytes(), range, chunk_bases)? {
        print_reference_chunk(&chunk?);
    }
    Ok(())
}

fn print_reference_chunk(chunk: &dino_seq::FastaReferenceChunk) {
    println!(
        "{}\t{}\t{}",
        String::from_utf8_lossy(&chunk.name),
        chunk.global_offset,
        String::from_utf8_lossy(&chunk.seq)
    );
}

fn verify_bgzf(args: Vec<String>) -> Result<()> {
    let path = one_path(args, "verify-bgzf")?;
    #[cfg(feature = "bgzf")]
    {
        let index = build_bgzf_index_strict(File::open(path)?)?;
        println!("status\tok");
        println!("blocks\t{}", index.len());
        println!("uncompressed_len\t{}", index.uncompressed_len());
        println!("compressed_len\t{}", index.compressed_len());
        Ok(())
    }
    #[cfg(not(feature = "bgzf"))]
    {
        let _ = path;
        Err(dino_seq::FastqError::Format(
            "verify-bgzf requires the bgzf feature".into(),
        ))
    }
}

fn print_stream_stats(stats: &StreamStats) {
    println!("records\t{}", stats.records);
    println!("bases\t{}", stats.bases);
    println!("qualities\t{}", stats.qualities);
    println!("name_bytes\t{}", stats.name_bytes);
    println!("checksum\t{}", stats.checksum);
}

fn one_path(args: Vec<String>, command: &str) -> Result<PathBuf> {
    if args.len() != 1 {
        return Err(dino_seq::FastqError::Format(format!(
            "{command} requires exactly one path argument"
        )));
    }
    Ok(PathBuf::from(&args[0]))
}

fn required_path(path: Option<PathBuf>, command: &str) -> Result<PathBuf> {
    path.ok_or_else(|| dino_seq::FastqError::Format(format!("{command} requires a path")))
}

fn required_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> Result<String> {
    iter.next()
        .ok_or_else(|| dino_seq::FastqError::Format(format!("{flag} requires a value")))
}

fn parse_u64_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> Result<u64> {
    let value = required_value(iter, flag)?;
    value
        .parse::<u64>()
        .map_err(|_| dino_seq::FastqError::Format(format!("{flag} requires an integer value")))
}

fn parse_usize_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> Result<usize> {
    let value = required_value(iter, flag)?;
    value
        .parse::<usize>()
        .map_err(|_| dino_seq::FastqError::Format(format!("{flag} requires an integer value")))
}

fn print_help() {
    eprintln!(
        "dino_seq <command>\n\ncommands:\n  stats [--format fastq|fasta] PATH\n  checksum --format fastq|fasta [PATH|-]\n  fasta-index PATH\n  fasta-fetch PATH --fai PATH.fai --name REF --start N --end N\n  fasta-partitions PATH --fai PATH.fai --parts N --overlap N\n  fasta-chunks PATH --fai PATH.fai --name REF --start N --end N --chunk-bases N\n  verify-bgzf PATH"
    );
}