JAL-952 fixed unresolved conflict breaking build
[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     else if (cs instanceof RNAHelicesColour)
256     {
257       index = RNAHELIX;
258     }
259     /*
260      * else if (cs instanceof CovariationColourScheme) { index = COVARIATION; }
261      */
262     else if (cs instanceof UserColourScheme)
263     {
264       if ((((UserColourScheme) cs).getName() != null)
265               && (((UserColourScheme) cs).getName().length() > 0))
266       {
267         return ((UserColourScheme) cs).getName();
268       }
269       // get default colourscheme name
270       index = USER_DEFINED;
271     }
272
273     return getColourName(index);
274   }
275
276   /**
277    * DOCUMENT ME!
278    * 
279    * @param index
280    *          DOCUMENT ME!
281    * 
282    * @return DOCUMENT ME!
283    */
284   public static String getColourName(int index)
285   {
286     String ret = null;
287
288     switch (index)
289     {
290     case CLUSTAL:
291       ret = "Clustal";
292
293       break;
294
295     case BLOSUM:
296       ret = "Blosum62";
297
298       break;
299
300     case PID:
301       ret = "% Identity";
302
303       break;
304
305     case ZAPPO:
306       ret = "Zappo";
307
308       break;
309
310     case TAYLOR:
311       ret = "Taylor";
312       break;
313
314     case HYDROPHOBIC:
315       ret = "Hydrophobic";
316
317       break;
318
319     case HELIX:
320       ret = "Helix Propensity";
321
322       break;
323
324     case STRAND:
325       ret = "Strand Propensity";
326
327       break;
328
329     case TURN:
330       ret = "Turn Propensity";
331
332       break;
333
334     case BURIED:
335       ret = "Buried Index";
336
337       break;
338
339     case NUCLEOTIDE:
340       ret = "Nucleotide";
341
342       break;
343
344     case PURINEPYRIMIDINE:
345       ret = "Purine/Pyrimidine";
346
347       break;
348
349     case TCOFFEE:
350       ret = "T-Coffee Scores";
351
352       break;
353 <<<<<<< HEAD
354       
355     case RNAINTERACTION:
356         ret = "RNA Interaction type";
357
358         break;
359 =======
360     case RNAHELIX:
361       ret = "RNA Helices";
362
363       break;
364 >>>>>>> JAL-952_rnahelix
365     /*
366      * case COVARIATION: ret = "Covariation";
367      * 
368      * break;
369      */
370     case USER_DEFINED:
371       ret = "User Defined";
372
373       break;
374
375     default:
376       ret = "None";
377
378       break;
379     }
380
381     return ret;
382   }
383
384   /**
385    * retrieve or create colourscheme associated with name
386    * 
387    * @param seqs
388    *          sequences to colour
389    * @param width
390    *          range of sequences to colour
391    * @param name
392    *          colourscheme name, applet colour parameter specification, or
393    *          string to parse as colour for new coloursheme
394    * @return Valid Colourscheme
395    */
396   public static ColourSchemeI getColour(AnnotatedCollectionI alignment,
397           String name)
398   {
399     int colindex = getColourIndexFromName(name);
400     if (colindex == UNDEFINED)
401     {
402       if (name.indexOf('=') == -1)
403       {
404         // try to build a colour from the string directly
405         try
406         {
407           return new UserColourScheme(name);
408         } catch (Exception e)
409         {
410           // System.err.println("Ignoring unknown colourscheme name");
411         }
412       }
413       else
414       {
415         // try to parse the string as a residue colourscheme
416         try
417         {
418           // fix the launchApp user defined coloursheme transfer bug
419           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
420                   "white");
421           ucs.parseAppletParameter(name);
422
423         } catch (Exception e)
424         {
425           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
426         }
427       }
428     }
429     return getColour(alignment, getColourIndexFromName(name));
430   }
431
432   /**
433    * Construct an instance of ColourSchemeI corresponding to the given
434    * colourscheme index
435    * 
436    * @param seqs
437    *          sequences to be coloured by colourscheme
438    * @param width
439    *          geometry of alignment
440    * @param index
441    *          colourscheme number
442    * 
443    * @return null or an instance of the colourscheme configured to colour given
444    *         sequence set
445    */
446   public static ColourSchemeI getColour(
447           jalview.datamodel.AnnotatedCollectionI coll, int index)
448   {
449     // TODO 3.0 2.8 refactor signature to take an alignmentI like container so
450     // colourschemes based on annotation can be initialised
451     ColourSchemeI cs = null;
452
453     switch (index)
454     {
455     case CLUSTAL:
456       cs = new ClustalxColourScheme(coll, null);
457
458       break;
459
460     case BLOSUM:
461       cs = new Blosum62ColourScheme();
462
463       break;
464
465     case PID:
466       cs = new PIDColourScheme();
467
468       break;
469
470     case ZAPPO:
471       cs = new ZappoColourScheme();
472
473       break;
474
475     case TAYLOR:
476       cs = new TaylorColourScheme();
477       break;
478
479     case HYDROPHOBIC:
480       cs = new HydrophobicColourScheme();
481
482       break;
483
484     case HELIX:
485       cs = new HelixColourScheme();
486
487       break;
488
489     case STRAND:
490       cs = new StrandColourScheme();
491
492       break;
493
494     case TURN:
495       cs = new TurnColourScheme();
496
497       break;
498
499     case BURIED:
500       cs = new BuriedColourScheme();
501
502       break;
503
504     case NUCLEOTIDE:
505       cs = new NucleotideColourScheme();
506
507       break;
508
509     case PURINEPYRIMIDINE:
510       cs = new PurinePyrimidineColourScheme();
511
512       break;
513
514     case TCOFFEE:
515       cs = new TCoffeeColourScheme(coll);
516       break;
517     
518     case RNAHELIX:
519       cs = new RNAHelicesColour(coll);
520       break;
521       
522       // case COVARIATION:
523       // cs = new CovariationColourScheme(annotation);
524     // break;
525
526     case USER_DEFINED:
527       Color[] col = new Color[24];
528       for (int i = 0; i < 24; i++)
529       {
530         col[i] = Color.white;
531       }
532       cs = new UserColourScheme(col);
533       break;
534
535     default:
536       break;
537     }
538
539     return cs;
540   }
541
542   public static Color getAWTColorFromName(String name)
543   {
544     Color col = null;
545     name = name.toLowerCase();
546     if (name.equals("black"))
547     {
548       col = Color.black;
549     }
550     else if (name.equals("blue"))
551     {
552       col = Color.blue;
553     }
554     else if (name.equals("cyan"))
555     {
556       col = Color.cyan;
557     }
558     else if (name.equals("darkGray"))
559     {
560       col = Color.darkGray;
561     }
562     else if (name.equals("gray"))
563     {
564       col = Color.gray;
565     }
566     else if (name.equals("green"))
567     {
568       col = Color.green;
569     }
570     else if (name.equals("lightGray"))
571     {
572       col = Color.lightGray;
573     }
574     else if (name.equals("magenta"))
575     {
576       col = Color.magenta;
577     }
578     else if (name.equals("orange"))
579     {
580       col = Color.orange;
581     }
582     else if (name.equals("pink"))
583     {
584       col = Color.pink;
585     }
586     else if (name.equals("red"))
587     {
588       col = Color.red;
589     }
590     else if (name.equals("white"))
591     {
592       col = Color.white;
593     }
594     else if (name.equals("yellow"))
595     {
596       col = Color.yellow;
597     }
598
599     return col;
600   }
601 }