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