JAL-2416 revert to '*' not '-' in score matrix, handle gaps explicitly
[jalview.git] / src / jalview / analysis / AlignSeq.java
index d04e897..6bf812c 100755 (executable)
@@ -112,16 +112,14 @@ public class AlignSeq
 
   int gapExtend = 20;
 
-  float[][] lookup;
-
-  int gapIndex = 23;
-
   StringBuffer output = new StringBuffer();
 
   String type; // AlignSeq.PEP or AlignSeq.DNA
 
   private ScoreMatrix scoreModel;
 
+  private static final int GAP_INDEX = -1;
+
   /**
    * Creates a new AlignSeq object.
    * 
@@ -322,10 +320,6 @@ public class AlignSeq
       return;
     }
 
-    seq1 = new int[s1str.length()];
-
-    seq2 = new int[s2str.length()];
-
     score = new float[s1str.length()][s2str.length()];
 
     E = new float[s1str.length()][s2str.length()];
@@ -351,8 +345,6 @@ public class AlignSeq
     type = moleculeType;
     scoreModel = ScoreModels.getInstance().getDefaultModel(
             PEP.equals(type));
-    lookup = scoreModel.getMatrix();
-    gapIndex = scoreModel.getGapIndex();
   }
 
   /**
@@ -417,13 +409,13 @@ public class AlignSeq
       else if (trace == 1)
       {
         j--;
-        aseq1[count] = gapIndex;
+        aseq1[count] = GAP_INDEX;
         sb1.replace(sb1.length() - 1, sb1.length(), "-");
       }
       else if (trace == -1)
       {
         i--;
-        aseq2[count] = gapIndex;
+        aseq2[count] = GAP_INDEX;
         sb2.replace(sb2.length() - 1, sb2.length(), "-");
       }
 
@@ -433,13 +425,13 @@ public class AlignSeq
     seq1start = i + 1;
     seq2start = j + 1;
 
-    if (aseq1[count] != gapIndex)
+    if (aseq1[count] != GAP_INDEX)
     {
       aseq1[count] = seq1[i];
       sb1.append(s1str.charAt(i));
     }
 
-    if (aseq2[count] != gapIndex)
+    if (aseq2[count] != GAP_INDEX)
     {
       aseq2[count] = seq2[j];
       sb2.append(s2str.charAt(j));
@@ -596,7 +588,10 @@ public class AlignSeq
   public int findTrace(int i, int j)
   {
     int t = 0;
-    float max = score[i - 1][j - 1] + (lookup[seq1[i]][seq2[j]] * 10);
+    // float pairwiseScore = lookup[seq1[i]][seq2[j]];
+    float pairwiseScore = scoreModel.getPairwiseScore(s1str.charAt(i),
+            s2str.charAt(j));
+    float max = score[i - 1][j - 1] + (pairwiseScore * 10);
 
     if (F[i][j] > max)
     {
@@ -640,7 +635,8 @@ public class AlignSeq
     int m = seq2.length;
 
     // top left hand element
-    score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
+    score[0][0] = scoreModel.getPairwiseScore(s1str.charAt(0),
+            s2str.charAt(0)) * 10;
     E[0][0] = -gapExtend;
     F[0][0] = 0;
 
@@ -651,7 +647,9 @@ public class AlignSeq
       E[0][j] = max(score[0][j - 1] - gapOpen, E[0][j - 1] - gapExtend);
       F[0][j] = -gapExtend;
 
-      score[0][j] = max(lookup[seq1[0]][seq2[j]] * 10, -gapOpen, -gapExtend);
+      float pairwiseScore = scoreModel.getPairwiseScore(s1str.charAt(0),
+              s2str.charAt(j));
+      score[0][j] = max(pairwiseScore * 10, -gapOpen, -gapExtend);
 
       traceback[0][j] = 1;
     }
@@ -662,7 +660,9 @@ public class AlignSeq
       E[i][0] = -gapOpen;
       F[i][0] = max(score[i - 1][0] - gapOpen, F[i - 1][0] - gapExtend);
 
-      score[i][0] = max(lookup[seq1[i]][seq2[0]] * 10, E[i][0], F[i][0]);
+      float pairwiseScore = scoreModel.getPairwiseScore(s1str.charAt(i),
+              s2str.charAt(0));
+      score[i][0] = max(pairwiseScore * 10, E[i][0], F[i][0]);
       traceback[i][0] = -1;
     }
 
@@ -674,8 +674,10 @@ public class AlignSeq
         E[i][j] = max(score[i][j - 1] - gapOpen, E[i][j - 1] - gapExtend);
         F[i][j] = max(score[i - 1][j] - gapOpen, F[i - 1][j] - gapExtend);
 
+        float pairwiseScore = scoreModel.getPairwiseScore(s1str.charAt(i),
+                s2str.charAt(j));
         score[i][j] = max(score[i - 1][j - 1]
-                + (lookup[seq1[i]][seq2[j]] * 10), E[i][j], F[i][j]);
+                + (pairwiseScore * 10), E[i][j], F[i][j]);
         traceback[i][j] = findTrace(i, j);
       }
     }
@@ -797,7 +799,7 @@ public class AlignSeq
   public static void displayMatrix(Graphics g, int[][] mat, int n, int m,
           int psize)
   {
-    // TODO method dosen't seem to be referenced anywhere delete??
+    // TODO method doesn't seem to be referenced anywhere delete??
     int max = -1000;
     int min = 1000;
 
@@ -971,8 +973,8 @@ public class AlignSeq
             bestm = msq;
           }
         }
-        System.out.println("Best Score for " + (matches.size() + 1) + " :"
-                + bestscore);
+        // System.out.println("Best Score for " + (matches.size() + 1) + " :"
+        // + bestscore);
         matches.add(bestm);
         aligns.add(bestaseq);
         al.deleteSequence(bestm);