JAL-1270 JAL-2416 JAL-2839 set alwaysRun=true in test AfterMethod!
[jalview.git] / src / jalview / schemes / UserColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.schemes;
22
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;
28
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;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.StringTokenizer;
37
38 public class UserColourScheme extends ResidueColourScheme
39 {
40   /*
41    * lookup (by symbol index) of lower case colours (if configured)
42    */
43   Color[] lowerCaseColours;
44
45   protected String schemeName;
46
47   public UserColourScheme()
48   {
49     super(ResidueProperties.aaIndex);
50   }
51
52   public UserColourScheme(Color[] newColors)
53   {
54     super(ResidueProperties.aaIndex);
55     colors = newColors;
56   }
57
58   @Override
59   public ColourSchemeI getInstance(AnnotatedCollectionI sg,
60           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
61   {
62     return new UserColourScheme(this);
63   }
64
65   /**
66    * Copy constructor
67    * 
68    * @return
69    */
70   protected UserColourScheme(UserColourScheme from)
71   {
72     this(from.colors);
73     schemeName = from.schemeName;
74     if (from.lowerCaseColours != null)
75     {
76       lowerCaseColours = new Color[from.lowerCaseColours.length];
77       System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0,
78               from.lowerCaseColours.length);
79     }
80   }
81
82   /**
83    * Constructor for an animino acid colour scheme. The colour specification may
84    * be one of
85    * <ul>
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>
89    * </ul>
90    * 
91    * @param colour
92    */
93   public UserColourScheme(String colour)
94   {
95     super(ResidueProperties.aaIndex);
96
97     if (colour.contains("="))
98     {
99       /*
100        * a list of colours per residue(s)
101        */
102       parseAppletParameter(colour);
103       return;
104     }
105
106     Color col = ColorUtils.parseColourString(colour);
107
108     if (col == null)
109     {
110       System.out.println("Making colour from name: " + colour);
111       col = ColorUtils.createColourFromName(colour);
112     }
113
114     setAll(col);
115     schemeName = colour;
116   }
117
118   /**
119    * Sets all symbols to the specified colour
120    * 
121    * @param col
122    */
123   protected void setAll(Color col)
124   {
125     if (symbolIndex == null)
126     {
127       return;
128     }
129     int max = 0;
130     for (int index : symbolIndex)
131     {
132       max = Math.max(max, index);
133     }
134     colors = new Color[max + 1];
135     for (int i = 0; i <= max; i++)
136     {
137       colors[i] = col;
138     }
139   }
140
141   public Color[] getColours()
142   {
143     return colors;
144   }
145
146   public Color[] getLowerCaseColours()
147   {
148     return lowerCaseColours;
149   }
150
151   public void setName(String name)
152   {
153     schemeName = name;
154   }
155
156   public String getName()
157   {
158     return schemeName;
159   }
160
161   /**
162    * Parse and save residue colours specified as (for example)
163    * 
164    * <pre>
165    *     D,E=red; K,R,H=0022FF; c=100,50,75
166    * </pre>
167    * 
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
172    * U.)
173    * 
174    * @param paramValue
175    */
176   void parseAppletParameter(String paramValue)
177   {
178     setAll(Color.white);
179
180     StringTokenizer st = new StringTokenizer(paramValue, ";");
181     StringTokenizer st2;
182     String token = null, colour, residues;
183     try
184     {
185       while (st.hasMoreElements())
186       {
187         token = st.nextToken().trim();
188         residues = token.substring(0, token.indexOf("="));
189         colour = token.substring(token.indexOf("=") + 1);
190
191         st2 = new StringTokenizer(residues, " ,");
192         while (st2.hasMoreTokens())
193         {
194           String residue = st2.nextToken();
195
196           int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
197           if (colIndex == -1)
198           {
199             continue;
200           }
201
202           if (residue.equalsIgnoreCase("lowerCase"))
203           {
204             if (lowerCaseColours == null)
205             {
206               lowerCaseColours = new Color[colors.length];
207             }
208             for (int i = 0; i < lowerCaseColours.length; i++)
209             {
210               if (lowerCaseColours[i] == null)
211               {
212                 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
213               }
214             }
215
216             continue;
217           }
218
219           if (residue.equals(residue.toLowerCase()))
220           {
221             if (lowerCaseColours == null)
222             {
223               lowerCaseColours = new Color[colors.length];
224             }
225             lowerCaseColours[colIndex] = ColorUtils
226                     .parseColourString(colour);
227           }
228           else
229           {
230             colors[colIndex] = ColorUtils.parseColourString(colour);
231           }
232         }
233       }
234     } catch (Exception ex)
235     {
236       System.out.println(
237               "Error parsing userDefinedColours:\n" + token + "\n" + ex);
238     }
239
240   }
241
242   public void setLowerCaseColours(Color[] lcolours)
243   {
244     lowerCaseColours = lcolours;
245   }
246
247   /**
248    * Returns the colour for the given residue character. If the residue is
249    * lower-case, and there is a specific colour defined for lower case, that
250    * colour is returned, else the colour for the upper case residue.
251    */
252   @Override
253   public Color findColour(char c)
254   {
255     if ('a' <= c && c <= 'z' && lowerCaseColours != null)
256     {
257       Color colour = lowerCaseColours[symbolIndex[c]];
258       if (colour != null)
259       {
260         return colour;
261       }
262     }
263     return super.findColour(c);
264   }
265
266   /**
267    * Answers the customised name of the colour scheme, if it has one, else "User
268    * Defined"
269    */
270   @Override
271   public String getSchemeName()
272   {
273     if (schemeName != null && schemeName.length() > 0)
274     {
275       return schemeName;
276     }
277     return ResidueColourScheme.USER_DEFINED;
278   }
279
280   /**
281    * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
282    * 
283    * @return
284    */
285   public String toAppletParameter()
286   {
287     /*
288      * step 1: build a map from colours to the symbol(s) that have the colour
289      */
290     Map<Color, List<String>> colours = new HashMap<>();
291
292     for (char symbol = 'A'; symbol <= 'Z'; symbol++)
293     {
294       String residue = String.valueOf(symbol);
295       int index = symbolIndex[symbol];
296       Color c = colors[index];
297       if (c != null && !c.equals(Color.white))
298       {
299         if (colours.get(c) == null)
300         {
301           colours.put(c, new ArrayList<String>());
302         }
303         colours.get(c).add(residue);
304       }
305       if (lowerCaseColours != null)
306       {
307         c = lowerCaseColours[index];
308         if (c != null && !c.equals(Color.white))
309         {
310           residue = residue.toLowerCase();
311           if (colours.get(c) == null)
312           {
313             colours.put(c, new ArrayList<String>());
314           }
315           colours.get(c).add(residue);
316         }
317       }
318     }
319
320     /*
321      * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
322      */
323     List<String> residueColours = new ArrayList<>();
324     for (Entry<Color, List<String>> cols : colours.entrySet())
325     {
326       boolean first = true;
327       StringBuilder sb = new StringBuilder();
328       for (String residue : cols.getValue())
329       {
330         if (!first)
331         {
332           sb.append(",");
333         }
334         sb.append(residue);
335         first = false;
336       }
337       sb.append("=");
338       /*
339        * get color as hex value, dropping the alpha (ff) part
340        */
341       String hexString = Integer.toHexString(cols.getKey().getRGB())
342               .substring(2);
343       sb.append(hexString);
344       residueColours.add(sb.toString());
345     }
346
347     /*
348      * sort and output
349      */
350     Collections.sort(residueColours);
351     return StringUtils.listToDelimitedString(residueColours, ";");
352   }
353
354   @Override
355   public boolean hasGapColour()
356   {
357     return (findColour(' ') != null);
358   }
359 }