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