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.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.AnnotatedCollectionI;
22 import jalview.datamodel.GraphLine;
23 import jalview.datamodel.SequenceCollectionI;
24 import jalview.datamodel.SequenceI;
26 import java.awt.Color;
27 import java.util.IdentityHashMap;
30 public class AnnotationColourGradient extends ResidueColourScheme
32 public static final int NO_THRESHOLD = -1;
34 public static final int BELOW_THRESHOLD = 0;
36 public static final int ABOVE_THRESHOLD = 1;
38 public AlignmentAnnotation annotation;
40 int aboveAnnotationThreshold = -1;
42 public boolean thresholdIsMinMax = false;
44 GraphLine annotationThreshold;
46 float r1, g1, b1, rr, gg, bb, dr, dg, db;
48 ColourSchemeI colourScheme;
50 public boolean predefinedColours = false;
52 public boolean seqAssociated = false;
54 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
57 * Creates a new AnnotationColourGradient object.
59 public AnnotationColourGradient(AlignmentAnnotation annotation,
60 ColourSchemeI originalColour, int aboveThreshold)
62 if (originalColour instanceof AnnotationColourGradient)
64 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
68 colourScheme = originalColour;
71 this.annotation = annotation;
73 aboveAnnotationThreshold = aboveThreshold;
75 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
77 annotationThreshold = annotation.threshold;
82 * Creates a new AnnotationColourGradient object.
84 public AnnotationColourGradient(AlignmentAnnotation annotation,
85 Color minColour, Color maxColour, int aboveThreshold)
87 this.annotation = annotation;
89 aboveAnnotationThreshold = aboveThreshold;
91 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
93 annotationThreshold = annotation.threshold;
96 r1 = minColour.getRed();
97 g1 = minColour.getGreen();
98 b1 = minColour.getBlue();
100 rr = maxColour.getRed() - r1;
101 gg = maxColour.getGreen() - g1;
102 bb = maxColour.getBlue() - b1;
106 public void alignmentChanged(AnnotatedCollectionI alignment,
107 Map<SequenceI, SequenceCollectionI> hiddenReps)
109 // TODO Auto-generated method stub
110 super.alignmentChanged(alignment, hiddenReps);
112 if (seqAssociated && annotation.getCalcId() != null)
114 if (seqannot != null)
120 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
122 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
125 if (alan.sequenceRef != null
126 && (alan.label != null && annotation != null && alan.label
127 .equals(annotation.label)))
129 seqannot.put(alan.sequenceRef, alan);
135 public String getAnnotation()
137 return annotation.label;
140 public int getAboveThreshold()
142 return aboveAnnotationThreshold;
145 public float getAnnotationThreshold()
147 if (annotationThreshold == null)
153 return annotationThreshold.value;
157 public ColourSchemeI getBaseColour()
162 public Color getMinColour()
164 return new Color((int) r1, (int) g1, (int) b1);
167 public Color getMaxColour()
169 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
178 * @return DOCUMENT ME!
180 public Color findColour(char c)
193 * @return DOCUMENT ME!
196 public Color findColour(char c, int j, SequenceI seq)
198 Color currentColour = Color.white;
199 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
201 if (annotation == null)
203 return currentColour;
205 if ((threshold == 0) || aboveThreshold(c, j))
207 if (j < annotation.annotations.length
208 && annotation.annotations[j] != null
209 && !jalview.util.Comparison.isGap(c))
212 if (predefinedColours)
214 if (annotation.annotations[j].colour != null)
215 return annotation.annotations[j].colour;
217 return currentColour;
220 if (aboveAnnotationThreshold == NO_THRESHOLD
221 || (annotationThreshold != null
222 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
223 || (annotationThreshold != null
224 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
228 if (thresholdIsMinMax
229 && annotation.threshold != null
230 && aboveAnnotationThreshold == ABOVE_THRESHOLD
231 && annotation.annotations[j].value > annotation.threshold.value)
233 range = (annotation.annotations[j].value - annotation.threshold.value)
234 / (annotation.graphMax - annotation.threshold.value);
236 else if (thresholdIsMinMax && annotation.threshold != null
237 && aboveAnnotationThreshold == BELOW_THRESHOLD
238 && annotation.annotations[j].value > annotation.graphMin)
240 range = (annotation.annotations[j].value - annotation.graphMin)
241 / (annotation.threshold.value - annotation.graphMin);
245 range = (annotation.annotations[j].value - annotation.graphMin)
246 / (annotation.graphMax - annotation.graphMin);
249 if (colourScheme != null)
251 currentColour = colourScheme.findColour(c, j, seq);
255 dr = rr * range + r1;
256 dg = gg * range + g1;
257 db = bb * range + b1;
259 currentColour = new Color((int) dr, (int) dg, (int) db);
265 if (conservationColouring)
267 currentColour = applyConservation(currentColour, j);
270 return currentColour;
273 public boolean isSeqAssociated()
275 return seqAssociated;
278 public void setSeqAssociated(boolean sassoc)
280 seqAssociated = sassoc;