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
40 * map positional scores to transparency rather than colour
42 boolean positionToTransparency = true;
45 * compute shade based on annotation row score
47 boolean perLineScore = true;
49 public static final int NO_THRESHOLD = -1;
51 public static final int BELOW_THRESHOLD = 0;
53 public static final int ABOVE_THRESHOLD = 1;
55 private final AlignmentAnnotation annotation;
57 private final int aboveAnnotationThreshold;
59 public boolean thresholdIsMinMax = false;
61 private GraphLine annotationThreshold;
71 private int greenRange;
73 private int blueRange;
75 private boolean predefinedColours = false;
77 private boolean seqAssociated = false;
80 * false if the scheme was constructed without a minColour and maxColour used
81 * to decide if existing colours should be taken from annotation elements when
84 private boolean noGradient = false;
86 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
89 public ColourSchemeI getInstance(AnnotatedCollectionI sg,
90 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
92 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
93 getColourScheme(), aboveAnnotationThreshold);
94 acg.thresholdIsMinMax = thresholdIsMinMax;
95 acg.annotationThreshold = (annotationThreshold == null) ? null
96 : new GraphLine(annotationThreshold);
98 acg.greenMin = greenMin;
99 acg.blueMin = blueMin;
100 acg.redRange = redRange;
101 acg.greenRange = greenRange;
102 acg.blueRange = blueRange;
103 acg.predefinedColours = predefinedColours;
104 acg.seqAssociated = seqAssociated;
105 acg.noGradient = noGradient;
106 acg.positionToTransparency = positionToTransparency;
107 acg.perLineScore = perLineScore;
112 * Creates a new AnnotationColourGradient object.
114 public AnnotationColourGradient(AlignmentAnnotation annotation,
115 ColourSchemeI originalColour, int aboveThreshold)
117 if (originalColour instanceof AnnotationColourGradient)
119 setColourScheme(((AnnotationColourGradient) originalColour)
124 setColourScheme(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...
148 * Creates a new AnnotationColourGradient object.
150 public AnnotationColourGradient(AlignmentAnnotation annotation,
151 Color minColour, Color maxColour, int aboveThreshold)
153 this.annotation = annotation;
155 aboveAnnotationThreshold = aboveThreshold;
157 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
159 annotationThreshold = annotation.threshold;
162 redMin = minColour.getRed();
163 greenMin = minColour.getGreen();
164 blueMin = minColour.getBlue();
166 redRange = maxColour.getRed() - redMin;
167 greenRange = maxColour.getGreen() - greenMin;
168 blueRange = maxColour.getBlue() - blueMin;
174 private void checkLimits()
176 aamax = annotation.graphMax;
177 aamin = annotation.graphMin;
178 if (annotation.isRNA())
180 // reset colour palette
181 ColourSchemeProperty.resetRnaHelicesShading();
182 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
187 public void alignmentChanged(AnnotatedCollectionI alignment,
188 Map<SequenceI, SequenceCollectionI> hiddenReps)
190 super.alignmentChanged(alignment, hiddenReps);
192 if (seqAssociated && annotation.getCalcId() != null)
194 if (seqannot != null)
200 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
202 // resolve the context containing all the annotation for the sequence
203 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
205 : alignment.getContext();
206 boolean f = true, sf = true, rna = false;
207 long plcount = 0, ancount = 0;
208 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
211 if (alan.sequenceRef != null
212 && (alan.label != null && annotation != null
213 && alan.label.equals(annotation.label)))
216 if (!rna && alan.isRNA())
220 seqannot.put(alan.sequenceRef, alan);
221 if (f || alan.graphMax > aamax)
223 aamax = alan.graphMax;
225 if (f || alan.graphMin < aamin)
227 aamin = alan.graphMin;
230 if (alan.score == alan.score)
232 if (sf || alan.score < plmin)
236 if (sf || alan.score > plmax)
245 if (plcount > 0 && plcount == ancount)
247 perLineScore = plcount == ancount;
252 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
258 * positional annotation max/min
260 double aamin = 0.0, aamax = 0.0;
263 * per line score max/min
265 double plmin = Double.NaN, plmax = Double.NaN;
267 public AlignmentAnnotation getAnnotation()
272 public int getAboveThreshold()
274 return aboveAnnotationThreshold;
277 public float getAnnotationThreshold()
279 if (annotationThreshold == null)
285 return annotationThreshold.value;
289 public Color getMinColour()
291 return new Color(redMin, greenMin, blueMin);
294 public Color getMaxColour()
296 return new Color(redMin + redRange, greenMin + greenRange,
297 blueMin + blueRange);
306 * @return DOCUMENT ME!
309 public Color findColour(char c)
315 * Returns the colour for a given character and position in a sequence
318 * the residue character
320 * the aligned position
326 public Color findColour(char c, int j, SequenceI seq)
329 * locate the annotation we are configured to colour by
331 AlignmentAnnotation ann = (seqAssociated && seqannot != null
336 * if gap or no annotation at position, no colour (White)
338 if (ann == null || ann.annotations == null
339 || j >= ann.annotations.length || ann.annotations[j] == null
340 || Comparison.isGap(c))
345 Annotation aj = ann.annotations[j];
346 // 'use original colours' => colourScheme != null
347 // -> look up colour to be used
348 // predefined colours => preconfigured shading
349 // -> only use original colours reference if thresholding enabled &
351 // annotation.hasIcons => null or black colours replaced with glyph
353 // -> reuse original colours if present
354 // -> if thresholding enabled then return colour on non-whitespace glyph
357 * if threshold applies, and annotation fails the test - no colour (white)
359 if (annotationThreshold != null)
361 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
362 && aj.value < annotationThreshold.value)
363 || (aboveAnnotationThreshold == BELOW_THRESHOLD
364 && aj.value > annotationThreshold.value))
371 * If 'use original colours' then return the colour of the annotation
372 * at the aligned position - computed using the background colour scheme
374 if (predefinedColours && aj.colour != null
375 && !aj.colour.equals(Color.black))
380 Color result = Color.white;
381 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
384 * secondary structure symbol colouring
386 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
387 && aj.secondaryStructure != '-')
389 if (getColourScheme() != null)
391 result = getColourScheme().findColour(c, j, seq, null, 0f);
397 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
401 result = ann.annotations[j].secondaryStructure == 'H'
402 ? AnnotationRenderer.HELIX_COLOUR
403 : ann.annotations[j].secondaryStructure == 'E'
404 ? AnnotationRenderer.SHEET_COLOUR
405 : AnnotationRenderer.STEM_COLOUR;
416 if (getColourScheme() != null)
418 result = getColourScheme().findColour(c, j, seq, null, 0f);
422 if (aj.colour != null)
430 result = shadeCalculation(ann, j);
437 * Returns a graduated colour for the annotation at the given column. If there
438 * is a threshold value, and it is used as the top/bottom of the colour range,
439 * and the value satisfies the threshold condition, then a colour
440 * proportionate to the range from the threshold is calculated. For all other
441 * cases, a colour proportionate to the annotation's min-max range is
442 * calulated. Note that thresholding is _not_ done here (a colour is computed
443 * even if threshold is not passed).
449 Color shadeCalculation(AlignmentAnnotation ann, int col)
452 float value = ann.annotations[col].value;
453 if (thresholdIsMinMax && ann.threshold != null
454 && aboveAnnotationThreshold == ABOVE_THRESHOLD
455 && value >= ann.threshold.value)
457 range = (value - ann.threshold.value)
458 / (ann.graphMax - ann.threshold.value);
460 else if (thresholdIsMinMax && ann.threshold != null
461 && aboveAnnotationThreshold == BELOW_THRESHOLD
462 && value <= ann.threshold.value)
464 range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
468 if (ann.graphMax != ann.graphMin)
470 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
478 // midtr sets the ceiling for bleaching out the shading
479 int trans = 0, midtr = 239;
482 trans = (int) ((1f - range) * midtr);
483 range = (float) ((annotation.score - plmin) / (plmax - aamin));
485 int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
487 if (annotation.score == annotation.score && positionToTransparency)
489 return new Color(Math.min(dr + trans, midtr), Math.min(dg
490 + trans, midtr), Math.min(db + trans, midtr));
494 return new Color(dr, dg, db);
498 public boolean isPredefinedColours()
500 return predefinedColours;
503 public void setPredefinedColours(boolean predefinedColours)
505 this.predefinedColours = predefinedColours;
508 public boolean isSeqAssociated()
510 return seqAssociated;
513 public void setSeqAssociated(boolean sassoc)
515 seqAssociated = sassoc;
518 public boolean isThresholdIsMinMax()
520 return thresholdIsMinMax;
523 public void setThresholdIsMinMax(boolean minMax)
525 this.thresholdIsMinMax = minMax;
529 public String getSchemeName()
535 public boolean isSimple()