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;
57 * false if the scheme was constructed without a minColour and maxColour used
58 * to decide if existing colours should be taken from annotation elements when
61 private boolean noGradient = false;
62 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
65 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
66 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
68 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
69 colourScheme, aboveAnnotationThreshold);
70 acg.thresholdIsMinMax = thresholdIsMinMax;
71 acg.annotationThreshold = (annotationThreshold == null) ? null
72 : new GraphLine(annotationThreshold);
79 acg.predefinedColours = predefinedColours;
80 acg.seqAssociated = seqAssociated;
81 acg.noGradient = noGradient;
86 * Creates a new AnnotationColourGradient object.
88 public AnnotationColourGradient(AlignmentAnnotation annotation,
89 ColourSchemeI originalColour, int aboveThreshold)
91 if (originalColour instanceof AnnotationColourGradient)
93 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
97 colourScheme = originalColour;
100 this.annotation = annotation;
102 aboveAnnotationThreshold = aboveThreshold;
104 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
106 annotationThreshold = annotation.threshold;
108 // clear values so we don't get weird black bands...
120 * Creates a new AnnotationColourGradient object.
122 public AnnotationColourGradient(AlignmentAnnotation annotation,
123 Color minColour, Color maxColour, int aboveThreshold)
125 this.annotation = annotation;
127 aboveAnnotationThreshold = aboveThreshold;
129 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
131 annotationThreshold = annotation.threshold;
134 r1 = minColour.getRed();
135 g1 = minColour.getGreen();
136 b1 = minColour.getBlue();
138 rr = maxColour.getRed() - r1;
139 gg = maxColour.getGreen() - g1;
140 bb = maxColour.getBlue() - b1;
143 aamax = annotation.graphMax;
144 aamin = annotation.graphMin;
145 if (annotation.isRNA())
147 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
152 public void alignmentChanged(AnnotatedCollectionI alignment,
153 Map<SequenceI, SequenceCollectionI> hiddenReps)
155 super.alignmentChanged(alignment, hiddenReps);
157 if (seqAssociated && annotation.getCalcId() != null)
159 if (seqannot != null)
165 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
167 // resolve the context containing all the annotation for the sequence
168 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
169 : alignment.getContext();
170 boolean f = true,rna=false;
171 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
174 if (alan.sequenceRef != null
175 && (alan.label != null && annotation != null && alan.label
176 .equals(annotation.label)))
178 if (!rna && alan.isRNA())
182 seqannot.put(alan.sequenceRef, alan);
183 if (f || alan.graphMax > aamax)
185 aamax = alan.graphMax;
187 if (f || alan.graphMin < aamin)
189 aamin = alan.graphMin;
196 ColourSchemeProperty.resetRnaHelicesShading();
197 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
202 float aamin = 0f, aamax = 0f;
203 public String getAnnotation()
205 return annotation.label;
208 public int getAboveThreshold()
210 return aboveAnnotationThreshold;
213 public float getAnnotationThreshold()
215 if (annotationThreshold == null)
221 return annotationThreshold.value;
225 public Color getMinColour()
227 return new Color((int) r1, (int) g1, (int) b1);
230 public Color getMaxColour()
232 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
241 * @return DOCUMENT ME!
243 public Color findColour(char c)
256 * @return DOCUMENT ME!
259 public Color findColour(char c, int j, SequenceI seq)
261 Color currentColour = Color.white;
262 AlignmentAnnotation annotation = (seqAssociated && seqannot!=null ? seqannot.get(seq)
264 if (annotation == null)
266 return currentColour;
268 if ((threshold == 0) || aboveThreshold(c, j))
270 if (annotation.annotations != null
271 && j < annotation.annotations.length
272 && annotation.annotations[j] != null
273 && !jalview.util.Comparison.isGap(c))
275 Annotation aj = annotation.annotations[j];
276 // 'use original colours' => colourScheme != null
277 // -> look up colour to be used
278 // predefined colours => preconfigured shading
279 // -> only use original colours reference if thresholding enabled &
281 // annotation.hasIcons => null or black colours replaced with glyph
283 // -> reuse original colours if present
284 // -> if thresholding enabled then return colour on non-whitespace glyph
286 if (aboveAnnotationThreshold == NO_THRESHOLD
287 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
288 : aj.value <= annotationThreshold.value)))
290 if (predefinedColours && aj.colour != null
291 && !aj.colour.equals(Color.black))
293 currentColour = aj.colour;
295 else if (annotation.hasIcons
296 && annotation.graph == AlignmentAnnotation.NO_GRAPH)
298 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
299 && aj.secondaryStructure != '-')
301 if (colourScheme != null)
303 currentColour = colourScheme.findColour(c, j, seq);
307 if (annotation.isRNA())
309 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
313 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
314 : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
315 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
327 if (colourScheme != null)
329 currentColour = colourScheme.findColour(c, j, seq);
333 if (aj.colour != null)
335 currentColour = aj.colour;
341 currentColour = shadeCalculation(annotation, j);
344 if (conservationColouring)
346 currentColour = applyConservation(currentColour, j);
350 return currentColour;
353 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
358 if (thresholdIsMinMax
359 && annotation.threshold != null
360 && aboveAnnotationThreshold == ABOVE_THRESHOLD
361 && annotation.annotations[j].value >= annotation.threshold.value)
363 range = (annotation.annotations[j].value - annotation.threshold.value)
364 / (annotation.graphMax - annotation.threshold.value);
366 else if (thresholdIsMinMax && annotation.threshold != null
367 && aboveAnnotationThreshold == BELOW_THRESHOLD
368 && annotation.annotations[j].value >= annotation.graphMin)
370 range = (annotation.annotations[j].value - annotation.graphMin)
371 / (annotation.threshold.value - annotation.graphMin);
375 if (annotation.graphMax != annotation.graphMin)
377 range = (annotation.annotations[j].value - annotation.graphMin)
378 / (annotation.graphMax - annotation.graphMin);
386 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
389 return new Color(dr, dg, db);
392 public boolean isPredefinedColours()
394 return predefinedColours;
397 public void setPredefinedColours(boolean predefinedColours)
399 this.predefinedColours = predefinedColours;
402 public boolean isSeqAssociated()
404 return seqAssociated;
407 public void setSeqAssociated(boolean sassoc)
409 seqAssociated = sassoc;