Properly integrated endGaps option for AlignSeq.traceAlignment()
[jalview.git] / src / jalview / analysis / AlignSeq.java
index 3448e79..4ec0457 100755 (executable)
@@ -381,7 +381,6 @@ public class AlignSeq
   /**
    * DOCUMENT ME!
    */
-  //&! not / 10
   public void traceAlignment()
   {
     // Find the maximum score along the rhs or bottom row
@@ -411,7 +410,103 @@ public class AlignSeq
     int j = maxj;
     int trace;
     maxscore = score[i][j] / 10f;
-    //maxscore = score[i][j];
+
+
+    aseq1 = new int[seq1.length + seq2.length];
+    aseq2 = new int[seq1.length + seq2.length];
+
+    StringBuilder sb1 = new StringBuilder(aseq1.length);
+    StringBuilder sb2 = new StringBuilder(aseq2.length);
+
+    count = (seq1.length + seq2.length) - 1;
+
+
+    while (i > 0 && j > 0)
+    {
+      aseq1[count] = seq1[i];
+      sb1.append(s1str.charAt(i));
+      aseq2[count] = seq2[j];
+      sb2.append(s2str.charAt(j));
+
+      trace = findTrace(i, j);
+
+      if (trace == 0)
+      {
+        i--;
+        j--;
+      }
+      else if (trace == 1)
+      {
+        j--;
+        aseq1[count] = GAP_INDEX;
+        sb1.replace(sb1.length() - 1, sb1.length(), "-");
+      }
+      else if (trace == -1)
+      {
+        i--;
+        aseq2[count] = GAP_INDEX;
+        sb2.replace(sb2.length() - 1, sb2.length(), "-");
+      }
+
+      count--;
+    }
+
+    seq1start = i + 1;
+    seq2start = j + 1;
+
+    if (aseq1[count] != GAP_INDEX)
+    {
+      aseq1[count] = seq1[i];
+      sb1.append(s1str.charAt(i));
+    }
+
+    if (aseq2[count] != GAP_INDEX)
+    {
+      aseq2[count] = seq2[j];
+      sb2.append(s2str.charAt(j));
+    }
+
+
+    /*
+     * we built the character strings backwards, so now
+     * reverse them to convert to sequence strings
+     */
+    astr1 = sb1.reverse().toString();
+    astr2 = sb2.reverse().toString();
+  }
+
+  /**
+   * DOCUMENT ME!
+   */
+  public void traceAlignmentWithEndGaps()
+  {
+    // Find the maximum score along the rhs or bottom row
+    float max = -Float.MAX_VALUE;
+
+    for (int i = 0; i < seq1.length; i++)
+    {
+      if (score[i][seq2.length - 1] > max)
+      {
+        max = score[i][seq2.length - 1];
+        maxi = i;
+        maxj = seq2.length - 1;
+      }
+    }
+
+    for (int j = 0; j < seq2.length; j++)
+    {
+      if (score[seq1.length - 1][j] > max)
+      {
+        max = score[seq1.length - 1][j];
+        maxi = seq1.length - 1;
+        maxj = j;
+      }
+    }
+
+    int i = maxi;
+    int j = maxj;
+    int trace;
+    maxscore = score[i][j] / 10f;
 
     //&! get trailing gaps
     while ((i < seq1.length - 1) || (j < seq2.length - 1))
@@ -658,14 +753,12 @@ public class AlignSeq
    * 
    * @return DOCUMENT ME!
    */
-  //&! not * 10
   public int findTrace(int i, int j)
   {
     int t = 0;
     float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
             s2str.charAt(j));
     float max = score[i - 1][j - 1] + (pairwiseScore * 10);
-    //float max = score[i - 1][j - 1] + (pairwiseScore);
 
     if (F[i][j] > max)
     {
@@ -703,7 +796,6 @@ public class AlignSeq
   /**
    * DOCUMENT ME!
    */
-  //&! not * 10
   public void calcScoreMatrix()
   {
     int n = seq1.length;
@@ -711,7 +803,6 @@ public class AlignSeq
 
     // top left hand element
     score[0][0] = scoreMatrix.getPairwiseScore(s1str.charAt(0),
-            //s2str.charAt(0));
             s2str.charAt(0)) * 10;
     E[0][0] = -GAP_EXTEND_COST;
     F[0][0] = 0;
@@ -727,7 +818,6 @@ public class AlignSeq
       float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(0),
               s2str.charAt(j));
       score[0][j] = max(pairwiseScore * 10, -GAP_OPEN_COST,
-      //score[0][j] = max(pairwiseScore, -GAP_OPEN_COST,
               -GAP_EXTEND_COST);
 
       traceback[0][j] = 1;
@@ -743,7 +833,6 @@ public class AlignSeq
       float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
               s2str.charAt(0));
       score[i][0] = max(pairwiseScore * 10, E[i][0], F[i][0]);
-      //score[i][0] = max(pairwiseScore, E[i][0], F[i][0]);
       traceback[i][0] = -1;
     }
 
@@ -760,7 +849,6 @@ public class AlignSeq
         float pairwiseScore = scoreMatrix.getPairwiseScore(s1str.charAt(i),
                 s2str.charAt(j));
         score[i][j] = max(score[i - 1][j - 1] + (pairwiseScore * 10),
-        //score[i][j] = max(score[i - 1][j - 1] + (pairwiseScore),
                 E[i][j], F[i][j]);
         traceback[i][j] = findTrace(i, j);
       }