bio_seq/codec/
masked.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
//! Experimental 4-bit DNA encoding with masked bases
//! Includes `N`, `n`, `.`, `-`

use crate::codec::Codec;
use crate::seq::{ReverseComplement, Seq};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Codec)]
#[bits(4)]
#[repr(u8)]
pub enum Dna {
    A = 0b1000,
    C = 0b0100,
    G = 0b0010,
    T = 0b0001,

    #[display('a')]
    AMasked = 0b0111,
    #[display('c')]
    CMasked = 0b1011,
    #[display('g')]
    GMasked = 0b1101,
    #[display('t')]
    TMasked = 0b1110,

    N = 0b0000,
    #[display('n')]
    NMasked = 0b1111,

    #[display('-')]
    #[alt(0b0011)]
    Gap = 0b1100,

    #[display('.')]
    #[alt(0b0101)]
    Pad = 0b1010,

    #[display('?')]
    Unknown1 = 0b0110,

    #[display('!')]
    Unknown2 = 0b1001,
}

/*
impl Complement for Dna {
    /// This representation can be complemented by reversing the bit pattern
    fn comp(&self) -> Self {
        // reverse the bits
        Dna::unsafe_from_bits(b)
    }
}
*/

impl Dna {
    /// Flipping the bit pattern masks/unmasks this representation
    pub fn mask(&self) -> Self {
        let b = *self as u8 ^ 0b1111;
        Dna::unsafe_from_bits(b)
    }
}

impl Seq<Dna> {
    pub fn mask(&self) -> Self {
        Self::from(!self.bv.clone())
    }
}

impl ReverseComplement for Seq<Dna> {
    type Output = Self;

    fn revcomp(&self) -> Self {
        let mut bv = self.bv.clone();
        bv.reverse();
        Self::from(bv)
    }
}

#[cfg(test)]
mod tests {
    use crate::codec::masked;
    use crate::prelude::*;

    #[test]
    fn mask_sequence() {
        let seq = Seq::<masked::Dna>::try_from("A.TCGCgtcataN--A").unwrap();

        assert_ne!(seq.mask().to_string(), "a.tcgcGTGATAN--a".to_string());
        assert_eq!(seq.mask().to_string(), "a.tcgcGTCATAn--a".to_string());
    }

    #[test]
    fn masked_revcomp() {
        let seq = Seq::<masked::Dna>::try_from("A.TCGCgtcataN--A").unwrap();

        assert_ne!(seq.revcomp().to_string(), "T--NtaagacGCGA.T".to_string());
        assert_eq!(seq.revcomp().to_string(), "T--NtatgacGCGA.T".to_string());
    }
}