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.AlignViewportI;
24 import jalview.bin.Jalview;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.AnnotatedCollectionI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.GraphLine;
30 import jalview.datamodel.SequenceCollectionI;
31 import jalview.datamodel.SequenceI;
32 import jalview.renderer.AnnotationRenderer;
33 import jalview.util.Comparison;
35 import java.awt.Color;
36 import java.util.IdentityHashMap;
39 public class AnnotationColourGradient extends FollowerColourScheme
41 public static final int NO_THRESHOLD = -1;
43 public static final int BELOW_THRESHOLD = 0;
45 public static final int ABOVE_THRESHOLD = 1;
47 private final AlignmentAnnotation annotation;
49 private final int aboveAnnotationThreshold;
51 public boolean thresholdIsMinMax = false;
53 private GraphLine annotationThreshold;
63 private int greenRange;
65 private int blueRange;
67 private boolean predefinedColours = false;
69 private boolean seqAssociated = false;
72 * false if the scheme was constructed without a minColour and maxColour used
73 * to decide if existing colours should be taken from annotation elements when
76 private boolean noGradient = false;
78 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
81 public ColourSchemeI getInstance(AlignViewportI view,
82 AnnotatedCollectionI sg)
84 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
85 getColourScheme(), aboveAnnotationThreshold);
86 acg.thresholdIsMinMax = thresholdIsMinMax;
87 acg.annotationThreshold = (annotationThreshold == null) ? null
88 : new GraphLine(annotationThreshold);
90 acg.greenMin = greenMin;
91 acg.blueMin = blueMin;
92 acg.redRange = redRange;
93 acg.greenRange = greenRange;
94 acg.blueRange = blueRange;
95 acg.predefinedColours = predefinedColours;
96 acg.seqAssociated = seqAssociated;
97 acg.noGradient = noGradient;
102 * Creates a new AnnotationColourGradient object.
104 public AnnotationColourGradient(AlignmentAnnotation annotation,
105 ColourSchemeI originalColour, int aboveThreshold)
107 if (originalColour instanceof AnnotationColourGradient)
109 setColourScheme(((AnnotationColourGradient) originalColour)
114 setColourScheme(originalColour);
117 this.annotation = annotation;
119 aboveAnnotationThreshold = aboveThreshold;
121 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
123 annotationThreshold = annotation.threshold;
125 // clear values so we don't get weird black bands...
138 * Creates a new AnnotationColourGradient object.
140 public AnnotationColourGradient(AlignmentAnnotation annotation,
141 Color minColour, Color maxColour, int aboveThreshold)
143 this.annotation = annotation;
145 aboveAnnotationThreshold = aboveThreshold;
147 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
149 annotationThreshold = annotation.threshold;
152 redMin = minColour.getRed();
153 greenMin = minColour.getGreen();
154 blueMin = minColour.getBlue();
156 redRange = maxColour.getRed() - redMin;
157 greenRange = maxColour.getGreen() - greenMin;
158 blueRange = maxColour.getBlue() - blueMin;
164 private void checkLimits()
166 aamax = annotation.graphMax;
167 aamin = annotation.graphMin;
168 if (annotation.isRNA())
170 // reset colour palette
171 ColourSchemeProperty.resetRnaHelicesShading();
172 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
177 public void alignmentChanged(AnnotatedCollectionI alignment,
178 Map<SequenceI, SequenceCollectionI> hiddenReps)
180 super.alignmentChanged(alignment, hiddenReps);
182 if (seqAssociated && annotation.getCalcId() != null)
184 if (seqannot != null)
190 seqannot = new IdentityHashMap<>();
192 // resolve the context containing all the annotation for the sequence
193 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
195 : alignment.getContext();
196 boolean f = true, rna = false;
197 for (AlignmentAnnotation alan : alcontext
198 .findAnnotation(annotation.getCalcId()))
200 if (alan.sequenceRef != null
201 && (alan.label != null && annotation != null
202 && alan.label.equals(annotation.label)))
204 if (!rna && alan.isRNA())
208 seqannot.put(alan.sequenceRef, alan);
209 if (f || alan.graphMax > aamax)
211 aamax = alan.graphMax;
213 if (f || alan.graphMin < aamin)
215 aamin = alan.graphMin;
222 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
227 float aamin = 0f, aamax = 0f;
229 public AlignmentAnnotation getAnnotation()
234 public int getAboveThreshold()
236 return aboveAnnotationThreshold;
239 public float getAnnotationThreshold()
241 if (annotationThreshold == null)
247 return annotationThreshold.value;
251 public Color getMinColour()
253 return new Color(redMin, greenMin, blueMin);
256 public Color getMaxColour()
258 return new Color(redMin + redRange, greenMin + greenRange,
259 blueMin + blueRange);
268 * @return DOCUMENT ME!
271 public Color findColour(char c)
277 * Returns the colour for a given character and position in a sequence
280 * the residue character
282 * the aligned position
288 public Color findColour(char c, int j, SequenceI seq)
291 * locate the annotation we are configured to colour by
293 AlignmentAnnotation ann = (seqAssociated && seqannot != null
298 * if gap or no annotation at position, no colour (White)
300 if (ann == null || ann.annotations == null
301 || j >= ann.annotations.length || ann.annotations[j] == null
302 || Comparison.isGap(c))
307 Annotation aj = ann.annotations[j];
308 // 'use original colours' => colourScheme != null
309 // -> look up colour to be used
310 // predefined colours => preconfigured shading
311 // -> only use original colours reference if thresholding enabled &
313 // annotation.hasIcons => null or black colours replaced with glyph
315 // -> reuse original colours if present
316 // -> if thresholding enabled then return colour on non-whitespace glyph
319 * if threshold applies, and annotation fails the test - no colour (white)
321 if (annotationThreshold != null)
323 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
324 && aj.value <= annotationThreshold.value)
325 || (aboveAnnotationThreshold == BELOW_THRESHOLD
326 && aj.value >= annotationThreshold.value))
333 * If 'use original colours' then return the colour of the annotation
334 * at the aligned position - computed using the background colour scheme
336 if (predefinedColours && aj.colour != null
337 && !aj.colour.equals(Color.black))
342 Color result = Color.white;
343 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
346 * secondary structure symbol colouring
348 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
349 && aj.secondaryStructure != '-')
351 if (getColourScheme() != null)
353 result = getColourScheme().findColour(c, j, seq, null, 0f);
359 result = Jalview.getInstance().rnaHelices[(int) aj.value];
363 result = ann.annotations[j].secondaryStructure == 'H'
364 ? AnnotationRenderer.HELIX_COLOUR
365 : ann.annotations[j].secondaryStructure == 'E'
366 ? AnnotationRenderer.SHEET_COLOUR
367 : AnnotationRenderer.STEM_COLOUR;
378 if (getColourScheme() != null)
380 result = getColourScheme().findColour(c, j, seq, null, 0f);
384 if (aj.colour != null)
392 result = shadeCalculation(ann, j);
399 * Returns a graduated colour for the annotation at the given column. If there
400 * is a threshold value, and it is used as the top/bottom of the colour range,
401 * and the value satisfies the threshold condition, then a colour
402 * proportionate to the range from the threshold is calculated. For all other
403 * cases, a colour proportionate to the annotation's min-max range is
404 * calulated. Note that thresholding is _not_ done here (a colour is computed
405 * even if threshold is not passed).
411 Color shadeCalculation(AlignmentAnnotation ann, int col)
414 float value = ann.annotations[col].value;
415 if (thresholdIsMinMax && ann.threshold != null
416 && aboveAnnotationThreshold == ABOVE_THRESHOLD
417 && value >= ann.threshold.value)
419 range = ann.graphMax == ann.threshold.value ? 1f
420 : (value - ann.threshold.value)
421 / (ann.graphMax - ann.threshold.value);
423 else if (thresholdIsMinMax && ann.threshold != null
424 && aboveAnnotationThreshold == BELOW_THRESHOLD
425 && value <= ann.threshold.value)
427 range = ann.graphMin == ann.threshold.value ? 0f
428 : (value - ann.graphMin)
429 / (ann.threshold.value - ann.graphMin);
433 if (ann.graphMax != ann.graphMin)
435 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
443 int dr = (int) (redRange * range + redMin);
444 int dg = (int) (greenRange * range + greenMin);
445 int db = (int) (blueRange * range + blueMin);
447 return new Color(dr, dg, db);
450 public boolean isPredefinedColours()
452 return predefinedColours;
455 public void setPredefinedColours(boolean predefinedColours)
457 this.predefinedColours = predefinedColours;
460 public boolean isSeqAssociated()
462 return seqAssociated;
465 public void setSeqAssociated(boolean sassoc)
467 seqAssociated = sassoc;
470 public boolean isThresholdIsMinMax()
472 return thresholdIsMinMax;
475 public void setThresholdIsMinMax(boolean minMax)
477 this.thresholdIsMinMax = minMax;
481 public String getSchemeName()
483 return ANNOTATION_COLOUR;
487 public boolean isSimple()