【问题标题】:I can't figure out why my if statement is working我不知道为什么我的 if 语句有效
【发布时间】:2016-04-30 03:12:15
【问题描述】:

所以如果序列 1:CAG 和序列 2:AG,我应该得到响应

"Best alignment score :2 CAG AG"

但是我得到了

"Best alignment score :0 CAG AG"

我相信我的问题出在第二个 if 语句中,就像调试器看起来一样。

当使用调试器时,它显示计算机没有进入 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;
}

【问题讨论】:

    标签: java string if-statement for-loop


    【解决方案1】:

    您的版本对两个字符串使用相同的索引。因此,它检查sequence1 ('C') 中索引 0 处的核苷酸是否与sequence2 ('A') 中索引 0 处的核苷酸匹配,然后它增加索引并检查 sequence1 中索引 1 处的核苷酸是否('A') 匹配sequence2 ('G') 中索引 1 处的核苷酸,然后它停止而找不到匹配项。

    012
    CAG
    AG
    

    从上面的例子可以看出,第一个字符串中的字符绝不会与第二个字符串相同(0:C/A,1:A/G,2:G/null)

    令我震惊的是,也许您正在寻找与 dnaSequence1 中的某些内容对齐的最长的 dnaSequence2 片段,而不仅仅是从索引 0 开始的最长片段。

    此版本尝试在第一个序列中找到整个第二个序列,如果找不到,它会从第二个序列的末端修剪 1 个核苷酸并再次尝试。一旦将第二个序列修剪为空,它会再次从整个第二个序列开始,并从头开始修剪 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();
    }
    

    【讨论】:

    • 效果很好(第二个较长的响应)。但我不明白你做了什么。你能解释一下你做了什么不同的事情吗?谢谢
    • 您的版本对两个字符串使用相同的索引。因此,它会检查sequence1 (C) 中索引 0 处的字符是否与 sequence2 (A) 中索引 0 处的字符匹配,然后它会增加索引并检查 sequence1 (A) 中索引 1 处的字符是否匹配sequence2 (G) 中索引 1 处的字符,然后它停止而没有找到匹配项
    • @Kowshal Choodi 我的版本尝试在第一个序列中找到整个第二个序列,如果找不到,它会从第二个序列的末端修剪 1 个核苷酸并重试。一旦将第二个序列修剪为空,它就会从整个第二个序列重新开始,并从头开始修剪 1 个核苷酸并重复该过程(如果找到匹配则停止)
    • 您能在答案中添加您的解释吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多