JAL-2808 add attribute filter(s) to FeatureColour
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Oct 2017 16:55:48 +0000 (16:55 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Oct 2017 16:55:48 +0000 (16:55 +0000)
src/jalview/api/FeatureColourI.java
src/jalview/schemes/FeatureColour.java

index 0ded079..9644831 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.api;
 
 import jalview.datamodel.SequenceFeature;
+import jalview.util.matcher.KeyedMatcherI;
 
 import java.awt.Color;
 
@@ -169,4 +170,20 @@ public interface FeatureColourI
    * @return
    */
   String toJalviewFormat(String featureType);
+
+  /**
+   * Sets the attribute filter conditions, or removes them if the argument is
+   * null
+   * 
+   * @param filter
+   */
+  public void setAttributeFilters(KeyedMatcherI filter);
+
+  /**
+   * Answers the attribute value filters for the colour scheme, or null if no
+   * filters are set
+   * 
+   * @return
+   */
+  public KeyedMatcherI getAttributeFilters();
 }
index 54d1c6c..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
@@ -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,6 +548,11 @@ public class FeatureColour implements FeatureColourI
   @Override
   public Color getColor(SequenceFeature feature)
   {
+    if (!matchesFilters(feature))
+    {
+      return null;
+    }
+
     if (isColourByLabel())
     {
       return ColorUtils.createColourFromName(feature.getDescription());
@@ -589,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
@@ -674,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;
+  }
 }