全局序列比对

时间:2014-02-27 04:23:36

标签: java

我需要对齐两个DNA序列(它们是字符串)。匹配的字符必须与垂直线|对齐,并与空格不匹配。这是我的代码,整个事情很好,我似乎无法找到一种方法来对齐字符串。

//Variable List
Scanner keyboard = new Scanner(System.in);
String s_one, s_two, aligner = " ";
int len_one = 0, len_two = 0, a = 0, total_pos = 0, total_neg = 0, total_score = 0;
char char_one = ' ', char_two = ' ', compare = ' ';
//End Variable List

System.out.println("Enter the first DNA sequence: ");
s_one = keyboard.next().toUpperCase();
System.out.println("Enter the second DNA sequence: ");
s_two = keyboard.next().toUpperCase();

len_one = s_one.length();
len_two = s_two.length();

while(len_one > a && len_two > a){
    char_one = s_one.charAt(a);
    char_two = s_two.charAt(a);
    a++;
    if(char_one == char_two){
        total_pos += 2;
        aligner = "|";
        System.out.println(aligner);
    }else if(char_one != char_two){
        total_neg -= 1;
        aligner = " ";
        System.out.println(aligner);
    }
}

//Output
total_score = total_pos + total_neg;
System.out.println(s_one);
System.out.println(s_two);
System.out.println("The total score is: " + total_score);

do{//Ask the user if they want to re-enter another DNA sequence
    System.out.println("Would you like to re-enter another DNA sequence? Y/N");
    compare = keyboard.next().charAt(0);
    if(compare == 'y' || compare == 'Y'){

        System.out.println("Enter the first DNA sequence: ");
        s_one = keyboard.next().toUpperCase();
        System.out.println("Enter the second DNA sequence: ");
        s_two = keyboard.next().toUpperCase();

        len_one = s_one.length();
        len_two = s_two.length();

        while(len_one > a && len_two > a){
            char_one = s_one.charAt(a);
            char_two = s_two.charAt(a);
            a++;
            if(char_one == char_two){
                total_pos += 2;
            }else if(char_one != char_two){
                total_neg -= 1;
            }
        }

        //Output
        total_score = total_pos + total_neg;
        System.out.println(s_one);
        System.out.println();
        System.out.println(s_two);
        System.out.println("The total score is: " + total_score);
    }
}while(compare == 'y' || compare == 'Y');
System.out.println("Thank You!");

让我们说我们有字符串1 = ABBABB和字符串2 = ABAAAB,我需要整个输出看起来像这样:

ABBABB
|| | |
ABAAAB
The total score is: 6

我只是不知道如何让垂直线进入正确的位置。

2 个答案:

答案 0 :(得分:0)

更新

我看起来你只需要在s_one和s_two的其他两个打印状态之间打印对齐器。

System.out.println(s_one);
System.out.println(aligner);
System.out.println(s_two);

看起来你的对齐循环中有调试输出。你需要删除那些println语句,以避免在输出的顶部出现“垃圾”。还要确保你附加到两个对齐器......

  if(char_one == char_two){
    total_pos += 2;
    aligner += "|";
  }else if(char_one != char_two){
    total_neg -= 1;
    aligner += " ";
  }
}

答案 1 :(得分:0)

让您的当前代码生效。

   System.out.println(s_one);
    while(len_one > a && len_two > a){
        char_one = s_one.charAt(a);
        char_two = s_two.charAt(a);
        a++;
        if(char_one == char_two){
            total_pos += 2;
            aligner = "|";
            System.out.print(aligner);
        }else if(char_one != char_two){
            total_neg -= 1;
            aligner = " ";
            System.out.print(aligner);
        }
    }
    System.out.println();
    System.out.println(s_two);

可能是一个更好的答案。

StringBuilder alignment = new StringBuilder();

for (int i = 0; i < s_one.length(); i++) {
   if (s_one.chatAt(i) == s_two.charAt(i)) {
      alignment.append("|");
   } else {
      alignment.append(" ");
   }
}

//在最后

System.out.println(s_one);
System.out.println(alignment.toString());
System.out.println(s_two);