From 3ba628ac78432d4c6cabc4a2bd4217471511c8b1 Mon Sep 17 00:00:00 2001 From: rav4s Date: Sat, 20 Feb 2021 18:04:43 -0600 Subject: [PATCH] Make sure first stop codon is used --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 59973f3..5e7348b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,25 +47,52 @@ fn break_into_codons(inter_rna: 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") { - println!("UAA stop codon found!"); - let stop_index = inter_codons.iter().position(|r| r == "UAA").unwrap(); - return stop_index; + stop_index_1 = inter_codons.iter().position(|r| r == "UAA").unwrap(); } - else if inter_codons.iter().any(|i| i == "UAG") { - println!("UAG stop codon found!"); - let stop_index = inter_codons.iter().position(|r| r == "UAG").unwrap(); - return stop_index; + if inter_codons.iter().any(|i| i == "UAG") { + stop_index_2 = inter_codons.iter().position(|r| r == "UAG").unwrap(); } - else if inter_codons.iter().any(|i| i == "UGA") { - println!("UGA stop codon found!"); - let stop_index = inter_codons.iter().position(|r| r == "UGA").unwrap(); - return stop_index; + 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) {