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