Must update globalColourScheme with new consensus
[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     if(globalColourScheme!=null)\r
305           globalColourScheme.setConsensus(vconsensus);\r
306 \r
307   }\r
308 \r
309   public SequenceGroup getSelectionGroup()\r
310   {\r
311     return selectionGroup;\r
312   }\r
313 \r
314   public void setSelectionGroup(SequenceGroup sg)\r
315   {\r
316     selectionGroup = sg;\r
317   }\r
318 \r
319   public boolean getConservationSelected()\r
320   {\r
321     return conservationColourSelected;\r
322   }\r
323 \r
324   public void setConservationSelected(boolean b)\r
325   {\r
326     conservationColourSelected = b;\r
327   }\r
328 \r
329   public boolean getAbovePIDThreshold()\r
330   {\r
331     return abovePIDThreshold;\r
332   }\r
333 \r
334   public void setAbovePIDThreshold(boolean b)\r
335   {\r
336     abovePIDThreshold = b;\r
337   }\r
338 \r
339   public int getStartRes()\r
340   {\r
341     return startRes;\r
342   }\r
343 \r
344   public int getEndRes()\r
345   {\r
346     return endRes;\r
347   }\r
348 \r
349   public int getStartSeq()\r
350   {\r
351     return startSeq;\r
352   }\r
353 \r
354   public void setGlobalColourScheme(ColourSchemeI cs)\r
355   {\r
356     globalColourScheme = cs;\r
357   }\r
358 \r
359   public ColourSchemeI getGlobalColourScheme()\r
360   {\r
361     return globalColourScheme;\r
362   }\r
363 \r
364   public void setStartRes(int res)\r
365   {\r
366     this.startRes = res;\r
367   }\r
368 \r
369   public void setStartSeq(int seq)\r
370   {\r
371     this.startSeq = seq;\r
372   }\r
373 \r
374   public void setEndRes(int res)\r
375   {\r
376     if (res > alignment.getWidth() - 1)\r
377     {\r
378       // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
379       res = alignment.getWidth() - 1;\r
380     }\r
381     if (res < 0)\r
382     {\r
383       res = 0;\r
384     }\r
385     this.endRes = res;\r
386   }\r
387 \r
388   public void setEndSeq(int seq)\r
389   {\r
390     if (seq > alignment.getHeight())\r
391     {\r
392       seq = alignment.getHeight();\r
393     }\r
394     if (seq < 0)\r
395     {\r
396       seq = 0;\r
397     }\r
398     this.endSeq = seq;\r
399   }\r
400 \r
401   public int getEndSeq()\r
402   {\r
403     return endSeq;\r
404   }\r
405 \r
406   public void setFont(Font f)\r
407   {\r
408     font = f;\r
409     java.awt.Frame temp = new java.awt.Frame();\r
410     temp.addNotify();\r
411     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
412     setCharHeight(fm.getHeight());\r
413     setCharWidth(fm.charWidth('M'));\r
414   }\r
415 \r
416   public Font getFont()\r
417   {\r
418     return font;\r
419   }\r
420 \r
421   public void setCharWidth(int w)\r
422   {\r
423     this.charWidth = w;\r
424   }\r
425 \r
426   public int getCharWidth()\r
427   {\r
428     return charWidth;\r
429   }\r
430 \r
431   public void setCharHeight(int h)\r
432   {\r
433     this.charHeight = h;\r
434   }\r
435 \r
436   public int getCharHeight()\r
437   {\r
438     return charHeight;\r
439   }\r
440 \r
441   public void setChunkWidth(int w)\r
442   {\r
443     this.chunkWidth = w;\r
444   }\r
445 \r
446   public int getChunkWidth()\r
447   {\r
448     return chunkWidth;\r
449   }\r
450 \r
451   public void setChunkHeight(int h)\r
452   {\r
453     this.chunkHeight = h;\r
454   }\r
455 \r
456   public int getChunkHeight()\r
457   {\r
458     return chunkHeight;\r
459   }\r
460 \r
461   public AlignmentI getAlignment()\r
462   {\r
463     return alignment;\r
464   }\r
465 \r
466   public void setAlignment(AlignmentI align)\r
467   {\r
468     this.alignment = align;\r
469   }\r
470 \r
471   public void setWrapAlignment(boolean state)\r
472   {\r
473     wrapAlignment = state;\r
474   }\r
475 \r
476   public void setShowText(boolean state)\r
477   {\r
478     showText = state;\r
479   }\r
480 \r
481   public void setRenderGaps(boolean state)\r
482   {\r
483     renderGaps = state;\r
484   }\r
485 \r
486   public boolean getColourText()\r
487   {\r
488     return showColourText;\r
489   }\r
490 \r
491   public void setColourText(boolean state)\r
492   {\r
493     showColourText = state;\r
494   }\r
495 \r
496   public void setShowBoxes(boolean state)\r
497   {\r
498     showBoxes = state;\r
499   }\r
500 \r
501   public boolean getWrapAlignment()\r
502   {\r
503     return wrapAlignment;\r
504   }\r
505 \r
506   public boolean getShowText()\r
507   {\r
508     return showText;\r
509   }\r
510 \r
511   public boolean getShowBoxes()\r
512   {\r
513     return showBoxes;\r
514   }\r
515 \r
516   public char getGapCharacter()\r
517   {\r
518     return getAlignment().getGapCharacter();\r
519   }\r
520 \r
521   public void setGapCharacter(char gap)\r
522   {\r
523     if (getAlignment() != null)\r
524     {\r
525       getAlignment().setGapCharacter(gap);\r
526     }\r
527   }\r
528 \r
529   public void setThreshold(int thresh)\r
530   {\r
531     threshold = thresh;\r
532   }\r
533 \r
534   public int getThreshold()\r
535   {\r
536     return threshold;\r
537   }\r
538 \r
539   public void setIncrement(int inc)\r
540   {\r
541     increment = inc;\r
542   }\r
543 \r
544   public int getIncrement()\r
545   {\r
546     return increment;\r
547   }\r
548 \r
549   public int getIndex(int y)\r
550   {\r
551     int y1 = 0;\r
552     int starty = getStartSeq();\r
553     int endy = getEndSeq();\r
554 \r
555     for (int i = starty; i <= endy; i++)\r
556     {\r
557       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null)\r
558       {\r
559         int y2 = y1 + getCharHeight();\r
560 \r
561         if (y >= y1 && y <= y2)\r
562         {\r
563           return i;\r
564         }\r
565         y1 = y2;\r
566       }\r
567       else\r
568       {\r
569         return -1;\r
570       }\r
571     }\r
572     return -1;\r
573   }\r
574 \r
575   public ColumnSelection getColumnSelection()\r
576   {\r
577     return colSel;\r
578   }\r
579 \r
580   public void resetSeqLimits(int height)\r
581   {\r
582     setEndSeq(height / getCharHeight());\r
583   }\r
584 \r
585   public void setCurrentTree(NJTree tree)\r
586   {\r
587     currentTree = tree;\r
588   }\r
589 \r
590   public NJTree getCurrentTree()\r
591   {\r
592     return currentTree;\r
593   }\r
594 \r
595   public void setColourAppliesToAllGroups(boolean b)\r
596   {\r
597     colourAppliesToAllGroups = b;\r
598   }\r
599 \r
600   public boolean getColourAppliesToAllGroups()\r
601   {\r
602     return colourAppliesToAllGroups;\r
603   }\r
604 \r
605   public boolean getShowFullId()\r
606   {\r
607     return showFullId;\r
608   }\r
609 \r
610   public void setShowFullId(boolean b)\r
611   {\r
612     showFullId = b;\r
613   }\r
614 \r
615   public boolean getShowAnnotation()\r
616   {\r
617     return showAnnotation;\r
618   }\r
619 \r
620   public void setShowAnnotation(boolean b)\r
621   {\r
622     showAnnotation = b;\r
623   }\r
624 \r
625   public boolean getScaleAboveWrapped()\r
626   {\r
627     return scaleAboveWrapped;\r
628   }\r
629 \r
630   public boolean getScaleLeftWrapped()\r
631   {\r
632     return scaleLeftWrapped;\r
633   }\r
634 \r
635   public boolean getScaleRightWrapped()\r
636   {\r
637     return scaleRightWrapped;\r
638   }\r
639 \r
640   public void setScaleAboveWrapped(boolean b)\r
641   {\r
642     scaleAboveWrapped = b;\r
643   }\r
644 \r
645   public void setScaleLeftWrapped(boolean b)\r
646   {\r
647     scaleLeftWrapped = b;\r
648   }\r
649 \r
650   public void setScaleRightWrapped(boolean b)\r
651   {\r
652     scaleRightWrapped = b;\r
653   }\r
654 \r
655   public void setIgnoreGapsConsensus(boolean b)\r
656   {\r
657     ignoreGapsInConsensusCalculation = b;\r
658     updateConsensus();\r
659     if (globalColourScheme!=null)\r
660     {\r
661       globalColourScheme.setThreshold(globalColourScheme.getThreshold(),\r
662           ignoreGapsInConsensusCalculation);\r
663 \r
664     }\r
665 \r
666   }\r
667 \r
668   public boolean getIgnoreGapsConsensus()\r
669   {\r
670     return ignoreGapsInConsensusCalculation;\r
671   }\r
672 \r
673 \r
674 }\r