bio_seq/codec/iupac.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 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
//! 4-bit IUPAC nucleotide ambiguity codes
//!
//! IUPAC nucleotide ambiguity codes are represented with 4 bits
//!
//! | | A | C | G | T |
//! | - | - | - | - | - |
//! | A | 1 | 0 | 0 | 0 |
//! | C | 0 | 1 | 0 | 0 |
//! | G | 0 | 0 | 1 | 0 |
//! | T | 0 | 0 | 0 | 1 |
//! | Y | 0 | 1 | 0 | 1 |
//! | R | 1 | 0 | 1 | 0 |
//! | W | 1 | 0 | 0 | 1 |
//! | S | 0 | 1 | 1 | 0 |
//! | K | 0 | 0 | 1 | 1 |
//! | M | 1 | 1 | 0 | 0 |
//! | D | 1 | 0 | 1 | 1 |
//! | V | 1 | 1 | 1 | 0 |
//! | H | 1 | 1 | 0 | 1 |
//! | B | 0 | 1 | 1 | 1 |
//! | N | 1 | 1 | 1 | 1 |
//! | X/- | 0 | 0 | 0 | 0 |
//!
//! This means that we can treat each symbol as a set and we get meaningful bitwise operations:
//!
//! ```rust
//! use bio_seq::prelude::*;
//!
//! // Set union:
//! let union = iupac!("AS-GYTNAN") | iupac!("ANTGCAT-N");
//! assert_eq!(union, iupac!("ANTGYWNAN"));
//!
//! // Set intersection:
//! let intersection = iupac!("ACGTSWKMN") & iupac!("WKMSTNNAN");
//! assert_eq!(intersection, iupac!("A----WKAN"));
//! ```
//!
//! Which can be used to implement pattern matching:
//!
//! ```rust
//! use bio_seq::prelude::*;
//!
//! let seq = iupac!("AGCTNNCAGTCGACGTATGTA");
//! let pattern = iupac!("AYG");
//!
//! for slice in seq.windows(pattern.len()) {
//! if pattern.contains(slice) {
//! println!("{slice} matches pattern");
//! }
//! }
//!
//! // ACG matches pattern
//! // ATG matches pattern
//! ```
use crate::codec::{dna::Dna, Codec};
use crate::seq::{Seq, SeqArray, SeqSlice};
/*
const LTABLE: [u8; 256] = {
let mut table = [0; 256];
table[b'A' as usize] = 0b1000;
table[b'C' as usize] = 0b0100;
table[b'G' as usize] = 0b0010;
table[b'T' as usize] = 0b0001;
table[b'R' as usize] = 0b1010;
table[b'Y' as usize] = 0b0101;
table[b'S' as usize] = 0b0110;
table[b'W' as usize] = 0b1001;
table[b'K' as usize] = 0b0011;
table[b'M' as usize] = 0b1100;
table[b'B' as usize] = 0b0111;
table[b'D' as usize] = 0b1011;
table[b'H' as usize] = 0b1101;
table[b'V' as usize] = 0b1110;
table[b'N' as usize] = 0b1111;
table[b'-' as usize] = 0b0000;
table
};
*/
impl From<Iupac> for u8 {
fn from(b: Iupac) -> u8 {
b as u8
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Codec)]
#[bits(4)]
#[repr(u8)]
pub enum Iupac {
A = 0b1000,
C = 0b0100,
G = 0b0010,
T = 0b0001,
R = 0b1010,
Y = 0b0101,
S = 0b0110,
W = 0b1001,
K = 0b0011,
M = 0b1100,
B = 0b0111,
D = 0b1011,
H = 0b1101,
V = 0b1110,
N = 0b1111,
#[display('-')]
X = 0b0000,
}
impl From<Dna> for Iupac {
fn from(dna: Dna) -> Self {
match dna {
Dna::A => Iupac::A,
Dna::C => Iupac::C,
Dna::G => Iupac::G,
Dna::T => Iupac::T,
}
}
}
impl Seq<Iupac> {
pub fn contains(&self, rhs: &SeqSlice<Iupac>) -> bool {
if rhs.len() != self.len() {
return false;
}
self.as_ref() & rhs == rhs
}
}
impl<const N: usize, const W: usize> SeqArray<Iupac, N, W> {
pub fn contains(&self, rhs: &SeqSlice<Iupac>) -> bool {
if N != rhs.len() {
return false;
}
self.as_ref() & rhs == rhs
}
}
impl SeqSlice<Iupac> {
pub fn contains(&self, rhs: &SeqSlice<Iupac>) -> bool {
if self.len() != rhs.len() {
return false;
}
self & rhs == rhs
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn iupac_ops() {
let seq = iupac!("AGCTNNCAGTCGACGTATGTA");
let pattern = iupac!("AYG");
let matches: Vec<Seq<Iupac>> = seq
.windows(pattern.len())
.filter(|w| pattern.contains(w))
.collect();
assert_eq!(matches, vec![iupac!("ACG"), iupac!("ATG")])
}
}