javadoc
[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    * Construct an instance of ColourSchemeI corresponding to the given colourscheme index
387    * 
388    * @param seqs
389    *          sequences to be coloured by colourscheme
390    * @param width
391    *          geometry of alignment
392    * @param index
393    *          colourscheme number 
394    * 
395    * @return null or an instance of the colourscheme configured to colour given sequence set
396    */
397   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
398           int index)
399   {
400     // TODO 3.0 2.8 refactor signature to take an alignmentI like container so colourschemes based on annotation can be initialised
401     ColourSchemeI cs = null;
402
403     switch (index)
404     {
405     case CLUSTAL:
406       cs = new ClustalxColourScheme(seqs, width);
407
408       break;
409
410     case BLOSUM:
411       cs = new Blosum62ColourScheme();
412
413       break;
414
415     case PID:
416       cs = new PIDColourScheme();
417
418       break;
419
420     case ZAPPO:
421       cs = new ZappoColourScheme();
422
423       break;
424
425     case TAYLOR:
426       cs = new TaylorColourScheme();
427       break;
428
429     case HYDROPHOBIC:
430       cs = new HydrophobicColourScheme();
431
432       break;
433
434     case HELIX:
435       cs = new HelixColourScheme();
436
437       break;
438
439     case STRAND:
440       cs = new StrandColourScheme();
441
442       break;
443
444     case TURN:
445       cs = new TurnColourScheme();
446
447       break;
448
449     case BURIED:
450       cs = new BuriedColourScheme();
451
452       break;
453
454     case NUCLEOTIDE:
455       cs = new NucleotideColourScheme();
456
457       break;
458
459     case USER_DEFINED:
460       Color[] col = new Color[24];
461       for (int i = 0; i < 24; i++)
462       {
463         col[i] = Color.white;
464       }
465       cs = new UserColourScheme(col);
466       break;
467
468     default:
469       break;
470     }
471
472     return cs;
473   }
474
475   public static Color getAWTColorFromName(String name)
476   {
477     Color col = null;
478     name = name.toLowerCase();
479     if (name.equals("black"))
480     {
481       col = Color.black;
482     }
483     else if (name.equals("blue"))
484     {
485       col = Color.blue;
486     }
487     else if (name.equals("cyan"))
488     {
489       col = Color.cyan;
490     }
491     else if (name.equals("darkGray"))
492     {
493       col = Color.darkGray;
494     }
495     else if (name.equals("gray"))
496     {
497       col = Color.gray;
498     }
499     else if (name.equals("green"))
500     {
501       col = Color.green;
502     }
503     else if (name.equals("lightGray"))
504     {
505       col = Color.lightGray;
506     }
507     else if (name.equals("magenta"))
508     {
509       col = Color.magenta;
510     }
511     else if (name.equals("orange"))
512     {
513       col = Color.orange;
514     }
515     else if (name.equals("pink"))
516     {
517       col = Color.pink;
518     }
519     else if (name.equals("red"))
520     {
521       col = Color.red;
522     }
523     else if (name.equals("white"))
524     {
525       col = Color.white;
526     }
527     else if (name.equals("yellow"))
528     {
529       col = Color.yellow;
530     }
531
532     return col;
533   }
534 }