basebits 1.2.0

A library for encoding DNA into u64 to allow for constant time hamming distance calculations.
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
/// Encode a DNA string of up to 21 bases as a u64 for fast hamming distance calculations.
/// Each BaseBits will take up u64 X 2 + usize amount of space. It works by having encodings for A,
/// C, T, and G that are all dist 2 away from eachother. A sequence is encoded into a u64 by
/// setting the bits for each character. Any unrecognized character is treated as an N. N's are
/// encoded as 001, but are also tracked speratalty to allow for two different methods of counting.
/// N's can be treated as wildcards by using the `hamming_dist_nany` method, or they can be treated
/// like a character by using the `hamming_dist_none` method.
///
/// Generally speaking, if you are going to compare against a string more than 4 times, it is worth
/// the cost of encoding it and using this package.
use std::fmt;
use std::str;
use std::u64;

pub const ENCODING_DIST: u32 = 2;
pub const ENCODING_LENGTH: u32 = 3;
pub const CONTAINER_WIDTH: u32 = 64;
pub const MAX_BASES: usize = (CONTAINER_WIDTH / ENCODING_LENGTH) as usize;
pub const UNDETERMINED: u64 = 0b100;
//pub const ANY: u64 = 0b111;
pub const MAX_VAL: u64 = u64::MAX;

struct Bases;
impl Bases {
    const A: u64 = 0b000;
    const C: u64 = 0b110;
    const T: u64 = 0b101;
    const G: u64 = 0b011;
    const N: u64 = UNDETERMINED;
}

/// A BaseBits encoding
#[derive(Hash, PartialEq, Eq, Debug, Copy, Clone)]
pub struct BaseBits {
    /// The u64 holding the encoding
    pub code: u64,
    /// The u64 holding an inverse encoding of N's
    nbits: u64,
    /// The length of the original input
    len: usize,
}

impl BaseBits {
    /// Create a new BaseBits object.
    pub fn new(seq: &[u8]) -> Result<BaseBits, &'static str> {
        let mut code: u64 = 0;
        let mut nbits: u64 = !0b0;
        let len = seq.len();
        if len > MAX_BASES {
            return Err("Length of string to encode exceeds MAX_BASES");
        }
        for c in seq.iter() {
            let base = match c {
                b'A' => Bases::A,
                b'C' => Bases::C,
                b'T' => Bases::T,
                b'G' => Bases::G,
                _ => Bases::N,
            };

            code = (code << ENCODING_LENGTH) | base;
            nbits = match base {
                Bases::N => (nbits << ENCODING_LENGTH) | 0b000,
                _ => (nbits << ENCODING_LENGTH) | 0b111,
            }
        }
        Ok(BaseBits { code, nbits, len })
    }

    /// Decode a BaseBits object into a string
    pub fn decode(&self) -> Vec<u8> {
        let mut s = Vec::new();
        let mut code = self.code;
        for _ in 0..self.len {
            let base = extract_bits(code, ENCODING_LENGTH);
            code = code >> ENCODING_LENGTH;
            s.push(match base {
                Bases::A => b'A',
                Bases::C => b'C',
                Bases::T => b'T',
                Bases::G => b'G',
                _ => b'N',
            });
        }
        s.into_iter().rev().collect()
    }
}

impl fmt::Display for BaseBits {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", str::from_utf8(&self.decode()).unwrap())
    }
}

/// Compute hamming distance between two strings, count N's as any character
#[inline]
pub fn hamming_dist_nany(alpha: &BaseBits, beta: &BaseBits) -> u32 {
    ((alpha.code ^ beta.code) & (alpha.nbits & beta.nbits)).count_ones() / ENCODING_DIST
}

/// Compute hamming distace but N's as mismatches. An N - N will still count as a mismatch
#[inline]
pub fn hamming_dist_none(alpha: &BaseBits, beta: &BaseBits) -> u32 {
    let nbits_and = alpha.nbits & beta.nbits;
    (((alpha.code ^ beta.code) & nbits_and).count_ones() / ENCODING_DIST)
        + ((!nbits_and).count_ones() / ENCODING_LENGTH)
}

/// Extract 'k' bits from the end of a u64 integer
#[inline]
fn extract_bits(n: u64, k: u32) -> u64 {
    !(!0u64 << k) & n
}

// Hamming distance functions that don't depend on BaseBits types
pub mod hamming {

    /// If you have encoded a string using hamming codes, this should work
    #[inline]
    pub fn hamming_code(alpha: u64, beta: u64) -> u32 {
        (alpha ^ beta).count_ones() / 2
    }

    /// Classical hamming distance on strings. Skips length check and will stop comparing after
    /// alpha is exhuasted
    pub fn hamming_str(alpha: &str, beta: &str) -> u32 {
        // skip length check. will default to up to length of alpha
        let mut dist = 0;
        for (a, b) in alpha.chars().zip(beta.chars()) {
            if a != b {
                dist += 1
            }
        }
        dist
    }
}

#[cfg(test)]
mod tests {
    use super::hamming::*;
    use super::*;
    #[test]
    fn test_hamming_str_dist() {
        assert_eq!(hamming_str("ACTG", "ACTT"), 1);
        assert_eq!(hamming_str("ACTG", "ACTTT"), 1);
    }

    #[test]
    fn test_base_bits() {
        let alpha = BaseBits::new(b"ACTG").unwrap();
        let beta = BaseBits::new(b"ACTT").unwrap();
        assert_eq!(hamming_dist_nany(&alpha, &beta), 1);
    }

    #[test]
    fn test_single_bases() {
        // Selfs
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"A").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"G").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"C").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"T").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"N").unwrap(), &BaseBits::new(b"N").unwrap()),
            0
        );

        // Others
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"N").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"N").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"N").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_nany(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"N").unwrap()),
            0
        );
        // Selfs
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"A").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"G").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"C").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"T").unwrap()),
            0
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"N").unwrap(), &BaseBits::new(b"N").unwrap()),
            1
        );

        // Others
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"A").unwrap(), &BaseBits::new(b"N").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"G").unwrap(), &BaseBits::new(b"N").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"T").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"C").unwrap(), &BaseBits::new(b"N").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"A").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"G").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"C").unwrap()),
            1
        );
        assert_eq!(
            hamming_dist_none(&BaseBits::new(b"T").unwrap(), &BaseBits::new(b"N").unwrap()),
            1
        );
    }

    #[test]
    fn test_case_n_hamming() {
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"NCTG").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );
    }

    #[test]
    fn test_cases_bb_hamming() {
        // Test N encoding
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"ACTN").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );

        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"ACTN").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            1
        );

        // Test N encoding
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"NCTG").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );

        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"NCTG").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            1
        );

        // Test that unkown chars treated like Ns
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"ACT9").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );

        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"ACT9").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            1
        );
        // Test regular equality
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"ACTG").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );
        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"ACTG").unwrap(),
                &BaseBits::new(b"ACTG").unwrap()
            ),
            0
        );

        // Test Other string
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"GATACA").unwrap(),
                &BaseBits::new(b"GATACT").unwrap()
            ),
            1
        );
        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"GATACA").unwrap(),
                &BaseBits::new(b"GATACT").unwrap()
            ),
            1
        );
        assert_eq!(
            hamming_dist_nany(
                &BaseBits::new(b"CATACAGATACTTCCATAGCT").unwrap(),
                &BaseBits::new(b"GATACAGATACTTCCATAGCA").unwrap()
            ),
            2
        );
        assert_eq!(
            hamming_dist_none(
                &BaseBits::new(b"CATACAGATACTTCCATAGCT").unwrap(),
                &BaseBits::new(b"GATACAGATACTTCCATAGCA").unwrap()
            ),
            2
        );
        // This one should fail since it overflows and wraps around. needs thought
        //assert_eq!(hamming_dist(&BaseBits::new("TATACAGATACTTCCATAGCATC"),
        //&BaseBits::new("GATACAGATACAACNATAGCATT")), 4);
    }

    #[test]
    fn test_bb_to_string() {
        let alpha = BaseBits::new(b"GCTAN").unwrap();
        let beta = BaseBits::new(b"ACTGN").unwrap();
        assert_eq!(alpha.to_string(), "GCTAN".to_string());
        assert_eq!(beta.to_string(), "ACTGN".to_string());

        let long = BaseBits::new(b"GATACAGATACAACNATAGCA").unwrap();
        assert_eq!(long.to_string(), "GATACAGATACAACNATAGCA".to_string());
    }

    #[test]
    fn test_encoding() {
        let bb = BaseBits::new(b"ACTG").unwrap();
        assert_eq!(bb.code, 0b000110101011);
    }
}