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