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.api.AlignViewportI;
24 import jalview.datamodel.AnnotatedCollectionI;
25 import jalview.util.ColorUtils;
26 import jalview.util.StringUtils;
28 import java.awt.Color;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.StringTokenizer;
37 public class UserColourScheme extends ResidueColourScheme
40 * lookup (by symbol index) of lower case colours (if configured)
42 Color[] lowerCaseColours;
44 protected String schemeName;
46 public UserColourScheme()
48 super(ResidueProperties.aaIndex);
51 public UserColourScheme(Color[] newColors)
53 super(ResidueProperties.aaIndex);
58 public ColourSchemeI getInstance(AlignViewportI view,
59 AnnotatedCollectionI sg)
61 return new UserColourScheme(this);
69 protected UserColourScheme(UserColourScheme from)
72 schemeName = from.schemeName;
73 if (from.lowerCaseColours != null)
75 lowerCaseColours = new Color[from.lowerCaseColours.length];
76 System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0,
77 from.lowerCaseColours.length);
82 * Constructor for an animino acid colour scheme. The colour specification may
85 * <li>an AWT colour name e.g. red</li>
86 * <li>an AWT hex rgb colour e.g. ff2288</li>
87 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
92 public UserColourScheme(String colour)
94 super(ResidueProperties.aaIndex);
96 if (colour.contains("="))
99 * a list of colours per residue(s)
101 parseAppletParameter(colour);
105 Color col = ColorUtils.parseColourString(colour);
109 System.out.println("Making colour from name: " + colour);
110 col = ColorUtils.createColourFromName(colour);
118 * Sets all symbols to the specified colour
122 protected void setAll(Color col)
124 if (symbolIndex == null)
129 for (int index : symbolIndex)
131 max = Math.max(max, index);
133 colors = new Color[max + 1];
134 for (int i = 0; i <= max; i++)
140 public Color[] getColours()
145 public Color[] getLowerCaseColours()
147 return lowerCaseColours;
150 public void setName(String name)
155 public String getName()
161 * Parse and save residue colours specified as (for example)
164 * D,E=red; K,R,H=0022FF; c=100,50,75
167 * This should be a semi-colon separated list of colours, which may be defined
168 * by colour name, hex value or comma-separated RGB triple. Each colour is
169 * defined for a comma-separated list of amino acid single letter codes. (Note
170 * that this also allows a colour scheme to be defined for ACGT, but not for
175 void parseAppletParameter(String paramValue)
179 StringTokenizer st = new StringTokenizer(paramValue, ";");
181 String token = null, colour, residues;
184 while (st.hasMoreElements())
186 token = st.nextToken().trim();
187 residues = token.substring(0, token.indexOf("="));
188 colour = token.substring(token.indexOf("=") + 1);
190 st2 = new StringTokenizer(residues, " ,");
191 while (st2.hasMoreTokens())
193 String residue = st2.nextToken();
195 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
201 if (residue.equalsIgnoreCase("lowerCase"))
203 if (lowerCaseColours == null)
205 lowerCaseColours = new Color[colors.length];
207 for (int i = 0; i < lowerCaseColours.length; i++)
209 if (lowerCaseColours[i] == null)
211 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
218 if (residue.equals(residue.toLowerCase()))
220 if (lowerCaseColours == null)
222 lowerCaseColours = new Color[colors.length];
224 lowerCaseColours[colIndex] = ColorUtils
225 .parseColourString(colour);
229 colors[colIndex] = ColorUtils.parseColourString(colour);
233 } catch (Exception ex)
236 "Error parsing userDefinedColours:\n" + token + "\n" + ex);
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 "User
270 public String getSchemeName()
272 if (schemeName != null && schemeName.length() > 0)
276 return ResidueColourScheme.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<>();
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<>();
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, ";");
354 public boolean hasGapColour()
356 return (findColour(' ') != null);