3cada8bdbce4505660b9eb9c3f5391cc2e2fbad3
[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    * 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   }
118
119   /**
120    * Creates a new AnnotationColourGradient object.
121    */
122   public AnnotationColourGradient(AlignmentAnnotation annotation,
123           Color minColour, Color maxColour, int aboveThreshold)
124   {
125     this.annotation = annotation;
126
127     aboveAnnotationThreshold = aboveThreshold;
128
129     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
130     {
131       annotationThreshold = annotation.threshold;
132     }
133
134     r1 = minColour.getRed();
135     g1 = minColour.getGreen();
136     b1 = minColour.getBlue();
137
138     rr = maxColour.getRed() - r1;
139     gg = maxColour.getGreen() - g1;
140     bb = maxColour.getBlue() - b1;
141
142     noGradient = false;
143     aamax = annotation.graphMax;
144     aamin = annotation.graphMin;
145     if (annotation.isRNA())
146     {
147       ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
148     }
149   }
150
151   @Override
152   public void alignmentChanged(AnnotatedCollectionI alignment,
153           Map<SequenceI, SequenceCollectionI> hiddenReps)
154   {
155     super.alignmentChanged(alignment, hiddenReps);
156
157     if (seqAssociated && annotation.getCalcId() != null)
158     {
159       if (seqannot != null)
160       {
161         seqannot.clear();
162       }
163       else
164       {
165         seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
166       }
167       // resolve the context containing all the annotation for the sequence
168       AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
169               : alignment.getContext();
170       boolean f = true,rna=false;
171       for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
172               .getCalcId()))
173       {
174         if (alan.sequenceRef != null
175                 && (alan.label != null && annotation != null && alan.label
176                         .equals(annotation.label)))
177         {
178           if (!rna && alan.isRNA())
179           {
180             rna = true;
181           }
182           seqannot.put(alan.sequenceRef, alan);
183           if (f || alan.graphMax > aamax)
184           {
185             aamax = alan.graphMax;
186           }
187           if (f || alan.graphMin < aamin)
188           {
189             aamin = alan.graphMin;
190           }
191           f = false;
192         }
193       }
194       if (rna)
195       {
196         ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
197       }
198     }
199   }
200
201   float aamin = 0f, aamax = 0f;
202   public String getAnnotation()
203   {
204     return annotation.label;
205   }
206
207   public int getAboveThreshold()
208   {
209     return aboveAnnotationThreshold;
210   }
211
212   public float getAnnotationThreshold()
213   {
214     if (annotationThreshold == null)
215     {
216       return 0;
217     }
218     else
219     {
220       return annotationThreshold.value;
221     }
222   }
223
224   public Color getMinColour()
225   {
226     return new Color((int) r1, (int) g1, (int) b1);
227   }
228
229   public Color getMaxColour()
230   {
231     return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
232   }
233
234   /**
235    * DOCUMENT ME!
236    * 
237    * @param n
238    *          DOCUMENT ME!
239    * 
240    * @return DOCUMENT ME!
241    */
242   public Color findColour(char c)
243   {
244     return Color.red;
245   }
246
247   /**
248    * DOCUMENT ME!
249    * 
250    * @param n
251    *          DOCUMENT ME!
252    * @param j
253    *          DOCUMENT ME!
254    * 
255    * @return DOCUMENT ME!
256    */
257   @Override
258   public Color findColour(char c, int j, SequenceI seq)
259   {
260     Color currentColour = Color.white;
261     AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
262             : this.annotation);
263     if (annotation == null)
264     {
265       return currentColour;
266     }
267     if ((threshold == 0) || aboveThreshold(c, j))
268     {
269       if (annotation.annotations != null
270               && j < annotation.annotations.length
271               && annotation.annotations[j] != null
272               && !jalview.util.Comparison.isGap(c))
273       {
274         Annotation aj = annotation.annotations[j];
275         // 'use original colours' => colourScheme != null
276         // -> look up colour to be used
277         // predefined colours => preconfigured shading
278         // -> only use original colours reference if thresholding enabled &
279         // minmax exists
280         // annotation.hasIcons => null or black colours replaced with glyph
281         // colours
282         // -> reuse original colours if present
283         // -> if thresholding enabled then return colour on non-whitespace glyph
284
285         if (aboveAnnotationThreshold == NO_THRESHOLD
286                 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
287                         : aj.value <= annotationThreshold.value)))
288         {
289           if (predefinedColours && aj.colour != null)
290           {
291             currentColour = aj.colour;
292           }
293           else if (annotation.hasIcons
294                   && annotation.graph == AlignmentAnnotation.NO_GRAPH)
295           {
296             if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
297                     && aj.secondaryStructure != '-')
298             {
299               if (colourScheme != null)
300               {
301                 currentColour = colourScheme.findColour(c, j, seq);
302               }
303               else
304               {
305                 if (annotation.isRNA())
306                 {
307                   currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
308                 }
309                 else
310                 {
311                   currentColour = annotation.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
312                           : annotation.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
313                                   : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
314                 }
315               }
316             }
317             else
318             {
319               //
320               return Color.white;
321             }
322           }
323           else if (noGradient)
324           {
325             if (colourScheme != null)
326             {
327               currentColour = colourScheme.findColour(c, j, seq);
328             }
329             else
330             {
331               if (aj.colour != null)
332               {
333                 currentColour = aj.colour;
334               }
335             }
336           }
337           else
338           {
339             currentColour = shadeCalculation(annotation, j);
340           }
341         }
342         if (conservationColouring)
343         {
344           currentColour = applyConservation(currentColour, j);
345         }
346       }
347     }
348     return currentColour;
349   }
350
351   private Color shadeCalculation(AlignmentAnnotation annotation, int j)
352   {
353
354     // calculate a shade
355     float range = 1f;
356     if (thresholdIsMinMax
357             && annotation.threshold != null
358             && aboveAnnotationThreshold == ABOVE_THRESHOLD
359             && annotation.annotations[j].value >= annotation.threshold.value)
360     {
361       range = (annotation.annotations[j].value - annotation.threshold.value)
362               / (annotation.graphMax - annotation.threshold.value);
363     }
364     else if (thresholdIsMinMax && annotation.threshold != null
365             && aboveAnnotationThreshold == BELOW_THRESHOLD
366             && annotation.annotations[j].value >= annotation.graphMin)
367     {
368       range = (annotation.annotations[j].value - annotation.graphMin)
369               / (annotation.threshold.value - annotation.graphMin);
370     }
371     else
372     {
373       if (annotation.graphMax != annotation.graphMin)
374       {
375         range = (annotation.annotations[j].value - annotation.graphMin)
376                 / (annotation.graphMax - annotation.graphMin);
377       }
378       else
379       {
380         range = 0f;
381       }
382     }
383
384     int dr = (int) (rr * range + r1), dg = (int) (gg * range + g1), db = (int) (bb
385             * range + b1);
386
387     return new Color(dr, dg, db);
388
389   }
390   public boolean isPredefinedColours()
391   {
392     return predefinedColours;
393   }
394
395   public void setPredefinedColours(boolean predefinedColours)
396   {
397     this.predefinedColours = predefinedColours;
398   }
399
400   public boolean isSeqAssociated()
401   {
402     return seqAssociated;
403   }
404
405   public void setSeqAssociated(boolean sassoc)
406   {
407     seqAssociated = sassoc;
408   }
409 }