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.

160 lines
5.6 KiB

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