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