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.api.AlignViewportI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.GraphLine;
29 import jalview.datamodel.SequenceCollectionI;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.AnnotationRenderer;
32 import jalview.util.Comparison;
34 import java.awt.Color;
35 import java.util.IdentityHashMap;
38 public class AnnotationColourGradient extends FollowerColourScheme
41 * map positional scores to transparency rather than colour
43 boolean positionToTransparency = false;
46 * compute shade based on annotation row score
48 boolean perLineScore = false;
50 public static final int NO_THRESHOLD = -1;
52 public static final int BELOW_THRESHOLD = 0;
54 public static final int ABOVE_THRESHOLD = 1;
56 private final AlignmentAnnotation annotation;
58 private final int aboveAnnotationThreshold;
60 public boolean thresholdIsMinMax = false;
62 private GraphLine annotationThreshold;
72 private int greenRange;
74 private int blueRange;
76 private boolean predefinedColours = false;
78 private boolean seqAssociated = false;
81 * false if the scheme was constructed without a minColour and maxColour used
82 * to decide if existing colours should be taken from annotation elements when
85 private boolean noGradient = false;
87 private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
90 public ColourSchemeI getInstance(AlignViewportI view,
91 AnnotatedCollectionI sg)
93 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
94 getColourScheme(), aboveAnnotationThreshold);
95 acg.thresholdIsMinMax = thresholdIsMinMax;
96 acg.annotationThreshold = (annotationThreshold == null) ? null
97 : new GraphLine(annotationThreshold);
99 acg.greenMin = greenMin;
100 acg.blueMin = blueMin;
101 acg.redRange = redRange;
102 acg.greenRange = greenRange;
103 acg.blueRange = blueRange;
104 acg.predefinedColours = predefinedColours;
105 acg.seqAssociated = seqAssociated;
106 acg.noGradient = noGradient;
107 acg.positionToTransparency = positionToTransparency;
108 acg.perLineScore = perLineScore;
113 * Creates a new AnnotationColourGradient object.
115 public AnnotationColourGradient(AlignmentAnnotation annotation,
116 ColourSchemeI originalColour, int aboveThreshold)
118 if (originalColour instanceof AnnotationColourGradient)
120 setColourScheme(((AnnotationColourGradient) originalColour)
125 setColourScheme(originalColour);
128 this.annotation = annotation;
130 aboveAnnotationThreshold = aboveThreshold;
132 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
134 annotationThreshold = annotation.threshold;
136 // clear values so we don't get weird black bands...
149 * Creates a new AnnotationColourGradient object.
151 public AnnotationColourGradient(AlignmentAnnotation annotation,
152 Color minColour, Color maxColour, int aboveThreshold)
154 this.annotation = annotation;
156 aboveAnnotationThreshold = aboveThreshold;
158 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
160 annotationThreshold = annotation.threshold;
163 redMin = minColour.getRed();
164 greenMin = minColour.getGreen();
165 blueMin = minColour.getBlue();
167 redRange = maxColour.getRed() - redMin;
168 greenRange = maxColour.getGreen() - greenMin;
169 blueRange = maxColour.getBlue() - blueMin;
175 private void checkLimits()
177 aamax = annotation.graphMax;
178 aamin = annotation.graphMin;
179 if (annotation.isRNA())
181 // reset colour palette
182 ColourSchemeProperty.resetRnaHelicesShading();
183 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
188 public void alignmentChanged(AnnotatedCollectionI alignment,
189 Map<SequenceI, SequenceCollectionI> hiddenReps)
191 super.alignmentChanged(alignment, hiddenReps);
193 if (seqAssociated && annotation.getCalcId() != null)
195 if (seqannot != null)
201 seqannot = new IdentityHashMap<>();
203 // resolve the context containing all the annotation for the sequence
204 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
206 : alignment.getContext();
207 boolean f = true, sf = true, rna = false;
208 long plcount = 0, ancount = 0;
209 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
212 if (alan.sequenceRef != null
213 && (alan.label != null && annotation != null
214 && alan.label.equals(annotation.label)))
217 if (!rna && alan.isRNA())
221 seqannot.put(alan.sequenceRef, alan);
222 if (f || alan.graphMax > aamax)
224 aamax = alan.graphMax;
226 if (f || alan.graphMin < aamin)
228 aamin = alan.graphMin;
231 if (alan.score == alan.score)
233 if (sf || alan.score < plmin)
237 if (sf || alan.score > plmax)
246 if (plcount > 0 && plcount == ancount)
248 perLineScore = plmax!=plmin;
253 perLineScore = false;
257 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
263 * positional annotation max/min
265 double aamin = 0.0, aamax = 0.0;
268 * per line score max/min
270 double plmin = Double.NaN, plmax = Double.NaN;
272 public AlignmentAnnotation getAnnotation()
277 public int getAboveThreshold()
279 return aboveAnnotationThreshold;
282 public float getAnnotationThreshold()
284 if (annotationThreshold == null)
290 return annotationThreshold.value;
294 public Color getMinColour()
296 return new Color(redMin, greenMin, blueMin);
299 public Color getMaxColour()
301 return new Color(redMin + redRange, greenMin + greenRange,
302 blueMin + blueRange);
311 * @return DOCUMENT ME!
314 public Color findColour(char c)
320 * Returns the colour for a given character and position in a sequence
323 * the residue character
325 * the aligned position
331 public Color findColour(char c, int j, SequenceI seq)
334 * locate the annotation we are configured to colour by
336 AlignmentAnnotation ann = (seqAssociated && seqannot != null
341 * if gap or no annotation at position, no colour (White)
343 if (ann == null || ann.annotations == null
344 || j >= ann.annotations.length || ann.annotations[j] == null
345 || Comparison.isGap(c))
350 Annotation aj = ann.annotations[j];
351 // 'use original colours' => colourScheme != null
352 // -> look up colour to be used
353 // predefined colours => preconfigured shading
354 // -> only use original colours reference if thresholding enabled &
356 // annotation.hasIcons => null or black colours replaced with glyph
358 // -> reuse original colours if present
359 // -> if thresholding enabled then return colour on non-whitespace glyph
362 * if threshold applies, and annotation fails the test - no colour (white)
364 if (annotationThreshold != null)
366 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
367 && aj.value <= annotationThreshold.value)
368 || (aboveAnnotationThreshold == BELOW_THRESHOLD
369 && aj.value >= annotationThreshold.value))
376 * If 'use original colours' then return the colour of the annotation
377 * at the aligned position - computed using the background colour scheme
379 if (predefinedColours && aj.colour != null
380 && !aj.colour.equals(Color.black))
385 Color result = Color.white;
386 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
389 * secondary structure symbol colouring
391 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
392 && aj.secondaryStructure != '-')
394 if (getColourScheme() != null)
396 result = getColourScheme().findColour(c, j, seq, null, 0f);
402 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
406 result = ann.annotations[j].secondaryStructure == 'H'
407 ? AnnotationRenderer.HELIX_COLOUR
408 : ann.annotations[j].secondaryStructure == 'E'
409 ? AnnotationRenderer.SHEET_COLOUR
410 : AnnotationRenderer.STEM_COLOUR;
421 if (getColourScheme() != null)
423 result = getColourScheme().findColour(c, j, seq, null, 0f);
427 if (aj.colour != null)
435 result = shadeCalculation(ann, j);
442 * Returns a graduated colour for the annotation at the given column. If there
443 * is a threshold value, and it is used as the top/bottom of the colour range,
444 * and the value satisfies the threshold condition, then a colour
445 * proportionate to the range from the threshold is calculated. For all other
446 * cases, a colour proportionate to the annotation's min-max range is
447 * calulated. Note that thresholding is _not_ done here (a colour is computed
448 * even if threshold is not passed).
454 Color shadeCalculation(AlignmentAnnotation ann, int col)
457 float value = ann.annotations[col].value;
458 if (thresholdIsMinMax && ann.threshold != null
459 && aboveAnnotationThreshold == ABOVE_THRESHOLD
460 && value >= ann.threshold.value)
462 range = ann.graphMax == ann.threshold.value ? 1f
463 : (value - ann.threshold.value)
464 / (ann.graphMax - ann.threshold.value);
466 else if (thresholdIsMinMax && ann.threshold != null
467 && aboveAnnotationThreshold == BELOW_THRESHOLD
468 && value <= ann.threshold.value)
470 range = ann.graphMin == ann.threshold.value ? 0f
471 : (value - ann.graphMin)
472 / (ann.threshold.value - ann.graphMin);
476 if (ann.graphMax != ann.graphMin)
478 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
486 // midtr sets the ceiling for bleaching out the shading
487 int trans = 0, midtr = 239;
490 trans = (int) ((1f - range) * midtr);
491 range = (float) ((ann.score - plmin) / (plmax - aamin));
493 int dr = (int) (redRange * range + redMin),
494 dg = (int) (greenRange * range + greenMin),
495 db = (int) (blueRange * range + blueMin);
496 if (ann.score == ann.score && positionToTransparency)
498 return new Color(Math.min(dr + trans, midtr), Math.min(dg
499 + trans, midtr), Math.min(db + trans, midtr));
503 return new Color(dr, dg, db);
507 public boolean isPredefinedColours()
509 return predefinedColours;
512 public void setPredefinedColours(boolean predefinedColours)
514 this.predefinedColours = predefinedColours;
517 public boolean isSeqAssociated()
519 return seqAssociated;
522 public void setSeqAssociated(boolean sassoc)
524 seqAssociated = sassoc;
527 public boolean isThresholdIsMinMax()
529 return thresholdIsMinMax;
532 public void setThresholdIsMinMax(boolean minMax)
534 this.thresholdIsMinMax = minMax;
538 public String getSchemeName()
540 return ANNOTATION_COLOUR;
544 public boolean isSimple()
549 public boolean isPositionToTransparency()
551 return positionToTransparency;
554 public void setPositionToTransparency(boolean positionToTransparency)
556 this.positionToTransparency = positionToTransparency;