Sequence is char []
[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(char c)\r
79    {\r
80        return colors[ResidueProperties.aaIndex[c]];\r
81    }\r
82 \r
83 \r
84 \r
85    public Color findColour(char c, int j)\r
86    {\r
87        Color currentColour;\r
88 \r
89        if ((threshold == 0) || aboveThreshold(c, j))\r
90        {\r
91            currentColour = colors[ResidueProperties.aaIndex[c]];\r
92        }\r
93        else\r
94        {\r
95            currentColour = Color.white;\r
96        }\r
97 \r
98        if(conservationColouring)\r
99          currentColour = applyConservation(currentColour, j);\r
100 \r
101 \r
102        return currentColour;\r
103    }\r
104 \r
105 \r
106     /**\r
107      * Get the percentage threshold for this colour scheme\r
108      *\r
109      * @return Returns the percentage threshold\r
110      */\r
111     public int getThreshold()\r
112     {\r
113         return threshold;\r
114     }\r
115 \r
116     /**\r
117      * DOCUMENT ME!\r
118      *\r
119      * @param ct DOCUMENT ME!\r
120      */\r
121     public void setThreshold(int ct, boolean ignoreGaps)\r
122     {\r
123         threshold = ct;\r
124         if(ignoreGaps)\r
125           this.ignoreGaps = AAFrequency.PID_NOGAPS;\r
126         else\r
127           this.ignoreGaps = AAFrequency.PID_GAPS;\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           return false;\r
149 \r
150         if ( ( ( (Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1) &&\r
151             consensus[j].contains(String.valueOf(c)))\r
152         {\r
153           if ( ( (Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)\r
154           {\r
155             return true;\r
156           }\r
157         }\r
158 \r
159         return false;\r
160     }\r
161 \r
162 \r
163     public boolean conservationApplied()\r
164     {\r
165       return conservationColouring;\r
166     }\r
167 \r
168     public void setConservationInc(int i)\r
169     {\r
170       inc = i;\r
171     }\r
172 \r
173     public int getConservationInc()\r
174     {\r
175       return inc;\r
176     }\r
177 \r
178     /**\r
179      * DOCUMENT ME!\r
180      *\r
181      * @param consensus DOCUMENT ME!\r
182      */\r
183     public void setConsensus(Hashtable [] consensus)\r
184     {\r
185       if(consensus==null)\r
186         return;\r
187 \r
188       this.consensus = consensus;\r
189     }\r
190 \r
191 \r
192 \r
193     public void setConservation(Conservation cons)\r
194     {\r
195       if(cons==null)\r
196       {\r
197         conservationColouring = false;\r
198         conservation = null;\r
199       }\r
200       else\r
201       {\r
202         conservationColouring = true;\r
203         int i, iSize = cons.getConsSequence().getLength();\r
204         conservation = new char[iSize];\r
205         for (i = 0; i < iSize; i++)\r
206           conservation[i] = cons.getConsSequence().getCharAt(i);\r
207       }\r
208 \r
209     }\r
210 \r
211 \r
212     /**\r
213     * DOCUMENT ME!\r
214     *\r
215     * @param s DOCUMENT ME!\r
216     * @param i DOCUMENT ME!\r
217     *\r
218     * @return DOCUMENT ME!\r
219     */\r
220 \r
221    Color applyConservation(Color currentColour, int i)\r
222    {\r
223 \r
224      if ((conservation[i] != '*') && (conservation[i] != '+'))\r
225      {\r
226        if(jalview.util.Comparison.isGap(conservation[i]))\r
227        {\r
228          currentColour = Color.white;\r
229        }\r
230        else\r
231        {\r
232          float t = 11 - (conservation[i] - '0');\r
233          if(t==0)\r
234          {\r
235            return Color.white;\r
236          }\r
237 \r
238          int red = currentColour.getRed();\r
239          int green = currentColour.getGreen();\r
240          int blue = currentColour.getBlue();\r
241 \r
242          int dr = 255 - red;\r
243          int dg = 255 - green;\r
244          int db = 255 - blue;\r
245 \r
246          dr *= t / 10f;\r
247          dg *= t / 10f;\r
248          db *= t / 10f;\r
249 \r
250          red += (inc / 20f) * dr;\r
251          green += (inc / 20f) * dg;\r
252          blue += (inc / 20f) * db;\r
253 \r
254          if (red > 255 || green > 255 || blue > 255)\r
255            currentColour = Color.white;\r
256          else\r
257            currentColour = new Color(red, green, blue);\r
258        }\r
259        }\r
260        return currentColour;\r
261    }\r
262 \r
263 \r
264 }\r