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.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
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,
62 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
64 return new UserColourScheme(this);
72 protected UserColourScheme(UserColourScheme from)
75 schemeName = from.schemeName;
76 if (from.lowerCaseColours != null)
78 lowerCaseColours = new Color[from.lowerCaseColours.length];
79 System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0,
80 from.lowerCaseColours.length);
85 * Constructor for an animino acid colour scheme. The colour specification may
88 * <li>an AWT colour name e.g. red</li>
89 * <li>an AWT hex rgb colour e.g. ff2288</li>
90 * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
95 public UserColourScheme(String colour)
97 super(ResidueProperties.aaIndex);
99 if (colour.contains("="))
102 * a list of colours per residue(s)
104 parseAppletParameter(colour);
108 Color col = ColorUtils.parseColourString(colour);
112 System.out.println("Making colour from name: " + colour);
113 col = ColorUtils.createColourFromName(colour);
121 * Sets all symbols to the specified colour
125 protected void setAll(Color col)
127 if (symbolIndex == null)
132 for (int index : symbolIndex)
134 max = Math.max(max, index);
136 colors = new Color[max + 1];
137 for (int i = 0; i <= max; i++)
143 public Color[] getColours()
148 public Color[] getLowerCaseColours()
150 return lowerCaseColours;
153 public void setName(String name)
158 public String getName()
164 * Parse and save residue colours specified as (for example)
167 * D,E=red; K,R,H=0022FF; c=100,50,75
170 * This should be a semi-colon separated list of colours, which may be defined
171 * by colour name, hex value or comma-separated RGB triple. Each colour is
172 * defined for a comma-separated list of amino acid single letter codes. (Note
173 * that this also allows a colour scheme to be defined for ACGT, but not for
178 void parseAppletParameter(String paramValue)
182 StringTokenizer st = new StringTokenizer(paramValue, ";");
184 String token = null, colour, residues;
187 while (st.hasMoreElements())
189 token = st.nextToken().trim();
190 residues = token.substring(0, token.indexOf("="));
191 colour = token.substring(token.indexOf("=") + 1);
193 st2 = new StringTokenizer(residues, " ,");
194 while (st2.hasMoreTokens())
196 String residue = st2.nextToken();
198 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
204 if (residue.equalsIgnoreCase("lowerCase"))
206 if (lowerCaseColours == null)
208 lowerCaseColours = new Color[colors.length];
210 for (int i = 0; i < lowerCaseColours.length; i++)
212 if (lowerCaseColours[i] == null)
214 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
221 if (residue.equals(residue.toLowerCase()))
223 if (lowerCaseColours == null)
225 lowerCaseColours = new Color[colors.length];
227 lowerCaseColours[colIndex] = ColorUtils
228 .parseColourString(colour);
232 colors[colIndex] = ColorUtils.parseColourString(colour);
236 } catch (Exception ex)
239 "Error parsing userDefinedColours:\n" + token + "\n" + ex);
244 public void setLowerCaseColours(Color[] lcolours)
246 lowerCaseColours = lcolours;
250 * Returns the colour for the given residue character. If the residue is
251 * lower-case, and there is a specific colour defined for lower case, that
252 * colour is returned, else the colour for the upper case residue.
255 public Color findColour(char c)
257 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
259 Color colour = lowerCaseColours[symbolIndex[c]];
265 return super.findColour(c);
269 * Answers the customised name of the colour scheme, if it has one, else "User
273 public String getSchemeName()
275 if (schemeName != null && schemeName.length() > 0)
279 return ResidueColourScheme.USER_DEFINED;
283 * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
287 public String toAppletParameter()
290 * step 1: build a map from colours to the symbol(s) that have the colour
292 Map<Color, List<String>> colours = new HashMap<>();
294 for (char symbol = 'A'; symbol <= 'Z'; symbol++)
296 String residue = String.valueOf(symbol);
297 int index = symbolIndex[symbol];
298 Color c = colors[index];
299 if (c != null && !c.equals(Color.white))
301 if (colours.get(c) == null)
303 colours.put(c, new ArrayList<String>());
305 colours.get(c).add(residue);
307 if (lowerCaseColours != null)
309 c = lowerCaseColours[index];
310 if (c != null && !c.equals(Color.white))
312 residue = residue.toLowerCase();
313 if (colours.get(c) == null)
315 colours.put(c, new ArrayList<String>());
317 colours.get(c).add(residue);
323 * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
325 List<String> residueColours = new ArrayList<>();
326 for (Entry<Color, List<String>> cols : colours.entrySet())
328 boolean first = true;
329 StringBuilder sb = new StringBuilder();
330 for (String residue : cols.getValue())
341 * get color as hex value, dropping the alpha (ff) part
343 String hexString = Integer.toHexString(cols.getKey().getRGB())
345 sb.append(hexString);
346 residueColours.add(sb.toString());
352 Collections.sort(residueColours);
353 return StringUtils.listToDelimitedString(residueColours, ";");
357 public boolean hasGapColour()
359 return (findColour(' ') != null);