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