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.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.IdentityHashMap;
31 public class AnnotationColourGradient extends FollowerColourScheme
33 public static final int NO_THRESHOLD = -1;
35 public static final int BELOW_THRESHOLD = 0;
37 public static final int ABOVE_THRESHOLD = 1;
39 public AlignmentAnnotation annotation;
41 int aboveAnnotationThreshold = -1;
43 public boolean thresholdIsMinMax = false;
45 GraphLine annotationThreshold;
47 float r1, g1, b1, rr, gg, bb, dr, dg, db;
49 private boolean predefinedColours = false;
51 private boolean seqAssociated = false;
53 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
56 * Creates a new AnnotationColourGradient object.
58 public AnnotationColourGradient(AlignmentAnnotation annotation,
59 ColourSchemeI originalColour, int aboveThreshold)
61 if (originalColour instanceof AnnotationColourGradient)
63 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
67 colourScheme = originalColour;
70 this.annotation = annotation;
72 aboveAnnotationThreshold = aboveThreshold;
74 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
76 annotationThreshold = annotation.threshold;
81 * Creates a new AnnotationColourGradient object.
83 public AnnotationColourGradient(AlignmentAnnotation annotation,
84 Color minColour, Color maxColour, int aboveThreshold)
86 this.annotation = annotation;
88 aboveAnnotationThreshold = aboveThreshold;
90 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
92 annotationThreshold = annotation.threshold;
95 r1 = minColour.getRed();
96 g1 = minColour.getGreen();
97 b1 = minColour.getBlue();
99 rr = maxColour.getRed() - r1;
100 gg = maxColour.getGreen() - g1;
101 bb = maxColour.getBlue() - b1;
105 public void alignmentChanged(AnnotatedCollectionI alignment,
106 Map<SequenceI, SequenceCollectionI> hiddenReps)
108 // TODO Auto-generated method stub
109 super.alignmentChanged(alignment, hiddenReps);
111 if (seqAssociated && annotation.getCalcId() != null)
113 if (seqannot != null)
119 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
121 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
124 if (alan.sequenceRef != null
125 && (alan.label != null && annotation != null && alan.label
126 .equals(annotation.label)))
128 seqannot.put(alan.sequenceRef, alan);
134 public String getAnnotation()
136 return annotation.label;
139 public int getAboveThreshold()
141 return aboveAnnotationThreshold;
144 public float getAnnotationThreshold()
146 if (annotationThreshold == null)
152 return annotationThreshold.value;
156 public Color getMinColour()
158 return new Color((int) r1, (int) g1, (int) b1);
161 public Color getMaxColour()
163 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
172 * @return DOCUMENT ME!
174 public Color findColour(char c)
187 * @return DOCUMENT ME!
190 public Color findColour(char c, int j, SequenceI seq)
192 Color currentColour = Color.white;
193 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
195 if (annotation == null)
197 return currentColour;
199 if ((threshold == 0) || aboveThreshold(c, j))
201 if (annotation.annotations!=null && j < annotation.annotations.length
202 && annotation.annotations[j] != null
203 && !jalview.util.Comparison.isGap(c))
206 if (predefinedColours)
208 if (annotation.annotations[j].colour != null)
209 return annotation.annotations[j].colour;
211 return currentColour;
214 if (aboveAnnotationThreshold == NO_THRESHOLD
215 || (annotationThreshold != null
216 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
217 || (annotationThreshold != null
218 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
222 if (thresholdIsMinMax
223 && annotation.threshold != null
224 && aboveAnnotationThreshold == ABOVE_THRESHOLD
225 && annotation.annotations[j].value >= annotation.threshold.value)
227 range = (annotation.annotations[j].value - annotation.threshold.value)
228 / (annotation.graphMax - annotation.threshold.value);
230 else if (thresholdIsMinMax && annotation.threshold != null
231 && aboveAnnotationThreshold == BELOW_THRESHOLD
232 && annotation.annotations[j].value >= annotation.graphMin)
234 range = (annotation.annotations[j].value - annotation.graphMin)
235 / (annotation.threshold.value - annotation.graphMin);
239 range = (annotation.annotations[j].value - annotation.graphMin)
240 / (annotation.graphMax - annotation.graphMin);
243 if (colourScheme != null)
245 currentColour = colourScheme.findColour(c, j, seq);
249 dr = rr * range + r1;
250 dg = gg * range + g1;
251 db = bb * range + b1;
253 currentColour = new Color((int) dr, (int) dg, (int) db);
259 if (conservationColouring)
261 currentColour = applyConservation(currentColour, j);
264 return currentColour;
267 public boolean isPredefinedColours()
269 return predefinedColours;
272 public void setPredefinedColours(boolean predefinedColours)
274 this.predefinedColours = predefinedColours;
277 public boolean isSeqAssociated()
279 return seqAssociated;
282 public void setSeqAssociated(boolean sassoc)
284 seqAssociated = sassoc;