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