pub fn translate(seq: &[u8]) -> Vec<u8> {
let mut peptide = Vec::with_capacity(seq.len() / 3);
'outer: for triplet in seq.chunks_exact(3) {
for c in triplet {
if !c.is_ascii() {
peptide.push(b'X');
continue 'outer;
}
}
let c1 = ASCII_TO_INDEX[triplet[0] as usize];
let c2 = ASCII_TO_INDEX[triplet[1] as usize];
let c3 = ASCII_TO_INDEX[triplet[2] as usize];
let amino_acid = if c1 == 4 || c2 == 4 || c3 == 4 {
'X'
} else {
AA_TABLE_CANONICAL[c1][c2][c3]
};
let mut b = [0; 1];
let res = amino_acid.encode_utf8(&mut b);
peptide.push(res.as_bytes()[0]);
}
peptide
}
static AA_TABLE_CANONICAL: [[[char; 4]; 4]; 4] = [
[
['K', 'N', 'K', 'N'], ['T', 'T', 'T', 'T'], ['R', 'S', 'R', 'S'], ['I', 'I', 'M', 'I'], ],
[
['Q', 'H', 'Q', 'H'], ['P', 'P', 'P', 'P'], ['R', 'R', 'R', 'R'], ['L', 'L', 'L', 'L'], ],
[
['E', 'D', 'E', 'D'], ['A', 'A', 'A', 'A'], ['G', 'G', 'G', 'G'], ['V', 'V', 'V', 'V'], ],
[
['*', 'Y', '*', 'Y'], ['S', 'S', 'S', 'S'], ['*', 'C', 'W', 'C'], ['L', 'F', 'L', 'F'], ],
];
static ASCII_TO_INDEX: [usize; 128] = [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ];