refactored gap insert/visible view modification code from JPredThread
authorjprocter <Jim Procter>
Fri, 17 Dec 2010 13:17:29 +0000 (13:17 +0000)
committerjprocter <Jim Procter>
Fri, 17 Dec 2010 13:17:29 +0000 (13:17 +0000)
src/jalview/datamodel/ColumnSelection.java

index a36faf7..0bcbd5b 100644 (file)
@@ -1112,4 +1112,120 @@ public class ColumnSelection
       }\r
     }\r
   }\r
+\r
+  /**\r
+   * Add gaps into the sequences aligned to profileseq under the given\r
+   * AlignmentView\r
+   * \r
+   * @param profileseq\r
+   * @param al - alignment to have gaps inserted into it\r
+   * @param input - alignment view where sequence corresponding to profileseq is first entry\r
+   * @return new Column selection for new alignment view, with insertions into profileseq marked as hidden.\r
+   */\r
+  public static ColumnSelection propagateInsertions(SequenceI profileseq,\r
+          Alignment al, AlignmentView input)\r
+  {\r
+    int profsqpos=0;\r
+    \r
+//    return propagateInsertions(profileseq, al, )\r
+    char gc = al.getGapCharacter();\r
+    Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);\r
+    ColumnSelection nview = (ColumnSelection) alandcolsel[1];\r
+    SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];\r
+    nview.propagateInsertions(profileseq,\r
+            al, origseq);\r
+    return nview;\r
+  }\r
+  /**\r
+   * \r
+   * @param profileseq - sequence in al which corresponds to origseq \r
+   * @param al - alignment which is to have gaps inserted into it\r
+   * @param origseq - sequence corresponding to profileseq which defines gap map for modifying al\r
+   */\r
+  public void propagateInsertions(SequenceI profileseq, AlignmentI al, SequenceI origseq)\r
+  {\r
+    char gc = al.getGapCharacter();\r
+    // recover mapping between sequence's non-gap positions and positions \r
+    // mapping to view.\r
+    pruneDeletions(ShiftList\r
+            .parseMap(origseq\r
+                    .gapMap())); \r
+    int[] viscontigs = getVisibleContigs(0, profileseq.getLength());\r
+    int spos = 0;\r
+    int offset = 0;\r
+    // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])\r
+    // alandcolsel[0])[0].gapMap()))\r
+    // add profile to visible contigs\r
+    for (int v = 0; v < viscontigs.length; v += 2)\r
+    {\r
+      if (viscontigs[v] > spos)\r
+      {\r
+        StringBuffer sb = new StringBuffer();\r
+        for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)\r
+        {\r
+          sb.append(gc);\r
+        }\r
+        for (int s = 0, ns = al.getHeight(); s < ns; s++)\r
+        {\r
+          SequenceI sqobj = al.getSequenceAt(s);\r
+          if (sqobj != profileseq)\r
+          {\r
+            String sq = al.getSequenceAt(s).getSequenceAsString();\r
+            if (sq.length() <= spos + offset)\r
+            {\r
+              // pad sequence\r
+              int diff = spos + offset - sq.length() - 1;\r
+              if (diff > 0)\r
+              {\r
+                // pad gaps\r
+                sq = sq + sb;\r
+                while ((diff = spos + offset - sq.length() - 1) > 0)\r
+                {\r
+                  sq = sq\r
+                          + ((diff >= sb.length()) ? sb.toString() : sb\r
+                                  .substring(0, diff));\r
+                }\r
+              }\r
+              sq += sb.toString();\r
+            }\r
+            else\r
+            {\r
+              al.getSequenceAt(s).setSequence(\r
+                      sq.substring(0, spos + offset) + sb.toString()\r
+                              + sq.substring(spos + offset));\r
+            }\r
+          }\r
+        }\r
+        // offset+=sb.length();\r
+      }\r
+      spos = viscontigs[v + 1] + 1;\r
+    }\r
+    if ((offset + spos) < profileseq.getLength())\r
+    {\r
+      // pad the final region with gaps.\r
+      StringBuffer sb = new StringBuffer();\r
+      for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)\r
+      {\r
+        sb.append(gc);\r
+      }\r
+      for (int s = 0, ns = al.getHeight(); s < ns; s++)\r
+      {\r
+        SequenceI sqobj = al.getSequenceAt(s);\r
+        if (sqobj==profileseq)\r
+        {\r
+          continue;\r
+        }\r
+        String sq = sqobj.getSequenceAsString();\r
+        // pad sequence\r
+        int diff = origseq.getLength() - sq.length();\r
+        while (diff > 0)\r
+        {\r
+          sq = sq\r
+                  + ((diff >= sb.length()) ? sb.toString() : sb\r
+                          .substring(0, diff));\r
+          diff = origseq.getLength() - sq.length();\r
+        }\r
+      }\r
+    }\r
+  }\r
 }\r