applied copyright 2008
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 6ef1799..9ec4270 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 (Version 2.4)\r
+ * Copyright (C) 2008 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
@@ -108,11 +108,11 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
+   * Column number at position i in selection\r
    *\r
-   * @param i DOCUMENT ME!\r
+   * @param i index into selected columns\r
    *\r
-   * @return DOCUMENT ME!\r
+   * @return column number in alignment\r
    */\r
   public int columnAt(int i)\r
   {\r
@@ -130,9 +130,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
+   * rightmost selected column\r
    *\r
-   * @return DOCUMENT ME!\r
+   * @return rightmost column in alignment that is selected\r
    */\r
   public int getMax()\r
   {\r
@@ -150,9 +150,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * DOCUMENT ME!\r
+   * Leftmost column in selection\r
    *\r
-   * @return DOCUMENT ME!\r
+   * @return column index of leftmost column in selection\r
    */\r
   public int getMin()\r
   {\r
@@ -578,9 +578,9 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * THis method returns the rightmost limit of a\r
+   * This method returns the leftmost limit of a\r
    * region of an alignment with hidden columns.\r
-   * In otherwords, the next hidden column.\r
+   * In otherwords, the previous hidden column.\r
    * @param index int\r
    */\r
   public int getHiddenBoundaryLeft(int alPos)\r
@@ -727,14 +727,16 @@ 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
@@ -898,4 +900,117 @@ public class ColumnSelection
           start, end - 1};\r
     }\r
   }\r
+\r
+  /**\r
+   * delete any columns in alignmentAnnotation that are hidden (including sequence associated annotation).\r
+   * @param alignmentAnnotation\r
+   */\r
+  public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)\r
+  {\r
+    makeVisibleAnnotation(-1,-1,alignmentAnnotation);\r
+  }\r
+  /**\r
+   * delete any columns in alignmentAnnotation that are hidden (including sequence associated annotation).\r
+   * @param start remove any annotation to the right of this column\r
+   * @param end remove any annotation to the left of this column\r
+   * @param alignmentAnnotation the annotation to operate on\r
+   */\r
+  public void makeVisibleAnnotation(int start, int end, 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, 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 (end-blockStart+1<=alignmentAnnotation.annotations.length)\r
+          {\r
+            System.arraycopy(alignmentAnnotation.annotations, blockStart, els, 0, els.length);\r
+          } else {\r
+            System.arraycopy(alignmentAnnotation.annotations, blockStart, els, 0, (end-blockStart+1));\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, chnk.length);\r
+          w+=chnk.length;\r
+        }\r
+    } else {\r
+      alignmentAnnotation.restrict(start, end);\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Invert the column selection from first to end-1.\r
+   * leaves hiddenColumns untouched (and unselected)\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