JAL-2069 add 'by attribute' options to graduated feature colour
[jalview.git] / src / jalview / datamodel / features / FeatureAttributes.java
index ed65750..3dc4f19 100644 (file)
@@ -34,6 +34,11 @@ public class FeatureAttributes
      */
     float max = 0f;
 
+    /*
+     * flag is set true if any numeric value is detected for this attribute
+     */
+    boolean hasValue = false;
+
     /**
      * Note one instance of this attribute, recording unique, non-null names,
      * and the min/max of any numerical values
@@ -43,27 +48,19 @@ public class FeatureAttributes
      */
     void addInstance(String desc, String value)
     {
-      if (desc != null)
+      addDescription(desc);
+
+      if (value != null)
       {
-        if (description == null)
-        {
-          description = new ArrayList<>();
-        }
-        if (!description.contains(desc))
+        try
         {
-          description.add(desc);
-        }
-        if (value != null)
+          float f = Float.valueOf(value);
+          min = Float.min(min, f);
+          max = Float.max(max, f);
+          hasValue = true;
+        } catch (NumberFormatException e)
         {
-          try
-          {
-            float f = Float.valueOf(value);
-            min = Float.min(min, f);
-            max = Float.max(max, f);
-          } catch (NumberFormatException e)
-          {
-            // ok, wasn't a number
-          }
+          // ok, wasn't a number, ignore for min-max purposes
         }
       }
     }
@@ -80,6 +77,27 @@ public class FeatureAttributes
       }
       return null;
     }
+
+    /**
+     * Adds the given description to the list of known descriptions (without
+     * duplication)
+     * 
+     * @param desc
+     */
+    public void addDescription(String desc)
+    {
+      if (desc != null)
+      {
+        if (description == null)
+        {
+          description = new ArrayList<>();
+        }
+        if (!description.contains(desc))
+        {
+          description.add(desc);
+        }
+      }
+    }
   }
 
   /**
@@ -188,4 +206,59 @@ public class FeatureAttributes
     }
     return desc;
   }
+
+  /**
+   * Answers the [min, max] value range of the given attribute for the given
+   * feature type, if known, else null. Attributes which only have text values
+   * would normally return null, however text values which happen to be numeric
+   * could result in a 'min-max' range.
+   * 
+   * @param featureType
+   * @param attName
+   * @return
+   */
+  public float[] getMinMax(String featureType, String attName)
+  {
+    Map<String, AttributeData> atts = attributes.get(featureType);
+    if (atts != null)
+    {
+      AttributeData attData = atts.get(attName);
+      if (attData != null && attData.hasValue)
+      {
+        return new float[] { attData.min, attData.max };
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Records the given attribute description for the given feature type
+   * 
+   * @param featureType
+   * @param attName
+   * @param description
+   */
+  public void addDescription(String featureType, String attName,
+          String description)
+  {
+    if (featureType == null || attName == null)
+    {
+      return;
+    }
+  
+    Map<String, AttributeData> atts = attributes.get(featureType);
+    if (atts == null)
+    {
+      atts = new TreeMap<String, AttributeData>(
+              String.CASE_INSENSITIVE_ORDER);
+      attributes.put(featureType, atts);
+    }
+    AttributeData attData = atts.get(attName);
+    if (attData == null)
+    {
+      attData = new AttributeData();
+      atts.put(attName, attData);
+    }
+    attData.addDescription(description);
+  }
 }