JAL-2808 add attribute filter(s) to FeatureColour
[jalview.git] / src / jalview / schemes / FeatureColour.java
index ed3e02d..480522a 100644 (file)
@@ -24,9 +24,11 @@ import jalview.api.FeatureColourI;
 import jalview.datamodel.SequenceFeature;
 import jalview.util.ColorUtils;
 import jalview.util.Format;
+import jalview.util.matcher.KeyedMatcherI;
 
 import java.awt.Color;
 import java.util.StringTokenizer;
+import java.util.function.Function;
 
 /**
  * A class that wraps either a simple colour or a graduated colour
@@ -73,6 +75,11 @@ public class FeatureColour implements FeatureColourI
 
   final private float deltaBlue;
 
+  /*
+   * optional filter by attribute values
+   */
+  private KeyedMatcherI attributeFilters;
+
   /**
    * Parses a Jalview features file format colour descriptor
    * [label|][mincolour|maxcolour
@@ -125,8 +132,8 @@ public class FeatureColour implements FeatureColourI
       Color colour = ColorUtils.parseColourString(descriptor);
       if (colour == null)
       {
-        throw new IllegalArgumentException("Invalid colour descriptor: "
-                + descriptor);
+        throw new IllegalArgumentException(
+                "Invalid colour descriptor: " + descriptor);
       }
       return new FeatureColour(colour);
     }
@@ -236,8 +243,8 @@ public class FeatureColour implements FeatureColourI
         {
           if (!ttype.toLowerCase().startsWith("no"))
           {
-            System.err.println("Ignoring unrecognised threshold type : "
-                    + ttype);
+            System.err.println(
+                    "Ignoring unrecognised threshold type : " + ttype);
           }
         }
       }
@@ -256,8 +263,8 @@ public class FeatureColour implements FeatureColourI
       }
       if (gcol.hasMoreTokens())
       {
-        System.err
-                .println("Ignoring additional tokens in parameters in graduated colour specification\n");
+        System.err.println(
+                "Ignoring additional tokens in parameters in graduated colour specification\n");
         while (gcol.hasMoreTokens())
         {
           System.err.println("|" + gcol.nextToken());
@@ -359,6 +366,7 @@ public class FeatureColour implements FeatureColourI
     base = fc.base;
     range = fc.range;
     isHighToLow = fc.isHighToLow;
+    attributeFilters = fc.attributeFilters;
     setAboveThreshold(fc.isAboveThreshold());
     setBelowThreshold(fc.isBelowThreshold());
     setThreshold(fc.getThreshold());
@@ -540,10 +548,14 @@ public class FeatureColour implements FeatureColourI
   @Override
   public Color getColor(SequenceFeature feature)
   {
+    if (!matchesFilters(feature))
+    {
+      return null;
+    }
+
     if (isColourByLabel())
     {
-      return ColorUtils
-              .createColourFromName(feature.getDescription());
+      return ColorUtils.createColourFromName(feature.getDescription());
     }
 
     if (!isGraduatedColour())
@@ -553,9 +565,13 @@ public class FeatureColour implements FeatureColourI
 
     /*
      * graduated colour case, optionally with threshold
-     * (treating Float.NaN as within visible range here)
+     * Float.NaN is assigned minimum visible score colour
      */
     float scr = feature.getScore();
+    if (Float.isNaN(scr))
+    {
+      return getMinColour();
+    }
     if (isAboveThreshold() && scr <= threshold)
     {
       return null;
@@ -568,10 +584,6 @@ public class FeatureColour implements FeatureColourI
     {
       return getMaxColour();
     }
-    if (Float.isNaN(scr))
-    {
-      return getMinColour();
-    }
     float scl = (scr - base) / range;
     if (isHighToLow)
     {
@@ -590,6 +602,23 @@ public class FeatureColour implements FeatureColourI
   }
 
   /**
+   * Answers true if there are any attribute value filters defined, and the
+   * feature matches all of the filter conditions
+   * 
+   * @param feature
+   * 
+   * @return
+   */
+  boolean matchesFilters(SequenceFeature feature)
+  {
+    Function<String, String> valueProvider = key -> feature.otherDetails == null ? null
+                    : (feature.otherDetails.containsKey(key) ? feature.otherDetails
+                            .get(key).toString() : null);
+    return attributeFilters == null ? true : attributeFilters
+            .matches(valueProvider);
+  }
+
+  /**
    * Returns the maximum score of the graduated colour range
    * 
    * @return
@@ -675,4 +704,21 @@ public class FeatureColour implements FeatureColourI
     return String.format("%s\t%s", featureType, colourString);
   }
 
+  /**
+   * Adds an attribute filter
+   * 
+   * @param attName
+   * @param filter
+   */
+  @Override
+  public void setAttributeFilters(KeyedMatcherI matcher)
+  {
+    attributeFilters = matcher;
+  }
+
+  @Override
+  public KeyedMatcherI getAttributeFilters()
+  {
+    return attributeFilters;
+  }
 }