JAL-1114 - refactor methods handling Vectors and Hashtables to Lists and Maps, and...
[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 jalview.datamodel.AnnotatedCollectionI;
21
22 import java.awt.Color;
23
24 /**
25  * ColourSchemeProperty Binds names to hardwired colourschemes and tries to deal
26  * intelligently with mapping unknown names to user defined colourschemes (that
27  * exist or can be created from the string representation of the colourscheme
28  * name - either a hex RGB triplet or a named colour under java.awt.color ). The
29  * values of the colourscheme constants is important for callers of
30  * getColourName(int i), since it can be used to enumerate the set of built in
31  * colours. The FIRST_COLOUR and LAST_COLOUR symbols are provided for this.
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class ColourSchemeProperty
37 {
38   /** Undefined Colourscheme Index */
39   public static final int UNDEFINED = -1;
40
41   /** for schemes defined on the fly */
42   public static final int USER_DEFINED = 0;
43
44   /** No Colourscheme Index */
45   public static final int NONE = 1;
46
47   /** DOCUMENT ME!! */
48   public static final int CLUSTAL = 2;
49
50   /** DOCUMENT ME!! */
51   public static final int BLOSUM = 3;
52
53   /** DOCUMENT ME!! */
54   public static final int PID = 4;
55
56   /** DOCUMENT ME!! */
57   public static final int ZAPPO = 5;
58
59   /** DOCUMENT ME!! */
60   public static final int TAYLOR = 6;
61
62   /** DOCUMENT ME!! */
63   public static final int HYDROPHOBIC = 7;
64
65   /** DOCUMENT ME!! */
66   public static final int HELIX = 8;
67
68   /** DOCUMENT ME!! */
69   public static final int STRAND = 9;
70
71   /** DOCUMENT ME!! */
72   public static final int TURN = 10;
73
74   /** DOCUMENT ME!! */
75   public static final int BURIED = 11;
76
77   /** DOCUMENT ME!! */
78   public static final int NUCLEOTIDE = 12;
79
80   /**
81    * purine/pyrimidine
82    */
83   public static final int PURINEPYRIMIDINE = 13;
84
85   public static final int COVARIATION = 14;
86   
87   public static final int TCOFFEE = 15;
88   
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     /*
240      * else if (cs instanceof CovariationColourScheme) { index = COVARIATION; }
241      */
242     else if (cs instanceof UserColourScheme)
243     {
244       if ((((UserColourScheme) cs).getName() != null)
245               && (((UserColourScheme) cs).getName().length() > 0))
246       {
247         return ((UserColourScheme) cs).getName();
248       }
249       // get default colourscheme name
250       index = USER_DEFINED;
251     }
252
253     return getColourName(index);
254   }
255
256   /**
257    * DOCUMENT ME!
258    * 
259    * @param index
260    *          DOCUMENT ME!
261    * 
262    * @return DOCUMENT ME!
263    */
264   public static String getColourName(int index)
265   {
266     String ret = null;
267
268     switch (index)
269     {
270     case CLUSTAL:
271       ret = "Clustal";
272
273       break;
274
275     case BLOSUM:
276       ret = "Blosum62";
277
278       break;
279
280     case PID:
281       ret = "% Identity";
282
283       break;
284
285     case ZAPPO:
286       ret = "Zappo";
287
288       break;
289
290     case TAYLOR:
291       ret = "Taylor";
292       break;
293
294     case HYDROPHOBIC:
295       ret = "Hydrophobic";
296
297       break;
298
299     case HELIX:
300       ret = "Helix Propensity";
301
302       break;
303
304     case STRAND:
305       ret = "Strand Propensity";
306
307       break;
308
309     case TURN:
310       ret = "Turn Propensity";
311
312       break;
313
314     case BURIED:
315       ret = "Buried Index";
316
317       break;
318
319     case NUCLEOTIDE:
320       ret = "Nucleotide";
321
322       break;
323
324     case PURINEPYRIMIDINE:
325       ret = "Purine/Pyrimidine";
326
327       break;
328
329     /*
330      * case COVARIATION: ret = "Covariation";
331      * 
332      * break;
333      */
334     case USER_DEFINED:
335       ret = "User Defined";
336
337       break;
338
339     default:
340       ret = "None";
341
342       break;
343     }
344
345     return ret;
346   }
347   /**
348    * retrieve or create colourscheme associated with name
349    *
350    * @param seqs
351    *          sequences to colour
352    * @param width
353    *          range of sequences to colour
354    * @param name
355    *          colourscheme name, applet colour parameter specification, or
356    *          string to parse as colour for new coloursheme
357    * @return Valid Colourscheme
358    */
359   public static ColourSchemeI getColour(AnnotatedCollectionI alignment,
360           String name)
361   {
362     int colindex = getColourIndexFromName(name);
363     if (colindex == UNDEFINED)
364     {
365       if (name.indexOf('=') == -1)
366       {
367         // try to build a colour from the string directly
368         try
369         {
370           return new UserColourScheme(name);
371         } catch (Exception e)
372         {
373           // System.err.println("Ignoring unknown colourscheme name");
374         }
375       }
376       else
377       {
378         // try to parse the string as a residue colourscheme
379         try
380         {
381           // fix the launchApp user defined coloursheme transfer bug
382           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
383                   "white");
384           ucs.parseAppletParameter(name);
385
386         } catch (Exception e)
387         {
388           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
389         }
390       }
391     }
392     return getColour(alignment, getColourIndexFromName(name));
393   }
394
395   /**
396    * Construct an instance of ColourSchemeI corresponding to the given colourscheme index
397    * 
398    * @param seqs
399    *          sequences to be coloured by colourscheme
400    * @param width
401    *          geometry of alignment
402    * @param index
403    *          colourscheme number 
404    * 
405    * @return null or an instance of the colourscheme configured to colour given sequence set
406    */
407   public static ColourSchemeI getColour(jalview.datamodel.AnnotatedCollectionI coll, int index)
408   {
409     // TODO 3.0 2.8 refactor signature to take an alignmentI like container so colourschemes based on annotation can be initialised
410     ColourSchemeI cs = null;
411
412     switch (index)
413     {
414     case CLUSTAL:
415       cs = new ClustalxColourScheme(coll, null);
416
417       break;
418
419     case BLOSUM:
420       cs = new Blosum62ColourScheme();
421
422       break;
423
424     case PID:
425       cs = new PIDColourScheme();
426
427       break;
428
429     case ZAPPO:
430       cs = new ZappoColourScheme();
431
432       break;
433
434     case TAYLOR:
435       cs = new TaylorColourScheme();
436       break;
437
438     case HYDROPHOBIC:
439       cs = new HydrophobicColourScheme();
440
441       break;
442
443     case HELIX:
444       cs = new HelixColourScheme();
445
446       break;
447
448     case STRAND:
449       cs = new StrandColourScheme();
450
451       break;
452
453     case TURN:
454       cs = new TurnColourScheme();
455
456       break;
457
458     case BURIED:
459       cs = new BuriedColourScheme();
460
461       break;
462
463     case NUCLEOTIDE:
464       cs = new NucleotideColourScheme();
465
466       break;
467
468     case PURINEPYRIMIDINE:
469       cs = new PurinePyrimidineColourScheme();
470
471       break;
472
473     // case COVARIATION:
474     // cs = new CovariationColourScheme(annotation);
475
476     // break;
477
478     case USER_DEFINED:
479       Color[] col = new Color[24];
480       for (int i = 0; i < 24; i++)
481       {
482         col[i] = Color.white;
483       }
484       cs = new UserColourScheme(col);
485       break;
486
487     default:
488       break;
489     }
490
491     return cs;
492   }
493
494   public static Color getAWTColorFromName(String name)
495   {
496     Color col = null;
497     name = name.toLowerCase();
498     if (name.equals("black"))
499     {
500       col = Color.black;
501     }
502     else if (name.equals("blue"))
503     {
504       col = Color.blue;
505     }
506     else if (name.equals("cyan"))
507     {
508       col = Color.cyan;
509     }
510     else if (name.equals("darkGray"))
511     {
512       col = Color.darkGray;
513     }
514     else if (name.equals("gray"))
515     {
516       col = Color.gray;
517     }
518     else if (name.equals("green"))
519     {
520       col = Color.green;
521     }
522     else if (name.equals("lightGray"))
523     {
524       col = Color.lightGray;
525     }
526     else if (name.equals("magenta"))
527     {
528       col = Color.magenta;
529     }
530     else if (name.equals("orange"))
531     {
532       col = Color.orange;
533     }
534     else if (name.equals("pink"))
535     {
536       col = Color.pink;
537     }
538     else if (name.equals("red"))
539     {
540       col = Color.red;
541     }
542     else if (name.equals("white"))
543     {
544       col = Color.white;
545     }
546     else if (name.equals("yellow"))
547     {
548       col = Color.yellow;
549     }
550
551     return col;
552   }
553 }