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<>();
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 perLineScore = false;
256 ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
262 * positional annotation max/min
264 double aamin = 0.0, aamax = 0.0;
267 * per line score max/min
269 double plmin = Double.NaN, plmax = Double.NaN;
271 public AlignmentAnnotation getAnnotation()
276 public int getAboveThreshold()
278 return aboveAnnotationThreshold;
281 public float getAnnotationThreshold()
283 if (annotationThreshold == null)
289 return annotationThreshold.value;
293 public Color getMinColour()
295 return new Color(redMin, greenMin, blueMin);
298 public Color getMaxColour()
300 return new Color(redMin + redRange, greenMin + greenRange,
301 blueMin + blueRange);
310 * @return DOCUMENT ME!
313 public Color findColour(char c)
319 * Returns the colour for a given character and position in a sequence
322 * the residue character
324 * the aligned position
330 public Color findColour(char c, int j, SequenceI seq)
333 * locate the annotation we are configured to colour by
335 AlignmentAnnotation ann = (seqAssociated && seqannot != null
340 * if gap or no annotation at position, no colour (White)
342 if (ann == null || ann.annotations == null
343 || j >= ann.annotations.length || ann.annotations[j] == null
344 || Comparison.isGap(c))
349 Annotation aj = ann.annotations[j];
350 // 'use original colours' => colourScheme != null
351 // -> look up colour to be used
352 // predefined colours => preconfigured shading
353 // -> only use original colours reference if thresholding enabled &
355 // annotation.hasIcons => null or black colours replaced with glyph
357 // -> reuse original colours if present
358 // -> if thresholding enabled then return colour on non-whitespace glyph
361 * if threshold applies, and annotation fails the test - no colour (white)
363 if (annotationThreshold != null)
365 if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
366 && aj.value < annotationThreshold.value)
367 || (aboveAnnotationThreshold == BELOW_THRESHOLD
368 && aj.value > annotationThreshold.value))
375 * If 'use original colours' then return the colour of the annotation
376 * at the aligned position - computed using the background colour scheme
378 if (predefinedColours && aj.colour != null
379 && !aj.colour.equals(Color.black))
384 Color result = Color.white;
385 if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
388 * secondary structure symbol colouring
390 if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
391 && aj.secondaryStructure != '-')
393 if (getColourScheme() != null)
395 result = getColourScheme().findColour(c, j, seq, null, 0f);
401 result = ColourSchemeProperty.rnaHelices[(int) aj.value];
405 result = ann.annotations[j].secondaryStructure == 'H'
406 ? AnnotationRenderer.HELIX_COLOUR
407 : ann.annotations[j].secondaryStructure == 'E'
408 ? AnnotationRenderer.SHEET_COLOUR
409 : AnnotationRenderer.STEM_COLOUR;
420 if (getColourScheme() != null)
422 result = getColourScheme().findColour(c, j, seq, null, 0f);
426 if (aj.colour != null)
434 result = shadeCalculation(ann, j);
441 * Returns a graduated colour for the annotation at the given column. If there
442 * is a threshold value, and it is used as the top/bottom of the colour range,
443 * and the value satisfies the threshold condition, then a colour
444 * proportionate to the range from the threshold is calculated. For all other
445 * cases, a colour proportionate to the annotation's min-max range is
446 * calulated. Note that thresholding is _not_ done here (a colour is computed
447 * even if threshold is not passed).
453 Color shadeCalculation(AlignmentAnnotation ann, int col)
456 float value = ann.annotations[col].value;
457 if (thresholdIsMinMax && ann.threshold != null
458 && aboveAnnotationThreshold == ABOVE_THRESHOLD
459 && value >= ann.threshold.value)
461 range = (value - ann.threshold.value)
462 / (ann.graphMax - ann.threshold.value);
464 else if (thresholdIsMinMax && ann.threshold != null
465 && aboveAnnotationThreshold == BELOW_THRESHOLD
466 && value <= ann.threshold.value)
468 range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
472 if (ann.graphMax != ann.graphMin)
474 range = (value - ann.graphMin) / (ann.graphMax - ann.graphMin);
482 // midtr sets the ceiling for bleaching out the shading
483 int trans = 0, midtr = 239;
486 trans = (int) ((1f - range) * midtr);
487 range = (float) ((ann.score - plmin) / (plmax - aamin));
489 int dr = (int) (redRange * range + redMin),
490 dg = (int) (greenRange * range + greenMin),
491 db = (int) (blueRange * range + blueMin);
492 if (ann.score == ann.score && positionToTransparency)
494 return new Color(Math.min(dr + trans, midtr), Math.min(dg
495 + trans, midtr), Math.min(db + trans, midtr));
499 return new Color(dr, dg, db);
503 public boolean isPredefinedColours()
505 return predefinedColours;
508 public void setPredefinedColours(boolean predefinedColours)
510 this.predefinedColours = predefinedColours;
513 public boolean isSeqAssociated()
515 return seqAssociated;
518 public void setSeqAssociated(boolean sassoc)
520 seqAssociated = sassoc;
523 public boolean isThresholdIsMinMax()
525 return thresholdIsMinMax;
528 public void setThresholdIsMinMax(boolean minMax)
530 this.thresholdIsMinMax = minMax;
534 public String getSchemeName()
540 public boolean isSimple()