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