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