update author list in license for (JAL-826)
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 6bba5d2..d0fc927 100644 (file)
@@ -1,20 +1,19 @@
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)\r
- * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
  * \r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
+ * This file is part of Jalview.\r
  * \r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
  * \r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 package jalview.datamodel;\r
 \r
@@ -36,7 +35,7 @@ public class ColumnSelection
    * Add a column to the selection\r
    * \r
    * @param col\r
-   *                index of column\r
+   *          index of column\r
    */\r
   public void addElement(int col)\r
   {\r
@@ -59,7 +58,7 @@ public class ColumnSelection
    * removes col from selection\r
    * \r
    * @param col\r
-   *                index of column to be removed\r
+   *          index of column to be removed\r
    */\r
   public void removeElement(int col)\r
   {\r
@@ -75,9 +74,9 @@ public class ColumnSelection
    * removes a range of columns from the selection\r
    * \r
    * @param start\r
-   *                int - first column in range to be removed\r
+   *          int - first column in range to be removed\r
    * @param end\r
-   *                int - last col\r
+   *          int - last col\r
    */\r
   public void removeElements(int start, int end)\r
   {\r
@@ -104,7 +103,7 @@ public class ColumnSelection
   /**\r
    * \r
    * @param col\r
-   *                index to search for in column selection\r
+   *          index to search for in column selection\r
    * \r
    * @return true if Integer(col) is in selection.\r
    */\r
@@ -117,7 +116,7 @@ public class ColumnSelection
    * Column number at position i in selection\r
    * \r
    * @param i\r
-   *                index into selected columns\r
+   *          index into selected columns\r
    * \r
    * @return column number in alignment\r
    */\r
@@ -180,9 +179,9 @@ public class ColumnSelection
    * propagate shift in alignment columns to column selection\r
    * \r
    * @param start\r
-   *                beginning of edit\r
+   *          beginning of edit\r
    * @param left\r
-   *                shift in edit (+ve for removal, or -ve for inserts)\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   public Vector compensateForEdit(int start, int change)\r
   {\r
@@ -238,9 +237,9 @@ public class ColumnSelection
    * compensateForEdit - allowing for edits within hidden regions\r
    * \r
    * @param start\r
-   *                beginning of edit\r
+   *          beginning of edit\r
    * @param left\r
-   *                shift in edit (+ve for removal, or -ve for inserts)\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   private void compensateForDelEdits(int start, int change)\r
   {\r
@@ -403,7 +402,7 @@ public class ColumnSelection
       }\r
     }\r
     return pruned; // true if any interval was removed or modified by\r
-                    // operations.\r
+    // operations.\r
   }\r
 \r
   private boolean pruneColumnList(Vector deletion, Vector list)\r
@@ -476,7 +475,7 @@ public class ColumnSelection
    * given index.\r
    * \r
    * @param end\r
-   *                int\r
+   *          int\r
    * @return Vector\r
    */\r
   public Vector getHiddenColumns()\r
@@ -488,7 +487,7 @@ public class ColumnSelection
    * Return absolute column index for a visible column index\r
    * \r
    * @param column\r
-   *                int column index in alignment view\r
+   *          int column index in alignment view\r
    * @return alignment column index for column\r
    */\r
   public int adjustForHiddenColumns(int column)\r
@@ -509,11 +508,12 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * Use this method to find out where a visible column is in the alignment when\r
-   * hidden columns exist\r
+   * Use this method to find out where a column will appear in the visible\r
+   * alignment when hidden columns exist. If the column is not visible, then the\r
+   * left-most visible column will always be returned.\r
    * \r
    * @param hiddenColumn\r
-   *                int\r
+   *          int\r
    * @return int\r
    */\r
   public int findColumnPosition(int hiddenColumn)\r
@@ -522,21 +522,21 @@ public class ColumnSelection
     if (hiddenColumns != null)\r
     {\r
       int index = 0;\r
-      int gaps = 0;\r
+      int[] region;\r
       do\r
       {\r
-        int[] region = (int[]) hiddenColumns.elementAt(index);\r
+        region = (int[]) hiddenColumns.elementAt(index++);\r
         if (hiddenColumn > region[1])\r
         {\r
           result -= region[1] + 1 - region[0];\r
         }\r
-        index++;\r
-      } while (index < hiddenColumns.size());\r
-\r
-      result -= gaps;\r
+      } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));\r
+      if (hiddenColumn > region[0] && hiddenColumn < region[1])\r
+      {\r
+        return region[0] + hiddenColumn - result;\r
+      }\r
     }\r
-\r
-    return result;\r
+    return result; // return the shifted position after removing hidden columns.\r
   }\r
 \r
   /**\r
@@ -573,7 +573,7 @@ public class ColumnSelection
    * hidden columns. In otherwords, the next hidden column.\r
    * \r
    * @param index\r
-   *                int\r
+   *          int\r
    */\r
   public int getHiddenBoundaryRight(int alPos)\r
   {\r
@@ -601,7 +601,7 @@ public class ColumnSelection
    * hidden columns. In otherwords, the previous hidden column.\r
    * \r
    * @param index\r
-   *                int\r
+   *          int\r
    */\r
   public int getHiddenBoundaryLeft(int alPos)\r
   {\r
@@ -679,7 +679,7 @@ public class ColumnSelection
    * specified\r
    * \r
    * @param res\r
-   *                int\r
+   *          int\r
    */\r
   public void hideColumns(int col)\r
   {\r
@@ -867,10 +867,11 @@ public class ColumnSelection
    * return all visible segments between the given start and end boundaries\r
    * \r
    * @param start\r
-   *                (first column inclusive from 0)\r
+   *          (first column inclusive from 0)\r
    * @param end\r
-   *                (last column - not inclusive)\r
-   * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end\r
+   *          (last column - not inclusive)\r
+   * @return int[] {i_start, i_end, ..} where intervals lie in\r
+   *         start<=i_start<=i_end<end\r
    */\r
   public int[] getVisibleContigs(int start, int end)\r
   {\r
@@ -940,11 +941,11 @@ public class ColumnSelection
    * sequence associated annotation).\r
    * \r
    * @param start\r
-   *                remove any annotation to the right of this column\r
+   *          remove any annotation to the right of this column\r
    * @param end\r
-   *                remove any annotation to the left of this column\r
+   *          remove any annotation to the left of this column\r
    * @param alignmentAnnotation\r
-   *                the annotation to operate on\r
+   *          the annotation to operate on\r
    */\r
   public void makeVisibleAnnotation(int start, int end,\r
           AlignmentAnnotation alignmentAnnotation)\r
@@ -998,7 +999,7 @@ public class ColumnSelection
       if (end > blockStart)\r
       {\r
         annels.addElement(els = new Annotation[end - blockStart + 1]);\r
-        if ((els.length+blockStart)<= alignmentAnnotation.annotations.length)\r
+        if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)\r
         {\r
           // copy just the visible segment of the annotation row\r
           System.arraycopy(alignmentAnnotation.annotations, blockStart,\r
@@ -1008,7 +1009,8 @@ public class ColumnSelection
         {\r
           // copy to the end of the annotation row\r
           System.arraycopy(alignmentAnnotation.annotations, blockStart,\r
-                  els, 0, (alignmentAnnotation.annotations.length - blockStart));\r
+                  els, 0,\r
+                  (alignmentAnnotation.annotations.length - blockStart));\r
         }\r
         w += els.length;\r
       }\r
@@ -1056,4 +1058,188 @@ public class ColumnSelection
       }\r
     }\r
   }\r
+\r
+  /**\r
+   * add in any unselected columns from the given column selection, excluding\r
+   * any that are hidden.\r
+   * \r
+   * @param colsel\r
+   */\r
+  public void addElementsFrom(ColumnSelection colsel)\r
+  {\r
+    if (colsel != null && colsel.size() > 0)\r
+    {\r
+      Enumeration e = colsel.getSelected().elements();\r
+      while (e.hasMoreElements())\r
+      {\r
+        Object eo = e.nextElement();\r
+        if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))\r
+        {\r
+          if (!selected.contains(eo))\r
+          {\r
+            selected.addElement(eo);\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * set the selected columns the given column selection, excluding any columns\r
+   * that are hidden.\r
+   * \r
+   * @param colsel\r
+   */\r
+  public void setElementsFrom(ColumnSelection colsel)\r
+  {\r
+    selected = new Vector();\r
+    if (colsel.selected != null && colsel.selected.size() > 0)\r
+    {\r
+      if (hiddenColumns != null && hiddenColumns.size() > 0)\r
+      {\r
+        // only select visible columns in this columns selection\r
+        selected = new Vector();\r
+        addElementsFrom(colsel);\r
+      }\r
+      else\r
+      {\r
+        // add everything regardless\r
+        Enumeration en = colsel.selected.elements();\r
+        while (en.hasMoreElements())\r
+        {\r
+          selected.addElement(en.nextElement());\r
+        }\r
+      }\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
+                  if (diff>=sb.length()) {\r
+                    sq+=sb.toString();\r
+                  } else {\r
+                    char[] buf = new char[diff];\r
+                    sb.getChars(0, diff, buf, 0);\r
+                    sq+=buf.toString();\r
+                  }\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
+          if (diff>=sb.length()) {\r
+            sq+=sb.toString();\r
+          } else {\r
+            char[] buf = new char[diff];\r
+            sb.getChars(0, diff, buf, 0);\r
+            sq+=buf.toString();\r
+          }\r
+          diff = origseq.getLength() - sq.length();\r
+        }\r
+      }\r
+    }\r
+  }\r
 }\r