JAL-1871 choose a new RNA Helices palette when annotation colouring is updated
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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    * false if the scheme was constructed without a minColour and maxColour used
58    * to decide if existing colours should be taken from annotation elements when
59    * they exist
60    */
61   private boolean noGradient = false;
62   IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
63
64   @Override
65   public ColourSchemeI applyTo(AnnotatedCollectionI sg,
66           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
67   {
68     AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
69             colourScheme, aboveAnnotationThreshold);
70     acg.thresholdIsMinMax = thresholdIsMinMax;
71     acg.annotationThreshold = (annotationThreshold == null) ? null
72             : new GraphLine(annotationThreshold);
73     acg.r1 = r1;
74     acg.g1 = g1;
75     acg.b1 = b1;
76     acg.rr = rr;
77     acg.gg = gg;
78     acg.bb = bb;
79     acg.predefinedColours = predefinedColours;
80     acg.seqAssociated = seqAssociated;
81     acg.noGradient = noGradient;
82     return acg;
83   }
84
85   /**
86    * Creates a new AnnotationColourGradient object.
87    */
88   public AnnotationColourGradient(AlignmentAnnotation annotation,
89           ColourSchemeI originalColour, int aboveThreshold)
90   {
91     if (originalColour instanceof AnnotationColourGradient)
92     {
93       colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
94     }
95     else
96     {
97       colourScheme = originalColour;
98     }
99
100     this.annotation = annotation;
101
102     aboveAnnotationThreshold = aboveThreshold;
103
104     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
105     {
106       annotationThreshold = annotation.threshold;
107     }
108     // clear values so we don't get weird black bands...
109     r1 = 254;
110     g1 = 254;
111     b1 = 254;
112     rr = 0;
113     gg = 0;
114     bb = 0;
115
116     noGradient = true;
117     checkLimits();
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     checkLimits();
145   }
146
147   private void checkLimits()
148   {
149     aamax = annotation.graphMax;
150     aamin = annotation.graphMin;
151     if (annotation.isRNA())
152     {
153       // reset colour palette
154       ColourSchemeProperty.resetRnaHelicesShading();
155       ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
156     }
157   }
158
159   @Override
160   public void alignmentChanged(AnnotatedCollectionI alignment,
161           Map<SequenceI, SequenceCollectionI> hiddenReps)
162   {
163     super.alignmentChanged(alignment, hiddenReps);
164
165     if (seqAssociated && annotation.getCalcId() != null)
166     {
167       if (seqannot != null)
168       {
169         seqannot.clear();
170       }
171       else
172       {
173         seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
174       }
175       // resolve the context containing all the annotation for the sequence
176       AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
177               : alignment.getContext();
178       boolean f = true,rna=false;
179       for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
180               .getCalcId()))
181       {
182         if (alan.sequenceRef != null
183                 && (alan.label != null && annotation != null && alan.label
184                         .equals(annotation.label)))
185         {
186           if (!rna && alan.isRNA())
187           {
188             rna = true;
189           }
190           seqannot.put(alan.sequenceRef, alan);
191           if (f || alan.graphMax > aamax)
192           {
193             aamax = alan.graphMax;
194           }
195           if (f || alan.graphMin < aamin)
196           {
197             aamin = alan.graphMin;
198           }
199           f = false;
200         }
201       }
202       if (rna)
203       {
204         ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
205       }
206     }
207   }
208
209   float aamin = 0f, aamax = 0f;
210   public String getAnnotation()
211   {
212     return annotation.label;
213   }
214
215   public int getAboveThreshold()
216   {
217     return aboveAnnotationThreshold;
218   }
219
220   public float getAnnotationThreshold()
221   {
222     if (annotationThreshold == null)
223     {
224       return 0;
225     }
226     else
227     {
228       return annotationThreshold.value;
229     }
230   }
231
232   public Color getMinColour()
233   {
234     return new Color((int) r1, (int) g1, (int) b1);
235   }
236
237   public Color getMaxColour()
238   {
239     return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
240   }
241
242   /**
243    * DOCUMENT ME!
244    * 
245    * @param n
246    *          DOCUMENT ME!
247    * 
248    * @return DOCUMENT ME!
249    */
250   public Color findColour(char c)
251   {
252     return Color.red;
253   }
254
255   /**
256    * DOCUMENT ME!
257    * 
258    * @param n
259    *          DOCUMENT ME!
260    * @param j
261    *          DOCUMENT ME!
262    * 
263    * @return DOCUMENT ME!
264    */
265   @Override
266   public Color findColour(char c, int j, SequenceI seq)
267   {
268     Color currentColour = Color.white;
269     AlignmentAnnotation annotation = (seqAssociated && seqannot!=null ? seqannot.get(seq)
270             : this.annotation);
271     if (annotation == null)
272     {
273       return currentColour;
274     }
275     if ((threshold == 0) || aboveThreshold(c, j))
276     {
277       if (annotation.annotations != null
278               && j < annotation.annotations.length
279               && annotation.annotations[j] != null
280               && !jalview.util.Comparison.isGap(c))
281       {
282         Annotation aj = annotation.annotations[j];
283         // 'use original colours' => colourScheme != null
284         // -> look up colour to be used
285         // predefined colours => preconfigured shading
286         // -> only use original colours reference if thresholding enabled &
287         // minmax exists
288         // annotation.hasIcons => null or black colours replaced with glyph
289         // colours
290         // -> reuse original colours if present
291         // -> if thresholding enabled then return colour on non-whitespace glyph
292
293         if (aboveAnnotationThreshold == NO_THRESHOLD
294                 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
295                         : aj.value <= annotationThreshold.value)))
296         {
297           if (predefinedColours && aj.colour != null
298                   && !aj.colour.equals(Color.black))
299           {
300             currentColour = aj.colour;
301           }
302           else if (annotation.hasIcons
303                   && annotation.graph == AlignmentAnnotation.NO_GRAPH)
304           {
305             if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
306                     && aj.secondaryStructure != '-')
307             {
308               if (colourScheme != null)
309               {
310                 currentColour = colourScheme.findColour(c, j, seq);
311               }
312               else
313               {
314                 if (annotation.isRNA())
315                 {
316                   currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
317                 }
318                 else
319                 {
320                   currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
321                           : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
322                                   : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
323                 }
324               }
325             }
326             else
327             {
328               //
329               return Color.white;
330             }
331           }
332           else if (noGradient)
333           {
334             if (colourScheme != null)
335             {
336               currentColour = colourScheme.findColour(c, j, seq);
337             }
338             else
339             {
340               if (aj.colour != null)
341               {
342                 currentColour = aj.colour;
343               }
344             }
345           }
346           else
347           {
348             currentColour = shadeCalculation(annotation, j);
349           }
350         }
351         if (conservationColouring)
352         {
353           currentColour = applyConservation(currentColour, j);
354         }
355       }
356     }
357     return currentColour;
358   }
359
360   private Color shadeCalculation(AlignmentAnnotation annotation, int j)
361   {
362
363     // calculate a shade
364     float range = 1f;
365     if (thresholdIsMinMax
366             && annotation.threshold != null
367             && aboveAnnotationThreshold == ABOVE_THRESHOLD
368             && annotation.annotations[j].value >= annotation.threshold.value)
369     {
370       range = (annotation.annotations[j].value - annotation.threshold.value)
371               / (annotation.graphMax - annotation.threshold.value);
372     }
373     else if (thresholdIsMinMax && annotation.threshold != null
374             && aboveAnnotationThreshold == BELOW_THRESHOLD
375             && annotation.annotations[j].value >= annotation.graphMin)
376     {
377       range = (annotation.annotations[j].value - annotation.graphMin)
378               / (annotation.threshold.value - annotation.graphMin);
379     }
380     else
381     {
382       if (annotation.graphMax != annotation.graphMin)
383       {
384         range = (annotation.annotations[j].value - annotation.graphMin)
385                 / (annotation.graphMax - annotation.graphMin);
386       }
387       else
388       {
389         range = 0f;
390       }
391     }
392
393     int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
394             * range + b1);
395
396     return new Color(dr, dg, db);
397
398   }
399   public boolean isPredefinedColours()
400   {
401     return predefinedColours;
402   }
403
404   public void setPredefinedColours(boolean predefinedColours)
405   {
406     this.predefinedColours = predefinedColours;
407   }
408
409   public boolean isSeqAssociated()
410   {
411     return seqAssociated;
412   }
413
414   public void setSeqAssociated(boolean sassoc)
415   {
416     seqAssociated = sassoc;
417   }
418 }