Advertisement
IronTomman

Untitled

Jan 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package programming.set9.dna;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5.  
  6. public class DNAMatcher {
  7.  
  8.     private static final String[] bases = { "A", "C", "G", "T" };
  9.     private static final HashMap<Character, Character> baseCombinations = new HashMap<Character, Character>();
  10.  
  11.     private final String dnaString;
  12.  
  13.     public DNAMatcher(String base) {
  14.  
  15.         baseCombinations.put('A', 'T');
  16.         baseCombinations.put('T', 'A');
  17.         baseCombinations.put('C', 'G');
  18.         baseCombinations.put('G', 'C');
  19.  
  20.         if (base.equals(null) || !validateDNA(base)) {
  21.             throw new IllegalArgumentException("YOU HAVE TO ENTER A VALID DNA STRING");
  22.         }
  23.         this.dnaString = base.toUpperCase();
  24.     }
  25.  
  26.     /**
  27.      * Returns the index of the first position in the base DNA string where
  28.      * candidateDNA can bind, if any.
  29.      *
  30.      * @param candidateDNA the DNA string to try to bind to the base DNA.
  31.      * @return index of the first binding position or {@code -1} if the candidate
  32.      *         DNA string cannot bind to the base string.
  33.      * @throws IllegalArgumentException if {@code candidateDNA} is {@code null},
  34.      *                                  empty, or contains characters other than A,
  35.      *                                  C, G, and T.
  36.      */
  37.     public int findFirstBindingPosition(String candidateDNA) {
  38.  
  39.         if (candidateDNA.equals(null) || !validateDNA(candidateDNA)) {
  40.             throw new IllegalArgumentException("YOU HAVE TO ENTER A VALID DNA STRING");
  41.         }
  42.         candidateDNA = candidateDNA.toUpperCase();
  43.         for (int i = 0; i < dnaString.length(); i++) {
  44.             for (int j = 0; true; j++) {
  45.                 if (i + j >= dnaString.length()) {
  46.                     break;
  47.                 } else {
  48.                     if (baseCombinations.get(dnaString.charAt(i + j)) == candidateDNA.charAt(j)) {
  49.                         if (j == candidateDNA.length() - 1)
  50.                             return i;
  51.                     } else {
  52.                         break;
  53.                     }
  54.                 }
  55.  
  56.             }
  57.  
  58.         }
  59.         return -1;
  60.     }
  61.  
  62.     private boolean validateDNA(String dnaString) {
  63.         for (int i = 0; i < dnaString.length(); i++) {
  64.             if (Arrays.stream(bases).anyMatch(String.valueOf(dnaString.charAt(i))::equals)) {
  65.             } else {
  66.                 return false;
  67.             }
  68.         }
  69.         return true;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement