JAL-1551 spotless
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 4cdd7af..3c2d166 100644 (file)
  */
 package jalview.datamodel;
 
-import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
-import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
-
 import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.Collections;
 import java.util.List;
+import java.util.regex.PatternSyntaxException;
+
+import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
+import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
 
 /**
  * Data class holding the selected columns and hidden column ranges for a view.
@@ -59,7 +60,7 @@ public class ColumnSelection
      */
     IntList()
     {
-      order = new ArrayList<Integer>();
+      order = new ArrayList<>();
       _uorder = Collections.unmodifiableList(order);
       selected = new BitSet();
     }
@@ -105,7 +106,7 @@ public class ColumnSelection
     void remove(int col)
     {
 
-      Integer colInt = new Integer(col);
+      Integer colInt = Integer.valueOf(col);
 
       if (selected.get(col))
       {
@@ -203,7 +204,7 @@ public class ColumnSelection
           // clear shifted bits and update List of selected columns
           selected.clear(temp);
           mask.set(temp - change);
-          order.set(i, new Integer(temp - change));
+          order.set(i, Integer.valueOf(temp - change));
         }
       }
       // lastly update the bitfield all at once
@@ -230,7 +231,7 @@ public class ColumnSelection
      */
     List<int[]> getRanges()
     {
-      List<int[]> rlist = new ArrayList<int[]>();
+      List<int[]> rlist = new ArrayList<>();
       if (selected.isEmpty())
       {
         return rlist;
@@ -263,7 +264,7 @@ public class ColumnSelection
     }
   }
 
-  IntList selection = new IntList();
+  private IntList selection = new IntList();
 
   /**
    * Add a column to the selection
@@ -308,7 +309,7 @@ public class ColumnSelection
     Integer colInt;
     for (int i = start; i < end; i++)
     {
-      colInt = new Integer(i);
+      colInt = Integer.valueOf(i);
       if (selection.contains(colInt))
       {
         selection.remove(colInt);
@@ -353,6 +354,22 @@ public class ColumnSelection
   }
 
   /**
+   * 
+   */
+  public boolean intersects(int from, int to)
+  {
+    // TODO: do this in a more efficient bitwise way
+    for (int f = from; f <= to; f++)
+    {
+      if (selection.isSelected(f))
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  /**
    * Answers true if no columns are selected, else false
    */
   public boolean isEmpty()
@@ -401,7 +418,6 @@ public class ColumnSelection
 
   }
 
-
   /**
    * Hides the specified column and any adjacent selected columns
    * 
@@ -444,10 +460,6 @@ public class ColumnSelection
     hidden.hideColumns(min, max);
   }
 
-
-
-
-
   /**
    * Copy constructor
    * 
@@ -468,11 +480,6 @@ public class ColumnSelection
   {
   }
 
-
-
-
-
-
   /**
    * Invert the column selection from first to end-1. leaves hiddenColumns
    * untouched (and unselected)
@@ -534,7 +541,6 @@ public class ColumnSelection
     }
   }
 
-
   /**
    * 
    * @return true if there are columns marked
@@ -544,92 +550,150 @@ public class ColumnSelection
     return (selection != null && selection.size() > 0);
   }
 
-
-
-  public boolean filterAnnotations(Annotation[] annotations,
+  /**
+   * Selects columns where the given annotation matches the provided filter
+   * condition(s). Any existing column selections are first cleared. Answers the
+   * number of columns added.
+   * 
+   * @param annotations
+   * @param filterParams
+   * @return
+   */
+  public int filterAnnotations(AlignmentAnnotation ann_row,
           AnnotationFilterParameter filterParams)
   {
+    Annotation[] annotations = ann_row.annotations;
     // JBPNote - this method needs to be refactored to become independent of
     // viewmodel package
     this.clear();
-    int count = 0;
-    do
+
+    if (ann_row.graph == AlignmentAnnotation.CONTACT_MAP && (filterParams
+            .getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
+            || filterParams
+                    .getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD))
     {
-      if (annotations[count] != null)
+      float tVal = filterParams.getThresholdValue();
+      if (ann_row.sequenceRef != null)
       {
+        // TODO - get ContactList from AlignmentView for non-seq-ref associatd
+        for (int column = 0; column < annotations.length; column++)
+        {
+          if (ann_row.annotations[column] == null)
+          {
+            continue;
+          }
 
-        boolean itemMatched = false;
+          int cpos = ann_row.sequenceRef.findPosition(column) - 1;
+          ContactListI clist = ann_row.sequenceRef
+                  .getContactListFor(ann_row, cpos);
+          for (int row = column + 8, rowEnd = clist
+                  .getContactHeight(); row < rowEnd; row++)
+          {
+            if (filterParams
+                    .getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
+                            ? (clist.getContactAt(row) > tVal)
+                            : (clist.getContactAt(row) < tVal))
+            {
+              addElement(column);
+              break;
+              // int column_forrowpos = ann_row.sequenceRef.findIndex(row + 1);
+              // addElement(column_forrowpos);
+            }
+          }
+        }
+      }
+      return selection.size();
+    }
 
-        if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
-                && annotations[count].value >= filterParams
-                        .getThresholdValue())
+    int addedCount = 0;
+    int column = 0;
+    do
+    {
+      Annotation ann = annotations[column];
+      if (ann != null)
+      {
+        float value = ann.value;
+        boolean matched = false;
+
+        /*
+         * filter may have multiple conditions - 
+         * these are or'd until a match is found
+         */
+        if (filterParams
+                .getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
+                && value > filterParams.getThresholdValue())
         {
-          itemMatched = true;
+          matched = true;
         }
-        if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
-                && annotations[count].value <= filterParams
-                        .getThresholdValue())
+
+        if (!matched && filterParams
+                .getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
+                && value < filterParams.getThresholdValue())
         {
-          itemMatched = true;
+          matched = true;
         }
 
-        if (filterParams.isFilterAlphaHelix()
-                && annotations[count].secondaryStructure == 'H')
+        if (!matched && filterParams.isFilterAlphaHelix()
+                && ann.secondaryStructure == 'H')
         {
-          itemMatched = true;
+          matched = true;
         }
 
-        if (filterParams.isFilterBetaSheet()
-                && annotations[count].secondaryStructure == 'E')
+        if (!matched && filterParams.isFilterBetaSheet()
+                && ann.secondaryStructure == 'E')
         {
-          itemMatched = true;
+          matched = true;
         }
 
-        if (filterParams.isFilterTurn()
-                && annotations[count].secondaryStructure == 'S')
+        if (!matched && filterParams.isFilterTurn()
+                && ann.secondaryStructure == 'S')
         {
-          itemMatched = true;
+          matched = true;
         }
 
         String regexSearchString = filterParams.getRegexString();
-        if (regexSearchString != null
-                && !filterParams.getRegexSearchFields().isEmpty())
+        if (!matched && regexSearchString != null)
         {
           List<SearchableAnnotationField> fields = filterParams
                   .getRegexSearchFields();
-          try
+          for (SearchableAnnotationField field : fields)
           {
-            if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
-                    && annotations[count].displayCharacter
-                            .matches(regexSearchString))
+            String compareTo = field == SearchableAnnotationField.DISPLAY_STRING
+                    ? ann.displayCharacter // match 'Label'
+                    : ann.description; // and/or 'Description'
+            if (compareTo != null)
             {
-              itemMatched = true;
-            }
-          } catch (java.util.regex.PatternSyntaxException pse)
-          {
-            if (annotations[count].displayCharacter
-                    .equals(regexSearchString))
-            {
-              itemMatched = true;
+              try
+              {
+                if (compareTo.matches(regexSearchString))
+                {
+                  matched = true;
+                }
+              } catch (PatternSyntaxException pse)
+              {
+                if (compareTo.equals(regexSearchString))
+                {
+                  matched = true;
+                }
+              }
+              if (matched)
+              {
+                break;
+              }
             }
           }
-          if (fields.contains(SearchableAnnotationField.DESCRIPTION)
-                  && annotations[count].description != null
-                  && annotations[count].description
-                          .matches(regexSearchString))
-          {
-            itemMatched = true;
-          }
         }
 
-        if (itemMatched)
+        if (matched)
         {
-          this.addElement(count);
+          this.addElement(column);
+          addedCount++;
         }
       }
-      count++;
-    } while (count < annotations.length);
-    return false;
+      column++;
+    } while (column < annotations.length);
+
+    return addedCount;
   }
 
   /**
@@ -691,8 +755,8 @@ public class ColumnSelection
    * 
    * @return
    */
-  public boolean markColumns(BitSet markedColumns, int startCol,
-          int endCol, boolean invert, boolean extendCurrent, boolean toggle)
+  public boolean markColumns(BitSet markedColumns, int startCol, int endCol,
+          boolean invert, boolean extendCurrent, boolean toggle)
   {
     boolean changed = false;
     if (!extendCurrent && !toggle)