public class Q1 { public static void main(String[] args) { // Input Protein Sequences String seq1 = "HCEA"; String seq2 = "HWEA"; // Array to store the score int[][] T = new int[seq1.length() + 1][seq2.length() + 1]; // initialize seq1 for (int i = 0; i <= seq1.length(); i++) { T[i][0] = i; } // Initialize seq2 for (int i = 0; i <= seq2.length(); i++) { T[0][i] = i; } // Compute the matrix score for (int i = 1; i <= seq1.length(); i++) { for (int j = 1; j <= seq2.length(); j++) { if ((seq1.charAt(i - 1) == seq2.charAt(j - 1)) || (seq1.charAt(i - 1) == 'C') && (seq2.charAt(j - 1) == 'W')) { T[i][j] = T[i - 1][j - 1]; } else { T[i][j] = Math.min(T[i - 1][j], T[i][j - 1]) + 1; } } } // Strings to store the aligned sequences StringBuilder alignedSeq1 = new StringBuilder(); StringBuilder alignedSeq2 = new StringBuilder(); // Build for sequences 1 & 2 from the matrix score for (int i = seq1.length(), j = seq2.length(); i > 0 || j > 0;) { if (i > 0 && T[i][j] == T[i - 1][j] + 1) { alignedSeq1.append(seq1.charAt(--i)); alignedSeq2.append("-"); } else if (j > 0 && T[i][j] == T[i][j - 1] + 1) { alignedSeq2.append(seq2.charAt(--j)); alignedSeq1.append("-"); } else if (i > 0 && j > 0 && T[i][j] == T[i - 1][j - 1]) { alignedSeq1.append(seq1.charAt(--i)); alignedSeq2.append(seq2.charAt(--j)); } } // Display the aligned sequence System.out.println(alignedSeq1.reverse().toString()); System.out.println(alignedSeq2.reverse().toString()); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)