rustkmer 0.5.2

High-performance k-mer counting tool in Rust
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
//! K-mer encoding and decoding functions
//!
//! Provides efficient bit-packed encoding of DNA sequences for k-mer counting.

use crate::error::KmerError;

/// DNA base encoding constants
const A: u8 = 0; // A = 00
const C: u8 = 1; // C = 01
const G: u8 = 2; // G = 10
const T: u8 = 3; // T = 11

/// Maximum k-mer size that fits in a u64 (64 / 2 = 32)
pub const MAX_KMER_SIZE_IN_U64: usize = 32;

/// Maximum k-mer size that fits in a u128 (128 / 2 = 64)
pub const MAX_KMER_SIZE_IN_U128: usize = 64;

/// Encode a DNA sequence into a packed bit representation
///
/// # Arguments
/// * `sequence` - DNA sequence string (must contain only A, C, G, T)
///
/// # Returns
/// * `Ok(u64)` - Packed representation if valid
/// * `Err(KmerError)` - Error if invalid characters found
///
/// # Examples
/// ```
/// use rustkmer::kmer::encoding::encode_kmer;
///
/// let encoded = encode_kmer("ATGC").unwrap();
/// // ATGC -> 00111001 (A=00, T=11, G=10, C=01)
/// // Step by step: A(00)->0, T(11)->3, G(10)->14, C(01)->57
/// assert_eq!(encoded, 57);
/// ```
pub fn encode_kmer(sequence: &str) -> Result<u64, KmerError> {
    encode_kmer_bytes(sequence.as_bytes())
}

/// Encode a DNA sequence from byte slice into a packed bit representation
///
/// # Arguments
/// * `sequence` - DNA sequence as bytes (must contain only A, C, G, T, case insensitive)
///
/// # Returns
/// * `Ok(u64)` - Packed representation if valid
/// * `Err(KmerError)` - Error if invalid characters found
pub fn encode_kmer_bytes(sequence: &[u8]) -> Result<u64, KmerError> {
    if sequence.len() > MAX_KMER_SIZE_IN_U64 {
        return Err(KmerError::InvalidKmerSize(
            (sequence.len() as u32).try_into().unwrap_or(u32::MAX),
        ));
    }

    let mut encoded = 0u64;
    let mut pos = sequence.len();

    for (i, &byte) in sequence.iter().enumerate() {
        let base = match byte.to_ascii_uppercase() {
            b'A' => A,
            b'C' => C,
            b'G' => G,
            b'T' => T,
            _ => {
                return Err(KmerError::InvalidCharacter {
                    pos: i,
                    char: char::from(byte),
                })
            }
        };

        // Shift to make room for 2 bits, then add the base
        encoded <<= 2;
        encoded |= base as u64;
        pos -= 1;
    }

    // Shift remaining bits to the left
    encoded <<= pos * 2;

    Ok(encoded)
}

/// Decode a packed k-mer back into a DNA sequence string
///
/// # Arguments
/// * `encoded` - Packed k-mer representation
/// * `length` - Length of the k-mer (number of bases)
///
/// # Returns
/// DNA sequence string
///
/// # Examples
/// ```
/// use rustkmer::kmer::encoding::{encode_kmer, decode_kmer};
///
/// let original = "ATGC";
/// let encoded = encode_kmer(original).unwrap();
/// let decoded = decode_kmer(encoded, original.len());
/// assert_eq!(decoded, original);
/// ```
pub fn decode_kmer(encoded: u64, length: usize) -> String {
    if length > MAX_KMER_SIZE_IN_U64 {
        return String::new();
    }

    let mut result = String::with_capacity(length);

    // Start from the most significant bits
    let bits_to_shift = (64 - (length * 2)) as u32;
    let mut encoded = encoded << bits_to_shift;

    for _ in 0..length {
        let base = (encoded >> 62) & 0b11;
        let char = match base {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            3 => 'T',
            _ => 'N', // Should not happen with valid encoding
        };
        result.push(char);
        encoded <<= 2;
    }

    result
}

/// Get the reverse complement of a packed k-mer
///
/// # Arguments
/// * `encoded` - Packed k-mer representation
/// * `length` - Length of the k-mer
///
/// # Returns
/// Packed reverse complement representation
pub fn reverse_complement(encoded: u64, length: usize) -> u64 {
    if length > MAX_KMER_SIZE_IN_U64 {
        return 0;
    }

    let mut rc = 0u64;

    // Process each base from right to left (reverse order)
    // Extract from least significant bits
    let mut temp_encoded = encoded;
    for _ in 0..length {
        // Extract the rightmost 2 bits (least significant base)
        let base = temp_encoded & 0b11;

        // Complement and add to result (building from left to right)
        let complement = 3 - base; // 3-base for complement (A<->T, C<->G, G<->C, T<->A)
        rc <<= 2;
        rc |= complement;

        // Shift to get next base (right shift removes the base we just processed)
        temp_encoded >>= 2;
    }

    rc
}

/// Validate that a DNA sequence contains only valid characters
///
/// # Arguments
/// * `sequence` - DNA sequence string
///
/// # Returns
/// `true` if valid, `false` if contains invalid characters
pub fn validate_sequence(sequence: &str) -> bool {
    sequence
        .chars()
        .all(|ch| matches!(ch.to_ascii_uppercase(), 'A' | 'C' | 'G' | 'T'))
}

/// Check if a sequence contains ambiguous bases (N)
///
/// # Arguments
/// * `sequence` - DNA sequence string
///
/// # Returns
/// `true` if contains ambiguous bases, `false` if all valid
pub fn has_ambiguous_bases(sequence: &str) -> bool {
    !sequence
        .chars()
        .all(|ch| matches!(ch.to_ascii_uppercase(), 'A' | 'C' | 'G' | 'T'))
}

// ========== U128 ENCODING FUNCTIONS ==========

/// Encode a DNA sequence into a packed u128 representation
///
/// Supports k-mers up to length 64
///
/// # Arguments
/// * `sequence` - DNA sequence string (must contain only A, C, G, T)
///
/// # Returns
/// * `Ok(u128)` - Packed representation if valid
/// * `Err(KmerError)` - Error if invalid characters found
///
/// # Examples
/// ```
/// use rustkmer::kmer::encoding::encode_kmer_u128;
///
/// let encoded = encode_kmer_u128("ATGC").unwrap();
/// // ATGC -> 00111001 (A=00, T=11, G=10, C=01)
/// assert_eq!(encoded, 57);
/// ```
pub fn encode_kmer_u128(sequence: &str) -> Result<u128, KmerError> {
    encode_kmer_bytes_u128(sequence.as_bytes())
}

/// Encode a DNA sequence from byte slice into a packed u128 representation
///
/// # Arguments
/// * `sequence` - DNA sequence as bytes (must contain only A, C, G, T, case insensitive)
///
/// # Returns
/// * `Ok(u128)` - Packed representation if valid
/// * `Err(KmerError)` - Error if invalid characters found
pub fn encode_kmer_bytes_u128(sequence: &[u8]) -> Result<u128, KmerError> {
    if sequence.is_empty() || sequence.len() > MAX_KMER_SIZE_IN_U128 {
        return Err(KmerError::InvalidKmerSize(
            (sequence.len() as u32).try_into().unwrap_or(u32::MAX),
        ));
    }

    let mut encoded: u128 = 0;
    let length = sequence.len();

    // Start from the most significant bits
    let bits_to_shift = (128 - (length * 2)) as u32;
    encoded <<= bits_to_shift;

    for &byte in sequence {
        let base = byte.to_ascii_uppercase();
        let value = match base {
            b'A' => A,
            b'C' => C,
            b'G' => G,
            b'T' => T,
            _ => {
                return Err(KmerError::InvalidCharacter {
                    pos: encoded.trailing_zeros() as usize / 2,
                    char: byte as char,
                });
            }
        };

        encoded = (encoded << 2) | (value as u128);
    }

    Ok(encoded)
}

/// Decode a packed u128 representation back to a DNA sequence
///
/// # Arguments
/// * `encoded` - Packed k-mer representation
/// * `length` - Length of the original k-mer
///
/// # Returns
/// The decoded DNA sequence
pub fn decode_kmer_u128(encoded: u128, length: usize) -> String {
    if length == 0 || length > MAX_KMER_SIZE_IN_U128 {
        return String::new();
    }

    let mut result = String::with_capacity(length);

    // Start from the most significant bits
    let bits_to_shift = (128 - (length * 2)) as u32;
    let mut encoded = encoded << bits_to_shift;

    for _ in 0..length {
        let base = (encoded >> 126) & 0b11;
        let char = match base {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            3 => 'T',
            _ => 'N', // Should not happen with valid encoding
        };
        result.push(char);
        encoded <<= 2;
    }

    result
}

/// Encode a DNA sequence using u64 if possible, otherwise u128
///
/// This function automatically chooses the appropriate encoding based on k-mer size.
/// For k ≤ 32, it uses u64 encoding. For k > 32, it uses u128 encoding.
///
/// # Arguments
/// * `sequence` - DNA sequence string (must contain only A, C, G, T)
///
/// # Returns
/// Tuple of (encoded_value, k_size, is_u128)
pub fn encode_kmer_auto(sequence: &str) -> (u128, u8, bool) {
    if sequence.len() <= MAX_KMER_SIZE_IN_U64 {
        let encoded_u64 = encode_kmer_u64(sequence).unwrap_or(0);
        (encoded_u64 as u128, sequence.len() as u8, false)
    } else {
        let encoded_u128 = encode_kmer_u128(sequence).unwrap_or(0);
        (encoded_u128, sequence.len() as u8, true)
    }
}

/// Legacy functions for u64 encoding (renamed for clarity)
pub fn encode_kmer_u64(sequence: &str) -> Result<u64, KmerError> {
    encode_kmer(sequence)
}

pub fn decode_kmer_u64(encoded: u64, length: usize) -> String {
    decode_kmer(encoded, length)
}

/// Get the reverse complement of a packed u128 k-mer
///
/// # Arguments
/// * `encoded` - Packed k-mer representation
/// * `length` - Length of the k-mer
///
/// # Returns
/// Packed reverse complement representation
pub fn reverse_complement_u128(encoded: u128, length: usize) -> u128 {
    if length > MAX_KMER_SIZE_IN_U128 {
        return 0;
    }

    let mut rc = 0u128;

    // Process each base from right to left (reverse order)
    // Extract from least significant bits
    let mut temp_encoded = encoded;
    for _ in 0..length {
        // Extract the rightmost 2 bits (least significant base)
        let base = temp_encoded & 0b11;

        // Complement and add to result (building from left to right)
        let complement = 3 - base; // 3-base for complement (A<->T, C<->G, G<->C, T<->A)
        rc <<= 2;
        rc |= complement;

        // Shift to get next base (right shift removes the base we just processed)
        temp_encoded >>= 2;
    }

    rc
}

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

    #[test]
    fn test_encode_decode_simple() {
        let sequence = "ATGC";
        let encoded = encode_kmer(sequence).unwrap();
        let decoded = decode_kmer(encoded, sequence.len());
        assert_eq!(decoded, sequence);
    }

    #[test]
    fn test_encode_decode_longer() {
        let sequence = "ATGCGATGCGATGCGATGCGATGCGATGCGATGC";
        // Use u128 encoding for sequences > 32 chars
        let encoded = encode_kmer_u128(sequence).unwrap();
        let decoded = decode_kmer_u128(encoded, sequence.len());
        assert_eq!(decoded, sequence);
    }

    #[test]
    fn test_reverse_complement() {
        let sequence = "ATGC";
        let encoded = encode_kmer(sequence).unwrap();
        let rc = reverse_complement(encoded, sequence.len());
        let rc_decoded = decode_kmer(rc, sequence.len());
        assert_eq!(rc_decoded, "GCAT");
    }

    #[test]
    fn test_invalid_character() {
        let result = encode_kmer("ATXG");
        assert!(result.is_err());
        if let Err(KmerError::InvalidCharacter { pos, char: c }) = result {
            assert_eq!(pos, 2);
            assert_eq!(c, 'X');
        } else {
            panic!("Expected InvalidCharacter error");
        }
    }

    #[test]
    fn test_too_long_kmer() {
        let long_seq = "A".repeat(33);
        let result = encode_kmer(&long_seq);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_sequence() {
        assert!(validate_sequence("ATGC"));
        assert!(validate_sequence("atgc"));
        assert!(!validate_sequence("ATXG"));
        assert!(validate_sequence("")); // Empty string is valid
    }

    #[test]
    fn test_has_ambiguous_bases() {
        assert!(!has_ambiguous_bases("ATGC"));
        assert!(has_ambiguous_bases("ATNG"));
        assert!(!has_ambiguous_bases("")); // Empty string has no ambiguous bases
    }

    #[test]
    fn test_canonical_property() {
        // Test that reverse complement of reverse complement equals original
        let sequence = "ATGCGAT";
        let encoded = encode_kmer(sequence).unwrap();
        let rc = reverse_complement(encoded, sequence.len());
        let rc_rc = reverse_complement(rc, sequence.len());
        assert_eq!(encoded, rc_rc);
    }

    #[test]
    fn test_bit_patterns() {
        // Test specific bit patterns
        let encoded_a = encode_kmer("A").unwrap();
        let encoded_t = encode_kmer("T").unwrap();
        let encoded_g = encode_kmer("G").unwrap();
        let encoded_c = encode_kmer("C").unwrap();

        // A = 00, T = 11, G = 10, C = 01
        assert_eq!((encoded_a & 0b11) as u8, A);
        assert_eq!((encoded_t & 0b11) as u8, T);
        assert_eq!((encoded_g & 0b11) as u8, G);
        assert_eq!((encoded_c & 0b11) as u8, C);
    }
}