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
293 && j < ann.annotations.length
294 && ann.annotations[j] != null
295 && !jalview.util.Comparison.isGap(c))
297 Annotation aj = ann.annotations[j];
298 // 'use original colours' => colourScheme != null
299 // -> look up colour to be used
300 // predefined colours => preconfigured shading
301 // -> only use original colours reference if thresholding enabled &
303 // annotation.hasIcons => null or black colours replaced with glyph
305 // -> reuse original colours if present
306 // -> if thresholding enabled then return colour on non-whitespace glyph
308 if (aboveAnnotationThreshold == NO_THRESHOLD
309 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
310 : aj.value <= annotationThreshold.value)))
312 if (predefinedColours && aj.colour != null
313 && !aj.colour.equals(Color.black))
315 currentColour = aj.colour;
317 else if (ann.hasIcons
318 && ann.graph == AlignmentAnnotation.NO_GRAPH)
320 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
321 && aj.secondaryStructure != '-')
323 if (colourScheme != null)
325 currentColour = colourScheme.findColour(c, j, seq);
331 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
335 currentColour = ann.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
336 : ann.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
337 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
349 if (colourScheme != null)
351 currentColour = colourScheme.findColour(c, j, seq);
355 if (aj.colour != null)
357 currentColour = aj.colour;
363 currentColour = shadeCalculation(ann, j);
366 if (conservationColouring)
368 currentColour = applyConservation(currentColour, j);
372 return currentColour;
375 private Color shadeCalculation(AlignmentAnnotation ann, int j)
380 if (thresholdIsMinMax && ann.threshold != null
381 && aboveAnnotationThreshold == ABOVE_THRESHOLD
382 && ann.annotations[j].value >= ann.threshold.value)
384 range = (ann.annotations[j].value - ann.threshold.value)
385 / (ann.graphMax - ann.threshold.value);
387 else if (thresholdIsMinMax && ann.threshold != null
388 && aboveAnnotationThreshold == BELOW_THRESHOLD
389 && ann.annotations[j].value >= ann.graphMin)
391 range = (ann.annotations[j].value - ann.graphMin)
392 / (ann.threshold.value - ann.graphMin);
396 if (ann.graphMax != ann.graphMin)
398 range = (ann.annotations[j].value - ann.graphMin)
399 / (ann.graphMax - ann.graphMin);
407 int dr = (int) (redRange * range + redMin);
408 int dg = (int) (greenRange * range + greenMin);
409 int db = (int) (blueRange * range + blueMin);
411 return new Color(dr, dg, db);
415 public boolean isPredefinedColours()
417 return predefinedColours;
420 public void setPredefinedColours(boolean predefinedColours)
422 this.predefinedColours = predefinedColours;
425 public boolean isSeqAssociated()
427 return seqAssociated;
430 public void setSeqAssociated(boolean sassoc)
432 seqAssociated = sassoc;
435 public boolean isThresholdIsMinMax()
437 return thresholdIsMinMax;
440 public void setThresholdIsMinMax(boolean thresholdIsMinMax)
442 this.thresholdIsMinMax = thresholdIsMinMax;