JAL-1683 replace year/version strings with tokens in source
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 35d467a..b7bc40e 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.datamodel;
 
-import java.util.*;
+import jalview.util.ShiftList;
+import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
+import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
 
-import jalview.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Vector;
 
 /**
  * NOTE: Columns are zero based.
@@ -32,7 +38,7 @@ public class ColumnSelection
   Vector selected = new Vector();
 
   // Vector of int [] {startCol, endCol}
-  Vector hiddenColumns;
+  Vector<int[]> hiddenColumns;
 
   /**
    * Add a column to the selection
@@ -98,7 +104,7 @@ public class ColumnSelection
    * 
    * @return Vector containing selected columns as Integers
    */
-  public Vector getSelected()
+  public Vector<Integer> getSelected()
   {
     return selected;
   }
@@ -186,9 +192,9 @@ public class ColumnSelection
    * @param left
    *          shift in edit (+ve for removal, or -ve for inserts)
    */
-  public Vector compensateForEdit(int start, int change)
+  public List<int[]> compensateForEdit(int start, int change)
   {
-    Vector deletedHiddenColumns = null;
+    List<int[]> deletedHiddenColumns = null;
     for (int i = 0; i < size(); i++)
     {
       int temp = columnAt(i);
@@ -201,14 +207,14 @@ public class ColumnSelection
 
     if (hiddenColumns != null)
     {
-      deletedHiddenColumns = new Vector();
+      deletedHiddenColumns = new ArrayList<int[]>();
       int hSize = hiddenColumns.size();
       for (int i = 0; i < hSize; i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (region[0] > start && start + change > region[1])
         {
-          deletedHiddenColumns.addElement(hiddenColumns.elementAt(i));
+          deletedHiddenColumns.add(region);
 
           hiddenColumns.removeElementAt(i);
           i--;
@@ -260,7 +266,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (region[0] >= start)
         {
           region[0] -= change;
@@ -474,16 +480,13 @@ public class ColumnSelection
   }
 
   /**
-   * This Method is used to return all the HiddenColumn regions less than the
-   * given index.
-   * 
-   * @param end
-   *          int
-   * @return Vector
+   * This Method is used to return all the HiddenColumn regions
+   * @return empty list or List of hidden column intervals
    */
-  public Vector getHiddenColumns()
+  public List<int[]> getHiddenColumns()
   {
-    return hiddenColumns;
+    return hiddenColumns == null ? Arrays.asList(new int[]
+    {}) : hiddenColumns;
   }
 
   /**
@@ -500,7 +503,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (result >= region[0])
         {
           result += region[1] - region[0] + 1;
@@ -528,7 +531,7 @@ public class ColumnSelection
       int[] region;
       do
       {
-        region = (int[]) hiddenColumns.elementAt(index++);
+        region = hiddenColumns.elementAt(index++);
         if (hiddenColumn > region[1])
         {
           result -= region[1] + 1 - region[0];
@@ -554,7 +557,7 @@ public class ColumnSelection
       int gaps = 0;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (hiddenRegion == 0)
         {
           return region[0];
@@ -585,7 +588,7 @@ public class ColumnSelection
       int index = 0;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (alPos < region[0])
         {
           return region[0];
@@ -613,7 +616,7 @@ public class ColumnSelection
       int index = hiddenColumns.size() - 1;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (alPos > region[1])
         {
           return region[1];
@@ -631,7 +634,7 @@ public class ColumnSelection
   {
     while (size() > 0)
     {
-      int column = ((Integer) getSelected().firstElement()).intValue();
+      int column = getSelected().firstElement().intValue();
       hideColumns(column);
     }
 
@@ -649,7 +652,7 @@ public class ColumnSelection
 
     for (int i = 0; i < hiddenColumns.size(); i++)
     {
-      int[] region = (int[]) hiddenColumns.elementAt(i);
+      int[] region = hiddenColumns.elementAt(i);
       if (start <= region[1] && end >= region[0])
       {
         hiddenColumns.removeElementAt(i);
@@ -716,7 +719,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         for (int j = region[0]; j < region[1] + 1; j++)
         {
           addElement(j);
@@ -731,7 +734,7 @@ public class ColumnSelection
   {
     for (int i = 0; i < hiddenColumns.size(); i++)
     {
-      int[] region = (int[]) hiddenColumns.elementAt(i);
+      int[] region = hiddenColumns.elementAt(i);
       if (res == region[0])
       {
         for (int j = region[0]; j < region[1] + 1; j++)
@@ -752,14 +755,16 @@ public class ColumnSelection
   public boolean isVisible(int column)
   {
     if (hiddenColumns != null)
+    {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (column >= region[0] && column <= region[1])
         {
           return false;
         }
       }
+    }
 
     return true;
   }
@@ -787,7 +792,7 @@ public class ColumnSelection
         for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
         {
           int[] rh, cp;
-          rh = (int[]) copy.hiddenColumns.elementAt(i);
+          rh = copy.hiddenColumns.elementAt(i);
           if (rh != null)
           {
             cp = new int[rh.length];
@@ -816,7 +821,7 @@ public class ColumnSelection
       for (i = 0; i < iSize; i++)
       {
         StringBuffer visibleSeq = new StringBuffer();
-        Vector regions = getHiddenColumns();
+        List<int[]> regions = getHiddenColumns();
 
         int blockStart = start, blockEnd = end;
         int[] region;
@@ -824,7 +829,7 @@ public class ColumnSelection
 
         for (int j = 0; j < regions.size(); j++)
         {
-          region = (int[]) regions.elementAt(j);
+          region = regions.get(j);
           hideStart = region[0];
           hideEnd = region[1];
 
@@ -881,7 +886,7 @@ public class ColumnSelection
     if (hiddenColumns != null && hiddenColumns.size() > 0)
     {
       Vector visiblecontigs = new Vector();
-      Vector regions = getHiddenColumns();
+      List<int[]> regions = getHiddenColumns();
 
       int vstart = start;
       int[] region;
@@ -889,7 +894,7 @@ public class ColumnSelection
 
       for (int j = 0; vstart < end && j < regions.size(); j++)
       {
-        region = (int[]) regions.elementAt(j);
+        region = regions.get(j);
         hideStart = region[0];
         hideEnd = region[1];
 
@@ -967,14 +972,14 @@ public class ColumnSelection
       // then mangle the alignmentAnnotation annotation array
       Vector annels = new Vector();
       Annotation[] els = null;
-      Vector regions = getHiddenColumns();
+      List<int[]> regions = getHiddenColumns();
       int blockStart = start, blockEnd = end;
       int[] region;
       int hideStart, hideEnd, w = 0;
 
       for (int j = 0; j < regions.size(); j++)
       {
-        region = (int[]) regions.elementAt(j);
+        region = regions.get(j);
         hideStart = region[0];
         hideEnd = region[1];
 
@@ -1018,7 +1023,9 @@ public class ColumnSelection
         w += els.length;
       }
       if (w == 0)
+      {
         return;
+      }
       Enumeration e = annels.elements();
       alignmentAnnotation.annotations = new Annotation[w];
       w = 0;
@@ -1258,4 +1265,131 @@ public class ColumnSelection
       }
     }
   }
+
+  /**
+   * 
+   * @return true if there are columns marked
+   */
+  public boolean hasSelectedColumns()
+  {
+    return (selected != null && selected.size() > 0);
+  }
+
+  /**
+   * 
+   * @return true if there are columns hidden
+   */
+  public boolean hasHiddenColumns()
+  {
+    return hiddenColumns != null && hiddenColumns.size() > 0;
+  }
+  
+  /**
+   * 
+   * @return true if there are more than one set of columns hidden
+   */
+  public boolean hasManyHiddenColumns()
+  {
+    return hiddenColumns != null && hiddenColumns.size() > 1;
+  }
+
+  /**
+   * mark the columns corresponding to gap characters as hidden in the column
+   * selection
+   * 
+   * @param sr
+   */
+  public void hideInsertionsFor(SequenceI sr)
+  {
+    List<int[]> inserts = sr.getInsertions();
+    for (int[] r : inserts)
+    {
+      hideColumns(r[0], r[1]);
+    }
+  }
+  
+  public boolean filterAnnotations(Annotation[] annotations,
+          AnnotationFilterParameter filterParams)
+  {
+    this.revealAllHiddenColumns();
+    this.clear();
+    int count = 0;
+    do
+    {
+      if (annotations[count] != null)
+      {
+
+        boolean itemMatched = false;
+
+        if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
+                && annotations[count].value >= filterParams
+                        .getThresholdValue())
+        {
+          itemMatched = true;
+        }
+        if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
+                && annotations[count].value <= filterParams
+                        .getThresholdValue())
+        {
+          itemMatched = true;
+        }
+
+        if (filterParams.isFilterAlphaHelix()
+                && annotations[count].secondaryStructure == 'H')
+        {
+          itemMatched = true;
+        }
+
+        if (filterParams.isFilterBetaSheet()
+                && annotations[count].secondaryStructure == 'E')
+        {
+          itemMatched = true;
+        }
+
+        if (filterParams.isFilterTurn()
+                && annotations[count].secondaryStructure == 'S')
+        {
+          itemMatched = true;
+        }
+
+        String regexSearchString = filterParams.getRegexString();
+        if (regexSearchString != null
+                && !filterParams.getRegexSearchFields().isEmpty())
+        {
+          List<SearchableAnnotationField> fields = filterParams
+                  .getRegexSearchFields();
+          try
+          {
+            if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
+                    && annotations[count].displayCharacter
+                            .matches(regexSearchString))
+            {
+              itemMatched = true;
+            }
+          } catch (java.util.regex.PatternSyntaxException pse)
+          {
+            if (annotations[count].displayCharacter
+                    .equals(regexSearchString))
+            {
+              itemMatched = true;
+            }
+          }
+          if (fields.contains(SearchableAnnotationField.DESCRIPTION)
+                  && annotations[count].description != null
+                  && annotations[count].description
+                          .matches(regexSearchString))
+          {
+            itemMatched = true;
+          }
+        }
+
+        if (itemMatched)
+        {
+          this.addElement(count);
+        }
+      }
+      count++;
+    } while (count < annotations.length);
+    return false;
+  }
 }