2014-02-27 27 views
0

我需要對齊兩個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 

我只是不知道如何讓垂直線去正確的地方。

+0

這將是你把你的電流輸出是什麼有用的,所以我/我們沒有運行它。 –

回答

0

更新:

我看起來像你只需要打印的s_one和s_two你的其他兩個打印statments之間的對準。

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 += " "; 
    } 
} 
+0

看起來他每次找到對齊符時都會打印一條新線。我相信你以前的解決方案是對的。 –

+0

我認爲這只是調試輸出。我應該加上這個答案。 –

+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);