slow5lib 0.1.0

Rust re-implementation of slow5lib: read and write SLOW5/BLOW5 nanopore sequencing files
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
/// Live parity tests against slow5tools.
///
/// These tests run slow5tools as a subprocess and compare its output against
/// the Rust reader/writer. They skip automatically when either slow5tools is
/// not on PATH or the large test file is absent.
///
/// Run with: cargo test --test c_parity
use std::collections::HashMap;
use std::process::Command;

use slow5lib::aux::{AuxMeta, AuxValue};
use slow5lib::header::{Header, ReadGroup, RecordCompression, SignalCompression};
use slow5lib::record::Record;
use slow5lib::{Slow5IndexedReader, Slow5Reader, Slow5Writer};

const SMALL_BLOW5: &str = "tests/data/small.blow5";
const PARITY_INPUT: &str = "tests/data/parity_input.slow5";

// ── Skip guards ───────────────────────────────────────────────────────────────

fn find_slow5tools() -> Option<String> {
    let ok = Command::new("slow5tools")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if ok {
        Some("slow5tools".to_string())
    } else {
        None
    }
}

fn skip_unless_slow5tools() -> Option<String> {
    match find_slow5tools() {
        Some(p) => Some(p),
        None => {
            eprintln!("c_parity: slow5tools not on PATH -- skipping");
            None
        }
    }
}

fn skip_unless_slow5tools_zstd() -> Option<String> {
    let st = skip_unless_slow5tools()?;
    // Write a tiny zstd BLOW5 and ask slow5tools to read it; if it errors with
    // "not compiled with zstd support" we skip rather than fail.
    let tmp = "/tmp/slow5lib_probe_zstd.blow5";
    let records = read_parity_input();
    write_blow5(
        tmp,
        &records[..1],
        RecordCompression::Zstd,
        SignalCompression::SvbZd,
    );
    let out = Command::new(&st)
        .args(["view", "--to", "slow5", tmp])
        .output()
        .expect("spawn slow5tools view");
    if !out.status.success() {
        let stderr = String::from_utf8_lossy(&out.stderr);
        if stderr.contains("not been compiled with zstd") {
            eprintln!("c_parity: slow5tools lacks zstd support -- skipping zstd test");
            return None;
        }
    }
    Some(st)
}

fn skip_unless_small_blow5() -> bool {
    if std::path::Path::new(SMALL_BLOW5).exists() {
        true
    } else {
        eprintln!("c_parity: {SMALL_BLOW5} not present -- skipping");
        false
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn read_parity_input() -> Vec<Record> {
    let mut r = Slow5Reader::open(PARITY_INPUT).expect("open parity_input.slow5");
    r.records().map(|rec| rec.expect("parity record")).collect()
}

fn write_blow5(path: &str, records: &[Record], rec: RecordCompression, sig: SignalCompression) {
    let header = Header {
        version: (0, 2, 0),
        num_read_groups: 1,
        record_compression: rec,
        signal_compression: sig,
        read_groups: vec![ReadGroup::default()],
        aux_meta: AuxMeta::default(),
    };
    let mut writer =
        Slow5Writer::create(path, header).unwrap_or_else(|e| panic!("create {path}: {e}"));
    for r in records {
        writer
            .write(r)
            .unwrap_or_else(|e| panic!("write {path}: {e}"));
    }
    writer
        .finish()
        .unwrap_or_else(|e| panic!("finish {path}: {e}"));
}

/// Compare two AuxValues for approximate equality.
///
/// Integer and string types use exact equality. Floats use a relative epsilon
/// because the SLOW5 text format represents floats with limited precision (C's
/// default %g gives 6 significant digits), so parsing the text value and the
/// binary value may differ slightly.
fn aux_approx_eq(a: &AuxValue, b: &AuxValue) -> bool {
    match (a, b) {
        (AuxValue::Float(x), AuxValue::Float(y)) => {
            if x == y {
                return true;
            }
            let max = x.abs().max(y.abs());
            if max == 0.0 {
                return true;
            }
            ((x - y) / max).abs() < 1e-4
        }
        (AuxValue::Double(x), AuxValue::Double(y)) => {
            if x == y {
                return true;
            }
            let max = x.abs().max(y.abs());
            if max == 0.0 {
                return true;
            }
            ((x - y) / max).abs() < 1e-6
        }
        _ => a == b,
    }
}

// ── Zstd parity ───────────────────────────────────────────────────────────────

/// slow5tools can extract reads from small.blow5 and the Rust reader produces
/// matching signals for the same read IDs.
///
/// Spot-checks 5 reads to keep the test fast.
#[test]
fn c_parity_slow5tools_output_matches_rust_reader() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };
    if !skip_unless_small_blow5() {
        return;
    }

    let reader = Slow5IndexedReader::open(SMALL_BLOW5).expect("open small.blow5");
    let ids: Vec<String> = reader.read_ids().take(5).map(str::to_string).collect();
    assert!(!ids.is_empty(), "small.blow5 has no reads");

    let tmp = "/tmp/slow5lib_c_parity_get.slow5";
    let status = Command::new(&slow5tools)
        .args(["get", "--to", "slow5", "-o", tmp])
        .arg(SMALL_BLOW5)
        .args(&ids)
        .status()
        .expect("spawn slow5tools get");
    assert!(
        status.success(),
        "slow5tools get exited with status {status}"
    );

    let mut text_reader = Slow5Reader::open(tmp).expect("open slow5tools output");
    let extracted: Vec<_> = text_reader
        .records()
        .map(|r| r.expect("record from slow5tools output"))
        .collect();

    assert_eq!(extracted.len(), ids.len());

    for rec in &extracted {
        let from_rust = reader
            .get(&rec.read_id)
            .unwrap_or_else(|e| panic!("rust get {}: {e}", rec.read_id));
        assert_eq!(
            from_rust.raw_signal, rec.raw_signal,
            "signal mismatch for {}",
            rec.read_id
        );
    }
}

/// slow5tools can read a zstd+svbzd BLOW5 produced by the Rust writer.
#[test]
fn c_parity_zstd_rust_writer_readable_by_slow5tools() {
    let Some(slow5tools) = skip_unless_slow5tools_zstd() else {
        return;
    };

    let records = read_parity_input();
    let tmp = "/tmp/slow5lib_c_parity_zstd.blow5";
    write_blow5(
        tmp,
        &records,
        RecordCompression::Zstd,
        SignalCompression::SvbZd,
    );

    let output = Command::new(&slow5tools)
        .args(["view", "--to", "slow5", tmp])
        .output()
        .expect("spawn slow5tools view");

    assert!(
        output.status.success(),
        "slow5tools view (zstd) failed:\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let data_lines = stdout
        .lines()
        .filter(|l| !l.starts_with('#') && !l.starts_with('@') && !l.is_empty())
        .count();
    assert_eq!(
        data_lines,
        records.len(),
        "zstd: wrong data line count from slow5tools"
    );
}

// ── Zlib parity ───────────────────────────────────────────────────────────────

/// Rust reader can read a C-generated zlib BLOW5.
///
/// slow5tools converts parity_input.slow5 to zlib+svb-zd BLOW5 in-test.
/// Rust reads it back and verifies signal values match the source.
#[test]
fn c_parity_zlib_c_generated_readable() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };

    let tmp = "/tmp/slow5lib_c_parity_zlib_gen.blow5";
    let status = Command::new(&slow5tools)
        .args([
            "view", "--to", "blow5", "-c", "zlib", "-s", "svb-zd", "-o", tmp,
        ])
        .arg(PARITY_INPUT)
        .status()
        .expect("spawn slow5tools view (convert to zlib)");
    assert!(status.success(), "slow5tools convert to zlib failed");

    let source = read_parity_input();
    let mut rust_reader = Slow5Reader::open(tmp).expect("open zlib blow5");
    let from_zlib: Vec<_> = rust_reader
        .records()
        .map(|r| r.expect("zlib record"))
        .collect();

    assert_eq!(from_zlib.len(), source.len(), "zlib: record count mismatch");
    for (i, (got, want)) in from_zlib.iter().zip(source.iter()).enumerate() {
        assert_eq!(got.read_id, want.read_id, "record {i}: read_id mismatch");
        assert_eq!(
            got.raw_signal, want.raw_signal,
            "record {i}: signal mismatch"
        );
    }
}

/// slow5tools can read a zlib+svb-zd BLOW5 produced by the Rust writer.
#[test]
fn c_parity_zlib_rust_writer_readable_by_slow5tools() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };

    let records = read_parity_input();
    let tmp = "/tmp/slow5lib_c_parity_zlib_rust.blow5";
    write_blow5(
        tmp,
        &records,
        RecordCompression::Zlib,
        SignalCompression::SvbZd,
    );

    let output = Command::new(&slow5tools)
        .args(["view", "--to", "slow5", tmp])
        .output()
        .expect("spawn slow5tools view (zlib)");

    assert!(
        output.status.success(),
        "slow5tools view (zlib) failed:\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let data_lines = stdout
        .lines()
        .filter(|l| !l.starts_with('#') && !l.starts_with('@') && !l.is_empty())
        .count();
    assert_eq!(
        data_lines,
        records.len(),
        "zlib: wrong data line count from slow5tools"
    );
}

/// slow5tools view output (SLOW5 text) and the BLOW5 indexed reader produce
/// identical aux field values for the same reads.
///
/// Uses small.blow5 which has a rich aux field schema from a real PromethION run.
/// Extracts 3 reads with slow5tools get, parses both sources with the Rust
/// reader/indexed-reader, and compares every aux field value by name.
#[test]
fn c_parity_aux_fields_blow5_vs_slow5_text() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };
    if !skip_unless_small_blow5() {
        return;
    }

    let blow5 = Slow5IndexedReader::open(SMALL_BLOW5).expect("open small.blow5");
    let ids: Vec<String> = blow5.read_ids().take(3).map(str::to_string).collect();
    assert!(!ids.is_empty(), "small.blow5 has no reads");

    let tmp = "/tmp/slow5lib_c_parity_aux.slow5";
    let status = Command::new(&slow5tools)
        .args(["get", "--to", "slow5", "-o", tmp])
        .arg(SMALL_BLOW5)
        .args(&ids)
        .status()
        .expect("spawn slow5tools get");
    assert!(status.success(), "slow5tools get (aux) exited {status}");

    // Index the SLOW5 text records by read_id.
    let mut text_reader = Slow5Reader::open(tmp).expect("open slow5tools output");
    let text_by_id: HashMap<String, Record> = text_reader
        .records()
        .map(|r| {
            let rec = r.expect("aux text record");
            (rec.read_id.clone(), rec)
        })
        .collect();

    let aux_names = blow5.header().aux_meta.names.clone();
    assert!(!aux_names.is_empty(), "small.blow5 has no aux fields");

    for id in &ids {
        let from_blow5 = blow5
            .get(id)
            .unwrap_or_else(|e| panic!("indexed get {id}: {e}"));
        let from_text = text_by_id
            .get(id.as_str())
            .unwrap_or_else(|| panic!("id {id} not in slow5tools output"));

        for name in &aux_names {
            let v_blow5 = from_blow5
                .aux
                .get(name.as_str())
                .cloned()
                .unwrap_or(AuxValue::Missing);
            let v_text = from_text
                .aux
                .get(name.as_str())
                .cloned()
                .unwrap_or(AuxValue::Missing);

            assert!(
                aux_approx_eq(&v_blow5, &v_text),
                "read {id} aux field {name}: blow5={v_blow5:?} text={v_text:?}"
            );
        }
    }
}

// ── ex-zd parity ──────────────────────────────────────────────────────────────

/// Rust reader can read a C-generated ex-zd BLOW5.
///
/// slow5tools converts parity_input.slow5 to none+ex-zd BLOW5 in-test.
/// Rust reads it back and verifies signal values match the source.
#[test]
fn c_parity_exzd_c_generated_readable() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };

    let tmp = "/tmp/slow5lib_c_parity_exzd_gen.blow5";
    let status = Command::new(&slow5tools)
        .args([
            "view", "--to", "blow5", "-c", "none", "-s", "ex-zd", "-o", tmp,
        ])
        .arg(PARITY_INPUT)
        .status()
        .expect("spawn slow5tools view (convert to ex-zd)");
    assert!(status.success(), "slow5tools convert to ex-zd failed");

    let source = read_parity_input();
    let mut rust_reader = Slow5Reader::open(tmp).expect("open ex-zd blow5");
    let from_exzd: Vec<_> = rust_reader
        .records()
        .map(|r| r.expect("ex-zd record"))
        .collect();

    assert_eq!(
        from_exzd.len(),
        source.len(),
        "ex-zd: record count mismatch"
    );
    for (i, (got, want)) in from_exzd.iter().zip(source.iter()).enumerate() {
        assert_eq!(got.read_id, want.read_id, "record {i}: read_id mismatch");
        assert_eq!(
            got.raw_signal, want.raw_signal,
            "record {i}: signal mismatch"
        );
    }
}

/// slow5tools can read a zstd+ex-zd BLOW5 produced by the Rust writer.
#[test]
fn c_parity_exzd_rust_writer_readable_by_slow5tools() {
    let Some(slow5tools) = skip_unless_slow5tools_zstd() else {
        return;
    };

    let records = read_parity_input();
    let tmp = "/tmp/slow5lib_c_parity_exzd_rust.blow5";
    write_blow5(
        tmp,
        &records,
        RecordCompression::Zstd,
        SignalCompression::ExZd,
    );

    let output = Command::new(&slow5tools)
        .args(["view", "--to", "slow5", tmp])
        .output()
        .expect("spawn slow5tools view");

    assert!(
        output.status.success(),
        "slow5tools view (ex-zd) failed:\nstderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut lines = stdout
        .lines()
        .filter(|l| !l.starts_with('#') && !l.starts_with('@') && !l.is_empty());
    let data_lines: Vec<&str> = lines.by_ref().collect();
    assert_eq!(
        data_lines.len(),
        records.len(),
        "ex-zd: wrong data line count from slow5tools"
    );

    // Also check the signal column values match, not just the line count.
    for (line, want) in data_lines.iter().zip(records.iter()) {
        let cols: Vec<&str> = line.split('\t').collect();
        let read_id = cols[0];
        let raw_signal_str = cols[cols.len() - 1];
        let got_signal: Vec<i16> = if raw_signal_str.is_empty() {
            Vec::new()
        } else {
            raw_signal_str
                .split(',')
                .map(|s| s.parse().expect("parse signal sample"))
                .collect()
        };
        assert_eq!(read_id, want.read_id, "ex-zd: read_id mismatch");
        assert_eq!(
            got_signal, want.raw_signal,
            "ex-zd: signal mismatch for {read_id}"
        );
    }
}

// ── Smoke test ────────────────────────────────────────────────────────────────

/// slow5tools view on small.blow5 exits 0.
#[test]
fn c_parity_small_blow5_accepted_by_slow5tools() {
    let Some(slow5tools) = skip_unless_slow5tools() else {
        return;
    };
    if !skip_unless_small_blow5() {
        return;
    }

    let output = Command::new(&slow5tools)
        .args(["view", "--to", "slow5", SMALL_BLOW5])
        .output()
        .expect("spawn slow5tools view");

    assert!(
        output.status.success(),
        "slow5tools view exited {}: {}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
}