JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / schemes / FeatureColour.java
index 51e7645..f1bade1 100644 (file)
  */
 package jalview.schemes;
 
+import java.util.Locale;
+
 import jalview.api.FeatureColourI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.features.FeatureMatcher;
 import jalview.util.ColorUtils;
 import jalview.util.Format;
+import jalview.util.MessageManager;
 
 import java.awt.Color;
 import java.util.StringTokenizer;
@@ -45,11 +48,18 @@ import java.util.StringTokenizer;
  * <li>the range may be the full value range, or may be limited by the threshold
  * value</li>
  * </ul>
- * <li>colour by (text) value of a named attribute</li> <li>graduated colour by
- * (numeric) value of a named attribute</li> </ul>
+ * <li>colour by (text) value of a named attribute</li>
+ * <li>graduated colour by (numeric) value of a named attribute</li>
+ * </ul>
  */
 public class FeatureColour implements FeatureColourI
 {
+  private static final String I18N_LABEL = MessageManager
+          .getString("label.label");
+
+  private static final String I18N_SCORE = MessageManager
+          .getString("label.score");
+
   private static final String ABSOLUTE = "abso";
 
   private static final String ABOVE = "above";
@@ -187,19 +197,19 @@ public class FeatureColour implements FeatureColourI
               "Expected either 'label' or a colour specification in the line: "
                       + descriptor);
     }
-    if (nextToken.toLowerCase().startsWith(LABEL))
+    if (nextToken.toLowerCase(Locale.ROOT).startsWith(LABEL))
     {
       byLabel = true;
       // get the token after the next delimiter:
       mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
       mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
     }
-    else if (nextToken.toLowerCase().startsWith(SCORE))
+    else if (nextToken.toLowerCase(Locale.ROOT).startsWith(SCORE))
     {
       mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
       mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
     }
-    else if (nextToken.toLowerCase().startsWith(ATTRIBUTE))
+    else if (nextToken.toLowerCase(Locale.ROOT).startsWith(ATTRIBUTE))
     {
       byAttribute = true;
       attName = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
@@ -297,7 +307,7 @@ public class FeatureColour implements FeatureColourI
       }
 
       gcol.nextToken(); // skip next '|'
-      if (tok.toLowerCase().startsWith(ABSOLUTE))
+      if (tok.toLowerCase(Locale.ROOT).startsWith(ABSOLUTE))
       {
         minval = gcol.nextToken();
         gcol.nextToken(); // skip next '|'
@@ -316,7 +326,7 @@ public class FeatureColour implements FeatureColourI
       {
         if (minval.length() > 0)
         {
-          min = new Float(minval).floatValue();
+          min = Float.valueOf(minval).floatValue();
         }
       } catch (Exception e)
       {
@@ -328,7 +338,7 @@ public class FeatureColour implements FeatureColourI
       {
         if (maxval.length() > 0)
         {
-          max = new Float(maxval).floatValue();
+          max = Float.valueOf(maxval).floatValue();
         }
       } catch (Exception e)
       {
@@ -358,8 +368,8 @@ public class FeatureColour implements FeatureColourI
       Color maxColour = ColorUtils.parseColourString(maxcol);
       Color noColour = noValueColour.equals(NO_VALUE_MAX) ? maxColour
               : (noValueColour.equals(NO_VALUE_NONE) ? null : minColour);
-      featureColour = new FeatureColour(minColour, maxColour, noColour, min,
-              max);
+      featureColour = new FeatureColour(maxColour, minColour, maxColour,
+              noColour, min, max);
       featureColour.setColourByLabel(minColour == null);
       featureColour.setAutoScaled(autoScaled);
       if (byAttribute)
@@ -373,17 +383,17 @@ public class FeatureColour implements FeatureColourI
       {
         // threshold type and possibly a threshold value
         ttype = gcol.nextToken();
-        if (ttype.toLowerCase().startsWith(BELOW))
+        if (ttype.toLowerCase(Locale.ROOT).startsWith(BELOW))
         {
           featureColour.setBelowThreshold(true);
         }
-        else if (ttype.toLowerCase().startsWith(ABOVE))
+        else if (ttype.toLowerCase(Locale.ROOT).startsWith(ABOVE))
         {
           featureColour.setAboveThreshold(true);
         }
         else
         {
-          if (!ttype.toLowerCase().startsWith("no"))
+          if (!ttype.toLowerCase(Locale.ROOT).startsWith("no"))
           {
             System.err.println(
                     "Ignoring unrecognised threshold type : " + ttype);
@@ -396,7 +406,7 @@ public class FeatureColour implements FeatureColourI
         {
           gcol.nextToken();
           tval = gcol.nextToken();
-          featureColour.setThreshold(new Float(tval).floatValue());
+          featureColour.setThreshold(Float.valueOf(tval).floatValue());
         } catch (Exception e)
         {
           System.err.println("Couldn't parse threshold value as a float: ("
@@ -429,36 +439,25 @@ public class FeatureColour implements FeatureColourI
   }
 
   /**
-   * Constructor given a simple colour
+   * Constructor given a simple colour. This also 'primes' a graduated colour
+   * range, where the maximum colour is the given simple colour, and the minimum
+   * colour a paler shade of it. This is for convenience when switching from a
+   * simple colour to a graduated colour scheme.
    * 
    * @param c
    */
   public FeatureColour(Color c)
   {
-    minColour = Color.WHITE;
-    maxColour = Color.BLACK;
-    noColour = DEFAULT_NO_COLOUR;
-    minRed = 0f;
-    minGreen = 0f;
-    minBlue = 0f;
-    deltaRed = 0f;
-    deltaGreen = 0f;
-    deltaBlue = 0f;
-    colour = c;
-  }
+    /*
+     * set max colour to the simple colour, min colour to a paler shade of it
+     */
+    this(c, c == null ? Color.white : ColorUtils.bleachColour(c, 0.9f),
+            c == null ? Color.black : c, DEFAULT_NO_COLOUR, 0, 0);
 
-  /**
-   * Constructor given a colour range and a score range, defaulting 'no value
-   * colour' to be the same as minimum colour
-   * 
-   * @param low
-   * @param high
-   * @param min
-   * @param max
-   */
-  public FeatureColour(Color low, Color high, float min, float max)
-  {
-    this(low, high, low, min, max);
+    /*
+     * but enforce simple colour for now!
+     */
+    setGraduatedColour(false);
   }
 
   /**
@@ -491,29 +490,23 @@ public class FeatureColour implements FeatureColourI
   }
 
   /**
-   * Copy constructor with new min/max ranges
-   * 
-   * @param fc
-   * @param min
-   * @param max
-   */
-  public FeatureColour(FeatureColour fc, float min, float max)
-  {
-    this(fc);
-    updateBounds(min, max);
-  }
-
-  /**
-   * Constructor for a graduated colour
+   * Constructor that sets both simple and graduated colour values. This allows
+   * alternative colour schemes to be 'preserved' while switching between them
+   * to explore their effects on the visualisation.
+   * <p>
+   * This sets the colour scheme to 'graduated' by default. Override this if
+   * wanted by calling <code>setGraduatedColour(false)</code> for a simple
+   * colour, or <code>setColourByLabel(true)</code> for colour by label.
    * 
+   * @param myColour
    * @param low
    * @param high
    * @param noValueColour
    * @param min
    * @param max
    */
-  public FeatureColour(Color low, Color high, Color noValueColour,
-          float min, float max)
+  public FeatureColour(Color myColour, Color low, Color high,
+          Color noValueColour, float min, float max)
   {
     if (low == null)
     {
@@ -523,10 +516,10 @@ public class FeatureColour implements FeatureColourI
     {
       high = Color.black;
     }
-    graduatedColour = true;
-    colour = null;
+    colour = myColour;
     minColour = low;
     maxColour = high;
+    setGraduatedColour(true);
     noColour = noValueColour;
     threshold = Float.NaN;
     isHighToLow = min >= max;
@@ -558,7 +551,7 @@ public class FeatureColour implements FeatureColourI
    * Sets the 'graduated colour' flag. If true, also sets 'colour by label' to
    * false.
    */
-  void setGraduatedColour(boolean b)
+  public void setGraduatedColour(boolean b)
   {
     graduatedColour = b;
     if (b)
@@ -689,9 +682,12 @@ public class FeatureColour implements FeatureColourI
 
   /**
    * Returns the colour for the given instance of the feature. This may be a
-   * simple colour, a colour generated from the feature description (if
-   * isColourByLabel()), or a colour derived from the feature score (if
-   * isGraduatedColour()).
+   * simple colour, a colour generated from the feature description or other
+   * attribute (if isColourByLabel()), or a colour derived from the feature
+   * score or other attribute (if isGraduatedColour()).
+   * <p>
+   * Answers null if feature score (or attribute) value lies outside a
+   * configured threshold.
    * 
    * @param feature
    * @return
@@ -703,8 +699,8 @@ public class FeatureColour implements FeatureColourI
     {
       String label = attributeName == null ? feature.getDescription()
               : feature.getValueAsString(attributeName);
-      return label == null ? noColour : ColorUtils
-              .createColourFromName(label);
+      return label == null ? noColour
+              : ColorUtils.createColourFromName(label);
     }
 
     if (!isGraduatedColour())
@@ -830,9 +826,20 @@ public class FeatureColour implements FeatureColourI
         sb.append(BAR).append(Format.getHexString(getMinColour()))
                 .append(BAR);
         sb.append(Format.getHexString(getMaxColour())).append(BAR);
-        String noValue = minColour.equals(noColour) ? NO_VALUE_MIN
-                : (maxColour.equals(noColour) ? NO_VALUE_MAX
-                        : NO_VALUE_NONE);
+
+        /*
+         * 'no value' colour should be null, min or max colour;
+         * if none of these, coerce to minColour
+         */
+        String noValue = NO_VALUE_MIN;
+        if (maxColour.equals(noColour))
+        {
+          noValue = NO_VALUE_MAX;
+        }
+        if (noColour == null)
+        {
+          noValue = NO_VALUE_NONE;
+        }
         sb.append(noValue).append(BAR);
         if (!isAutoScaled())
         {
@@ -890,4 +897,78 @@ public class FeatureColour implements FeatureColourI
     attributeName = name;
   }
 
+  @Override
+  public boolean isOutwithThreshold(SequenceFeature feature)
+  {
+    if (!isGraduatedColour())
+    {
+      return false;
+    }
+    float scr = feature.getScore();
+    if (attributeName != null)
+    {
+      try
+      {
+        String attVal = feature.getValueAsString(attributeName);
+        scr = Float.valueOf(attVal);
+      } catch (Throwable e)
+      {
+        scr = Float.NaN;
+      }
+    }
+    if (Float.isNaN(scr))
+    {
+      return false;
+    }
+
+    return ((isAboveThreshold() && scr <= threshold)
+            || (isBelowThreshold() && scr >= threshold));
+  }
+
+  @Override
+  public String getDescription()
+  {
+    if (isSimpleColour())
+    {
+      return "r=" + colour.getRed() + ",g=" + colour.getGreen() + ",b="
+              + colour.getBlue();
+    }
+    StringBuilder tt = new StringBuilder();
+    String by = null;
+
+    if (getAttributeName() != null)
+    {
+      by = FeatureMatcher.toAttributeDisplayName(getAttributeName());
+    }
+    else if (isColourByLabel())
+    {
+      by = I18N_LABEL;
+    }
+    else
+    {
+      by = I18N_SCORE;
+    }
+    tt.append(MessageManager.formatMessage("action.by_title_param", by));
+
+    /*
+     * add threshold if any
+     */
+    if (isAboveThreshold() || isBelowThreshold())
+    {
+      tt.append(" (");
+      if (isColourByLabel())
+      {
+        /*
+         * Jalview features file supports the combination of 
+         * colour by label or attribute text with score threshold
+         */
+        tt.append(I18N_SCORE).append(" ");
+      }
+      tt.append(isAboveThreshold() ? "> " : "< ");
+      tt.append(getThreshold()).append(")");
+    }
+
+    return tt.toString();
+  }
+
 }