2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.schemes;
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.AlignmentI;
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.GraphLine;
25 import jalview.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
28 import java.awt.Color;
29 import java.util.IdentityHashMap;
32 public class AnnotationColourGradient extends FollowerColourScheme
34 public static final int NO_THRESHOLD = -1;
36 public static final int BELOW_THRESHOLD = 0;
38 public static final int ABOVE_THRESHOLD = 1;
40 public AlignmentAnnotation annotation;
42 int aboveAnnotationThreshold = -1;
44 public boolean thresholdIsMinMax = false;
46 GraphLine annotationThreshold;
48 float r1, g1, b1, rr, gg, bb, dr, dg, db;
50 private boolean predefinedColours = false;
52 private boolean seqAssociated = false;
54 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
56 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
57 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
59 AnnotationColourGradient acg = new AnnotationColourGradient(annotation, colourScheme, aboveAnnotationThreshold);
60 acg.thresholdIsMinMax = thresholdIsMinMax;
61 acg.annotationThreshold = (annotationThreshold==null) ? null : new GraphLine(annotationThreshold);
71 acg.predefinedColours = predefinedColours;
72 acg.seqAssociated = seqAssociated;
77 * Creates a new AnnotationColourGradient object.
79 public AnnotationColourGradient(AlignmentAnnotation annotation,
80 ColourSchemeI originalColour, int aboveThreshold)
82 if (originalColour instanceof AnnotationColourGradient)
84 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
88 colourScheme = originalColour;
91 this.annotation = annotation;
93 aboveAnnotationThreshold = aboveThreshold;
95 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
97 annotationThreshold = annotation.threshold;
102 * Creates a new AnnotationColourGradient object.
104 public AnnotationColourGradient(AlignmentAnnotation annotation,
105 Color minColour, Color maxColour, int aboveThreshold)
107 this.annotation = annotation;
109 aboveAnnotationThreshold = aboveThreshold;
111 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
113 annotationThreshold = annotation.threshold;
116 r1 = minColour.getRed();
117 g1 = minColour.getGreen();
118 b1 = minColour.getBlue();
120 rr = maxColour.getRed() - r1;
121 gg = maxColour.getGreen() - g1;
122 bb = maxColour.getBlue() - b1;
126 public void alignmentChanged(AnnotatedCollectionI alignment,
127 Map<SequenceI, SequenceCollectionI> hiddenReps)
129 super.alignmentChanged(alignment, hiddenReps);
131 if (seqAssociated && annotation.getCalcId() != null)
133 if (seqannot != null)
139 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
141 // resolve the context containing all the annotation for the sequence
142 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment : alignment.getContext();
143 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
146 if (alan.sequenceRef != null
147 && (alan.label != null && annotation != null && alan.label
148 .equals(annotation.label)))
150 seqannot.put(alan.sequenceRef, alan);
156 public String getAnnotation()
158 return annotation.label;
161 public int getAboveThreshold()
163 return aboveAnnotationThreshold;
166 public float getAnnotationThreshold()
168 if (annotationThreshold == null)
174 return annotationThreshold.value;
178 public Color getMinColour()
180 return new Color((int) r1, (int) g1, (int) b1);
183 public Color getMaxColour()
185 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
194 * @return DOCUMENT ME!
196 public Color findColour(char c)
209 * @return DOCUMENT ME!
212 public Color findColour(char c, int j, SequenceI seq)
214 Color currentColour = Color.white;
215 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
217 if (annotation == null)
219 return currentColour;
221 if ((threshold == 0) || aboveThreshold(c, j))
223 if (annotation.annotations!=null && j < annotation.annotations.length
224 && annotation.annotations[j] != null
225 && !jalview.util.Comparison.isGap(c))
228 if (predefinedColours)
230 if (annotation.annotations[j].colour != null)
231 return annotation.annotations[j].colour;
233 return currentColour;
236 if (aboveAnnotationThreshold == NO_THRESHOLD
237 || (annotationThreshold != null
238 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
239 || (annotationThreshold != null
240 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
244 if (thresholdIsMinMax
245 && annotation.threshold != null
246 && aboveAnnotationThreshold == ABOVE_THRESHOLD
247 && annotation.annotations[j].value >= annotation.threshold.value)
249 range = (annotation.annotations[j].value - annotation.threshold.value)
250 / (annotation.graphMax - annotation.threshold.value);
252 else if (thresholdIsMinMax && annotation.threshold != null
253 && aboveAnnotationThreshold == BELOW_THRESHOLD
254 && annotation.annotations[j].value >= annotation.graphMin)
256 range = (annotation.annotations[j].value - annotation.graphMin)
257 / (annotation.threshold.value - annotation.graphMin);
261 range = (annotation.annotations[j].value - annotation.graphMin)
262 / (annotation.graphMax - annotation.graphMin);
265 if (colourScheme != null)
267 currentColour = colourScheme.findColour(c, j, seq);
271 dr = rr * range + r1;
272 dg = gg * range + g1;
273 db = bb * range + b1;
275 currentColour = new Color((int) dr, (int) dg, (int) db);
281 if (conservationColouring)
283 currentColour = applyConservation(currentColour, j);
286 return currentColour;
289 public boolean isPredefinedColours()
291 return predefinedColours;
294 public void setPredefinedColours(boolean predefinedColours)
296 this.predefinedColours = predefinedColours;
299 public boolean isSeqAssociated()
301 return seqAssociated;
304 public void setSeqAssociated(boolean sassoc)
306 seqAssociated = sassoc;