nafcodec_py/
lib.rs

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
#![doc = include_str!("../README.md")]

extern crate nafcodec;
extern crate pyo3;

mod pyfile;

use self::pyfile::PyFileRead;
use self::pyfile::PyFileReadWrapper;
use self::pyfile::PyFileWrite;
use self::pyfile::PyFileWriteWrapper;

use std::io::BufReader;
use std::ops::DerefMut;

use nafcodec::DecoderBuilder;
use pyo3::exceptions::PyFileNotFoundError;
use pyo3::exceptions::PyIsADirectoryError;
use pyo3::exceptions::PyOSError;
use pyo3::exceptions::PyRuntimeError;
use pyo3::exceptions::PyUnicodeError;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::PyList;
use pyo3::types::PyString;
use pyo3::PyTypeInfo;

/// Convert a `nafcodec::error::Error` into a Python exception.
fn convert_error(_py: Python, error: nafcodec::error::Error, path: Option<&str>) -> PyErr {
    use nafcodec::error::Error;

    match error {
        Error::Utf8(_utf8_error) => PyUnicodeError::new_err("failed to decode UTF-8 data"),
        Error::Nom(nom_error) => {
            PyValueError::new_err(format!("parser failed: {:?}", nom_error.code))
        }
        Error::MissingField(field) => {
            PyValueError::new_err(format!("missing record field: {:?}", field))
        }
        Error::InvalidSequence => PyValueError::new_err("invalid characters found in sequence"),
        Error::Io(io_error) => {
            let desc = io_error.to_string();
            if let Some(p) = path.map(str::to_string) {
                match io_error.raw_os_error() {
                    Some(2) => PyFileNotFoundError::new_err((p,)),
                    #[cfg(target_os = "windows")]
                    Some(3) => PyFileNotFoundError::new_err((p,)),
                    #[cfg(not(target_os = "windows"))]
                    Some(21) => PyIsADirectoryError::new_err((p,)),
                    Some(code) => PyOSError::new_err((code, desc, p)),
                    None => PyOSError::new_err((desc,)),
                }
            } else {
                match io_error.raw_os_error() {
                    Some(2) => PyFileNotFoundError::new_err((desc,)),
                    #[cfg(target_os = "windows")]
                    Some(3) => PyFileNotFoundError::new_err((desc,)),
                    #[cfg(not(target_os = "windows"))]
                    Some(21) => PyIsADirectoryError::new_err((desc,)),
                    Some(code) => PyOSError::new_err((code, desc)),
                    None => PyOSError::new_err((desc,)),
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------

pub struct SequenceType(nafcodec::SequenceType);

impl<'py> FromPyObject<'py> for SequenceType {
    fn extract(ob: &'py PyAny) -> PyResult<Self> {
        let py = ob.py();
        match ob.downcast::<PyString>()?.to_string_lossy().as_ref() {
            "dna" => Ok(SequenceType(nafcodec::SequenceType::Dna)),
            "rna" => Ok(SequenceType(nafcodec::SequenceType::Rna)),
            "protein" => Ok(SequenceType(nafcodec::SequenceType::Protein)),
            "text" => Ok(SequenceType(nafcodec::SequenceType::Text)),
            other => {
                let msg =
                    PyString::new_bound(py, "expected 'dna', 'rna', 'protein' or 'text', got {!r}")
                        .call_method1("format", (other,))?
                        .to_object(py);
                Err(PyValueError::new_err(msg))
            }
        }
    }
}

impl<'py> ToPyObject for SequenceType {
    fn to_object(&self, py: Python<'_>) -> PyObject {
        let tag = match self.0 {
            nafcodec::SequenceType::Dna => pyo3::intern!(py, "dna"),
            nafcodec::SequenceType::Rna => pyo3::intern!(py, "rna"),
            nafcodec::SequenceType::Protein => pyo3::intern!(py, "protein"),
            nafcodec::SequenceType::Text => pyo3::intern!(py, "text"),
        };
        tag.to_object(py)
    }
}

impl From<nafcodec::SequenceType> for SequenceType {
    fn from(ty: nafcodec::SequenceType) -> Self {
        Self(ty)
    }
}

impl From<SequenceType> for nafcodec::SequenceType {
    fn from(ty: SequenceType) -> Self {
        ty.0
    }
}

// ---------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq)]
pub enum OpenMode {
    Read,
    Write,
}

impl<'py> FromPyObject<'py> for OpenMode {
    fn extract(ob: &'py PyAny) -> PyResult<Self> {
        let py = ob.py();
        match ob.downcast::<PyString>()?.to_string_lossy().as_ref() {
            "r" => Ok(OpenMode::Read),
            "w" => Ok(OpenMode::Write),
            other => {
                let msg = PyString::new_bound(py, "expected 'r' or 'w', got {!r}")
                    .call_method1("format", (other,))?
                    .to_object(py);
                Err(PyValueError::new_err(msg))
            }
        }
    }
}

// ---------------------------------------------------------------------------

/// A single sequence record stored in a Nucleotide Archive Format file.
#[pyclass(module = "nafcodec")]
#[derive(Clone, Debug)]
pub struct Record {
    /// `str` or `None`: The record identifier.
    #[pyo3(get, set)]
    id: Option<Py<PyString>>,
    /// `str` or `None`: The record comment.
    #[pyo3(get, set)]
    comment: Option<Py<PyString>>,
    /// `str` or `None`: The record sequence.
    #[pyo3(get, set)]
    sequence: Option<Py<PyString>>,
    /// `str` or `None`: The record quality.
    #[pyo3(get, set)]
    quality: Option<Py<PyString>>,
    /// `str` or `None`: The record sequence length.
    #[pyo3(get, set)]
    length: Option<u64>,
}

#[pymethods]
impl Record {
    #[new]
    #[pyo3(signature = (*, id=None, comment=None, sequence=None, quality=None, length=None))]
    fn __init__<'py>(
        py: Python<'py>,
        id: Option<Py<PyString>>,
        comment: Option<Py<PyString>>,
        sequence: Option<Py<PyString>>,
        quality: Option<Py<PyString>>,
        mut length: Option<u64>,
    ) -> PyResult<PyClassInitializer<Self>> {
        // Check lengths are consistent.
        if let Some(seq) = sequence.as_ref() {
            if let Some(qual) = quality.as_ref() {
                if seq.bind(py).len()? != qual.bind(py).len()? {
                    return Err(PyValueError::new_err(
                        "lengths of sequence and quality don't match",
                    ));
                }
            }
            if let Some(&l) = length.as_ref() {
                if seq.bind(py).len()? != l as usize {
                    return Err(PyValueError::new_err(
                        "length of sequence and record length don't match",
                    ));
                }
            } else {
                length = Some(seq.bind(py).len()? as u64);
            }
        }
        if let Some(qual) = quality.as_ref() {
            if let Some(&l) = length.as_ref() {
                if qual.bind(py).len()? != l as usize {
                    return Err(PyValueError::new_err(
                        "length of quality and record length don't match",
                    ));
                }
            } else {
                length = Some(qual.bind(py).len()? as u64);
            }
        }

        Ok(PyClassInitializer::from(Record {
            id,
            comment,
            sequence,
            quality,
            length,
        }))
    }

    fn __repr__<'py>(slf: &Bound<'_, Self>) -> PyResult<PyObject> {
        let py = slf.py();
        let format = pyo3::intern!(py, "format");
        let args = PyList::empty_bound(py);
        let record = slf.borrow();
        if let Some(id) = &record.id {
            args.append(pyo3::intern!(py, "id={!r}").call_method1(format, (id,))?)?;
        }
        if let Some(comment) = &record.comment {
            args.append(pyo3::intern!(py, "comment={!r}").call_method1(format, (comment,))?)?;
        }
        if let Some(sequence) = &record.sequence {
            args.append(pyo3::intern!(py, "sequence={!r}").call_method1(format, (sequence,))?)?;
        }
        if let Some(quality) = &record.quality {
            args.append(pyo3::intern!(py, "quality={!r}").call_method1(format, (quality,))?)?;
        }
        if let Some(length) = &record.length {
            args.append(format!("length={}", length).to_object(py))?;
        }
        pyo3::intern!(py, "{}({})")
            .call_method1(
                format,
                (
                    slf.get_type().name()?,
                    pyo3::intern!(py, ", ").call_method1("join", (args,))?,
                ),
            )
            .map(|x| x.to_object(py))
    }
}

impl pyo3::conversion::IntoPy<Record> for nafcodec::Record {
    fn into_py(self, py: Python<'_>) -> Record {
        let id = self.id.map(|x| PyString::new_bound(py, &x).into());
        let sequence = self.sequence.map(|x| PyString::new_bound(py, &x).into());
        let comment = self.comment.map(|x| PyString::new_bound(py, &x).into());
        let quality = self.quality.map(|x| PyString::new_bound(py, &x).into());
        let length = self.length;
        Record {
            id,
            sequence,
            comment,
            quality,
            length,
        }
    }
}

impl TryFrom<&Record> for nafcodec::Record {
    type Error = PyErr;
    fn try_from(value: &Record) -> Result<Self, PyErr> {
        Python::with_gil(|py| {
            let id = value
                .id
                .as_ref()
                .map(|s| s.to_str(py))
                .transpose()?
                .map(String::from);
            let comment = value
                .comment
                .as_ref()
                .map(|s| s.to_str(py))
                .transpose()?
                .map(String::from);
            let sequence = value
                .sequence
                .as_ref()
                .map(|s| s.to_str(py))
                .transpose()?
                .map(String::from);
            let quality = value
                .quality
                .as_ref()
                .map(|s| s.to_str(py))
                .transpose()?
                .map(String::from);
            let length = value.length.clone();
            Ok(nafcodec::Record {
                id,
                comment,
                sequence,
                quality,
                length,
            })
        })
    }
}

// ---------------------------------------------------------------------------

/// A streaming decoder to read a Nucleotide Archive Format file.
#[pyclass(module = "nafcodec")]
pub struct Decoder {
    decoder: nafcodec::Decoder<'static, BufReader<PyFileReadWrapper>>,
}

#[pymethods]
impl Decoder {
    #[new]
    #[pyo3(signature = (file, *, id=true, comment=true, sequence=true, quality=true, mask=true, buffer_size=None))]
    pub fn __init__<'py>(
        file: Bound<'py, PyAny>,
        id: bool,
        comment: bool,
        sequence: bool,
        quality: bool,
        mask: bool,
        buffer_size: Option<usize>,
    ) -> PyResult<PyClassInitializer<Self>> {
        let py = file.py();

        let mut builder = DecoderBuilder::new();
        builder.id(id);
        builder.comment(comment);
        builder.sequence(sequence);
        builder.quality(quality);
        builder.mask(mask);
        builder.buffer_size(buffer_size.map(Ok).unwrap_or_else(|| {
            py.import_bound(pyo3::intern!(py, "io"))?
                .getattr(pyo3::intern!(py, "DEFAULT_BUFFER_SIZE"))?
                .extract::<usize>()
        })?);

        let decoder = match PyFileRead::from_ref(&file) {
            Ok(handle) => {
                let wrapper = PyFileReadWrapper::PyFile(handle);
                builder
                    .with_reader(std::io::BufReader::new(wrapper))
                    .map_err(|e| convert_error(py, e, None))?
            }
            Err(_e) => {
                let path = py
                    .import_bound("os")?
                    .call_method1(pyo3::intern!(py, "fspath"), (file,))?
                    .extract::<Bound<'_, PyString>>()?;
                let path_str = path.to_str()?;
                let wrapper = std::fs::File::open(path_str)
                    .map_err(nafcodec::error::Error::Io)
                    .map_err(|e| convert_error(py, e, Some(path_str)))
                    .map(PyFileReadWrapper::File)?;
                builder
                    .with_reader(std::io::BufReader::new(wrapper))
                    .map_err(|e| convert_error(py, e, Some(path_str)))?
            }
        };

        Ok(Decoder { decoder }.into())
    }

    pub fn __iter__(slf: PyRef<'_, Self>) -> PyResult<PyRef<'_, Self>> {
        Ok(slf)
    }

    pub fn __len__(slf: PyRef<'_, Self>) -> PyResult<usize> {
        Ok(slf.decoder.len())
    }

    pub fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult<Option<Record>> {
        let py = slf.py();
        let result = slf.deref_mut().decoder.next().transpose();
        match result {
            Ok(None) => Ok(None),
            Ok(Some(record)) => Ok(Some(record.into_py(py))),
            Err(e) => Err(convert_error(py, e, None)),
        }
    }

    pub fn __enter__<'py>(slf: PyRef<'py, Self>) -> PyRef<'py, Self> {
        slf
    }

    #[allow(unused)]
    pub fn __exit__<'py>(
        slf: PyRefMut<'py, Self>,
        exc_type: Bound<'py, PyAny>,
        exc_value: Bound<'py, PyAny>,
        traceback: Bound<'py, PyAny>,
    ) -> PyResult<bool> {
        Ok(false)
    }

    /// `str`: The type of sequence stored in the archive.
    #[getter]
    pub fn sequence_type(slf: PyRef<'_, Self>) -> PyObject {
        let py = slf.py();
        SequenceType(slf.decoder.sequence_type()).to_object(py)
    }

    /// `str`: The length of sequence lines in the original FASTA file.
    #[getter]
    pub fn format_version(slf: PyRef<'_, Self>) -> &Bound<'_, PyString> {
        use nafcodec::FormatVersion;
        let py = slf.py();
        match slf.decoder.header().format_version() {
            FormatVersion::V1 => pyo3::intern!(py, "v1"),
            FormatVersion::V2 => pyo3::intern!(py, "v2"),
        }
    }

    /// `int`: The length of sequence lines in the original FASTA file.
    #[getter]
    pub fn line_length(slf: PyRef<'_, Self>) -> u64 {
        slf.decoder.header().line_length()
    }

    /// `str`: The separator between sequence identifiers and comments.
    #[getter]
    pub fn name_separator(slf: PyRef<'_, Self>) -> char {
        slf.decoder.header().name_separator()
    }

    /// `int`: The total number of sequences stored in the archive.
    #[getter]
    pub fn number_of_sequences(slf: PyRef<'_, Self>) -> u64 {
        slf.decoder.header().number_of_sequences()
    }

    /// Read the next record from the archive.
    ///
    /// This method will returns `None` when no more records are available.
    pub fn read(mut slf: PyRefMut<'_, Self>) -> PyResult<Option<Record>> {
        let py = slf.py();
        let result = slf.deref_mut().decoder.next().transpose();
        match result {
            Ok(None) => Ok(None),
            Ok(Some(record)) => Ok(Some(record.into_py(py))),
            Err(e) => Err(convert_error(py, e, None)),
        }
    }
}

// ---------------------------------------------------------------------------

/// An encoder to iteratively write a Nucleotide Archive Format file.
#[pyclass(module = "nafcodec")]
pub struct Encoder {
    encoder: Option<nafcodec::Encoder<'static, nafcodec::Memory>>,
    file: PyFileWriteWrapper,
}

#[pymethods]
impl Encoder {
    #[new]
    #[pyo3(signature=(
        file,
        sequence_type=SequenceType(nafcodec::SequenceType::Dna),
        *,
        id = false,
        comment = false,
        sequence = false,
        quality = false,
        compression_level = 0,
    ))]
    pub fn __init__<'py>(
        file: Bound<'py, PyAny>,
        sequence_type: SequenceType,
        id: bool,
        comment: bool,
        sequence: bool,
        quality: bool,
        compression_level: i32,
    ) -> PyResult<PyClassInitializer<Self>> {
        let py = file.py();
        let file = match PyFileWrite::from_ref(&file) {
            Ok(handle) => PyFileWriteWrapper::PyFile(handle),
            Err(_e) => {
                let path = py
                    .import_bound("os")?
                    .call_method1(pyo3::intern!(py, "fspath"), (file,))?
                    .extract::<Bound<'_, PyString>>()?;
                let path_str = path.to_str()?;
                std::fs::File::create(path_str)
                    .map_err(nafcodec::error::Error::Io)
                    .map_err(|e| convert_error(py, e, Some(path_str)))
                    .map(PyFileWriteWrapper::File)?
            }
        };
        let encoder = nafcodec::EncoderBuilder::new(sequence_type.0)
            .id(id)
            .comment(comment)
            .quality(quality)
            .sequence(sequence)
            .compression_level(compression_level)
            .with_memory()
            .map(Some)
            .map_err(|e| convert_error(py, e, None))?;
        Ok(Self { file, encoder }.into())
    }

    pub fn __enter__<'py>(slf: PyRef<'py, Self>) -> PyRef<'py, Self> {
        slf
    }

    #[allow(unused)]
    pub fn __exit__<'py>(
        slf: PyRefMut<'py, Self>,
        exc_type: Bound<'py, PyAny>,
        exc_value: Bound<'py, PyAny>,
        traceback: Bound<'py, PyAny>,
    ) -> PyResult<bool> {
        Encoder::close(slf)?;
        Ok(false)
    }

    pub fn write<'py>(mut slf: PyRefMut<'py, Self>, record: &'py Record) -> PyResult<()> {
        let py = slf.py();
        if let Some(encoder) = slf.encoder.as_mut() {
            encoder
                .push(&record.try_into()?)
                .map_err(|err| convert_error(py, err, None))
        } else {
            Err(PyRuntimeError::new_err("operation on closed encoder."))
        }
    }

    pub fn close<'py>(mut slf: PyRefMut<'py, Self>) -> PyResult<()> {
        let py = slf.py();
        if let Some(encoder) = slf.encoder.take() {
            encoder
                .write(&mut slf.file)
                .map_err(|e| convert_error(py, e, None))?;
        }
        Ok(())
    }
}

/// An encoder/decoder for Nucleotide Archive Format files.
#[pymodule]
#[pyo3(name = "lib")]
pub fn init<'py>(_py: Python<'py>, m: &Bound<'py, PyModule>) -> PyResult<()> {
    m.add("__package__", "nafcodec")?;
    m.add("__version__", env!("CARGO_PKG_VERSION"))?;
    m.add("__author__", env!("CARGO_PKG_AUTHORS").replace(':', "\n"))?;

    m.add_class::<Decoder>()?;
    m.add_class::<Encoder>()?;
    m.add_class::<Record>()?;

    /// Open a Nucleotide Archive Format file.
    ///
    /// This function acts as a high-level wrapper and returns either
    /// a `~nafcodec.Decoder` or an `~nafcodec.Encoder` depending on the
    /// provided mode.
    ///
    /// Arguments:
    ///     file (`str`, `pathlib.Path` or file-like object): The file to
    ///         read the archive from, or write the archive to.
    ///     mode (`str`): The mode to open the archive with, either 'r'
    ///         to read an existing archive, or 'w' to write a new
    ///         archive.
    ///     options (`object`): Additional options to pass to the
    ///         `~nafcodec.Decoder` or `~nafcodec.Encoder` constructors.
    ///  
    /// Example:
    ///     Open an archive and read all the records from an existing
    ///     archive into a `list`::
    ///
    ///     >>> with open("LuxC.naf") as decoder:
    ///     ...     records = list(decoder)
    ///     
    ///     Create a new archive for recording FASTA records (identifiers
    ///     and DNA sequences)::
    ///     
    ///     >>> with tempfile.NamedTemporaryFile() as dst:
    ///     ...     with open(dst, "w", id=True, sequence=True) as encoder:
    ///     ...         encoder.write(Record(id="r1", sequence="ATGC"))
    ///
    #[pyfn(m)]
    #[pyo3(signature = (file, mode = OpenMode::Read, **options))]
    fn open<'py>(
        file: &Bound<'py, PyAny>,
        mode: OpenMode,
        options: Option<&Bound<'py, PyDict>>,
    ) -> PyResult<PyObject> {
        let py = file.py();
        match mode {
            OpenMode::Read => Ok(Decoder::type_object_bound(py)
                .call((file,), options)?
                .to_object(py)),
            OpenMode::Write => Ok(Encoder::type_object_bound(py)
                .call((file,), options)?
                .to_object(py)),
        }
    }

    Ok(())
}