Remove desc for conserve graph
[jalview.git] / src / jalview / appletgui / AlignViewport.java
1 package jalview.appletgui;\r
2 \r
3 import java.awt.*;\r
4 import jalview.bin.*;\r
5 import jalview.analysis.*;\r
6 import jalview.datamodel.*;\r
7 import jalview.schemes.*;\r
8 import java.util.*;\r
9 \r
10 public class AlignViewport\r
11 {\r
12   int startRes;\r
13   int endRes;\r
14 \r
15   int startSeq;\r
16   int endSeq;\r
17 \r
18   boolean showFullId = true;\r
19   boolean showText=true;\r
20   boolean showColourText=false;\r
21   boolean showBoxes=true;\r
22   boolean wrapAlignment=false;\r
23   boolean renderGaps = true;\r
24   boolean showSequenceFeatures = false;\r
25   boolean showAnnotation = true;\r
26   boolean showConservation = true;\r
27   boolean showQuality = true;\r
28   boolean showConsensus = true;\r
29 \r
30   boolean colourAppliesToAllGroups = true;\r
31   ColourSchemeI globalColourScheme = null;\r
32   boolean conservationColourSelected = false;\r
33   boolean abovePIDThreshold = false;\r
34 \r
35   SequenceGroup selectionGroup = new SequenceGroup();\r
36 \r
37   int             charHeight;\r
38   int             charWidth;\r
39   int             chunkWidth;\r
40   int             chunkHeight;\r
41 \r
42   Font            font = new Font("SansSerif",Font.PLAIN,10);\r
43   AlignmentI      alignment;\r
44 \r
45   ColumnSelection colSel = new ColumnSelection();\r
46 \r
47   int threshold;\r
48   int increment;\r
49 \r
50   NJTree currentTree = null;\r
51 \r
52   boolean scaleAboveWrapped = true;\r
53   boolean scaleLeftWrapped  = true;\r
54   boolean scaleRightWrapped = true;\r
55 \r
56   public AlignViewport(AlignmentI al, JalviewLite applet)\r
57   {\r
58     setAlignment(al);\r
59     this.startRes = 0;\r
60     this.endRes = al.getWidth()-1;\r
61     this.startSeq = 0;\r
62     this.endSeq = al.getHeight()-1;\r
63     setFont(font);\r
64 \r
65 \r
66     if(applet!=null)\r
67     {\r
68       String param = applet.getParameter("showFullId");\r
69       if (param != null)\r
70         showFullId = Boolean.valueOf(param).booleanValue();\r
71 \r
72       param = applet.getParameter("showAnnotation");\r
73       if (param != null)\r
74         showAnnotation = Boolean.valueOf(param).booleanValue();\r
75 \r
76       param = applet.getParameter("showConservation");\r
77       if (param != null)\r
78         showConservation = Boolean.valueOf(param).booleanValue();\r
79 \r
80       param = applet.getParameter("showQuality");\r
81       if (param != null)\r
82         showQuality = Boolean.valueOf(param).booleanValue();\r
83 \r
84       param = applet.getParameter("showConsensus");\r
85       if (param != null)\r
86         showConsensus = Boolean.valueOf(param).booleanValue();\r
87     }\r
88     // We must set conservation and consensus before setting colour,\r
89     // as Blosum and Clustal require this to be done\r
90     updateConservation();\r
91     updateConsensus();\r
92 \r
93     if(applet!=null && applet.getParameter("defaultColour")!=null)\r
94     {\r
95       globalColourScheme = ColourSchemeProperty.getColour(alignment,\r
96           applet.getParameter("defaultColour"));\r
97       if(globalColourScheme!=null)\r
98         globalColourScheme.setConsensus( vconsensus );\r
99    }\r
100  }\r
101 \r
102 \r
103  public void showSequenceFeatures(boolean b)\r
104  {\r
105    showSequenceFeatures = b;\r
106  }\r
107 \r
108   public Vector vconsensus;\r
109   AlignmentAnnotation consensus;\r
110   AlignmentAnnotation conservation;\r
111   AlignmentAnnotation quality;\r
112 \r
113   public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!\r
114 \r
115   public void updateConservation()\r
116   {\r
117     Conservation cons = new jalview.analysis.Conservation("All",\r
118         jalview.schemes.ResidueProperties.propHash, 3,\r
119         alignment.getSequences(), 0,\r
120         alignment.getWidth()-1);\r
121     cons.calculate();\r
122     cons.verdict(false, ConsPercGaps);\r
123     cons.findQuality();\r
124     int alWidth = alignment.getWidth();\r
125     Annotation [] annotations = new Annotation[alWidth];\r
126     Annotation [] qannotations = new Annotation[alWidth];\r
127     String sequence = cons.getConsSequence().getSequence();\r
128     float minR,minG,minB, maxR,maxG,maxB;\r
129     minR = 0.3f;\r
130     minG = 0.0f;\r
131     minB = 0f;\r
132     maxR = 1.0f-minR; maxG=0.9f-minG; maxB=0f-minB; // scalable range for colouring both Conservation and Quality\r
133     float min = 0f;\r
134     float max = 11f;\r
135     float qmin = cons.qualityRange[0].floatValue();\r
136     float qmax = cons.qualityRange[1].floatValue();\r
137 \r
138     for (int i = 0; i < alWidth; i++)\r
139     {\r
140       float value = 0;\r
141       try\r
142         {\r
143           value = Integer.parseInt(sequence.charAt(i) + "");\r
144         }\r
145       catch (Exception ex)\r
146         {\r
147           if (sequence.charAt(i) == '*') value = 11;\r
148           if (sequence.charAt(i) == '+') value = 10;\r
149         }\r
150       float vprop = value-min;\r
151       vprop/=max;\r
152 \r
153       annotations[i] = new Annotation(sequence.charAt(i) + "",\r
154                                       "", ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
155       // Quality calc\r
156       value = ((Double) cons.quality.elementAt(i)).floatValue();\r
157       vprop = value - qmin;\r
158       vprop/=qmax;\r
159       qannotations[i] = new Annotation(" ",\r
160                                       String.valueOf(value), ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
161     }\r
162 \r
163     if(conservation==null)\r
164     {\r
165       conservation = new AlignmentAnnotation("Conservation",\r
166                                              "Conservation of total alignment less than "+ConsPercGaps+"% gaps",\r
167                                              annotations,\r
168                                              0f, // cons.qualityRange[0].floatValue(),\r
169                                              11f, // cons.qualityRange[1].floatValue()\r
170                                              1);\r
171       if(showConservation)\r
172       alignment.addAnnotation(conservation);\r
173       quality = new AlignmentAnnotation("Quality",\r
174                                         "Alignment Quality based on Blosum62 scores",\r
175                                         qannotations,\r
176                                         cons.qualityRange[0].floatValue(),\r
177                                         cons.qualityRange[1].floatValue(),\r
178                                         1);\r
179       if(showQuality)\r
180         alignment.addAnnotation(quality);\r
181     }\r
182     else {\r
183       conservation.annotations = annotations;\r
184       quality.annotations = qannotations;\r
185       quality.graphMax = cons.qualityRange[1].floatValue();\r
186     }\r
187 \r
188 \r
189   }\r
190 \r
191   public void updateConsensus()\r
192   {\r
193     Annotation [] annotations = new Annotation[alignment.getWidth()];\r
194 \r
195     // this routine prevents vconsensus becoming a new object each time\r
196     // consenus is calculated. Important for speed of Blosum62\r
197     // and PID colouring of alignment\r
198     if(vconsensus == null)\r
199         vconsensus = alignment.getAAFrequency();\r
200     else\r
201     {\r
202         Vector temp = alignment.getAAFrequency();\r
203         vconsensus.removeAllElements();\r
204         Enumeration e = temp.elements();\r
205         while(e.hasMoreElements())\r
206         {\r
207           vconsensus.addElement(e.nextElement());\r
208         }\r
209     }\r
210     Hashtable hash = null;\r
211     for (int i = 0; i<alignment.getWidth(); i++)\r
212     {\r
213         hash = (Hashtable) vconsensus.elementAt(i);\r
214         float value = new Float(hash.get("maxCount").toString()).floatValue();\r
215         value /= new Float(hash.get("size").toString()).floatValue();\r
216 \r
217         value *= 100;\r
218         String maxRes = hash.get("maxResidue")+" ";\r
219         String mouseOver = hash.get("maxResidue")+" ";\r
220         if(maxRes.length()>2)\r
221         {\r
222           mouseOver = "["+maxRes+"] ";\r
223           maxRes = "+ ";\r
224         }\r
225 \r
226         mouseOver += (int)value+"%";\r
227         annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
228 \r
229     }\r
230 \r
231      if(consensus==null)\r
232      {\r
233        consensus = new AlignmentAnnotation("Consensus",\r
234                                            "PID", annotations, 0f, 100f, 1);\r
235        if(showConsensus)\r
236          alignment.addAnnotation(consensus);\r
237      }\r
238      else\r
239        consensus.annotations = annotations;\r
240 \r
241   }\r
242 \r
243 \r
244   public SequenceGroup getSelectionGroup()\r
245   {\r
246     return selectionGroup;\r
247   }\r
248 \r
249   public void setSelectionGroup(SequenceGroup sg)\r
250   {\r
251     selectionGroup = sg;\r
252   }\r
253 \r
254 \r
255  public boolean getConservationSelected()\r
256  {\r
257    return conservationColourSelected;\r
258  }\r
259 \r
260  public void setConservationSelected(boolean b)\r
261  {\r
262    conservationColourSelected = b;\r
263  }\r
264 \r
265  public boolean getAbovePIDThreshold()\r
266  {\r
267    return abovePIDThreshold;\r
268  }\r
269 \r
270  public void setAbovePIDThreshold(boolean b)\r
271  {\r
272    abovePIDThreshold = b;\r
273  }\r
274 \r
275   public int getStartRes() {\r
276     return startRes;\r
277   }\r
278 \r
279   public int getEndRes() {\r
280     return endRes;\r
281   }\r
282 \r
283   public int getStartSeq() {\r
284     return startSeq;\r
285   }\r
286 \r
287   public void setGlobalColourScheme(ColourSchemeI cs)\r
288   {\r
289      globalColourScheme = cs;\r
290   }\r
291 \r
292   public ColourSchemeI getGlobalColourScheme()\r
293   {\r
294     return globalColourScheme;\r
295   }\r
296 \r
297 \r
298   public void setStartRes(int res) {\r
299     this.startRes = res;\r
300   }\r
301   public void setStartSeq(int seq) {\r
302     this.startSeq = seq;\r
303   }\r
304   public void setEndRes(int res) {\r
305     if (res > alignment.getWidth()-1) {\r
306       // log.System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
307        res = alignment.getWidth()-1;\r
308     }\r
309     if (res < 0) {\r
310       res = 0;\r
311     }\r
312     this.endRes = res;\r
313   }\r
314   public void setEndSeq(int seq) {\r
315     if (seq > alignment.getHeight()) {\r
316       seq = alignment.getHeight();\r
317     }\r
318     if (seq < 0) {\r
319       seq = 0;\r
320     }\r
321     this.endSeq = seq;\r
322   }\r
323   public int getEndSeq() {\r
324     return endSeq;\r
325   }\r
326 \r
327   public void setFont(Font f) {\r
328     font = f;\r
329     java.awt.Frame temp = new java.awt.Frame();\r
330     temp.addNotify();\r
331     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
332     setCharHeight(fm.getHeight());\r
333     setCharWidth(fm.charWidth('M'));\r
334   }\r
335 \r
336   public Font getFont() {\r
337     return font;\r
338   }\r
339   public void setCharWidth(int w) {\r
340     this.charWidth = w;\r
341   }\r
342   public int getCharWidth() {\r
343     return charWidth;\r
344   }\r
345   public void setCharHeight(int h) {\r
346     this.charHeight = h;\r
347   }\r
348   public int getCharHeight() {\r
349     return charHeight;\r
350   }\r
351   public void setChunkWidth(int w) {\r
352     this.chunkWidth = w;\r
353   }\r
354   public int getChunkWidth() {\r
355     return chunkWidth;\r
356   }\r
357   public void setChunkHeight(int h) {\r
358     this.chunkHeight = h;\r
359   }\r
360   public int getChunkHeight() {\r
361     return chunkHeight;\r
362   }\r
363   public AlignmentI getAlignment() {\r
364     return alignment;\r
365   }\r
366   public void setAlignment(AlignmentI align) {\r
367     this.alignment = align;\r
368   }\r
369 \r
370   public void setWrapAlignment(boolean state) {\r
371     wrapAlignment = state;\r
372   }\r
373   public void setShowText(boolean state) {\r
374     showText = state;\r
375   }\r
376 \r
377   public void setRenderGaps(boolean state){\r
378     renderGaps = state;\r
379   }\r
380 \r
381 \r
382   public boolean getColourText()\r
383   {\r
384     return showColourText;\r
385   }\r
386 \r
387   public void setColourText(boolean state)\r
388   {\r
389     showColourText = state;\r
390   }\r
391 \r
392   public void setShowBoxes(boolean state) {\r
393     showBoxes = state;\r
394   }\r
395 \r
396   public boolean getWrapAlignment() {\r
397       return wrapAlignment;\r
398   }\r
399   public boolean getShowText() {\r
400     return showText;\r
401   }\r
402   public boolean getShowBoxes() {\r
403     return showBoxes;\r
404   }\r
405 \r
406   public char getGapCharacter() {\r
407     return getAlignment().getGapCharacter();\r
408   }\r
409   public void setGapCharacter(char gap) {\r
410     if (getAlignment() != null) {\r
411       getAlignment().setGapCharacter(gap);\r
412     }\r
413   }\r
414   public void setThreshold(int thresh) {\r
415     threshold = thresh;\r
416   }\r
417   public int getThreshold() {\r
418     return threshold;\r
419   }\r
420   public void setIncrement(int inc) {\r
421     increment = inc;\r
422   }\r
423   public int getIncrement() {\r
424     return increment;\r
425   }\r
426   public int getIndex(int y) {\r
427     int y1     = 0;\r
428     int starty = getStartSeq();\r
429     int endy   = getEndSeq();\r
430 \r
431     for (int i = starty; i <= endy; i++) {\r
432       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null) {\r
433         int y2 = y1 + getCharHeight();\r
434 \r
435         if (y>=y1 && y <=y2) {\r
436           return i;\r
437         }\r
438         y1  = y2;\r
439       } else {\r
440         return -1;\r
441       }\r
442     }\r
443     return -1;\r
444   }\r
445 \r
446   public ColumnSelection getColumnSelection() {\r
447     return colSel;\r
448   }\r
449 \r
450   public void resetSeqLimits(int height) {\r
451     setEndSeq(height/getCharHeight());\r
452   }\r
453   public void setCurrentTree(NJTree tree) {\r
454       currentTree = tree;\r
455   }\r
456   public NJTree getCurrentTree() {\r
457     return currentTree;\r
458   }\r
459 \r
460 \r
461   public void setColourAppliesToAllGroups(boolean b)\r
462   {   colourAppliesToAllGroups = b; }\r
463 \r
464   public boolean getColourAppliesToAllGroups()\r
465   {return colourAppliesToAllGroups; }\r
466 \r
467   public boolean getShowFullId()\r
468   {\r
469     return showFullId;\r
470   }\r
471 \r
472   public void setShowFullId(boolean b)\r
473   {\r
474     showFullId = b;\r
475   }\r
476 \r
477   public boolean getShowAnnotation()\r
478   {\r
479     return showAnnotation;\r
480   }\r
481 \r
482   public void setShowAnnotation(boolean b)\r
483   {\r
484     showAnnotation = b;\r
485   }\r
486 \r
487   public boolean getScaleAboveWrapped()\r
488   { return scaleAboveWrapped;}\r
489 \r
490   public boolean getScaleLeftWrapped()\r
491   { return scaleLeftWrapped; }\r
492 \r
493   public boolean getScaleRightWrapped()\r
494   { return scaleRightWrapped; }\r
495 \r
496   public void setScaleAboveWrapped(boolean b)\r
497   { scaleAboveWrapped = b; }\r
498 \r
499   public void setScaleLeftWrapped(boolean b)\r
500   { scaleLeftWrapped = b; }\r
501 \r
502   public void setScaleRightWrapped(boolean b)\r
503   { scaleRightWrapped = b; }\r
504 \r
505 \r
506 }\r