2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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.analysis.Conservation;
22 import jalview.datamodel.AlignmentAnnotation;
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.Hashtable;
30 import java.util.IdentityHashMap;
33 public class AnnotationColourGradient extends ResidueColourScheme
35 public static final int NO_THRESHOLD = -1;
37 public static final int BELOW_THRESHOLD = 0;
39 public static final int ABOVE_THRESHOLD = 1;
41 public AlignmentAnnotation annotation;
43 int aboveAnnotationThreshold = -1;
45 public boolean thresholdIsMinMax = false;
47 GraphLine annotationThreshold;
49 float r1, g1, b1, rr, gg, bb, dr, dg, db;
51 ColourSchemeI colourScheme;
53 private boolean predefinedColours = false;
55 private boolean seqAssociated = false;
57 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
60 * Creates a new AnnotationColourGradient object.
62 public AnnotationColourGradient(AlignmentAnnotation annotation,
63 ColourSchemeI originalColour, int aboveThreshold)
65 if (originalColour instanceof AnnotationColourGradient)
67 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
71 colourScheme = originalColour;
74 this.annotation = annotation;
76 aboveAnnotationThreshold = aboveThreshold;
78 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
80 annotationThreshold = annotation.threshold;
85 * Creates a new AnnotationColourGradient object.
87 public AnnotationColourGradient(AlignmentAnnotation annotation,
88 Color minColour, Color maxColour, int aboveThreshold)
90 this.annotation = annotation;
92 aboveAnnotationThreshold = aboveThreshold;
94 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
96 annotationThreshold = annotation.threshold;
99 r1 = minColour.getRed();
100 g1 = minColour.getGreen();
101 b1 = minColour.getBlue();
103 rr = maxColour.getRed() - r1;
104 gg = maxColour.getGreen() - g1;
105 bb = maxColour.getBlue() - b1;
109 public void alignmentChanged(AnnotatedCollectionI alignment,
110 Map<SequenceI, SequenceCollectionI> hiddenReps)
112 // TODO Auto-generated method stub
113 super.alignmentChanged(alignment, hiddenReps);
115 if (seqAssociated && annotation.getCalcId() != null)
117 if (seqannot != null)
123 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
125 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
128 if (alan.sequenceRef != null
129 && (alan.label != null && annotation != null && alan.label
130 .equals(annotation.label)))
132 seqannot.put(alan.sequenceRef, alan);
138 public String getAnnotation()
140 return annotation.label;
143 public int getAboveThreshold()
145 return aboveAnnotationThreshold;
148 public float getAnnotationThreshold()
150 if (annotationThreshold == null)
156 return annotationThreshold.value;
160 public ColourSchemeI getBaseColour()
165 public Color getMinColour()
167 return new Color((int) r1, (int) g1, (int) b1);
170 public Color getMaxColour()
172 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
181 * @return DOCUMENT ME!
183 public Color findColour(char c)
196 * @return DOCUMENT ME!
199 public Color findColour(char c, int j, SequenceI seq)
201 Color currentColour = Color.white;
202 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
204 if (annotation == null)
206 return currentColour;
208 if ((threshold == 0) || aboveThreshold(c, j))
210 if (annotation.annotations!=null && j < annotation.annotations.length
211 && annotation.annotations[j] != null
212 && !jalview.util.Comparison.isGap(c))
215 if (predefinedColours)
217 if (annotation.annotations[j].colour != null)
218 return annotation.annotations[j].colour;
220 return currentColour;
223 if (aboveAnnotationThreshold == NO_THRESHOLD
224 || (annotationThreshold != null
225 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
226 || (annotationThreshold != null
227 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
231 if (thresholdIsMinMax
232 && annotation.threshold != null
233 && aboveAnnotationThreshold == ABOVE_THRESHOLD
234 && annotation.annotations[j].value >= annotation.threshold.value)
236 range = (annotation.annotations[j].value - annotation.threshold.value)
237 / (annotation.graphMax - annotation.threshold.value);
239 else if (thresholdIsMinMax && annotation.threshold != null
240 && aboveAnnotationThreshold == BELOW_THRESHOLD
241 && annotation.annotations[j].value >= annotation.graphMin)
243 range = (annotation.annotations[j].value - annotation.graphMin)
244 / (annotation.threshold.value - annotation.graphMin);
248 range = (annotation.annotations[j].value - annotation.graphMin)
249 / (annotation.graphMax - annotation.graphMin);
252 if (colourScheme != null)
254 currentColour = colourScheme.findColour(c, j, seq);
258 dr = rr * range + r1;
259 dg = gg * range + g1;
260 db = bb * range + b1;
262 currentColour = new Color((int) dr, (int) dg, (int) db);
268 if (conservationColouring)
270 currentColour = applyConservation(currentColour, j);
273 return currentColour;
276 public boolean isPredefinedColours()
278 return predefinedColours;
281 public void setPredefinedColours(boolean predefinedColours)
283 this.predefinedColours = predefinedColours;
286 public boolean isSeqAssociated()
288 return seqAssociated;
291 public void setSeqAssociated(boolean sassoc)
293 seqAssociated = sassoc;
296 public void setConsensus(Hashtable[] consensus)
298 if (colourScheme!=null)
300 colourScheme.setConsensus(consensus);
304 public void setConservation(Conservation cons)
306 if (colourScheme!=null)
308 colourScheme.setConservation(cons);
312 public void setConservationInc(int i)
314 if (colourScheme!=null)
316 colourScheme.setConservationInc(i);