*/
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;
*/
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";
+
+ private static final String BELOW = "below";
+
+ /*
+ * constants used to read or write a Jalview Features file
+ */
+ private static final String LABEL = "label";
+
+ private static final String SCORE = "score";
+
+ private static final String ATTRIBUTE = "attribute";
+
+ private static final String NO_VALUE_MIN = "noValueMin";
+
+ private static final String NO_VALUE_MAX = "noValueMax";
+
+ private static final String NO_VALUE_NONE = "noValueNone";
+
static final Color DEFAULT_NO_COLOUR = null;
private static final String BAR = "|";
private boolean aboveThreshold;
- private boolean thresholdIsMinOrMax;
-
private boolean isHighToLow;
private boolean autoScaled;
/**
* Parses a Jalview features file format colour descriptor
- * [label|][mincolour|maxcolour
- * |[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue] Examples:
+ * <p>
+ * <code>
+ * [label|score|[attribute|attributeName]|][mincolour|maxcolour|
+ * [absolute|]minvalue|maxvalue|[noValueOption|]thresholdtype|thresholdvalue]</code>
+ * <p>
+ * 'Score' is optional (default) for a graduated colour. An attribute with
+ * sub-attribute should be written as (for example) CSQ:Consequence.
+ * noValueOption is one of <code>noValueMin, noValueMax, noValueNone</code>
+ * with default noValueMin.
+ * <p>
+ * Examples:
* <ul>
* <li>red</li>
* <li>a28bbb</li>
* <li>25,125,213</li>
* <li>label</li>
+ * <li>attribute|CSQ:PolyPhen</li>
* <li>label|||0.0|0.0|above|12.5</li>
* <li>label|||0.0|0.0|below|12.5</li>
* <li>red|green|12.0|26.0|none</li>
+ * <li>score|red|green|12.0|26.0|none</li>
+ * <li>attribute|AF|red|green|12.0|26.0|none</li>
+ * <li>attribute|AF|red|green|noValueNone|12.0|26.0|none</li>
* <li>a28bbb|3eb555|12.0|26.0|above|12.5</li>
* <li>a28bbb|3eb555|abso|12.0|26.0|below|12.5</li>
* </ul>
* @throws IllegalArgumentException
* if not parseable
*/
- public static FeatureColour parseJalviewFeatureColour(String descriptor)
+ public static FeatureColourI parseJalviewFeatureColour(String descriptor)
{
- StringTokenizer gcol = new StringTokenizer(descriptor, "|", true);
+ StringTokenizer gcol = new StringTokenizer(descriptor, BAR, true);
float min = Float.MIN_VALUE;
float max = Float.MAX_VALUE;
- boolean labelColour = false;
+ boolean byLabel = false;
+ boolean byAttribute = false;
+ String attName = null;
+ String mincol = null;
+ String maxcol = null;
- String mincol = gcol.nextToken();
- if (mincol == "|")
+ /*
+ * first token should be 'label', or 'score', or an
+ * attribute name, or simple colour, or minimum colour
+ */
+ String nextToken = gcol.nextToken();
+ if (nextToken == BAR)
{
throw new IllegalArgumentException(
"Expected either 'label' or a colour specification in the line: "
+ descriptor);
}
- String maxcol = null;
- if (mincol.toLowerCase().indexOf("label") == 0)
+ if (nextToken.toLowerCase(Locale.ROOT).startsWith(LABEL))
{
- labelColour = true;
+ byLabel = true;
+ // get the token after the next delimiter:
mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
- // skip '|'
mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
}
+ else if (nextToken.toLowerCase(Locale.ROOT).startsWith(SCORE))
+ {
+ mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ }
+ else if (nextToken.toLowerCase(Locale.ROOT).startsWith(ATTRIBUTE))
+ {
+ byAttribute = true;
+ attName = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ attName = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
+ }
+ else
+ {
+ mincol = nextToken;
+ }
- if (!labelColour && !gcol.hasMoreTokens())
+ /*
+ * if only one token, it can validly be label, attributeName,
+ * or a plain colour value
+ */
+ if (!gcol.hasMoreTokens())
{
- /*
- * only a simple colour specification - parse it
- */
+ if (byLabel || byAttribute)
+ {
+ FeatureColourI fc = new FeatureColour();
+ fc.setColourByLabel(true);
+ if (byAttribute)
+ {
+ fc.setAttributeName(
+ FeatureMatcher.fromAttributeDisplayName(attName));
+ }
+ return fc;
+ }
+
Color colour = ColorUtils.parseColourString(descriptor);
if (colour == null)
{
}
/*
+ * continue parsing for min/max/no colour (if graduated)
+ * and for threshold (colour by text or graduated)
+ */
+
+ /*
* autoScaled == true: colours range over actual score range
* autoScaled == false ('abso'): colours range over min/max range
*/
boolean autoScaled = true;
String tok = null, minval, maxval;
+ String noValueColour = NO_VALUE_MIN;
+
if (mincol != null)
{
// at least four more tokens
- if (mincol.equals("|"))
+ if (mincol.equals(BAR))
{
- mincol = "";
+ mincol = null;
}
else
{
gcol.nextToken(); // skip next '|'
}
maxcol = gcol.nextToken();
- if (maxcol.equals("|"))
+ if (maxcol.equals(BAR))
{
- maxcol = "";
+ maxcol = null;
}
else
{
gcol.nextToken(); // skip next '|'
}
tok = gcol.nextToken();
+
+ /*
+ * check for specifier for colour for no attribute value
+ * (new in 2.11, defaults to minColour if not specified)
+ */
+ if (tok.equalsIgnoreCase(NO_VALUE_MIN))
+ {
+ tok = gcol.nextToken();
+ tok = gcol.nextToken();
+ }
+ else if (tok.equalsIgnoreCase(NO_VALUE_MAX))
+ {
+ noValueColour = NO_VALUE_MAX;
+ tok = gcol.nextToken();
+ tok = gcol.nextToken();
+ }
+ else if (tok.equalsIgnoreCase(NO_VALUE_NONE))
+ {
+ noValueColour = NO_VALUE_NONE;
+ tok = gcol.nextToken();
+ tok = gcol.nextToken();
+ }
+
gcol.nextToken(); // skip next '|'
- if (tok.toLowerCase().startsWith("abso"))
+ if (tok.toLowerCase(Locale.ROOT).startsWith(ABSOLUTE))
{
minval = gcol.nextToken();
gcol.nextToken(); // skip next '|'
{
if (minval.length() > 0)
{
- min = new Float(minval).floatValue();
+ min = Float.valueOf(minval).floatValue();
}
} catch (Exception e)
{
throw new IllegalArgumentException(
- "Couldn't parse the minimum value for graduated colour ("
- + descriptor + ")");
+ "Couldn't parse the minimum value for graduated colour ('"
+ + minval + "')");
}
try
{
if (maxval.length() > 0)
{
- max = new Float(maxval).floatValue();
+ max = Float.valueOf(maxval).floatValue();
}
} catch (Exception e)
{
}
else
{
- // add in some dummy min/max colours for the label-only
- // colourscheme.
- mincol = "FFFFFF";
- maxcol = "000000";
+ /*
+ * dummy min/max colours for colour by text
+ * (label or attribute value)
+ */
+ mincol = "white";
+ maxcol = "black";
+ byLabel = true;
}
/*
- * construct the FeatureColour
+ * construct the FeatureColour!
*/
FeatureColour featureColour;
try
{
Color minColour = ColorUtils.parseColourString(mincol);
Color maxColour = ColorUtils.parseColourString(maxcol);
- featureColour = new FeatureColour(minColour, maxColour, min, max);
- featureColour.setColourByLabel(labelColour);
+ Color noColour = noValueColour.equals(NO_VALUE_MAX) ? maxColour
+ : (noValueColour.equals(NO_VALUE_NONE) ? null : minColour);
+ featureColour = new FeatureColour(maxColour, minColour, maxColour,
+ noColour, min, max);
+ featureColour.setColourByLabel(minColour == null);
featureColour.setAutoScaled(autoScaled);
+ if (byAttribute)
+ {
+ featureColour.setAttributeName(
+ FeatureMatcher.fromAttributeDisplayName(attName));
+ }
// add in any additional parameters
String ttype = null, tval = null;
if (gcol.hasMoreTokens())
{
// 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);
{
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: ("
"Ignoring additional tokens in parameters in graduated colour specification\n");
while (gcol.hasMoreTokens())
{
- System.err.println("|" + gcol.nextToken());
+ System.err.println(BAR + gcol.nextToken());
}
System.err.println("\n");
}
}
/**
- * 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);
}
/**
}
/**
- * Copy constructor with new min/max ranges
+ * 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 fc
+ * @param myColour
+ * @param low
+ * @param high
+ * @param noValueColour
* @param min
* @param max
*/
- public FeatureColour(FeatureColour fc, float min, float max)
- {
- this(fc);
- updateBounds(min, 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)
{
{
high = Color.black;
}
- graduatedColour = true;
- colour = null;
+ colour = myColour;
minColour = low;
maxColour = high;
+ setGraduatedColour(true);
noColour = noValueColour;
threshold = Float.NaN;
isHighToLow = min >= max;
* 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)
}
@Override
- public boolean isThresholdMinMax()
- {
- return thresholdIsMinOrMax;
- }
-
- @Override
- public void setThresholdMinMax(boolean b)
- {
- thresholdIsMinOrMax = b;
- }
-
- @Override
public float getThreshold()
{
return threshold;
/**
* 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
else
{
StringBuilder sb = new StringBuilder(32);
- if (isColourByLabel())
+ if (isColourByAttribute())
{
- sb.append("label");
- if (hasThreshold())
- {
- sb.append(BAR).append(BAR).append(BAR);
- }
+ sb.append(ATTRIBUTE).append(BAR);
+ sb.append(
+ FeatureMatcher.toAttributeDisplayName(getAttributeName()));
+ }
+ else if (isColourByLabel())
+ {
+ sb.append(LABEL);
+ }
+ else
+ {
+ sb.append(SCORE);
}
if (isGraduatedColour())
{
- sb.append(Format.getHexString(getMinColour())).append(BAR);
+ sb.append(BAR).append(Format.getHexString(getMinColour()))
+ .append(BAR);
sb.append(Format.getHexString(getMaxColour())).append(BAR);
+
+ /*
+ * '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())
{
- sb.append("abso").append(BAR);
+ sb.append(ABSOLUTE).append(BAR);
+ }
+ }
+ else
+ {
+ /*
+ * colour by text with score threshold: empty fields for
+ * minColour and maxColour (not used)
+ */
+ if (hasThreshold())
+ {
+ sb.append(BAR).append(BAR).append(BAR);
}
}
if (hasThreshold() || isGraduatedColour())
sb.append(getMax()).append(BAR);
if (isBelowThreshold())
{
- sb.append("below").append(BAR).append(getThreshold());
+ sb.append(BELOW).append(BAR).append(getThreshold());
}
else if (isAboveThreshold())
{
- sb.append("above").append(BAR).append(getThreshold());
+ sb.append(ABOVE).append(BAR).append(getThreshold());
}
else
{
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();
+ }
+
}