jalview 2.5 release banner
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import java.awt.*;
21
22 /**
23  * ColourSchemeProperty Binds names to hardwired colourschemes and tries to deal
24  * intelligently with mapping unknown names to user defined colourschemes (that
25  * exist or can be created from the string representation of the colourscheme
26  * name - either a hex RGB triplet or a named colour under java.awt.color )
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class ColourSchemeProperty
32 {
33   /** DOCUMENT ME!! */
34   public static final int CLUSTAL = 0;
35
36   /** DOCUMENT ME!! */
37   public static final int BLOSUM = 1;
38
39   /** DOCUMENT ME!! */
40   public static final int PID = 2;
41
42   /** DOCUMENT ME!! */
43   public static final int ZAPPO = 3;
44
45   /** DOCUMENT ME!! */
46   public static final int TAYLOR = 4;
47
48   /** DOCUMENT ME!! */
49   public static final int HYDROPHOBIC = 5;
50
51   /** DOCUMENT ME!! */
52   public static final int HELIX = 6;
53
54   /** DOCUMENT ME!! */
55   public static final int STRAND = 7;
56
57   /** DOCUMENT ME!! */
58   public static final int TURN = 8;
59
60   /** DOCUMENT ME!! */
61   public static final int BURIED = 9;
62
63   /** DOCUMENT ME!! */
64   public static final int NUCLEOTIDE = 10;
65
66   /** DOCUMENT ME!! */
67   public static final int USER_DEFINED = 11;
68
69   /** No Colourscheme Index */
70   public static final int NONE = 12;
71
72   /** Undefined Colourscheme Index */
73   public static final int UNDEFINED = 13;
74
75   /**
76    * DOCUMENT ME!
77    * 
78    * @param name
79    *          DOCUMENT ME!
80    * 
81    * @return DOCUMENT ME!
82    */
83   public static int getColourIndexFromName(String name)
84   {
85     int ret = UNDEFINED;
86
87     if (name.equalsIgnoreCase("Clustal"))
88     {
89       ret = CLUSTAL;
90     }
91     else if (name.equalsIgnoreCase("Blosum62"))
92     {
93       ret = BLOSUM;
94     }
95     else if (name.equalsIgnoreCase("% Identity"))
96     {
97       ret = PID;
98     }
99     else if (name.equalsIgnoreCase("Zappo"))
100     {
101       ret = ZAPPO;
102     }
103     else if (name.equalsIgnoreCase("Taylor"))
104     {
105       ret = TAYLOR;
106     }
107     else if (name.equalsIgnoreCase("Hydrophobic"))
108     {
109       ret = HYDROPHOBIC;
110     }
111     else if (name.equalsIgnoreCase("Helix Propensity"))
112     {
113       ret = HELIX;
114     }
115     else if (name.equalsIgnoreCase("Strand Propensity"))
116     {
117       ret = STRAND;
118     }
119     else if (name.equalsIgnoreCase("Turn Propensity"))
120     {
121       ret = TURN;
122     }
123     else if (name.equalsIgnoreCase("Buried Index"))
124     {
125       ret = BURIED;
126     }
127     else if (name.equalsIgnoreCase("Nucleotide"))
128     {
129       ret = NUCLEOTIDE;
130     }
131     else if (name.equalsIgnoreCase("User Defined"))
132     {
133       ret = USER_DEFINED;
134     }
135     else if (name.equalsIgnoreCase("None"))
136     {
137       ret = NONE;
138     }
139
140     return ret;
141   }
142
143   /**
144    * DOCUMENT ME!
145    * 
146    * @param cs
147    *          DOCUMENT ME!
148    * 
149    * @return DOCUMENT ME!
150    */
151   public static String getColourName(ColourSchemeI cs)
152   {
153
154     int index = NONE;
155
156     if (cs instanceof ClustalxColourScheme)
157     {
158       index = CLUSTAL;
159     }
160     else if (cs instanceof Blosum62ColourScheme)
161     {
162       index = BLOSUM;
163     }
164     else if (cs instanceof PIDColourScheme)
165     {
166       index = PID;
167     }
168     else if (cs instanceof ZappoColourScheme)
169     {
170       index = ZAPPO;
171     }
172     else if (cs instanceof TaylorColourScheme)
173     {
174       index = TAYLOR;
175     }
176     else if (cs instanceof HydrophobicColourScheme)
177     {
178       index = HYDROPHOBIC;
179     }
180     else if (cs instanceof HelixColourScheme)
181     {
182       index = HELIX;
183     }
184     else if (cs instanceof StrandColourScheme)
185     {
186       index = STRAND;
187     }
188     else if (cs instanceof TurnColourScheme)
189     {
190       index = TURN;
191     }
192     else if (cs instanceof BuriedColourScheme)
193     {
194       index = BURIED;
195     }
196     else if (cs instanceof NucleotideColourScheme)
197     {
198       index = NUCLEOTIDE;
199     }
200     else if (cs instanceof UserColourScheme)
201     {
202       if ((((UserColourScheme) cs).getName() != null)
203               && (((UserColourScheme) cs).getName().length() > 0))
204       {
205         return ((UserColourScheme) cs).getName();
206       }
207       // get default colourscheme name
208       index = USER_DEFINED;
209     }
210
211     return getColourName(index);
212   }
213
214   /**
215    * DOCUMENT ME!
216    * 
217    * @param index
218    *          DOCUMENT ME!
219    * 
220    * @return DOCUMENT ME!
221    */
222   public static String getColourName(int index)
223   {
224     String ret = null;
225
226     switch (index)
227     {
228     case CLUSTAL:
229       ret = "Clustal";
230
231       break;
232
233     case BLOSUM:
234       ret = "Blosum62";
235
236       break;
237
238     case PID:
239       ret = "% Identity";
240
241       break;
242
243     case ZAPPO:
244       ret = "Zappo";
245
246       break;
247
248     case TAYLOR:
249       ret = "Taylor";
250       break;
251
252     case HYDROPHOBIC:
253       ret = "Hydrophobic";
254
255       break;
256
257     case HELIX:
258       ret = "Helix Propensity";
259
260       break;
261
262     case STRAND:
263       ret = "Strand Propensity";
264
265       break;
266
267     case TURN:
268       ret = "Turn Propensity";
269
270       break;
271
272     case BURIED:
273       ret = "Buried Index";
274
275       break;
276
277     case NUCLEOTIDE:
278       ret = "Nucleotide";
279
280       break;
281
282     case USER_DEFINED:
283       ret = "User Defined";
284
285       break;
286
287     default:
288       ret = "None";
289
290       break;
291     }
292
293     return ret;
294   }
295
296   /**
297    * DOCUMENT ME!
298    * 
299    * @param al
300    *          DOCUMENT ME!
301    * @param name
302    *          DOCUMENT ME!
303    * 
304    * @return DOCUMENT ME!
305    */
306   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
307           String name)
308   {
309     return getColour(al.getSequences(), al.getWidth(), name);
310   }
311
312   /**
313    * retrieve or create colourscheme associated with name
314    * 
315    * @param seqs
316    *          sequences to colour
317    * @param width
318    *          range of sequences to colour
319    * @param name
320    *          colourscheme name, applet colour parameter specification, or
321    *          string to parse as colour for new coloursheme
322    * @return Valid Colourscheme
323    */
324   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
325           String name)
326   {
327     int colindex = getColourIndexFromName(name);
328     if (colindex == UNDEFINED)
329     {
330       if (name.indexOf('=') == -1)
331       {
332         // try to build a colour from the string directly
333         try
334         {
335           return new UserColourScheme(name);
336         } catch (Exception e)
337         {
338           // System.err.println("Ignoring unknown colourscheme name");
339         }
340       }
341       else
342       {
343         // try to parse the string as a residue colourscheme
344         try
345         {
346           // fix the launchApp user defined coloursheme transfer bug
347           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
348                   "white");
349           ucs.parseAppletParameter(name);
350
351         } catch (Exception e)
352         {
353           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
354         }
355       }
356     }
357     return getColour(seqs, width, getColourIndexFromName(name));
358   }
359
360   /**
361    * DOCUMENT ME!
362    * 
363    * @param seqs
364    *          DOCUMENT ME!
365    * @param width
366    *          DOCUMENT ME!
367    * @param index
368    *          DOCUMENT ME!
369    * 
370    * @return DOCUMENT ME!
371    */
372   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
373           int index)
374   {
375     ColourSchemeI cs = null;
376
377     switch (index)
378     {
379     case CLUSTAL:
380       cs = new ClustalxColourScheme(seqs, width);
381
382       break;
383
384     case BLOSUM:
385       cs = new Blosum62ColourScheme();
386
387       break;
388
389     case PID:
390       cs = new PIDColourScheme();
391
392       break;
393
394     case ZAPPO:
395       cs = new ZappoColourScheme();
396
397       break;
398
399     case TAYLOR:
400       cs = new TaylorColourScheme();
401       break;
402
403     case HYDROPHOBIC:
404       cs = new HydrophobicColourScheme();
405
406       break;
407
408     case HELIX:
409       cs = new HelixColourScheme();
410
411       break;
412
413     case STRAND:
414       cs = new StrandColourScheme();
415
416       break;
417
418     case TURN:
419       cs = new TurnColourScheme();
420
421       break;
422
423     case BURIED:
424       cs = new BuriedColourScheme();
425
426       break;
427
428     case NUCLEOTIDE:
429       cs = new NucleotideColourScheme();
430
431       break;
432
433     case USER_DEFINED:
434       Color[] col = new Color[24];
435       for (int i = 0; i < 24; i++)
436       {
437         col[i] = Color.white;
438       }
439       cs = new UserColourScheme(col);
440       break;
441
442     default:
443       break;
444     }
445
446     return cs;
447   }
448
449   public static Color getAWTColorFromName(String name)
450   {
451     Color col = null;
452     name = name.toLowerCase();
453     if (name.equals("black"))
454     {
455       col = Color.black;
456     }
457     else if (name.equals("blue"))
458     {
459       col = Color.blue;
460     }
461     else if (name.equals("cyan"))
462     {
463       col = Color.cyan;
464     }
465     else if (name.equals("darkGray"))
466     {
467       col = Color.darkGray;
468     }
469     else if (name.equals("gray"))
470     {
471       col = Color.gray;
472     }
473     else if (name.equals("green"))
474     {
475       col = Color.green;
476     }
477     else if (name.equals("lightGray"))
478     {
479       col = Color.lightGray;
480     }
481     else if (name.equals("magenta"))
482     {
483       col = Color.magenta;
484     }
485     else if (name.equals("orange"))
486     {
487       col = Color.orange;
488     }
489     else if (name.equals("pink"))
490     {
491       col = Color.pink;
492     }
493     else if (name.equals("red"))
494     {
495       col = Color.red;
496     }
497     else if (name.equals("white"))
498     {
499       col = Color.white;
500     }
501     else if (name.equals("yellow"))
502     {
503       col = Color.yellow;
504     }
505
506     return col;
507   }
508 }