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