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.GraphLine;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
30 import java.awt.Color;
31 import java.util.IdentityHashMap;
34 public class AnnotationColourGradient extends FollowerColourScheme
36 public static final int NO_THRESHOLD = -1;
38 public static final int BELOW_THRESHOLD = 0;
40 public static final int ABOVE_THRESHOLD = 1;
42 public AlignmentAnnotation annotation;
44 int aboveAnnotationThreshold = -1;
46 public boolean thresholdIsMinMax = false;
48 GraphLine annotationThreshold;
50 float r1, g1, b1, rr, gg, bb, dr, dg, db;
52 private boolean predefinedColours = false;
54 private boolean seqAssociated = false;
56 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
59 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
60 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
62 AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
63 colourScheme, aboveAnnotationThreshold);
64 acg.thresholdIsMinMax = thresholdIsMinMax;
65 acg.annotationThreshold = (annotationThreshold == null) ? null
66 : new GraphLine(annotationThreshold);
76 acg.predefinedColours = predefinedColours;
77 acg.seqAssociated = seqAssociated;
83 * Creates a new AnnotationColourGradient object.
85 public AnnotationColourGradient(AlignmentAnnotation annotation,
86 ColourSchemeI originalColour, int aboveThreshold)
88 if (originalColour instanceof AnnotationColourGradient)
90 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
94 colourScheme = originalColour;
97 this.annotation = annotation;
99 aboveAnnotationThreshold = aboveThreshold;
101 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
103 annotationThreshold = annotation.threshold;
108 * Creates a new AnnotationColourGradient object.
110 public AnnotationColourGradient(AlignmentAnnotation annotation,
111 Color minColour, Color maxColour, int aboveThreshold)
113 this.annotation = annotation;
115 aboveAnnotationThreshold = aboveThreshold;
117 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
119 annotationThreshold = annotation.threshold;
122 r1 = minColour.getRed();
123 g1 = minColour.getGreen();
124 b1 = minColour.getBlue();
126 rr = maxColour.getRed() - r1;
127 gg = maxColour.getGreen() - g1;
128 bb = maxColour.getBlue() - b1;
132 public void alignmentChanged(AnnotatedCollectionI alignment,
133 Map<SequenceI, SequenceCollectionI> hiddenReps)
135 super.alignmentChanged(alignment, hiddenReps);
137 if (seqAssociated && annotation.getCalcId() != null)
139 if (seqannot != null)
145 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
147 // resolve the context containing all the annotation for the sequence
148 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
149 : alignment.getContext();
150 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
153 if (alan.sequenceRef != null
154 && (alan.label != null && annotation != null && alan.label
155 .equals(annotation.label)))
157 seqannot.put(alan.sequenceRef, alan);
163 public String getAnnotation()
165 return annotation.label;
168 public int getAboveThreshold()
170 return aboveAnnotationThreshold;
173 public float getAnnotationThreshold()
175 if (annotationThreshold == null)
181 return annotationThreshold.value;
185 public Color getMinColour()
187 return new Color((int) r1, (int) g1, (int) b1);
190 public Color getMaxColour()
192 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
201 * @return DOCUMENT ME!
203 public Color findColour(char c)
216 * @return DOCUMENT ME!
219 public Color findColour(char c, int j, SequenceI seq)
221 Color currentColour = Color.white;
222 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
224 if (annotation == null)
226 return currentColour;
228 if ((threshold == 0) || aboveThreshold(c, j))
230 if (annotation.annotations != null
231 && j < annotation.annotations.length
232 && annotation.annotations[j] != null
233 && !jalview.util.Comparison.isGap(c))
236 if (predefinedColours)
238 if (annotation.annotations[j].colour != null)
239 return annotation.annotations[j].colour;
241 return currentColour;
244 if (aboveAnnotationThreshold == NO_THRESHOLD
245 || (annotationThreshold != null
246 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
247 || (annotationThreshold != null
248 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
252 if (thresholdIsMinMax
253 && annotation.threshold != null
254 && aboveAnnotationThreshold == ABOVE_THRESHOLD
255 && annotation.annotations[j].value >= annotation.threshold.value)
257 range = (annotation.annotations[j].value - annotation.threshold.value)
258 / (annotation.graphMax - annotation.threshold.value);
260 else if (thresholdIsMinMax && annotation.threshold != null
261 && aboveAnnotationThreshold == BELOW_THRESHOLD
262 && annotation.annotations[j].value >= annotation.graphMin)
264 range = (annotation.annotations[j].value - annotation.graphMin)
265 / (annotation.threshold.value - annotation.graphMin);
269 range = (annotation.annotations[j].value - annotation.graphMin)
270 / (annotation.graphMax - annotation.graphMin);
273 if (colourScheme != null)
275 currentColour = colourScheme.findColour(c, j, seq);
279 dr = rr * range + r1;
280 dg = gg * range + g1;
281 db = bb * range + b1;
283 currentColour = new Color((int) dr, (int) dg, (int) db);
289 if (conservationColouring)
291 currentColour = applyConservation(currentColour, j);
294 return currentColour;
297 public boolean isPredefinedColours()
299 return predefinedColours;
302 public void setPredefinedColours(boolean predefinedColours)
304 this.predefinedColours = predefinedColours;
307 public boolean isSeqAssociated()
309 return seqAssociated;
312 public void setSeqAssociated(boolean sassoc)
314 seqAssociated = sassoc;