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);
394 * Constructor for a graduated colour
398 * @param noValueColour
402 public FeatureColour(Color low, Color high, Color noValueColour,
403 float min, float max)
413 graduatedColour = true;
417 noColour = noValueColour;
418 threshold = Float.NaN;
419 isHighToLow = min >= max;
420 minRed = low.getRed() / 255f;
421 minGreen = low.getGreen() / 255f;
422 minBlue = low.getBlue() / 255f;
423 deltaRed = (high.getRed() / 255f) - minRed;
424 deltaGreen = (high.getGreen() / 255f) - minGreen;
425 deltaBlue = (high.getBlue() / 255f) - minBlue;
439 public boolean isGraduatedColour()
441 return graduatedColour;
445 * Sets the 'graduated colour' flag. If true, also sets 'colour by label' to
448 void setGraduatedColour(boolean b)
453 setColourByLabel(false);
458 public Color getColour()
464 public Color getMinColour()
470 public Color getMaxColour()
476 public Color getNoColour()
482 public boolean isColourByLabel()
484 return colourByLabel;
488 * Sets the 'colour by label' flag. If true, also sets 'graduated colour' to
492 public void setColourByLabel(boolean b)
497 setGraduatedColour(false);
502 public boolean isBelowThreshold()
504 return belowThreshold;
508 public void setBelowThreshold(boolean b)
513 setAboveThreshold(false);
518 public boolean isAboveThreshold()
520 return aboveThreshold;
524 public void setAboveThreshold(boolean b)
529 setBelowThreshold(false);
534 public float getThreshold()
540 public void setThreshold(float f)
546 public boolean isAutoScaled()
552 public void setAutoScaled(boolean b)
561 public void updateBounds(float min, float max)
578 * Returns the colour for the given instance of the feature. This may be a
579 * simple colour, a colour generated from the feature description (if
580 * isColourByLabel()), or a colour derived from the feature score (if
581 * isGraduatedColour()).
587 public Color getColor(SequenceFeature feature)
589 if (isColourByLabel())
591 String label = attributeName == null ? feature.getDescription()
592 : feature.getValueAsString(attributeName);
593 return label == null ? noColour : ColorUtils
594 .createColourFromName(label);
597 if (!isGraduatedColour())
603 * graduated colour case, optionally with threshold
604 * may be based on feature score on an attribute value
605 * Float.NaN, or no value, is assigned the 'no value' colour
607 float scr = feature.getScore();
608 if (attributeName != null)
612 String attVal = feature.getValueAsString(attributeName);
613 scr = Float.valueOf(attVal);
614 } catch (Throwable e)
619 if (Float.isNaN(scr))
624 if (isAboveThreshold() && scr <= threshold)
629 if (isBelowThreshold() && scr >= threshold)
635 return getMaxColour();
637 float scl = (scr - base) / range;
650 return new Color(minRed + scl * deltaRed, minGreen + scl * deltaGreen,
651 minBlue + scl * deltaBlue);
655 * Returns the maximum score of the graduated colour range
660 public float getMax()
662 // regenerate the original values passed in to the constructor
663 return (isHighToLow) ? base : (base + range);
667 * Returns the minimum score of the graduated colour range
672 public float getMin()
674 // regenerate the original value passed in to the constructor
675 return (isHighToLow) ? (base + range) : base;
679 public boolean isSimpleColour()
681 return (!isColourByLabel() && !isGraduatedColour());
685 public boolean hasThreshold()
687 return isAboveThreshold() || isBelowThreshold();
691 public String toJalviewFormat(String featureType)
693 String colourString = null;
694 if (isSimpleColour())
696 colourString = Format.getHexString(getColour());
700 StringBuilder sb = new StringBuilder(32);
701 if (isColourByLabel())
706 sb.append(BAR).append(BAR).append(BAR);
709 if (isGraduatedColour())
711 sb.append(Format.getHexString(getMinColour())).append(BAR);
712 sb.append(Format.getHexString(getMaxColour())).append(BAR);
715 sb.append("abso").append(BAR);
718 if (hasThreshold() || isGraduatedColour())
720 sb.append(getMin()).append(BAR);
721 sb.append(getMax()).append(BAR);
722 if (isBelowThreshold())
724 sb.append("below").append(BAR).append(getThreshold());
726 else if (isAboveThreshold())
728 sb.append("above").append(BAR).append(getThreshold());
735 colourString = sb.toString();
737 return String.format("%s\t%s", featureType, colourString);
741 public boolean isColourByAttribute()
743 return attributeName != null;
747 public String[] getAttributeName()
749 return attributeName;
753 public void setAttributeName(String... name)
755 attributeName = name;