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.awt.Color;
24 import java.util.IdentityHashMap;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AnnotatedCollectionI;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.GraphLine;
32 import jalview.datamodel.SequenceCollectionI;
33 import jalview.datamodel.SequenceI;
35 public class AnnotationColourGradient extends FollowerColourScheme
37 public static final int NO_THRESHOLD = -1;
39 public static final int BELOW_THRESHOLD = 0;
41 public static final int ABOVE_THRESHOLD = 1;
43 private final AlignmentAnnotation annotation;
45 private final int aboveAnnotationThreshold;
47 private boolean thresholdIsMinMax = false;
49 private GraphLine annotationThreshold;
59 private int greenRange;
61 private int blueRange;
63 private boolean predefinedColours = false;
65 private boolean seqAssociated = false;
68 * false if the scheme was constructed without a minColour and maxColour used
69 * to decide if existing colours should be taken from annotation elements when
72 private boolean noGradient = false;
74 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
77 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
78 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
80 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
81 colourScheme, aboveAnnotationThreshold);
82 acg.thresholdIsMinMax = thresholdIsMinMax;
83 acg.annotationThreshold = (annotationThreshold == null) ? null
84 : new GraphLine(annotationThreshold);
86 acg.greenMin = greenMin;
87 acg.blueMin = blueMin;
88 acg.redRange = redRange;
89 acg.greenRange = greenRange;
90 acg.blueRange = blueRange;
91 acg.predefinedColours = predefinedColours;
92 acg.seqAssociated = seqAssociated;
93 acg.noGradient = noGradient;
98 * Creates a new AnnotationColourGradient object.
100 public AnnotationColourGradient(AlignmentAnnotation annotation,
101 ColourSchemeI originalColour, int aboveThreshold)
103 if (originalColour instanceof AnnotationColourGradient)
105 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
109 colourScheme = originalColour;
112 this.annotation = annotation;
114 aboveAnnotationThreshold = aboveThreshold;
116 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
118 annotationThreshold = annotation.threshold;
120 // clear values so we don't get weird black bands...
133 * Creates a new AnnotationColourGradient object.
135 public AnnotationColourGradient(AlignmentAnnotation annotation,
136 Color minColour, Color maxColour, int aboveThreshold)
138 this.annotation = annotation;
140 aboveAnnotationThreshold = aboveThreshold;
142 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
144 annotationThreshold = annotation.threshold;
147 redMin = minColour.getRed();
148 greenMin = minColour.getGreen();
149 blueMin = minColour.getBlue();
151 redRange = maxColour.getRed() - redMin;
152 greenRange = maxColour.getGreen() - greenMin;
153 blueRange = maxColour.getBlue() - blueMin;
159 private void checkLimits()
161 aamax = annotation.graphMax;
162 aamin = annotation.graphMin;
163 if (annotation.isRNA())
165 // reset colour palette
166 ColourSchemeProperty.resetRnaHelicesShading();
167 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
172 public void alignmentChanged(AnnotatedCollectionI alignment,
173 Map<SequenceI, SequenceCollectionI> hiddenReps)
175 super.alignmentChanged(alignment, hiddenReps);
177 if (seqAssociated && annotation.getCalcId() != null)
179 if (seqannot != null)
185 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
187 // resolve the context containing all the annotation for the sequence
188 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
189 : alignment.getContext();
190 boolean f = true, rna = false;
191 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
194 if (alan.sequenceRef != null
195 && (alan.label != null && annotation != null && alan.label
196 .equals(annotation.label)))
198 if (!rna && alan.isRNA())
202 seqannot.put(alan.sequenceRef, alan);
203 if (f || alan.graphMax > aamax)
205 aamax = alan.graphMax;
207 if (f || alan.graphMin < aamin)
209 aamin = alan.graphMin;
216 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
221 float aamin = 0f, aamax = 0f;
223 public AlignmentAnnotation getAnnotation()
228 public int getAboveThreshold()
230 return aboveAnnotationThreshold;
233 public float getAnnotationThreshold()
235 if (annotationThreshold == null)
241 return annotationThreshold.value;
245 public Color getMinColour()
247 return new Color(redMin, greenMin, blueMin);
250 public Color getMaxColour()
252 return new Color(redMin + redRange, greenMin + greenRange, blueMin
262 * @return DOCUMENT ME!
265 public Color findColour(char c)
278 * @return DOCUMENT ME!
281 public Color findColour(char c, int j, SequenceI seq)
283 Color currentColour = Color.white;
284 AlignmentAnnotation ann = (seqAssociated && seqannot != null ? seqannot
285 .get(seq) : this.annotation);
288 return currentColour;
290 if ((threshold == 0) || aboveThreshold(c, j))
292 if (ann.annotations != null && j < ann.annotations.length
293 && ann.annotations[j] != null
294 && !jalview.util.Comparison.isGap(c))
296 Annotation aj = ann.annotations[j];
297 // 'use original colours' => colourScheme != null
298 // -> look up colour to be used
299 // predefined colours => preconfigured shading
300 // -> only use original colours reference if thresholding enabled &
302 // annotation.hasIcons => null or black colours replaced with glyph
304 // -> reuse original colours if present
305 // -> if thresholding enabled then return colour on non-whitespace glyph
307 if (aboveAnnotationThreshold == NO_THRESHOLD
308 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
309 : aj.value <= annotationThreshold.value)))
311 if (predefinedColours && aj.colour != null
312 && !aj.colour.equals(Color.black))
314 currentColour = aj.colour;
316 else if (ann.hasIcons
317 && ann.graph == AlignmentAnnotation.NO_GRAPH)
319 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
320 && aj.secondaryStructure != '-')
322 if (colourScheme != null)
324 currentColour = colourScheme.findColour(c, j, seq);
330 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
334 currentColour = ann.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
335 : ann.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
336 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
348 if (colourScheme != null)
350 currentColour = colourScheme.findColour(c, j, seq);
354 if (aj.colour != null)
356 currentColour = aj.colour;
362 currentColour = shadeCalculation(ann, j);
365 if (conservationColouring)
367 currentColour = applyConservation(currentColour, j);
371 return currentColour;
375 * Returns a graduated colour for the annotation at the given column. If there
376 * is a threshold value, and it is used as the top/bottom of the colour range,
377 * and the value satisfies the threshold condition, then a colour
378 * proportionate to the range from the threshold is calculated. For all other
379 * cases, a colour proportionate to the annotation's min-max range is
380 * calulated. Note that thresholding is _not_ done here (a colour is computed
381 * even if threshold is not passed), but by the renderer.
387 Color shadeCalculation(AlignmentAnnotation ann, int col)
390 float value = ann.annotations[col].value;
391 if (thresholdIsMinMax && ann.threshold != null
392 && aboveAnnotationThreshold == ABOVE_THRESHOLD
393 && value >= ann.threshold.value)
395 range = (value - ann.threshold.value)
396 / (ann.graphMax - ann.threshold.value);
398 else if (thresholdIsMinMax && ann.threshold != null
399 && aboveAnnotationThreshold == BELOW_THRESHOLD
400 && value <= ann.threshold.value)
402 range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
406 if (ann.graphMax != ann.graphMin)
408 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
416 int dr = (int) (redRange * range + redMin);
417 int dg = (int) (greenRange * range + greenMin);
418 int db = (int) (blueRange * range + blueMin);
420 return new Color(dr, dg, db);
423 public boolean isPredefinedColours()
425 return predefinedColours;
428 public void setPredefinedColours(boolean predefinedColours)
430 this.predefinedColours = predefinedColours;
433 public boolean isSeqAssociated()
435 return seqAssociated;
438 public void setSeqAssociated(boolean sassoc)
440 seqAssociated = sassoc;
443 public boolean isThresholdIsMinMax()
445 return thresholdIsMinMax;
448 public void setThresholdIsMinMax(boolean thresholdIsMinMax)
450 this.thresholdIsMinMax = thresholdIsMinMax;