Formatting
[jalview.git] / src / jalview / schemes / ResidueColourScheme.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.util.*;\r
22 \r
23 import java.awt.*;\r
24 \r
25 import jalview.analysis.*;\r
26 \r
27 /**\r
28  * DOCUMENT ME!\r
29  *\r
30  * @author $author$\r
31  * @version $Revision$\r
32  */\r
33 public class ResidueColourScheme\r
34     implements ColourSchemeI\r
35 {\r
36 \r
37   boolean conservationColouring = false;\r
38 \r
39   Color[] colors;\r
40   int threshold = 0;\r
41 \r
42   /* Set when threshold colouring to either pid_gaps or pid_nogaps*/\r
43   protected String ignoreGaps = AAFrequency.PID_GAPS;\r
44 \r
45   /** Consenus as a hashtable array */\r
46   Hashtable[] consensus;\r
47 \r
48   /** Conservation string as a char array */\r
49   char[] conservation;\r
50 \r
51   /** DOCUMENT ME!! */\r
52   int inc = 30;\r
53 \r
54   /**\r
55    * Creates a new ResidueColourScheme object.\r
56    *\r
57    * @param colors DOCUMENT ME!\r
58    * @param threshold DOCUMENT ME!\r
59    */\r
60   public ResidueColourScheme(Color[] colours, int threshold)\r
61   {\r
62     this.colors = colours;\r
63     this.threshold = threshold;\r
64   }\r
65 \r
66   /**\r
67    * Creates a new ResidueColourScheme object.\r
68    */\r
69   public ResidueColourScheme()\r
70   {\r
71   }\r
72 \r
73   /**\r
74    * Find a colour without an index in a sequence\r
75    */\r
76   public Color findColour(char c)\r
77   {\r
78     return colors[ResidueProperties.aaIndex[c]];\r
79   }\r
80 \r
81   public Color findColour(char c, int j)\r
82   {\r
83     Color currentColour;\r
84 \r
85     if ( (threshold == 0) || aboveThreshold(c, j))\r
86     {\r
87       currentColour = colors[ResidueProperties.aaIndex[c]];\r
88     }\r
89     else\r
90     {\r
91       currentColour = Color.white;\r
92     }\r
93 \r
94     if (conservationColouring)\r
95     {\r
96       currentColour = applyConservation(currentColour, j);\r
97     }\r
98 \r
99     return currentColour;\r
100   }\r
101 \r
102   /**\r
103    * Get the percentage threshold for this colour scheme\r
104    *\r
105    * @return Returns the percentage threshold\r
106    */\r
107   public int getThreshold()\r
108   {\r
109     return threshold;\r
110   }\r
111 \r
112   /**\r
113    * DOCUMENT ME!\r
114    *\r
115    * @param ct DOCUMENT ME!\r
116    */\r
117   public void setThreshold(int ct, boolean ignoreGaps)\r
118   {\r
119     threshold = ct;\r
120     if (ignoreGaps)\r
121     {\r
122       this.ignoreGaps = AAFrequency.PID_NOGAPS;\r
123     }\r
124     else\r
125     {\r
126       this.ignoreGaps = AAFrequency.PID_GAPS;\r
127     }\r
128   }\r
129 \r
130   /**\r
131    * DOCUMENT ME!\r
132    *\r
133    * @param s DOCUMENT ME!\r
134    * @param j DOCUMENT ME!\r
135    *\r
136    * @return DOCUMENT ME!\r
137    */\r
138   public boolean aboveThreshold(char c, int j)\r
139   {\r
140     if ('a' <= c && c <= 'z')\r
141     {\r
142       // TO UPPERCASE !!!\r
143       //Faster than toUpperCase\r
144       c -= ('a' - 'A');\r
145     }\r
146 \r
147     if (consensus == null || consensus[j] == null)\r
148     {\r
149       return false;\r
150     }\r
151 \r
152     if ( ( ( (Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1) &&\r
153         consensus[j].contains(String.valueOf(c)))\r
154     {\r
155       if ( ( (Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)\r
156       {\r
157         return true;\r
158       }\r
159     }\r
160 \r
161     return false;\r
162   }\r
163 \r
164   public boolean conservationApplied()\r
165   {\r
166     return conservationColouring;\r
167   }\r
168 \r
169   public void setConservationInc(int i)\r
170   {\r
171     inc = i;\r
172   }\r
173 \r
174   public int getConservationInc()\r
175   {\r
176     return inc;\r
177   }\r
178 \r
179   /**\r
180    * DOCUMENT ME!\r
181    *\r
182    * @param consensus DOCUMENT ME!\r
183    */\r
184   public void setConsensus(Hashtable[] consensus)\r
185   {\r
186     if (consensus == null)\r
187     {\r
188       return;\r
189     }\r
190 \r
191     this.consensus = consensus;\r
192   }\r
193 \r
194   public void setConservation(Conservation cons)\r
195   {\r
196     if (cons == null)\r
197     {\r
198       conservationColouring = false;\r
199       conservation = null;\r
200     }\r
201     else\r
202     {\r
203       conservationColouring = true;\r
204       int i, iSize = cons.getConsSequence().getLength();\r
205       conservation = new char[iSize];\r
206       for (i = 0; i < iSize; i++)\r
207       {\r
208         conservation[i] = cons.getConsSequence().getCharAt(i);\r
209       }\r
210     }\r
211 \r
212   }\r
213 \r
214   /**\r
215    * DOCUMENT ME!\r
216    *\r
217    * @param s DOCUMENT ME!\r
218    * @param i DOCUMENT ME!\r
219    *\r
220    * @return DOCUMENT ME!\r
221    */\r
222 \r
223   Color applyConservation(Color currentColour, int i)\r
224   {\r
225 \r
226     if ( (conservation[i] != '*') && (conservation[i] != '+'))\r
227     {\r
228       if (jalview.util.Comparison.isGap(conservation[i]))\r
229       {\r
230         currentColour = Color.white;\r
231       }\r
232       else\r
233       {\r
234         float t = 11 - (conservation[i] - '0');\r
235         if (t == 0)\r
236         {\r
237           return Color.white;\r
238         }\r
239 \r
240         int red = currentColour.getRed();\r
241         int green = currentColour.getGreen();\r
242         int blue = currentColour.getBlue();\r
243 \r
244         int dr = 255 - red;\r
245         int dg = 255 - green;\r
246         int db = 255 - blue;\r
247 \r
248         dr *= t / 10f;\r
249         dg *= t / 10f;\r
250         db *= t / 10f;\r
251 \r
252         red += (inc / 20f) * dr;\r
253         green += (inc / 20f) * dg;\r
254         blue += (inc / 20f) * db;\r
255 \r
256         if (red > 255 || green > 255 || blue > 255)\r
257         {\r
258           currentColour = Color.white;\r
259         }\r
260         else\r
261         {\r
262           currentColour = new Color(red, green, blue);\r
263         }\r
264       }\r
265     }\r
266     return currentColour;\r
267   }\r
268 \r
269 }\r