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