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
//! Tests for the `fastx` binary, driven through the real executable.

use std::fs;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};

const EXE: &str = env!("CARGO_BIN_EXE_fastx");

const FASTQ: &[u8] = b"@read1 sample\nACGTACGTAC\n+\n@@++IIIIII\n\
@read2 sample\nNNNNACGTAC\n+\n!!!!IIIIII\n\
@read3\nACGT\n+\nIIII\n";

const FASTA: &[u8] = b">contig1 first\nACGTACGTAC\nGGGG\n\
>contig2\nATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG\n";

struct Scratch(PathBuf);

impl Scratch {
    fn new(tag: &str) -> Scratch {
        let dir = std::env::temp_dir().join(format!("fastx-cli-{}-{tag}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        Scratch(dir)
    }

    fn file(&self, name: &str, contents: &[u8]) -> PathBuf {
        let path = self.0.join(name);
        fs::write(&path, contents).unwrap();
        path
    }

    fn path(&self, name: &str) -> PathBuf {
        self.0.join(name)
    }
}

impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

fn run(args: &[&str]) -> Output {
    Command::new(EXE).args(args).output().expect("run fastx")
}

fn run_stdin(args: &[&str], input: &[u8]) -> Output {
    use std::io::Write;
    let mut child = Command::new(EXE)
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn fastx");
    child.stdin.as_mut().unwrap().write_all(input).unwrap();
    child.wait_with_output().expect("wait for fastx")
}

fn stdout(output: &Output) -> String {
    assert!(
        output.status.success(),
        "command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).replace("\r\n", "\n")
}

#[test]
fn help_and_version() {
    assert!(stdout(&run(&["help"])).contains("fast FASTA/FASTQ toolkit"));
    assert!(stdout(&run(&[])).contains("USAGE"));
    assert!(stdout(&run(&["--version"])).starts_with("fastx 0."));
}

#[test]
fn unknown_command_fails() {
    let output = run(&["frobnicate"]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("unknown command"));
}

#[test]
fn stats_reports_counts() {
    let scratch = Scratch::new("stats");
    let path = scratch.file("reads.fq", FASTQ);
    let text = stdout(&run(&["stats", path.to_str().unwrap()]));
    assert!(text.contains("records      3"), "{text}");
    assert!(text.contains("total bases  24"), "{text}");
    assert!(text.contains("Q30%"), "{text}");
}

#[test]
fn convert_fastq_to_fasta() {
    let scratch = Scratch::new("convert");
    let input = scratch.file("reads.fq", FASTQ);
    let output = scratch.path("reads.fa");
    let result = run(&[
        "convert",
        input.to_str().unwrap(),
        "-o",
        output.to_str().unwrap(),
        "-w",
        "8",
    ]);
    assert!(
        result.status.success(),
        "{}",
        String::from_utf8_lossy(&result.stderr)
    );

    let text = fs::read_to_string(&output).unwrap();
    assert_eq!(
        text.replace("\r\n", "\n"),
        ">read1 sample\nACGTACGT\nAC\n>read2 sample\nNNNNACGT\nAC\n>read3\nACGT\n"
    );
}

#[cfg(feature = "gzip")]
#[test]
fn convert_writes_gzip() {
    let scratch = Scratch::new("gzip");
    let input = scratch.file("reads.fq", FASTQ);
    let output = scratch.path("reads.fq.gz");
    assert!(run(&[
        "convert",
        input.to_str().unwrap(),
        "-o",
        output.to_str().unwrap()
    ])
    .status
    .success());

    assert_eq!(&fs::read(&output).unwrap()[..2], &[0x1f, 0x8b]);
    // And it reads back through the same tool.
    let text = stdout(&run(&["stats", output.to_str().unwrap()]));
    assert!(text.contains("records      3"), "{text}");
}

#[cfg(feature = "gzip")]
#[test]
fn gzip_level_is_configurable() {
    let scratch = Scratch::new("level");
    let input = scratch.file("reads.fq", FASTQ);

    let mut sizes = Vec::new();
    for level in ["0", "1", "9"] {
        let output = scratch.path(&format!("L{level}.fq.gz"));
        let result = run(&[
            "convert",
            input.to_str().unwrap(),
            "-o",
            output.to_str().unwrap(),
            "-l",
            level,
        ]);
        assert!(
            result.status.success(),
            "{}",
            String::from_utf8_lossy(&result.stderr)
        );
        let bytes = fs::read(&output).unwrap();
        assert_eq!(&bytes[..2], &[0x1f, 0x8b], "level {level} is not gzip");
        sizes.push(bytes.len());
    }
    // Level 0 stores the data, so it must be the largest of the three.
    assert!(sizes[0] > sizes[2], "{sizes:?}");

    // Out-of-range levels are rejected rather than silently clamped.
    let bad = run(&["convert", input.to_str().unwrap(), "-l", "42"]);
    assert!(!bad.status.success());
    assert!(String::from_utf8_lossy(&bad.stderr).contains("between 0 and 9"));
}

#[cfg(feature = "gzip")]
#[test]
fn compressed_output_is_bgzf_and_indexable() {
    let scratch = Scratch::new("bgzf");
    let source = scratch.file("source.fa", FASTA);

    // Write the same content twice, wrapped identically, so that the only
    // difference between the two files is the compression.
    let plain = scratch.path("ref.fa");
    let compressed = scratch.path("ref.fa.gz");
    for target in [&plain, &compressed] {
        let result = run(&[
            "convert",
            source.to_str().unwrap(),
            "-o",
            target.to_str().unwrap(),
            "-w",
            "12",
        ]);
        assert!(
            result.status.success(),
            "{}",
            String::from_utf8_lossy(&result.stderr)
        );
    }

    // Compressed output defaults to BGZF, so it is seekable rather than merely
    // compressed — and still plain gzip as far as any other tool is concerned.
    let bytes = fs::read(&compressed).unwrap();
    assert!(fastx::bgzf::is_bgzf(&bytes), "output is not BGZF");
    assert!(bytes.ends_with(&fastx::bgzf::EOF_BLOCK), "no EOF marker");

    // faidx writes both indices for a BGZF file, and only the .fai for a plain one.
    for target in [&plain, &compressed] {
        let result = run(&["faidx", target.to_str().unwrap()]);
        assert!(
            result.status.success(),
            "{}",
            String::from_utf8_lossy(&result.stderr)
        );
    }
    assert!(scratch.path("ref.fa.gz.fai").exists());
    assert!(scratch.path("ref.fa.gz.gzi").exists());
    assert!(scratch.path("ref.fa.fai").exists());
    assert!(!scratch.path("ref.fa.gzi").exists());

    // The .fai is byte-identical for the two, because its offsets are positions
    // in the uncompressed data either way. That is what makes one `.fai` usable
    // with samtools whether or not the reference is compressed.
    assert_eq!(
        fs::read(scratch.path("ref.fa.gz.fai")).unwrap(),
        fs::read(scratch.path("ref.fa.fai")).unwrap()
    );

    // And regions come back identically from both.
    for region in ["contig1:1-12", "contig1:9-14", "contig2", "contig2:13-25"] {
        assert_eq!(
            stdout(&run(&["faidx", compressed.to_str().unwrap(), region])),
            stdout(&run(&["faidx", plain.to_str().unwrap(), region])),
            "{region}"
        );
    }
}

#[test]
fn head_limits_records() {
    let scratch = Scratch::new("head");
    let path = scratch.file("reads.fq", FASTQ);
    let text = stdout(&run(&["head", "-n", "2", path.to_str().unwrap()]));
    assert_eq!(text.matches('@').count(), 4); // two headers + two '@' quality chars
    assert!(text.contains("@read1"));
    assert!(text.contains("@read2"));
    assert!(!text.contains("@read3"));
}

#[test]
fn filter_by_length_and_ambiguity() {
    let scratch = Scratch::new("filter");
    let path = scratch.file("reads.fq", FASTQ);

    let text = stdout(&run(&["filter", "--min-len", "5", path.to_str().unwrap()]));
    assert!(text.contains("read1") && text.contains("read2") && !text.contains("read3"));

    let text = stdout(&run(&["filter", "--max-n", "0", path.to_str().unwrap()]));
    assert!(text.contains("read1") && !text.contains("read2"));

    // -v inverts the selection.
    let text = stdout(&run(&[
        "filter",
        "--max-n",
        "0",
        "-v",
        path.to_str().unwrap(),
    ]));
    assert!(!text.contains("read1") && text.contains("read2"));
}

#[test]
fn reverse_complement_through_a_pipe() {
    let text = stdout(&run_stdin(&["rc", "-t", "fastq"], FASTQ));
    // The first read's sequence is reversed and complemented, and so is quality.
    assert!(text.contains("GTACGTACGT"), "{text}");
    assert!(text.contains("IIIIII++@@"), "{text}");
}

#[test]
fn translate_uses_the_standard_code() {
    let scratch = Scratch::new("translate");
    let path = scratch.file("contigs.fa", FASTA);
    let text = stdout(&run(&[
        "translate",
        "--stop-at-stop",
        "-w",
        "0",
        path.to_str().unwrap(),
    ]));
    assert!(text.contains("\nMAIVMGR\n"), "{text}");
}

#[test]
fn faidx_builds_and_queries() {
    let scratch = Scratch::new("faidx");
    let path = scratch.file("ref.fa", FASTA);

    assert!(run(&["faidx", path.to_str().unwrap()]).status.success());
    let fai = fs::read_to_string(scratch.path("ref.fa.fai")).unwrap();
    assert!(fai.starts_with("contig1\t14\t"), "{fai}");

    let text = stdout(&run(&[
        "faidx",
        path.to_str().unwrap(),
        "contig1:1-12",
        "contig2",
    ]));
    assert!(text.contains(">contig1:1-12\nACGTACGTACGG\n"), "{text}");
    assert!(text.contains(">contig2\n"), "{text}");
}

#[test]
fn faidx_rejects_bad_regions() {
    let scratch = Scratch::new("faidx-bad");
    let path = scratch.file("ref.fa", FASTA);
    let output = run(&["faidx", path.to_str().unwrap(), "contig1:1-9999"]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("out of bounds"));
}

const R1: &[u8] = b"@read1/1\nACGTACGTAC\n+\n@@++IIIIII\n\
@read2/1\nTTTTTTTTTT\n+\nIIIIIIIIII\n\
@read3/1\nAAAA\n+\nIIII\n";

const R2: &[u8] = b"@read1/2\nCCCCCCCCCC\n+\nIIIIIIIIII\n\
@read2/2\nGGGGGGGGGG\n+\nIIIIIIIIII\n\
@read3/2\nTTTT\n+\nIIII\n";

#[test]
fn stats_json_is_one_object_per_line() {
    let scratch = Scratch::new("json");
    let path = scratch.file("reads.fq", FASTQ);
    let text = stdout(&run(&["stats", "--json", path.to_str().unwrap()]));

    let lines: Vec<&str> = text.lines().filter(|l| !l.is_empty()).collect();
    assert_eq!(lines.len(), 1, "one input should give one line: {text}");
    let line = lines[0];
    assert!(line.starts_with('{') && line.ends_with('}'), "{line}");
    assert!(line.contains("\"records\":3"), "{line}");
    // FASTA-only figures must be null rather than missing.
    assert!(line.contains("\"quality_bases\":24"), "{line}");

    // Several inputs give several lines, plus a total.
    let second = scratch.file("more.fq", FASTQ);
    let text = stdout(&run(&[
        "stats",
        "--json",
        path.to_str().unwrap(),
        second.to_str().unwrap(),
    ]));
    let lines: Vec<&str> = text.lines().filter(|l| !l.is_empty()).collect();
    assert_eq!(lines.len(), 3, "{text}");
    assert!(lines[2].contains("\"file\":\"total\""), "{}", lines[2]);
    assert!(lines[2].contains("\"records\":6"), "{}", lines[2]);
}

#[test]
fn interleave_and_deinterleave_round_trip() {
    let scratch = Scratch::new("pairs");
    let first = scratch.file("R1.fq", R1);
    let second = scratch.file("R2.fq", R2);
    let both = scratch.path("both.fq");

    let result = run(&[
        "interleave",
        first.to_str().unwrap(),
        second.to_str().unwrap(),
        "-o",
        both.to_str().unwrap(),
    ]);
    assert!(
        result.status.success(),
        "{}",
        String::from_utf8_lossy(&result.stderr)
    );
    let merged = fs::read_to_string(&both).unwrap().replace("\r\n", "\n");
    assert!(merged.starts_with("@read1/1\n"), "{merged}");
    assert_eq!(merged.matches("@read").count(), 6);
    // Mates must alternate.
    let ids: Vec<&str> = merged
        .lines()
        .filter(|l| l.starts_with("@read"))
        .map(|l| &l[1..])
        .collect();
    assert_eq!(
        ids,
        ["read1/1", "read1/2", "read2/1", "read2/2", "read3/1", "read3/2"]
    );

    let back1 = scratch.path("back1.fq");
    let back2 = scratch.path("back2.fq");
    let result = run(&[
        "deinterleave",
        both.to_str().unwrap(),
        "--out1",
        back1.to_str().unwrap(),
        "--out2",
        back2.to_str().unwrap(),
    ]);
    assert!(
        result.status.success(),
        "{}",
        String::from_utf8_lossy(&result.stderr)
    );
    assert_eq!(fs::read(&back1).unwrap(), R1);
    assert_eq!(fs::read(&back2).unwrap(), R2);
}

#[test]
fn mispaired_files_are_rejected() {
    let scratch = Scratch::new("mispair");
    let first = scratch.file("R1.fq", R1);
    // The same three records as R2, in a different order: the classic silent
    // pipeline bug, and the reason the name check exists.
    let shuffled = scratch.file(
        "R2.fq",
        b"@read3/2\nTTTT\n+\nIIII\n\
          @read2/2\nGGGGGGGGGG\n+\nIIIIIIIIII\n\
          @read1/2\nCCCCCCCCCC\n+\nIIIIIIIIII\n",
    );

    let output = run(&[
        "interleave",
        first.to_str().unwrap(),
        shuffled.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("mispaired"), "{stderr}");

    // And the check can be waived deliberately.
    assert!(run(&[
        "interleave",
        first.to_str().unwrap(),
        shuffled.to_str().unwrap(),
        "--no-check-names",
    ])
    .status
    .success());
}

#[test]
fn sample_is_reproducible() {
    let scratch = Scratch::new("sample");
    let path = scratch.file("reads.fq", R1);

    let once = stdout(&run(&[
        "sample",
        "-n",
        "2",
        "--seed",
        "42",
        path.to_str().unwrap(),
    ]));
    let twice = stdout(&run(&[
        "sample",
        "-n",
        "2",
        "--seed",
        "42",
        path.to_str().unwrap(),
    ]));
    assert_eq!(once, twice, "same seed must give the same subset");
    assert_eq!(once.matches("@read").count(), 2);

    // A fraction of 1.0 keeps everything, 0.0 nothing.
    assert_eq!(
        stdout(&run(&[
            "sample",
            "--fraction",
            "1.0",
            path.to_str().unwrap()
        ]))
        .matches("@read")
        .count(),
        3
    );
    assert!(stdout(&run(&[
        "sample",
        "--fraction",
        "0.0",
        path.to_str().unwrap()
    ]))
    .is_empty());

    // Exactly one of -n and --fraction is required.
    assert!(!run(&["sample", path.to_str().unwrap()]).status.success());
    assert!(!run(&[
        "sample",
        "-n",
        "1",
        "--fraction",
        "0.5",
        path.to_str().unwrap()
    ])
    .status
    .success());
}

#[test]
fn dedup_drops_repeats() {
    let scratch = Scratch::new("dedup");
    let mut doubled = R1.to_vec();
    doubled.extend_from_slice(R1);
    let path = scratch.file("reads.fq", &doubled);

    let text = stdout(&run(&["dedup", path.to_str().unwrap()]));
    assert_eq!(text.matches("@read").count(), 3, "{text}");

    // By sequence, two records with different names but the same bases collapse.
    let same_seq = scratch.file(
        "same.fq",
        b"@a\nACGT\n+\nIIII\n@b\nACGT\n+\nJJJJ\n@c\nTTTT\n+\nIIII\n",
    );
    let text = stdout(&run(&["dedup", "--by-seq", same_seq.to_str().unwrap()]));
    assert_eq!(text.matches('@').count(), 2, "{text}");
}

#[test]
fn malformed_input_exits_nonzero() {
    let scratch = Scratch::new("bad");
    let path = scratch.file("bad.fa", b"this is not a sequence file\n");
    let output = run(&["stats", path.to_str().unwrap()]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("parse error"));
}