簡體   English   中英

我不知道為什么我的if語句有效

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

所以如果序列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;
}

您的版本對兩個字符串使用相同的索引。 因此它檢查sequence1 ('C')的索引0處的核苷酸是否與sequence2 ('A')的索引0處的核苷酸相匹配,然后遞增索引並檢查sequence1 1('A')的索引1處的核苷酸是否與之匹配。在索引1處的核苷酸sequence2 (“G”),那么它停止而沒有找到匹配。

012
CAG
AG

您可以從上面的示例中看到,第一個字符串中的字符絕不會與第二個字符串中的字符相同(0:C / A,1:A / G,2:G / null)

令我驚訝的是,也許您正在尋找與dnaSequence1中的內容對齊的最長的dnaSequence2片段,而不僅僅是從索引0開始的最長的片段。

此版本嘗試在第一個序列中查找整個第二個序列,如果找不到,則從第二個序列的末尾修剪1個核苷酸,然后重試。 一旦將第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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM