update author list in license for (JAL-826)
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 ). The
27  * values of the colourscheme constants is important for callers of
28  * getColourName(int i), since it can be used to enumerate the set of built in
29  * colours. The FIRST_COLOUR and LAST_COLOUR symbols are provided for this.
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class ColourSchemeProperty
35 {
36   /** Undefined Colourscheme Index */
37   public static final int UNDEFINED = -1;
38
39   /** for schemes defined on the fly */
40   public static final int USER_DEFINED = 0;
41
42   /** No Colourscheme Index */
43   public static final int NONE = 1;
44
45   /** DOCUMENT ME!! */
46   public static final int CLUSTAL = 2;
47
48   /** DOCUMENT ME!! */
49   public static final int BLOSUM = 3;
50
51   /** DOCUMENT ME!! */
52   public static final int PID = 4;
53
54   /** DOCUMENT ME!! */
55   public static final int ZAPPO = 5;
56
57   /** DOCUMENT ME!! */
58   public static final int TAYLOR = 6;
59
60   /** DOCUMENT ME!! */
61   public static final int HYDROPHOBIC = 7;
62
63   /** DOCUMENT ME!! */
64   public static final int HELIX = 8;
65
66   /** DOCUMENT ME!! */
67   public static final int STRAND = 9;
68
69   /** DOCUMENT ME!! */
70   public static final int TURN = 10;
71
72   /** DOCUMENT ME!! */
73   public static final int BURIED = 11;
74
75   /** DOCUMENT ME!! */
76   public static final int NUCLEOTIDE = 12;
77
78   /**
79    * purine/pyrimidine
80    */
81   public static final int PURINEPYRIMIDINE = 13;
82
83   public static final int COVARIATION = 14;
84
85   /**
86    * index of first colourscheme (includes 'None')
87    */
88   public static final int FIRST_COLOUR = NONE;
89
90   public static final int LAST_COLOUR = NUCLEOTIDE;
91
92   /**
93    * DOCUMENT ME!
94    * 
95    * @param name
96    *          DOCUMENT ME!
97    * 
98    * @return DOCUMENT ME!
99    */
100   public static int getColourIndexFromName(String name)
101   {
102     int ret = UNDEFINED;
103
104     if (name.equalsIgnoreCase("Clustal"))
105     {
106       ret = CLUSTAL;
107     }
108     else if (name.equalsIgnoreCase("Blosum62"))
109     {
110       ret = BLOSUM;
111     }
112     else if (name.equalsIgnoreCase("% Identity"))
113     {
114       ret = PID;
115     }
116     else if (name.equalsIgnoreCase("Zappo"))
117     {
118       ret = ZAPPO;
119     }
120     else if (name.equalsIgnoreCase("Taylor"))
121     {
122       ret = TAYLOR;
123     }
124     else if (name.equalsIgnoreCase("Hydrophobic"))
125     {
126       ret = HYDROPHOBIC;
127     }
128     else if (name.equalsIgnoreCase("Helix Propensity"))
129     {
130       ret = HELIX;
131     }
132     else if (name.equalsIgnoreCase("Strand Propensity"))
133     {
134       ret = STRAND;
135     }
136     else if (name.equalsIgnoreCase("Turn Propensity"))
137     {
138       ret = TURN;
139     }
140     else if (name.equalsIgnoreCase("Buried Index"))
141     {
142       ret = BURIED;
143     }
144     else if (name.equalsIgnoreCase("Nucleotide"))
145     {
146       ret = NUCLEOTIDE;
147     }
148     else if (name.equalsIgnoreCase("User Defined"))
149     {
150       ret = USER_DEFINED;
151     }
152     else if (name.equalsIgnoreCase("None"))
153     {
154       ret = NONE;
155     }
156     else if (name.equalsIgnoreCase("Purine/Pyrimidine"))
157     {
158       ret = PURINEPYRIMIDINE;
159     }
160     // else if (name.equalsIgnoreCase("Covariation"))
161     // {
162     // ret = COVARIATION;
163     // }
164
165     return ret;
166   }
167
168   /**
169    * DOCUMENT ME!
170    * 
171    * @param cs
172    *          DOCUMENT ME!
173    * 
174    * @return DOCUMENT ME!
175    */
176   public static String getColourName(ColourSchemeI cs)
177   {
178
179     int index = NONE;
180
181     if (cs instanceof ClustalxColourScheme)
182     {
183       index = CLUSTAL;
184     }
185     else if (cs instanceof Blosum62ColourScheme)
186     {
187       index = BLOSUM;
188     }
189     else if (cs instanceof PIDColourScheme)
190     {
191       index = PID;
192     }
193     else if (cs instanceof ZappoColourScheme)
194     {
195       index = ZAPPO;
196     }
197     else if (cs instanceof TaylorColourScheme)
198     {
199       index = TAYLOR;
200     }
201     else if (cs instanceof HydrophobicColourScheme)
202     {
203       index = HYDROPHOBIC;
204     }
205     else if (cs instanceof HelixColourScheme)
206     {
207       index = HELIX;
208     }
209     else if (cs instanceof StrandColourScheme)
210     {
211       index = STRAND;
212     }
213     else if (cs instanceof TurnColourScheme)
214     {
215       index = TURN;
216     }
217     else if (cs instanceof BuriedColourScheme)
218     {
219       index = BURIED;
220     }
221     else if (cs instanceof NucleotideColourScheme)
222     {
223       index = NUCLEOTIDE;
224     }
225     else if (cs instanceof PurinePyrimidineColourScheme)
226     {
227       index = PURINEPYRIMIDINE;
228     }
229     /*
230      * else if (cs instanceof CovariationColourScheme) { index = COVARIATION; }
231      */
232     else if (cs instanceof UserColourScheme)
233     {
234       if ((((UserColourScheme) cs).getName() != null)
235               && (((UserColourScheme) cs).getName().length() > 0))
236       {
237         return ((UserColourScheme) cs).getName();
238       }
239       // get default colourscheme name
240       index = USER_DEFINED;
241     }
242
243     return getColourName(index);
244   }
245
246   /**
247    * DOCUMENT ME!
248    * 
249    * @param index
250    *          DOCUMENT ME!
251    * 
252    * @return DOCUMENT ME!
253    */
254   public static String getColourName(int index)
255   {
256     String ret = null;
257
258     switch (index)
259     {
260     case CLUSTAL:
261       ret = "Clustal";
262
263       break;
264
265     case BLOSUM:
266       ret = "Blosum62";
267
268       break;
269
270     case PID:
271       ret = "% Identity";
272
273       break;
274
275     case ZAPPO:
276       ret = "Zappo";
277
278       break;
279
280     case TAYLOR:
281       ret = "Taylor";
282       break;
283
284     case HYDROPHOBIC:
285       ret = "Hydrophobic";
286
287       break;
288
289     case HELIX:
290       ret = "Helix Propensity";
291
292       break;
293
294     case STRAND:
295       ret = "Strand Propensity";
296
297       break;
298
299     case TURN:
300       ret = "Turn Propensity";
301
302       break;
303
304     case BURIED:
305       ret = "Buried Index";
306
307       break;
308
309     case NUCLEOTIDE:
310       ret = "Nucleotide";
311
312       break;
313
314     case PURINEPYRIMIDINE:
315       ret = "Purine/Pyrimidine";
316
317       break;
318
319     /*
320      * case COVARIATION: ret = "Covariation";
321      * 
322      * break;
323      */
324     case USER_DEFINED:
325       ret = "User Defined";
326
327       break;
328
329     default:
330       ret = "None";
331
332       break;
333     }
334
335     return ret;
336   }
337
338   /**
339    * DOCUMENT ME!
340    * 
341    * @param al
342    *          DOCUMENT ME!
343    * @param name
344    *          DOCUMENT ME!
345    * 
346    * @return DOCUMENT ME!
347    */
348   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
349           String name)
350   {
351     return getColour(al.getSequences(), al.getWidth(), name);
352   }
353
354   /**
355    * retrieve or create colourscheme associated with name
356    * 
357    * @param seqs
358    *          sequences to colour
359    * @param width
360    *          range of sequences to colour
361    * @param name
362    *          colourscheme name, applet colour parameter specification, or
363    *          string to parse as colour for new coloursheme
364    * @return Valid Colourscheme
365    */
366   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
367           String name)
368   {
369     int colindex = getColourIndexFromName(name);
370     if (colindex == UNDEFINED)
371     {
372       if (name.indexOf('=') == -1)
373       {
374         // try to build a colour from the string directly
375         try
376         {
377           return new UserColourScheme(name);
378         } catch (Exception e)
379         {
380           // System.err.println("Ignoring unknown colourscheme name");
381         }
382       }
383       else
384       {
385         // try to parse the string as a residue colourscheme
386         try
387         {
388           // fix the launchApp user defined coloursheme transfer bug
389           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
390                   "white");
391           ucs.parseAppletParameter(name);
392
393         } catch (Exception e)
394         {
395           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
396         }
397       }
398     }
399     return getColour(seqs, width, getColourIndexFromName(name));
400   }
401
402   /**
403    * DOCUMENT ME!
404    * 
405    * @param seqs
406    *          DOCUMENT ME!
407    * @param width
408    *          DOCUMENT ME!
409    * @param index
410    *          DOCUMENT ME!
411    * 
412    * @return DOCUMENT ME!
413    */
414   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
415           int index)
416   {
417     ColourSchemeI cs = null;
418
419     switch (index)
420     {
421     case CLUSTAL:
422       cs = new ClustalxColourScheme(seqs, width);
423
424       break;
425
426     case BLOSUM:
427       cs = new Blosum62ColourScheme();
428
429       break;
430
431     case PID:
432       cs = new PIDColourScheme();
433
434       break;
435
436     case ZAPPO:
437       cs = new ZappoColourScheme();
438
439       break;
440
441     case TAYLOR:
442       cs = new TaylorColourScheme();
443       break;
444
445     case HYDROPHOBIC:
446       cs = new HydrophobicColourScheme();
447
448       break;
449
450     case HELIX:
451       cs = new HelixColourScheme();
452
453       break;
454
455     case STRAND:
456       cs = new StrandColourScheme();
457
458       break;
459
460     case TURN:
461       cs = new TurnColourScheme();
462
463       break;
464
465     case BURIED:
466       cs = new BuriedColourScheme();
467
468       break;
469
470     case NUCLEOTIDE:
471       cs = new NucleotideColourScheme();
472
473       break;
474
475     case PURINEPYRIMIDINE:
476       cs = new PurinePyrimidineColourScheme();
477
478       break;
479
480     // case COVARIATION:
481     // cs = new CovariationColourScheme(annotation);
482
483     // break;
484
485     case USER_DEFINED:
486       Color[] col = new Color[24];
487       for (int i = 0; i < 24; i++)
488       {
489         col[i] = Color.white;
490       }
491       cs = new UserColourScheme(col);
492       break;
493
494     default:
495       break;
496     }
497
498     return cs;
499   }
500
501   public static Color getAWTColorFromName(String name)
502   {
503     Color col = null;
504     name = name.toLowerCase();
505     if (name.equals("black"))
506     {
507       col = Color.black;
508     }
509     else if (name.equals("blue"))
510     {
511       col = Color.blue;
512     }
513     else if (name.equals("cyan"))
514     {
515       col = Color.cyan;
516     }
517     else if (name.equals("darkGray"))
518     {
519       col = Color.darkGray;
520     }
521     else if (name.equals("gray"))
522     {
523       col = Color.gray;
524     }
525     else if (name.equals("green"))
526     {
527       col = Color.green;
528     }
529     else if (name.equals("lightGray"))
530     {
531       col = Color.lightGray;
532     }
533     else if (name.equals("magenta"))
534     {
535       col = Color.magenta;
536     }
537     else if (name.equals("orange"))
538     {
539       col = Color.orange;
540     }
541     else if (name.equals("pink"))
542     {
543       col = Color.pink;
544     }
545     else if (name.equals("red"))
546     {
547       col = Color.red;
548     }
549     else if (name.equals("white"))
550     {
551       col = Color.white;
552     }
553     else if (name.equals("yellow"))
554     {
555       col = Color.yellow;
556     }
557
558     return col;
559   }
560 }