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