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