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.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.GraphLine;
28 import jalview.datamodel.SequenceCollectionI;
29 import jalview.datamodel.SequenceI;
30 import jalview.renderer.AnnotationRenderer;
31 import jalview.util.Comparison;
33 import java.awt.Color;
34 import java.util.IdentityHashMap;
37 public class AnnotationColourGradient extends FollowerColourScheme
39 public static final int NO_THRESHOLD = -1;
41 public static final int BELOW_THRESHOLD = 0;
43 public static final int ABOVE_THRESHOLD = 1;
45 private final AlignmentAnnotation annotation;
47 private final int aboveAnnotationThreshold;
49 public boolean thresholdIsMinMax = false;
51 private GraphLine annotationThreshold;
61 private int greenRange;
63 private int blueRange;
65 private boolean predefinedColours = false;
67 private boolean seqAssociated = false;
70 * false if the scheme was constructed without a minColour and maxColour used
71 * to decide if existing colours should be taken from annotation elements when
74 private boolean noGradient = false;
76 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
79 public ColourSchemeI getInstance(AnnotatedCollectionI sg,
80 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
82 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
83 getColourScheme(), aboveAnnotationThreshold);
84 acg.thresholdIsMinMax = thresholdIsMinMax;
85 acg.annotationThreshold = (annotationThreshold == null) ? null
86 : new GraphLine(annotationThreshold);
88 acg.greenMin = greenMin;
89 acg.blueMin = blueMin;
90 acg.redRange = redRange;
91 acg.greenRange = greenRange;
92 acg.blueRange = blueRange;
93 acg.predefinedColours = predefinedColours;
94 acg.seqAssociated = seqAssociated;
95 acg.noGradient = noGradient;
100 * Creates a new AnnotationColourGradient object.
102 public AnnotationColourGradient(AlignmentAnnotation annotation,
103 ColourSchemeI originalColour, int aboveThreshold)
105 if (originalColour instanceof AnnotationColourGradient)
107 setColourScheme(((AnnotationColourGradient) originalColour)
112 setColourScheme(originalColour);
115 this.annotation = annotation;
117 aboveAnnotationThreshold = aboveThreshold;
119 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
121 annotationThreshold = annotation.threshold;
123 // clear values so we don't get weird black bands...
136 * Creates a new AnnotationColourGradient object.
138 public AnnotationColourGradient(AlignmentAnnotation annotation,
139 Color minColour, Color maxColour, int aboveThreshold)
141 this.annotation = annotation;
143 aboveAnnotationThreshold = aboveThreshold;
145 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
147 annotationThreshold = annotation.threshold;
150 redMin = minColour.getRed();
151 greenMin = minColour.getGreen();
152 blueMin = minColour.getBlue();
154 redRange = maxColour.getRed() - redMin;
155 greenRange = maxColour.getGreen() - greenMin;
156 blueRange = maxColour.getBlue() - blueMin;
162 private void checkLimits()
164 aamax = annotation.graphMax;
165 aamin = annotation.graphMin;
166 if (annotation.isRNA())
168 // reset colour palette
169 ColourSchemeProperty.resetRnaHelicesShading();
170 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
175 public void alignmentChanged(AnnotatedCollectionI alignment,
176 Map<SequenceI, SequenceCollectionI> hiddenReps)
178 super.alignmentChanged(alignment, hiddenReps);
180 if (seqAssociated && annotation.getCalcId() != null)
182 if (seqannot != null)
188 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
190 // resolve the context containing all the annotation for the sequence
191 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
192 : alignment.getContext();
193 boolean f = true, rna = false;
194 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
197 if (alan.sequenceRef != null
198 && (alan.label != null && annotation != null && alan.label
199 .equals(annotation.label)))
201 if (!rna && alan.isRNA())
205 seqannot.put(alan.sequenceRef, alan);
206 if (f || alan.graphMax > aamax)
208 aamax = alan.graphMax;
210 if (f || alan.graphMin < aamin)
212 aamin = alan.graphMin;
219 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
224 float aamin = 0f, aamax = 0f;
226 public AlignmentAnnotation getAnnotation()
231 public int getAboveThreshold()
233 return aboveAnnotationThreshold;
236 public float getAnnotationThreshold()
238 if (annotationThreshold == null)
244 return annotationThreshold.value;
248 public Color getMinColour()
250 return new Color(redMin, greenMin, blueMin);
253 public Color getMaxColour()
255 return new Color(redMin + redRange, greenMin + greenRange, blueMin
265 * @return DOCUMENT ME!
268 public Color findColour(char c)
274 * Returns the colour for a given character and position in a sequence
277 * the residue character
279 * the aligned position
285 public Color findColour(char c, int j, SequenceI seq)
288 * locate the annotation we are configured to colour by
290 AlignmentAnnotation ann = (seqAssociated && seqannot != null ? seqannot
291 .get(seq) : this.annotation);
294 * if gap or no annotation at position, no colour (White)
296 if (ann == null || ann.annotations == null
297 || j >= ann.annotations.length || ann.annotations[j] == null
298 || Comparison.isGap(c))
303 Annotation aj = ann.annotations[j];
304 // 'use original colours' => colourScheme != null
305 // -> look up colour to be used
306 // predefined colours => preconfigured shading
307 // -> only use original colours reference if thresholding enabled &
309 // annotation.hasIcons => null or black colours replaced with glyph
311 // -> reuse original colours if present
312 // -> if thresholding enabled then return colour on non-whitespace glyph
315 * if threshold applies, and annotation fails the test - no colour (white)
317 if (annotationThreshold != null)
319 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD && aj.value < annotationThreshold.value)
320 || (aboveAnnotationThreshold == BELOW_THRESHOLD && aj.value > annotationThreshold.value))
327 * If 'use original colours' then return the colour of the annotation
328 * at the aligned position - computed using the background colour scheme
330 if (predefinedColours && aj.colour != null
331 && !aj.colour.equals(Color.black))
336 Color result = Color.white;
337 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
340 * secondary structure symbol colouring
342 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
343 && aj.secondaryStructure != '-')
345 if (getColourScheme() != null)
347 result = getColourScheme().findColour(c, j, seq, null, 0f);
353 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
357 result = ann.annotations[j].secondaryStructure == 'H' ? AnnotationRenderer.HELIX_COLOUR
358 : ann.annotations[j].secondaryStructure == 'E' ? AnnotationRenderer.SHEET_COLOUR
359 : AnnotationRenderer.STEM_COLOUR;
370 if (getColourScheme() != null)
372 result = getColourScheme().findColour(c, j, seq, null, 0f);
376 if (aj.colour != null)
384 result = shadeCalculation(ann, j);
391 * Returns a graduated colour for the annotation at the given column. If there
392 * is a threshold value, and it is used as the top/bottom of the colour range,
393 * and the value satisfies the threshold condition, then a colour
394 * proportionate to the range from the threshold is calculated. For all other
395 * cases, a colour proportionate to the annotation's min-max range is
396 * calulated. Note that thresholding is _not_ done here (a colour is computed
397 * even if threshold is not passed).
403 Color shadeCalculation(AlignmentAnnotation ann, int col)
406 float value = ann.annotations[col].value;
407 if (thresholdIsMinMax && ann.threshold != null
408 && aboveAnnotationThreshold == ABOVE_THRESHOLD
409 && value >= ann.threshold.value)
411 range = (value - ann.threshold.value)
412 / (ann.graphMax - ann.threshold.value);
414 else if (thresholdIsMinMax && ann.threshold != null
415 && aboveAnnotationThreshold == BELOW_THRESHOLD
416 && value <= ann.threshold.value)
418 range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
422 if (ann.graphMax != ann.graphMin)
424 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
432 int dr = (int) (redRange * range + redMin);
433 int dg = (int) (greenRange * range + greenMin);
434 int db = (int) (blueRange * range + blueMin);
436 return new Color(dr, dg, db);
439 public boolean isPredefinedColours()
441 return predefinedColours;
444 public void setPredefinedColours(boolean predefinedColours)
446 this.predefinedColours = predefinedColours;
449 public boolean isSeqAssociated()
451 return seqAssociated;
454 public void setSeqAssociated(boolean sassoc)
456 seqAssociated = sassoc;
459 public boolean isThresholdIsMinMax()
461 return thresholdIsMinMax;
464 public void setThresholdIsMinMax(boolean minMax)
466 this.thresholdIsMinMax = minMax;
470 public String getSchemeName()
476 public boolean isSimple()