JAL-2103 patch routine for inserting gaps for hidden regions in a prediction
authorJim Procter <jprocter@issues.jalview.org>
Thu, 12 May 2016 19:59:41 +0000 (20:59 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 12 May 2016 20:00:10 +0000 (21:00 +0100)
src/jalview/ws/jws1/JPredWSUtils.java

index 49c4fb3..2d562e9 100644 (file)
@@ -14,7 +14,6 @@ import java.io.IOException;
 import java.util.Hashtable;
 import java.util.List;
 
-
 /**
  * extraction of processing routines to allow mocking
  * 
@@ -47,8 +46,7 @@ public class JPredWSUtils
    * @throws Exception
    */
   public static Object[] processJnetResult(AlignmentI currentView,
-          AlignmentView input,
-          char gapChar, Hashtable SequenceInfo,
+          AlignmentView input, char gapChar, Hashtable SequenceInfo,
           boolean msaPred, int[] predMap, String result_PredFile,
           String result_Aligfile, FileParse full_alignment)
           throws Exception
@@ -299,8 +297,7 @@ public class JPredWSUtils
    * @param profileseq
    */
   public static void insertHiddenResidues(AlignmentI al, char gc,
-          int[] predMap,
-          SequenceI origseq)
+          int[] predMap, SequenceI origseq)
   {
     // orig: asdfPPPPPPPasdfPPPPasdf
     // pred: PPPPPPPPPPP
@@ -313,12 +310,25 @@ public class JPredWSUtils
     // al: asdf-----P-P-P---P---P----P---Pasdf-P--PP---P---asdf
     // s1: ....SSSSSSS-SS---S---SSSSSS---S....-S--SSSSSSSSS....
     // s2: ....SSSSSSS-SSSSSSSSSSS----SSS-....S-SSS-----SSS....
+
+    // iteration 0: add asdf, append -----P
+    // iteration 1: append -P
+    // iteration 2: append -P
+    // iteration 3: append ---P
+    // iteration 4: append ---P
+    // iteration 5: append ----P
+    // iteration 6: append ---P
+    // iteration 7: append -P
+    // iteration 8: append P
+    // iteration 9: append P
+    // iteration 10: append ---P
+    // tail: append: ---, add asdf
+
     String alseq = "";
-    int lsp = 0;
     SequenceI predseq = al.getSequenceAt(0);
     int predIdx = 0; // next column of prediction to preserve
     // positions in original and prediction sequence
-    int lp = origseq.getStart(), predPos = predseq.getStart();
+    int lp = origseq.getStart() - 1, predPos = predseq.getStart();
     for (int r = 0; r < predMap.length; r++)
     {
       // also need to keep track of trimmed prediction sequence numbering
@@ -326,7 +336,7 @@ public class JPredWSUtils
       {
         // hidden region insert from origseq
         String insert = origseq.getSequenceAsString(
-                origseq.findIndex(lp) - 1,
+                origseq.findIndex(lp + 1) - 1,
                 origseq.findIndex(predMap[r]) - 1);
 
         insertGapsAt(al, gc, alseq.length(), insert.length());
@@ -334,7 +344,12 @@ public class JPredWSUtils
       }
       // Now update prediction sequence for next position.
       {
-        int predIdxNext = predseq.findIndex(predPos + 1) - 1;
+        int predIdxNext = predseq.findIndex(predPos); // everything up
+                                                      // to the current
+                                                      // position in the
+                                                      // prediction
+                                                      // sequence
+                                                      // alignment
         if (predIdxNext <= predIdx)
         {
           predIdxNext = predseq.getLength();
@@ -348,10 +363,28 @@ public class JPredWSUtils
       predPos++;
     }
     // append final bits
+    // add any remaining gaps
+    {
+      int predIdxNext = predseq.findIndex(predPos); // everything up
+                                                    // to the current
+                                                    // position in the
+                                                    // prediction
+                                                    // sequence
+                                                    // alignment
+      if (predIdxNext <= predIdx)
+      {
+        predIdxNext = predseq.getLength();
+      }
+      // just add in next segment of predseq
+      String predsert = predseq.getSequenceAsString(predIdx, predIdxNext);
+      alseq += predsert;
+      predIdx = predIdxNext;
+    }
+
     if (lp < origseq.getEnd())
     {
       String insert = origseq.getSequenceAsString(
-              origseq.findIndex(lp) - 1, origseq.getLength());
+              origseq.findIndex(lp + 1) - 1, origseq.getLength());
       insertGapsAt(al, gc, alseq.length(), insert.length());
       alseq += insert;
     }
@@ -366,7 +399,7 @@ public class JPredWSUtils
     {
       if (gapMap[r] - lp > 1)
       {
-        insertGapsAt(al, gc, gapMap[r], gapMap[r]-lp);
+        insertGapsAt(al, gc, gapMap[r], gapMap[r] - lp);
       }
       lp = gapMap[r];
     }