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.AlignmentI;
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.IdentityHashMap;
32 public class AnnotationColourGradient extends FollowerColourScheme
34 public static final int NO_THRESHOLD = -1;
36 public static final int BELOW_THRESHOLD = 0;
38 public static final int ABOVE_THRESHOLD = 1;
40 public AlignmentAnnotation annotation;
42 int aboveAnnotationThreshold = -1;
44 public boolean thresholdIsMinMax = false;
46 GraphLine annotationThreshold;
48 float r1, g1, b1, rr, gg, bb, dr, dg, db;
50 private boolean predefinedColours = false;
52 private boolean seqAssociated = false;
54 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
56 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
57 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
59 AnnotationColourGradient acg = new AnnotationColourGradient(annotation, colourScheme, aboveAnnotationThreshold);
60 acg.thresholdIsMinMax = thresholdIsMinMax;
61 acg.annotationThreshold = (annotationThreshold==null) ? null : new GraphLine(annotationThreshold);
71 acg.predefinedColours = predefinedColours;
72 acg.seqAssociated = seqAssociated;
77 * Creates a new AnnotationColourGradient object.
79 public AnnotationColourGradient(AlignmentAnnotation annotation,
80 ColourSchemeI originalColour, int aboveThreshold)
82 if (originalColour instanceof AnnotationColourGradient)
84 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
88 colourScheme = originalColour;
91 this.annotation = annotation;
93 aboveAnnotationThreshold = aboveThreshold;
95 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
97 annotationThreshold = annotation.threshold;
102 * Creates a new AnnotationColourGradient object.
104 public AnnotationColourGradient(AlignmentAnnotation annotation,
105 Color minColour, Color maxColour, int aboveThreshold)
107 this.annotation = annotation;
109 aboveAnnotationThreshold = aboveThreshold;
111 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
113 annotationThreshold = annotation.threshold;
116 r1 = minColour.getRed();
117 g1 = minColour.getGreen();
118 b1 = minColour.getBlue();
120 rr = maxColour.getRed() - r1;
121 gg = maxColour.getGreen() - g1;
122 bb = maxColour.getBlue() - b1;
126 public void alignmentChanged(AnnotatedCollectionI alignment,
127 Map<SequenceI, SequenceCollectionI> hiddenReps)
129 // TODO Auto-generated method stub
130 super.alignmentChanged(alignment, hiddenReps);
132 if (seqAssociated && annotation.getCalcId() != null)
134 if (seqannot != null)
140 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
142 // resolve the context containing all the annotation for the sequence
143 AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment : alignment.getContext();
144 for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
147 if (alan.sequenceRef != null
148 && (alan.label != null && annotation != null && alan.label
149 .equals(annotation.label)))
151 seqannot.put(alan.sequenceRef, alan);
157 public String getAnnotation()
159 return annotation.label;
162 public int getAboveThreshold()
164 return aboveAnnotationThreshold;
167 public float getAnnotationThreshold()
169 if (annotationThreshold == null)
175 return annotationThreshold.value;
179 public Color getMinColour()
181 return new Color((int) r1, (int) g1, (int) b1);
184 public Color getMaxColour()
186 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
195 * @return DOCUMENT ME!
197 public Color findColour(char c)
210 * @return DOCUMENT ME!
213 public Color findColour(char c, int j, SequenceI seq)
215 Color currentColour = Color.white;
216 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
218 if (annotation == null)
220 return currentColour;
222 if ((threshold == 0) || aboveThreshold(c, j))
224 if (annotation.annotations!=null && j < annotation.annotations.length
225 && annotation.annotations[j] != null
226 && !jalview.util.Comparison.isGap(c))
229 if (predefinedColours)
231 if (annotation.annotations[j].colour != null)
232 return annotation.annotations[j].colour;
234 return currentColour;
237 if (aboveAnnotationThreshold == NO_THRESHOLD
238 || (annotationThreshold != null
239 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
240 || (annotationThreshold != null
241 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
245 if (thresholdIsMinMax
246 && annotation.threshold != null
247 && aboveAnnotationThreshold == ABOVE_THRESHOLD
248 && annotation.annotations[j].value >= annotation.threshold.value)
250 range = (annotation.annotations[j].value - annotation.threshold.value)
251 / (annotation.graphMax - annotation.threshold.value);
253 else if (thresholdIsMinMax && annotation.threshold != null
254 && aboveAnnotationThreshold == BELOW_THRESHOLD
255 && annotation.annotations[j].value >= annotation.graphMin)
257 range = (annotation.annotations[j].value - annotation.graphMin)
258 / (annotation.threshold.value - annotation.graphMin);
262 range = (annotation.annotations[j].value - annotation.graphMin)
263 / (annotation.graphMax - annotation.graphMin);
266 if (colourScheme != null)
268 currentColour = colourScheme.findColour(c, j, seq);
272 dr = rr * range + r1;
273 dg = gg * range + g1;
274 db = bb * range + b1;
276 currentColour = new Color((int) dr, (int) dg, (int) db);
282 if (conservationColouring)
284 currentColour = applyConservation(currentColour, j);
287 return currentColour;
290 public boolean isPredefinedColours()
292 return predefinedColours;
295 public void setPredefinedColours(boolean predefinedColours)
297 this.predefinedColours = predefinedColours;
300 public boolean isSeqAssociated()
302 return seqAssociated;
305 public void setSeqAssociated(boolean sassoc)
307 seqAssociated = sassoc;