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