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