apply version 2.7 copyright
[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.*;
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   /**
86    * index of first colourscheme (includes 'None')
87    */
88   public static final int FIRST_COLOUR = NONE;
89
90   public static final int LAST_COLOUR = NUCLEOTIDE;
91
92   /**
93    * DOCUMENT ME!
94    * 
95    * @param name
96    *          DOCUMENT ME!
97    * 
98    * @return DOCUMENT ME!
99    */
100   public static int getColourIndexFromName(String name)
101   {
102     int ret = UNDEFINED;
103
104     if (name.equalsIgnoreCase("Clustal"))
105     {
106       ret = CLUSTAL;
107     }
108     else if (name.equalsIgnoreCase("Blosum62"))
109     {
110       ret = BLOSUM;
111     }
112     else if (name.equalsIgnoreCase("% Identity"))
113     {
114       ret = PID;
115     }
116     else if (name.equalsIgnoreCase("Zappo"))
117     {
118       ret = ZAPPO;
119     }
120     else if (name.equalsIgnoreCase("Taylor"))
121     {
122       ret = TAYLOR;
123     }
124     else if (name.equalsIgnoreCase("Hydrophobic"))
125     {
126       ret = HYDROPHOBIC;
127     }
128     else if (name.equalsIgnoreCase("Helix Propensity"))
129     {
130       ret = HELIX;
131     }
132     else if (name.equalsIgnoreCase("Strand Propensity"))
133     {
134       ret = STRAND;
135     }
136     else if (name.equalsIgnoreCase("Turn Propensity"))
137     {
138       ret = TURN;
139     }
140     else if (name.equalsIgnoreCase("Buried Index"))
141     {
142       ret = BURIED;
143     }
144     else if (name.equalsIgnoreCase("Nucleotide"))
145     {
146       ret = NUCLEOTIDE;
147     }
148     else if (name.equalsIgnoreCase("User Defined"))
149     {
150       ret = USER_DEFINED;
151     }
152     else if (name.equalsIgnoreCase("None"))
153     {
154       ret = NONE;
155     }
156
157     return ret;
158   }
159
160   /**
161    * DOCUMENT ME!
162    * 
163    * @param cs
164    *          DOCUMENT ME!
165    * 
166    * @return DOCUMENT ME!
167    */
168   public static String getColourName(ColourSchemeI cs)
169   {
170
171     int index = NONE;
172
173     if (cs instanceof ClustalxColourScheme)
174     {
175       index = CLUSTAL;
176     }
177     else if (cs instanceof Blosum62ColourScheme)
178     {
179       index = BLOSUM;
180     }
181     else if (cs instanceof PIDColourScheme)
182     {
183       index = PID;
184     }
185     else if (cs instanceof ZappoColourScheme)
186     {
187       index = ZAPPO;
188     }
189     else if (cs instanceof TaylorColourScheme)
190     {
191       index = TAYLOR;
192     }
193     else if (cs instanceof HydrophobicColourScheme)
194     {
195       index = HYDROPHOBIC;
196     }
197     else if (cs instanceof HelixColourScheme)
198     {
199       index = HELIX;
200     }
201     else if (cs instanceof StrandColourScheme)
202     {
203       index = STRAND;
204     }
205     else if (cs instanceof TurnColourScheme)
206     {
207       index = TURN;
208     }
209     else if (cs instanceof BuriedColourScheme)
210     {
211       index = BURIED;
212     }
213     else if (cs instanceof NucleotideColourScheme)
214     {
215       index = NUCLEOTIDE;
216     }
217     else if (cs instanceof UserColourScheme)
218     {
219       if ((((UserColourScheme) cs).getName() != null)
220               && (((UserColourScheme) cs).getName().length() > 0))
221       {
222         return ((UserColourScheme) cs).getName();
223       }
224       // get default colourscheme name
225       index = USER_DEFINED;
226     }
227
228     return getColourName(index);
229   }
230
231   /**
232    * DOCUMENT ME!
233    * 
234    * @param index
235    *          DOCUMENT ME!
236    * 
237    * @return DOCUMENT ME!
238    */
239   public static String getColourName(int index)
240   {
241     String ret = null;
242
243     switch (index)
244     {
245     case CLUSTAL:
246       ret = "Clustal";
247
248       break;
249
250     case BLOSUM:
251       ret = "Blosum62";
252
253       break;
254
255     case PID:
256       ret = "% Identity";
257
258       break;
259
260     case ZAPPO:
261       ret = "Zappo";
262
263       break;
264
265     case TAYLOR:
266       ret = "Taylor";
267       break;
268
269     case HYDROPHOBIC:
270       ret = "Hydrophobic";
271
272       break;
273
274     case HELIX:
275       ret = "Helix Propensity";
276
277       break;
278
279     case STRAND:
280       ret = "Strand Propensity";
281
282       break;
283
284     case TURN:
285       ret = "Turn Propensity";
286
287       break;
288
289     case BURIED:
290       ret = "Buried Index";
291
292       break;
293
294     case NUCLEOTIDE:
295       ret = "Nucleotide";
296
297       break;
298
299     case USER_DEFINED:
300       ret = "User Defined";
301
302       break;
303
304     default:
305       ret = "None";
306
307       break;
308     }
309
310     return ret;
311   }
312
313   /**
314    * DOCUMENT ME!
315    * 
316    * @param al
317    *          DOCUMENT ME!
318    * @param name
319    *          DOCUMENT ME!
320    * 
321    * @return DOCUMENT ME!
322    */
323   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
324           String name)
325   {
326     return getColour(al.getSequences(), al.getWidth(), name);
327   }
328
329   /**
330    * retrieve or create colourscheme associated with name
331    * 
332    * @param seqs
333    *          sequences to colour
334    * @param width
335    *          range of sequences to colour
336    * @param name
337    *          colourscheme name, applet colour parameter specification, or
338    *          string to parse as colour for new coloursheme
339    * @return Valid Colourscheme
340    */
341   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
342           String name)
343   {
344     int colindex = getColourIndexFromName(name);
345     if (colindex == UNDEFINED)
346     {
347       if (name.indexOf('=') == -1)
348       {
349         // try to build a colour from the string directly
350         try
351         {
352           return new UserColourScheme(name);
353         } catch (Exception e)
354         {
355           // System.err.println("Ignoring unknown colourscheme name");
356         }
357       }
358       else
359       {
360         // try to parse the string as a residue colourscheme
361         try
362         {
363           // fix the launchApp user defined coloursheme transfer bug
364           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
365                   "white");
366           ucs.parseAppletParameter(name);
367
368         } catch (Exception e)
369         {
370           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
371         }
372       }
373     }
374     return getColour(seqs, width, getColourIndexFromName(name));
375   }
376
377   /**
378    * DOCUMENT ME!
379    * 
380    * @param seqs
381    *          DOCUMENT ME!
382    * @param width
383    *          DOCUMENT ME!
384    * @param index
385    *          DOCUMENT ME!
386    * 
387    * @return DOCUMENT ME!
388    */
389   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
390           int index)
391   {
392     ColourSchemeI cs = null;
393
394     switch (index)
395     {
396     case CLUSTAL:
397       cs = new ClustalxColourScheme(seqs, width);
398
399       break;
400
401     case BLOSUM:
402       cs = new Blosum62ColourScheme();
403
404       break;
405
406     case PID:
407       cs = new PIDColourScheme();
408
409       break;
410
411     case ZAPPO:
412       cs = new ZappoColourScheme();
413
414       break;
415
416     case TAYLOR:
417       cs = new TaylorColourScheme();
418       break;
419
420     case HYDROPHOBIC:
421       cs = new HydrophobicColourScheme();
422
423       break;
424
425     case HELIX:
426       cs = new HelixColourScheme();
427
428       break;
429
430     case STRAND:
431       cs = new StrandColourScheme();
432
433       break;
434
435     case TURN:
436       cs = new TurnColourScheme();
437
438       break;
439
440     case BURIED:
441       cs = new BuriedColourScheme();
442
443       break;
444
445     case NUCLEOTIDE:
446       cs = new NucleotideColourScheme();
447
448       break;
449
450     case USER_DEFINED:
451       Color[] col = new Color[24];
452       for (int i = 0; i < 24; i++)
453       {
454         col[i] = Color.white;
455       }
456       cs = new UserColourScheme(col);
457       break;
458
459     default:
460       break;
461     }
462
463     return cs;
464   }
465
466   public static Color getAWTColorFromName(String name)
467   {
468     Color col = null;
469     name = name.toLowerCase();
470     if (name.equals("black"))
471     {
472       col = Color.black;
473     }
474     else if (name.equals("blue"))
475     {
476       col = Color.blue;
477     }
478     else if (name.equals("cyan"))
479     {
480       col = Color.cyan;
481     }
482     else if (name.equals("darkGray"))
483     {
484       col = Color.darkGray;
485     }
486     else if (name.equals("gray"))
487     {
488       col = Color.gray;
489     }
490     else if (name.equals("green"))
491     {
492       col = Color.green;
493     }
494     else if (name.equals("lightGray"))
495     {
496       col = Color.lightGray;
497     }
498     else if (name.equals("magenta"))
499     {
500       col = Color.magenta;
501     }
502     else if (name.equals("orange"))
503     {
504       col = Color.orange;
505     }
506     else if (name.equals("pink"))
507     {
508       col = Color.pink;
509     }
510     else if (name.equals("red"))
511     {
512       col = Color.red;
513     }
514     else if (name.equals("white"))
515     {
516       col = Color.white;
517     }
518     else if (name.equals("yellow"))
519     {
520       col = Color.yellow;
521     }
522
523     return col;
524   }
525 }