JAL-2371 CollectionColourScheme wraps ColourSchemeI
[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[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.parseColourString(colour);
226           }
227           else
228           {
229             colors[colIndex] = ColorUtils.parseColourString(colour);
230           }
231         }
232       }
233     } catch (Exception ex)
234     {
235       System.out.println("Error parsing userDefinedColours:\n" + token
236               + "\n" + ex);
237     }
238
239   }
240
241   @Override
242   public Color findColour(char c, int j, SequenceI seq)
243   {
244     Color colour;
245     int index = ResidueProperties.aaIndex[c];
246
247     if (lowerCaseColours != null && 'a' <= c && c <= 'z')
248     {
249       colour = lowerCaseColours[index];
250     }
251     else
252     {
253       colour = colors[index];
254     }
255
256     return colour;
257   }
258
259   public void setLowerCaseColours(Color[] lcolours)
260   {
261     lowerCaseColours = lcolours;
262   }
263
264   /**
265    * Returns the colour for the given residue character. If the residue is
266    * lower-case, and there is a specific colour defined for lower case, that
267    * colour is returned, else the colour for the upper case residue.
268    */
269   @Override
270   public Color findColour(char c)
271   {
272     if ('a' <= c && c <= 'z' && lowerCaseColours != null)
273     {
274       Color colour = lowerCaseColours[symbolIndex[c]];
275       if (colour != null)
276       {
277         return colour;
278       }
279     }
280     return super.findColour(c);
281   }
282
283   /**
284    * Answers the customised name of the colour scheme, if it has one, else
285    * "User Defined"
286    */
287   @Override
288   public String getSchemeName()
289   {
290     if (schemeName != null && schemeName.length() > 0)
291     {
292       return schemeName;
293     }
294     return "User Defined";
295   }
296
297   /**
298    * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
299    * 
300    * @return
301    */
302   public String toAppletParameter()
303   {
304     /*
305      * step 1: build a map from colours to the symbol(s) that have the colour
306      */
307     Map<Color, List<String>> colours = new HashMap<Color, List<String>>();
308
309     for (char symbol = 'A'; symbol <= 'Z'; symbol++)
310     {
311       String residue = String.valueOf(symbol);
312       int index = symbolIndex[symbol];
313       Color c = colors[index];
314       if (c != null && !c.equals(Color.white))
315       {
316         if (colours.get(c) == null)
317         {
318           colours.put(c, new ArrayList<String>());
319         }
320         colours.get(c).add(residue);
321       }
322       if (lowerCaseColours != null)
323       {
324         c = lowerCaseColours[index];
325         if (c != null && !c.equals(Color.white))
326         {
327           residue = residue.toLowerCase();
328           if (colours.get(c) == null)
329           {
330             colours.put(c, new ArrayList<String>());
331           }
332           colours.get(c).add(residue);
333         }
334       }
335     }
336
337     /*
338      * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
339      */
340     List<String> residueColours = new ArrayList<String>();
341     for (Entry<Color, List<String>> cols : colours.entrySet())
342     {
343       boolean first = true;
344       StringBuilder sb = new StringBuilder();
345       for (String residue : cols.getValue())
346       {
347         if (!first)
348         {
349           sb.append(",");
350         }
351         sb.append(residue);
352         first = false;
353       }
354       sb.append("=");
355       /*
356        * get color as hex value, dropping the alpha (ff) part
357        */
358       String hexString = Integer.toHexString(cols.getKey().getRGB())
359               .substring(2);
360       sb.append(hexString);
361       residueColours.add(sb.toString());
362     }
363
364     /*
365      * sort and output
366      */
367     Collections.sort(residueColours);
368     return StringUtils.listToDelimitedString(residueColours, ";");
369   }
370 }