Ignore:
Timestamp:
06/04/14 16:42:02 (10 years ago)
Author:
rkrimmel
Message:

Further adjustments of the smithWatermanRepeated algorithm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java

    r1558 r1559  
    5151                matrix = new MatrixEntry[length1+2][length2+1]; 
    5252                 
    53                 for (int i = 0; i <= length1; i++) { 
     53                for (int i = 0; i <= length1+1; i++) { 
    5454                        for(int j = 0; j< length2; j++) { 
    5555                                matrix[i][j] = new MatrixEntry(); 
     
    7171         * @return Cost of substitution of the character in str1 by the one in str2 
    7272         */ 
    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) {  
    8274                return submat.getDistance(input1[i - 1], input2[j - 1]); 
    8375        } 
    8476 
    8577        /** 
    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. 
    8979         */ 
    9080        private void buildMatrix() { 
     
    9383                } 
    9484                 
    95  
    96                 // base case 
     85                // it's a gap 
    9786                matrix[0][0].setScore(0); 
    9887                matrix[0][0].setPrevious(null); // starting point 
     
    10796                 
    10897                 
    109                 for (int i = 1; i <= length1; i++) { 
     98                for (int i = 1; i <= length1 + 1; i++) { 
    11099                 
    111100                        // Formula for first row: 
     
    113102                         
    114103                        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                                         
    127125                        tempMax -= scoreThreshold; 
    128126                        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                        } 
    129146                         
    130147                         
    131148                        for (int j = 1; j < length2; j++) { 
    132149                                float diagScore = matrix[i - 1][j - 1].getScore() + similarity(i, j); 
    133                                 float upScore = matrix[i][j - 1].getScore() + similarity(0, j); 
    134                                 float leftScore = matrix[i - 1][j].getScore() + similarity(i, 0); 
     150                                float upScore = matrix[i][j - 1].getScore() + submat.getGapPenalty(); 
     151                                float leftScore = matrix[i - 1][j].getScore() + submat.getGapPenalty(); 
    135152 
    136153                                matrix[i][j].setScore(Math.max(diagScore,Math.max(upScore, Math.max(leftScore,matrix[i][0].getScore())))); 
     
    138155                                // find the directions that give the maximum scores. 
    139156                                // Multiple directions are ignored TODO 
     157                                //True if we had a match 
    140158                                if (diagScore == matrix[i][j].getScore()) { 
    141159                                        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 
    143164                                if (leftScore == matrix[i][j].getScore()) { 
     165                                        matrix[i][j].setXvalue(input1[i-1]); 
     166                                        matrix[i][j].setYvalue(-1); 
    144167                                        matrix[i][j].setPrevious(matrix[i-1][j]); 
    145168                                } 
     169                                //true if we took an event from sequence y and not from x 
    146170                                if (upScore == matrix[i][j].getScore()) { 
     171                                        matrix[i][j].setXvalue(-1); 
     172                                        matrix[i][j].setYvalue(input2[j-1]); 
    147173                                        matrix[i][j].setPrevious(matrix[i][j-1]); 
    148174                                } 
     175                                //true if we ended a matching region  
    149176                                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                         
    153185                } 
    154186        } 
     
    175207         * Get the alignment score between the two input strings. 
    176208         */ 
    177         public double getAlignmentScore() { 
    178                 return getMaxScore(); 
     209        public float getAlignmentScore() { 
     210                return matrix[length1+1][0].getScore(); 
    179211        } 
    180212 
     
    188220         */ 
    189221        private int[] traceback(int i, int j) { 
    190  
     222                 
     223                         
    191224                return null; 
    192225        } 
     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         
    193270 
    194271         
     
    207284                                System.out.print("      "); 
    208285                        } 
    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                        }                        
    212292                        System.out.println(); 
    213293                } 
Note: See TracChangeset for help on using the changeset viewer.