update author list in license for (JAL-826)
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
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.
15  * 
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  */
18 package jalview.schemes;
19
20 import java.awt.*;
21
22 import jalview.datamodel.*;
23
24 public class AnnotationColourGradient extends ResidueColourScheme
25 {
26   public static final int NO_THRESHOLD = -1;
27
28   public static final int BELOW_THRESHOLD = 0;
29
30   public static final int ABOVE_THRESHOLD = 1;
31
32   public AlignmentAnnotation annotation;
33
34   int aboveAnnotationThreshold = -1;
35
36   public boolean thresholdIsMinMax = false;
37
38   GraphLine annotationThreshold;
39
40   float r1, g1, b1, rr, gg, bb, dr, dg, db;
41
42   ColourSchemeI colourScheme;
43
44   public boolean predefinedColours = false;
45
46   /**
47    * Creates a new AnnotationColourGradient object.
48    */
49   public AnnotationColourGradient(AlignmentAnnotation annotation,
50           ColourSchemeI originalColour, int aboveThreshold)
51   {
52     if (originalColour instanceof AnnotationColourGradient)
53     {
54       colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
55     }
56     else
57     {
58       colourScheme = originalColour;
59     }
60
61     this.annotation = annotation;
62
63     aboveAnnotationThreshold = aboveThreshold;
64
65     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
66     {
67       annotationThreshold = annotation.threshold;
68     }
69   }
70
71   /**
72    * Creates a new AnnotationColourGradient object.
73    */
74   public AnnotationColourGradient(AlignmentAnnotation annotation,
75           Color minColour, Color maxColour, int aboveThreshold)
76   {
77     this.annotation = annotation;
78
79     aboveAnnotationThreshold = aboveThreshold;
80
81     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
82     {
83       annotationThreshold = annotation.threshold;
84     }
85
86     r1 = minColour.getRed();
87     g1 = minColour.getGreen();
88     b1 = minColour.getBlue();
89
90     rr = maxColour.getRed() - r1;
91     gg = maxColour.getGreen() - g1;
92     bb = maxColour.getBlue() - b1;
93   }
94
95   public String getAnnotation()
96   {
97     return annotation.label;
98   }
99
100   public int getAboveThreshold()
101   {
102     return aboveAnnotationThreshold;
103   }
104
105   public float getAnnotationThreshold()
106   {
107     if (annotationThreshold == null)
108     {
109       return 0;
110     }
111     else
112     {
113       return annotationThreshold.value;
114     }
115   }
116
117   public ColourSchemeI getBaseColour()
118   {
119     return colourScheme;
120   }
121
122   public Color getMinColour()
123   {
124     return new Color((int) r1, (int) g1, (int) b1);
125   }
126
127   public Color getMaxColour()
128   {
129     return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
130   }
131
132   /**
133    * DOCUMENT ME!
134    * 
135    * @param n
136    *          DOCUMENT ME!
137    * 
138    * @return DOCUMENT ME!
139    */
140   public Color findColour(char c)
141   {
142     return Color.red;
143   }
144
145   /**
146    * DOCUMENT ME!
147    * 
148    * @param n
149    *          DOCUMENT ME!
150    * @param j
151    *          DOCUMENT ME!
152    * 
153    * @return DOCUMENT ME!
154    */
155   public Color findColour(char c, int j)
156   {
157     Color currentColour = Color.white;
158
159     if ((threshold == 0) || aboveThreshold(c, j))
160     {
161       if (j < annotation.annotations.length
162               && annotation.annotations[j] != null
163               && !jalview.util.Comparison.isGap(c))
164       {
165
166         if (predefinedColours)
167         {
168           if (annotation.annotations[j].colour != null)
169             return annotation.annotations[j].colour;
170           else
171             return currentColour;
172         }
173
174         if (aboveAnnotationThreshold == NO_THRESHOLD
175                 || (annotationThreshold != null
176                         && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
177                 || (annotationThreshold != null
178                         && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
179         {
180
181           float range = 1f;
182           if (thresholdIsMinMax
183                   && annotation.threshold != null
184                   && aboveAnnotationThreshold == ABOVE_THRESHOLD
185                   && annotation.annotations[j].value > annotation.threshold.value)
186           {
187             range = (annotation.annotations[j].value - annotation.threshold.value)
188                     / (annotation.graphMax - annotation.threshold.value);
189           }
190           else if (thresholdIsMinMax && annotation.threshold != null
191                   && aboveAnnotationThreshold == BELOW_THRESHOLD
192                   && annotation.annotations[j].value > annotation.graphMin)
193           {
194             range = (annotation.annotations[j].value - annotation.graphMin)
195                     / (annotation.threshold.value - annotation.graphMin);
196           }
197           else
198           {
199             range = (annotation.annotations[j].value - annotation.graphMin)
200                     / (annotation.graphMax - annotation.graphMin);
201           }
202
203           if (colourScheme != null)
204           {
205             currentColour = colourScheme.findColour(c, j);
206           }
207           else if (range != 0)
208           {
209             dr = rr * range + r1;
210             dg = gg * range + g1;
211             db = bb * range + b1;
212
213             currentColour = new Color((int) dr, (int) dg, (int) db);
214           }
215         }
216       }
217     }
218
219     if (conservationColouring)
220     {
221       currentColour = applyConservation(currentColour, j);
222     }
223
224     return currentColour;
225   }
226 }