Advertisement
CeaselessSoul

Codon Solver

Mar 10th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         HashMap<String, Integer> codons = codons("atatcgcata");
  6.         System.out.println(codons);
  7.     }
  8.  
  9.     // Create the static method that returns a HashMap with a String as the key and an Integer as the value.
  10.     // The parameter nucleotides is a string that we need to find the frequency of.
  11.     public static HashMap<String, Integer> codons(String nucleotides) {
  12.         // We need to create the HashMap to store the frequencies in.
  13.         HashMap<String, Integer> frequency = new HashMap<>();
  14.  
  15.         // Create the for loop that starts at the beginning o the nucleotides string and ends two characters before the
  16.         // end of it. This is so we don't get an out of bounds substring.
  17.         for (int index = 0; index < nucleotides.length() - 2; index++) {
  18.             // Create the key to search for in the frequency HashMap.
  19.             // Get the three characters starting from index ending at index + 3.
  20.  
  21.             /*
  22.                 *(Assume nucleotides is atatcgcata)*
  23.                 How does this work? Well, at the start of the for loop index is 0, the beginning of the string.
  24.  
  25.                 So, first iteration index = 0
  26.                 substring(0, 0 + 3) -> substring(0, 3)
  27.                 is going to be ata
  28.  
  29.                 Second iteration index = 1
  30.                 substring(1, 1 + 3) -> substring(1, 4)
  31.                 is going to be tat (note the index now starts at 1, so the substring is offset by 1 character)
  32.  
  33.                 Third iteration index = 2
  34.                 substring(2, 2 + 3) -> substring(2, 5)
  35.                 is going to be atc (note the index now starts at 2, so the substring is offset by 2 characters)
  36.  
  37.                 fourth iteration index = 3
  38.                 substring(3, 3 + 3) -> substring(3, 6)
  39.                 is going to be tcg (note the index now starts at 3, so the substring is offset by 3 characters)
  40.  
  41.                 fifth iteration index = 4
  42.                 substring(4, 4 + 3) -> substring(4, 7)
  43.                 is going to be cgc (note the index now starts at 4, so the substring is offset by 3 characters)
  44.  
  45.                 ... and so on. See if you can finish the for loop.
  46.             */
  47.             String key = nucleotides.substring(index, index + 3);
  48.  
  49.             // Now to record how many times it shows up. First we need to check if the key already exists in the
  50.             // frequency table. If it does exist (aka if it does not equal null (!= null)) then we add one to the
  51.             // frequency since it has already showed up.
  52.             if (frequency.get(key) != null) {
  53.                 frequency.put(key, frequency.get(key) + 1);
  54.             } else {
  55.                 // In this case, the key does not exist so we need to add it to the frequency HashMap,
  56.                 // that means he need to set the key in the HashMap to 1 since it shows up at least once.
  57.                 frequency.put(key, 1);
  58.             }
  59.         }
  60.  
  61.         return frequency;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement