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 public AlignmentAnnotation annotation;
47 int aboveAnnotationThreshold = -1;
49 public boolean thresholdIsMinMax = false;
51 GraphLine annotationThreshold;
53 float r1, g1, b1, rr, gg, bb;
55 private boolean predefinedColours = false;
57 private boolean seqAssociated = false;
59 * false if the scheme was constructed without a minColour and maxColour used
60 * to decide if existing colours should be taken from annotation elements when
63 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...
122 * Creates a new AnnotationColourGradient object.
124 public AnnotationColourGradient(AlignmentAnnotation annotation,
125 Color minColour, Color maxColour, int aboveThreshold)
127 this.annotation = annotation;
129 aboveAnnotationThreshold = aboveThreshold;
131 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
133 annotationThreshold = annotation.threshold;
136 r1 = minColour.getRed();
137 g1 = minColour.getGreen();
138 b1 = minColour.getBlue();
140 rr = maxColour.getRed() - r1;
141 gg = maxColour.getGreen() - g1;
142 bb = maxColour.getBlue() - b1;
145 aamax = annotation.graphMax;
146 aamin = annotation.graphMin;
147 if (annotation.isRNA())
149 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
154 public void alignmentChanged(AnnotatedCollectionI alignment,
155 Map<SequenceI, SequenceCollectionI> hiddenReps)
157 super.alignmentChanged(alignment, hiddenReps);
159 if (seqAssociated && annotation.getCalcId() != null)
161 if (seqannot != null)
167 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
169 // resolve the context containing all the annotation for the sequence
170 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
171 : alignment.getContext();
172 boolean f = true,rna=false;
173 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
176 if (alan.sequenceRef != null
177 && (alan.label != null && annotation != null && alan.label
178 .equals(annotation.label)))
180 if (!rna && alan.isRNA())
184 seqannot.put(alan.sequenceRef, alan);
185 if (f || alan.graphMax > aamax)
187 aamax = alan.graphMax;
189 if (f || alan.graphMin < aamin)
191 aamin = alan.graphMin;
198 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
203 float aamin = 0f, aamax = 0f;
204 public String getAnnotation()
206 return annotation.label;
209 public int getAboveThreshold()
211 return aboveAnnotationThreshold;
214 public float getAnnotationThreshold()
216 if (annotationThreshold == null)
222 return annotationThreshold.value;
226 public Color getMinColour()
228 return new Color((int) r1, (int) g1, (int) b1);
231 public Color getMaxColour()
233 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
242 * @return DOCUMENT ME!
244 public Color findColour(char c)
257 * @return DOCUMENT ME!
260 public Color findColour(char c, int j, SequenceI seq) {
261 Color currentColour = Color.white;
262 AlignmentAnnotation annotation = (seqAssociated && seqannot != null ? seqannot
263 .get(seq) : this.annotation);
264 if (annotation == null) {
265 return currentColour;
267 if ((threshold == 0) || aboveThreshold(c, j)) {
268 if (annotation.annotations != null && j < annotation.annotations.length
269 && annotation.annotations[j] != null && !Comparison.isGap(c)) {
270 Annotation aj = annotation.annotations[j];
271 // 'use original colours' => colourScheme != null
272 // -> look up colour to be used
273 // predefined colours => preconfigured shading
274 // -> only use original colours reference if thresholding enabled &
276 // annotation.hasIcons => null or black colours replaced with glyph
278 // -> reuse original colours if present
279 // -> if thresholding enabled then return colour on non-whitespace glyph
281 if (aboveAnnotationThreshold == NO_THRESHOLD
282 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
283 : aj.value <= annotationThreshold.value))) {
284 if (predefinedColours && aj.colour != null
285 && !aj.colour.equals(Color.black)) {
286 currentColour = aj.colour;
287 } else if (annotation.hasIcons
288 && annotation.graph == AlignmentAnnotation.NO_GRAPH) {
289 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
290 && aj.secondaryStructure != '-') {
291 if (colourScheme != null) {
292 currentColour = colourScheme.findColour(c, j, seq);
294 if (annotation.isRNA()) {
295 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
297 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? AnnotationRenderer.HELIX_COLOUR
298 : annotation.annotations[j].secondaryStructure == 'E' ? AnnotationRenderer.SHEET_COLOUR
299 : AnnotationRenderer.STEM_COLOUR;
306 } else if (noGradient) {
307 if (colourScheme != null) {
308 currentColour = colourScheme.findColour(c, j, seq);
310 if (aj.colour != null) {
311 currentColour = aj.colour;
315 currentColour = shadeCalculation(annotation, j);
318 if (conservationColouring) {
319 currentColour = applyConservation(currentColour, j);
323 return currentColour;
326 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
331 if (thresholdIsMinMax
332 && annotation.threshold != null
333 && aboveAnnotationThreshold == ABOVE_THRESHOLD
334 && annotation.annotations[j].value >= annotation.threshold.value)
336 range = (annotation.annotations[j].value - annotation.threshold.value)
337 / (annotation.graphMax - annotation.threshold.value);
339 else if (thresholdIsMinMax && annotation.threshold != null
340 && aboveAnnotationThreshold == BELOW_THRESHOLD
341 && annotation.annotations[j].value >= annotation.graphMin)
343 range = (annotation.annotations[j].value - annotation.graphMin)
344 / (annotation.threshold.value - annotation.graphMin);
348 if (annotation.graphMax != annotation.graphMin)
350 range = (annotation.annotations[j].value - annotation.graphMin)
351 / (annotation.graphMax - annotation.graphMin);
359 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
362 return new Color(dr, dg, db);
365 public boolean isPredefinedColours()
367 return predefinedColours;
370 public void setPredefinedColours(boolean predefinedColours)
372 this.predefinedColours = predefinedColours;
375 public boolean isSeqAssociated()
377 return seqAssociated;
380 public void setSeqAssociated(boolean sassoc)
382 seqAssociated = sassoc;