From a198f7f333697273ffc48576ecd8c1a600cba2d3 Mon Sep 17 00:00:00 2001 From: rav4s Date: Sat, 20 Feb 2021 19:35:11 -0600 Subject: [PATCH] Added rest of program and tests --- src/main.rs | 41 ++++++++++++- tests/test.rs | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 tests/test.rs diff --git a/src/main.rs b/src/main.rs index 5e7348b..d989ebb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,10 +95,41 @@ fn find_first(stop_index_1: usize, stop_index_2: usize, stop_index_3: usize) -> return stop_index; } -fn translation(inter_codons: Vec) { +fn translation(inter_codons: Vec) -> Vec { + let mut amino_acids_list: Vec = Vec::new(); + for i in inter_codons { - println!("{}", i); + match i.as_str() { + "GUU" | "GUC" | "GUA" | "GUG" => amino_acids_list.push("Valine".to_string()), + "GCU" | "GCC" | "GCA" | "GCG" => amino_acids_list.push("Alanine".to_string()), + "GAU" | "GAC" => amino_acids_list.push("Aspartic Acid".to_string()), + "GAA" | "GAG" => amino_acids_list.push("Glutamic Acid".to_string()), + "GGU" | "GGC" | "GGA" | "GGG" => amino_acids_list.push("Glycine".to_string()), + "UUU" | "UUC" => amino_acids_list.push("Phenylalanine".to_string()), + "UUA" | "UUG" | "CUU" | "CUC" | "CUA" | "CUG" => amino_acids_list.push("Leucine".to_string()), + "UCU" | "UCC" | "UCA" | "UCG" | "AGU" | "AGC" => amino_acids_list.push("Serine".to_string()), + "UAU" | "UAC" => amino_acids_list.push("Tyrosine".to_string()), + "UAA" | "UAG" => amino_acids_list.push("STOP".to_string()), + "UGU" | "UGC" => amino_acids_list.push("Cysteine".to_string()), + "UGA" => amino_acids_list.push("STOP".to_string()), + "UGG" => amino_acids_list.push("Tryptophan".to_string()), + "CCU" | "CCC" | "CCA" | "CCG" => amino_acids_list.push("Proline".to_string()), + "CAU" | "CAC" => amino_acids_list.push("Histidine".to_string()), + "CAA" | "CAG" => amino_acids_list.push("Glutamine".to_string()), + "CGU" | "CGC" | "CGA" | "CGG" | "AGA" | "AGG" => amino_acids_list.push("Arginine".to_string()), + "AUU" | "AUC" | "AUA" => amino_acids_list.push("Isoleucine".to_string()), + "AUG" => amino_acids_list.push("Methionine".to_string()), + "ACU" | "ACC" | "ACA" | "ACG" => amino_acids_list.push("Threonine".to_string()), + "AAU" | "AAC" => amino_acids_list.push("Asparginine".to_string()), + "AAA" | "AAG" => amino_acids_list.push("Lysine".to_string()), + _ => { + // println!("Incorrect char"); + break; + } + } } + + return amino_acids_list; } fn main() { @@ -121,5 +152,9 @@ fn main() { stop_index = stop_index + 1; println!("{}", stop_index); inter_codons.truncate(stop_index); - translation(inter_codons); + let amino_acids_list = translation(inter_codons); + print!("The translated amino acids are: "); + for i in amino_acids_list { + print!("{}, ", i); + } } diff --git a/tests/test.rs b/tests/test.rs new file mode 100644 index 0000000..255d6c0 --- /dev/null +++ b/tests/test.rs @@ -0,0 +1,163 @@ +#[cfg(test)] + +use std::io; +use std::str; +use std::process; + +fn transcription(dna: String) -> String { + let char_vec: Vec = dna.chars().collect(); + let mut transcribed_vec: Vec = Vec::new(); + for i in char_vec { + + match i { + 'A' => transcribed_vec.push('U'), + 'T' => transcribed_vec.push('A'), + 'C' => transcribed_vec.push('G'), + 'G' => transcribed_vec.push('C'), + _ => { + // println!("Incorrect char"); + break; + } + } + } + + let transcribed_string: String = transcribed_vec.into_iter().collect(); + return transcribed_string; +} + +fn find_start(messenger_rna: String) -> String { + let start_codon = "AUG"; + let start_index = messenger_rna.find(start_codon).unwrap(); + let inter_rna: String = messenger_rna.chars().skip(start_index).collect(); + return inter_rna; +} + +fn break_into_codons(inter_rna: String) -> Vec { + let sub_len = 3; + let subs = inter_rna.as_bytes() + .chunks(sub_len) + .map(str::from_utf8) + .collect::, _>>() + .unwrap(); + + let mut string_vec: Vec = Vec::new(); + for i in &subs { + string_vec.push(i.to_string()); + } + + return string_vec; +} + +fn find_stop(inter_codons: &[String]) -> usize { + let mut stop_index_1: usize = usize::MAX; + let mut stop_index_2: usize = usize::MAX; + let mut stop_index_3: usize = usize::MAX; + + if inter_codons.iter().any(|i| i == "UAA") { + stop_index_1 = inter_codons.iter().position(|r| r == "UAA").unwrap(); + } + if inter_codons.iter().any(|i| i == "UAG") { + stop_index_2 = inter_codons.iter().position(|r| r == "UAG").unwrap(); + } + if inter_codons.iter().any(|i| i == "UGA") { + stop_index_3 = inter_codons.iter().position(|r| r == "UGA").unwrap(); + } + + let stop_index = find_first(stop_index_1, stop_index_2, stop_index_3); + + return stop_index; +} + +fn find_first(stop_index_1: usize, stop_index_2: usize, stop_index_3: usize) -> usize { + let mut stop_index: usize = 1; + + if stop_index_1 < stop_index_2 { + if stop_index_1 < stop_index_3{ + println!("UAA stop codon found!"); + stop_index = stop_index_1; + } + } + else if stop_index_2 < stop_index_1 { + if stop_index_2 < stop_index_3 { + println!("UAG stop codon found!"); + stop_index = stop_index_2; + } + } + else if stop_index_3 < stop_index_1 { + if stop_index_3 < stop_index_2 { + println!("UGA stop codon found!"); + stop_index = stop_index_3; + } + } + else { + println!("No stop codon found!"); + process::exit(1); + } + + return stop_index; +} + +fn translation(inter_codons: Vec) -> Vec { + let mut amino_acids_list: Vec = Vec::new(); + + for i in inter_codons { + match i.as_str() { + "GUU" | "GUC" | "GUA" | "GUG" => amino_acids_list.push("Valine".to_string()), + "GCU" | "GCC" | "GCA" | "GCG" => amino_acids_list.push("Alanine".to_string()), + "GAU" | "GAC" => amino_acids_list.push("Aspartic Acid".to_string()), + "GAA" | "GAG" => amino_acids_list.push("Glutamic Acid".to_string()), + "GGU" | "GGC" | "GGA" | "GGG" => amino_acids_list.push("Glycine".to_string()), + "UUU" | "UUC" => amino_acids_list.push("Phenylalanine".to_string()), + "UUA" | "UUG" | "CUU" | "CUC" | "CUA" | "CUG" => amino_acids_list.push("Leucine".to_string()), + "UCU" | "UCC" | "UCA" | "UCG" | "AGU" | "AGC" => amino_acids_list.push("Serine".to_string()), + "UAU" | "UAC" => amino_acids_list.push("Tyrosine".to_string()), + "UAA" | "UAG" => amino_acids_list.push("STOP".to_string()), + "UGU" | "UGC" => amino_acids_list.push("Cysteine".to_string()), + "UGA" => amino_acids_list.push("STOP".to_string()), + "UGG" => amino_acids_list.push("Tryptophan".to_string()), + "CCU" | "CCC" | "CCA" | "CCG" => amino_acids_list.push("Proline".to_string()), + "CAU" | "CAC" => amino_acids_list.push("Histidine".to_string()), + "CAA" | "CAG" => amino_acids_list.push("Glutamine".to_string()), + "CGU" | "CGC" | "CGA" | "CGG" | "AGA" | "AGG" => amino_acids_list.push("Arginine".to_string()), + "AUU" | "AUC" | "AUA" => amino_acids_list.push("Isoleucine".to_string()), + "AUG" => amino_acids_list.push("Methionine".to_string()), + "ACU" | "ACC" | "ACA" | "ACG" => amino_acids_list.push("Threonine".to_string()), + "AAU" | "AAC" => amino_acids_list.push("Asparginine".to_string()), + "AAA" | "AAG" => amino_acids_list.push("Lysine".to_string()), + _ => { + // println!("Incorrect char"); + break; + } + } + } + + return amino_acids_list; +} + +fn test() { + println!("Enter the DNA strand to be transcribed and translated: "); + + let mut strand: String = "TACATGCCATACGAGACGAGCGCGCCTAAGCGGCGCAGACTCATGGTCATT".to_string(); + + strand = strand.to_uppercase(); + + let messenger_rna = transcription(strand); + println!("The transcribed strand is: {}", messenger_rna); + let inter_rna = find_start(messenger_rna); + println!("{}", inter_rna); + let mut inter_codons = break_into_codons(inter_rna); + let mut stop_index = find_stop(&inter_codons); + stop_index = stop_index + 1; + println!("{}", stop_index); + inter_codons.truncate(stop_index); + let amino_acids_list = translation(inter_codons); + print!("The translated amino acids are: "); + for i in amino_acids_list { + print!("{}, ", i); + } +} + +#[test] +fn main() { + test(); +} \ No newline at end of file