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 getInstance(AnnotatedCollectionI sg,
68 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
70 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
71 getColourScheme(), 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 setColourScheme(((AnnotationColourGradient) originalColour)
100 setColourScheme(originalColour);
103 this.annotation = annotation;
105 aboveAnnotationThreshold = aboveThreshold;
107 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
109 annotationThreshold = annotation.threshold;
111 // clear values so we don't get weird black bands...
124 * Creates a new AnnotationColourGradient object.
126 public AnnotationColourGradient(AlignmentAnnotation annotation,
127 Color minColour, Color maxColour, int aboveThreshold)
129 this.annotation = annotation;
131 aboveAnnotationThreshold = aboveThreshold;
133 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
135 annotationThreshold = annotation.threshold;
138 r1 = minColour.getRed();
139 g1 = minColour.getGreen();
140 b1 = minColour.getBlue();
142 rr = maxColour.getRed() - r1;
143 gg = maxColour.getGreen() - g1;
144 bb = maxColour.getBlue() - b1;
150 private void checkLimits()
152 aamax = annotation.graphMax;
153 aamin = annotation.graphMin;
154 if (annotation.isRNA())
156 // reset colour palette
157 ColourSchemeProperty.resetRnaHelicesShading();
158 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
163 public void alignmentChanged(AnnotatedCollectionI alignment,
164 Map<SequenceI, SequenceCollectionI> hiddenReps)
166 super.alignmentChanged(alignment, hiddenReps);
168 if (seqAssociated && annotation.getCalcId() != null)
170 if (seqannot != null)
176 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
178 // resolve the context containing all the annotation for the sequence
179 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
180 : alignment.getContext();
181 boolean f = true, rna = false;
182 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
185 if (alan.sequenceRef != null
186 && (alan.label != null && annotation != null && alan.label
187 .equals(annotation.label)))
189 if (!rna && alan.isRNA())
193 seqannot.put(alan.sequenceRef, alan);
194 if (f || alan.graphMax > aamax)
196 aamax = alan.graphMax;
198 if (f || alan.graphMin < aamin)
200 aamin = alan.graphMin;
207 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
212 float aamin = 0f, aamax = 0f;
214 public String getAnnotation()
216 return annotation.label;
219 public int getAboveThreshold()
221 return aboveAnnotationThreshold;
224 public float getAnnotationThreshold()
226 if (annotationThreshold == null)
232 return annotationThreshold.value;
236 public Color getMinColour()
238 return new Color((int) r1, (int) g1, (int) b1);
241 public Color getMaxColour()
243 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
252 * @return DOCUMENT ME!
255 public Color findColour(char c)
268 * @return DOCUMENT ME!
271 public Color findColour(char c, int j, SequenceI seq)
273 Color currentColour = Color.white;
274 AlignmentAnnotation annotation = (seqAssociated && seqannot != null ? seqannot
275 .get(seq) : this.annotation);
276 if (annotation == null)
278 return currentColour;
280 // if ((threshold == 0) || aboveThreshold(c, j))
282 if (annotation.annotations != null && j < annotation.annotations.length
283 && annotation.annotations[j] != null
284 && !jalview.util.Comparison.isGap(c))
286 Annotation aj = annotation.annotations[j];
287 // 'use original colours' => colourScheme != null
288 // -> look up colour to be used
289 // predefined colours => preconfigured shading
290 // -> only use original colours reference if thresholding enabled &
292 // annotation.hasIcons => null or black colours replaced with glyph
294 // -> reuse original colours if present
295 // -> if thresholding enabled then return colour on non-whitespace glyph
297 if (aboveAnnotationThreshold == NO_THRESHOLD
298 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
299 : aj.value <= annotationThreshold.value)))
301 if (predefinedColours && aj.colour != null
302 && !aj.colour.equals(Color.black))
304 currentColour = aj.colour;
306 else if (annotation.hasIcons
307 && annotation.graph == AlignmentAnnotation.NO_GRAPH)
309 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
310 && aj.secondaryStructure != '-')
312 if (getColourScheme() != null)
314 currentColour = getColourScheme().findColour(c, j, seq, null,
319 if (annotation.isRNA())
321 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
325 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
326 : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
327 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
339 if (getColourScheme() != null)
341 currentColour = getColourScheme().findColour(c, j, seq, null,
346 if (aj.colour != null)
348 currentColour = aj.colour;
354 currentColour = shadeCalculation(annotation, j);
357 // if (conservationColouring)
359 // currentColour = applyConservation(currentColour, j);
363 return currentColour;
366 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
371 if (thresholdIsMinMax
372 && annotation.threshold != null
373 && aboveAnnotationThreshold == ABOVE_THRESHOLD
374 && annotation.annotations[j].value >= annotation.threshold.value)
376 range = (annotation.annotations[j].value - annotation.threshold.value)
377 / (annotation.graphMax - annotation.threshold.value);
379 else if (thresholdIsMinMax && annotation.threshold != null
380 && aboveAnnotationThreshold == BELOW_THRESHOLD
381 && annotation.annotations[j].value >= annotation.graphMin)
383 range = (annotation.annotations[j].value - annotation.graphMin)
384 / (annotation.threshold.value - annotation.graphMin);
388 if (annotation.graphMax != annotation.graphMin)
390 range = (annotation.annotations[j].value - annotation.graphMin)
391 / (annotation.graphMax - annotation.graphMin);
399 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
402 return new Color(dr, dg, db);
406 public boolean isPredefinedColours()
408 return predefinedColours;
411 public void setPredefinedColours(boolean predefinedColours)
413 this.predefinedColours = predefinedColours;
416 public boolean isSeqAssociated()
418 return seqAssociated;
421 public void setSeqAssociated(boolean sassoc)
423 seqAssociated = sassoc;
427 public String getSchemeName()
433 public boolean isSimple()