Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package programming.set9.dna;
- import java.util.Arrays;
- import java.util.HashMap;
- public class DNAMatcher {
- private static final String[] bases = { "A", "C", "G", "T" };
- private static final HashMap<Character, Character> baseCombinations = new HashMap<Character, Character>();
- private final String dnaString;
- public DNAMatcher(String base) {
- baseCombinations.put('A', 'T');
- baseCombinations.put('T', 'A');
- baseCombinations.put('C', 'G');
- baseCombinations.put('G', 'C');
- if (base.equals(null) || !validateDNA(base)) {
- throw new IllegalArgumentException("YOU HAVE TO ENTER A VALID DNA STRING");
- }
- this.dnaString = base.toUpperCase();
- }
- /**
- * Returns the index of the first position in the base DNA string where
- * candidateDNA can bind, if any.
- *
- * @param candidateDNA the DNA string to try to bind to the base DNA.
- * @return index of the first binding position or {@code -1} if the candidate
- * DNA string cannot bind to the base string.
- * @throws IllegalArgumentException if {@code candidateDNA} is {@code null},
- * empty, or contains characters other than A,
- * C, G, and T.
- */
- public int findFirstBindingPosition(String candidateDNA) {
- if (candidateDNA.equals(null) || !validateDNA(candidateDNA)) {
- throw new IllegalArgumentException("YOU HAVE TO ENTER A VALID DNA STRING");
- }
- candidateDNA = candidateDNA.toUpperCase();
- for (int i = 0; i < dnaString.length(); i++) {
- for (int j = 0; true; j++) {
- if (i + j >= dnaString.length()) {
- break;
- } else {
- if (baseCombinations.get(dnaString.charAt(i + j)) == candidateDNA.charAt(j)) {
- if (j == candidateDNA.length() - 1)
- return i;
- } else {
- break;
- }
- }
- }
- }
- return -1;
- }
- private boolean validateDNA(String dnaString) {
- for (int i = 0; i < dnaString.length(); i++) {
- if (Arrays.stream(bases).anyMatch(String.valueOf(dnaString.charAt(i))::equals)) {
- } else {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement