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