SequenceFeature display added
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import javax.swing.*;\r
5 import java.awt.event.*;\r
6 import jalview.datamodel.*;\r
7 import jalview.schemes.*;\r
8 import jalview.analysis.*;\r
9 \r
10 \r
11 public class SeqCanvas extends JPanel\r
12 {\r
13      FeatureRenderer fr;\r
14     Image             img;\r
15     Graphics          gg;\r
16     int               imgWidth;\r
17     int               imgHeight;\r
18 \r
19     AlignViewport     av;\r
20 \r
21     public boolean paintFlag = false;\r
22 \r
23     boolean showScores = false;\r
24     boolean displaySearch = false;\r
25     int [] searchResults = null;\r
26 \r
27     int chunkHeight;\r
28     int chunkWidth;\r
29 \r
30 \r
31     public SeqCanvas(AlignViewport av)\r
32     {\r
33         this.av         = av;\r
34        fr = new FeatureRenderer(av);\r
35        setLayout(new BorderLayout());\r
36        PaintRefresher.Register(this);\r
37 \r
38     }\r
39 \r
40   public void drawScale(Graphics g, int startx, int endx,int ypos) {\r
41       int scalestartx = startx - startx%10 + 10;\r
42 \r
43       g.setColor(Color.black);\r
44 \r
45       for (int i=scalestartx;i < endx;i+= 10)\r
46       {\r
47           String string = String.valueOf(i);\r
48           g.drawString(string,(i-startx-1)*av.charWidth,ypos - av.charHeight/2);\r
49 \r
50           g.drawLine( (i-startx-1)*av.charWidth +av.charWidth/2, ypos+2 - av.charHeight/2,\r
51                     (i-startx-1)*av.charWidth +av.charWidth/2, ypos-2 );\r
52 \r
53       }\r
54 \r
55   }\r
56 \r
57 \r
58 /**\r
59  * Definitions of startx and endx (hopefully):\r
60  * SMJS This is what I'm working towards!\r
61  *   startx is the first residue (starting at 0) to display.\r
62  *   endx   is the last residue to display (starting at 0).\r
63  *   starty is the first sequence to display (starting at 0).\r
64  *   endy   is the last sequence to display (starting at 0).\r
65  * NOTE 1: The av limits are set in setFont in this class and\r
66  * in the adjustment listener in SeqPanel when the scrollbars move.\r
67  */\r
68 \r
69   public void paintComponent(Graphics g)\r
70   {\r
71 \r
72     if (img==null ||  imgWidth!=getWidth() || imgHeight!=getHeight() || paintFlag)\r
73     {\r
74       imgWidth  = getWidth();\r
75       imgHeight = getHeight();\r
76       img = createImage(imgWidth,imgHeight);\r
77       gg  = img.getGraphics();\r
78       gg.setFont(av.getFont());\r
79       paintFlag = false;\r
80     }\r
81 \r
82 \r
83     chunkWidth  =   getWidth()/av.charWidth;\r
84     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
85 \r
86     av.setChunkHeight(chunkHeight);\r
87     av.setChunkWidth(chunkWidth);\r
88 \r
89     fillBackground(gg,Color.WHITE,0,0,imgWidth,imgHeight);\r
90 \r
91     if (av.getWrapAlignment())\r
92       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
93     else\r
94       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
95 \r
96 \r
97     g.drawImage(img,0,0,this);\r
98 \r
99   }\r
100 \r
101   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
102   {\r
103       AlignmentI da = av.getAlignment();\r
104 \r
105       int cWidth  =   canvasWidth/av.charWidth;\r
106       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
107 \r
108       int  endx   = startRes+cWidth-1;\r
109       int ypos  = 2*av.charHeight;\r
110 \r
111       while (ypos <= canvasHeight)\r
112       {\r
113         drawScale(g, startRes, endx, ypos);\r
114         drawPanel(g, startRes, endx, 0, da.getHeight(), startRes, 0, ypos);\r
115 \r
116         ypos += cHeight;\r
117         startRes += cWidth;\r
118         endx = startRes + cWidth - 1;\r
119 \r
120         if (endx > da.getWidth())\r
121           endx = da.getWidth();\r
122       }\r
123 \r
124   }\r
125 \r
126 \r
127   public void drawPanel(Graphics g,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
128 \r
129 \r
130     g.setFont(av.getFont());\r
131     int            charWidth  = av.getCharWidth();\r
132     int            charHeight = av.getCharHeight();\r
133     RendererI sr = av.getRenderer();\r
134 \r
135     /*Vector    pid    = av.getConsensus(false);\r
136     Vector tmpseq = new Vector();\r
137     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
138         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
139             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
140 \r
141     if (sr instanceof SequenceRenderer)\r
142         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
143 \r
144     else if (sr instanceof GraphRenderer)\r
145         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
146                                        av.getAlignment().getSequences(),\r
147                                        av.getPIDWindow(),x1,x2);\r
148 \r
149 */\r
150 \r
151    /* if (y2 > starty && y1 < av.getEndSeq())\r
152     {\r
153        fillBackground(g,\r
154                    Color.red,\r
155                    (x1-startx)*charWidth,\r
156                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
157                    (x2-x1+1)*charWidth,\r
158                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
159     }*/\r
160 \r
161     SequenceI nextSeq;\r
162     SequenceGroup group=null;\r
163     SequenceGroup [] groups=null;\r
164     boolean inGroup=false; // are we in a defined group?\r
165     boolean inRB=false; //Are we in a rubberband group?\r
166     int sx=-1, sy=-1, ex=-1, ey=-1;\r
167     int oldStartX=0,oldEndX=0,oldY=0;\r
168     int oldRStartX=0,oldREndX=0,oldRY=0;\r
169     for (int i = y1 ; i < y2 ;i++)\r
170     {\r
171      nextSeq = av.getAlignment().getSequenceAt(i);\r
172 \r
173      group = av.alignment.findGroup( nextSeq );\r
174      groups = av.alignment.findAllGroups( nextSeq );\r
175 \r
176      sr.drawSequence(g, nextSeq, groups,x1,x2,\r
177                  (x1 - startx) * charWidth,\r
178                  offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight()),\r
179                  charWidth,charHeight,null, i);\r
180 \r
181      if(av.showSequenceFeatures)\r
182      {\r
183        fr.drawSequence(g, nextSeq, groups, x1, x2,\r
184                        (x1 - startx) * charWidth,\r
185                        offset +\r
186                        AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight()),\r
187                        charWidth, charHeight, null, i);\r
188      }\r
189 \r
190 \r
191 \r
192 \r
193      if( group!=null )\r
194      {\r
195 \r
196         g.setColor(Color.lightGray);\r
197 \r
198         sx = (group.getStartRes()-startx)*charWidth;\r
199         sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
200         ex = (group.getEndRes()+1-startx)*charWidth;\r
201         ey = offset + AlignmentUtil.getPixelHeight(starty, i+1, av.getCharHeight());\r
202 \r
203 \r
204         if (!inGroup)\r
205         {\r
206           g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
207           inGroup=true;\r
208         }\r
209 \r
210         g.drawLine(sx, sy, sx, ey ); // vertical line, left hand side\r
211         g.drawLine( ex,sy,ex,ey);    // vertical line, right hand side\r
212         if (i == y2 - 1)// last line of alignment, seal the box\r
213           g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
214 \r
215         oldStartX = sx;\r
216         oldEndX = ex;\r
217         oldY = ey;\r
218 \r
219      }\r
220      else if(inGroup)\r
221      {\r
222        g.setColor(Color.lightGray);\r
223        g.drawLine(oldStartX, oldY, oldEndX, oldY); // Horizontal, bottom of old box\r
224      }\r
225      else\r
226        inGroup = false;\r
227 \r
228      group = av.getRubberbandGroup();\r
229      if( group != null && group.sequences.contains(nextSeq) )\r
230      {\r
231          g.setColor(Color.RED.brighter());\r
232          sx = (group.getStartRes() - startx) * charWidth;\r
233          sy = offset +\r
234              AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
235          ex = (group.getEndRes() + 1 - startx) * charWidth;\r
236          ey = offset +\r
237              AlignmentUtil.getPixelHeight(starty, i + 1, av.getCharHeight());\r
238 \r
239          if (!inRB)\r
240          {\r
241 \r
242            g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
243            inRB = true;\r
244          }\r
245 \r
246          g.drawLine(sx, sy, sx, ey); // vertical line, left hand side\r
247          g.drawLine(ex, sy, ex, ey); // vertical line, right hand side\r
248          if (i == y2 - 1) // last line of alignment, seal the box\r
249            g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
250 \r
251           oldRStartX = sx;\r
252           oldREndX = ex;\r
253           oldRY = ey;\r
254      }\r
255      else if(inRB)\r
256      {\r
257        g.setColor(Color.RED.brighter());\r
258        g.drawLine(oldRStartX, oldRY, oldREndX, oldRY); // Horizontal, bottom of old box\r
259        inRB = false;\r
260      }\r
261      else\r
262        inRB = false;\r
263 \r
264     }\r
265 \r
266     /// Highlight search Results once all sequences have been drawn\r
267     if(displaySearch)\r
268     {\r
269       for(int r=0; r<searchResults.length; r+=3)\r
270       {\r
271         int searchSeq = searchResults[r];\r
272         int searchStart = searchResults[r+1];\r
273         int searchEnd = searchResults[r+2];\r
274 \r
275         if (searchSeq >= y1 && searchSeq < y2)\r
276         {\r
277           SequenceRenderer ssr = (SequenceRenderer) sr;\r
278           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
279                                   searchStart,\r
280                                   searchEnd,\r
281                                   (searchStart - startx) * charWidth,\r
282                                   offset +\r
283                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
284               charHeight),\r
285                                   charWidth,\r
286                                   charHeight);\r
287         }\r
288       }\r
289     }\r
290 \r
291   }\r
292 \r
293   public void fillBackground(Graphics g,Color c, int x1,int y1,int width,int height) {\r
294     g.setColor(c);\r
295     g.fillRect(x1,y1,width,height);\r
296   }\r
297 \r
298   public int getChunkWidth() {\r
299     return chunkWidth;\r
300   }\r
301 \r
302   public void highlightSearchResults(int [] results)\r
303   {\r
304     // results are in the order sequence, startRes, endRes\r
305     if(results==null)\r
306       displaySearch = false;\r
307     else\r
308       displaySearch = true;\r
309 \r
310     searchResults = results;\r
311 \r
312     paintFlag = true;\r
313     repaint();\r
314   }\r
315 \r
316 \r
317 }\r