8367cadc36020b23d62e1f1e29dc38423d1641b6
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 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 extends ResidueColourScheme\r
26 {\r
27     public static int NO_THRESHOLD = -1;\r
28     public static int BELOW_THRESHOLD = 0;\r
29     public static int ABOVE_THRESHOLD = 1;\r
30 \r
31     AlignmentAnnotation annotation;\r
32     int aboveAnnotationThreshold = -1;\r
33 \r
34     GraphLine annotationThreshold;\r
35 \r
36     float r1, g1, b1, rr, gg, bb, dr, dg, db;\r
37     float range = 0;\r
38 \r
39     ColourSchemeI colourScheme;\r
40 \r
41     /**\r
42      * Creates a new AnnotationColourGradient object.\r
43      */\r
44     public AnnotationColourGradient(AlignmentAnnotation annotation,\r
45                                     ColourSchemeI originalColour,\r
46                                     int aboveThreshold)\r
47     {\r
48       if(originalColour instanceof AnnotationColourGradient)\r
49       {\r
50         colourScheme = ((AnnotationColourGradient)originalColour).colourScheme;\r
51       }\r
52       else\r
53         colourScheme = originalColour;\r
54 \r
55       this.annotation = annotation;\r
56 \r
57       aboveAnnotationThreshold = aboveThreshold;\r
58 \r
59       if(aboveThreshold!=NO_THRESHOLD && annotation.threshold!=null)\r
60         annotationThreshold = annotation.threshold;\r
61     }\r
62 \r
63     /**\r
64      * Creates a new AnnotationColourGradient object.\r
65      */\r
66     public AnnotationColourGradient(AlignmentAnnotation annotation,\r
67         Color minColour, Color maxColour, int aboveThreshold)\r
68     {\r
69       this.annotation = annotation;\r
70 \r
71       aboveAnnotationThreshold = aboveThreshold;\r
72 \r
73       if(aboveThreshold!=NO_THRESHOLD && annotation.threshold!=null)\r
74         annotationThreshold = annotation.threshold;\r
75 \r
76       r1 = minColour.getRed();\r
77       g1 = minColour.getGreen();\r
78       b1 = minColour.getBlue();\r
79 \r
80       rr = maxColour.getRed() - r1;\r
81       gg = maxColour.getGreen() - g1;\r
82       bb = maxColour.getBlue() - b1;\r
83 \r
84       range = annotation.graphMax - annotation.graphMin;\r
85 \r
86     }\r
87 \r
88 \r
89     public String getAnnotation()\r
90     {\r
91       return annotation.label;\r
92     }\r
93 \r
94     public int getAboveThreshold()\r
95     {\r
96       return aboveAnnotationThreshold;\r
97     }\r
98 \r
99     public float getAnnotationThreshold()\r
100     {\r
101       if(annotationThreshold==null)\r
102         return 0;\r
103       else\r
104         return annotationThreshold.value;\r
105     }\r
106 \r
107     public ColourSchemeI getBaseColour()\r
108     {\r
109       return colourScheme;\r
110     }\r
111 \r
112     public Color getMinColour()\r
113     {\r
114       return new Color( (int) r1, (int) g1, (int) b1);\r
115     }\r
116 \r
117     public Color getMaxColour()\r
118     {\r
119       return new Color( (int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));\r
120     }\r
121 \r
122     /**\r
123      * DOCUMENT ME!\r
124      *\r
125      * @param n DOCUMENT ME!\r
126      *\r
127      * @return DOCUMENT ME!\r
128      */\r
129     public Color findColour(String n)\r
130     {\r
131         return Color.red;\r
132     }\r
133 \r
134     /**\r
135      * DOCUMENT ME!\r
136      *\r
137      * @param n DOCUMENT ME!\r
138      * @param j DOCUMENT ME!\r
139      *\r
140      * @return DOCUMENT ME!\r
141      */\r
142     public Color findColour(String n, int j)\r
143     {\r
144         Color currentColour = Color.white;\r
145 \r
146         if ((threshold == 0) || aboveThreshold(n, j))\r
147         {\r
148           if( j<annotation.annotations.length\r
149               && annotation.annotations[j]!=null\r
150               && !jalview.util.Comparison.isGap(n.charAt(0)))\r
151           {\r
152             if(  aboveAnnotationThreshold==NO_THRESHOLD\r
153                || (annotationThreshold!=null && aboveAnnotationThreshold==ABOVE_THRESHOLD && annotation.annotations[j].value>=annotationThreshold.value)\r
154                || (annotationThreshold!=null && aboveAnnotationThreshold==BELOW_THRESHOLD && annotation.annotations[j].value<=annotationThreshold.value))\r
155             {\r
156               if(colourScheme!=null)\r
157               {\r
158                 currentColour = colourScheme.findColour(n, j);\r
159               }\r
160               else if(range!=0)\r
161               {\r
162                 dr = rr *\r
163                     ( (annotation.annotations[j].value - annotation.graphMin) /\r
164                      range)\r
165                     + r1;\r
166                 dg = gg *\r
167                     ( (annotation.annotations[j].value - annotation.graphMin) /\r
168                      range)\r
169                     + g1;\r
170                 db = bb *\r
171                     ( (annotation.annotations[j].value - annotation.graphMin) /\r
172                      range)\r
173                     + b1;\r
174 \r
175                 currentColour = new Color( (int) dr, (int) dg, (int) db);\r
176               }\r
177             }\r
178           }\r
179         }\r
180 \r
181         if(conservationColouring)\r
182          applyConservation(currentColour, j);\r
183 \r
184        return currentColour;\r
185     }\r
186 }\r