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