2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
56 Color rnaHelices[] = null;
57 private void initRnaHelicesShading(int n)
62 rnaHelices = new Color[n + 1];
65 if (rnaHelices != null && rnaHelices.length <= n)
67 Color[] t = new Color[n + 1];
68 System.arraycopy(rnaHelices, 0, t, 0, rnaHelices.length);
69 j = rnaHelices.length;
76 // Generate random colors and store
79 rnaHelices[j] = jalview.util.ColorUtils
80 .generateRandomColor(Color.white);
84 * false if the scheme was constructed without a minColour and maxColour used
85 * to decide if existing colours should be taken from annotation elements when
88 private boolean noGradient = false;
89 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
92 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
93 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
95 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
96 colourScheme, aboveAnnotationThreshold);
97 acg.thresholdIsMinMax = thresholdIsMinMax;
98 acg.annotationThreshold = (annotationThreshold == null) ? null
99 : new GraphLine(annotationThreshold);
106 acg.predefinedColours = predefinedColours;
107 acg.seqAssociated = seqAssociated;
108 acg.noGradient = noGradient;
113 * Creates a new AnnotationColourGradient object.
115 public AnnotationColourGradient(AlignmentAnnotation annotation,
116 ColourSchemeI originalColour, int aboveThreshold)
118 if (originalColour instanceof AnnotationColourGradient)
120 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
124 colourScheme = originalColour;
127 this.annotation = annotation;
129 aboveAnnotationThreshold = aboveThreshold;
131 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
133 annotationThreshold = annotation.threshold;
135 // clear values so we don't get weird black bands...
147 * Creates a new AnnotationColourGradient object.
149 public AnnotationColourGradient(AlignmentAnnotation annotation,
150 Color minColour, Color maxColour, int aboveThreshold)
152 this.annotation = annotation;
154 aboveAnnotationThreshold = aboveThreshold;
156 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
158 annotationThreshold = annotation.threshold;
161 r1 = minColour.getRed();
162 g1 = minColour.getGreen();
163 b1 = minColour.getBlue();
165 rr = maxColour.getRed() - r1;
166 gg = maxColour.getGreen() - g1;
167 bb = maxColour.getBlue() - b1;
170 aamax = annotation.graphMax;
171 aamin = annotation.graphMin;
172 if (annotation.isRNA())
174 initRnaHelicesShading(1 + (int) aamax);
179 public void alignmentChanged(AnnotatedCollectionI alignment,
180 Map<SequenceI, SequenceCollectionI> hiddenReps)
182 super.alignmentChanged(alignment, hiddenReps);
184 if (seqAssociated && annotation.getCalcId() != null)
186 if (seqannot != null)
192 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
194 // resolve the context containing all the annotation for the sequence
195 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
196 : alignment.getContext();
197 boolean f = true,rna=false;
198 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
201 if (alan.sequenceRef != null
202 && (alan.label != null && annotation != null && alan.label
203 .equals(annotation.label)))
205 if (!rna && alan.isRNA())
209 seqannot.put(alan.sequenceRef, alan);
210 if (f || alan.graphMax > aamax)
212 aamax = alan.graphMax;
214 if (f || alan.graphMin < aamin)
216 aamin = alan.graphMin;
223 initRnaHelicesShading(1 + (int) aamax);
228 float aamin = 0f, aamax = 0f;
229 public String getAnnotation()
231 return annotation.label;
234 public int getAboveThreshold()
236 return aboveAnnotationThreshold;
239 public float getAnnotationThreshold()
241 if (annotationThreshold == null)
247 return annotationThreshold.value;
251 public Color getMinColour()
253 return new Color((int) r1, (int) g1, (int) b1);
256 public Color getMaxColour()
258 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
267 * @return DOCUMENT ME!
269 public Color findColour(char c)
282 * @return DOCUMENT ME!
285 public Color findColour(char c, int j, SequenceI seq)
287 Color currentColour = Color.white;
288 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
290 if (annotation == null)
292 return currentColour;
294 if ((threshold == 0) || aboveThreshold(c, j))
296 if (annotation.annotations != null
297 && j < annotation.annotations.length
298 && annotation.annotations[j] != null
299 && !jalview.util.Comparison.isGap(c))
301 Annotation aj = annotation.annotations[j];
302 // 'use original colours' => colourScheme != null
303 // -> look up colour to be used
304 // predefined colours => preconfigured shading
305 // -> only use original colours reference if thresholding enabled &
307 // annotation.hasIcons => null or black colours replaced with glyph
309 // -> reuse original colours if present
310 // -> if thresholding enabled then return colour on non-whitespace glyph
312 if (aboveAnnotationThreshold == NO_THRESHOLD
313 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
314 : aj.value <= annotationThreshold.value)))
316 if (predefinedColours && aj.colour != null)
318 currentColour = aj.colour;
320 else if (annotation.hasIcons
321 && annotation.graph == AlignmentAnnotation.NO_GRAPH)
323 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
324 && aj.secondaryStructure != '-')
326 if (colourScheme != null)
328 currentColour = colourScheme.findColour(c, j, seq);
332 if (annotation.isRNA())
334 currentColour = rnaHelices[(int) aj.value];
338 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
339 : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
340 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
352 if (colourScheme != null)
354 currentColour = colourScheme.findColour(c, j, seq);
358 if (aj.colour != null)
360 currentColour = aj.colour;
366 currentColour = shadeCalculation(annotation, j);
369 if (conservationColouring)
371 currentColour = applyConservation(currentColour, j);
375 return currentColour;
378 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
383 if (thresholdIsMinMax
384 && annotation.threshold != null
385 && aboveAnnotationThreshold == ABOVE_THRESHOLD
386 && annotation.annotations[j].value >= annotation.threshold.value)
388 range = (annotation.annotations[j].value - annotation.threshold.value)
389 / (annotation.graphMax - annotation.threshold.value);
391 else if (thresholdIsMinMax && annotation.threshold != null
392 && aboveAnnotationThreshold == BELOW_THRESHOLD
393 && annotation.annotations[j].value >= annotation.graphMin)
395 range = (annotation.annotations[j].value - annotation.graphMin)
396 / (annotation.threshold.value - annotation.graphMin);
400 if (annotation.graphMax != annotation.graphMin)
402 range = (annotation.annotations[j].value - annotation.graphMin)
403 / (annotation.graphMax - annotation.graphMin);
411 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
414 return new Color(dr, dg, db);
417 public boolean isPredefinedColours()
419 return predefinedColours;
422 public void setPredefinedColours(boolean predefinedColours)
424 this.predefinedColours = predefinedColours;
427 public boolean isSeqAssociated()
429 return seqAssociated;
432 public void setSeqAssociated(boolean sassoc)
434 seqAssociated = sassoc;