Applet files
[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       annotations[i] = new Annotation(sequence.charAt(i) + "",\r
153                                       "Conservation graph", ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
154       // Quality calc\r
155       value = ((Double) cons.quality.elementAt(i)).floatValue();\r
156       vprop = value - qmin;\r
157       vprop/=qmax;\r
158       qannotations[i] = new Annotation(" ",\r
159                                       String.valueOf(value), ' ', value, new Color(minR+maxR*vprop, minG+maxG*vprop, minB+maxB*vprop));\r
160     }\r
161 \r
162     if(conservation==null)\r
163     {\r
164       conservation = new AlignmentAnnotation("Conservation",\r
165                                              "Conservation of total alignment less than "+ConsPercGaps+"% gaps",\r
166                                              annotations,\r
167                                              0f, // cons.qualityRange[0].floatValue(),\r
168                                              11f, // cons.qualityRange[1].floatValue()\r
169                                              1);\r
170       if(showConservation)\r
171       alignment.addAnnotation(conservation);\r
172       quality = new AlignmentAnnotation("Quality",\r
173                                         "Alignment Quality based on Blosum62 scores",\r
174                                         qannotations,\r
175                                         cons.qualityRange[0].floatValue(),\r
176                                         cons.qualityRange[1].floatValue(),\r
177                                         1);\r
178       if(showQuality)\r
179         alignment.addAnnotation(quality);\r
180     }\r
181     else {\r
182       conservation.annotations = annotations;\r
183       quality.annotations = qannotations;\r
184       quality.graphMax = cons.qualityRange[1].floatValue();\r
185     }\r
186 \r
187 \r
188   }\r
189 \r
190   public void updateConsensus()\r
191   {\r
192     Annotation [] annotations = new Annotation[alignment.getWidth()];\r
193 \r
194     // this routine prevents vconsensus becoming a new object each time\r
195     // consenus is calculated. Important for speed of Blosum62\r
196     // and PID colouring of alignment\r
197     if(vconsensus == null)\r
198         vconsensus = alignment.getAAFrequency();\r
199     else\r
200     {\r
201         Vector temp = alignment.getAAFrequency();\r
202         vconsensus.removeAllElements();\r
203         Enumeration e = temp.elements();\r
204         while(e.hasMoreElements())\r
205         {\r
206           vconsensus.addElement(e.nextElement());\r
207         }\r
208     }\r
209     Hashtable hash = null;\r
210     for (int i = 0; i<alignment.getWidth(); i++)\r
211     {\r
212         hash = (Hashtable) vconsensus.elementAt(i);\r
213         float value = new Float(hash.get("maxCount").toString()).floatValue();\r
214         value /= new Float(hash.get("size").toString()).floatValue();\r
215 \r
216         value *= 100;\r
217         String maxRes = hash.get("maxResidue")+" ";\r
218         String mouseOver = hash.get("maxResidue")+" ";\r
219         if(maxRes.length()>2)\r
220         {\r
221           mouseOver = "["+maxRes+"] ";\r
222           maxRes = "+ ";\r
223         }\r
224 \r
225         mouseOver += (int)value+"%";\r
226         annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
227 \r
228     }\r
229 \r
230      if(consensus==null)\r
231      {\r
232        consensus = new AlignmentAnnotation("Consensus",\r
233                                            "PID", annotations, 0f, 100f, 1);\r
234        if(showConsensus)\r
235          alignment.addAnnotation(consensus);\r
236      }\r
237      else\r
238        consensus.annotations = annotations;\r
239 \r
240   }\r
241 \r
242 \r
243   public SequenceGroup getSelectionGroup()\r
244   {\r
245     return selectionGroup;\r
246   }\r
247 \r
248   public void setSelectionGroup(SequenceGroup sg)\r
249   {\r
250     selectionGroup = sg;\r
251   }\r
252 \r
253 \r
254  public boolean getConservationSelected()\r
255  {\r
256    return conservationColourSelected;\r
257  }\r
258 \r
259  public void setConservationSelected(boolean b)\r
260  {\r
261    conservationColourSelected = b;\r
262  }\r
263 \r
264  public boolean getAbovePIDThreshold()\r
265  {\r
266    return abovePIDThreshold;\r
267  }\r
268 \r
269  public void setAbovePIDThreshold(boolean b)\r
270  {\r
271    abovePIDThreshold = b;\r
272  }\r
273 \r
274   public int getStartRes() {\r
275     return startRes;\r
276   }\r
277 \r
278   public int getEndRes() {\r
279     return endRes;\r
280   }\r
281 \r
282   public int getStartSeq() {\r
283     return startSeq;\r
284   }\r
285 \r
286   public void setGlobalColourScheme(ColourSchemeI cs)\r
287   {\r
288      globalColourScheme = cs;\r
289   }\r
290 \r
291   public ColourSchemeI getGlobalColourScheme()\r
292   {\r
293     return globalColourScheme;\r
294   }\r
295 \r
296 \r
297   public void setStartRes(int res) {\r
298     this.startRes = res;\r
299   }\r
300   public void setStartSeq(int seq) {\r
301     this.startSeq = seq;\r
302   }\r
303   public void setEndRes(int res) {\r
304     if (res > alignment.getWidth()-1) {\r
305       System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
306        res = alignment.getWidth()-1;\r
307     }\r
308     if (res < 0) {\r
309       res = 0;\r
310     }\r
311     this.endRes = res;\r
312   }\r
313   public void setEndSeq(int seq) {\r
314     if (seq > alignment.getHeight()) {\r
315       seq = alignment.getHeight();\r
316     }\r
317     if (seq < 0) {\r
318       seq = 0;\r
319     }\r
320     this.endSeq = seq;\r
321   }\r
322   public int getEndSeq() {\r
323     return endSeq;\r
324   }\r
325 \r
326   public void setFont(Font f) {\r
327     font = f;\r
328     java.awt.Frame temp = new java.awt.Frame();\r
329     temp.addNotify();\r
330     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
331     setCharHeight(fm.getHeight());\r
332     setCharWidth(fm.charWidth('M'));\r
333   }\r
334 \r
335   public Font getFont() {\r
336     return font;\r
337   }\r
338   public void setCharWidth(int w) {\r
339     this.charWidth = w;\r
340   }\r
341   public int getCharWidth() {\r
342     return charWidth;\r
343   }\r
344   public void setCharHeight(int h) {\r
345     this.charHeight = h;\r
346   }\r
347   public int getCharHeight() {\r
348     return charHeight;\r
349   }\r
350   public void setChunkWidth(int w) {\r
351     this.chunkWidth = w;\r
352   }\r
353   public int getChunkWidth() {\r
354     return chunkWidth;\r
355   }\r
356   public void setChunkHeight(int h) {\r
357     this.chunkHeight = h;\r
358   }\r
359   public int getChunkHeight() {\r
360     return chunkHeight;\r
361   }\r
362   public AlignmentI getAlignment() {\r
363     return alignment;\r
364   }\r
365   public void setAlignment(AlignmentI align) {\r
366     this.alignment = align;\r
367   }\r
368 \r
369   public void setWrapAlignment(boolean state) {\r
370     wrapAlignment = state;\r
371   }\r
372   public void setShowText(boolean state) {\r
373     showText = state;\r
374   }\r
375 \r
376   public void setRenderGaps(boolean state){\r
377     renderGaps = state;\r
378   }\r
379 \r
380 \r
381   public boolean getColourText()\r
382   {\r
383     return showColourText;\r
384   }\r
385 \r
386   public void setColourText(boolean state)\r
387   {\r
388     showColourText = state;\r
389   }\r
390 \r
391   public void setShowBoxes(boolean state) {\r
392     showBoxes = state;\r
393   }\r
394 \r
395   public boolean getWrapAlignment() {\r
396       return wrapAlignment;\r
397   }\r
398   public boolean getShowText() {\r
399     return showText;\r
400   }\r
401   public boolean getShowBoxes() {\r
402     return showBoxes;\r
403   }\r
404 \r
405   public char getGapCharacter() {\r
406     return getAlignment().getGapCharacter();\r
407   }\r
408   public void setGapCharacter(char gap) {\r
409     if (getAlignment() != null) {\r
410       getAlignment().setGapCharacter(gap);\r
411     }\r
412   }\r
413   public void setThreshold(int thresh) {\r
414     threshold = thresh;\r
415   }\r
416   public int getThreshold() {\r
417     return threshold;\r
418   }\r
419   public void setIncrement(int inc) {\r
420     increment = inc;\r
421   }\r
422   public int getIncrement() {\r
423     return increment;\r
424   }\r
425   public int getIndex(int y) {\r
426     int y1     = 0;\r
427     int starty = getStartSeq();\r
428     int endy   = getEndSeq();\r
429 \r
430     for (int i = starty; i <= endy; i++) {\r
431       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null) {\r
432         int y2 = y1 + getCharHeight();\r
433 \r
434         if (y>=y1 && y <=y2) {\r
435           return i;\r
436         }\r
437         y1  = y2;\r
438       } else {\r
439         return -1;\r
440       }\r
441     }\r
442     return -1;\r
443   }\r
444 \r
445   public ColumnSelection getColumnSelection() {\r
446     return colSel;\r
447   }\r
448 \r
449   public void resetSeqLimits(int height) {\r
450     setEndSeq(height/getCharHeight());\r
451   }\r
452   public void setCurrentTree(NJTree tree) {\r
453       currentTree = tree;\r
454   }\r
455   public NJTree getCurrentTree() {\r
456     return currentTree;\r
457   }\r
458 \r
459 \r
460   public void setColourAppliesToAllGroups(boolean b)\r
461   {   colourAppliesToAllGroups = b; }\r
462 \r
463   public boolean getColourAppliesToAllGroups()\r
464   {return colourAppliesToAllGroups; }\r
465 \r
466   public boolean getShowFullId()\r
467   {\r
468     return showFullId;\r
469   }\r
470 \r
471   public void setShowFullId(boolean b)\r
472   {\r
473     showFullId = b;\r
474   }\r
475 \r
476   public boolean getShowAnnotation()\r
477   {\r
478     return showAnnotation;\r
479   }\r
480 \r
481   public void setShowAnnotation(boolean b)\r
482   {\r
483     showAnnotation = b;\r
484   }\r
485 \r
486   public boolean getScaleAboveWrapped()\r
487   { return scaleAboveWrapped;}\r
488 \r
489   public boolean getScaleLeftWrapped()\r
490   { return scaleLeftWrapped; }\r
491 \r
492   public boolean getScaleRightWrapped()\r
493   { return scaleRightWrapped; }\r
494 \r
495   public void setScaleAboveWrapped(boolean b)\r
496   { scaleAboveWrapped = b; }\r
497 \r
498   public void setScaleLeftWrapped(boolean b)\r
499   { scaleLeftWrapped = b; }\r
500 \r
501   public void setScaleRightWrapped(boolean b)\r
502   { scaleRightWrapped = b; }\r
503 \r
504 \r
505 }\r