deepbiop-fa 0.1.17

Deep Learning Preprocessing Library for Fastq Format
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::path::PathBuf;

use crate::encode::Encoder;
use crate::{
    dataset::FastaStreamIterator,
    encode::{self},
    io,
};

use ahash::HashSet;
use anyhow::Result;
use bstr::BString;
#[cfg(feature = "cache")]
use deepbiop_utils::io as deepbiop_io;
use log::warn;
use noodles::fastq;
use numpy::convert::ToPyArray;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use rayon::prelude::*;

use pyo3_stub_gen::derive::*;

#[gen_stub_pymethods]
#[pymethods]
impl encode::ParquetEncoder {
    #[new]
    fn py_new(option: encode::EncoderOption) -> Self {
        encode::ParquetEncoder::new(option)
    }
}

#[gen_stub_pyclass]
#[pyclass(name = "RecordData", module = "deepbiop.fa")]
pub struct PyRecordData(encode::RecordData);

impl From<encode::RecordData> for PyRecordData {
    fn from(data: encode::RecordData) -> Self {
        Self(data)
    }
}

// Implement FromPyObject for PyRecordData
impl<'a, 'py> FromPyObject<'a, 'py> for PyRecordData {
    type Error = PyErr;

    fn extract(ob: pyo3::Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
        // Assuming Python objects are tuples of (id, seq)
        let (id, seq): (String, String) = ob.extract()?;
        Ok(PyRecordData(encode::RecordData {
            id: id.into(),
            seq: seq.into(),
        }))
    }
}

#[gen_stub_pymethods]
#[pymethods]
impl PyRecordData {
    #[new]
    fn new(id: String, seq: String) -> Self {
        Self(encode::RecordData {
            id: id.into(),
            seq: seq.into(),
        })
    }

    #[getter]
    fn id(&self) -> String {
        self.0.id.to_string()
    }

    #[setter]
    fn set_id(&mut self, id: String) {
        self.0.id = id.into();
    }

    #[getter]
    fn seq(&self) -> String {
        self.0.seq.to_string()
    }

    #[setter]
    fn set_seq(&mut self, seq: String) {
        self.0.seq = seq.into();
    }
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
#[pyo3(signature = (records_data, file_path=None))]
fn write_fa(records_data: Vec<PyRecordData>, file_path: Option<PathBuf>) -> Result<()> {
    let records: Vec<encode::RecordData> = records_data
        .into_par_iter()
        .map(|py_record| py_record.0)
        .collect();
    io::write_fa(&records, file_path)
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
fn write_fa_parallel(
    records_data: Vec<PyRecordData>,
    file_path: PathBuf,
    threads: usize,
) -> Result<()> {
    let records: Vec<encode::RecordData> = records_data
        .into_par_iter()
        .map(|py_record| py_record.0)
        .collect();
    io::write_bzip_fa_parallel(&records, file_path, Some(threads))
}

#[cfg(feature = "cache")]
#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
fn encode_fa_path_to_parquet_chunk(
    fa_path: PathBuf,
    chunk_size: usize,
    parallel: bool,
    bases: String,
) -> Result<()> {
    let option = encode::EncoderOptionBuilder::default()
        .bases(bases.as_bytes().to_vec())
        .build()?;

    let mut fa_encoder = encode::ParquetEncoderBuilder::default()
        .option(option)
        .build()?;
    fa_encoder.encode_chunk(&fa_path, chunk_size, parallel)?;
    Ok(())
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
#[pyo3(signature = (fa_path, bases, result_path=None))]
fn encode_fa_path_to_parquet(
    fa_path: PathBuf,
    bases: String,
    result_path: Option<PathBuf>,
) -> Result<()> {
    let option = encode::EncoderOptionBuilder::default()
        .bases(bases.as_bytes().to_vec())
        .build()?;

    let mut fa_encoder = encode::ParquetEncoderBuilder::default()
        .option(option)
        .build()?;
    let (record_batch, schema) = fa_encoder.encode(&fa_path)?;

    // result file is fq_path with .parquet extension
    let parquet_path = if let Some(path) = result_path {
        if path.with_extension("parquet").exists() {
            warn!("{} already exists, overwriting", path.display());
        }
        path.with_extension("parquet")
    } else {
        fa_path.with_extension("parquet")
    };

    #[cfg(feature = "cache")]
    {
        deepbiop_io::write_parquet_for_batches(parquet_path, &record_batch, schema)?;
        Ok(())
    }

    #[cfg(not(feature = "cache"))]
    {
        let _ = (parquet_path, record_batch, schema); // Silence unused warnings
        Err(anyhow::anyhow!("Parquet encoding requires 'cache' feature"))
    }
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
fn encode_fa_paths_to_parquet(fa_path: Vec<PathBuf>, bases: String) -> Result<()> {
    for path in &fa_path {
        encode_fa_path_to_parquet(path.clone(), bases.clone(), None)?;
    }
    Ok(())
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
fn convert_multiple_fas_to_one_fa(
    paths: Vec<PathBuf>,
    result_path: PathBuf,
    parallel: bool,
) -> Result<()> {
    if paths.is_empty() {
        return Ok(());
    }
    io::convert_multiple_fas_to_one_bgzip_fa(&paths, result_path, parallel)?;
    Ok(())
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction(name = "select_record_from_fa")]
pub fn py_select_record_from_fq(
    selected_reads: Vec<String>,
    fq: PathBuf,
    output: PathBuf,
) -> Result<()> {
    let selected_reads: HashSet<BString> =
        selected_reads.into_par_iter().map(|s| s.into()).collect();
    let records = io::select_record_from_fa_by_stream(fq, &selected_reads)?;
    io::write_fa_for_noodle_record(&records, output)?;
    Ok(())
}

#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction(name = "select_record_from_fa_by_random")]
pub fn py_select_record_from_fq_by_random(
    fq: PathBuf,
    number: usize,
    output: PathBuf,
) -> Result<()> {
    let records = io::select_record_from_fa_by_random(fq, number)?;
    io::write_fa_for_noodle_record(&records, output)?;
    Ok(())
}

/// Convert FASTA file to FASTQ file with default quality scores.
///
/// Since FASTA files don't contain quality information, assigns
/// default quality score (Phred+33 Q40 = '~') to all bases.
#[gen_stub_pyfunction(module = "deepbiop.fa")]
#[pyfunction]
pub fn fasta_to_fastq(fasta_path: PathBuf, fastq_path: PathBuf) -> Result<()> {
    let fq_records = io::fasta_to_fastq(&fasta_path)?;
    let handle = std::fs::File::create(fastq_path)?;
    let mut writer = fastq::io::Writer::new(handle);
    for record in fq_records {
        writer.write_record(&record)?;
    }
    Ok(())
}

/// Streaming FASTA dataset for efficient iteration over large files.
///
/// This dataset provides memory-efficient streaming iteration over FASTA files,
/// reading records one at a time without loading the entire file into memory.
/// Yields records as dictionaries with NumPy arrays for zero-copy efficiency.
///
/// Note: FASTA files do not contain quality scores.
///
/// # Examples
///
/// ```python
/// from deepbiop.fa import FastaStreamDataset
///
/// dataset = FastaStreamDataset("genome.fasta.gz")
/// for record in dataset:
///     seq = record['sequence']  # NumPy array
///     print(f"ID: {record['id']}, Length: {len(seq)}")
/// ```
#[gen_stub_pyclass]
#[pyclass(name = "FastaStreamDataset", module = "deepbiop.fa")]
pub struct PyFastaStreamDataset {
    file_path: String,
    size_hint: Option<usize>,
}

#[gen_stub_pymethods]
#[pymethods]
impl PyFastaStreamDataset {
    /// Create a new streaming FASTA dataset.
    ///
    /// # Arguments
    ///
    /// * `file_path` - Path to FASTA file (supports .fasta, .fa, .fasta.gz, .fa.gz)
    ///
    /// # Returns
    ///
    /// FastaStreamDataset instance
    ///
    /// # Raises
    ///
    /// * `FileNotFoundError` - If file doesn't exist
    /// * `IOError` - If file cannot be opened
    #[new]
    fn new(file_path: String) -> PyResult<Self> {
        // Create temporary dataset to validate file and get size hint
        let dataset = crate::dataset::FastaDataset::new(file_path.clone())
            .map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;

        let size_hint = dataset.records_count();

        Ok(Self {
            file_path,
            size_hint,
        })
    }

    /// Return iterator over dataset records.
    ///
    /// Each record is a dictionary with:
    /// - 'id': str - Sequence identifier
    /// - 'sequence': np.ndarray (uint8) - Nucleotide sequence bytes
    /// - 'description': Optional[str] - Sequence description
    fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<PyFastaStreamIterator>> {
        let iter = FastaStreamIterator::open(&slf.file_path)
            .map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
        Py::new(
            slf.py(),
            PyFastaStreamIterator {
                iter: std::sync::Mutex::new(iter),
            },
        )
    }

    /// Get estimated number of records in dataset.
    ///
    /// # Returns
    ///
    /// int - Estimated record count (0 if unavailable)
    fn __len__(&self) -> PyResult<usize> {
        Ok(self.size_hint.unwrap_or(0))
    }

    /// Human-readable representation.
    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "FastaStreamDataset(file='{}', records={})",
            self.file_path,
            self.size_hint
                .map_or("unknown".to_string(), |n| n.to_string())
        ))
    }

    /// Provide arguments for __new__ during unpickling.
    ///
    /// This is called by pickle to get arguments to pass to __new__().
    ///
    /// # Returns
    ///
    /// Tuple of (file_path,) to pass to __new__
    fn __getnewargs__(&self) -> PyResult<(String,)> {
        Ok((self.file_path.clone(),))
    }

    /// Pickling support for multiprocessing (DataLoader with num_workers > 0).
    fn __getstate__(&self, py: Python) -> PyResult<Py<PyDict>> {
        let state = PyDict::new(py);
        state.set_item("file_path", &self.file_path)?;
        state.set_item("size_hint", self.size_hint)?;
        Ok(state.into())
    }

    /// Unpickling support for multiprocessing.
    fn __setstate__(&mut self, state: &Bound<'_, PyDict>) -> PyResult<()> {
        self.file_path = state.get_item("file_path")?.unwrap().extract()?;
        self.size_hint = state.get_item("size_hint")?.unwrap().extract()?;
        Ok(())
    }
}

/// Iterator for streaming FASTA dataset.
#[gen_stub_pyclass]
#[pyclass(name = "FastaStreamIterator", module = "deepbiop.fa")]
pub struct PyFastaStreamIterator {
    iter: std::sync::Mutex<FastaStreamIterator>,
}

#[gen_stub_pymethods]
#[pymethods]
impl PyFastaStreamIterator {
    fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
        slf
    }

    fn __next__(&mut self, py: Python) -> PyResult<Option<Py<PyDict>>> {
        let iter = self
            .iter
            .get_mut()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        match iter.next() {
            None => Ok(None),
            Some(Ok(record)) => {
                // Create dict with NumPy arrays for zero-copy
                let dict = PyDict::new(py);
                dict.set_item("id", record.id)?;

                // Convert sequence to NumPy array (zero-copy from Rust Vec)
                let seq_array = record.sequence.to_pyarray(py);
                dict.set_item("sequence", seq_array)?;

                // FASTA files don't have quality scores
                dict.set_item("quality", py.None())?;

                dict.set_item("description", record.description)?;

                Ok(Some(dict.into()))
            }
            Some(Err(e)) => Err(pyo3::exceptions::PyValueError::new_err(format!(
                "Failed to read FASTA record: {}",
                e
            ))),
        }
    }
}

// register fq sub_module
pub fn register_fa_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
    let sub_module_name = "fa";
    let child_module = PyModule::new(parent_module.py(), sub_module_name)?;

    child_module.add_class::<PyRecordData>()?;
    child_module.add_class::<encode::EncoderOption>()?;
    child_module.add_class::<encode::ParquetEncoder>()?;

    child_module.add_function(wrap_pyfunction!(write_fa, &child_module)?)?;
    child_module.add_function(wrap_pyfunction!(write_fa_parallel, &child_module)?)?;
    child_module.add_function(wrap_pyfunction!(encode_fa_path_to_parquet, &child_module)?)?;
    child_module.add_function(wrap_pyfunction!(encode_fa_paths_to_parquet, &child_module)?)?;
    #[cfg(feature = "cache")]
    child_module.add_function(wrap_pyfunction!(
        encode_fa_path_to_parquet_chunk,
        &child_module
    )?)?;
    child_module.add_function(wrap_pyfunction!(
        convert_multiple_fas_to_one_fa,
        &child_module
    )?)?;

    child_module.add_function(wrap_pyfunction!(py_select_record_from_fq, &child_module)?)?;
    child_module.add_function(wrap_pyfunction!(
        py_select_record_from_fq_by_random,
        &child_module
    )?)?;
    child_module.add_function(wrap_pyfunction!(fasta_to_fastq, &child_module)?)?;

    // Add streaming dataset classes
    child_module.add_class::<PyFastaStreamDataset>()?;
    child_module.add_class::<PyFastaStreamIterator>()?;

    parent_module.add_submodule(&child_module)?;

    Ok(())
}