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