Write a method in Java called FirstDNAMatch(shortDNA, longDNA) that receives 2 strings with the letters that have to do with the four main nucleobases found in the nucleic acids DNA (A for Adenosine, G for Guanine, C for Cytosine and T for Thymine). The method must return the index of the first spot where the first DNA chain can connect to the second or -1 if there is no spot for the 2 chains to match. A matches with T (and other way round) and C matches with G (and other way round). For instance, Chain1: T A A C G G T A C G and Chain2: G C C A match on the index 3.
1
Expert's answer
2016-01-25T07:04:22-0500
import java.util.Scanner;
public class Main { public static int FirstDNAMatch(String shortDNA, String longDNA) { shortDNA = shortDNA.replaceAll("\\s", "").replaceAll("T", "A").replaceAll("G", "C"); //removing whitespaces, replacing "T" with "A" and "G" with "C" longDNA = longDNA.replaceAll("\\s", "").replaceAll("T", "A").replaceAll("G", "C"); //removing whitespaces, replacing "T" with "A" and "G" with "C" return longDNA.indexOf(shortDNA); //searching for the match }
public static void main(String[] args) { //manual testing Scanner sc = new Scanner(System.in); System.out.println("Please enter longDNA: "); String longDNA = sc.nextLine(); System.out.println("Please enter shortDNA: "); String shortDNA = sc.nextLine(); System.out.println("Match: " + FirstDNAMatch(shortDNA, longDNA)); //predefined test longDNA = "T A A C G G T A C G"; shortDNA = "G C C A "; System.out.println("Match for predefined strings: " + FirstDNAMatch(shortDNA, longDNA));
Comments
Leave a comment