2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.schemes;
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.ColorUtils;
28 import java.awt.Color;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.StringTokenizer;
36 public class UserColourScheme extends ResidueColourScheme
39 * lookup (by symbol index) of lower case colours (if configured)
41 Color[] lowerCaseColours;
43 protected String schemeName;
45 public UserColourScheme()
47 super(ResidueProperties.aaIndex);
50 public UserColourScheme(Color[] newColors)
52 super(ResidueProperties.aaIndex);
57 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
58 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
60 UserColourScheme usc = new UserColourScheme(colors);
61 if (lowerCaseColours != null)
63 usc.schemeName = schemeName;
64 usc.lowerCaseColours = new Color[lowerCaseColours.length];
65 System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0,
66 lowerCaseColours.length);
72 * Constructor for an animino acid colour scheme. The colour specification may
75 * <li>an AWT colour name e.g. red</li>
76 * <li>an AWT hex rgb colour e.g. ff2288</li>
77 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
82 public UserColourScheme(String colour)
84 super(ResidueProperties.aaIndex);
86 if (colour.contains("="))
89 * a list of colours per residue(s)
91 parseAppletParameter(colour);
95 Color col = ColorUtils.parseColourString(colour);
99 System.out.println("Making colour from name: " + colour);
100 col = ColorUtils.createColourFromName(colour);
108 * Sets all symbols to the specified colour
112 protected void setAll(Color col)
114 if (symbolIndex == null)
119 for (int index : symbolIndex)
121 max = Math.max(max, index);
123 colors = new Color[max + 1];
124 for (int i = 0; i <= max; i++)
130 public Color[] getColours()
135 public Color[] getLowerCaseColours()
137 return lowerCaseColours;
140 public void setName(String name)
145 public String getName()
151 * Parse and save residue colours specified as (for example)
154 * D,E=red; K,R,H=0022FF; c=100,50,75
157 * This should be a semi-colon separated list of colours, which may be defined
158 * by colour name, hex value or comma-separated RGB triple. Each colour is
159 * defined for a comma-separated list of amino acid single letter codes. (Note
160 * that this also allows a colour scheme to be defined for ACGT, but not for
165 void parseAppletParameter(String paramValue)
169 StringTokenizer st = new StringTokenizer(paramValue, ";");
171 String token = null, colour, residues;
174 while (st.hasMoreElements())
176 token = st.nextToken().trim();
177 residues = token.substring(0, token.indexOf("="));
178 colour = token.substring(token.indexOf("=") + 1);
180 st2 = new StringTokenizer(residues, " ,");
181 while (st2.hasMoreTokens())
183 String residue = st2.nextToken();
185 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
191 if (residue.equalsIgnoreCase("lowerCase"))
193 if (lowerCaseColours == null)
195 lowerCaseColours = new Color[colors.length];
197 for (int i = 0; i < lowerCaseColours.length; i++)
199 if (lowerCaseColours[i] == null)
201 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
208 if (residue.equals(residue.toLowerCase()))
210 if (lowerCaseColours == null)
212 lowerCaseColours = new Color[colors.length];
214 lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour);
218 colors[colIndex] = ColorUtils.parseColourString(colour);
222 } catch (Exception ex)
224 System.out.println("Error parsing userDefinedColours:\n" + token
231 public Color findColour(char c, int j, SequenceI seq)
234 int index = ResidueProperties.aaIndex[c];
236 if ((threshold == 0) || aboveThreshold(c, j))
238 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
240 currentColour = lowerCaseColours[index];
244 currentColour = colors[index];
249 currentColour = Color.white;
252 if (conservationColouring)
254 currentColour = applyConservation(currentColour, j);
257 return currentColour;
260 public void setLowerCaseColours(Color[] lcolours)
262 lowerCaseColours = lcolours;
266 * Returns the colour for the given residue character. If the residue is
267 * lower-case, and there is a specific colour defined for lower case, that
268 * colour is returned, else the colour for the upper case residue.
271 public Color findColour(char c)
273 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
275 Color colour = lowerCaseColours[symbolIndex[c]];
281 return super.findColour(c);
285 * Answers the customised name of the colour scheme, if it has one, else
289 public String getSchemeName()
291 if (schemeName != null && schemeName.length() > 0)
295 return JalviewColourScheme.UserDefined.toString();
299 * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
303 public String toAppletParameter()
305 Map<Color, List<String>> colours = new HashMap<Color, List<String>>();
307 for (char symbol = 'A'; symbol <= 'Z'; symbol++)
309 String residue = String.valueOf(symbol);
310 int index = symbolIndex[symbol];
311 Color c = colors[index];
312 if (c != null && !c.equals(Color.white))
314 if (colours.get(c) == null)
316 colours.put(c, new ArrayList<String>());
318 colours.get(c).add(residue);
320 if (lowerCaseColours != null)
322 c = lowerCaseColours[index];
323 if (c != null && !c.equals(Color.white))
325 residue = residue.toLowerCase();
326 if (colours.get(c) == null)
328 colours.put(c, new ArrayList<String>());
330 colours.get(c).add(residue);
334 StringBuilder sb = new StringBuilder();
335 for (Entry<Color, List<String>> cols : colours.entrySet())
341 boolean first = true;
342 for (String residue : cols.getValue())
353 * get color as hex value, dropping the alpha (ff) part
355 String hexString = Integer.toHexString(cols.getKey().getRGB())
357 sb.append(hexString);
360 return sb.toString();