#![deny(missing_docs)]
pub fn translate(seq: &[u8], delimiter: Option<char>) -> String {
let mut peptide = String::with_capacity(seq.len().checked_div(3).unwrap());
let delimit = delimiter.unwrap_or(char::from_u32(0).unwrap());
'outer: for triplet in seq.chunks_exact(3) {
for c in triplet {
if !c.is_ascii() {
peptide.push('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:char = if c1 == 4 || c2 == 4 || c3 == 4 {
'X'
} else {
AA_TABLE_CANONICAL[c1][c2][c3]
};
peptide.push_str(&amino_acid.to_string());
if delimit != '\0' { peptide.push_str(&delimit.to_string()) };
}
peptide.trim_end_matches(delimit).to_string()
}
pub fn translate3(seq: &[u8], delimiter: Option<char>) -> String {
let mut peptide = String::with_capacity(seq.len());
let delimit = delimiter.unwrap_or(char::from_u32(0).unwrap());
'outer: for triplet in seq.chunks_exact(3) {
for c in triplet {
if !c.is_ascii() {
peptide.push('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:&str = if c1 == 4 || c2 == 4 || c3 == 4 {
"X"
} else {
AA_TABLE_CANONICAL_3[c1][c2][c3]
};
peptide.push_str(amino_acid);
if delimit != '\0' { peptide.push_str(&delimit.to_string()) };
}
peptide.trim_end_matches(delimit).to_string()
}
pub fn translate_full(seq: &[u8], delimiter: Option<char>) -> String {
let mut peptide = String::with_capacity(seq.len()*4); let delimit = delimiter.unwrap_or(char::from_u32(0).unwrap());
'outer: for triplet in seq.chunks_exact(3) {
for c in triplet {
if !c.is_ascii() {
peptide.push('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:&str = if c1 == 4 || c2 == 4 || c3 == 4 {
"X"
} else {
AA_TABLE_CANONICAL_FULL[c1][c2][c3]
};
peptide.push_str(amino_acid);
if delimit != '\0' { peptide.push_str(&delimit.to_string()) };
}
peptide.trim_end_matches(delimit).to_string()
}
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 AA_TABLE_CANONICAL_3: [[[&str; 4]; 4]; 4] = [
[
["Lys", "Asn", "Lys", "Asn"], ["Thr", "Thr", "Thr", "Thr"], ["Arg", "Ser", "Arg", "Ser"], ["Ile", "Ile", "Met", "Ile"], ],
[
["Gln", "His", "Gln", "His"], ["Pro", "Pro", "Pro", "Pro"], ["Arg", "Arg", "Arg", "Arg"], ["Leu", "Leu", "Leu", "Leu"], ],
[
["Glu", "Asp", "Glu", "Asp"], ["Ala", "Ala", "Ala", "Ala"], ["Gly", "Gly", "Gly", "Gly"], ["Val", "Val", "Val", "Val"], ],
[
["*", "Tyr", "*", "Tyr"], ["Ser", "Ser", "Ser", "Ser"], ["*", "Cys", "Trp", "Cys"], ["Leu", "Phe", "Leu", "Phe"], ],
];
static AA_TABLE_CANONICAL_FULL: [[[&str; 4]; 4]; 4] = [
[
["Lysine", "Asparagine", "Lysine", "Asparagine"], ["Threonine", "Threonine", "Threonine", "Threonine"], ["Arginine", "Serine", "Arginine", "Serine"], ["Isoleucine", "Isoleucine", "Methionine", "Isoleucine"], ],
[
["Glutamine", "Histidine", "Glutamine", "His"], ["Proline", "Proline", "Proline", "Proline"], ["Arginine", "Arginine", "Arginine", "Arginine"], ["Leucine", "Leucine", "Leucine", "Leucine"], ],
[
["Glutamine", "Aspartic acid", "Glu", "Asp"], ["Alanine", "Alanine", "Alanine", "Alanine"], ["Glycine", "Glycine", "Glycine", "Glycine"], ["Valine", "Valine", "Valine", "Valine"], ],
[
["STOP", "Tyrosine", "STOP", "Tyrosine"], ["Serine", "Serine", "Serine", "Serine"], ["", "Cysteine", "Tryptophan", "Cysteine"], ["Leucine", "Phenylalanine", "Leucine", "Phenylalanine"], ],
];
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, ];