Annotation gradient will use direct colours if no graph exists
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.schemes;\r
20 \r
21 import java.awt.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 public class AnnotationColourGradient\r
26     extends ResidueColourScheme\r
27 {\r
28   public static int NO_THRESHOLD = -1;\r
29   public static int BELOW_THRESHOLD = 0;\r
30   public static int ABOVE_THRESHOLD = 1;\r
31 \r
32   public AlignmentAnnotation annotation;\r
33   int aboveAnnotationThreshold = -1;\r
34   public boolean thresholdIsMinMax = false;\r
35 \r
36   GraphLine annotationThreshold;\r
37 \r
38   float r1, g1, b1, rr, gg, bb, dr, dg, db;\r
39 \r
40   ColourSchemeI colourScheme;\r
41 \r
42   public boolean predefinedColours = false;\r
43 \r
44   /**\r
45    * Creates a new AnnotationColourGradient object.\r
46    */\r
47   public AnnotationColourGradient(AlignmentAnnotation annotation,\r
48                                   ColourSchemeI originalColour,\r
49                                   int aboveThreshold)\r
50   {\r
51     if (originalColour instanceof AnnotationColourGradient)\r
52     {\r
53       colourScheme = ( (AnnotationColourGradient) originalColour).colourScheme;\r
54     }\r
55     else\r
56     {\r
57       colourScheme = originalColour;\r
58     }\r
59 \r
60     this.annotation = annotation;\r
61 \r
62     aboveAnnotationThreshold = aboveThreshold;\r
63 \r
64     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)\r
65     {\r
66       annotationThreshold = annotation.threshold;\r
67     }\r
68   }\r
69 \r
70   /**\r
71    * Creates a new AnnotationColourGradient object.\r
72    */\r
73   public AnnotationColourGradient(AlignmentAnnotation annotation,\r
74                                   Color minColour, Color maxColour,\r
75                                   int aboveThreshold)\r
76   {\r
77     this.annotation = annotation;\r
78 \r
79     aboveAnnotationThreshold = aboveThreshold;\r
80 \r
81     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)\r
82     {\r
83       annotationThreshold = annotation.threshold;\r
84     }\r
85 \r
86     r1 = minColour.getRed();\r
87     g1 = minColour.getGreen();\r
88     b1 = minColour.getBlue();\r
89 \r
90     rr = maxColour.getRed() - r1;\r
91     gg = maxColour.getGreen() - g1;\r
92     bb = maxColour.getBlue() - b1;\r
93   }\r
94 \r
95   public String getAnnotation()\r
96   {\r
97     return annotation.label;\r
98   }\r
99 \r
100   public int getAboveThreshold()\r
101   {\r
102     return aboveAnnotationThreshold;\r
103   }\r
104 \r
105   public float getAnnotationThreshold()\r
106   {\r
107     if (annotationThreshold == null)\r
108     {\r
109       return 0;\r
110     }\r
111     else\r
112     {\r
113       return annotationThreshold.value;\r
114     }\r
115   }\r
116 \r
117   public ColourSchemeI getBaseColour()\r
118   {\r
119     return colourScheme;\r
120   }\r
121 \r
122   public Color getMinColour()\r
123   {\r
124     return new Color( (int) r1, (int) g1, (int) b1);\r
125   }\r
126 \r
127   public Color getMaxColour()\r
128   {\r
129     return new Color( (int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));\r
130   }\r
131 \r
132   /**\r
133    * DOCUMENT ME!\r
134    *\r
135    * @param n DOCUMENT ME!\r
136    *\r
137    * @return DOCUMENT ME!\r
138    */\r
139   public Color findColour(char c)\r
140   {\r
141     return Color.red;\r
142   }\r
143 \r
144   /**\r
145    * DOCUMENT ME!\r
146    *\r
147    * @param n DOCUMENT ME!\r
148    * @param j DOCUMENT ME!\r
149    *\r
150    * @return DOCUMENT ME!\r
151    */\r
152   public Color findColour(char c, int j)\r
153   {\r
154     Color currentColour = Color.white;\r
155 \r
156     if ( (threshold == 0) || aboveThreshold(c, j))\r
157     {\r
158       if (j < annotation.annotations.length\r
159           && annotation.annotations[j] != null\r
160           && !jalview.util.Comparison.isGap(c))\r
161       {\r
162 \r
163        if (predefinedColours)\r
164         {\r
165           if(annotation.annotations[j].colour != null)\r
166               return annotation.annotations[j].colour;\r
167           else\r
168             return currentColour;\r
169         }\r
170 \r
171         if (aboveAnnotationThreshold == NO_THRESHOLD\r
172             ||\r
173             (annotationThreshold != null && aboveAnnotationThreshold == ABOVE_THRESHOLD &&\r
174              annotation.annotations[j].value >= annotationThreshold.value)\r
175             ||\r
176             (annotationThreshold != null && aboveAnnotationThreshold == BELOW_THRESHOLD &&\r
177              annotation.annotations[j].value <= annotationThreshold.value))\r
178         {\r
179 \r
180           float range=1f;\r
181           if (thresholdIsMinMax\r
182               && annotation.threshold != null\r
183               && aboveAnnotationThreshold == ABOVE_THRESHOLD\r
184               && annotation.annotations[j].value>annotation.threshold.value)\r
185           {\r
186             range =\r
187                 (annotation.annotations[j].value - annotation.threshold.value) /\r
188                 (annotation.graphMax - annotation.threshold.value);\r
189           }\r
190           else if (thresholdIsMinMax\r
191                    && annotation.threshold != null\r
192                    && aboveAnnotationThreshold == BELOW_THRESHOLD\r
193               &&  annotation.annotations[j].value > annotation.graphMin)\r
194           {\r
195             range =\r
196                 ( annotation.annotations[j].value - annotation.graphMin ) /\r
197                 (annotation.threshold.value - annotation.graphMin );\r
198           }\r
199           else\r
200           {\r
201             range = (annotation.annotations[j].value -\r
202                      annotation.graphMin) /\r
203                 (annotation.graphMax - annotation.graphMin);\r
204           }\r
205 \r
206 \r
207           if (colourScheme != null)\r
208           {\r
209             currentColour = colourScheme.findColour(c, j);\r
210           }\r
211           else if (range != 0)\r
212           {\r
213             dr = rr * range   + r1;\r
214             dg = gg * range   + g1;\r
215             db = bb * range   + b1;\r
216 \r
217             currentColour = new Color( (int) dr, (int) dg, (int) db);\r
218           }\r
219         }\r
220       }\r
221     }\r
222 \r
223     if (conservationColouring)\r
224     {\r
225       currentColour = applyConservation(currentColour, j);\r
226     }\r
227 \r
228     return currentColour;\r
229   }\r
230 }\r