Hello there! I've got the following assignment:
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.
My Code is:

public class DNA extends ConsoleProgram{
    public void run(){
        println("This program receives 2 DNA chains(a combination of A, G, C, T) and evaluates whether they can actually match at a specific position(shows the position) or not(returns -1)"); 
        println("Make sure that the 2nd chain you insert is of the same or longer length than the first please! ");
        boolean flag1 = true;
        boolean flag2 = true;
        String shortDNA = readLine("-Insert the first DNA chain: ");
        String longDNA = readLine("-Insert the second DNA chain: ");    
        shortDNA= shortDNA.toUpperCase();
        longDNA= longDNA.toUpperCase();     

        while (flag2==true){
            for( int i=0; i<shortDNA.length(); i++){
                    char ch = shortDNA.charAt(i);
                    if (ch == 'A'|| ch == 'G' || ch == 'C' || ch == 'T'){
                        flag2 = false;
                    }else {
                        println("The chains should contain only a combination of the letters A, G, C, T !!! ");
                        shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
                        longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
                        shortDNA= shortDNA.toUpperCase();
                        longDNA= longDNA.toUpperCase();
                        break;
                    }   
            }
            for( int i=0; i<longDNA.length(); i++){
                char ch = longDNA.charAt(i);
                if (ch == 'A'|| ch == 'G' || ch == 'C' || ch == 'T'){
                    flag2 = false;
                }else {
                    println("The chains should contain only a combination of the letters A, G, C, T !!! ");
                    shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
                    longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
                    shortDNA= shortDNA.toUpperCase();
                    longDNA= longDNA.toUpperCase();
                    break;
                }   
            }
            if (shortDNA.length()>longDNA.length()) {
                println("Please insert the chain lengths correctly as requested above...(Second DNA chain should be longer than the first one.)");
                 shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
                 longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
                 shortDNA= shortDNA.toUpperCase();
                    longDNA= longDNA.toUpperCase();
                flag2=true;
            }else{
                flag2=false;
            }   
        }   
        println(findFirstMatchingPosition(shortDNA, longDNA));  
    }
    public static  int findFirstMatchingPosition(String shortDNA, String longDNA){
        char s;
        char l;
        boolean match = true;
        int count = 1;
        int counter = 0;
        boolean matchFound = false;
        for (int i = 0; i < shortDNA.length(); i++) {
            s = shortDNA.charAt(i);
            //Part 2: This part determines if the characters following the 1st matching character are also matches
            if (matchFound == true) {
                for (int j = count + counter; j < longDNA.length(); j++) {
                    l = longDNA.charAt(j);
                    if (s != l) {
                        match = false;
                    } else {
                        counter++;
                        matchFound = true;
                        break;
                    }
                }
            }   
            //Part1: This part determines whether the 1st character of shortDNA has a match in longDNA
            //If a match is found, Part 2 happens
            if (matchFound == false) {
                for (int k = 0; k < longDNA.length(); k++) {
                    l = longDNA.charAt(k);  
                    if (s != l) {
                        count ++; 
                        matchFound = false;
                    } else {
                        matchFound = true;
                        break;
                    }   
                    match = true;
                }   
            }   

        }
        if (match == true){
            return count;
        }else{
            return -1;
        }   


    }   

}

Sadly, my code is wrong since if the first chain is ATGC and the second is GGGTACG the result is -1. Can anyone please help me? I'd appreciate it a lot...

Recommended Answers

All 9 Replies

I find that code very hard to read, but looking at the matching around line 78 it looks like you are testing if the chars from the two DNA strings are the same, not T matches A etc

Well, yes true, I somehow erased the part I was "translating" the first chain. The missing code is here, but the program is still wrong:

for( int i=0; i<shortDNA.length(); i++){
                    char ch = shortDNA.charAt(i);
                    if (ch == 'A'){
                        transchain= transchain.concat("T");
                    }else if (ch =='T'){
                        transchain= transchain.concat("A");
                    }else if(ch =='G'){
                        transchain= transchain.concat("C");
                    }else{
                        transchain= transchain.concat("G");
                    }
}

I've already tried that. Sadly, it doesn't work, I only get -1 as a result. :(

It does work - there's another bug in your code somewhere.
Just to prove it here's an example that works (I'm not suggesting you copy this, your teacher will realise it's not your code, but proves the idea is OK)

    int firstDNAMatch(String shortDNA, String longDNA) {

        // some simple input validation...
        assert shortDNA.matches("[ATCG]*");
        assert longDNA.matches("[ATCG]*");

        // create the string we will be searching for by swapping T<>A, C<>G
        String matchingShortDNA = shortDNA.toLowerCase().
                replaceAll("t", "A").
                replaceAll("a", "T").
                replaceAll("c", "G").
                replaceAll("g", "C");

        // now it's a simple search...
        return longDNA.indexOf(matchingShortDNA);
    }

Well, when I translate the line(shortDNA) I put a println method to see if it translates correctly and it indeed translates correctly, so where could the bug be. If the indexOf worked, it'd show the right index, yet it never does.

I think Imight have fixed it!
The method looks like this now, and it works so far.

    public static  int findFirstMatchingPosition(String shortDNA, String longDNA){

        String transchain="";
        for( int i=0; i<shortDNA.length(); i++){
                    char ch = shortDNA.charAt(i);
                    if (ch == 'A'){
                        transchain= transchain.concat("T");
                    }else if (ch =='T'){
                        transchain= transchain.concat("A");
                    }else if(ch =='G'){
                        transchain= transchain.concat("C");
                    }else{
                        transchain= transchain.concat("G");
                    }
        }   

        int index = longDNA.indexOf(transchain);
        return index;


    }

OK! I never really doubted that Java's indexOf method works correctly, so it had to be something else. Your latest code looks good to me.
Don't forget to mark this "solved" when you have finished testing.

JC

Yes, the problem was that I had typed

String transchain=" ";

Instead of,

String transchain="";

Therefore, that messed the whole thing up! I had thought of the indeOf too, but since the code was wrong in the part I mentioned above, it didn't work. Thank you very much for your help! :3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.