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