JAL-2360 ColourSchemes holds configured schemes, AlignFrame colour menu
[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     UserColourScheme usc = new UserColourScheme(colors);
63     if (lowerCaseColours != null)
64     {
65       usc.schemeName = schemeName;
66       usc.lowerCaseColours = new Color[lowerCaseColours.length];
67       System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0,
68               lowerCaseColours.length);
69     }
70     return usc;
71   }
72
73   /**
74    * Constructor for an animino acid colour scheme. The colour specification may
75    * be one of
76    * <ul>
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>
80    * </ul>
81    * 
82    * @param colour
83    */
84   public UserColourScheme(String colour)
85   {
86     super(ResidueProperties.aaIndex);
87
88     if (colour.contains("="))
89     {
90       /*
91        * a list of colours per residue(s)
92        */
93       parseAppletParameter(colour);
94       return;
95     }
96
97     Color col = ColorUtils.parseColourString(colour);
98
99     if (col == null)
100     {
101       System.out.println("Making colour from name: " + colour);
102       col = ColorUtils.createColourFromName(colour);
103     }
104
105     setAll(col);
106     schemeName = colour;
107   }
108
109   /**
110    * Sets all symbols to the specified colour
111    * 
112    * @param col
113    */
114   protected void setAll(Color col)
115   {
116     if (symbolIndex == null)
117     {
118       return;
119     }
120     int max = 0;
121     for (int index : symbolIndex)
122     {
123       max = Math.max(max, index);
124     }
125     colors = new Color[max + 1];
126     for (int i = 0; i <= max; i++)
127     {
128       colors[i] = col;
129     }
130   }
131
132   public Color[] getColours()
133   {
134     return colors;
135   }
136
137   public Color[] getLowerCaseColours()
138   {
139     return lowerCaseColours;
140   }
141
142   public void setName(String name)
143   {
144     schemeName = name;
145   }
146
147   public String getName()
148   {
149     return schemeName;
150   }
151
152   /**
153    * Parse and save residue colours specified as (for example)
154    * 
155    * <pre>
156    *     D,E=red; K,R,H=0022FF; c=100,50,75
157    * </pre>
158    * 
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
163    * U.)
164    * 
165    * @param paramValue
166    */
167   void parseAppletParameter(String paramValue)
168   {
169     setAll(Color.white);
170
171     StringTokenizer st = new StringTokenizer(paramValue, ";");
172     StringTokenizer st2;
173     String token = null, colour, residues;
174     try
175     {
176       while (st.hasMoreElements())
177       {
178         token = st.nextToken().trim();
179         residues = token.substring(0, token.indexOf("="));
180         colour = token.substring(token.indexOf("=") + 1);
181
182         st2 = new StringTokenizer(residues, " ,");
183         while (st2.hasMoreTokens())
184         {
185           String residue = st2.nextToken();
186
187           int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
188           if (colIndex == -1)
189           {
190             continue;
191           }
192
193           if (residue.equalsIgnoreCase("lowerCase"))
194           {
195             if (lowerCaseColours == null)
196             {
197               lowerCaseColours = new Color[colors.length];
198             }
199             for (int i = 0; i < lowerCaseColours.length; i++)
200             {
201               if (lowerCaseColours[i] == null)
202               {
203                 lowerCaseColours[i] = ColorUtils.parseColourString(colour);
204               }
205             }
206
207             continue;
208           }
209
210           if (residue.equals(residue.toLowerCase()))
211           {
212             if (lowerCaseColours == null)
213             {
214               lowerCaseColours = new Color[colors.length];
215             }
216             lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour);
217           }
218           else
219           {
220             colors[colIndex] = ColorUtils.parseColourString(colour);
221           }
222         }
223       }
224     } catch (Exception ex)
225     {
226       System.out.println("Error parsing userDefinedColours:\n" + token
227               + "\n" + ex);
228     }
229
230   }
231
232   @Override
233   public Color findColour(char c, int j, SequenceI seq)
234   {
235     Color currentColour;
236     int index = ResidueProperties.aaIndex[c];
237
238     if ((threshold == 0) || aboveThreshold(c, j))
239     {
240       if (lowerCaseColours != null && 'a' <= c && c <= 'z')
241       {
242         currentColour = lowerCaseColours[index];
243       }
244       else
245       {
246         currentColour = colors[index];
247       }
248     }
249     else
250     {
251       currentColour = Color.white;
252     }
253
254     if (conservationColouring)
255     {
256       currentColour = applyConservation(currentColour, j);
257     }
258
259     return currentColour;
260   }
261
262   public void setLowerCaseColours(Color[] lcolours)
263   {
264     lowerCaseColours = lcolours;
265   }
266
267   /**
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.
271    */
272   @Override
273   public Color findColour(char c)
274   {
275     if ('a' <= c && c <= 'z' && lowerCaseColours != null)
276     {
277       Color colour = lowerCaseColours[symbolIndex[c]];
278       if (colour != null)
279       {
280         return colour;
281       }
282     }
283     return super.findColour(c);
284   }
285
286   /**
287    * Answers the customised name of the colour scheme, if it has one, else
288    * "User Defined"
289    */
290   @Override
291   public String getSchemeName()
292   {
293     if (schemeName != null && schemeName.length() > 0)
294     {
295       return schemeName;
296     }
297     return "User Defined";
298   }
299
300   /**
301    * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd
302    * 
303    * @return
304    */
305   public String toAppletParameter()
306   {
307     /*
308      * step 1: build a map from colours to the symbol(s) that have the colour
309      */
310     Map<Color, List<String>> colours = new HashMap<Color, List<String>>();
311
312     for (char symbol = 'A'; symbol <= 'Z'; symbol++)
313     {
314       String residue = String.valueOf(symbol);
315       int index = symbolIndex[symbol];
316       Color c = colors[index];
317       if (c != null && !c.equals(Color.white))
318       {
319         if (colours.get(c) == null)
320         {
321           colours.put(c, new ArrayList<String>());
322         }
323         colours.get(c).add(residue);
324       }
325       if (lowerCaseColours != null)
326       {
327         c = lowerCaseColours[index];
328         if (c != null && !c.equals(Color.white))
329         {
330           residue = residue.toLowerCase();
331           if (colours.get(c) == null)
332           {
333             colours.put(c, new ArrayList<String>());
334           }
335           colours.get(c).add(residue);
336         }
337       }
338     }
339
340     /*
341      * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
342      */
343     List<String> residueColours = new ArrayList<String>();
344     for (Entry<Color, List<String>> cols : colours.entrySet())
345     {
346       boolean first = true;
347       StringBuilder sb = new StringBuilder();
348       for (String residue : cols.getValue())
349       {
350         if (!first)
351         {
352           sb.append(",");
353         }
354         sb.append(residue);
355         first = false;
356       }
357       sb.append("=");
358       /*
359        * get color as hex value, dropping the alpha (ff) part
360        */
361       String hexString = Integer.toHexString(cols.getKey().getRGB())
362               .substring(2);
363       sb.append(hexString);
364       residueColours.add(sb.toString());
365     }
366
367     /*
368      * sort and output
369      */
370     Collections.sort(residueColours);
371     return StringUtils.listToDelimitedString(residueColours, ";");
372   }
373 }