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