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;
58 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
59 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
61 AnnotationColourGradient acg = new AnnotationColourGradient(annotation, colourScheme, aboveAnnotationThreshold);
62 acg.thresholdIsMinMax = thresholdIsMinMax;
63 acg.annotationThreshold = (annotationThreshold==null) ? null : new GraphLine(annotationThreshold);
73 acg.predefinedColours = predefinedColours;
74 acg.seqAssociated = seqAssociated;
79 * Creates a new AnnotationColourGradient object.
81 public AnnotationColourGradient(AlignmentAnnotation annotation,
82 ColourSchemeI originalColour, int aboveThreshold)
84 if (originalColour instanceof AnnotationColourGradient)
86 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
90 colourScheme = originalColour;
93 this.annotation = annotation;
95 aboveAnnotationThreshold = aboveThreshold;
97 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
99 annotationThreshold = annotation.threshold;
104 * Creates a new AnnotationColourGradient object.
106 public AnnotationColourGradient(AlignmentAnnotation annotation,
107 Color minColour, Color maxColour, int aboveThreshold)
109 this.annotation = annotation;
111 aboveAnnotationThreshold = aboveThreshold;
113 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
115 annotationThreshold = annotation.threshold;
118 r1 = minColour.getRed();
119 g1 = minColour.getGreen();
120 b1 = minColour.getBlue();
122 rr = maxColour.getRed() - r1;
123 gg = maxColour.getGreen() - g1;
124 bb = maxColour.getBlue() - b1;
128 public void alignmentChanged(AnnotatedCollectionI alignment,
129 Map<SequenceI, SequenceCollectionI> hiddenReps)
131 super.alignmentChanged(alignment, hiddenReps);
133 if (seqAssociated && annotation.getCalcId() != null)
135 if (seqannot != null)
141 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
143 // resolve the context containing all the annotation for the sequence
144 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment : alignment.getContext();
145 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
148 if (alan.sequenceRef != null
149 && (alan.label != null && annotation != null && alan.label
150 .equals(annotation.label)))
152 seqannot.put(alan.sequenceRef, alan);
158 public String getAnnotation()
160 return annotation.label;
163 public int getAboveThreshold()
165 return aboveAnnotationThreshold;
168 public float getAnnotationThreshold()
170 if (annotationThreshold == null)
176 return annotationThreshold.value;
180 public Color getMinColour()
182 return new Color((int) r1, (int) g1, (int) b1);
185 public Color getMaxColour()
187 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
196 * @return DOCUMENT ME!
198 public Color findColour(char c)
211 * @return DOCUMENT ME!
214 public Color findColour(char c, int j, SequenceI seq)
216 Color currentColour = Color.white;
217 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
219 if (annotation == null)
221 return currentColour;
223 if ((threshold == 0) || aboveThreshold(c, j))
225 if (annotation.annotations!=null && j < annotation.annotations.length
226 && annotation.annotations[j] != null
227 && !jalview.util.Comparison.isGap(c))
230 if (predefinedColours)
232 if (annotation.annotations[j].colour != null)
233 return annotation.annotations[j].colour;
235 return currentColour;
238 if (aboveAnnotationThreshold == NO_THRESHOLD
239 || (annotationThreshold != null
240 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
241 || (annotationThreshold != null
242 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
246 if (thresholdIsMinMax
247 && annotation.threshold != null
248 && aboveAnnotationThreshold == ABOVE_THRESHOLD
249 && annotation.annotations[j].value >= annotation.threshold.value)
251 range = (annotation.annotations[j].value - annotation.threshold.value)
252 / (annotation.graphMax - annotation.threshold.value);
254 else if (thresholdIsMinMax && annotation.threshold != null
255 && aboveAnnotationThreshold == BELOW_THRESHOLD
256 && annotation.annotations[j].value >= annotation.graphMin)
258 range = (annotation.annotations[j].value - annotation.graphMin)
259 / (annotation.threshold.value - annotation.graphMin);
263 range = (annotation.annotations[j].value - annotation.graphMin)
264 / (annotation.graphMax - annotation.graphMin);
267 if (colourScheme != null)
269 currentColour = colourScheme.findColour(c, j, seq);
273 dr = rr * range + r1;
274 dg = gg * range + g1;
275 db = bb * range + b1;
277 currentColour = new Color((int) dr, (int) dg, (int) db);
283 if (conservationColouring)
285 currentColour = applyConservation(currentColour, j);
288 return currentColour;
291 public boolean isPredefinedColours()
293 return predefinedColours;
296 public void setPredefinedColours(boolean predefinedColours)
298 this.predefinedColours = predefinedColours;
301 public boolean isSeqAssociated()
303 return seqAssociated;
306 public void setSeqAssociated(boolean sassoc)
308 seqAssociated = sassoc;