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