Kowshal Choodi Kowshal Choodi - 7 months ago 13
Java Question

I can't figure out why my if statement is working

so if Sequence 1 :CAG and Sequence 2 :AG, i am supposed to get the response

"Best alignment score :2 CAG AG"


but instead i am getting

"Best alignment score :0 CAG AG"


I believe my issue is in the 2nd if statement, as that what it seems like with the debugger.

when using the debugger it shows that the computer not going into the if statement.

public static int allignment (String dnaSequence1 , String dnaSequence2 /*,int offset*/){
int newScore = 0;
int bestScore = 0;
int newOffset = 0;
int bestOffset = 0;

for(int offset =0; offset<=(dnaSequence1.length()-dnaSequence2.length());offset++){
//newOffset ++;
newScore = 0;
for(int place =0; place<dnaSequence2.length();place++ ){
if(dnaSequence1.charAt(place) == dnaSequence2.charAt(place/*+offset*/)){

newScore ++;
if(newScore>bestScore){
bestScore = newScore;
bestOffset = newOffset;

}
}else{ continue;}
}
newOffset ++;
}

String space = " ";
System.out.println("Best alignment score :"+bestScore);
System.out.println(dnaSequence1);
System.out.print( space(bestOffset) + dnaSequence2);

int alignmentScore = dnaSequence1.compareToIgnoreCase(dnaSequence2);

return alignmentScore;
}

public static String space (int bestOffset){
String space = " ";
String offsetScaces = "";

for(int i = 0; i<bestOffset; i++){
offsetScaces+=space;
return offsetScaces;
}
return offsetScaces;
}

Answer

Your version is using the same index for both strings. So it checks if the nucleotide at index 0 in sequence1 ('C') matches the nucleotide at index 0 in sequence2 ('A') then it increments the index and checks if the nucleotide at index 1 in sequence1 ('A') matches the nucleotide at index 1 in sequence2 ('G') then it stops without ever finding a match.

012
CAG
AG

You can see from the example above that at no time is the character in the first string the same as the second (0: C/A, 1: A/G, 2: G/null)

It strikes me that maybe you're looking for the longest piece of dnaSequence2 that aligns to something in dnaSequence1, not just the longest piece that starts at index 0.

This version tries to find the whole 2nd sequence inside the 1st sequence and if it can't, it trims 1 nucleotide from the end of the 2nd sequence and tries again. Once it's trimmed the 2nd sequence to nothing, it starts again with the whole 2nd sequence and trims 1 nucleotide from the start and repeats the process (stopping if it finds a match)

public static int allignment(String dnaSequence1, String dnaSequence2 /*,int offset*/) {
    int bestScore = -1;
    int bestOffset = 0;
    String bestSequence = null;

    for(String tempSequence = dnaSequence2; tempSequence.length() > 0; tempSequence = tempSequence.substring(1)) {
        for(String match = tempSequence; match.length() > 0; match = match.substring(0, match.length() - 1)) {
            int matchIndex;
            if (-1 != (matchIndex = dnaSequence1.indexOf(match))) {
                if (match.length() > bestScore) {
                    bestOffset = matchIndex;
                    bestScore = match.length() ;
                    bestSequence = match;
                    break;
                }
            }
        }
        if (null != bestSequence && bestScore > tempSequence.length()) {
            break; // don't bother checking any shorter sequences, we already have a better match
        }
    }

    if (null != bestSequence) {
        System.out.println("Best alignment score :" + bestScore);
        System.out.println(dnaSequence1);
        System.out.print(space(bestOffset) + bestSequence);
    } else {
        System.out.print(dnaSequence1+" and "+dnaSequence2+" cannot be aligned");
    }

    int alignmentScore = dnaSequence1.compareToIgnoreCase(dnaSequence2);

    return alignmentScore;
}

public static String space(int bestOffset) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < bestOffset; i++) {
        builder.append(" ");
    }
    return builder.toString();
}
Comments