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;
27 import jalview.util.StringUtils;
29 import java.awt.Color;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.StringTokenizer;
38 public class UserColourScheme extends ResidueColourScheme
41 * lookup (by symbol index) of lower case colours (if configured)
43 Color[] lowerCaseColours;
45 protected String schemeName;
47 public UserColourScheme()
49 super(ResidueProperties.aaIndex);
52 public UserColourScheme(Color[] newColors)
54 super(ResidueProperties.aaIndex);
59 public ColourSchemeI getInstance(AnnotatedCollectionI sg,
60 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
62 return new UserColourScheme(this);
70 protected UserColourScheme(UserColourScheme from)
73 schemeName = from.schemeName;
74 if (from.lowerCaseColours != null)
76 lowerCaseColours = new Color[from.lowerCaseColours.length];
77 System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0,
78 from.lowerCaseColours.length);
83 * Constructor for an animino acid colour scheme. The colour specification may
86 * <li>an AWT colour name e.g. red</li>
87 * <li>an AWT hex rgb colour e.g. ff2288</li>
88 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
93 public UserColourScheme(String colour)
95 super(ResidueProperties.aaIndex);
97 if (colour.contains("="))
100 * a list of colours per residue(s)
102 parseAppletParameter(colour);
106 Color col = ColorUtils.parseColourString(colour);
110 System.out.println("Making colour from name: " + colour);
111 col = ColorUtils.createColourFromName(colour);
119 * Sets all symbols to the specified colour
123 protected void setAll(Color col)
125 if (symbolIndex == null)
130 for (int index : symbolIndex)
132 max = Math.max(max, index);
134 colors = new Color[max + 1];
135 for (int i = 0; i <= max; i++)
141 public Color[] getColours()
146 public Color[] getLowerCaseColours()
148 return lowerCaseColours;
151 public void setName(String name)
156 public String getName()
162 * Parse and save residue colours specified as (for example)
165 * D,E=red; K,R,H=0022FF; c=100,50,75
168 * This should be a semi-colon separated list of colours, which may be defined
169 * by colour name, hex value or comma-separated RGB triple. Each colour is
170 * defined for a comma-separated list of amino acid single letter codes. (Note
171 * that this also allows a colour scheme to be defined for ACGT, but not for
176 void parseAppletParameter(String paramValue)
180 StringTokenizer st = new StringTokenizer(paramValue, ";");
182 String token = null, colour, residues;
185 while (st.hasMoreElements())
187 token = st.nextToken().trim();
188 residues = token.substring(0, token.indexOf("="));
189 colour = token.substring(token.indexOf("=") + 1);
191 st2 = new StringTokenizer(residues, " ,");
192 while (st2.hasMoreTokens())
194 String residue = st2.nextToken();
196 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
202 if (residue.equalsIgnoreCase("lowerCase"))
204 if (lowerCaseColours == null)
206 lowerCaseColours = new Color[colors.length];
208 for (int i = 0; i < lowerCaseColours.length; i++)
210 if (lowerCaseColours[i] == null)
212 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
219 if (residue.equals(residue.toLowerCase()))
221 if (lowerCaseColours == null)
223 lowerCaseColours = new Color[colors.length];
225 lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour);
229 colors[colIndex] = ColorUtils.parseColourString(colour);
233 } catch (Exception ex)
235 System.out.println("Error parsing userDefinedColours:\n" + token
241 public void setLowerCaseColours(Color[] lcolours)
243 lowerCaseColours = lcolours;
247 * Returns the colour for the given residue character. If the residue is
248 * lower-case, and there is a specific colour defined for lower case, that
249 * colour is returned, else the colour for the upper case residue.
252 public Color findColour(char c)
254 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
256 Color colour = lowerCaseColours[symbolIndex[c]];
262 return super.findColour(c);
266 * Answers the customised name of the colour scheme, if it has one, else
270 public String getSchemeName()
272 if (schemeName != null && schemeName.length() > 0)
276 return "User Defined";
280 * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
284 public String toAppletParameter()
287 * step 1: build a map from colours to the symbol(s) that have the colour
289 Map<Color, List<String>> colours = new HashMap<Color, List<String>>();
291 for (char symbol = 'A'; symbol <= 'Z'; symbol++)
293 String residue = String.valueOf(symbol);
294 int index = symbolIndex[symbol];
295 Color c = colors[index];
296 if (c != null && !c.equals(Color.white))
298 if (colours.get(c) == null)
300 colours.put(c, new ArrayList<String>());
302 colours.get(c).add(residue);
304 if (lowerCaseColours != null)
306 c = lowerCaseColours[index];
307 if (c != null && !c.equals(Color.white))
309 residue = residue.toLowerCase();
310 if (colours.get(c) == null)
312 colours.put(c, new ArrayList<String>());
314 colours.get(c).add(residue);
320 * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
322 List<String> residueColours = new ArrayList<String>();
323 for (Entry<Color, List<String>> cols : colours.entrySet())
325 boolean first = true;
326 StringBuilder sb = new StringBuilder();
327 for (String residue : cols.getValue())
338 * get color as hex value, dropping the alpha (ff) part
340 String hexString = Integer.toHexString(cols.getKey().getRGB())
342 sb.append(hexString);
343 residueColours.add(sb.toString());
349 Collections.sort(residueColours);
350 return StringUtils.listToDelimitedString(residueColours, ";");