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 ResidueColourScheme
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 ColourSchemeI colourScheme;
51 public boolean predefinedColours = false;
53 public boolean seqAssociated = false;
55 IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
58 * Creates a new AnnotationColourGradient object.
60 public AnnotationColourGradient(AlignmentAnnotation annotation,
61 ColourSchemeI originalColour, int aboveThreshold)
63 if (originalColour instanceof AnnotationColourGradient)
65 colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
69 colourScheme = originalColour;
72 this.annotation = annotation;
74 aboveAnnotationThreshold = aboveThreshold;
76 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
78 annotationThreshold = annotation.threshold;
83 * Creates a new AnnotationColourGradient object.
85 public AnnotationColourGradient(AlignmentAnnotation annotation,
86 Color minColour, Color maxColour, int aboveThreshold)
88 this.annotation = annotation;
90 aboveAnnotationThreshold = aboveThreshold;
92 if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
94 annotationThreshold = annotation.threshold;
97 r1 = minColour.getRed();
98 g1 = minColour.getGreen();
99 b1 = minColour.getBlue();
101 rr = maxColour.getRed() - r1;
102 gg = maxColour.getGreen() - g1;
103 bb = maxColour.getBlue() - b1;
107 public void alignmentChanged(AnnotatedCollectionI alignment,
108 Map<SequenceI, SequenceCollectionI> hiddenReps)
110 // TODO Auto-generated method stub
111 super.alignmentChanged(alignment, hiddenReps);
113 if (seqAssociated && annotation.getCalcId() != null)
115 if (seqannot != null)
121 seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
123 for (AlignmentAnnotation alan : alignment.findAnnotation(annotation
126 if (alan.sequenceRef != null
127 && (alan.label != null && annotation != null && alan.label
128 .equals(annotation.label)))
130 seqannot.put(alan.sequenceRef, alan);
136 public String getAnnotation()
138 return annotation.label;
141 public int getAboveThreshold()
143 return aboveAnnotationThreshold;
146 public float getAnnotationThreshold()
148 if (annotationThreshold == null)
154 return annotationThreshold.value;
158 public ColourSchemeI getBaseColour()
163 public Color getMinColour()
165 return new Color((int) r1, (int) g1, (int) b1);
168 public Color getMaxColour()
170 return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
179 * @return DOCUMENT ME!
181 public Color findColour(char c)
194 * @return DOCUMENT ME!
197 public Color findColour(char c, int j, SequenceI seq)
199 Color currentColour = Color.white;
200 AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
202 if (annotation == null)
204 return currentColour;
206 if ((threshold == 0) || aboveThreshold(c, j))
208 if (j < annotation.annotations.length
209 && annotation.annotations[j] != null
210 && !jalview.util.Comparison.isGap(c))
213 if (predefinedColours)
215 if (annotation.annotations[j].colour != null)
216 return annotation.annotations[j].colour;
218 return currentColour;
221 if (aboveAnnotationThreshold == NO_THRESHOLD
222 || (annotationThreshold != null
223 && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
224 || (annotationThreshold != null
225 && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
229 if (thresholdIsMinMax
230 && annotation.threshold != null
231 && aboveAnnotationThreshold == ABOVE_THRESHOLD
232 && annotation.annotations[j].value >= annotation.threshold.value)
234 range = (annotation.annotations[j].value - annotation.threshold.value)
235 / (annotation.graphMax - annotation.threshold.value);
237 else if (thresholdIsMinMax && annotation.threshold != null
238 && aboveAnnotationThreshold == BELOW_THRESHOLD
239 && annotation.annotations[j].value >= annotation.graphMin)
241 range = (annotation.annotations[j].value - annotation.graphMin)
242 / (annotation.threshold.value - annotation.graphMin);
246 range = (annotation.annotations[j].value - annotation.graphMin)
247 / (annotation.graphMax - annotation.graphMin);
250 if (colourScheme != null)
252 currentColour = colourScheme.findColour(c, j, seq);
256 dr = rr * range + r1;
257 dg = gg * range + g1;
258 db = bb * range + b1;
260 currentColour = new Color((int) dr, (int) dg, (int) db);
266 if (conservationColouring)
268 currentColour = applyConservation(currentColour, j);
271 return currentColour;
274 public boolean isSeqAssociated()
276 return seqAssociated;
279 public void setSeqAssociated(boolean sassoc)
281 seqAssociated = sassoc;