merge from 2_4_Release branch
[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    * DOCUMENT ME!
315    * 
316    * @param seqs
317    *                DOCUMENT ME!
318    * @param width
319    *                DOCUMENT ME!
320    * @param name
321    *                DOCUMENT ME!
322    * 
323    * @return DOCUMENT ME!
324    */
325   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
326           String name)
327   {
328     int colindex = getColourIndexFromName(name);
329     if (colindex == UNDEFINED) // USER_DEFINED)
330     {
331       try
332       {
333         return new UserColourScheme(name);
334       } catch (Exception e)
335       {
336         // System.err.println("Ignoring unknown colourscheme name");
337       }
338     }
339     return getColour(seqs, width, getColourIndexFromName(name));
340   }
341
342   /**
343    * DOCUMENT ME!
344    * 
345    * @param seqs
346    *                DOCUMENT ME!
347    * @param width
348    *                DOCUMENT ME!
349    * @param index
350    *                DOCUMENT ME!
351    * 
352    * @return DOCUMENT ME!
353    */
354   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
355           int index)
356   {
357     ColourSchemeI cs = null;
358
359     switch (index)
360     {
361     case CLUSTAL:
362       cs = new ClustalxColourScheme(seqs, width);
363
364       break;
365
366     case BLOSUM:
367       cs = new Blosum62ColourScheme();
368
369       break;
370
371     case PID:
372       cs = new PIDColourScheme();
373
374       break;
375
376     case ZAPPO:
377       cs = new ZappoColourScheme();
378
379       break;
380
381     case TAYLOR:
382       cs = new TaylorColourScheme();
383       break;
384
385     case HYDROPHOBIC:
386       cs = new HydrophobicColourScheme();
387
388       break;
389
390     case HELIX:
391       cs = new HelixColourScheme();
392
393       break;
394
395     case STRAND:
396       cs = new StrandColourScheme();
397
398       break;
399
400     case TURN:
401       cs = new TurnColourScheme();
402
403       break;
404
405     case BURIED:
406       cs = new BuriedColourScheme();
407
408       break;
409
410     case NUCLEOTIDE:
411       cs = new NucleotideColourScheme();
412
413       break;
414
415     case USER_DEFINED:
416       Color[] col = new Color[24];
417       for (int i = 0; i < 24; i++)
418       {
419         col[i] = Color.white;
420       }
421       cs = new UserColourScheme(col);
422       break;
423
424     default:
425       break;
426     }
427
428     return cs;
429   }
430
431   public static Color getAWTColorFromName(String name)
432   {
433     Color col = null;
434     name = name.toLowerCase();
435     if (name.equals("black"))
436     {
437       col = Color.black;
438     }
439     else if (name.equals("blue"))
440     {
441       col = Color.blue;
442     }
443     else if (name.equals("cyan"))
444     {
445       col = Color.cyan;
446     }
447     else if (name.equals("darkGray"))
448     {
449       col = Color.darkGray;
450     }
451     else if (name.equals("gray"))
452     {
453       col = Color.gray;
454     }
455     else if (name.equals("green"))
456     {
457       col = Color.green;
458     }
459     else if (name.equals("lightGray"))
460     {
461       col = Color.lightGray;
462     }
463     else if (name.equals("magenta"))
464     {
465       col = Color.magenta;
466     }
467     else if (name.equals("orange"))
468     {
469       col = Color.orange;
470     }
471     else if (name.equals("pink"))
472     {
473       col = Color.pink;
474     }
475     else if (name.equals("red"))
476     {
477       col = Color.red;
478     }
479     else if (name.equals("white"))
480     {
481       col = Color.white;
482     }
483     else if (name.equals("yellow"))
484     {
485       col = Color.yellow;
486     }
487
488     return col;
489   }
490 }