slow5 0.12.1

Library for interacting with slow5
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
use std::{
    ffi::{CStr, CString},
    marker::PhantomData,
    mem::size_of,
    os::unix::prelude::OsStrExt,
    path::Path,
};

use cstr::cstr;
use libc::{c_char, c_void};
use slow5lib_sys::{
    slow5_file_t, slow5_get, slow5_get_aux_enum_labels, slow5_get_hdr_keys, slow5_get_rids,
    slow5_hdr_t, slow5_rec_t,
};

use crate::{
    error::Slow5Error,
    header::HeaderExt,
    record::{Record, RecordIter},
    to_cstring, Header, RecordCompression, SignalCompression,
};

/// Read from a SLOW5 file
pub struct FileReader {
    pub(crate) slow5_file: *mut slow5_file_t,
}

unsafe impl Send for FileReader {}

impl std::fmt::Debug for FileReader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FileReader").finish()
    }
}

impl FileReader {
    fn new(slow5_file: *mut slow5_file_t) -> Self {
        Self { slow5_file }
    }

    /// Open a SLOW5 file, creates an index if one doesn't exist.
    ///
    /// # Example
    /// ```
    /// use slow5::FileReader;
    ///
    /// # fn main() -> anyhow::Result<()> {
    /// let reader = FileReader::open("examples/example.slow5")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn open<P: AsRef<Path>>(file_path: P) -> Result<Self, Slow5Error> {
        // If we aren't testing or running in debug mode, silence slow5lib logs
        #[cfg(any(not(test), not(debug_assertions)))]
        unsafe {
            slow5lib_sys::slow5_set_log_level(slow5lib_sys::slow5_log_level_opt_SLOW5_LOG_OFF);
        }
        let file_path = file_path.as_ref();
        if !file_path.exists() {
            log::error!("File path doesn't exist: {file_path:?}");
            return Err(Slow5Error::IncorrectPath(file_path.to_owned()));
        }

        let file_path = file_path.as_os_str().as_bytes();
        let file_path = to_cstring(file_path)?;
        let mode = cstr!("r");
        let slow5_file: *mut slow5_file_t =
            unsafe { slow5lib_sys::slow5_open(file_path.as_ptr(), mode.as_ptr()) };
        let ret = unsafe { slow5lib_sys::slow5_idx_load(slow5_file) };
        if ret == -1 {
            log::error!("No index was loaded");
            Err(Slow5Error::NoIndex)
        } else {
            Ok(FileReader::new(slow5_file))
        }
    }

    /// Get file's record compression
    pub fn record_compression(&self) -> RecordCompression {
        let compress = unsafe { (*self.slow5_file).compress };
        if compress.is_null() {
            return RecordCompression::None;
        }
        let record_press = unsafe { (*(*compress).record_press).method };
        RecordCompression::from_u32(record_press)
    }

    /// Get file's signal compression
    pub fn signal_compression(&self) -> SignalCompression {
        let compress = unsafe { (*self.slow5_file).compress };
        if compress.is_null() {
            return SignalCompression::None;
        }
        let signal_press = unsafe { (*(*compress).signal_press).method };
        SignalCompression::from_u32(signal_press)
    }

    /// Access header of a SLOW5 file
    pub fn header(&self) -> Header<'_> {
        let header: *mut slow5_hdr_t = unsafe { (*self.slow5_file).header };
        Header::new(header)
    }

    /// Return iterator over each read in a SLOW5 file as a [`RecordIter`].
    ///
    /// # Example
    /// ```
    /// # use slow5::FileReader;
    /// use slow5::RecordExt;
    ///
    /// # fn main() -> anyhow::Result<()> {
    /// # let mut reader = FileReader::open("examples/example.slow5")?;
    /// for record in reader.records() {
    ///     println!("{:?}", record?.read_id());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    // TODO I could avoid the reader being consumed by using rewinding the file ptr
    // and making it &mut self. RecordIter would need to do the rewinding once its
    // finished.
    pub fn records(&mut self) -> RecordIter {
        RecordIter::new(self)
    }

    /// Random-access a single [`Record`] by read_id.
    ///
    /// # Example
    /// ```
    /// # use slow5::FileReader;
    /// use slow5::RecordExt;
    ///
    /// # fn main() -> anyhow::Result<()> {
    /// # let reader = FileReader::open("examples/example.slow5")?;
    /// let read_id: &[u8] = b"r3";
    /// let record = reader.get_record(read_id)?;
    /// assert_eq!(record.read_id(), read_id);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Mutating the Record will not cause changes in the SLOW5 file.
    pub fn get_record<B>(&self, read_id: B) -> Result<Record, Slow5Error>
    where
        B: Into<Vec<u8>>,
    {
        let mut slow5_rec =
            unsafe { libc::calloc(1, size_of::<slow5_rec_t>()) as *mut slow5_rec_t };
        let read_id = to_cstring(read_id)?;
        let rid_ptr = read_id.into_raw();
        let ret = unsafe { slow5_get(rid_ptr, &mut slow5_rec, self.slow5_file) };
        let _ = unsafe { CString::from_raw(rid_ptr) };
        if ret >= 0 {
            Ok(Record::new(slow5_rec))
        } else {
            // TODO Handle error code properly
            unsafe { libc::free(slow5_rec as *mut c_void) };
            Err(Slow5Error::GetRecordFailed)
        }
    }

    /// Returns iterator over all the read ids in a SLOW5 file
    /// ```
    /// # use slow5::FileReader;
    /// use std::str;
    ///
    /// let slow5 = FileReader::open("examples/example.slow5").unwrap();
    /// # let mut read_ids = Vec::new();
    /// let read_id_iter = slow5.iter_read_ids().unwrap();
    /// for rid in read_id_iter {
    ///     println!("{}", str::from_utf8(rid).unwrap());
    /// #   read_ids.push(rid);
    /// }
    /// # assert_eq!(read_ids.len(), 5);
    /// # assert_eq!(read_ids[0], b"r1");
    /// # assert_eq!(read_ids[1], b"r2");
    /// ```
    // TODO figure out how to seek back after
    // Records has to take ownership because the file pointer is changed during
    // iteration Maybe ideal to fseek + other with the fp after dropping the
    // RecordIter
    pub fn iter_read_ids(&self) -> Result<ReadIdIter<'_>, Slow5Error> {
        ReadIdIter::new(self)
    }

    /// Returns iterator over the labels for an enum auxiliary field. Useful for
    /// converting into an indexable collection
    /// and using [`crate::EnumField`] to get the value for an auxiliary field
    ///
    /// # Errors
    /// Return Err if field's type is not an auxiliary enum or field is not
    /// within in the header. This can be due to the wrong name being passed as
    /// field, the field is not an enum field or more.
    ///
    /// # Example
    /// ```
    /// # use slow5::FileReader;
    /// # use slow5::EnumField;
    /// # fn main() -> anyhow::Result<()> {
    /// let mut reader = FileReader::open("examples/example3.blow5")?;
    /// let labels = reader
    ///     .iter_aux_enum_labels("end_reason")?
    ///     .map(|x| String::from_utf8(x.to_vec()))
    ///     .collect::<Result<Vec<_>, _>>()?;
    /// let rec = reader.get_record("0035aaf9-a746-4bbd-97c4-390ddc27c756")?;
    /// let EnumField(idx) = rec.get_aux_field("end_reason")?;
    /// assert_eq!(labels[idx], "unknown");
    /// # Ok(())
    /// # }
    /// ```
    pub fn iter_aux_enum_labels<B>(&self, field: B) -> Result<AuxEnumLabelIter, Slow5Error>
    where
        B: Into<Vec<u8>>,
    {
        let mut n = 0;
        let field = to_cstring(field)?;
        let label_ptr =
            unsafe { slow5_get_aux_enum_labels(self.header().header, field.as_ptr(), &mut n) };
        if label_ptr.is_null() {
            Err(Slow5Error::Unknown)
        } else {
            Ok(AuxEnumLabelIter::new(self, label_ptr, n))
        }
    }

    /// Returns iterator over attribute keys for all read groups
    ///
    /// # Example
    /// ```
    /// # use slow5::FileReader;
    /// # use slow5::EnumField;
    /// # use std::collections::HashSet;
    /// # fn main() -> anyhow::Result<()> {
    /// let mut reader = FileReader::open("examples/example3.blow5")?;
    /// let attr_keys = reader
    ///     .iter_attr_keys()?
    ///     .map(|attr| std::str::from_utf8(attr).unwrap())
    ///     .collect::<HashSet<&str>>();
    ///
    /// assert!(attr_keys.contains("file_version"));
    /// assert!(attr_keys.contains("asic_id"));
    /// assert!(!attr_keys.contains("random attributefdsafs"));
    /// # Ok(())
    /// # }
    /// ```
    pub fn iter_attr_keys(&self) -> Result<AttrKeysIter, Slow5Error> {
        let mut n = 0;
        let keys = unsafe { slow5_get_hdr_keys(self.header().header, &mut n) };
        if keys.is_null() {
            Err(Slow5Error::Unknown)
        } else {
            Ok(AttrKeysIter {
                _reader: self,
                keys,
                n_keys: n,
                idx: 0,
            })
        }
    }
}

impl HeaderExt for FileReader {
    fn header(&self) -> Header<'_> {
        self.header()
    }
}

impl Drop for FileReader {
    fn drop(&mut self) {
        unsafe {
            slow5lib_sys::slow5_close(self.slow5_file);
        }
    }
}

/// Iterator over all the read IDs in a SLOW5 file
// TODO Is liftime fine, or should I just hold the &'a FileReader?
pub struct ReadIdIter<'a> {
    idx: u64,
    num_reads: u64,
    read_id_ptr: *mut *mut c_char,
    _lifetime: PhantomData<&'a ()>,
}

impl<'a> std::fmt::Debug for ReadIdIter<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ReadIdIter")
            .field("idx", &self.idx)
            .field("num_reads", &self.num_reads)
            .finish()
    }
}

impl<'a> ReadIdIter<'a> {
    fn new(reader: &FileReader) -> Result<Self, Slow5Error> {
        let mut num_reads = 0;
        let rids = unsafe { slow5_get_rids(reader.slow5_file, &mut num_reads) };
        if rids.is_null() || num_reads == 0 {
            Err(Slow5Error::ReadIdIterError)
        } else {
            Ok(ReadIdIter {
                idx: 0,
                num_reads,
                read_id_ptr: rids,
                _lifetime: PhantomData,
            })
        }
    }
}

impl<'a> Iterator for ReadIdIter<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.num_reads {
            let rid = unsafe { self.read_id_ptr.offset(self.idx as isize) };
            let rid = unsafe { CStr::from_ptr(*rid) };
            self.idx += 1;
            Some(rid.to_bytes())
        } else {
            None
        }
    }
}

/// Iterator over labels for an auxiliary field enum
pub struct AuxEnumLabelIter<'a> {
    _reader: &'a FileReader,
    label_ptr: *mut *mut c_char,
    n: u8,
    idx: u8,
}

impl<'a> AuxEnumLabelIter<'a> {
    fn new(_reader: &'a FileReader, label_ptr: *mut *mut c_char, n: u8) -> Self {
        AuxEnumLabelIter {
            _reader,
            label_ptr,
            n,
            idx: 0,
        }
    }
}
impl<'a> std::fmt::Debug for AuxEnumLabelIter<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AuxEnumLabelIter")
            .field("idx", &self.idx)
            .field("n_labels", &self.n)
            .finish()
    }
}

impl<'a> Iterator for AuxEnumLabelIter<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.n {
            let label = unsafe { self.label_ptr.offset(self.idx as isize) };
            let label = unsafe { CStr::from_ptr(*label) };
            self.idx += 1;
            Some(label.to_bytes())
        } else {
            None
        }
    }
}

pub struct AttrKeysIter<'a> {
    _reader: &'a FileReader,
    keys: *mut *const c_char,
    n_keys: u64,
    idx: u64,
}

impl<'a> std::fmt::Debug for AttrKeysIter<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AuxEnumLabelIter")
            .field("idx", &self.idx)
            .field("n_keys", &self.n_keys)
            .finish()
    }
}

impl<'a> Iterator for AttrKeysIter<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.n_keys {
            let attr_key = unsafe { self.keys.offset(self.idx as isize) };
            let attr_key = unsafe { CStr::from_ptr(*attr_key) };
            self.idx += 1;
            Some(attr_key.to_bytes())
        } else {
            None
        }
    }
}

impl<'a> Drop for AttrKeysIter<'a> {
    fn drop(&mut self) {
        unsafe { libc::free(self.keys as *mut c_void) }
    }
}

#[cfg(test)]
mod test {

    use super::*;
    use crate::RecordExt;

    #[test]
    fn test_reader() {
        let filename = "examples/example.slow5";
        let mut reader = FileReader::open(filename).unwrap();

        let read_id: &[u8] = b"r3";
        let rec = reader.get_record(read_id).unwrap();
        assert_eq!(rec.read_id(), read_id);

        let mut acc = Vec::new();
        for rec in reader.records() {
            acc.push(rec);
        }
        assert!(!acc.is_empty());
    }

    #[test]
    fn test_bad_path() {
        let filename = "random_fileoufnseif";
        let reader = FileReader::open(filename);
        assert!(matches!(reader, Err(Slow5Error::IncorrectPath(_))));
    }

    #[test]
    fn test_no_compression() {
        let filename = "examples/example.slow5";
        let reader = FileReader::open(filename).unwrap();
        assert_eq!(reader.record_compression(), RecordCompression::None);
        assert_eq!(reader.signal_compression(), SignalCompression::None);
    }
}