Changeset 1559 for branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java
- Timestamp:
- 06/04/14 16:42:02 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java
r1558 r1559 51 51 matrix = new MatrixEntry[length1+2][length2+1]; 52 52 53 for (int i = 0; i <= length1 ; i++) {53 for (int i = 0; i <= length1+1; i++) { 54 54 for(int j = 0; j< length2; j++) { 55 55 matrix[i][j] = new MatrixEntry(); … … 71 71 * @return Cost of substitution of the character in str1 by the one in str2 72 72 */ 73 private float similarity(int i, int j) { 74 if (i == 0 || j == 0) { 75 // it's a gap 76 return submat.getGapPenalty(); 77 } 78 // System.out.println("Diag letters: " + input1[i-1] + " " + 79 // input2[j-1]); 80 // return (input1[i - 1] == input2[j - 1]) ? MATCH_SCORE : 81 // MISMATCH_SCORE; 73 private float similarity(int i, int j) { 82 74 return submat.getDistance(input1[i - 1], input2[j - 1]); 83 75 } 84 76 85 77 /** 86 * Build the score matrix using dynamic programming. Note: The indel scores 87 * must be negative. Otherwise, the part handling the first row and column 88 * has to be modified. 78 * Build the score matrix using dynamic programming. 89 79 */ 90 80 private void buildMatrix() { … … 93 83 } 94 84 95 96 // base case 85 // it's a gap 97 86 matrix[0][0].setScore(0); 98 87 matrix[0][0].setPrevious(null); // starting point … … 107 96 108 97 109 for (int i = 1; i <= length1 ; i++) {98 for (int i = 1; i <= length1 + 1; i++) { 110 99 111 100 // Formula for first row: … … 113 102 114 103 float firstRowLeftScore = matrix[i-1][0].getScore(); 115 float tempMax = matrix[i-1][1].getScore(); 116 117 //position of the maximal score of the previous row 118 int maxRowIndex = 1; 119 120 for(int j = 2; j < length2;j++) { 121 if(matrix[i-1][j].getScore() > tempMax) { 122 tempMax = matrix[i-1][j].getScore(); 123 maxRowIndex = j; 124 } 125 } 126 104 //for sequences of length 1 105 float tempMax; 106 int maxRowIndex; 107 if(length2 == 1) { 108 tempMax = matrix[i-1][0].getScore(); 109 maxRowIndex = 0; 110 } else { 111 tempMax = matrix[i-1][1].getScore(); 112 maxRowIndex = 1; 113 //position of the maximal score of the previous row 114 115 for(int j = 2; j < length2;j++) { 116 if(matrix[i-1][j].getScore() > tempMax) { 117 tempMax = matrix[i-1][j].getScore(); 118 maxRowIndex = j; 119 } 120 } 121 122 } 123 124 127 125 tempMax -= scoreThreshold; 128 126 matrix[i][0].setScore(Math.max(firstRowLeftScore, tempMax)); 127 if(tempMax ==matrix[i][0].getScore()){ 128 matrix[i][0].setPrevious(matrix[i-1][maxRowIndex]); 129 } 130 131 if(firstRowLeftScore == matrix[i][0].getScore()) { 132 matrix[i][0].setPrevious(matrix[i-1][0]); 133 } 134 135 //The last additional score is not related to a character in the input sequence, it's the total score. Therefore we don't need to save something for it 136 if(i<length1+1) 137 { 138 matrix[i][0].setXvalue(input1[i-1]); 139 matrix[i][0].setYvalue(-2); 140 141 } 142 else { 143 //End after we calculated final score 144 return; 145 } 129 146 130 147 131 148 for (int j = 1; j < length2; j++) { 132 149 float diagScore = matrix[i - 1][j - 1].getScore() + similarity(i, j); 133 float upScore = matrix[i][j - 1].getScore() + s imilarity(0, j);134 float leftScore = matrix[i - 1][j].getScore() + s imilarity(i, 0);150 float upScore = matrix[i][j - 1].getScore() + submat.getGapPenalty(); 151 float leftScore = matrix[i - 1][j].getScore() + submat.getGapPenalty(); 135 152 136 153 matrix[i][j].setScore(Math.max(diagScore,Math.max(upScore, Math.max(leftScore,matrix[i][0].getScore())))); … … 138 155 // find the directions that give the maximum scores. 139 156 // Multiple directions are ignored TODO 157 //True if we had a match 140 158 if (diagScore == matrix[i][j].getScore()) { 141 159 matrix[i][j].setPrevious(matrix[i-1][j-1]); 142 } 160 matrix[i][j].setXvalue(input1[i-1]); 161 matrix[i][j].setYvalue(input2[j-1]); 162 } 163 //true if we took an event from sequence x and not from y 143 164 if (leftScore == matrix[i][j].getScore()) { 165 matrix[i][j].setXvalue(input1[i-1]); 166 matrix[i][j].setYvalue(-1); 144 167 matrix[i][j].setPrevious(matrix[i-1][j]); 145 168 } 169 //true if we took an event from sequence y and not from x 146 170 if (upScore == matrix[i][j].getScore()) { 171 matrix[i][j].setXvalue(-1); 172 matrix[i][j].setYvalue(input2[j-1]); 147 173 matrix[i][j].setPrevious(matrix[i][j-1]); 148 174 } 175 //true if we ended a matching region 149 176 if (matrix[i][0].getScore() == matrix[i][j].getScore()) { 150 matrix[i][j].setPrevious(matrix[i-1][maxRowIndex]); 151 } 152 } 177 matrix[i][j].setPrevious(matrix[i][0]); 178 matrix[i][j].setXvalue(input1[i-1]); 179 matrix[i][j].setYvalue(-2); 180 } 181 } 182 183 //Set the complete score cell 184 153 185 } 154 186 } … … 175 207 * Get the alignment score between the two input strings. 176 208 */ 177 public doublegetAlignmentScore() {178 return getMaxScore();209 public float getAlignmentScore() { 210 return matrix[length1+1][0].getScore(); 179 211 } 180 212 … … 188 220 */ 189 221 private int[] traceback(int i, int j) { 190 222 223 191 224 return null; 192 225 } 226 227 public void traceback() { 228 MatrixEntry tmp = matrix[length1+1][0]; 229 String aligned1 = ""; 230 String aligned2 = ""; 231 int count = 0; 232 do 233 { 234 String append1=""; 235 String append2=""; 236 237 if(tmp.getXvalue() == -1) { 238 append1 = " ___"; 239 } 240 else if(tmp.getXvalue() == -2) { 241 append1 = " ..."; 242 } 243 else { 244 append1 = String.format("%5d", tmp.getXvalue()); 245 } 246 247 if(tmp.getYvalue() == -1) { 248 append2 = " ___"; 249 } 250 else if(tmp.getYvalue() == -2) { 251 append2 = " ..."; 252 } 253 else { 254 append2 = String.format("%5d", tmp.getYvalue()); 255 } 256 if(count != 0) 257 { 258 aligned1 = append1 + aligned1; 259 aligned2 = append2 + aligned2; 260 } 261 262 tmp = tmp.getPrevious(); 263 count++; 264 265 } while(tmp != null); 266 System.out.println(aligned1); 267 System.out.println(aligned2); 268 } 269 193 270 194 271 … … 207 284 System.out.print(" "); 208 285 } 209 for (int i = 0; i <= length1; i++) { 210 System.out.format("%4.1f ",matrix[i][j].getScore()); 211 } 286 for (int i = 0; i <= length1 + 1; i++) { 287 if((i<length1+1) || (i==length1+1 && j==0)) { 288 System.out.format("%4.1f ",matrix[i][j].getScore()); 289 } 290 291 } 212 292 System.out.println(); 213 293 }
Note: See TracChangeset
for help on using the changeset viewer.