3738f61bd07e02551571f09458ad1162045893cd
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, 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 java.awt.Color;
21
22 /**
23  * ColourSchemeProperty Binds names to hardwired colourschemes and tries to deal
24  * intelligently with mapping unknown names to user defined colourschemes (that
25  * exist or can be created from the string representation of the colourscheme
26  * name - either a hex RGB triplet or a named colour under java.awt.color ). The
27  * values of the colourscheme constants is important for callers of
28  * getColourName(int i), since it can be used to enumerate the set of built in
29  * colours. The FIRST_COLOUR and LAST_COLOUR symbols are provided for this.
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class ColourSchemeProperty
35 {
36   /** Undefined Colourscheme Index */
37   public static final int UNDEFINED = -1;
38
39   /** for schemes defined on the fly */
40   public static final int USER_DEFINED = 0;
41
42   /** No Colourscheme Index */
43   public static final int NONE = 1;
44
45   /** DOCUMENT ME!! */
46   public static final int CLUSTAL = 2;
47
48   /** DOCUMENT ME!! */
49   public static final int BLOSUM = 3;
50
51   /** DOCUMENT ME!! */
52   public static final int PID = 4;
53
54   /** DOCUMENT ME!! */
55   public static final int ZAPPO = 5;
56
57   /** DOCUMENT ME!! */
58   public static final int TAYLOR = 6;
59
60   /** DOCUMENT ME!! */
61   public static final int HYDROPHOBIC = 7;
62
63   /** DOCUMENT ME!! */
64   public static final int HELIX = 8;
65
66   /** DOCUMENT ME!! */
67   public static final int STRAND = 9;
68
69   /** DOCUMENT ME!! */
70   public static final int TURN = 10;
71
72   /** DOCUMENT ME!! */
73   public static final int BURIED = 11;
74
75   /** DOCUMENT ME!! */
76   public static final int NUCLEOTIDE = 12;
77
78   /**
79    * purine/pyrimidine
80    */
81   public static final int PURINEPYRIMIDINE = 13;
82
83   public static final int COVARIATION = 14;
84   
85   public static final int TCOFFEE = 15;
86   
87
88   /**
89    * index of first colourscheme (includes 'None')
90    */
91   public static final int FIRST_COLOUR = NONE;
92
93   public static final int LAST_COLOUR = NUCLEOTIDE;
94
95   /**
96    * DOCUMENT ME!
97    * 
98    * @param name
99    *          DOCUMENT ME!
100    * 
101    * @return DOCUMENT ME!
102    */
103   public static int getColourIndexFromName(String name)
104   {
105     int ret = UNDEFINED;
106
107     if (name.equalsIgnoreCase("Clustal"))
108     {
109       ret = CLUSTAL;
110     }
111     else if (name.equalsIgnoreCase("Blosum62"))
112     {
113       ret = BLOSUM;
114     }
115     else if (name.equalsIgnoreCase("% Identity"))
116     {
117       ret = PID;
118     }
119     else if (name.equalsIgnoreCase("Zappo"))
120     {
121       ret = ZAPPO;
122     }
123     else if (name.equalsIgnoreCase("Taylor"))
124     {
125       ret = TAYLOR;
126     }
127     else if (name.equalsIgnoreCase("Hydrophobic"))
128     {
129       ret = HYDROPHOBIC;
130     }
131     else if (name.equalsIgnoreCase("Helix Propensity"))
132     {
133       ret = HELIX;
134     }
135     else if (name.equalsIgnoreCase("Strand Propensity"))
136     {
137       ret = STRAND;
138     }
139     else if (name.equalsIgnoreCase("Turn Propensity"))
140     {
141       ret = TURN;
142     }
143     else if (name.equalsIgnoreCase("Buried Index"))
144     {
145       ret = BURIED;
146     }
147     else if (name.equalsIgnoreCase("Nucleotide"))
148     {
149       ret = NUCLEOTIDE;
150     }
151     else if (name.equalsIgnoreCase("T-Coffee scores"))
152     {
153       ret = TCOFFEE;
154     }
155
156     else if (name.equalsIgnoreCase("User Defined"))
157     {
158       ret = USER_DEFINED;
159     }
160     else if (name.equalsIgnoreCase("None"))
161     {
162       ret = NONE;
163     }
164
165     return ret;
166   }
167
168   /**
169    * DOCUMENT ME!
170    * 
171    * @param cs
172    *          DOCUMENT ME!
173    * 
174    * @return DOCUMENT ME!
175    */
176   public static String getColourName(ColourSchemeI cs)
177   {
178
179     int index = NONE;
180
181     if (cs instanceof ClustalxColourScheme)
182     {
183       index = CLUSTAL;
184     }
185     else if (cs instanceof Blosum62ColourScheme)
186     {
187       index = BLOSUM;
188     }
189     else if (cs instanceof PIDColourScheme)
190     {
191       index = PID;
192     }
193     else if (cs instanceof ZappoColourScheme)
194     {
195       index = ZAPPO;
196     }
197     else if (cs instanceof TaylorColourScheme)
198     {
199       index = TAYLOR;
200     }
201     else if (cs instanceof HydrophobicColourScheme)
202     {
203       index = HYDROPHOBIC;
204     }
205     else if (cs instanceof HelixColourScheme)
206     {
207       index = HELIX;
208     }
209     else if (cs instanceof StrandColourScheme)
210     {
211       index = STRAND;
212     }
213     else if (cs instanceof TurnColourScheme)
214     {
215       index = TURN;
216     }
217     else if (cs instanceof BuriedColourScheme)
218     {
219       index = BURIED;
220     }
221     else if (cs instanceof NucleotideColourScheme)
222     {
223       index = NUCLEOTIDE;
224     }
225     else if (cs instanceof UserColourScheme)
226     {
227       if ((((UserColourScheme) cs).getName() != null)
228               && (((UserColourScheme) cs).getName().length() > 0))
229       {
230         return ((UserColourScheme) cs).getName();
231       }
232       // get default colourscheme name
233       index = USER_DEFINED;
234     }
235
236     return getColourName(index);
237   }
238
239   /**
240    * DOCUMENT ME!
241    * 
242    * @param index
243    *          DOCUMENT ME!
244    * 
245    * @return DOCUMENT ME!
246    */
247   public static String getColourName(int index)
248   {
249     String ret = null;
250
251     switch (index)
252     {
253     case CLUSTAL:
254       ret = "Clustal";
255
256       break;
257
258     case BLOSUM:
259       ret = "Blosum62";
260
261       break;
262
263     case PID:
264       ret = "% Identity";
265
266       break;
267
268     case ZAPPO:
269       ret = "Zappo";
270
271       break;
272
273     case TAYLOR:
274       ret = "Taylor";
275       break;
276
277     case HYDROPHOBIC:
278       ret = "Hydrophobic";
279
280       break;
281
282     case HELIX:
283       ret = "Helix Propensity";
284
285       break;
286
287     case STRAND:
288       ret = "Strand Propensity";
289
290       break;
291
292     case TURN:
293       ret = "Turn Propensity";
294
295       break;
296
297     case BURIED:
298       ret = "Buried Index";
299
300       break;
301
302     case NUCLEOTIDE:
303       ret = "Nucleotide";
304
305       break;
306
307     case USER_DEFINED:
308       ret = "User Defined";
309
310       break;
311
312     default:
313       ret = "None";
314
315       break;
316     }
317
318     return ret;
319   }
320
321   /**
322    * DOCUMENT ME!
323    * 
324    * @param al
325    *          DOCUMENT ME!
326    * @param name
327    *          DOCUMENT ME!
328    * 
329    * @return DOCUMENT ME!
330    */
331   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
332           String name)
333   {
334     return getColour(al.getSequences(), al.getWidth(), name);
335   }
336
337   /**
338    * retrieve or create colourscheme associated with name
339    * 
340    * @param seqs
341    *          sequences to colour
342    * @param width
343    *          range of sequences to colour
344    * @param name
345    *          colourscheme name, applet colour parameter specification, or
346    *          string to parse as colour for new coloursheme
347    * @return Valid Colourscheme
348    */
349   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
350           String name)
351   {
352     int colindex = getColourIndexFromName(name);
353     if (colindex == UNDEFINED)
354     {
355       if (name.indexOf('=') == -1)
356       {
357         // try to build a colour from the string directly
358         try
359         {
360           return new UserColourScheme(name);
361         } catch (Exception e)
362         {
363           // System.err.println("Ignoring unknown colourscheme name");
364         }
365       }
366       else
367       {
368         // try to parse the string as a residue colourscheme
369         try
370         {
371           // fix the launchApp user defined coloursheme transfer bug
372           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
373                   "white");
374           ucs.parseAppletParameter(name);
375
376         } catch (Exception e)
377         {
378           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
379         }
380       }
381     }
382     return getColour(seqs, width, getColourIndexFromName(name));
383   }
384
385   /**
386    * DOCUMENT ME!
387    * 
388    * @param seqs
389    *          DOCUMENT ME!
390    * @param width
391    *          DOCUMENT ME!
392    * @param index
393    *          DOCUMENT ME!
394    * 
395    * @return DOCUMENT ME!
396    */
397   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
398           int index)
399   {
400     ColourSchemeI cs = null;
401
402     switch (index)
403     {
404     case CLUSTAL:
405       cs = new ClustalxColourScheme(seqs, width);
406
407       break;
408
409     case BLOSUM:
410       cs = new Blosum62ColourScheme();
411
412       break;
413
414     case PID:
415       cs = new PIDColourScheme();
416
417       break;
418
419     case ZAPPO:
420       cs = new ZappoColourScheme();
421
422       break;
423
424     case TAYLOR:
425       cs = new TaylorColourScheme();
426       break;
427
428     case HYDROPHOBIC:
429       cs = new HydrophobicColourScheme();
430
431       break;
432
433     case HELIX:
434       cs = new HelixColourScheme();
435
436       break;
437
438     case STRAND:
439       cs = new StrandColourScheme();
440
441       break;
442
443     case TURN:
444       cs = new TurnColourScheme();
445
446       break;
447
448     case BURIED:
449       cs = new BuriedColourScheme();
450
451       break;
452
453     case NUCLEOTIDE:
454       cs = new NucleotideColourScheme();
455
456       break;
457
458     case USER_DEFINED:
459       Color[] col = new Color[24];
460       for (int i = 0; i < 24; i++)
461       {
462         col[i] = Color.white;
463       }
464       cs = new UserColourScheme(col);
465       break;
466
467     default:
468       break;
469     }
470
471     return cs;
472   }
473
474   public static Color getAWTColorFromName(String name)
475   {
476     Color col = null;
477     name = name.toLowerCase();
478     if (name.equals("black"))
479     {
480       col = Color.black;
481     }
482     else if (name.equals("blue"))
483     {
484       col = Color.blue;
485     }
486     else if (name.equals("cyan"))
487     {
488       col = Color.cyan;
489     }
490     else if (name.equals("darkGray"))
491     {
492       col = Color.darkGray;
493     }
494     else if (name.equals("gray"))
495     {
496       col = Color.gray;
497     }
498     else if (name.equals("green"))
499     {
500       col = Color.green;
501     }
502     else if (name.equals("lightGray"))
503     {
504       col = Color.lightGray;
505     }
506     else if (name.equals("magenta"))
507     {
508       col = Color.magenta;
509     }
510     else if (name.equals("orange"))
511     {
512       col = Color.orange;
513     }
514     else if (name.equals("pink"))
515     {
516       col = Color.pink;
517     }
518     else if (name.equals("red"))
519     {
520       col = Color.red;
521     }
522     else if (name.equals("white"))
523     {
524       col = Color.white;
525     }
526     else if (name.equals("yellow"))
527     {
528       col = Color.yellow;
529     }
530
531     return col;
532   }
533 }