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 jalview.api.FeatureColourI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.util.ColorUtils;
26 import jalview.util.Format;
28 import java.awt.Color;
29 import java.util.StringTokenizer;
32 * A class that represents a colour scheme for a feature type. Options supported
35 * <li>a simple colour e.g. Red</li>
36 * <li>colour by label - a colour is generated from the feature description</li>
37 * <li>graduated colour by feature score</li>
39 * <li>minimum and maximum score range must be provided</li>
40 * <li>minimum and maximum value colours should be specified</li>
41 * <li>a colour for 'no value' may optionally be provided</li>
42 * <li>colours for intermediate scores are interpolated RGB values</li>
43 * <li>there is an optional threshold above/below which to colour values</li>
44 * <li>the range may be the full value range, or may be limited by the threshold
47 * <li>colour by (text) value of a named attribute</li> <li>graduated colour by
48 * (numeric) value of a named attribute</li> </ul>
50 public class FeatureColour implements FeatureColourI
52 static final Color DEFAULT_NO_COLOUR = null;
54 private static final String BAR = "|";
56 final private Color colour;
58 final private Color minColour;
60 final private Color maxColour;
63 * colour to use for colour by attribute when the
64 * attribute value is absent
66 final private Color noColour;
69 * if true, then colour has a gradient based on a numerical
70 * range (either feature score, or an attribute value)
72 private boolean graduatedColour;
75 * if true, colour values are generated from a text string,
76 * either feature description, or an attribute value
78 private boolean colourByLabel;
81 * if not null, the value of [attribute, [sub-attribute] ...]
82 * is used for colourByLabel or graduatedColour
84 private String[] attributeName;
86 private float threshold;
92 private boolean belowThreshold;
94 private boolean aboveThreshold;
96 private boolean isHighToLow;
98 private boolean autoScaled;
100 final private float minRed;
102 final private float minGreen;
104 final private float minBlue;
106 final private float deltaRed;
108 final private float deltaGreen;
110 final private float deltaBlue;
113 * Parses a Jalview features file format colour descriptor
114 * [label|][mincolour|maxcolour
115 * |[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue] Examples:
119 * <li>25,125,213</li>
121 * <li>label|||0.0|0.0|above|12.5</li>
122 * <li>label|||0.0|0.0|below|12.5</li>
123 * <li>red|green|12.0|26.0|none</li>
124 * <li>a28bbb|3eb555|12.0|26.0|above|12.5</li>
125 * <li>a28bbb|3eb555|abso|12.0|26.0|below|12.5</li>
130 * @throws IllegalArgumentException
133 public static FeatureColour parseJalviewFeatureColour(String descriptor)
135 StringTokenizer gcol = new StringTokenizer(descriptor, "|", true);
136 float min = Float.MIN_VALUE;
137 float max = Float.MAX_VALUE;
138 boolean labelColour = false;
140 String mincol = gcol.nextToken();
143 throw new IllegalArgumentException(
144 "Expected either 'label' or a colour specification in the line: "
147 String maxcol = null;
148 if (mincol.toLowerCase().indexOf("label") == 0)
151 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
153 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
156 if (!labelColour && !gcol.hasMoreTokens())
159 * only a simple colour specification - parse it
161 Color colour = ColorUtils.parseColourString(descriptor);
164 throw new IllegalArgumentException(
165 "Invalid colour descriptor: " + descriptor);
167 return new FeatureColour(colour);
171 * autoScaled == true: colours range over actual score range
172 * autoScaled == false ('abso'): colours range over min/max range
174 boolean autoScaled = true;
175 String tok = null, minval, maxval;
178 // at least four more tokens
179 if (mincol.equals("|"))
185 gcol.nextToken(); // skip next '|'
187 maxcol = gcol.nextToken();
188 if (maxcol.equals("|"))
194 gcol.nextToken(); // skip next '|'
196 tok = gcol.nextToken();
197 gcol.nextToken(); // skip next '|'
198 if (tok.toLowerCase().startsWith("abso"))
200 minval = gcol.nextToken();
201 gcol.nextToken(); // skip next '|'
208 maxval = gcol.nextToken();
209 if (gcol.hasMoreTokens())
211 gcol.nextToken(); // skip next '|'
215 if (minval.length() > 0)
217 min = new Float(minval).floatValue();
219 } catch (Exception e)
221 throw new IllegalArgumentException(
222 "Couldn't parse the minimum value for graduated colour ("
227 if (maxval.length() > 0)
229 max = new Float(maxval).floatValue();
231 } catch (Exception e)
233 throw new IllegalArgumentException(
234 "Couldn't parse the maximum value for graduated colour ("
240 // add in some dummy min/max colours for the label-only
247 * construct the FeatureColour
249 FeatureColour featureColour;
252 Color minColour = ColorUtils.parseColourString(mincol);
253 Color maxColour = ColorUtils.parseColourString(maxcol);
254 featureColour = new FeatureColour(minColour, maxColour, min, max);
255 featureColour.setColourByLabel(labelColour);
256 featureColour.setAutoScaled(autoScaled);
257 // add in any additional parameters
258 String ttype = null, tval = null;
259 if (gcol.hasMoreTokens())
261 // threshold type and possibly a threshold value
262 ttype = gcol.nextToken();
263 if (ttype.toLowerCase().startsWith("below"))
265 featureColour.setBelowThreshold(true);
267 else if (ttype.toLowerCase().startsWith("above"))
269 featureColour.setAboveThreshold(true);
273 if (!ttype.toLowerCase().startsWith("no"))
276 "Ignoring unrecognised threshold type : " + ttype);
280 if (featureColour.hasThreshold())
285 tval = gcol.nextToken();
286 featureColour.setThreshold(new Float(tval).floatValue());
287 } catch (Exception e)
289 System.err.println("Couldn't parse threshold value as a float: ("
293 if (gcol.hasMoreTokens())
296 "Ignoring additional tokens in parameters in graduated colour specification\n");
297 while (gcol.hasMoreTokens())
299 System.err.println("|" + gcol.nextToken());
301 System.err.println("\n");
303 return featureColour;
304 } catch (Exception e)
306 throw new IllegalArgumentException(e.getMessage());
311 * Default constructor
313 public FeatureColour()
319 * Constructor given a simple colour
323 public FeatureColour(Color c)
325 minColour = Color.WHITE;
326 maxColour = Color.BLACK;
327 noColour = DEFAULT_NO_COLOUR;
338 * Constructor given a colour range and a score range, defaulting 'no value
339 * colour' to be the same as minimum colour
346 public FeatureColour(Color low, Color high, float min, float max)
348 this(low, high, low, min, max);
356 public FeatureColour(FeatureColour fc)
358 graduatedColour = fc.graduatedColour;
360 minColour = fc.minColour;
361 maxColour = fc.maxColour;
362 noColour = fc.noColour;
364 minGreen = fc.minGreen;
365 minBlue = fc.minBlue;
366 deltaRed = fc.deltaRed;
367 deltaGreen = fc.deltaGreen;
368 deltaBlue = fc.deltaBlue;
371 isHighToLow = fc.isHighToLow;
372 attributeName = fc.attributeName;
373 setAboveThreshold(fc.isAboveThreshold());
374 setBelowThreshold(fc.isBelowThreshold());
375 setThreshold(fc.getThreshold());
376 setAutoScaled(fc.isAutoScaled());
377 setColourByLabel(fc.isColourByLabel());
381 * Copy constructor with new min/max ranges
387 public FeatureColour(FeatureColour fc, float min, float max)
390 updateBounds(min, max);
393 public FeatureColour(Color low, Color high, Color noValueColour,
394 float min, float max)
404 graduatedColour = true;
408 noColour = noValueColour;
409 threshold = Float.NaN;
410 isHighToLow = min >= max;
411 minRed = low.getRed() / 255f;
412 minGreen = low.getGreen() / 255f;
413 minBlue = low.getBlue() / 255f;
414 deltaRed = (high.getRed() / 255f) - minRed;
415 deltaGreen = (high.getGreen() / 255f) - minGreen;
416 deltaBlue = (high.getBlue() / 255f) - minBlue;
430 public boolean isGraduatedColour()
432 return graduatedColour;
436 * Sets the 'graduated colour' flag. If true, also sets 'colour by label' to
439 void setGraduatedColour(boolean b)
444 setColourByLabel(false);
449 public Color getColour()
455 public Color getMinColour()
461 public Color getMaxColour()
467 public Color getNoColour()
473 public boolean isColourByLabel()
475 return colourByLabel;
479 * Sets the 'colour by label' flag. If true, also sets 'graduated colour' to
483 public void setColourByLabel(boolean b)
488 setGraduatedColour(false);
493 public boolean isBelowThreshold()
495 return belowThreshold;
499 public void setBelowThreshold(boolean b)
504 setAboveThreshold(false);
509 public boolean isAboveThreshold()
511 return aboveThreshold;
515 public void setAboveThreshold(boolean b)
520 setBelowThreshold(false);
525 public float getThreshold()
531 public void setThreshold(float f)
537 public boolean isAutoScaled()
543 public void setAutoScaled(boolean b)
552 public void updateBounds(float min, float max)
569 * Returns the colour for the given instance of the feature. This may be a
570 * simple colour, a colour generated from the feature description (if
571 * isColourByLabel()), or a colour derived from the feature score (if
572 * isGraduatedColour()).
578 public Color getColor(SequenceFeature feature)
580 if (isColourByLabel())
582 String label = attributeName == null ? feature.getDescription()
583 : feature.getValueAsString(attributeName);
584 return label == null ? noColour : ColorUtils
585 .createColourFromName(label);
588 if (!isGraduatedColour())
594 * graduated colour case, optionally with threshold
595 * may be based on feature score on an attribute value
596 * Float.NaN, or no value, is assigned the 'no value' colour
598 float scr = feature.getScore();
599 if (attributeName != null)
603 String attVal = feature.getValueAsString(attributeName);
604 scr = Float.valueOf(attVal);
605 } catch (Throwable e)
610 if (Float.isNaN(scr))
615 if (isAboveThreshold() && scr <= threshold)
620 if (isBelowThreshold() && scr >= threshold)
626 return getMaxColour();
628 float scl = (scr - base) / range;
641 return new Color(minRed + scl * deltaRed, minGreen + scl * deltaGreen,
642 minBlue + scl * deltaBlue);
646 * Returns the maximum score of the graduated colour range
651 public float getMax()
653 // regenerate the original values passed in to the constructor
654 return (isHighToLow) ? base : (base + range);
658 * Returns the minimum score of the graduated colour range
663 public float getMin()
665 // regenerate the original value passed in to the constructor
666 return (isHighToLow) ? (base + range) : base;
670 public boolean isSimpleColour()
672 return (!isColourByLabel() && !isGraduatedColour());
676 public boolean hasThreshold()
678 return isAboveThreshold() || isBelowThreshold();
682 public String toJalviewFormat(String featureType)
684 String colourString = null;
685 if (isSimpleColour())
687 colourString = Format.getHexString(getColour());
691 StringBuilder sb = new StringBuilder(32);
692 if (isColourByLabel())
697 sb.append(BAR).append(BAR).append(BAR);
700 if (isGraduatedColour())
702 sb.append(Format.getHexString(getMinColour())).append(BAR);
703 sb.append(Format.getHexString(getMaxColour())).append(BAR);
706 sb.append("abso").append(BAR);
709 if (hasThreshold() || isGraduatedColour())
711 sb.append(getMin()).append(BAR);
712 sb.append(getMax()).append(BAR);
713 if (isBelowThreshold())
715 sb.append("below").append(BAR).append(getThreshold());
717 else if (isAboveThreshold())
719 sb.append("above").append(BAR).append(getThreshold());
726 colourString = sb.toString();
728 return String.format("%s\t%s", featureType, colourString);
732 public boolean isColourByAttribute()
734 return attributeName != null;
738 public String[] getAttributeName()
740 return attributeName;
744 public void setAttributeName(String... name)
746 attributeName = name;