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.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.GraphLine;
29 import jalview.datamodel.SequenceCollectionI;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.AnnotationRenderer;
32 import jalview.util.Comparison;
34 import java.awt.Color;
35 import java.util.IdentityHashMap;
38 public class AnnotationColourGradient extends FollowerColourScheme
40 public static final int NO_THRESHOLD = -1;
42 public static final int BELOW_THRESHOLD = 0;
44 public static final int ABOVE_THRESHOLD = 1;
46 private final AlignmentAnnotation annotation;
48 private final int aboveAnnotationThreshold;
50 public boolean thresholdIsMinMax = false;
52 private GraphLine annotationThreshold;
62 private int greenRange;
64 private int blueRange;
66 private boolean predefinedColours = false;
68 private boolean seqAssociated = false;
71 * false if the scheme was constructed without a minColour and maxColour used
72 * to decide if existing colours should be taken from annotation elements when
75 private boolean noGradient = false;
77 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
80 public ColourSchemeI getInstance(AlignViewportI view,
81 AnnotatedCollectionI sg)
83 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
84 getColourScheme(), aboveAnnotationThreshold);
85 acg.thresholdIsMinMax = thresholdIsMinMax;
86 acg.annotationThreshold = (annotationThreshold == null) ? null
87 : new GraphLine(annotationThreshold);
89 acg.greenMin = greenMin;
90 acg.blueMin = blueMin;
91 acg.redRange = redRange;
92 acg.greenRange = greenRange;
93 acg.blueRange = blueRange;
94 acg.predefinedColours = predefinedColours;
95 acg.seqAssociated = seqAssociated;
96 acg.noGradient = noGradient;
101 * Creates a new AnnotationColourGradient object.
103 public AnnotationColourGradient(AlignmentAnnotation annotation,
104 ColourSchemeI originalColour, int aboveThreshold)
106 if (originalColour instanceof AnnotationColourGradient)
108 setColourScheme(((AnnotationColourGradient) originalColour)
113 setColourScheme(originalColour);
116 this.annotation = annotation;
118 aboveAnnotationThreshold = aboveThreshold;
120 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
122 annotationThreshold = annotation.threshold;
124 // clear values so we don't get weird black bands...
137 * Creates a new AnnotationColourGradient object.
139 public AnnotationColourGradient(AlignmentAnnotation annotation,
140 Color minColour, Color maxColour, int aboveThreshold)
142 this.annotation = annotation;
144 aboveAnnotationThreshold = aboveThreshold;
146 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
148 annotationThreshold = annotation.threshold;
151 redMin = minColour.getRed();
152 greenMin = minColour.getGreen();
153 blueMin = minColour.getBlue();
155 redRange = maxColour.getRed() - redMin;
156 greenRange = maxColour.getGreen() - greenMin;
157 blueRange = maxColour.getBlue() - blueMin;
163 private void checkLimits()
165 aamax = annotation.graphMax;
166 aamin = annotation.graphMin;
167 if (annotation.isRNA())
169 // reset colour palette
170 ColourSchemeProperty.resetRnaHelicesShading();
171 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
176 public void alignmentChanged(AnnotatedCollectionI alignment,
177 Map<SequenceI, SequenceCollectionI> hiddenReps)
179 super.alignmentChanged(alignment, hiddenReps);
181 if (seqAssociated && annotation.getCalcId() != null)
183 if (seqannot != null)
189 seqannot = new IdentityHashMap<>();
191 // resolve the context containing all the annotation for the sequence
192 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
194 : alignment.getContext();
195 boolean f = true, rna = false;
196 for (AlignmentAnnotation alan : alcontext
197 .findAnnotation(annotation.getCalcId()))
199 if (alan.sequenceRef != null
200 && (alan.label != null && annotation != null
201 && alan.label.equals(annotation.label)))
203 if (!rna && alan.isRNA())
207 seqannot.put(alan.sequenceRef, alan);
208 if (f || alan.graphMax > aamax)
210 aamax = alan.graphMax;
212 if (f || alan.graphMin < aamin)
214 aamin = alan.graphMin;
221 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
226 float aamin = 0f, aamax = 0f;
228 public AlignmentAnnotation getAnnotation()
233 public int getAboveThreshold()
235 return aboveAnnotationThreshold;
238 public float getAnnotationThreshold()
240 if (annotationThreshold == null)
246 return annotationThreshold.value;
250 public Color getMinColour()
252 return new Color(redMin, greenMin, blueMin);
255 public Color getMaxColour()
257 return new Color(redMin + redRange, greenMin + greenRange,
258 blueMin + blueRange);
267 * @return DOCUMENT ME!
270 public Color findColour(char c)
276 * Returns the colour for a given character and position in a sequence
279 * the residue character
281 * the aligned position
287 public Color findColour(char c, int j, SequenceI seq)
290 * locate the annotation we are configured to colour by
292 AlignmentAnnotation ann = (seqAssociated && seqannot != null
297 * if gap or no annotation at position, no colour (White)
299 if (ann == null || ann.annotations == null
300 || j >= ann.annotations.length || ann.annotations[j] == null
301 || Comparison.isGap(c))
306 Annotation aj = ann.annotations[j];
307 // 'use original colours' => colourScheme != null
308 // -> look up colour to be used
309 // predefined colours => preconfigured shading
310 // -> only use original colours reference if thresholding enabled &
312 // annotation.hasIcons => null or black colours replaced with glyph
314 // -> reuse original colours if present
315 // -> if thresholding enabled then return colour on non-whitespace glyph
318 * if threshold applies, and annotation fails the test - no colour (white)
320 if (annotationThreshold != null)
322 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
323 && aj.value <= annotationThreshold.value)
324 || (aboveAnnotationThreshold == BELOW_THRESHOLD
325 && aj.value >= annotationThreshold.value))
332 * If 'use original colours' then return the colour of the annotation
333 * at the aligned position - computed using the background colour scheme
335 if (predefinedColours && aj.colour != null
336 && !aj.colour.equals(Color.black))
341 Color result = Color.white;
342 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
345 * secondary structure symbol colouring
347 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
348 && aj.secondaryStructure != '-')
350 if (getColourScheme() != null)
352 result = getColourScheme().findColour(c, j, seq, null, 0f);
358 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
362 result = ann.annotations[j].secondaryStructure == 'H'
363 ? AnnotationRenderer.HELIX_COLOUR
364 : ann.annotations[j].secondaryStructure == 'E'
365 ? AnnotationRenderer.SHEET_COLOUR
366 : AnnotationRenderer.STEM_COLOUR;
377 if (getColourScheme() != null)
379 result = getColourScheme().findColour(c, j, seq, null, 0f);
383 if (aj.colour != null)
391 result = shadeCalculation(ann, j);
398 * Returns a graduated colour for the annotation at the given column. If there
399 * is a threshold value, and it is used as the top/bottom of the colour range,
400 * and the value satisfies the threshold condition, then a colour
401 * proportionate to the range from the threshold is calculated. For all other
402 * cases, a colour proportionate to the annotation's min-max range is
403 * calulated. Note that thresholding is _not_ done here (a colour is computed
404 * even if threshold is not passed).
410 Color shadeCalculation(AlignmentAnnotation ann, int col)
413 float value = ann.annotations[col].value;
414 if (thresholdIsMinMax && ann.threshold != null
415 && aboveAnnotationThreshold == ABOVE_THRESHOLD
416 && value >= ann.threshold.value)
418 range = ann.graphMax == ann.threshold.value ? 1f
419 : (value - ann.threshold.value)
420 / (ann.graphMax - ann.threshold.value);
422 else if (thresholdIsMinMax && ann.threshold != null
423 && aboveAnnotationThreshold == BELOW_THRESHOLD
424 && value <= ann.threshold.value)
426 range = ann.graphMin == ann.threshold.value ? 0f
427 : (value - ann.graphMin)
428 / (ann.threshold.value - ann.graphMin);
432 if (ann.graphMax != ann.graphMin)
434 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
442 int dr = (int) (redRange * range + redMin);
443 int dg = (int) (greenRange * range + greenMin);
444 int db = (int) (blueRange * range + blueMin);
446 return new Color(dr, dg, db);
449 public boolean isPredefinedColours()
451 return predefinedColours;
454 public void setPredefinedColours(boolean predefinedColours)
456 this.predefinedColours = predefinedColours;
459 public boolean isSeqAssociated()
461 return seqAssociated;
464 public void setSeqAssociated(boolean sassoc)
466 seqAssociated = sassoc;
469 public boolean isThresholdIsMinMax()
471 return thresholdIsMinMax;
474 public void setThresholdIsMinMax(boolean minMax)
476 this.thresholdIsMinMax = minMax;
480 public String getSchemeName()
482 return ANNOTATION_COLOUR;
486 public boolean isSimple()