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