setting and merging selected columns from another selection object
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 6ef1799..afa2e07 100644 (file)
@@ -1,17 +1,17 @@
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\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
+ * \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
- *\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
- *\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
@@ -29,13 +29,14 @@ public class ColumnSelection
 {\r
   Vector selected = new Vector();\r
 \r
-  //Vector of int [] {startCol, endCol}\r
+  // Vector of int [] {startCol, endCol}\r
   Vector hiddenColumns;\r
 \r
   /**\r
    * Add a column to the selection\r
-   *\r
-   * @param col index of column\r
+   * \r
+   * @param col\r
+   *          index of column\r
    */\r
   public void addElement(int col)\r
   {\r
@@ -56,8 +57,9 @@ public class ColumnSelection
 \r
   /**\r
    * removes col from selection\r
-   *\r
-   * @param col index of column to be removed\r
+   * \r
+   * @param col\r
+   *          index of column to be removed\r
    */\r
   public void removeElement(int col)\r
   {\r
@@ -71,8 +73,11 @@ public class ColumnSelection
 \r
   /**\r
    * removes a range of columns from the selection\r
-   * @param start int - first column in range to be removed\r
-   * @param end int - last col\r
+   * \r
+   * @param start\r
+   *          int - first column in range to be removed\r
+   * @param end\r
+   *          int - last col\r
    */\r
   public void removeElements(int start, int end)\r
   {\r
@@ -88,7 +93,7 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   *\r
+   * \r
    * @return Vector containing selected columns as Integers\r
    */\r
   public Vector getSelected()\r
@@ -97,9 +102,10 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   *\r
-   * @param col index to search for in column selection\r
-   *\r
+   * \r
+   * @param col\r
+   *          index to search for in column selection\r
+   * \r
    * @return true if Integer(col) is in selection.\r
    */\r
   public boolean contains(int col)\r
@@ -108,20 +114,21 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param i DOCUMENT ME!\r
-   *\r
-   * @return DOCUMENT ME!\r
+   * Column number at position i in selection\r
+   * \r
+   * @param i\r
+   *          index into selected columns\r
+   * \r
+   * @return column number in alignment\r
    */\r
   public int columnAt(int i)\r
   {\r
-    return ( (Integer) selected.elementAt(i)).intValue();\r
+    return ((Integer) selected.elementAt(i)).intValue();\r
   }\r
 \r
   /**\r
    * DOCUMENT ME!\r
-   *\r
+   * \r
    * @return DOCUMENT ME!\r
    */\r
   public int size()\r
@@ -130,9 +137,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @return DOCUMENT ME!\r
+   * rightmost selected column\r
+   * \r
+   * @return rightmost column in alignment that is selected\r
    */\r
   public int getMax()\r
   {\r
@@ -150,9 +157,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @return DOCUMENT ME!\r
+   * Leftmost column in selection\r
+   * \r
+   * @return column index of leftmost column in selection\r
    */\r
   public int getMin()\r
   {\r
@@ -171,9 +178,11 @@ public class ColumnSelection
 \r
   /**\r
    * propagate shift in alignment columns to column selection\r
-   *\r
-   * @param start beginning of edit\r
-   * @param left shift in edit (+ve for removal, or -ve for inserts)\r
+   * \r
+   * @param start\r
+   *          beginning of edit\r
+   * @param left\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   public Vector compensateForEdit(int start, int change)\r
   {\r
@@ -197,8 +206,7 @@ public class ColumnSelection
         int[] region = (int[]) hiddenColumns.elementAt(i);\r
         if (region[0] > start && start + change > region[1])\r
         {\r
-          deletedHiddenColumns.addElement(\r
-              hiddenColumns.elementAt(i));\r
+          deletedHiddenColumns.addElement(hiddenColumns.elementAt(i));\r
 \r
           hiddenColumns.removeElementAt(i);\r
           i--;\r
@@ -226,10 +234,13 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * propagate shift in alignment columns to column selection\r
-   * special version of compensateForEdit - allowing for edits within hidden regions\r
-   * @param start beginning of edit\r
-   * @param left shift in edit (+ve for removal, or -ve for inserts)\r
+   * propagate shift in alignment columns to column selection special version of\r
+   * compensateForEdit - allowing for edits within hidden regions\r
+   * \r
+   * @param start\r
+   *          beginning of edit\r
+   * @param left\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   private void compensateForDelEdits(int start, int change)\r
   {\r
@@ -274,8 +285,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * Adjust hidden column boundaries based on a series of column\r
-   * additions or deletions in visible regions.\r
+   * Adjust hidden column boundaries based on a series of column additions or\r
+   * deletions in visible regions.\r
+   * \r
    * @param shiftrecord\r
    * @return\r
    */\r
@@ -290,7 +302,7 @@ public class ColumnSelection
         for (int i = 0, j = shifts.size(); i < j; i++)\r
         {\r
           int[] sh = (int[]) shifts.elementAt(i);\r
-          //compensateForEdit(shifted+sh[0], sh[1]);\r
+          // compensateForEdit(shifted+sh[0], sh[1]);\r
           compensateForDelEdits(shifted + sh[0], sh[1]);\r
           shifted -= sh[1];\r
         }\r
@@ -301,8 +313,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * removes intersection of position,length ranges in deletions\r
-   * from the start,end regions marked in intervals.\r
+   * removes intersection of position,length ranges in deletions from the\r
+   * start,end regions marked in intervals.\r
+   * \r
    * @param deletions\r
    * @param intervals\r
    * @return\r
@@ -389,7 +402,8 @@ public class ColumnSelection
         }\r
       }\r
     }\r
-    return pruned; // true if any interval was removed or modified by operations.\r
+    return pruned; // true if any interval was removed or modified by\r
+    // operations.\r
   }\r
 \r
   private boolean pruneColumnList(Vector deletion, Vector list)\r
@@ -400,7 +414,7 @@ public class ColumnSelection
     int i = 0, j = list.size();\r
     while (i < j && s <= t)\r
     {\r
-      int c = ( (Integer) list.elementAt(i++)).intValue();\r
+      int c = ((Integer) list.elementAt(i++)).intValue();\r
       if (sr[0] <= c)\r
       {\r
         if (sr[1] + sr[0] >= c)\r
@@ -422,8 +436,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * remove any hiddenColumns or selected columns and shift remaining\r
-   * based on a series of position, range deletions.\r
+   * remove any hiddenColumns or selected columns and shift remaining based on a\r
+   * series of position, range deletions.\r
+   * \r
    * @param deletions\r
    */\r
   public void pruneDeletions(ShiftList deletions)\r
@@ -457,9 +472,11 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * This Method is used to return all the HiddenColumn regions\r
-   * less than the given index.\r
-   * @param end int\r
+   * This Method is used to return all the HiddenColumn regions less than the\r
+   * given index.\r
+   * \r
+   * @param end\r
+   *          int\r
    * @return Vector\r
    */\r
   public Vector getHiddenColumns()\r
@@ -469,7 +486,9 @@ public class ColumnSelection
 \r
   /**\r
    * Return absolute column index for a visible column index\r
-   * @param column int column index in alignment view\r
+   * \r
+   * @param column\r
+   *          int column index in alignment view\r
    * @return alignment column index for column\r
    */\r
   public int adjustForHiddenColumns(int column)\r
@@ -490,9 +509,11 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * Use this method to find out where a visible column is in the alignment\r
-   * when hidden columns exist\r
-   * @param hiddenColumn int\r
+   * Use this method to find out where a visible column is in the alignment when\r
+   * hidden columns exist\r
+   * \r
+   * @param hiddenColumn\r
+   *          int\r
    * @return int\r
    */\r
   public int findColumnPosition(int hiddenColumn)\r
@@ -510,8 +531,7 @@ public class ColumnSelection
           result -= region[1] + 1 - region[0];\r
         }\r
         index++;\r
-      }\r
-      while (index < hiddenColumns.size());\r
+      } while (index < hiddenColumns.size());\r
 \r
       result -= gaps;\r
     }\r
@@ -540,8 +560,7 @@ public class ColumnSelection
         gaps += region[1] + 1 - region[0];\r
         result = region[1] + 1;\r
         index++;\r
-      }\r
-      while (index < hiddenRegion + 1);\r
+      } while (index < hiddenRegion + 1);\r
 \r
       result -= gaps;\r
     }\r
@@ -550,10 +569,11 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * THis method returns the rightmost limit of a\r
-   * region of an alignment with hidden columns.\r
-   * In otherwords, the next hidden column.\r
-   * @param index int\r
+   * THis method returns the rightmost limit of a region of an alignment with\r
+   * hidden columns. In otherwords, the next hidden column.\r
+   * \r
+   * @param index\r
+   *          int\r
    */\r
   public int getHiddenBoundaryRight(int alPos)\r
   {\r
@@ -569,8 +589,7 @@ public class ColumnSelection
         }\r
 \r
         index++;\r
-      }\r
-      while (index < hiddenColumns.size());\r
+      } while (index < hiddenColumns.size());\r
     }\r
 \r
     return alPos;\r
@@ -578,10 +597,11 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * THis method returns the rightmost limit of a\r
-   * region of an alignment with hidden columns.\r
-   * In otherwords, the next hidden column.\r
-   * @param index int\r
+   * This method returns the leftmost limit of a region of an alignment with\r
+   * hidden columns. In otherwords, the previous hidden column.\r
+   * \r
+   * @param index\r
+   *          int\r
    */\r
   public int getHiddenBoundaryLeft(int alPos)\r
   {\r
@@ -597,8 +617,7 @@ public class ColumnSelection
         }\r
 \r
         index--;\r
-      }\r
-      while (index > -1);\r
+      } while (index > -1);\r
     }\r
 \r
     return alPos;\r
@@ -609,7 +628,7 @@ public class ColumnSelection
   {\r
     while (size() > 0)\r
     {\r
-      int column = ( (Integer) getSelected().firstElement()).intValue();\r
+      int column = ((Integer) getSelected().firstElement()).intValue();\r
       hideColumns(column);\r
     }\r
 \r
@@ -637,7 +656,7 @@ public class ColumnSelection
       else if (end < region[0] && start < region[0])\r
       {\r
         hiddenColumns.insertElementAt(new int[]\r
-                                      {start, end}, i);\r
+        { start, end }, i);\r
         added = true;\r
         break;\r
       }\r
@@ -650,15 +669,17 @@ public class ColumnSelection
     else if (!added)\r
     {\r
       hiddenColumns.addElement(new int[]\r
-                               {start, end});\r
+      { start, end });\r
     }\r
 \r
   }\r
 \r
   /**\r
-   * This method will find a range of selected columns\r
-   * around the column specified\r
-   * @param res int\r
+   * This method will find a range of selected columns around the column\r
+   * specified\r
+   * \r
+   * @param res\r
+   *          int\r
    */\r
   public void hideColumns(int col)\r
   {\r
@@ -727,19 +748,22 @@ public class ColumnSelection
 \r
   public boolean isVisible(int column)\r
   {\r
-    for (int i = 0; i < hiddenColumns.size(); i++)\r
-    {\r
-      int[] region = (int[]) hiddenColumns.elementAt(i);\r
-      if (column >= region[0] && column <= region[1])\r
+    if (hiddenColumns != null)\r
+      for (int i = 0; i < hiddenColumns.size(); i++)\r
       {\r
-        return false;\r
+        int[] region = (int[]) hiddenColumns.elementAt(i);\r
+        if (column >= region[0] && column <= region[1])\r
+        {\r
+          return false;\r
+        }\r
       }\r
-    }\r
+\r
     return true;\r
   }\r
 \r
   /**\r
    * Copy constructor\r
+   * \r
    * @param copy\r
    */\r
   public ColumnSelection(ColumnSelection copy)\r
@@ -780,7 +804,7 @@ public class ColumnSelection
   }\r
 \r
   public String[] getVisibleSequenceStrings(int start, int end,\r
-                                            SequenceI[] seqs)\r
+          SequenceI[] seqs)\r
   {\r
     int i, iSize = seqs.length;\r
     String selection[] = new String[iSize];\r
@@ -841,10 +865,13 @@ public class ColumnSelection
 \r
   /**\r
    * return all visible segments between the given start and end boundaries\r
-   *\r
-   * @param start (first column inclusive from 0)\r
-   * @param end (last column - not inclusive)\r
-   * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end\r
+   * \r
+   * @param start\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\r
+   *         start<=i_start<=i_end<end\r
    */\r
   public int[] getVisibleContigs(int start, int end)\r
   {\r
@@ -870,7 +897,7 @@ public class ColumnSelection
         if (hideStart > vstart)\r
         {\r
           visiblecontigs.addElement(new int[]\r
-                                    {vstart, hideStart - 1});\r
+          { vstart, hideStart - 1 });\r
         }\r
         vstart = hideEnd + 1;\r
       }\r
@@ -878,7 +905,7 @@ public class ColumnSelection
       if (vstart < end)\r
       {\r
         visiblecontigs.addElement(new int[]\r
-                                  {vstart, end - 1});\r
+        { vstart, end - 1 });\r
       }\r
       int[] vcontigs = new int[visiblecontigs.size() * 2];\r
       for (int i = 0, j = visiblecontigs.size(); i < j; i++)\r
@@ -894,8 +921,186 @@ public class ColumnSelection
     else\r
     {\r
       return new int[]\r
+      { start, end - 1 };\r
+    }\r
+  }\r
+\r
+  /**\r
+   * delete any columns in alignmentAnnotation that are hidden (including\r
+   * sequence associated annotation).\r
+   * \r
+   * @param alignmentAnnotation\r
+   */\r
+  public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)\r
+  {\r
+    makeVisibleAnnotation(-1, -1, alignmentAnnotation);\r
+  }\r
+\r
+  /**\r
+   * delete any columns in alignmentAnnotation that are hidden (including\r
+   * sequence associated annotation).\r
+   * \r
+   * @param start\r
+   *          remove any annotation to the right of this column\r
+   * @param end\r
+   *          remove any annotation to the left of this column\r
+   * @param alignmentAnnotation\r
+   *          the annotation to operate on\r
+   */\r
+  public void makeVisibleAnnotation(int start, int end,\r
+          AlignmentAnnotation alignmentAnnotation)\r
+  {\r
+    if (alignmentAnnotation.annotations == null)\r
+    {\r
+      return;\r
+    }\r
+    if (start == end && end == -1)\r
+    {\r
+      start = 0;\r
+      end = alignmentAnnotation.annotations.length;\r
+    }\r
+    if (hiddenColumns != null && hiddenColumns.size() > 0)\r
+    {\r
+      // then mangle the alignmentAnnotation annotation array\r
+      Vector annels = new Vector();\r
+      Annotation[] els = null;\r
+      Vector regions = getHiddenColumns();\r
+      int blockStart = start, blockEnd = end;\r
+      int[] region;\r
+      int hideStart, hideEnd, w = 0;\r
+\r
+      for (int j = 0; j < regions.size(); j++)\r
+      {\r
+        region = (int[]) regions.elementAt(j);\r
+        hideStart = region[0];\r
+        hideEnd = region[1];\r
+\r
+        if (hideStart < start)\r
+        {\r
+          continue;\r
+        }\r
+\r
+        blockStart = Math.min(blockStart, hideEnd + 1);\r
+        blockEnd = Math.min(blockEnd, hideStart);\r
+\r
+        if (blockStart > blockEnd)\r
+        {\r
+          break;\r
+        }\r
+\r
+        annels.addElement(els = new Annotation[blockEnd - blockStart]);\r
+        System.arraycopy(alignmentAnnotation.annotations, blockStart, els,\r
+                0, els.length);\r
+        w += els.length;\r
+        blockStart = hideEnd + 1;\r
+        blockEnd = end;\r
+      }\r
+\r
+      if (end > blockStart)\r
+      {\r
+        annels.addElement(els = new Annotation[end - blockStart + 1]);\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
+                  els, 0, els.length);\r
+        }\r
+        else\r
+        {\r
+          // copy to the end of the annotation row\r
+          System.arraycopy(alignmentAnnotation.annotations, blockStart,\r
+                  els, 0,\r
+                  (alignmentAnnotation.annotations.length - blockStart));\r
+        }\r
+        w += els.length;\r
+      }\r
+      if (w == 0)\r
+        return;\r
+      Enumeration e = annels.elements();\r
+      alignmentAnnotation.annotations = new Annotation[w];\r
+      w = 0;\r
+      while (e.hasMoreElements())\r
+      {\r
+        Annotation[] chnk = (Annotation[]) e.nextElement();\r
+        System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,\r
+                chnk.length);\r
+        w += chnk.length;\r
+      }\r
+    }\r
+    else\r
+    {\r
+      alignmentAnnotation.restrict(start, end);\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Invert the column selection from first to end-1. leaves hiddenColumns\r
+   * untouched (and unselected)\r
+   * \r
+   * @param first\r
+   * @param end\r
+   */\r
+  public void invertColumnSelection(int first, int width)\r
+  {\r
+    boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;\r
+    for (int i = first; i < width; i++)\r
+    {\r
+      if (contains(i))\r
+      {\r
+        removeElement(i);\r
+      }\r
+      else\r
+      {\r
+        if (!hasHidden || isVisible(i))\r
+        {\r
+          addElement(i);\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * add in any unselected columns from the given column selection, excluding any that are hidden.\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
+          if (!selected.contains(eo))\r
           {\r
-          start, end - 1};\r
+            selected.addElement(eo);\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+/**\r
+ * set the selected columns the given column selection, excluding any columns that are hidden. \r
+ * @param colsel\r
+ */\r
+  public void setElementsFrom(ColumnSelection colsel)\r
+  {\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
+      } else {\r
+        // be quick\r
+        selected = new Vector(colsel.selected);\r
+      }\r
+    }\r
+    else\r
+    {\r
+      selected = new Vector();\r
     }\r
   }\r
 }\r