713e55d7ea4e94e0ff050dbd867d21a87544c162
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.schemes;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.GraphLine;
28 import jalview.datamodel.SequenceCollectionI;
29 import jalview.datamodel.SequenceI;
30
31 import java.awt.Color;
32 import java.util.IdentityHashMap;
33 import java.util.Map;
34
35 public class AnnotationColourGradient extends FollowerColourScheme
36 {
37   public static final int NO_THRESHOLD = -1;
38
39   public static final int BELOW_THRESHOLD = 0;
40
41   public static final int ABOVE_THRESHOLD = 1;
42
43   public AlignmentAnnotation annotation;
44
45   int aboveAnnotationThreshold = -1;
46
47   public boolean thresholdIsMinMax = false;
48
49   GraphLine annotationThreshold;
50
51   float r1, g1, b1, rr, gg, bb;
52
53   private boolean predefinedColours = false;
54
55   private boolean seqAssociated = false;
56
57   /**
58    * false if the scheme was constructed without a minColour and maxColour used
59    * to decide if existing colours should be taken from annotation elements when
60    * they exist
61    */
62   private boolean noGradient = false;
63   IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
64
65   @Override
66   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
67           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
68   {
69     AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
70             colourScheme, aboveAnnotationThreshold);
71     acg.thresholdIsMinMax = thresholdIsMinMax;
72     acg.annotationThreshold = (annotationThreshold == null) ? null
73             : new GraphLine(annotationThreshold);
74     acg.r1 = r1;
75     acg.g1 = g1;
76     acg.b1 = b1;
77     acg.rr = rr;
78     acg.gg = gg;
79     acg.bb = bb;
80     acg.predefinedColours = predefinedColours;
81     acg.seqAssociated = seqAssociated;
82     acg.noGradient = noGradient;
83     return acg;
84   }
85
86   /**
87    * Creates a new AnnotationColourGradient object.
88    */
89   public AnnotationColourGradient(AlignmentAnnotation annotation,
90           ColourSchemeI originalColour, int aboveThreshold)
91   {
92     if (originalColour instanceof AnnotationColourGradient)
93     {
94       colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
95     }
96     else
97     {
98       colourScheme = originalColour;
99     }
100
101     this.annotation = annotation;
102
103     aboveAnnotationThreshold = aboveThreshold;
104
105     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
106     {
107       annotationThreshold = annotation.threshold;
108     }
109     // clear values so we don't get weird black bands...
110     r1 = 254;
111     g1 = 254;
112     b1 = 254;
113     rr = 0;
114     gg = 0;
115     bb = 0;
116
117     noGradient = true;
118   }
119
120   /**
121    * Creates a new AnnotationColourGradient object.
122    */
123   public AnnotationColourGradient(AlignmentAnnotation annotation,
124           Color minColour, Color maxColour, int aboveThreshold)
125   {
126     this.annotation = annotation;
127
128     aboveAnnotationThreshold = aboveThreshold;
129
130     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
131     {
132       annotationThreshold = annotation.threshold;
133     }
134
135     r1 = minColour.getRed();
136     g1 = minColour.getGreen();
137     b1 = minColour.getBlue();
138
139     rr = maxColour.getRed() - r1;
140     gg = maxColour.getGreen() - g1;
141     bb = maxColour.getBlue() - b1;
142
143     noGradient = false;
144   }
145
146   @Override
147   public void alignmentChanged(AnnotatedCollectionI alignment,
148           Map<SequenceI, SequenceCollectionI> hiddenReps)
149   {
150     super.alignmentChanged(alignment, hiddenReps);
151
152     if (seqAssociated && annotation.getCalcId() != null)
153     {
154       if (seqannot != null)
155       {
156         seqannot.clear();
157       }
158       else
159       {
160         seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
161       }
162       // resolve the context containing all the annotation for the sequence
163       AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
164               : alignment.getContext();
165       for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
166               .getCalcId()))
167       {
168         if (alan.sequenceRef != null
169                 && (alan.label != null && annotation != null && alan.label
170                         .equals(annotation.label)))
171         {
172           seqannot.put(alan.sequenceRef, alan);
173         }
174       }
175     }
176   }
177
178   public String getAnnotation()
179   {
180     return annotation.label;
181   }
182
183   public int getAboveThreshold()
184   {
185     return aboveAnnotationThreshold;
186   }
187
188   public float getAnnotationThreshold()
189   {
190     if (annotationThreshold == null)
191     {
192       return 0;
193     }
194     else
195     {
196       return annotationThreshold.value;
197     }
198   }
199
200   public Color getMinColour()
201   {
202     return new Color((int) r1, (int) g1, (int) b1);
203   }
204
205   public Color getMaxColour()
206   {
207     return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
208   }
209
210   /**
211    * DOCUMENT ME!
212    * 
213    * @param n
214    *          DOCUMENT ME!
215    * 
216    * @return DOCUMENT ME!
217    */
218   public Color findColour(char c)
219   {
220     return Color.red;
221   }
222
223   /**
224    * DOCUMENT ME!
225    * 
226    * @param n
227    *          DOCUMENT ME!
228    * @param j
229    *          DOCUMENT ME!
230    * 
231    * @return DOCUMENT ME!
232    */
233   @Override
234   public Color findColour(char c, int j, SequenceI seq)
235   {
236     Color currentColour = Color.white;
237     AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
238             : this.annotation);
239     if (annotation == null)
240     {
241       return currentColour;
242     }
243     if ((threshold == 0) || aboveThreshold(c, j))
244     {
245       if (annotation.annotations != null
246               && j < annotation.annotations.length
247               && annotation.annotations[j] != null
248               && !jalview.util.Comparison.isGap(c))
249       {
250         Annotation aj = annotation.annotations[j];
251         // 'use original colours' => colourScheme != null
252         // -> look up colour to be used
253         // predefined colours => preconfigured shading
254         // -> only use original colours reference if thresholding enabled &
255         // minmax exists
256         // annotation.hasIcons => null or black colours replaced with glyph
257         // colours
258         // -> reuse original colours if present
259         // -> if thresholding enabled then return colour on non-whitespace glyph
260
261         if (aboveAnnotationThreshold == NO_THRESHOLD
262                 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
263                         : aj.value <= annotationThreshold.value)))
264         {
265           if (predefinedColours && aj.colour != null)
266           {
267             currentColour = aj.colour;
268           }
269           else if (annotation.hasIcons
270                   && annotation.graph == AlignmentAnnotation.NO_GRAPH)
271           {
272             if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
273                     && aj.secondaryStructure != '-')
274             {
275               if (colourScheme != null)
276               {
277                 currentColour = colourScheme.findColour(c, j, seq);
278               }
279               else
280               {
281               currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
282                       : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
283                               : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
284               }
285             }
286             else
287             {
288               //
289               return Color.white;
290             }
291           }
292           else if (noGradient)
293           {
294             if (colourScheme != null)
295             {
296               currentColour = colourScheme.findColour(c, j, seq);
297             }
298             else
299             {
300               if (aj.colour != null)
301               {
302                 currentColour = aj.colour;
303               }
304             }
305           }
306           else
307           {
308             // calculate a shade
309             float range = 1f;
310             if (thresholdIsMinMax
311                     && annotation.threshold != null
312                     && aboveAnnotationThreshold == ABOVE_THRESHOLD
313                     && annotation.annotations[j].value >= annotation.threshold.value)
314             {
315               range = (annotation.annotations[j].value - annotation.threshold.value)
316                       / (annotation.graphMax - annotation.threshold.value);
317             }
318             else if (thresholdIsMinMax
319                     && annotation.threshold != null
320                     && aboveAnnotationThreshold == BELOW_THRESHOLD
321                     && annotation.annotations[j].value >= annotation.graphMin)
322             {
323               range = (annotation.annotations[j].value - annotation.graphMin)
324                       / (annotation.threshold.value - annotation.graphMin);
325             }
326             else
327             {
328               range = (annotation.annotations[j].value - annotation.graphMin)
329                       / (annotation.graphMax - annotation.graphMin);
330             }
331
332             int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
333                     * range + b1);
334
335             currentColour = new Color(dr, dg, db);
336
337           }
338         }
339         if (conservationColouring)
340         {
341           currentColour = applyConservation(currentColour, j);
342         }
343       }
344     }
345     return currentColour;
346   }
347
348   public boolean isPredefinedColours()
349   {
350     return predefinedColours;
351   }
352
353   public void setPredefinedColours(boolean predefinedColours)
354   {
355     this.predefinedColours = predefinedColours;
356   }
357
358   public boolean isSeqAssociated()
359   {
360     return seqAssociated;
361   }
362
363   public void setSeqAssociated(boolean sassoc)
364   {
365     seqAssociated = sassoc;
366   }
367 }