source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java @ 1649

Last change on this file since 1649 was 1649, checked in by rkrimmel, 10 years ago

Adding tests to some classes.

File size: 10.0 KB
RevLine 
[1558]1package de.ugoe.cs.autoquest.tasktrees.alignment.algorithms;
2
3import java.util.ArrayList;
[1589]4import java.util.Iterator;
5import java.util.LinkedList;
[1558]6
[1619]7
[1572]8import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix;
[1578]9import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants;
[1558]10
[1619]11
[1586]12public class SmithWatermanRepeated implements AlignmentAlgorithm {
[1558]13
14        /**
15         * The first input
16         */
17        private int[] input1;
18
19        /**
20         * The second input String
21         */
22        private int[] input2;
23
24        /**
25         * The lengths of the input
26         */
27        private int length1, length2;
28
29        /**
30         * The score matrix. The true scores should be divided by the normalization
31         * factor.
32         */
33        private MatrixEntry[][] matrix;
34
[1572]35
[1585]36        private ArrayList<NumberSequence> alignment;
[1558]37       
38        private float scoreThreshold;
39
40        /**
41         * Substitution matrix to calculate scores
42         */
43        private SubstitutionMatrix submat;
44
[1612]45        public SmithWatermanRepeated() {
[1558]46       
47        }
48
49        /**
50         * Compute the similarity score of substitution The position of the first
51         * character is 1. A position of 0 represents a gap.
52         *
53         * @param i
54         *            Position of the character in str1
55         * @param j
56         *            Position of the character in str2
57         * @return Cost of substitution of the character in str1 by the one in str2
58         */
[1568]59        private double similarity(int i, int j) {
[1578]60                return submat.getScore(input1[i - 1], input2[j - 1]);
[1558]61        }
62
63        /**
[1559]64         * Build the score matrix using dynamic programming.
[1558]65         */
66        private void buildMatrix() {
67                if (submat.getGapPenalty() >= 0) {
68                        throw new Error("Indel score must be negative");
69                }
70               
[1559]71                // it's a gap
[1558]72                matrix[0][0].setScore(0);
73                matrix[0][0].setPrevious(null); // starting point
[1592]74                matrix[0][0].setXvalue(Constants.UNMATCHED_SYMBOL);
75                matrix[0][0].setYvalue(Constants.UNMATCHED_SYMBOL);
[1558]76
77                // the first column
78                for (int j = 1; j < length2; j++) {
79                        matrix[0][j].setScore(0);
[1575]80                        //We don't need to go back to [0][0] if we reached matrix[0][x], so just end here
81                        //matrix[0][j].setPrevious(matrix[0][j-1]);
82                        matrix[0][j].setPrevious(null);
[1558]83                }
84               
85               
86               
[1587]87                for (int i = 1; i < length1 + 2; i++) {
[1558]88               
89                        // Formula for first row:
90                        // F(i,0) = max { F(i-1,0), F(i-1,j)-T  j=1,...,m
91                       
[1568]92                        double firstRowLeftScore = matrix[i-1][0].getScore();
[1559]93                        //for sequences of length 1
[1568]94                        double tempMax;
[1559]95                        int maxRowIndex;
96                        if(length2 == 1) {
97                                tempMax = matrix[i-1][0].getScore();
98                                maxRowIndex = 0;
99                        } else {
100                                tempMax = matrix[i-1][1].getScore();
101                                maxRowIndex = 1;
102                                //position of the maximal score of the previous row
103                               
[1649]104                                for(int j = 2; j <= length2;j++) {
[1559]105                                        if(matrix[i-1][j].getScore() > tempMax) {
106                                                tempMax = matrix[i-1][j].getScore();
107                                                maxRowIndex = j;
108                                        }
[1558]109                                }
[1559]110
[1558]111                        }
[1559]112                                       
[1558]113                        tempMax -= scoreThreshold;
114                        matrix[i][0].setScore(Math.max(firstRowLeftScore, tempMax));
[1592]115                        if(tempMax == matrix[i][0].getScore()){
[1559]116                                matrix[i][0].setPrevious(matrix[i-1][maxRowIndex]);
117                        }
[1558]118                       
[1559]119                        if(firstRowLeftScore == matrix[i][0].getScore()) {
120                                matrix[i][0].setPrevious(matrix[i-1][0]);
121                        }
[1558]122                       
[1559]123                        //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
[1649]124                        //and can end here
125                        if(i<length1+1) {
[1559]126                                matrix[i][0].setXvalue(input1[i-1]);
[1578]127                                matrix[i][0].setYvalue(Constants.UNMATCHED_SYMBOL);
[1559]128                        }
[1649]129                        else {
[1559]130                                return;
131                        }
[1649]132                 
[1559]133                       
134                       
[1649]135                        for (int j = 1; j <= length2; j++) {
[1568]136                                double diagScore = matrix[i - 1][j - 1].getScore() + similarity(i, j);
137                                double upScore = matrix[i][j - 1].getScore() + submat.getGapPenalty();
138                                double leftScore = matrix[i - 1][j].getScore() + submat.getGapPenalty();
[1558]139
140                                matrix[i][j].setScore(Math.max(diagScore,Math.max(upScore, Math.max(leftScore,matrix[i][0].getScore()))));
141
142                                // find the directions that give the maximum scores.
[1578]143                                // TODO: Multiple directions are ignored, we choose the first maximum score
[1559]144                                //True if we had a match
[1558]145                                if (diagScore == matrix[i][j].getScore()) {
146                                        matrix[i][j].setPrevious(matrix[i-1][j-1]);
[1559]147                                        matrix[i][j].setXvalue(input1[i-1]);
148                                        matrix[i][j].setYvalue(input2[j-1]);
[1558]149                                }
[1559]150                                //true if we took an event from sequence x and not from y
[1558]151                                if (leftScore == matrix[i][j].getScore()) {
[1559]152                                        matrix[i][j].setXvalue(input1[i-1]);
[1578]153                                        matrix[i][j].setYvalue(Constants.GAP_SYMBOL);
[1558]154                                        matrix[i][j].setPrevious(matrix[i-1][j]);
155                                }
[1559]156                                //true if we took an event from sequence y and not from x
[1558]157                                if (upScore == matrix[i][j].getScore()) {
[1578]158                                        matrix[i][j].setXvalue(Constants.GAP_SYMBOL);
[1559]159                                        matrix[i][j].setYvalue(input2[j-1]);
[1558]160                                        matrix[i][j].setPrevious(matrix[i][j-1]);
161                                }
[1559]162                                //true if we ended a matching region
[1558]163                                if (matrix[i][0].getScore() == matrix[i][j].getScore()) {
[1559]164                                        matrix[i][j].setPrevious(matrix[i][0]);
165                                        matrix[i][j].setXvalue(input1[i-1]);
[1578]166                                        matrix[i][j].setYvalue(Constants.UNMATCHED_SYMBOL);
[1558]167                                }
168                        }
[1559]169                       
170                        //Set the complete score cell
171                       
[1558]172                }
173        }
174
175        /**
176         * Get the maximum value in the score matrix.
177         */
178        public double getMaxScore() {
179                double maxScore = 0;
180
181                // skip the first row and column
182                for (int i = 1; i <= length1; i++) {
[1649]183                        for (int j = 1; j <= length2; j++) {
[1558]184                                if (matrix[i][j].getScore() > maxScore) {
185                                        maxScore = matrix[i][j].getScore();
186                                }
187                        }
188                }
189
190                return maxScore;
191        }
192
[1586]193        /* (non-Javadoc)
194         * @see de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentAlgorithm#getAlignmentScore()
[1558]195         */
[1586]196        @Override
[1568]197        public double getAlignmentScore() {
[1559]198                return matrix[length1+1][0].getScore();
[1558]199        }
200
[1572]201        public void traceback() {
[1596]202                MatrixEntry tmp = matrix[length1+1][0].getPrevious();
[1589]203                LinkedList<Integer> aligned1 = new LinkedList<Integer>();
204                LinkedList<Integer> aligned2 = new LinkedList<Integer>();
[1592]205                while (tmp.getPrevious() != null) {
[1572]206                       
[1589]207                        aligned1.add(new Integer(tmp.getXvalue()));
208                        aligned2.add(new Integer(tmp.getYvalue()));
209
[1572]210                        tmp = tmp.getPrevious();
[1592]211                }
[1589]212               
213                // reverse order of the alignment
214                int reversed1[] = new int[aligned1.size()];
215                int reversed2[] = new int[aligned2.size()];
216
217                int count = 0;
[1596]218                for (Iterator<Integer> it = aligned1.iterator(); it.hasNext();) {
[1572]219                        count++;
[1589]220                        reversed1[reversed1.length - count] = it.next();
221                       
[1572]222                }
[1589]223                count = 0;
[1596]224                for (Iterator<Integer> it = aligned2.iterator(); it.hasNext();) {
[1589]225                        count++;
226                        reversed2[reversed2.length - count] = it.next();
227                }
228
[1572]229                NumberSequence ns1 = new NumberSequence(reversed1.length);
230                NumberSequence ns2 = new NumberSequence(reversed2.length);
231                ns1.setSequence(reversed1);
232                ns2.setSequence(reversed2);
[1620]233                ns1.setId(alignment.get(0).getId());
234                ns2.setId(alignment.get(1).getId());
235               
236                alignment.set(0, ns1);
237                alignment.set(1, ns2);
[1572]238        }
239       
[1589]240
241       
[1572]242        public void printAlignment() {
[1596]243                int[] tmp1 = alignment.get(0).getSequence();
244                int[] tmp2 = alignment.get(1).getSequence();
245                for (int i=0; i< tmp1.length;i++) {
246                        if(tmp1[i] == Constants.GAP_SYMBOL) {
247                                System.out.print("  ___");
[1559]248                        }
[1596]249                        else if(tmp1[i] == Constants.UNMATCHED_SYMBOL) {
250                                System.out.print("  ...");
[1559]251                        }
252                        else {
[1596]253                                System.out.format("%5d", tmp1[i]);
[1559]254                        }
[1596]255                       
256                }
257                System.out.println();
258                for (int i=0; i< tmp2.length;i++) {
259                        if(tmp2[i] == Constants.GAP_SYMBOL) {
260                                System.out.print("  ___");
[1559]261                        }
[1596]262                        else if(tmp2[i] == Constants.UNMATCHED_SYMBOL) {
263                                System.out.print("  ...");
[1559]264                        }
265                        else {
[1596]266                                System.out.format("%5d", tmp2[i]);
[1559]267                        }
268                       
[1596]269                }
270                System.out.println();
271               
272               
273       
[1559]274        }
[1558]275       
[1620]276        public ArrayList<Match> getMatches() {
277                ArrayList<Match> result = new ArrayList<Match>();
[1592]278               
279                //both alignment sequences should be equally long
280                int i = 0;
281                int[] seq1 = alignment.get(0).getSequence();
282                int[] seq2 = alignment.get(1).getSequence();
283                int start = 0;
284                while (i < seq1.length){
285                        if(seq2[i] != Constants.UNMATCHED_SYMBOL) {
286                                start = i;
287                                int count = 0;
288                                while(i < seq2.length && seq2[i] != Constants.UNMATCHED_SYMBOL) {
289                                        i++;
290                                        count++;
291                                }
[1620]292                                //I am really missing memcpy here? How does one do this better in java?
[1592]293                                int[] tmp1 = new int[count];
294                                int[] tmp2 = new int[count];
295                                for (int j = 0; j<count;j++) {
296                                        tmp1[j] = seq1[start+j];
297                                        tmp2[j] = seq2[start+j];
298                                }
299                                NumberSequence tmpns1 = new NumberSequence(count);
300                                NumberSequence tmpns2 = new NumberSequence(count);
301                                tmpns1.setSequence(tmp1);
302                                tmpns2.setSequence(tmp2);
[1620]303                                Match tmpal = new Match();
304                                tmpal.setFirstSequence(tmpns1);
305                                tmpal.setSecondSequence(tmpns2);
306                                tmpal.addOccurence(new MatchOccurence(start,alignment.get(0).getId()));
307                                tmpal.addOccurence(new MatchOccurence(start,alignment.get(1).getId()));
[1592]308                                result.add(tmpal);
309                        }
310                        i++;
311                }
312                return result;
313        }
[1559]314       
[1558]315        /**
316         * print the dynmaic programming matrix
317         */
318        public void printDPMatrix() {
319                System.out.print("          ");
320                for (int i = 1; i <= length1; i++)
321                        System.out.format("%5d", input1[i - 1]);
322                System.out.println();
[1587]323                for (int j = 0; j <= length2; j++) {
[1558]324                        if (j > 0)
325                                System.out.format("%5d ",input2[j - 1]);
326                        else{
327                                System.out.print("      ");
328                        }
[1559]329                        for (int i = 0; i <= length1 + 1; i++) {
330                                if((i<length1+1) || (i==length1+1 && j==0))     {
331                                        System.out.format("%4.1f ",matrix[i][j].getScore());
332                                }
333                               
334                        }                       
[1558]335                        System.out.println();
336                }
337        }
338
339
[1586]340        /* (non-Javadoc)
341         * @see de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentAlgorithm#getAlignment()
342         */
343        @Override
[1585]344        public ArrayList<NumberSequence> getAlignment() {
[1572]345                return alignment;
346        }
347
[1585]348        public void setAlignment(ArrayList<NumberSequence> alignment) {
[1572]349                this.alignment = alignment;
350        }
351
[1612]352        @Override
[1620]353        public void align(NumberSequence input1, NumberSequence input2, SubstitutionMatrix submat,
[1612]354                        float threshold) {
[1620]355               
356                alignment = new ArrayList<NumberSequence>();
357                alignment.add(input1);
358                alignment.add(input2);
359               
360                this.input1=input1.getSequence();
361                this.input2=input2.getSequence();
362               
363                length1 = input1.size();
364                length2 = input2.size();
[1612]365                this.submat = submat;
366
367                //System.out.println("Starting SmithWaterman algorithm with a "
368                //              + submat.getClass() + " Substitution Matrix: " + submat.getClass().getCanonicalName());
369                this.scoreThreshold = threshold;
370               
371                matrix = new MatrixEntry[length1+2][length2+1];
372               
[1620]373               
[1612]374                for (int i = 0; i <= length1+1; i++) {
[1649]375                        for(int j = 0; j<= length2; j++) {
[1612]376                                matrix[i][j] = new MatrixEntry();
377                        }
378                }
379       
[1618]380                //Console.traceln(Level.INFO,"Generating DP Matrix");
[1612]381                buildMatrix();
[1618]382                //Console.traceln(Level.INFO,"Doing traceback");
[1612]383                traceback();
384        }
385
[1558]386}
Note: See TracBrowser for help on using the repository browser.