Ignore:
Timestamp:
07/08/14 21:38:11 (10 years ago)
Author:
rkrimmel
Message:

Fixed Needleman Wunsch Algorithm

File:
1 edited

Legend:

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

    r1587 r1588  
    22 
    33import java.util.ArrayList; 
    4 import java.util.logging.Level; 
     4import java.util.Iterator; 
     5import java.util.LinkedList; 
    56 
    67import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix; 
    78import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants; 
    8 import de.ugoe.cs.util.console.Console; 
     9 
    910 
    1011public class NeedlemanWunsch implements AlignmentAlgorithm { 
     
    116117 
    117118                                matrix[i][j].setScore(Math.max(diagScore, 
    118                                                 Math.max(upScore, Math.max(leftScore, 0)))); 
     119                                                Math.max(upScore, leftScore))); 
    119120 
    120121                                // find the directions that give the maximum scores. 
     
    175176        public void traceback() { 
    176177                MatrixEntry tmp = matrix[length1][length2]; 
    177                 int aligned1[] = new int[length1 + length2 + 2]; 
    178                 int aligned2[] = new int[length1 + length2 + 2]; 
     178                LinkedList<Integer> aligned1 = new LinkedList<Integer>(); 
     179                LinkedList<Integer> aligned2 = new LinkedList<Integer>(); 
     180                do { 
     181                         
     182                        aligned1.add(new Integer(tmp.getXvalue())); 
     183                        aligned2.add(new Integer(tmp.getYvalue())); 
     184 
     185                        tmp = tmp.getPrevious(); 
     186 
     187                } while (tmp != null); 
     188                 
     189                // reverse order of the alignment 
     190                int reversed1[] = new int[aligned1.size()]; 
     191                int reversed2[] = new int[aligned2.size()]; 
     192 
    179193                int count = 0; 
    180                 do { 
    181                         if (length1 + length2 + 2 == count) { 
    182                                 Console.traceln(Level.WARNING, 
    183                                                 "Traceback longer than both sequences summed up!"); 
    184                                 break; 
    185                         } 
    186                         aligned1[count] = tmp.getXvalue(); 
    187                         aligned2[count] = tmp.getYvalue(); 
    188  
    189                         tmp = tmp.getPrevious(); 
     194                for (Iterator<Integer> it = aligned1.descendingIterator(); it.hasNext();) { 
    190195                        count++; 
    191  
    192                 } while (tmp != null); 
    193                 count--; 
    194                 // reverse order of the alignment 
    195                 int reversed1[] = new int[count]; 
    196                 int reversed2[] = new int[count]; 
    197  
    198                 for (int i = count; i > 0; i--) { 
    199                         reversed1[reversed1.length - i] = aligned1[i]; 
    200                         reversed2[reversed2.length - i] = aligned2[i]; 
     196                        reversed1[reversed1.length - count] = it.next(); 
     197                         
     198                } 
     199                count = 0; 
     200                for (Iterator<Integer> it = aligned2.descendingIterator(); it.hasNext();) { 
     201                        count++; 
     202                        reversed2[reversed2.length - count] = it.next(); 
    201203                } 
    202204 
     
    216218                int count = 0; 
    217219                do { 
     220                        System.out.println(tmp); 
    218221                        String append1 = ""; 
    219222                        String append2 = ""; 
Note: See TracChangeset for help on using the changeset viewer.