1c6c4537fdba534388698c0bd9544f876ce97ce2
[jalview.git] / src / jalview / gui / AlignViewport.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import jalview.io.*;\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 showScores=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 \r
25   ColourSchemeI globalColourScheme = null;\r
26   boolean conservationColourSelected = false;\r
27 \r
28   SequenceGroup rubberbandGroup = null;\r
29 \r
30   RendererI renderer = new SequenceRenderer(this);\r
31 \r
32   int             charHeight;\r
33   int             charWidth;\r
34   int             chunkWidth;\r
35   int             chunkHeight;\r
36 \r
37   Font            font = new Font("SansSerif",Font.PLAIN,10);\r
38   AlignmentI      alignment;\r
39 \r
40   Selection       sel    = new Selection(this);\r
41   ColumnSelection colSel = new ColumnSelection();\r
42 \r
43   OutputGenerator og;\r
44 \r
45   String visibleConsensus;\r
46 \r
47   int threshold;\r
48   int increment;\r
49 \r
50   NJTree currentTree = null;\r
51 \r
52   int window   = 50;\r
53   int baseline = 30;\r
54 \r
55   Vector kmers;\r
56 \r
57   public AlignViewport(AlignmentI da,\r
58                        boolean showScores,\r
59                        boolean showText,\r
60                        boolean showBoxes,\r
61                        boolean wrapAlignment) {\r
62     this(0,da.getWidth()-1,0,da.getHeight()-1,showScores,\r
63          showText,\r
64          showBoxes,\r
65          wrapAlignment);\r
66 \r
67     setAlignment(da);\r
68   }\r
69 \r
70   public AlignViewport(int startRes, int endRes,\r
71                        int startSeq, int endSeq,\r
72                        boolean showScores,\r
73                        boolean showText,\r
74                        boolean showBoxes,\r
75                        boolean wrapAlignment) {\r
76 \r
77     this.startRes = startRes;\r
78     this.endRes   = endRes;\r
79     this.startSeq = startSeq;\r
80     this.endSeq   = endSeq;\r
81 \r
82     this.showScores    = showScores;\r
83     this.showText      = showText;\r
84     this.showBoxes     = showBoxes;\r
85     this.wrapAlignment = wrapAlignment;\r
86 \r
87    // og  = new AlignmentOutputGenerator(this);\r
88 \r
89     setFont( font );\r
90  }\r
91 \r
92 \r
93   public String getVisibleConsensus()\r
94   {\r
95     return visibleConsensus;\r
96   }\r
97 \r
98   Vector consensus = new Vector();\r
99   public Vector getConsensus(boolean recalculate)\r
100   {\r
101     if(recalculate || consensus.size()<1)\r
102     {\r
103       consensus = alignment.getAAFrequency();\r
104       StringBuffer sb = new StringBuffer();\r
105       Hashtable hash = null;\r
106       for (int i = 0; i < consensus.size(); i++)\r
107       {\r
108         hash = (Hashtable) consensus.elementAt(i);\r
109         sb.append(hash.get("maxResidue").toString().charAt(0));\r
110       }\r
111       visibleConsensus = sb.toString();\r
112     }\r
113 \r
114 \r
115     return consensus;\r
116   }\r
117 \r
118   public SequenceGroup getRubberbandGroup()\r
119   {\r
120     return rubberbandGroup;\r
121   }\r
122 \r
123   public void setRubberbandGroup(SequenceGroup sg)\r
124   {\r
125     rubberbandGroup = sg;\r
126   }\r
127 \r
128  public boolean getConservationSelected()\r
129  {\r
130    return conservationColourSelected;\r
131  }\r
132 \r
133  public void setConservationSelected(boolean b)\r
134  {\r
135    conservationColourSelected = b;\r
136  }\r
137 \r
138   public int getStartRes() {\r
139     return startRes;\r
140   }\r
141 \r
142   public int getEndRes() {\r
143     return endRes;\r
144   }\r
145 \r
146   public int getStartSeq() {\r
147     return startSeq;\r
148   }\r
149 \r
150   public void setGlobalColourScheme(ColourSchemeI cs)\r
151   {\r
152      globalColourScheme = cs;\r
153   }\r
154 \r
155   public ColourSchemeI getGlobalColourScheme()\r
156   {\r
157     return globalColourScheme;\r
158   }\r
159 \r
160 \r
161   public void setStartRes(int res) {\r
162     this.startRes = res;\r
163   }\r
164   public void setStartSeq(int seq) {\r
165     this.startSeq = seq;\r
166   }\r
167   public void setEndRes(int res) {\r
168     if (res > alignment.getWidth()-1) {\r
169       System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
170        res = alignment.getWidth() -1;\r
171     }\r
172     if (res < 0) {\r
173       res = 0;\r
174     }\r
175     this.endRes = res;\r
176   }\r
177   public void setEndSeq(int seq) {\r
178     if (seq > alignment.getHeight()) {\r
179       seq = alignment.getHeight();\r
180     }\r
181     if (seq < 0) {\r
182       seq = 0;\r
183     }\r
184     this.endSeq = seq;\r
185   }\r
186   public int getEndSeq() {\r
187     return endSeq;\r
188   }\r
189 \r
190   public void setFont(Font f) {\r
191     font = f;\r
192     javax.swing.JFrame temp = new javax.swing.JFrame();\r
193     temp.addNotify();\r
194     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
195     setCharHeight(fm.getHeight());\r
196     setCharWidth(fm.charWidth('M'));\r
197   }\r
198 \r
199   public Font getFont() {\r
200     return font;\r
201   }\r
202   public void setCharWidth(int w) {\r
203     this.charWidth = w;\r
204   }\r
205   public int getCharWidth() {\r
206     return charWidth;\r
207   }\r
208   public void setCharHeight(int h) {\r
209     this.charHeight = h;\r
210   }\r
211   public int getCharHeight() {\r
212     return charHeight;\r
213   }\r
214   public void setChunkWidth(int w) {\r
215     this.chunkWidth = w;\r
216   }\r
217   public int getChunkWidth() {\r
218     return chunkWidth;\r
219   }\r
220   public void setChunkHeight(int h) {\r
221     this.chunkHeight = h;\r
222   }\r
223   public int getChunkHeight() {\r
224     return chunkHeight;\r
225   }\r
226   public AlignmentI getAlignment() {\r
227     return alignment;\r
228   }\r
229   public void setAlignment(AlignmentI align) {\r
230     this.alignment = align;\r
231   }\r
232   public void setShowScores(boolean state) {\r
233     showScores = state;\r
234   }\r
235   public void setWrapAlignment(boolean state) {\r
236     wrapAlignment = state;\r
237   }\r
238   public void setShowText(boolean state) {\r
239     showText = state;\r
240   }\r
241 \r
242   public void setRenderGaps(boolean state){\r
243     renderGaps = state;\r
244     if(renderer instanceof SequenceRenderer)\r
245     {\r
246       SequenceRenderer sr = (SequenceRenderer)renderer;\r
247       sr.renderGaps(state);\r
248     }\r
249   }\r
250 \r
251 \r
252   public boolean getColourText()\r
253   {\r
254     return showColourText;\r
255   }\r
256 \r
257   public void setColourText(boolean state)\r
258   {\r
259     showColourText = state;\r
260   }\r
261 \r
262   public void setShowBoxes(boolean state) {\r
263     showBoxes = state;\r
264   }\r
265   public boolean getShowScores() {\r
266     return showScores;\r
267   }\r
268   public boolean getWrapAlignment() {\r
269       return wrapAlignment;\r
270   }\r
271   public boolean getShowText() {\r
272     return showText;\r
273   }\r
274   public boolean getShowBoxes() {\r
275     return showBoxes;\r
276   }\r
277 \r
278   public char getGapCharacter() {\r
279     return getAlignment().getGapCharacter();\r
280   }\r
281   public void setGapCharacter(char gap) {\r
282     if (getAlignment() != null) {\r
283       getAlignment().setGapCharacter(gap);\r
284     }\r
285   }\r
286   public void setThreshold(int thresh) {\r
287     threshold = thresh;\r
288   }\r
289   public int getThreshold() {\r
290     return threshold;\r
291   }\r
292   public void setIncrement(int inc) {\r
293     increment = inc;\r
294   }\r
295   public int getIncrement() {\r
296     return increment;\r
297   }\r
298   public int getIndex(int y) {\r
299     int y1     = 0;\r
300     int starty = getStartSeq();\r
301     int endy   = getEndSeq();\r
302 \r
303     for (int i = starty; i <= endy; i++) {\r
304       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null) {\r
305         int y2 = y1 + getCharHeight();\r
306 \r
307         if (y>=y1 && y <=y2) {\r
308           return i;\r
309         }\r
310         y1  = y2;\r
311       } else {\r
312         return -1;\r
313       }\r
314     }\r
315     return -1;\r
316   }\r
317   public Selection getSelection() {\r
318     return sel;\r
319   }\r
320   public ColumnSelection getColumnSelection() {\r
321     return colSel;\r
322   }\r
323   public OutputGenerator getOutputGenerator() {\r
324     return og;\r
325   }\r
326   public void resetSeqLimits(int height) {\r
327     setStartSeq(0);\r
328     setEndSeq(height/getCharHeight());\r
329   }\r
330   public void setCurrentTree(NJTree tree) {\r
331       currentTree = tree;\r
332   }\r
333   public NJTree getCurrentTree() {\r
334     return currentTree;\r
335   }\r
336 \r
337     public void setRenderer(RendererI rend) {\r
338         this.renderer = rend;\r
339     }\r
340 \r
341     public RendererI getRenderer() {\r
342         return renderer;\r
343     }\r
344     public int getPIDWindow() {\r
345         return window;\r
346     }\r
347     public void setPIDWindow(int window) {\r
348         this.window = window;\r
349     }\r
350 \r
351     public int getPIDBaseline() {\r
352         return baseline;\r
353     }\r
354     public void setPIDBaseline(int baseline) {\r
355         this.baseline = baseline;\r
356     }\r
357 \r
358     public void setKmers(Vector kmers) {\r
359         this.kmers = kmers;\r
360     }\r
361 \r
362     public Vector getKmers() {\r
363         return this.kmers;\r
364     }\r
365 \r
366 \r
367 }\r