2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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) {
117 seqannot=new IdentityHashMap<SequenceI, AlignmentAnnotation>();
119 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
122 if (alan.sequenceRef != null
123 && (alan.label != null && annotation != null && alan.label
124 .equals(annotation.label)))
126 seqannot.put(alan.sequenceRef, alan);
132 public String getAnnotation()
134 return annotation.label;
137 public int getAboveThreshold()
139 return aboveAnnotationThreshold;
142 public float getAnnotationThreshold()
144 if (annotationThreshold == null)
150 return annotationThreshold.value;
154 public ColourSchemeI getBaseColour()
159 public Color getMinColour()
161 return new Color((int) r1, (int) g1, (int) b1);
164 public Color getMaxColour()
166 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
175 * @return DOCUMENT ME!
177 public Color findColour(char c)
190 * @return DOCUMENT ME!
193 public Color findColour(char c, int j, SequenceI seq)
195 Color currentColour = Color.white;
196 AlignmentAnnotation annotation=(seqAssociated ? seqannot.get(seq):this.annotation);
197 if (annotation==null)
199 return currentColour;
201 if ((threshold == 0) || aboveThreshold(c, j))
203 if (j < annotation.annotations.length
204 && annotation.annotations[j] != null
205 && !jalview.util.Comparison.isGap(c))
208 if (predefinedColours)
210 if (annotation.annotations[j].colour != null)
211 return annotation.annotations[j].colour;
213 return currentColour;
216 if (aboveAnnotationThreshold == NO_THRESHOLD
217 || (annotationThreshold != null
218 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
219 || (annotationThreshold != null
220 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
224 if (thresholdIsMinMax
225 && annotation.threshold != null
226 && aboveAnnotationThreshold == ABOVE_THRESHOLD
227 && annotation.annotations[j].value > annotation.threshold.value)
229 range = (annotation.annotations[j].value - annotation.threshold.value)
230 / (annotation.graphMax - annotation.threshold.value);
232 else if (thresholdIsMinMax && annotation.threshold != null
233 && aboveAnnotationThreshold == BELOW_THRESHOLD
234 && annotation.annotations[j].value > annotation.graphMin)
236 range = (annotation.annotations[j].value - annotation.graphMin)
237 / (annotation.threshold.value - annotation.graphMin);
241 range = (annotation.annotations[j].value - annotation.graphMin)
242 / (annotation.graphMax - annotation.graphMin);
245 if (colourScheme != null)
247 currentColour = colourScheme.findColour(c, j, seq);
251 dr = rr * range + r1;
252 dg = gg * range + g1;
253 db = bb * range + b1;
255 currentColour = new Color((int) dr, (int) dg, (int) db);
261 if (conservationColouring)
263 currentColour = applyConservation(currentColour, j);
266 return currentColour;
269 public boolean isSeqAssociated()
271 return seqAssociated;
274 public void setSeqAssociated(boolean sassoc)
276 seqAssociated = sassoc;