2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.schemes;
20 import jalview.analysis.Conservation;
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.AnnotatedCollectionI;
23 import jalview.datamodel.GraphLine;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
27 import java.awt.Color;
28 import java.util.Hashtable;
29 import java.util.IdentityHashMap;
32 public class AnnotationColourGradient extends ResidueColourScheme
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 ColourSchemeI colourScheme;
52 private boolean predefinedColours = false;
54 private boolean seqAssociated = false;
56 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
59 * Creates a new AnnotationColourGradient object.
61 public AnnotationColourGradient(AlignmentAnnotation annotation,
62 ColourSchemeI originalColour, int aboveThreshold)
64 if (originalColour instanceof AnnotationColourGradient)
66 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
70 colourScheme = originalColour;
73 this.annotation = annotation;
75 aboveAnnotationThreshold = aboveThreshold;
77 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
79 annotationThreshold = annotation.threshold;
84 * Creates a new AnnotationColourGradient object.
86 public AnnotationColourGradient(AlignmentAnnotation annotation,
87 Color minColour, Color maxColour, int aboveThreshold)
89 this.annotation = annotation;
91 aboveAnnotationThreshold = aboveThreshold;
93 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
95 annotationThreshold = annotation.threshold;
98 r1 = minColour.getRed();
99 g1 = minColour.getGreen();
100 b1 = minColour.getBlue();
102 rr = maxColour.getRed() - r1;
103 gg = maxColour.getGreen() - g1;
104 bb = maxColour.getBlue() - b1;
108 public void alignmentChanged(AnnotatedCollectionI alignment,
109 Map<SequenceI, SequenceCollectionI> hiddenReps)
111 // TODO Auto-generated method stub
112 super.alignmentChanged(alignment, hiddenReps);
114 if (seqAssociated && annotation.getCalcId() != null)
116 if (seqannot != null)
122 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
124 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
127 if (alan.sequenceRef != null
128 && (alan.label != null && annotation != null && alan.label
129 .equals(annotation.label)))
131 seqannot.put(alan.sequenceRef, alan);
137 public String getAnnotation()
139 return annotation.label;
142 public int getAboveThreshold()
144 return aboveAnnotationThreshold;
147 public float getAnnotationThreshold()
149 if (annotationThreshold == null)
155 return annotationThreshold.value;
159 public ColourSchemeI getBaseColour()
164 public Color getMinColour()
166 return new Color((int) r1, (int) g1, (int) b1);
169 public Color getMaxColour()
171 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
180 * @return DOCUMENT ME!
182 public Color findColour(char c)
195 * @return DOCUMENT ME!
198 public Color findColour(char c, int j, SequenceI seq)
200 Color currentColour = Color.white;
201 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
203 if (annotation == null)
205 return currentColour;
207 if ((threshold == 0) || aboveThreshold(c, j))
209 if (j < annotation.annotations.length
210 && annotation.annotations[j] != null
211 && !jalview.util.Comparison.isGap(c))
214 if (predefinedColours)
216 if (annotation.annotations[j].colour != null)
217 return annotation.annotations[j].colour;
219 return currentColour;
222 if (aboveAnnotationThreshold == NO_THRESHOLD
223 || (annotationThreshold != null
224 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
225 || (annotationThreshold != null
226 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
230 if (thresholdIsMinMax
231 && annotation.threshold != null
232 && aboveAnnotationThreshold == ABOVE_THRESHOLD
233 && annotation.annotations[j].value >= annotation.threshold.value)
235 range = (annotation.annotations[j].value - annotation.threshold.value)
236 / (annotation.graphMax - annotation.threshold.value);
238 else if (thresholdIsMinMax && annotation.threshold != null
239 && aboveAnnotationThreshold == BELOW_THRESHOLD
240 && annotation.annotations[j].value >= annotation.graphMin)
242 range = (annotation.annotations[j].value - annotation.graphMin)
243 / (annotation.threshold.value - annotation.graphMin);
247 range = (annotation.annotations[j].value - annotation.graphMin)
248 / (annotation.graphMax - annotation.graphMin);
251 if (colourScheme != null)
253 currentColour = colourScheme.findColour(c, j, seq);
257 dr = rr * range + r1;
258 dg = gg * range + g1;
259 db = bb * range + b1;
261 currentColour = new Color((int) dr, (int) dg, (int) db);
267 if (conservationColouring)
269 currentColour = applyConservation(currentColour, j);
272 return currentColour;
275 public boolean isPredefinedColours()
277 return predefinedColours;
280 public void setPredefinedColours(boolean predefinedColours)
282 this.predefinedColours = predefinedColours;
285 public boolean isSeqAssociated()
287 return seqAssociated;
290 public void setSeqAssociated(boolean sassoc)
292 seqAssociated = sassoc;
295 public void setConsensus(Hashtable[] consensus)
297 if (colourScheme!=null)
299 colourScheme.setConsensus(consensus);
303 public void setConservation(Conservation cons)
305 if (colourScheme!=null)
307 colourScheme.setConservation(cons);
311 public void setConservationInc(int i)
313 if (colourScheme!=null)
315 colourScheme.setConservationInc(i);