I need to align two DNA sequences (they are strings). The matching characters have to be aligned with a vertical line |
and the mismatches with a space. Here is my code, the whole thing works well I just can't seem to find a way to align the strings.
//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!");
Lets say we have string 1 = ABBABB and string 2 = ABAAAB, I need the whole output to look like this:
ABBABB
|| | |
ABAAAB
The total score is: 6
I just don't know how to get the vertical lines to get into the right place.
Update:
I looks like you just need to print the aligner between your other two print statments for s_one and s_two.
System.out.println(s_one);
System.out.println(aligner);
System.out.println(s_two);
It looks like there is debugging output in you alignment loop. You need to remove those println statements to avoid a bunch a "garbage" at the top your output. Also make sure you append to both aligners...
if(char_one == char_two){
total_pos += 2;
aligner += "|";
}else if(char_one != char_two){
total_neg -= 1;
aligner += " ";
}
}
To get your current code to work.
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);
Possibly a better answer.
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(" ");
}
}
//at the end
System.out.println(s_one);
System.out.println(alignment.toString());
System.out.println(s_two);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.