简体   繁体   English

我不知道为什么我的if语句有效

[英]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 所以如果序列1:CAG和序列2:AG,我应该得到响应

"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. 我相信我的问题在第二个if语句中,就像调试器一样。

when using the debugger it shows that the computer not going into the if statement. 使用调试器时,它表明计算机没有进入if语句。

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;
}

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. 因此它检查sequence1 ('C')的索引0处的核苷酸是否与sequence2 ('A')的索引0处的核苷酸相匹配,然后递增索引并检查sequence1 1('A')的索引1处的核苷酸是否与之匹配。在索引1处的核苷酸sequence2 (“G”),那么它停止而没有找到匹配。

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) 您可以从上面的示例中看到,第一个字符串中的字符绝不会与第二个字符串中的字符相同(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. 令我惊讶的是,也许您正在寻找与dnaSequence1中的内容对齐的最长的dnaSequence2片段,而不仅仅是从索引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. 此版本尝试在第一个序列中查找整个第二个序列,如果找不到,则从第二个序列的末尾修剪1个核苷酸,然后重试。 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) 一旦将第2个序列修剪为零,它将再次从整个第2个序列开始,并从头开始修剪1个核苷酸并重复该过程(如果找到匹配项则停止)

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚为什么我的 if 语句不起作用 - Can't figure why my if statement is not working 我不知道为什么我的Java继承无法正常工作 - I can't figure out why my java inheritance isn't working 您好,我似乎无法弄清楚为什么我的程序无法正常工作 - Hello I can't seem to figure out why my program isn't working 我不知道为什么此递归不起作用 - I can't figure out why this Recursion isn't working 无法弄清楚为什么我在 EditText 上收到此 NullPointerException - Can't figure out why I get this NullPointerException on my EditText 无法弄清楚为什么 java 在 if else 语句之后忽略了我的打印语句 - Can;t figure out why java is ignoring my print statement after the if else statement 我正在为我的独立班级研究二叉搜索树,但我不明白为什么这没有返回正确的高度 - I'm working on a binary search tree for my independent class and I can't figure out why this isn't returning the correct height 无法弄清楚为什么SQL语句不能正常运行 - Can't figure out why SQL statement isn't woking 无法弄清楚为什么我在我的数组print语句中获取空值 - Can't figure out why im getting null values in my array print statement 如果声明奇怪地不能正常工作......无法弄明白 - If statement oddly not working properly…can't figure it out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM