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;
31 import java.awt.Color;
32 import java.util.IdentityHashMap;
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 public AlignmentAnnotation annotation;
45 int aboveAnnotationThreshold = -1;
47 public boolean thresholdIsMinMax = false;
49 GraphLine annotationThreshold;
51 float r1, g1, b1, rr, gg, bb;
53 private boolean predefinedColours = false;
55 private boolean seqAssociated = false;
58 * false if the scheme was constructed without a minColour and maxColour used
59 * to decide if existing colours should be taken from annotation elements when
62 private boolean noGradient = false;
64 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
67 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
68 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
70 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
71 colourScheme, aboveAnnotationThreshold);
72 acg.thresholdIsMinMax = thresholdIsMinMax;
73 acg.annotationThreshold = (annotationThreshold == null) ? null
74 : new GraphLine(annotationThreshold);
81 acg.predefinedColours = predefinedColours;
82 acg.seqAssociated = seqAssociated;
83 acg.noGradient = noGradient;
88 * Creates a new AnnotationColourGradient object.
90 public AnnotationColourGradient(AlignmentAnnotation annotation,
91 ColourSchemeI originalColour, int aboveThreshold)
93 if (originalColour instanceof AnnotationColourGradient)
95 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
99 colourScheme = originalColour;
102 this.annotation = annotation;
104 aboveAnnotationThreshold = aboveThreshold;
106 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
108 annotationThreshold = annotation.threshold;
110 // clear values so we don't get weird black bands...
123 * Creates a new AnnotationColourGradient object.
125 public AnnotationColourGradient(AlignmentAnnotation annotation,
126 Color minColour, Color maxColour, int aboveThreshold)
128 this.annotation = annotation;
130 aboveAnnotationThreshold = aboveThreshold;
132 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
134 annotationThreshold = annotation.threshold;
137 r1 = minColour.getRed();
138 g1 = minColour.getGreen();
139 b1 = minColour.getBlue();
141 rr = maxColour.getRed() - r1;
142 gg = maxColour.getGreen() - g1;
143 bb = maxColour.getBlue() - b1;
149 private void checkLimits()
151 aamax = annotation.graphMax;
152 aamin = annotation.graphMin;
153 if (annotation.isRNA())
155 // reset colour palette
156 ColourSchemeProperty.resetRnaHelicesShading();
157 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
162 public void alignmentChanged(AnnotatedCollectionI alignment,
163 Map<SequenceI, SequenceCollectionI> hiddenReps)
165 super.alignmentChanged(alignment, hiddenReps);
167 if (seqAssociated && annotation.getCalcId() != null)
169 if (seqannot != null)
175 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
177 // resolve the context containing all the annotation for the sequence
178 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
179 : alignment.getContext();
180 boolean f = true, rna = false;
181 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
184 if (alan.sequenceRef != null
185 && (alan.label != null && annotation != null && alan.label
186 .equals(annotation.label)))
188 if (!rna && alan.isRNA())
192 seqannot.put(alan.sequenceRef, alan);
193 if (f || alan.graphMax > aamax)
195 aamax = alan.graphMax;
197 if (f || alan.graphMin < aamin)
199 aamin = alan.graphMin;
206 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
211 float aamin = 0f, aamax = 0f;
213 public String getAnnotation()
215 return annotation.label;
218 public int getAboveThreshold()
220 return aboveAnnotationThreshold;
223 public float getAnnotationThreshold()
225 if (annotationThreshold == null)
231 return annotationThreshold.value;
235 public Color getMinColour()
237 return new Color((int) r1, (int) g1, (int) b1);
240 public Color getMaxColour()
242 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
251 * @return DOCUMENT ME!
253 public Color findColour(char c)
266 * @return DOCUMENT ME!
269 public Color findColour(char c, int j, SequenceI seq)
271 Color currentColour = Color.white;
272 AlignmentAnnotation annotation = (seqAssociated && seqannot != null ? seqannot
273 .get(seq) : this.annotation);
274 if (annotation == null)
276 return currentColour;
278 if ((threshold == 0) || aboveThreshold(c, j))
280 if (annotation.annotations != null
281 && j < annotation.annotations.length
282 && annotation.annotations[j] != null
283 && !jalview.util.Comparison.isGap(c))
285 Annotation aj = annotation.annotations[j];
286 // 'use original colours' => colourScheme != null
287 // -> look up colour to be used
288 // predefined colours => preconfigured shading
289 // -> only use original colours reference if thresholding enabled &
291 // annotation.hasIcons => null or black colours replaced with glyph
293 // -> reuse original colours if present
294 // -> if thresholding enabled then return colour on non-whitespace glyph
296 if (aboveAnnotationThreshold == NO_THRESHOLD
297 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
298 : aj.value <= annotationThreshold.value)))
300 if (predefinedColours && aj.colour != null
301 && !aj.colour.equals(Color.black))
303 currentColour = aj.colour;
305 else if (annotation.hasIcons
306 && annotation.graph == AlignmentAnnotation.NO_GRAPH)
308 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
309 && aj.secondaryStructure != '-')
311 if (colourScheme != null)
313 currentColour = colourScheme.findColour(c, j, seq);
317 if (annotation.isRNA())
319 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
323 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
324 : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
325 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
337 if (colourScheme != null)
339 currentColour = colourScheme.findColour(c, j, seq);
343 if (aj.colour != null)
345 currentColour = aj.colour;
351 currentColour = shadeCalculation(annotation, j);
354 if (conservationColouring)
356 currentColour = applyConservation(currentColour, j);
360 return currentColour;
363 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
368 if (thresholdIsMinMax
369 && annotation.threshold != null
370 && aboveAnnotationThreshold == ABOVE_THRESHOLD
371 && annotation.annotations[j].value >= annotation.threshold.value)
373 range = (annotation.annotations[j].value - annotation.threshold.value)
374 / (annotation.graphMax - annotation.threshold.value);
376 else if (thresholdIsMinMax && annotation.threshold != null
377 && aboveAnnotationThreshold == BELOW_THRESHOLD
378 && annotation.annotations[j].value >= annotation.graphMin)
380 range = (annotation.annotations[j].value - annotation.graphMin)
381 / (annotation.threshold.value - annotation.graphMin);
385 if (annotation.graphMax != annotation.graphMin)
387 range = (annotation.annotations[j].value - annotation.graphMin)
388 / (annotation.graphMax - annotation.graphMin);
396 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
399 return new Color(dr, dg, db);
403 public boolean isPredefinedColours()
405 return predefinedColours;
408 public void setPredefinedColours(boolean predefinedColours)
410 this.predefinedColours = predefinedColours;
413 public boolean isSeqAssociated()
415 return seqAssociated;
418 public void setSeqAssociated(boolean sassoc)
420 seqAssociated = sassoc;