2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.schemes;
23 import java.util.Locale;
25 import jalview.api.FeatureColourI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.features.FeatureMatcher;
28 import jalview.util.ColorUtils;
29 import jalview.util.Format;
30 import jalview.util.MessageManager;
32 import java.awt.Color;
33 import java.util.StringTokenizer;
36 * A class that represents a colour scheme for a feature type. Options supported
39 * <li>a simple colour e.g. Red</li>
40 * <li>colour by label - a colour is generated from the feature description</li>
41 * <li>graduated colour by feature score</li>
43 * <li>minimum and maximum score range must be provided</li>
44 * <li>minimum and maximum value colours should be specified</li>
45 * <li>a colour for 'no value' may optionally be provided</li>
46 * <li>colours for intermediate scores are interpolated RGB values</li>
47 * <li>there is an optional threshold above/below which to colour values</li>
48 * <li>the range may be the full value range, or may be limited by the threshold
51 * <li>colour by (text) value of a named attribute</li>
52 * <li>graduated colour by (numeric) value of a named attribute</li>
55 public class FeatureColour implements FeatureColourI
57 private static final String I18N_LABEL = MessageManager
58 .getString("label.label");
60 private static final String I18N_SCORE = MessageManager
61 .getString("label.score");
63 private static final String ABSOLUTE = "abso";
65 private static final String ABOVE = "above";
67 private static final String BELOW = "below";
70 * constants used to read or write a Jalview Features file
72 private static final String LABEL = "label";
74 private static final String SCORE = "score";
76 private static final String ATTRIBUTE = "attribute";
78 private static final String NO_VALUE_MIN = "noValueMin";
80 private static final String NO_VALUE_MAX = "noValueMax";
82 private static final String NO_VALUE_NONE = "noValueNone";
84 static final Color DEFAULT_NO_COLOUR = null;
86 private static final String BAR = "|";
88 final private Color colour;
90 final private Color minColour;
92 final private Color maxColour;
95 * colour to use for colour by attribute when the
96 * attribute value is absent
98 final private Color noColour;
101 * if true, then colour has a gradient based on a numerical
102 * range (either feature score, or an attribute value)
104 private boolean graduatedColour;
107 * if true, colour values are generated from a text string,
108 * either feature description, or an attribute value
110 private boolean colourByLabel;
113 * if not null, the value of [attribute, [sub-attribute] ...]
114 * is used for colourByLabel or graduatedColour
116 private String[] attributeName;
118 private float threshold;
124 private boolean belowThreshold;
126 private boolean aboveThreshold;
128 private boolean isHighToLow;
130 private boolean autoScaled;
132 final private float minRed;
134 final private float minGreen;
136 final private float minBlue;
138 final private float deltaRed;
140 final private float deltaGreen;
142 final private float deltaBlue;
145 * Parses a Jalview features file format colour descriptor
148 * [label|score|[attribute|attributeName]|][mincolour|maxcolour|
149 * [absolute|]minvalue|maxvalue|[noValueOption|]thresholdtype|thresholdvalue]</code>
151 * 'Score' is optional (default) for a graduated colour. An attribute with
152 * sub-attribute should be written as (for example) CSQ:Consequence.
153 * noValueOption is one of <code>noValueMin, noValueMax, noValueNone</code>
154 * with default noValueMin.
160 * <li>25,125,213</li>
162 * <li>attribute|CSQ:PolyPhen</li>
163 * <li>label|||0.0|0.0|above|12.5</li>
164 * <li>label|||0.0|0.0|below|12.5</li>
165 * <li>red|green|12.0|26.0|none</li>
166 * <li>score|red|green|12.0|26.0|none</li>
167 * <li>attribute|AF|red|green|12.0|26.0|none</li>
168 * <li>attribute|AF|red|green|noValueNone|12.0|26.0|none</li>
169 * <li>a28bbb|3eb555|12.0|26.0|above|12.5</li>
170 * <li>a28bbb|3eb555|abso|12.0|26.0|below|12.5</li>
175 * @throws IllegalArgumentException
178 public static FeatureColourI parseJalviewFeatureColour(String descriptor)
180 StringTokenizer gcol = new StringTokenizer(descriptor, BAR, true);
181 float min = Float.MIN_VALUE;
182 float max = Float.MAX_VALUE;
183 boolean byLabel = false;
184 boolean byAttribute = false;
185 String attName = null;
186 String mincol = null;
187 String maxcol = null;
190 * first token should be 'label', or 'score', or an
191 * attribute name, or simple colour, or minimum colour
193 String nextToken = gcol.nextToken();
194 if (nextToken == BAR)
196 throw new IllegalArgumentException(
197 "Expected either 'label' or a colour specification in the line: "
200 if (nextToken.toLowerCase(Locale.ROOT).startsWith(LABEL))
203 // get the token after the next delimiter:
204 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
205 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
207 else if (nextToken.toLowerCase(Locale.ROOT).startsWith(SCORE))
209 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
210 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
212 else if (nextToken.toLowerCase(Locale.ROOT).startsWith(ATTRIBUTE))
215 attName = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
216 attName = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
217 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
218 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
226 * if only one token, it can validly be label, attributeName,
227 * or a plain colour value
229 if (!gcol.hasMoreTokens())
231 if (byLabel || byAttribute)
233 FeatureColourI fc = new FeatureColour();
234 fc.setColourByLabel(true);
238 FeatureMatcher.fromAttributeDisplayName(attName));
243 Color colour = ColorUtils.parseColourString(descriptor);
246 throw new IllegalArgumentException(
247 "Invalid colour descriptor: " + descriptor);
249 return new FeatureColour(colour);
253 * continue parsing for min/max/no colour (if graduated)
254 * and for threshold (colour by text or graduated)
258 * autoScaled == true: colours range over actual score range
259 * autoScaled == false ('abso'): colours range over min/max range
261 boolean autoScaled = true;
262 String tok = null, minval, maxval;
263 String noValueColour = NO_VALUE_MIN;
267 // at least four more tokens
268 if (mincol.equals(BAR))
274 gcol.nextToken(); // skip next '|'
276 maxcol = gcol.nextToken();
277 if (maxcol.equals(BAR))
283 gcol.nextToken(); // skip next '|'
285 tok = gcol.nextToken();
288 * check for specifier for colour for no attribute value
289 * (new in 2.11, defaults to minColour if not specified)
291 if (tok.equalsIgnoreCase(NO_VALUE_MIN))
293 tok = gcol.nextToken();
294 tok = gcol.nextToken();
296 else if (tok.equalsIgnoreCase(NO_VALUE_MAX))
298 noValueColour = NO_VALUE_MAX;
299 tok = gcol.nextToken();
300 tok = gcol.nextToken();
302 else if (tok.equalsIgnoreCase(NO_VALUE_NONE))
304 noValueColour = NO_VALUE_NONE;
305 tok = gcol.nextToken();
306 tok = gcol.nextToken();
309 gcol.nextToken(); // skip next '|'
310 if (tok.toLowerCase(Locale.ROOT).startsWith(ABSOLUTE))
312 minval = gcol.nextToken();
313 gcol.nextToken(); // skip next '|'
320 maxval = gcol.nextToken();
321 if (gcol.hasMoreTokens())
323 gcol.nextToken(); // skip next '|'
327 if (minval.length() > 0)
329 min = Float.valueOf(minval).floatValue();
331 } catch (Exception e)
333 throw new IllegalArgumentException(
334 "Couldn't parse the minimum value for graduated colour ('"
339 if (maxval.length() > 0)
341 max = Float.valueOf(maxval).floatValue();
343 } catch (Exception e)
345 throw new IllegalArgumentException(
346 "Couldn't parse the maximum value for graduated colour ("
353 * dummy min/max colours for colour by text
354 * (label or attribute value)
362 * construct the FeatureColour!
364 FeatureColour featureColour;
367 Color minColour = ColorUtils.parseColourString(mincol);
368 Color maxColour = ColorUtils.parseColourString(maxcol);
369 Color noColour = noValueColour.equals(NO_VALUE_MAX) ? maxColour
370 : (noValueColour.equals(NO_VALUE_NONE) ? null : minColour);
371 featureColour = new FeatureColour(maxColour, minColour, maxColour,
373 featureColour.setColourByLabel(minColour == null);
374 featureColour.setAutoScaled(autoScaled);
377 featureColour.setAttributeName(
378 FeatureMatcher.fromAttributeDisplayName(attName));
380 // add in any additional parameters
381 String ttype = null, tval = null;
382 if (gcol.hasMoreTokens())
384 // threshold type and possibly a threshold value
385 ttype = gcol.nextToken();
386 if (ttype.toLowerCase(Locale.ROOT).startsWith(BELOW))
388 featureColour.setBelowThreshold(true);
390 else if (ttype.toLowerCase(Locale.ROOT).startsWith(ABOVE))
392 featureColour.setAboveThreshold(true);
396 if (!ttype.toLowerCase(Locale.ROOT).startsWith("no"))
398 jalview.bin.Console.errPrintln(
399 "Ignoring unrecognised threshold type : " + ttype);
403 if (featureColour.hasThreshold())
408 tval = gcol.nextToken();
409 featureColour.setThreshold(Float.valueOf(tval).floatValue());
410 } catch (Exception e)
413 .errPrintln("Couldn't parse threshold value as a float: ("
417 if (gcol.hasMoreTokens())
419 jalview.bin.Console.errPrintln(
420 "Ignoring additional tokens in parameters in graduated colour specification\n");
421 while (gcol.hasMoreTokens())
423 jalview.bin.Console.errPrintln(BAR + gcol.nextToken());
425 jalview.bin.Console.errPrintln("\n");
427 return featureColour;
428 } catch (Exception e)
430 throw new IllegalArgumentException(e.getMessage());
435 * Default constructor
437 public FeatureColour()
443 * Constructor given a simple colour. This also 'primes' a graduated colour
444 * range, where the maximum colour is the given simple colour, and the minimum
445 * colour a paler shade of it. This is for convenience when switching from a
446 * simple colour to a graduated colour scheme.
450 public FeatureColour(Color c)
453 * set max colour to the simple colour, min colour to a paler shade of it
455 this(c, c == null ? Color.white : ColorUtils.bleachColour(c, 0.9f),
456 c == null ? Color.black : c, DEFAULT_NO_COLOUR, 0, 0);
459 * but enforce simple colour for now!
461 setGraduatedColour(false);
469 public FeatureColour(FeatureColour fc)
471 graduatedColour = fc.graduatedColour;
473 minColour = fc.minColour;
474 maxColour = fc.maxColour;
475 noColour = fc.noColour;
477 minGreen = fc.minGreen;
478 minBlue = fc.minBlue;
479 deltaRed = fc.deltaRed;
480 deltaGreen = fc.deltaGreen;
481 deltaBlue = fc.deltaBlue;
484 isHighToLow = fc.isHighToLow;
485 attributeName = fc.attributeName;
486 setAboveThreshold(fc.isAboveThreshold());
487 setBelowThreshold(fc.isBelowThreshold());
488 setThreshold(fc.getThreshold());
489 setAutoScaled(fc.isAutoScaled());
490 setColourByLabel(fc.isColourByLabel());
494 * Constructor that sets both simple and graduated colour values. This allows
495 * alternative colour schemes to be 'preserved' while switching between them
496 * to explore their effects on the visualisation.
498 * This sets the colour scheme to 'graduated' by default. Override this if
499 * wanted by calling <code>setGraduatedColour(false)</code> for a simple
500 * colour, or <code>setColourByLabel(true)</code> for colour by label.
505 * @param noValueColour
509 public FeatureColour(Color myColour, Color low, Color high,
510 Color noValueColour, float min, float max)
523 setGraduatedColour(true);
524 noColour = noValueColour;
525 threshold = Float.NaN;
526 isHighToLow = min >= max;
527 minRed = low.getRed() / 255f;
528 minGreen = low.getGreen() / 255f;
529 minBlue = low.getBlue() / 255f;
530 deltaRed = (high.getRed() / 255f) - minRed;
531 deltaGreen = (high.getGreen() / 255f) - minGreen;
532 deltaBlue = (high.getBlue() / 255f) - minBlue;
546 public boolean isGraduatedColour()
548 return graduatedColour;
552 * Sets the 'graduated colour' flag. If true, also sets 'colour by label' to
555 public void setGraduatedColour(boolean b)
560 setColourByLabel(false);
565 public Color getColour()
571 public Color getMinColour()
577 public Color getMaxColour()
583 public Color getNoColour()
589 public boolean isColourByLabel()
591 return colourByLabel;
595 * Sets the 'colour by label' flag. If true, also sets 'graduated colour' to
599 public void setColourByLabel(boolean b)
604 setGraduatedColour(false);
609 public boolean isBelowThreshold()
611 return belowThreshold;
615 public void setBelowThreshold(boolean b)
620 setAboveThreshold(false);
625 public boolean isAboveThreshold()
627 return aboveThreshold;
631 public void setAboveThreshold(boolean b)
636 setBelowThreshold(false);
641 public float getThreshold()
647 public void setThreshold(float f)
653 public boolean isAutoScaled()
659 public void setAutoScaled(boolean b)
668 public void updateBounds(float min, float max)
685 * Returns the colour for the given instance of the feature. This may be a
686 * simple colour, a colour generated from the feature description or other
687 * attribute (if isColourByLabel()), or a colour derived from the feature
688 * score or other attribute (if isGraduatedColour()).
690 * Answers null if feature score (or attribute) value lies outside a
691 * configured threshold.
697 public Color getColor(SequenceFeature feature)
699 if (isColourByLabel())
701 String label = attributeName == null ? feature.getDescription()
702 : feature.getValueAsString(attributeName);
703 return label == null ? noColour
704 : ColorUtils.createColourFromName(label);
707 if (!isGraduatedColour())
713 * graduated colour case, optionally with threshold
714 * may be based on feature score on an attribute value
715 * Float.NaN, or no value, is assigned the 'no value' colour
717 float scr = feature.getScore();
718 if (attributeName != null)
722 String attVal = feature.getValueAsString(attributeName);
723 scr = Float.valueOf(attVal);
724 } catch (Throwable e)
729 if (Float.isNaN(scr))
734 if (isAboveThreshold() && scr <= threshold)
739 if (isBelowThreshold() && scr >= threshold)
745 return getMaxColour();
747 float scl = (scr - base) / range;
760 return new Color(minRed + scl * deltaRed, minGreen + scl * deltaGreen,
761 minBlue + scl * deltaBlue);
765 * Returns the maximum score of the graduated colour range
770 public float getMax()
772 // regenerate the original values passed in to the constructor
773 return (isHighToLow) ? base : (base + range);
777 * Returns the minimum score of the graduated colour range
782 public float getMin()
784 // regenerate the original value passed in to the constructor
785 return (isHighToLow) ? (base + range) : base;
789 public boolean isSimpleColour()
791 return (!isColourByLabel() && !isGraduatedColour());
795 public boolean hasThreshold()
797 return isAboveThreshold() || isBelowThreshold();
801 public String toJalviewFormat(String featureType)
803 String colourString = null;
804 if (isSimpleColour())
806 colourString = Format.getHexString(getColour());
810 StringBuilder sb = new StringBuilder(32);
811 if (isColourByAttribute())
813 sb.append(ATTRIBUTE).append(BAR);
815 FeatureMatcher.toAttributeDisplayName(getAttributeName()));
817 else if (isColourByLabel())
825 if (isGraduatedColour())
827 sb.append(BAR).append(Format.getHexString(getMinColour()))
829 sb.append(Format.getHexString(getMaxColour())).append(BAR);
832 * 'no value' colour should be null, min or max colour;
833 * if none of these, coerce to minColour
835 String noValue = NO_VALUE_MIN;
836 if (maxColour.equals(noColour))
838 noValue = NO_VALUE_MAX;
840 if (noColour == null)
842 noValue = NO_VALUE_NONE;
844 sb.append(noValue).append(BAR);
847 sb.append(ABSOLUTE).append(BAR);
853 * colour by text with score threshold: empty fields for
854 * minColour and maxColour (not used)
858 sb.append(BAR).append(BAR).append(BAR);
861 if (hasThreshold() || isGraduatedColour())
863 sb.append(getMin()).append(BAR);
864 sb.append(getMax()).append(BAR);
865 if (isBelowThreshold())
867 sb.append(BELOW).append(BAR).append(getThreshold());
869 else if (isAboveThreshold())
871 sb.append(ABOVE).append(BAR).append(getThreshold());
878 colourString = sb.toString();
880 return String.format("%s\t%s", featureType, colourString);
884 public boolean isColourByAttribute()
886 return attributeName != null;
890 public String[] getAttributeName()
892 return attributeName;
896 public void setAttributeName(String... name)
898 attributeName = name;
902 public boolean isOutwithThreshold(SequenceFeature feature)
904 if (!isGraduatedColour())
908 float scr = feature.getScore();
909 if (attributeName != null)
913 String attVal = feature.getValueAsString(attributeName);
914 scr = Float.valueOf(attVal);
915 } catch (Throwable e)
920 if (Float.isNaN(scr))
925 return ((isAboveThreshold() && scr <= threshold)
926 || (isBelowThreshold() && scr >= threshold));
930 public String getDescription()
932 if (isSimpleColour())
934 return "r=" + colour.getRed() + ",g=" + colour.getGreen() + ",b="
937 StringBuilder tt = new StringBuilder();
940 if (getAttributeName() != null)
942 by = FeatureMatcher.toAttributeDisplayName(getAttributeName());
944 else if (isColourByLabel())
952 tt.append(MessageManager.formatMessage("action.by_title_param", by));
955 * add threshold if any
957 if (isAboveThreshold() || isBelowThreshold())
960 if (isColourByLabel())
963 * Jalview features file supports the combination of
964 * colour by label or attribute text with score threshold
966 tt.append(I18N_SCORE).append(" ");
968 tt.append(isAboveThreshold() ? "> " : "< ");
969 tt.append(getThreshold()).append(")");
972 return tt.toString();