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;
55 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
56 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
58 AnnotationColourGradient acg = new AnnotationColourGradient(annotation, colourScheme, aboveAnnotationThreshold);
59 acg.thresholdIsMinMax = thresholdIsMinMax;
60 acg.annotationThreshold = (annotationThreshold==null) ? null : new GraphLine(annotationThreshold);
70 acg.predefinedColours = predefinedColours;
71 acg.seqAssociated = seqAssociated;
76 * Creates a new AnnotationColourGradient object.
78 public AnnotationColourGradient(AlignmentAnnotation annotation,
79 ColourSchemeI originalColour, int aboveThreshold)
81 if (originalColour instanceof AnnotationColourGradient)
83 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
87 colourScheme = originalColour;
90 this.annotation = annotation;
92 aboveAnnotationThreshold = aboveThreshold;
94 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
96 annotationThreshold = annotation.threshold;
101 * Creates a new AnnotationColourGradient object.
103 public AnnotationColourGradient(AlignmentAnnotation annotation,
104 Color minColour, Color maxColour, int aboveThreshold)
106 this.annotation = annotation;
108 aboveAnnotationThreshold = aboveThreshold;
110 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
112 annotationThreshold = annotation.threshold;
115 r1 = minColour.getRed();
116 g1 = minColour.getGreen();
117 b1 = minColour.getBlue();
119 rr = maxColour.getRed() - r1;
120 gg = maxColour.getGreen() - g1;
121 bb = maxColour.getBlue() - b1;
125 public void alignmentChanged(AnnotatedCollectionI alignment,
126 Map<SequenceI, SequenceCollectionI> hiddenReps)
128 // TODO Auto-generated method stub
129 super.alignmentChanged(alignment, hiddenReps);
131 if (seqAssociated && annotation.getCalcId() != null)
133 if (seqannot != null)
139 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
141 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
144 if (alan.sequenceRef != null
145 && (alan.label != null && annotation != null && alan.label
146 .equals(annotation.label)))
148 seqannot.put(alan.sequenceRef, alan);
154 public String getAnnotation()
156 return annotation.label;
159 public int getAboveThreshold()
161 return aboveAnnotationThreshold;
164 public float getAnnotationThreshold()
166 if (annotationThreshold == null)
172 return annotationThreshold.value;
176 public Color getMinColour()
178 return new Color((int) r1, (int) g1, (int) b1);
181 public Color getMaxColour()
183 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
192 * @return DOCUMENT ME!
194 public Color findColour(char c)
207 * @return DOCUMENT ME!
210 public Color findColour(char c, int j, SequenceI seq)
212 Color currentColour = Color.white;
213 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
215 if (annotation == null)
217 return currentColour;
219 if ((threshold == 0) || aboveThreshold(c, j))
221 if (annotation.annotations!=null && j < annotation.annotations.length
222 && annotation.annotations[j] != null
223 && !jalview.util.Comparison.isGap(c))
226 if (predefinedColours)
228 if (annotation.annotations[j].colour != null)
229 return annotation.annotations[j].colour;
231 return currentColour;
234 if (aboveAnnotationThreshold == NO_THRESHOLD
235 || (annotationThreshold != null
236 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
237 || (annotationThreshold != null
238 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
242 if (thresholdIsMinMax
243 && annotation.threshold != null
244 && aboveAnnotationThreshold == ABOVE_THRESHOLD
245 && annotation.annotations[j].value >= annotation.threshold.value)
247 range = (annotation.annotations[j].value - annotation.threshold.value)
248 / (annotation.graphMax - annotation.threshold.value);
250 else if (thresholdIsMinMax && annotation.threshold != null
251 && aboveAnnotationThreshold == BELOW_THRESHOLD
252 && annotation.annotations[j].value >= annotation.graphMin)
254 range = (annotation.annotations[j].value - annotation.graphMin)
255 / (annotation.threshold.value - annotation.graphMin);
259 range = (annotation.annotations[j].value - annotation.graphMin)
260 / (annotation.graphMax - annotation.graphMin);
263 if (colourScheme != null)
265 currentColour = colourScheme.findColour(c, j, seq);
269 dr = rr * range + r1;
270 dg = gg * range + g1;
271 db = bb * range + b1;
273 currentColour = new Color((int) dr, (int) dg, (int) db);
279 if (conservationColouring)
281 currentColour = applyConservation(currentColour, j);
284 return currentColour;
287 public boolean isPredefinedColours()
289 return predefinedColours;
292 public void setPredefinedColours(boolean predefinedColours)
294 this.predefinedColours = predefinedColours;
297 public boolean isSeqAssociated()
299 return seqAssociated;
302 public void setSeqAssociated(boolean sassoc)
304 seqAssociated = sassoc;