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 java.util.Locale;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.util.ColorUtils;
28 import jalview.util.StringUtils;
30 import java.awt.Color;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.StringTokenizer;
39 public class UserColourScheme extends ResidueColourScheme
42 * lookup (by symbol index) of lower case colours (if configured)
44 Color[] lowerCaseColours;
46 protected String schemeName;
48 public UserColourScheme()
50 super(ResidueProperties.aaIndex);
53 public UserColourScheme(Color[] newColors)
55 super(ResidueProperties.aaIndex);
60 public ColourSchemeI getInstance(AlignViewportI view,
61 AnnotatedCollectionI sg)
63 return new UserColourScheme(this);
71 protected UserColourScheme(UserColourScheme from)
74 schemeName = from.schemeName;
75 if (from.lowerCaseColours != null)
77 lowerCaseColours = new Color[from.lowerCaseColours.length];
78 System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0,
79 from.lowerCaseColours.length);
84 * Constructor for an animino acid colour scheme. The colour specification may
87 * <li>an AWT colour name e.g. red</li>
88 * <li>an AWT hex rgb colour e.g. ff2288</li>
89 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
94 public UserColourScheme(String colour)
96 super(ResidueProperties.aaIndex);
98 if (colour.contains("="))
101 * a list of colours per residue(s)
103 parseAppletParameter(colour);
107 Color col = ColorUtils.parseColourString(colour);
111 System.out.println("Making colour from name: " + colour);
112 col = ColorUtils.createColourFromName(colour);
120 * Sets all symbols to the specified colour
124 protected void setAll(Color col)
126 if (symbolIndex == null)
131 for (int index : symbolIndex)
133 max = Math.max(max, index);
135 colors = new Color[max + 1];
136 for (int i = 0; i <= max; i++)
142 public Color[] getColours()
147 public Color[] getLowerCaseColours()
149 return lowerCaseColours;
152 public void setName(String name)
157 public String getName()
163 * Parse and save residue colours specified as (for example)
166 * D,E=red; K,R,H=0022FF; c=100,50,75
169 * This should be a semi-colon separated list of colours, which may be defined
170 * by colour name, hex value or comma-separated RGB triple. Each colour is
171 * defined for a comma-separated list of amino acid single letter codes. (Note
172 * that this also allows a colour scheme to be defined for ACGT, but not for
177 void parseAppletParameter(String paramValue)
181 StringTokenizer st = new StringTokenizer(paramValue, ";");
183 String token = null, colour, residues;
186 while (st.hasMoreElements())
188 token = st.nextToken().trim();
189 residues = token.substring(0, token.indexOf("="));
190 colour = token.substring(token.indexOf("=") + 1);
192 st2 = new StringTokenizer(residues, " ,");
193 while (st2.hasMoreTokens())
195 String residue = st2.nextToken();
197 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
203 if (residue.equalsIgnoreCase("lowerCase"))
205 if (lowerCaseColours == null)
207 lowerCaseColours = new Color[colors.length];
209 for (int i = 0; i < lowerCaseColours.length; i++)
211 if (lowerCaseColours[i] == null)
213 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
220 if (residue.equals(residue.toLowerCase(Locale.ROOT)))
222 if (lowerCaseColours == null)
224 lowerCaseColours = new Color[colors.length];
226 lowerCaseColours[colIndex] = ColorUtils
227 .parseColourString(colour);
231 colors[colIndex] = ColorUtils.parseColourString(colour);
235 } catch (Exception ex)
238 "Error parsing userDefinedColours:\n" + token + "\n" + ex);
243 public void setLowerCaseColours(Color[] lcolours)
245 lowerCaseColours = lcolours;
249 * Returns the colour for the given residue character. If the residue is
250 * lower-case, and there is a specific colour defined for lower case, that
251 * colour is returned, else the colour for the upper case residue.
254 public Color findColour(char c)
256 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
258 Color colour = lowerCaseColours[symbolIndex[c]];
264 return super.findColour(c);
268 * Answers the customised name of the colour scheme, if it has one, else "User
272 public String getSchemeName()
274 if (schemeName != null && schemeName.length() > 0)
278 return ResidueColourScheme.USER_DEFINED;
282 * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
286 public String toAppletParameter()
289 * step 1: build a map from colours to the symbol(s) that have the colour
291 Map<Color, List<String>> colours = new HashMap<>();
293 for (char symbol = 'A'; symbol <= 'Z'; symbol++)
295 String residue = String.valueOf(symbol);
296 int index = symbolIndex[symbol];
297 Color c = colors[index];
298 if (c != null && !c.equals(Color.white))
300 if (colours.get(c) == null)
302 colours.put(c, new ArrayList<String>());
304 colours.get(c).add(residue);
306 if (lowerCaseColours != null)
308 c = lowerCaseColours[index];
309 if (c != null && !c.equals(Color.white))
311 residue = residue.toLowerCase(Locale.ROOT);
312 if (colours.get(c) == null)
314 colours.put(c, new ArrayList<String>());
316 colours.get(c).add(residue);
322 * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
324 List<String> residueColours = new ArrayList<>();
325 for (Entry<Color, List<String>> cols : colours.entrySet())
327 boolean first = true;
328 StringBuilder sb = new StringBuilder();
329 for (String residue : cols.getValue())
340 * get color as hex value, dropping the alpha (ff) part
342 String hexString = Integer.toHexString(cols.getKey().getRGB())
344 sb.append(hexString);
345 residueColours.add(sb.toString());
351 Collections.sort(residueColours);
352 return StringUtils.listToDelimitedString(residueColours, ";");
356 public boolean hasGapColour()
358 return (findColour(' ') != null);