From 41f573fbca6dab8b039dfe2ad8512146bf88c664 Mon Sep 17 00:00:00 2001 From: rav4s Date: Tue, 20 Apr 2021 22:23:28 -0500 Subject: [PATCH] Added some comments --- src/lib.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 58d187d..c26d146 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,10 @@ +// Copyright Ravi Shah 2021 +// You should've got the GPL v3.0 with this software +// If ya didn't, it's available here: +// https://www.gnu.org/licenses/gpl-3.0.en.html + +// Imports + mod utils; use wasm_bindgen::prelude::*; @@ -15,6 +22,8 @@ pub fn transcription(dna: String) -> String { let mut transcribed_vec: Vec = Vec::new(); for i in char_vec { + // Basically converts DNA strand to mRNA + match i { 'A' => transcribed_vec.push('U'), 'T' => transcribed_vec.push('A'), @@ -26,25 +35,30 @@ pub fn transcription(dna: String) -> String { } } + // Collect transcribed mRNA into a String let transcribed_string: String = transcribed_vec.into_iter().collect(); return transcribed_string; } pub fn find_start(messenger_rna: String) -> String { let start_codon = "AUG"; + // Find index of start codon let start_index = messenger_rna.find(start_codon).unwrap(); + // Remove anything before start codon let inter_rna: String = messenger_rna.chars().skip(start_index).collect(); return inter_rna; } pub fn break_into_codons(inter_rna: String) -> Vec { let sub_len = 3; + // Split into codons (chunks of 3 bases each) let subs = inter_rna.as_bytes() .chunks(sub_len) .map(str::from_utf8) .collect::, _>>() .unwrap(); + // Combine into a vector let mut string_vec: Vec = Vec::new(); for i in &subs { string_vec.push(i.to_string()); @@ -58,6 +72,7 @@ pub fn find_stop(inter_codons: &[String]) -> usize { let mut stop_index_2: usize = usize::MAX; let mut stop_index_3: usize = usize::MAX; + // Find position of any stop codons if inter_codons.iter().any(|i| i == "UAA") { stop_index_1 = inter_codons.iter().position(|r| r == "UAA").unwrap(); } @@ -76,6 +91,7 @@ pub fn find_stop(inter_codons: &[String]) -> usize { pub fn find_first(stop_index_1: usize, stop_index_2: usize, stop_index_3: usize) -> usize { let mut stop_index: usize = 1; + // Find which stop codon actually occured first if stop_index_1 < stop_index_2 { if stop_index_1 < stop_index_3{ println!("UAA stop codon found!"); @@ -105,6 +121,7 @@ pub fn find_first(stop_index_1: usize, stop_index_2: usize, stop_index_3: usize) pub fn translation(inter_codons: Vec) -> Vec { let mut amino_acids_list: Vec = Vec::new(); + // Matching codons to amino acids for i in inter_codons { match i.as_str() { "GUU" | "GUC" | "GUA" | "GUG" => amino_acids_list.push("Valine".to_string()), @@ -142,30 +159,32 @@ pub fn translation(inter_codons: Vec) -> Vec { pub fn main(mut strand: String) { println!("Enter the DNA strand to be transcribed and translated: "); - strand = strand.to_string().to_uppercase(); + strand = strand.to_string().to_uppercase(); // Convert to uppercase - let messenger_rna = transcription(strand); + let messenger_rna = transcription(strand); // Transcribe strand println!("The transcribed strand is: {}", messenger_rna); - let inter_rna = find_start(messenger_rna); + let inter_rna = find_start(messenger_rna); // Find start codon println!("{}", inter_rna); - let mut inter_codons = break_into_codons(inter_rna); - let mut stop_index = find_stop(&inter_codons); + let mut inter_codons = break_into_codons(inter_rna); // Break into codons + let mut stop_index = find_stop(&inter_codons); // Find index of stop codon stop_index = stop_index + 1; println!("{}", stop_index); - inter_codons.truncate(stop_index); - let amino_acids_list = translation(inter_codons); + inter_codons.truncate(stop_index); // Truncate at stop index + let amino_acids_list = translation(inter_codons); // Translate to amino acids // Use `web_sys`'s global `window` function to get a handle on the global // window object. let window = web_sys::window().expect("no global `window` exists"); let document = window.document().expect("should have a document on window"); - let codons_div = document.get_element_by_id("codons").expect("codons div should exist"); + let codons_div = document.get_element_by_id("codons").expect("codons div should exist"); // Get codons div + // Insert codons header into the DOM let val = document.create_element("h2").unwrap(); val.set_text_content(Some("Codons:")); codons_div.append_with_node_1(&val).unwrap(); print!("The translated amino acids are: "); + // Insert amino acids into the DOM for i in amino_acids_list { let val = document.create_element("p").unwrap(); val.set_text_content(Some(&i));