Conservation colour scheme is no more
[jalview.git] / src / jalview / appletgui / AlignViewport.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 \r
26 import jalview.analysis.*;\r
27 import jalview.bin.*;\r
28 import jalview.datamodel.*;\r
29 import jalview.schemes.*;\r
30 \r
31 public class AlignViewport\r
32 {\r
33   int startRes;\r
34   int endRes;\r
35 \r
36   int startSeq;\r
37   int endSeq;\r
38 \r
39   boolean showFullId = true;\r
40   boolean showText = true;\r
41   boolean showColourText = false;\r
42   boolean showBoxes = true;\r
43   boolean wrapAlignment = false;\r
44   boolean renderGaps = true;\r
45   boolean showSequenceFeatures = false;\r
46   boolean showAnnotation = true;\r
47   boolean showConservation = true;\r
48   boolean showQuality = true;\r
49   boolean showConsensus = true;\r
50 \r
51   boolean colourAppliesToAllGroups = true;\r
52   ColourSchemeI globalColourScheme = null;\r
53   boolean conservationColourSelected = false;\r
54   boolean abovePIDThreshold = false;\r
55 \r
56   SequenceGroup selectionGroup = new SequenceGroup();\r
57 \r
58   int charHeight;\r
59   int charWidth;\r
60   int chunkWidth;\r
61   int chunkHeight;\r
62 \r
63   Font font = new Font("SansSerif", Font.PLAIN, 10);\r
64   AlignmentI alignment;\r
65 \r
66   ColumnSelection colSel = new ColumnSelection();\r
67 \r
68   int threshold;\r
69   int increment;\r
70 \r
71   NJTree currentTree = null;\r
72 \r
73   boolean scaleAboveWrapped = true;\r
74   boolean scaleLeftWrapped = true;\r
75   boolean scaleRightWrapped = true;\r
76 \r
77 \r
78   boolean ignoreGapsInConsensusCalculation = false;\r
79 \r
80   public AlignViewport(AlignmentI al, JalviewLite applet)\r
81   {\r
82     setAlignment(al);\r
83     this.startRes = 0;\r
84     this.endRes = al.getWidth() - 1;\r
85     this.startSeq = 0;\r
86     this.endSeq = al.getHeight() - 1;\r
87     setFont(font);\r
88 \r
89     if (applet != null)\r
90     {\r
91       String param = applet.getParameter("showFullId");\r
92       if (param != null)\r
93       {\r
94         showFullId = Boolean.valueOf(param).booleanValue();\r
95       }\r
96 \r
97       param = applet.getParameter("showAnnotation");\r
98       if (param != null)\r
99       {\r
100         showAnnotation = Boolean.valueOf(param).booleanValue();\r
101       }\r
102 \r
103       param = applet.getParameter("showConservation");\r
104       if (param != null)\r
105       {\r
106         showConservation = Boolean.valueOf(param).booleanValue();\r
107       }\r
108 \r
109       param = applet.getParameter("showQuality");\r
110       if (param != null)\r
111       {\r
112         showQuality = Boolean.valueOf(param).booleanValue();\r
113       }\r
114 \r
115       param = applet.getParameter("showConsensus");\r
116       if (param != null)\r
117       {\r
118         showConsensus = Boolean.valueOf(param).booleanValue();\r
119       }\r
120     }\r
121     // We must set conservation and consensus before setting colour,\r
122     // as Blosum and Clustal require this to be done\r
123     updateConservation();\r
124     updateConsensus();\r
125 \r
126     if (applet != null && applet.getParameter("defaultColour") != null)\r
127     {\r
128       globalColourScheme = ColourSchemeProperty.getColour(alignment,\r
129           applet.getParameter("defaultColour"));\r
130       if (globalColourScheme != null)\r
131       {\r
132         globalColourScheme.setConsensus(vconsensus);\r
133       }\r
134     }\r
135   }\r
136 \r
137   public void showSequenceFeatures(boolean b)\r
138   {\r
139     showSequenceFeatures = b;\r
140   }\r
141 \r
142   public Vector vconsensus;\r
143   AlignmentAnnotation consensus;\r
144   AlignmentAnnotation conservation;\r
145   AlignmentAnnotation quality;\r
146 \r
147   public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!\r
148 \r
149   public void updateConservation()\r
150   {\r
151     Conservation cons = new jalview.analysis.Conservation("All",\r
152         jalview.schemes.ResidueProperties.propHash, 3,\r
153         alignment.getSequences(), 0,\r
154         alignment.getWidth() - 1);\r
155     cons.calculate();\r
156     cons.verdict(false, ConsPercGaps);\r
157     cons.findQuality();\r
158     int alWidth = alignment.getWidth();\r
159     Annotation[] annotations = new Annotation[alWidth];\r
160     Annotation[] qannotations = new Annotation[alWidth];\r
161     String sequence = cons.getConsSequence().getSequence();\r
162     float minR, minG, minB, maxR, maxG, maxB;\r
163     minR = 0.3f;\r
164     minG = 0.0f;\r
165     minB = 0f;\r
166     maxR = 1.0f - minR;\r
167     maxG = 0.9f - minG;\r
168     maxB = 0f - minB; // scalable range for colouring both Conservation and Quality\r
169     float min = 0f;\r
170     float max = 11f;\r
171     float qmin = cons.qualityRange[0].floatValue();\r
172     float qmax = cons.qualityRange[1].floatValue();\r
173 \r
174     for (int i = 0; i < alWidth; i++)\r
175     {\r
176       float value = 0;\r
177       try\r
178       {\r
179         value = Integer.parseInt(sequence.charAt(i) + "");\r
180       }\r
181       catch (Exception ex)\r
182       {\r
183         if (sequence.charAt(i) == '*')\r
184         {\r
185           value = 11;\r
186         }\r
187         if (sequence.charAt(i) == '+')\r
188         {\r
189           value = 10;\r
190         }\r
191       }\r
192       float vprop = value - min;\r
193       vprop /= max;\r
194 \r
195       annotations[i] = new Annotation(sequence.charAt(i) + "",\r
196                                       "", ' ', value,\r
197                                       new Color(minR + maxR * vprop,\r
198                                                 minG + maxG * vprop,\r
199                                                 minB + maxB * vprop));\r
200       // Quality calc\r
201       value = ( (Double) cons.quality.elementAt(i)).floatValue();\r
202       vprop = value - qmin;\r
203       vprop /= qmax;\r
204       qannotations[i] = new Annotation(" ",\r
205                                        String.valueOf(value), ' ', value,\r
206                                        new\r
207                                        Color(minR + maxR * vprop,\r
208                                              minG + maxG * vprop,\r
209                                              minB + maxB * vprop));\r
210     }\r
211 \r
212     if (conservation == null)\r
213     {\r
214       conservation = new AlignmentAnnotation("Conservation",\r
215                                              "Conservation of total alignment less than " +\r
216                                              ConsPercGaps + "% gaps",\r
217                                              annotations,\r
218                                              0f, // cons.qualityRange[0].floatValue(),\r
219                                              11f, // cons.qualityRange[1].floatValue()\r
220                                              1);\r
221       if (showConservation)\r
222       {\r
223         alignment.addAnnotation(conservation);\r
224       }\r
225       quality = new AlignmentAnnotation("Quality",\r
226                                         "Alignment Quality based on Blosum62 scores",\r
227                                         qannotations,\r
228                                         cons.qualityRange[0].floatValue(),\r
229                                         cons.qualityRange[1].floatValue(),\r
230                                         1);\r
231       if (showQuality)\r
232       {\r
233         alignment.addAnnotation(quality);\r
234       }\r
235     }\r
236     else\r
237     {\r
238       conservation.annotations = annotations;\r
239       quality.annotations = qannotations;\r
240       quality.graphMax = cons.qualityRange[1].floatValue();\r
241     }\r
242 \r
243   }\r
244 \r
245   public void updateConsensus()\r
246   {\r
247     Annotation[] annotations = new Annotation[alignment.getWidth()];\r
248 \r
249     // this routine prevents vconsensus becoming a new object each time\r
250     // consenus is calculated. Important for speed of Blosum62\r
251     // and PID colouring of alignment\r
252     if (vconsensus == null)\r
253     {\r
254       vconsensus = alignment.getAAFrequency();\r
255     }\r
256     else\r
257     {\r
258       Vector temp = alignment.getAAFrequency();\r
259       vconsensus.removeAllElements();\r
260       Enumeration e = temp.elements();\r
261       while (e.hasMoreElements())\r
262       {\r
263         vconsensus.addElement(e.nextElement());\r
264       }\r
265     }\r
266     Hashtable hash = null;\r
267     for (int i = 0; i < alignment.getWidth(); i++)\r
268     {\r
269       hash = (Hashtable) vconsensus.elementAt(i);\r
270       float value = 0;\r
271       if(ignoreGapsInConsensusCalculation)\r
272         value = ((Float)hash.get("pid_nogaps")).floatValue();\r
273       else\r
274         value = ((Float)hash.get("pid_gaps")).floatValue();\r
275 \r
276       String maxRes = hash.get("maxResidue").toString();\r
277       String mouseOver = hash.get("maxResidue") + " ";\r
278       if (maxRes.length() > 1)\r
279       {\r
280         mouseOver = "[" + maxRes + "] ";\r
281         maxRes = "+";\r
282       }\r
283 \r
284 \r
285       mouseOver += (int) value + "%";\r
286       annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
287 \r
288     }\r
289 \r
290     if (consensus == null)\r
291     {\r
292       consensus = new AlignmentAnnotation("Consensus",\r
293                                           "PID", annotations, 0f, 100f, 1);\r
294       if (showConsensus)\r
295       {\r
296         alignment.addAnnotation(consensus);\r
297       }\r
298     }\r
299     else\r
300     {\r
301       consensus.annotations = annotations;\r
302     }\r
303 \r
304   }\r
305 \r
306   public SequenceGroup getSelectionGroup()\r
307   {\r
308     return selectionGroup;\r
309   }\r
310 \r
311   public void setSelectionGroup(SequenceGroup sg)\r
312   {\r
313     selectionGroup = sg;\r
314   }\r
315 \r
316   public boolean getConservationSelected()\r
317   {\r
318     return conservationColourSelected;\r
319   }\r
320 \r
321   public void setConservationSelected(boolean b)\r
322   {\r
323     conservationColourSelected = b;\r
324   }\r
325 \r
326   public boolean getAbovePIDThreshold()\r
327   {\r
328     return abovePIDThreshold;\r
329   }\r
330 \r
331   public void setAbovePIDThreshold(boolean b)\r
332   {\r
333     abovePIDThreshold = b;\r
334   }\r
335 \r
336   public int getStartRes()\r
337   {\r
338     return startRes;\r
339   }\r
340 \r
341   public int getEndRes()\r
342   {\r
343     return endRes;\r
344   }\r
345 \r
346   public int getStartSeq()\r
347   {\r
348     return startSeq;\r
349   }\r
350 \r
351   public void setGlobalColourScheme(ColourSchemeI cs)\r
352   {\r
353     globalColourScheme = cs;\r
354   }\r
355 \r
356   public ColourSchemeI getGlobalColourScheme()\r
357   {\r
358     return globalColourScheme;\r
359   }\r
360 \r
361   public void setStartRes(int res)\r
362   {\r
363     this.startRes = res;\r
364   }\r
365 \r
366   public void setStartSeq(int seq)\r
367   {\r
368     this.startSeq = seq;\r
369   }\r
370 \r
371   public void setEndRes(int res)\r
372   {\r
373     if (res > alignment.getWidth() - 1)\r
374     {\r
375       // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
376       res = alignment.getWidth() - 1;\r
377     }\r
378     if (res < 0)\r
379     {\r
380       res = 0;\r
381     }\r
382     this.endRes = res;\r
383   }\r
384 \r
385   public void setEndSeq(int seq)\r
386   {\r
387     if (seq > alignment.getHeight())\r
388     {\r
389       seq = alignment.getHeight();\r
390     }\r
391     if (seq < 0)\r
392     {\r
393       seq = 0;\r
394     }\r
395     this.endSeq = seq;\r
396   }\r
397 \r
398   public int getEndSeq()\r
399   {\r
400     return endSeq;\r
401   }\r
402 \r
403   public void setFont(Font f)\r
404   {\r
405     font = f;\r
406     java.awt.Frame temp = new java.awt.Frame();\r
407     temp.addNotify();\r
408     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
409     setCharHeight(fm.getHeight());\r
410     setCharWidth(fm.charWidth('M'));\r
411   }\r
412 \r
413   public Font getFont()\r
414   {\r
415     return font;\r
416   }\r
417 \r
418   public void setCharWidth(int w)\r
419   {\r
420     this.charWidth = w;\r
421   }\r
422 \r
423   public int getCharWidth()\r
424   {\r
425     return charWidth;\r
426   }\r
427 \r
428   public void setCharHeight(int h)\r
429   {\r
430     this.charHeight = h;\r
431   }\r
432 \r
433   public int getCharHeight()\r
434   {\r
435     return charHeight;\r
436   }\r
437 \r
438   public void setChunkWidth(int w)\r
439   {\r
440     this.chunkWidth = w;\r
441   }\r
442 \r
443   public int getChunkWidth()\r
444   {\r
445     return chunkWidth;\r
446   }\r
447 \r
448   public void setChunkHeight(int h)\r
449   {\r
450     this.chunkHeight = h;\r
451   }\r
452 \r
453   public int getChunkHeight()\r
454   {\r
455     return chunkHeight;\r
456   }\r
457 \r
458   public AlignmentI getAlignment()\r
459   {\r
460     return alignment;\r
461   }\r
462 \r
463   public void setAlignment(AlignmentI align)\r
464   {\r
465     this.alignment = align;\r
466   }\r
467 \r
468   public void setWrapAlignment(boolean state)\r
469   {\r
470     wrapAlignment = state;\r
471   }\r
472 \r
473   public void setShowText(boolean state)\r
474   {\r
475     showText = state;\r
476   }\r
477 \r
478   public void setRenderGaps(boolean state)\r
479   {\r
480     renderGaps = state;\r
481   }\r
482 \r
483   public boolean getColourText()\r
484   {\r
485     return showColourText;\r
486   }\r
487 \r
488   public void setColourText(boolean state)\r
489   {\r
490     showColourText = state;\r
491   }\r
492 \r
493   public void setShowBoxes(boolean state)\r
494   {\r
495     showBoxes = state;\r
496   }\r
497 \r
498   public boolean getWrapAlignment()\r
499   {\r
500     return wrapAlignment;\r
501   }\r
502 \r
503   public boolean getShowText()\r
504   {\r
505     return showText;\r
506   }\r
507 \r
508   public boolean getShowBoxes()\r
509   {\r
510     return showBoxes;\r
511   }\r
512 \r
513   public char getGapCharacter()\r
514   {\r
515     return getAlignment().getGapCharacter();\r
516   }\r
517 \r
518   public void setGapCharacter(char gap)\r
519   {\r
520     if (getAlignment() != null)\r
521     {\r
522       getAlignment().setGapCharacter(gap);\r
523     }\r
524   }\r
525 \r
526   public void setThreshold(int thresh)\r
527   {\r
528     threshold = thresh;\r
529   }\r
530 \r
531   public int getThreshold()\r
532   {\r
533     return threshold;\r
534   }\r
535 \r
536   public void setIncrement(int inc)\r
537   {\r
538     increment = inc;\r
539   }\r
540 \r
541   public int getIncrement()\r
542   {\r
543     return increment;\r
544   }\r
545 \r
546   public int getIndex(int y)\r
547   {\r
548     int y1 = 0;\r
549     int starty = getStartSeq();\r
550     int endy = getEndSeq();\r
551 \r
552     for (int i = starty; i <= endy; i++)\r
553     {\r
554       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null)\r
555       {\r
556         int y2 = y1 + getCharHeight();\r
557 \r
558         if (y >= y1 && y <= y2)\r
559         {\r
560           return i;\r
561         }\r
562         y1 = y2;\r
563       }\r
564       else\r
565       {\r
566         return -1;\r
567       }\r
568     }\r
569     return -1;\r
570   }\r
571 \r
572   public ColumnSelection getColumnSelection()\r
573   {\r
574     return colSel;\r
575   }\r
576 \r
577   public void resetSeqLimits(int height)\r
578   {\r
579     setEndSeq(height / getCharHeight());\r
580   }\r
581 \r
582   public void setCurrentTree(NJTree tree)\r
583   {\r
584     currentTree = tree;\r
585   }\r
586 \r
587   public NJTree getCurrentTree()\r
588   {\r
589     return currentTree;\r
590   }\r
591 \r
592   public void setColourAppliesToAllGroups(boolean b)\r
593   {\r
594     colourAppliesToAllGroups = b;\r
595   }\r
596 \r
597   public boolean getColourAppliesToAllGroups()\r
598   {\r
599     return colourAppliesToAllGroups;\r
600   }\r
601 \r
602   public boolean getShowFullId()\r
603   {\r
604     return showFullId;\r
605   }\r
606 \r
607   public void setShowFullId(boolean b)\r
608   {\r
609     showFullId = b;\r
610   }\r
611 \r
612   public boolean getShowAnnotation()\r
613   {\r
614     return showAnnotation;\r
615   }\r
616 \r
617   public void setShowAnnotation(boolean b)\r
618   {\r
619     showAnnotation = b;\r
620   }\r
621 \r
622   public boolean getScaleAboveWrapped()\r
623   {\r
624     return scaleAboveWrapped;\r
625   }\r
626 \r
627   public boolean getScaleLeftWrapped()\r
628   {\r
629     return scaleLeftWrapped;\r
630   }\r
631 \r
632   public boolean getScaleRightWrapped()\r
633   {\r
634     return scaleRightWrapped;\r
635   }\r
636 \r
637   public void setScaleAboveWrapped(boolean b)\r
638   {\r
639     scaleAboveWrapped = b;\r
640   }\r
641 \r
642   public void setScaleLeftWrapped(boolean b)\r
643   {\r
644     scaleLeftWrapped = b;\r
645   }\r
646 \r
647   public void setScaleRightWrapped(boolean b)\r
648   {\r
649     scaleRightWrapped = b;\r
650   }\r
651 \r
652   public void setIgnoreGapsConsensus(boolean b)\r
653   {\r
654     ignoreGapsInConsensusCalculation = b;\r
655     updateConsensus();\r
656     if (globalColourScheme!=null)\r
657     {\r
658       globalColourScheme.setThreshold(globalColourScheme.getThreshold(),\r
659           ignoreGapsInConsensusCalculation);\r
660 \r
661     }\r
662 \r
663   }\r
664 \r
665   public boolean getIgnoreGapsConsensus()\r
666   {\r
667     return ignoreGapsInConsensusCalculation;\r
668   }\r
669 \r
670 \r
671 }\r