JAL-1066 - T-Coffee color scheme + signature change on findColour method
[jalview.git] / src / jalview / schemes / UserColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import java.awt.Color;
21 import java.util.StringTokenizer;
22
23 public class UserColourScheme extends ResidueColourScheme
24 {
25   Color[] lowerCaseColours;
26
27   protected String schemeName;
28
29   public UserColourScheme()
30   {
31   }
32
33   public UserColourScheme(Color[] newColors)
34   {
35     colors = newColors;
36   }
37
38   public UserColourScheme(String colour)
39   {
40     Color col = getColourFromString(colour);
41
42     if (col == null)
43     {
44       System.out.println("Unknown colour!! " + colour);
45       col = createColourFromName(colour);
46     }
47
48     colors = new Color[24];
49     for (int i = 0; i < 24; i++)
50     {
51       colors[i] = col;
52     }
53     schemeName = colour;
54   }
55
56   public Color[] getColours()
57   {
58     return colors;
59   }
60
61   public Color[] getLowerCaseColours()
62   {
63     return lowerCaseColours;
64   }
65
66   public void setName(String name)
67   {
68     schemeName = name;
69   }
70
71   public String getName()
72   {
73     return schemeName;
74   }
75
76   public Color getColourFromString(String colour)
77   {
78     colour = colour.trim();
79
80     Color col = null;
81     try
82     {
83       int value = Integer.parseInt(colour, 16);
84       col = new Color(value);
85     } catch (NumberFormatException ex)
86     {
87     }
88
89     if (col == null)
90     {
91       col = ColourSchemeProperty.getAWTColorFromName(colour);
92     }
93
94     if (col == null)
95     {
96       try
97       {
98         java.util.StringTokenizer st = new java.util.StringTokenizer(
99                 colour, ",");
100         int r = Integer.parseInt(st.nextToken());
101         int g = Integer.parseInt(st.nextToken());
102         int b = Integer.parseInt(st.nextToken());
103         col = new Color(r, g, b);
104       } catch (Exception ex)
105       {
106       }
107     }
108
109     return col;
110
111   }
112
113   public Color createColourFromName(String name)
114   {
115     int r, g, b;
116
117     int lsize = name.length();
118     int start = 0, end = lsize / 3;
119
120     int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
121
122     r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
123     start = end;
124     end += lsize / 3;
125     if (end > lsize)
126     {
127       end = lsize;
128     }
129
130     g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
131
132     b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
133
134     Color color = new Color(r, g, b);
135
136     return color;
137   }
138
139   public void parseAppletParameter(String paramValue)
140   {
141     StringTokenizer st = new StringTokenizer(paramValue, ";");
142     StringTokenizer st2;
143     String token = null, colour, residues;
144     try
145     {
146       while (st.hasMoreElements())
147       {
148         token = st.nextToken().trim();
149         residues = token.substring(0, token.indexOf("="));
150         colour = token.substring(token.indexOf("=") + 1);
151
152         st2 = new StringTokenizer(residues, " ,");
153         while (st2.hasMoreTokens())
154         {
155           token = st2.nextToken();
156
157           if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
158           {
159             continue;
160           }
161
162           int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
163
164           if (token.equalsIgnoreCase("lowerCase"))
165           {
166             if (lowerCaseColours == null)
167             {
168               lowerCaseColours = new Color[23];
169             }
170             for (int i = 0; i < 23; i++)
171             {
172               if (lowerCaseColours[i] == null)
173               {
174                 lowerCaseColours[i] = getColourFromString(colour);
175               }
176             }
177
178             continue;
179           }
180
181           if (token.equals(token.toLowerCase()))
182           {
183             if (lowerCaseColours == null)
184             {
185               lowerCaseColours = new Color[23];
186             }
187             lowerCaseColours[colIndex] = getColourFromString(colour);
188           }
189           else
190           {
191             colors[colIndex] = getColourFromString(colour);
192           }
193         }
194       }
195     } catch (Exception ex)
196     {
197       System.out.println("Error parsing userDefinedColours:\n" + token
198               + "\n" + ex);
199     }
200
201   }
202
203   @Override
204   public Color findColour(char c, int j, int sequenceIndex)
205   {
206     Color currentColour;
207     int index = ResidueProperties.aaIndex[c];
208
209     if ((threshold == 0) || aboveThreshold(c, j))
210     {
211       if (lowerCaseColours != null && 'a' <= c && c <= 'z')
212       {
213         currentColour = lowerCaseColours[index];
214       }
215       else
216       {
217         currentColour = colors[index];
218       }
219     }
220     else
221     {
222       currentColour = Color.white;
223     }
224
225     if (conservationColouring)
226     {
227       currentColour = applyConservation(currentColour, j);
228     }
229
230     return currentColour;
231   }
232
233   public void setLowerCaseColours(Color[] lcolours)
234   {
235     lowerCaseColours = lcolours;
236   }
237
238 }