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 applyTo(AnnotatedCollectionI sg,
60 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
62 UserColourScheme usc = new UserColourScheme(colors);
63 if (lowerCaseColours != null)
65 usc.schemeName = schemeName;
66 usc.lowerCaseColours = new Color[lowerCaseColours.length];
67 System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0,
68 lowerCaseColours.length);
74 * Constructor for an animino acid colour scheme. The colour specification may
77 * <li>an AWT colour name e.g. red</li>
78 * <li>an AWT hex rgb colour e.g. ff2288</li>
79 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
84 public UserColourScheme(String colour)
86 super(ResidueProperties.aaIndex);
88 if (colour.contains("="))
91 * a list of colours per residue(s)
93 parseAppletParameter(colour);
97 Color col = ColorUtils.parseColourString(colour);
101 System.out.println("Making colour from name: " + colour);
102 col = ColorUtils.createColourFromName(colour);
110 * Sets all symbols to the specified colour
114 protected void setAll(Color col)
116 if (symbolIndex == null)
121 for (int index : symbolIndex)
123 max = Math.max(max, index);
125 colors = new Color[max + 1];
126 for (int i = 0; i <= max; i++)
132 public Color[] getColours()
137 public Color[] getLowerCaseColours()
139 return lowerCaseColours;
142 public void setName(String name)
147 public String getName()
153 * Parse and save residue colours specified as (for example)
156 * D,E=red; K,R,H=0022FF; c=100,50,75
159 * This should be a semi-colon separated list of colours, which may be defined
160 * by colour name, hex value or comma-separated RGB triple. Each colour is
161 * defined for a comma-separated list of amino acid single letter codes. (Note
162 * that this also allows a colour scheme to be defined for ACGT, but not for
167 void parseAppletParameter(String paramValue)
171 StringTokenizer st = new StringTokenizer(paramValue, ";");
173 String token = null, colour, residues;
176 while (st.hasMoreElements())
178 token = st.nextToken().trim();
179 residues = token.substring(0, token.indexOf("="));
180 colour = token.substring(token.indexOf("=") + 1);
182 st2 = new StringTokenizer(residues, " ,");
183 while (st2.hasMoreTokens())
185 String residue = st2.nextToken();
187 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
193 if (residue.equalsIgnoreCase("lowerCase"))
195 if (lowerCaseColours == null)
197 lowerCaseColours = new Color[colors.length];
199 for (int i = 0; i < lowerCaseColours.length; i++)
201 if (lowerCaseColours[i] == null)
203 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
210 if (residue.equals(residue.toLowerCase()))
212 if (lowerCaseColours == null)
214 lowerCaseColours = new Color[colors.length];
216 lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour);
220 colors[colIndex] = ColorUtils.parseColourString(colour);
224 } catch (Exception ex)
226 System.out.println("Error parsing userDefinedColours:\n" + token
233 public Color findColour(char c, int j, SequenceI seq)
236 int index = ResidueProperties.aaIndex[c];
238 if ((threshold == 0) || aboveThreshold(c, j))
240 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
242 currentColour = lowerCaseColours[index];
246 currentColour = colors[index];
251 currentColour = Color.white;
254 if (conservationColouring)
256 currentColour = applyConservation(currentColour, j);
259 return currentColour;
262 public void setLowerCaseColours(Color[] lcolours)
264 lowerCaseColours = lcolours;
268 * Returns the colour for the given residue character. If the residue is
269 * lower-case, and there is a specific colour defined for lower case, that
270 * colour is returned, else the colour for the upper case residue.
273 public Color findColour(char c)
275 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
277 Color colour = lowerCaseColours[symbolIndex[c]];
283 return super.findColour(c);
287 * Answers the customised name of the colour scheme, if it has one, else
291 public String getSchemeName()
293 if (schemeName != null && schemeName.length() > 0)
297 return JalviewColourScheme.UserDefined.toString();
301 * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
305 public String toAppletParameter()
308 * step 1: build a map from colours to the symbol(s) that have the colour
310 Map<Color, List<String>> colours = new HashMap<Color, List<String>>();
312 for (char symbol = 'A'; symbol <= 'Z'; symbol++)
314 String residue = String.valueOf(symbol);
315 int index = symbolIndex[symbol];
316 Color c = colors[index];
317 if (c != null && !c.equals(Color.white))
319 if (colours.get(c) == null)
321 colours.put(c, new ArrayList<String>());
323 colours.get(c).add(residue);
325 if (lowerCaseColours != null)
327 c = lowerCaseColours[index];
328 if (c != null && !c.equals(Color.white))
330 residue = residue.toLowerCase();
331 if (colours.get(c) == null)
333 colours.put(c, new ArrayList<String>());
335 colours.get(c).add(residue);
341 * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
343 List<String> residueColours = new ArrayList<String>();
344 for (Entry<Color, List<String>> cols : colours.entrySet())
346 boolean first = true;
347 StringBuilder sb = new StringBuilder();
348 for (String residue : cols.getValue())
359 * get color as hex value, dropping the alpha (ff) part
361 String hexString = Integer.toHexString(cols.getKey().getRGB())
363 sb.append(hexString);
364 residueColours.add(sb.toString());
370 Collections.sort(residueColours);
371 return StringUtils.listToDelimitedString(residueColours, ";");