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
38 * map positional scores to transparency rather than colour
40 boolean positionToTransparency = true;
43 * compute shade based on annotation row score
45 boolean perLineScore = true;
47 public static final int NO_THRESHOLD = -1;
49 public static final int BELOW_THRESHOLD = 0;
51 public static final int ABOVE_THRESHOLD = 1;
53 public AlignmentAnnotation annotation;
55 int aboveAnnotationThreshold = -1;
57 public boolean thresholdIsMinMax = false;
59 GraphLine annotationThreshold;
61 float r1, g1, b1, rr, gg, bb;
63 private boolean predefinedColours = false;
65 private boolean seqAssociated = false;
68 * false if the scheme was constructed without a minColour and maxColour used
69 * to decide if existing colours should be taken from annotation elements when
72 private boolean noGradient = false;
74 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
77 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
78 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
80 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
81 colourScheme, aboveAnnotationThreshold);
82 acg.thresholdIsMinMax = thresholdIsMinMax;
83 acg.annotationThreshold = (annotationThreshold == null) ? null
84 : new GraphLine(annotationThreshold);
91 acg.predefinedColours = predefinedColours;
92 acg.seqAssociated = seqAssociated;
93 acg.noGradient = noGradient;
94 acg.positionToTransparency = positionToTransparency;
95 acg.perLineScore = perLineScore;
100 * Creates a new AnnotationColourGradient object.
102 public AnnotationColourGradient(AlignmentAnnotation annotation,
103 ColourSchemeI originalColour, int aboveThreshold)
105 if (originalColour instanceof AnnotationColourGradient)
107 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
111 colourScheme = originalColour;
114 this.annotation = annotation;
116 aboveAnnotationThreshold = aboveThreshold;
118 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
120 annotationThreshold = annotation.threshold;
122 // clear values so we don't get weird black bands...
135 * Creates a new AnnotationColourGradient object.
137 public AnnotationColourGradient(AlignmentAnnotation annotation,
138 Color minColour, Color maxColour, int aboveThreshold)
140 this.annotation = annotation;
142 aboveAnnotationThreshold = aboveThreshold;
144 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
146 annotationThreshold = annotation.threshold;
149 r1 = minColour.getRed();
150 g1 = minColour.getGreen();
151 b1 = minColour.getBlue();
153 rr = maxColour.getRed() - r1;
154 gg = maxColour.getGreen() - g1;
155 bb = maxColour.getBlue() - b1;
161 private void checkLimits()
163 aamax = annotation.graphMax;
164 aamin = annotation.graphMin;
165 if (annotation.isRNA())
167 // reset colour palette
168 ColourSchemeProperty.resetRnaHelicesShading();
169 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
174 public void alignmentChanged(AnnotatedCollectionI alignment,
175 Map<SequenceI, SequenceCollectionI> hiddenReps)
177 super.alignmentChanged(alignment, hiddenReps);
179 if (seqAssociated && annotation.getCalcId() != null)
181 if (seqannot != null)
187 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
189 // resolve the context containing all the annotation for the sequence
190 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
191 : alignment.getContext();
192 boolean f = true, sf = true, rna = false;
193 long plcount = 0, ancount = 0;
194 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
197 if (alan.sequenceRef != null
198 && (alan.label != null && annotation != null && alan.label
199 .equals(annotation.label)))
202 if (!rna && alan.isRNA())
206 seqannot.put(alan.sequenceRef, alan);
207 if (f || alan.graphMax > aamax)
209 aamax = alan.graphMax;
211 if (f || alan.graphMin < aamin)
213 aamin = alan.graphMin;
216 if (alan.score == alan.score)
218 if (sf || alan.score < plmin)
222 if (sf || alan.score > plmax)
231 if (plcount > 0 && plcount == ancount)
233 perLineScore = plcount == ancount;
238 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
244 * positional annotation max/min
246 double aamin = 0.0, aamax = 0.0;
249 * per line score max/min
251 double plmin = Double.NaN, plmax = Double.NaN;
253 public String getAnnotation()
255 return annotation.label;
258 public int getAboveThreshold()
260 return aboveAnnotationThreshold;
263 public float getAnnotationThreshold()
265 if (annotationThreshold == null)
271 return annotationThreshold.value;
275 public Color getMinColour()
277 return new Color((int) r1, (int) g1, (int) b1);
280 public Color getMaxColour()
282 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
291 * @return DOCUMENT ME!
294 public Color findColour(char c)
307 * @return DOCUMENT ME!
310 public Color findColour(char c, int j, SequenceI seq)
312 Color currentColour = Color.white;
313 AlignmentAnnotation annotation = (seqAssociated && seqannot != null ? seqannot
314 .get(seq) : this.annotation);
315 if (annotation == null)
317 return currentColour;
319 if ((threshold == 0) || aboveThreshold(c, j))
321 if (annotation.annotations != null
322 && j < annotation.annotations.length
323 && annotation.annotations[j] != null
324 && !jalview.util.Comparison.isGap(c))
326 Annotation aj = annotation.annotations[j];
327 // 'use original colours' => colourScheme != null
328 // -> look up colour to be used
329 // predefined colours => preconfigured shading
330 // -> only use original colours reference if thresholding enabled &
332 // annotation.hasIcons => null or black colours replaced with glyph
334 // -> reuse original colours if present
335 // -> if thresholding enabled then return colour on non-whitespace glyph
337 if (aboveAnnotationThreshold == NO_THRESHOLD
338 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
339 : aj.value <= annotationThreshold.value)))
341 if (predefinedColours && aj.colour != null
342 && !aj.colour.equals(Color.black))
344 currentColour = aj.colour;
346 else if (annotation.hasIcons
347 && annotation.graph == AlignmentAnnotation.NO_GRAPH)
349 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
350 && aj.secondaryStructure != '-')
352 if (colourScheme != null)
354 currentColour = colourScheme.findColour(c, j, seq);
358 if (annotation.isRNA())
360 currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
364 currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
365 : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
366 : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
378 if (colourScheme != null)
380 currentColour = colourScheme.findColour(c, j, seq);
384 if (aj.colour != null)
386 currentColour = aj.colour;
392 currentColour = shadeCalculation(annotation, j);
395 if (conservationColouring)
397 currentColour = applyConservation(currentColour, j);
401 return currentColour;
404 private Color shadeCalculation(AlignmentAnnotation annotation, int j)
409 if (thresholdIsMinMax
410 && annotation.threshold != null
411 && aboveAnnotationThreshold == ABOVE_THRESHOLD
412 && annotation.annotations[j].value >= annotation.threshold.value)
414 range = (annotation.annotations[j].value - annotation.threshold.value)
415 / (annotation.graphMax - annotation.threshold.value);
417 else if (thresholdIsMinMax && annotation.threshold != null
418 && aboveAnnotationThreshold == BELOW_THRESHOLD
419 && annotation.annotations[j].value >= annotation.graphMin)
421 range = (annotation.annotations[j].value - annotation.graphMin)
422 / (annotation.threshold.value - annotation.graphMin);
426 if (annotation.graphMax != annotation.graphMin)
428 range = (annotation.annotations[j].value - annotation.graphMin)
429 / (annotation.graphMax - annotation.graphMin);
436 // midtr sets the ceiling for bleaching out the shading
437 int trans = 0, midtr = 239;
440 trans = (int) ((1f - range) * midtr);
441 range = (float) ((annotation.score - plmin) / (plmax - aamin));
443 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
445 if (annotation.score == annotation.score && positionToTransparency)
447 return new Color(Integer.min(dr + trans, midtr), Integer.min(dg
448 + trans, midtr), Integer.min(db + trans, midtr));
452 return new Color(dr, dg, db);
456 public boolean isPredefinedColours()
458 return predefinedColours;
461 public void setPredefinedColours(boolean predefinedColours)
463 this.predefinedColours = predefinedColours;
466 public boolean isSeqAssociated()
468 return seqAssociated;
471 public void setSeqAssociated(boolean sassoc)
473 seqAssociated = sassoc;