A simple, fast program which transcribes a DNA strand into messenger RNA (mRNA), which it then translates into amino acids, implemented in Rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.6 KiB

  1. #[cfg(test)]
  2. use std::io;
  3. use std::str;
  4. use std::process;
  5. fn transcription(dna: String) -> String {
  6. let char_vec: Vec<char> = dna.chars().collect();
  7. let mut transcribed_vec: Vec<char> = Vec::new();
  8. for i in char_vec {
  9. match i {
  10. 'A' => transcribed_vec.push('U'),
  11. 'T' => transcribed_vec.push('A'),
  12. 'C' => transcribed_vec.push('G'),
  13. 'G' => transcribed_vec.push('C'),
  14. _ => {
  15. // println!("Incorrect char");
  16. break;
  17. }
  18. }
  19. }
  20. let transcribed_string: String = transcribed_vec.into_iter().collect();
  21. return transcribed_string;
  22. }
  23. fn find_start(messenger_rna: String) -> String {
  24. let start_codon = "AUG";
  25. let start_index = messenger_rna.find(start_codon).unwrap();
  26. let inter_rna: String = messenger_rna.chars().skip(start_index).collect();
  27. return inter_rna;
  28. }
  29. fn break_into_codons(inter_rna: String) -> Vec<String> {
  30. let sub_len = 3;
  31. let subs = inter_rna.as_bytes()
  32. .chunks(sub_len)
  33. .map(str::from_utf8)
  34. .collect::<Result<Vec<&str>, _>>()
  35. .unwrap();
  36. let mut string_vec: Vec<String> = Vec::new();
  37. for i in &subs {
  38. string_vec.push(i.to_string());
  39. }
  40. return string_vec;
  41. }
  42. fn find_stop(inter_codons: &[String]) -> usize {
  43. let mut stop_index_1: usize = usize::MAX;
  44. let mut stop_index_2: usize = usize::MAX;
  45. let mut stop_index_3: usize = usize::MAX;
  46. if inter_codons.iter().any(|i| i == "UAA") {
  47. stop_index_1 = inter_codons.iter().position(|r| r == "UAA").unwrap();
  48. }
  49. if inter_codons.iter().any(|i| i == "UAG") {
  50. stop_index_2 = inter_codons.iter().position(|r| r == "UAG").unwrap();
  51. }
  52. if inter_codons.iter().any(|i| i == "UGA") {
  53. stop_index_3 = inter_codons.iter().position(|r| r == "UGA").unwrap();
  54. }
  55. let stop_index = find_first(stop_index_1, stop_index_2, stop_index_3);
  56. return stop_index;
  57. }
  58. fn find_first(stop_index_1: usize, stop_index_2: usize, stop_index_3: usize) -> usize {
  59. let mut stop_index: usize = 1;
  60. if stop_index_1 < stop_index_2 {
  61. if stop_index_1 < stop_index_3{
  62. println!("UAA stop codon found!");
  63. stop_index = stop_index_1;
  64. }
  65. }
  66. else if stop_index_2 < stop_index_1 {
  67. if stop_index_2 < stop_index_3 {
  68. println!("UAG stop codon found!");
  69. stop_index = stop_index_2;
  70. }
  71. }
  72. else if stop_index_3 < stop_index_1 {
  73. if stop_index_3 < stop_index_2 {
  74. println!("UGA stop codon found!");
  75. stop_index = stop_index_3;
  76. }
  77. }
  78. else {
  79. println!("No stop codon found!");
  80. process::exit(1);
  81. }
  82. return stop_index;
  83. }
  84. fn translation(inter_codons: Vec<String>) -> Vec<String> {
  85. let mut amino_acids_list: Vec<String> = Vec::new();
  86. for i in inter_codons {
  87. match i.as_str() {
  88. "GUU" | "GUC" | "GUA" | "GUG" => amino_acids_list.push("Valine".to_string()),
  89. "GCU" | "GCC" | "GCA" | "GCG" => amino_acids_list.push("Alanine".to_string()),
  90. "GAU" | "GAC" => amino_acids_list.push("Aspartic Acid".to_string()),
  91. "GAA" | "GAG" => amino_acids_list.push("Glutamic Acid".to_string()),
  92. "GGU" | "GGC" | "GGA" | "GGG" => amino_acids_list.push("Glycine".to_string()),
  93. "UUU" | "UUC" => amino_acids_list.push("Phenylalanine".to_string()),
  94. "UUA" | "UUG" | "CUU" | "CUC" | "CUA" | "CUG" => amino_acids_list.push("Leucine".to_string()),
  95. "UCU" | "UCC" | "UCA" | "UCG" | "AGU" | "AGC" => amino_acids_list.push("Serine".to_string()),
  96. "UAU" | "UAC" => amino_acids_list.push("Tyrosine".to_string()),
  97. "UAA" | "UAG" => amino_acids_list.push("STOP".to_string()),
  98. "UGU" | "UGC" => amino_acids_list.push("Cysteine".to_string()),
  99. "UGA" => amino_acids_list.push("STOP".to_string()),
  100. "UGG" => amino_acids_list.push("Tryptophan".to_string()),
  101. "CCU" | "CCC" | "CCA" | "CCG" => amino_acids_list.push("Proline".to_string()),
  102. "CAU" | "CAC" => amino_acids_list.push("Histidine".to_string()),
  103. "CAA" | "CAG" => amino_acids_list.push("Glutamine".to_string()),
  104. "CGU" | "CGC" | "CGA" | "CGG" | "AGA" | "AGG" => amino_acids_list.push("Arginine".to_string()),
  105. "AUU" | "AUC" | "AUA" => amino_acids_list.push("Isoleucine".to_string()),
  106. "AUG" => amino_acids_list.push("Methionine".to_string()),
  107. "ACU" | "ACC" | "ACA" | "ACG" => amino_acids_list.push("Threonine".to_string()),
  108. "AAU" | "AAC" => amino_acids_list.push("Asparginine".to_string()),
  109. "AAA" | "AAG" => amino_acids_list.push("Lysine".to_string()),
  110. _ => {
  111. // println!("Incorrect char");
  112. break;
  113. }
  114. }
  115. }
  116. return amino_acids_list;
  117. }
  118. fn test() {
  119. println!("Enter the DNA strand to be transcribed and translated: ");
  120. let mut strand: String = "TACATGCCATACGAGACGAGCGCGCCTAAGCGGCGCAGACTCATGGTCATT".to_string();
  121. strand = strand.to_uppercase();
  122. let messenger_rna = transcription(strand);
  123. println!("The transcribed strand is: {}", messenger_rna);
  124. let inter_rna = find_start(messenger_rna);
  125. println!("{}", inter_rna);
  126. let mut inter_codons = break_into_codons(inter_rna);
  127. let mut stop_index = find_stop(&inter_codons);
  128. stop_index = stop_index + 1;
  129. println!("{}", stop_index);
  130. inter_codons.truncate(stop_index);
  131. let amino_acids_list = translation(inter_codons);
  132. print!("The translated amino acids are: ");
  133. for i in amino_acids_list {
  134. print!("{}, ", i);
  135. }
  136. }
  137. #[test]
  138. fn main() {
  139. test();
  140. }