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