deepbiop-core 0.1.17

Deep Learning Preprocessing Library
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
//! K-mer encoding for machine learning applications.
//!
//! This module provides k-mer encoding functionality optimized for deep learning,
//! converting biological sequences into k-mer representations suitable for ML models.

use ahash::HashMap;
use anyhow::Result;
use ndarray::{Array1, Array2};
use rayon::prelude::*;

use crate::error::DPError;
use crate::types::EncodingType;

/// K-mer encoder for biological sequences.
///
/// Encodes sequences as dense k-mer count vectors over the full k-mer vocabulary.
/// With `canonical = true` (DNA/RNA only) a k-mer and its reverse complement share
/// one index, so the vocabulary shrinks to the number of canonical k-mers.
///
/// # Examples
///
/// ```
/// use deepbiop_core::kmer::encode::KmerEncoder;
/// use deepbiop_core::types::EncodingType;
///
/// let encoder = KmerEncoder::new(3, true, EncodingType::DNA).unwrap();
/// let encoded = encoder.encode(b"ACGTACGT").unwrap();
/// assert_eq!(encoded.len(), encoder.vocabulary_size());
/// ```
pub struct KmerEncoder {
    /// K-mer length
    k: usize,
    /// Use canonical k-mers (k-mer and reverse complement are the same)
    canonical: bool,
    /// Encoding type (DNA, RNA, or Protein)
    encoding_type: EncodingType,
    /// Every k-mer of the alphabet mapped to its output index (canonical k-mers
    /// and their reverse complements share an index).
    kmer_to_idx: HashMap<Vec<u8>, usize>,
    /// Number of distinct output indices.
    vocabulary_size: usize,
}

/// Largest dense vocabulary the encoder will materialize (16M entries).
pub const MAX_VOCABULARY_SIZE: usize = 1 << 24;

impl KmerEncoder {
    /// Create a new k-mer encoder.
    ///
    /// # Arguments
    ///
    /// * `k` - K-mer length (`1..`)
    /// * `canonical` - Fold each k-mer with its reverse complement (DNA/RNA only)
    /// * `encoding_type` - The type of sequence (DNA, RNA, or Protein)
    ///
    /// # Errors
    ///
    /// Returns an error if `k == 0`, if `alphabet_size^k` exceeds
    /// [`MAX_VOCABULARY_SIZE`], or if `canonical` is requested for protein.
    pub fn new(k: usize, canonical: bool, encoding_type: EncodingType) -> Result<Self> {
        if k == 0 {
            return Err(DPError::InvalidValue("k-mer length must be at least 1".into()).into());
        }
        if canonical && encoding_type == EncodingType::Protein {
            return Err(DPError::InvalidValue(
                "canonical k-mers are only defined for DNA/RNA".into(),
            )
            .into());
        }
        let alphabet = encoding_type.alphabet();
        let total = u32::try_from(k)
            .ok()
            .and_then(|k| alphabet.len().checked_pow(k))
            .filter(|&n| n <= MAX_VOCABULARY_SIZE)
            .ok_or_else(|| {
                DPError::InvalidValue(format!(
                    "k={k} yields more than {MAX_VOCABULARY_SIZE} {encoding_type:?} k-mers; choose a smaller k"
                ))
            })?;

        let mut kmer_to_idx = HashMap::with_capacity_and_hasher(total, Default::default());
        let mut next_idx = 0usize;
        let mut current = vec![alphabet[0]; k];
        // Iterate the vocabulary in lexicographic order via an odometer over alphabet positions.
        let mut digits = vec![0usize; k];
        loop {
            for (slot, &d) in current.iter_mut().zip(&digits) {
                *slot = alphabet[d];
            }
            let idx = if canonical {
                let rc = reverse_complement(&current, encoding_type);
                // The canonical representative is the lexicographically smaller strand; it
                // has already been assigned an index when it precedes `current`.
                match kmer_to_idx.get(&rc) {
                    Some(&i) if rc < current => i,
                    _ => {
                        next_idx += 1;
                        next_idx - 1
                    }
                }
            } else {
                next_idx += 1;
                next_idx - 1
            };
            kmer_to_idx.insert(current.clone(), idx);

            // Advance odometer.
            let mut pos = k;
            loop {
                if pos == 0 {
                    return Ok(Self {
                        k,
                        canonical,
                        encoding_type,
                        kmer_to_idx,
                        vocabulary_size: next_idx,
                    });
                }
                pos -= 1;
                digits[pos] += 1;
                if digits[pos] < alphabet.len() {
                    break;
                }
                digits[pos] = 0;
            }
        }
    }

    /// Get the k-mer length.
    pub fn k(&self) -> usize {
        self.k
    }

    /// Check if using canonical k-mers.
    pub fn is_canonical(&self) -> bool {
        self.canonical
    }

    /// Get the encoding type.
    pub fn encoding_type(&self) -> EncodingType {
        self.encoding_type
    }

    /// Length of the vectors produced by [`encode`](Self::encode).
    pub fn vocabulary_size(&self) -> usize {
        self.vocabulary_size
    }

    /// Encode a sequence as a k-mer count vector.
    ///
    /// # Errors
    ///
    /// Returns an error if the sequence is shorter than `k` or contains a
    /// character outside the alphabet.
    pub fn encode(&self, sequence: &[u8]) -> Result<Array1<f32>> {
        if sequence.len() < self.k {
            return Err(DPError::InvalidValue(format!(
                "sequence length {} is shorter than k={}",
                sequence.len(),
                self.k
            ))
            .into());
        }

        for (pos, &base) in sequence.iter().enumerate() {
            if !self.encoding_type.is_valid_char(base) {
                return Err(DPError::InvalidAlphabet {
                    character: base as char,
                    position: pos,
                    expected: String::from_utf8_lossy(self.encoding_type.alphabet()).to_string(),
                }
                .into());
            }
        }

        let mut counts = Array1::<f32>::zeros(self.vocabulary_size);
        let mut kmer = vec![0u8; self.k];
        for window in sequence.windows(self.k) {
            for (dst, &b) in kmer.iter_mut().zip(window) {
                *dst = b.to_ascii_uppercase();
            }
            if let Some(&idx) = self.kmer_to_idx.get(&kmer) {
                counts[idx] += 1.0;
            }
        }
        Ok(counts)
    }

    /// Encode multiple sequences in parallel as k-mer count vectors.
    ///
    /// # Errors
    ///
    /// Returns the first error produced by [`encode`](Self::encode).
    pub fn encode_batch(&self, sequences: &[&[u8]]) -> Result<Array2<f32>> {
        let mut batch = Array2::<f32>::zeros((sequences.len(), self.vocabulary_size));
        if sequences.is_empty() {
            return Ok(batch);
        }

        let rows: Vec<Array1<f32>> = sequences
            .par_iter()
            .map(|sequence| self.encode(sequence))
            .collect::<Result<_>>()?;
        for (mut dst, src) in batch.rows_mut().into_iter().zip(&rows) {
            dst.assign(src);
        }
        Ok(batch)
    }
}

fn reverse_complement(kmer: &[u8], encoding_type: EncodingType) -> Vec<u8> {
    kmer.iter()
        .rev()
        .map(|&b| match (b, encoding_type) {
            (b'A', EncodingType::RNA) => b'U',
            (b'U', EncodingType::RNA) => b'A',
            (b'A', _) => b'T',
            (b'T', _) => b'A',
            (b'C', _) => b'G',
            (b'G', _) => b'C',
            (other, _) => other,
        })
        .collect()
}

// Implement SequenceEncoder trait for KmerEncoder
impl crate::encoder::SequenceEncoder for KmerEncoder {
    type EncodeOutput = Array1<f32>;

    fn encode_sequence(&self, seq: &[u8], qual: Option<&[u8]>) -> Result<Self::EncodeOutput> {
        self.validate_input(seq, qual)?;
        self.encode(seq)
    }

    fn expected_output_size(&self, _seq_len: usize) -> usize {
        self.vocabulary_size
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_kmer_encoder_new() {
        let encoder = KmerEncoder::new(3, true, EncodingType::DNA).unwrap();
        assert_eq!(encoder.k(), 3);
        assert!(encoder.is_canonical());
        assert_eq!(encoder.encoding_type(), EncodingType::DNA);
        // 64 3-mers fold to 32 canonical ones (no palindromic odd-length k-mers).
        assert_eq!(encoder.vocabulary_size(), 32);
    }

    #[test]
    fn test_kmer_encoder_rejects_bad_params() {
        assert!(KmerEncoder::new(0, false, EncodingType::DNA).is_err());
        assert!(KmerEncoder::new(40, false, EncodingType::DNA).is_err());
        assert!(KmerEncoder::new(3, true, EncodingType::Protein).is_err());
    }

    #[test]
    fn test_kmer_encode_dna() {
        let encoder = KmerEncoder::new(3, false, EncodingType::DNA).unwrap();
        let sequence = b"ACGTACGT";

        let encoded = encoder.encode(sequence).unwrap();

        assert_eq!(encoded.len(), 64);
        let total_count: f32 = encoded.iter().sum();
        assert_eq!(total_count, (sequence.len() - 3 + 1) as f32);
    }

    #[test]
    fn test_kmer_encode_canonical_folds_reverse_complement() {
        let plain = KmerEncoder::new(2, false, EncodingType::DNA).unwrap();
        let canonical = KmerEncoder::new(2, true, EncodingType::DNA).unwrap();

        // AC and its reverse complement GT land on different indices without folding...
        let ac = plain.encode(b"AC").unwrap();
        let gt = plain.encode(b"GT").unwrap();
        assert_ne!(ac, gt);
        // ...and on the same index with folding. AA/TT, AC/GT, AG/CT, AT, CA/TG, CC/GG, CG, GA/TC, TA -> 10.
        assert_eq!(canonical.vocabulary_size(), 10);
        assert_eq!(
            canonical.encode(b"AC").unwrap(),
            canonical.encode(b"GT").unwrap()
        );
        assert_eq!(
            canonical.encode(b"AA").unwrap(),
            canonical.encode(b"TT").unwrap()
        );
        assert_ne!(
            canonical.encode(b"AC").unwrap(),
            canonical.encode(b"CA").unwrap()
        );
    }

    #[test]
    fn test_kmer_encode_canonical_rna() {
        let canonical = KmerEncoder::new(2, true, EncodingType::RNA).unwrap();
        assert_eq!(
            canonical.encode(b"AC").unwrap(),
            canonical.encode(b"GU").unwrap()
        );
    }

    #[test]
    fn test_kmer_encode_short_sequence_errors() {
        let encoder = KmerEncoder::new(5, false, EncodingType::DNA).unwrap();
        assert!(encoder.encode(b"ACG").is_err());
    }

    #[test]
    fn test_kmer_encode_invalid_char() {
        let encoder = KmerEncoder::new(3, false, EncodingType::DNA).unwrap();
        assert!(encoder.encode(b"ACGTN").is_err());
    }

    #[test]
    fn test_kmer_encode_batch() {
        let encoder = KmerEncoder::new(3, false, EncodingType::DNA).unwrap();
        let sequences = vec![b"ACGTACGT".as_ref(), b"AAACCCGGG".as_ref()];

        let batch = encoder.encode_batch(&sequences).unwrap();

        assert_eq!(batch.shape(), &[2, 64]);
        assert_eq!(batch.row(0).sum(), 6.0);
        assert_eq!(batch.row(1).sum(), 7.0);
    }

    #[test]
    fn test_kmer_encode_empty_batch() {
        let encoder = KmerEncoder::new(3, false, EncodingType::DNA).unwrap();
        let sequences: Vec<&[u8]> = vec![];

        let batch = encoder.encode_batch(&sequences).unwrap();
        assert_eq!(batch.shape(), &[0, 64]);
    }

    #[test]
    fn test_kmer_encode_case_insensitive() {
        let encoder = KmerEncoder::new(3, false, EncodingType::DNA).unwrap();
        assert_eq!(
            encoder.encode(b"ACGT").unwrap(),
            encoder.encode(b"acgt").unwrap()
        );
    }
}

// Python bindings
#[cfg(feature = "python")]
pub mod python {
    use super::*;
    use numpy::{PyArray1, PyArray2};
    use pyo3::prelude::*;
    use pyo3_stub_gen::derive::*;

    /// Python wrapper for KmerEncoder.
    ///
    /// Encodes biological sequences as k-mer frequency vectors.
    #[gen_stub_pyclass]
    #[pyclass(name = "KmerEncoder", module = "deepbiop.core")]
    pub struct PyKmerEncoder {
        inner: KmerEncoder,
    }

    #[gen_stub_pymethods]
    #[pymethods]
    impl PyKmerEncoder {
        /// Create a new k-mer encoder.
        ///
        /// Args:
        ///     k: K-mer length
        ///     canonical: Whether to use canonical k-mers (k-mer and reverse complement are the same)
        ///     encoding_type: Type of sequence ("dna", "rna", or "protein")
        ///
        /// Returns:
        ///     A new KmerEncoder instance
        ///
        /// Raises:
        ///     ValueError: If k is 0, the vocabulary would exceed 16M k-mers,
        ///         or canonical is requested for protein sequences
        #[new]
        pub fn new(k: usize, canonical: bool, encoding_type: &str) -> PyResult<Self> {
            let enc_type = encoding_type
                .parse::<EncodingType>()
                .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;

            Ok(Self {
                inner: KmerEncoder::new(k, canonical, enc_type)
                    .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?,
            })
        }

        /// Encode a single sequence as a k-mer frequency vector.
        ///
        /// Args:
        ///     sequence: The sequence to encode (bytes)
        ///
        /// Returns:
        ///     NumPy array of shape [num_possible_kmers] with k-mer counts
        ///
        /// Raises:
        ///     ValueError: If the sequence contains invalid characters
        #[pyo3(name = "encode")]
        pub fn encode<'py>(
            &mut self,
            py: Python<'py>,
            sequence: Vec<u8>,
        ) -> PyResult<Bound<'py, PyArray1<f32>>> {
            let encoded = self
                .inner
                .encode(&sequence)
                .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;

            Ok(PyArray1::from_array(py, &encoded))
        }

        /// Encode multiple sequences in parallel.
        ///
        /// Args:
        ///     sequences: List of sequences to encode (list of bytes)
        ///
        /// Returns:
        ///     NumPy array of shape [num_sequences, num_possible_kmers]
        ///
        /// Raises:
        ///     ValueError: If any sequence fails to encode
        #[pyo3(name = "encode_batch")]
        pub fn encode_batch<'py>(
            &mut self,
            py: Python<'py>,
            sequences: Vec<Vec<u8>>,
        ) -> PyResult<Bound<'py, PyArray2<f32>>> {
            let seq_refs: Vec<&[u8]> = sequences.iter().map(|s| s.as_slice()).collect();

            // Release GIL for parallel processing with Rayon
            let encoded = py
                .detach(|| self.inner.encode_batch(&seq_refs))
                .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;

            Ok(PyArray2::from_array(py, &encoded))
        }

        /// Get the k-mer length.
        #[pyo3(name = "k")]
        pub fn k(&self) -> usize {
            self.inner.k()
        }

        /// Check if using canonical k-mers.
        #[pyo3(name = "is_canonical")]
        pub fn is_canonical(&self) -> bool {
            self.inner.is_canonical()
        }

        /// Get the encoding type.
        #[pyo3(name = "encoding_type")]
        pub fn encoding_type(&self) -> String {
            match self.inner.encoding_type() {
                EncodingType::DNA => "dna".to_string(),
                EncodingType::RNA => "rna".to_string(),
                EncodingType::Protein => "protein".to_string(),
            }
        }

        /// String representation.
        #[pyo3(name = "__repr__")]
        pub fn repr(&self) -> String {
            format!(
                "KmerEncoder(k={}, canonical={}, encoding_type='{}')",
                self.k(),
                self.is_canonical(),
                self.encoding_type()
            )
        }
    }
}