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