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
193 : alignment.getContext();
194 boolean f = true, rna = false;
195 for (AlignmentAnnotation alan : alcontext
196 .findAnnotation(annotation.getCalcId()))
198 if (alan.sequenceRef != null
199 && (alan.label != null && annotation != null
200 && alan.label.equals(annotation.label)))
202 if (!rna && alan.isRNA())
206 seqannot.put(alan.sequenceRef, alan);
207 if (f || alan.graphMax > aamax)
209 aamax = alan.graphMax;
211 if (f || alan.graphMin < aamin)
213 aamin = alan.graphMin;
220 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
225 float aamin = 0f, aamax = 0f;
227 public AlignmentAnnotation getAnnotation()
232 public int getAboveThreshold()
234 return aboveAnnotationThreshold;
237 public float getAnnotationThreshold()
239 if (annotationThreshold == null)
245 return annotationThreshold.value;
249 public Color getMinColour()
251 return new Color(redMin, greenMin, blueMin);
254 public Color getMaxColour()
256 return new Color(redMin + redRange, greenMin + greenRange,
257 blueMin + blueRange);
266 * @return DOCUMENT ME!
269 public Color findColour(char c)
275 * Returns the colour for a given character and position in a sequence
278 * the residue character
280 * the aligned position
286 public Color findColour(char c, int j, SequenceI seq)
289 * locate the annotation we are configured to colour by
291 AlignmentAnnotation ann = (seqAssociated && seqannot != null
296 * if gap or no annotation at position, no colour (White)
298 if (ann == null || ann.annotations == null
299 || j >= ann.annotations.length || ann.annotations[j] == null
300 || Comparison.isGap(c))
305 Annotation aj = ann.annotations[j];
306 // 'use original colours' => colourScheme != null
307 // -> look up colour to be used
308 // predefined colours => preconfigured shading
309 // -> only use original colours reference if thresholding enabled &
311 // annotation.hasIcons => null or black colours replaced with glyph
313 // -> reuse original colours if present
314 // -> if thresholding enabled then return colour on non-whitespace glyph
317 * if threshold applies, and annotation fails the test - no colour (white)
319 if (annotationThreshold != null)
321 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
322 && aj.value < annotationThreshold.value)
323 || (aboveAnnotationThreshold == BELOW_THRESHOLD
324 && aj.value > annotationThreshold.value))
331 * If 'use original colours' then return the colour of the annotation
332 * at the aligned position - computed using the background colour scheme
334 if (predefinedColours && aj.colour != null
335 && !aj.colour.equals(Color.black))
340 Color result = Color.white;
341 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
344 * secondary structure symbol colouring
346 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
347 && aj.secondaryStructure != '-')
349 if (getColourScheme() != null)
351 result = getColourScheme().findColour(c, j, seq, null, 0f);
357 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
361 result = ann.annotations[j].secondaryStructure == 'H'
362 ? AnnotationRenderer.HELIX_COLOUR
363 : ann.annotations[j].secondaryStructure == 'E'
364 ? AnnotationRenderer.SHEET_COLOUR
365 : AnnotationRenderer.STEM_COLOUR;
376 if (getColourScheme() != null)
378 result = getColourScheme().findColour(c, j, seq, null, 0f);
382 if (aj.colour != null)
390 result = shadeCalculation(ann, j);
397 * Returns a graduated colour for the annotation at the given column. If there
398 * is a threshold value, and it is used as the top/bottom of the colour range,
399 * and the value satisfies the threshold condition, then a colour
400 * proportionate to the range from the threshold is calculated. For all other
401 * cases, a colour proportionate to the annotation's min-max range is
402 * calulated. Note that thresholding is _not_ done here (a colour is computed
403 * even if threshold is not passed).
409 Color shadeCalculation(AlignmentAnnotation ann, int col)
412 float value = ann.annotations[col].value;
413 if (thresholdIsMinMax && ann.threshold != null
414 && aboveAnnotationThreshold == ABOVE_THRESHOLD
415 && value >= ann.threshold.value)
417 range = (value - ann.threshold.value)
418 / (ann.graphMax - ann.threshold.value);
420 else if (thresholdIsMinMax && ann.threshold != null
421 && aboveAnnotationThreshold == BELOW_THRESHOLD
422 && value <= ann.threshold.value)
424 range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
428 if (ann.graphMax != ann.graphMin)
430 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
438 int dr = (int) (redRange * range + redMin);
439 int dg = (int) (greenRange * range + greenMin);
440 int db = (int) (blueRange * range + blueMin);
442 return new Color(dr, dg, db);
445 public boolean isPredefinedColours()
447 return predefinedColours;
450 public void setPredefinedColours(boolean predefinedColours)
452 this.predefinedColours = predefinedColours;
455 public boolean isSeqAssociated()
457 return seqAssociated;
460 public void setSeqAssociated(boolean sassoc)
462 seqAssociated = sassoc;
465 public boolean isThresholdIsMinMax()
467 return thresholdIsMinMax;
470 public void setThresholdIsMinMax(boolean minMax)
472 this.thresholdIsMinMax = minMax;
476 public String getSchemeName()
482 public boolean isSimple()