Applet PDB viewer
[jalview.git] / src / MCview / AppletPDBCanvas.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package MCview;\r
20 \r
21 import jalview.analysis.AlignSeq;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug\r
26 import java.awt.*;\r
27 import java.awt.event.*;\r
28 \r
29 import java.io.*;\r
30 \r
31 import java.util.*;\r
32 \r
33 \r
34 \r
35 public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener\r
36 {\r
37     MCMatrix idmat = new MCMatrix(3, 3);\r
38     MCMatrix objmat = new MCMatrix(3, 3);\r
39     boolean redrawneeded = true;\r
40     int omx = 0;\r
41     int mx = 0;\r
42     int omy = 0;\r
43     int my = 0;\r
44     public PDBfile pdb;\r
45     int bsize;\r
46     Image img;\r
47     Graphics ig;\r
48     Dimension prefsize;\r
49     float[] centre = new float[3];\r
50     float[] width = new float[3];\r
51     float maxwidth;\r
52     float scale;\r
53     String inStr;\r
54     String inType;\r
55     boolean bysequence = true;\r
56     boolean depthcue = true;\r
57     boolean wire = false;\r
58     boolean bymolecule = false;\r
59     boolean zbuffer = true;\r
60     boolean dragging;\r
61     int xstart;\r
62     int xend;\r
63     int ystart;\r
64     int yend;\r
65     int xmid;\r
66     int ymid;\r
67     Font font = new Font("Helvetica", Font.PLAIN, 10);\r
68     jalview.appletgui.SequenceRenderer sr;\r
69     jalview.appletgui.FeatureRenderer  fr;\r
70     jalview.appletgui.SeqCanvas seqcanvas;\r
71     Sequence sequence;\r
72     final StringBuffer mappingDetails = new StringBuffer();\r
73     String appletToolTip = null;\r
74     int toolx, tooly;\r
75 \r
76     public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)\r
77     {\r
78       this.seqcanvas = seqcanvas;\r
79       this.sequence = seq;\r
80       sr = seqcanvas.getSequenceRenderer();\r
81       fr = seqcanvas.getFeatureRenderer();\r
82 \r
83       seqcanvas.setPDBViewer(this);\r
84       addKeyListener(new KeyAdapter()\r
85       {\r
86 \r
87         public void keyPressed(KeyEvent evt)\r
88         {\r
89           doKeyPressed(evt);\r
90         }\r
91       });\r
92 \r
93     }\r
94 \r
95   public void setPDBFile(PDBfile pdb)\r
96    {\r
97         this.sr = sr;\r
98         this.fr = fr;\r
99         int max = -10;\r
100         int maxchain = -1;\r
101         int pdbstart = 0;\r
102         int pdbend = 0;\r
103         int seqstart = 0;\r
104         int seqend = 0;\r
105 \r
106         for (int i = 0; i < pdb.chains.size(); i++)\r
107         {\r
108 \r
109           mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence());\r
110           mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");\r
111 \r
112             // Now lets compare the sequences to get\r
113             // the start and end points.\r
114             // Align the sequence to the pdb\r
115             AlignSeq as = new AlignSeq(sequence,\r
116                     ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");\r
117             as.calcScoreMatrix();\r
118             as.traceAlignment();\r
119             PrintStream  ps = new PrintStream(System.out)\r
120            {\r
121               public void print(String x) {\r
122                    mappingDetails.append(x);\r
123                }\r
124                public void println()\r
125                {\r
126                  mappingDetails.append("\n");\r
127                }\r
128             };\r
129 \r
130             as.printAlignment(ps);\r
131 \r
132             if (as.maxscore > max) {\r
133                 max = as.maxscore;\r
134                 maxchain = i;\r
135 \r
136                 pdbstart = as.seq2start;\r
137                 pdbend = as.seq2end;\r
138                 seqstart = as.seq1start + sequence.getStart()-1;\r
139                 seqend = as.seq1end + sequence.getEnd()-1;\r
140             }\r
141 \r
142             mappingDetails.append("\nPDB start/end "  + pdbstart + " " + pdbend);\r
143             mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);\r
144         }\r
145 \r
146         ((PDBChain) pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;\r
147         ((PDBChain) pdb.chains.elementAt(maxchain)).pdbend = pdbend;\r
148         ((PDBChain) pdb.chains.elementAt(maxchain)).seqstart = seqstart;\r
149         ((PDBChain) pdb.chains.elementAt(maxchain)).seqend = seqend;\r
150         ((PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;\r
151         ((PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;\r
152 \r
153         this.pdb = pdb;\r
154         this.prefsize = new Dimension(getSize().width, getSize().height);\r
155 \r
156         //Initialize the matrices to identity\r
157         for (int i = 0; i < 3; i++) {\r
158             for (int j = 0; j < 3; j++) {\r
159                 if (i != j) {\r
160                     idmat.addElement(i, j, 0);\r
161                     objmat.addElement(i, j, 0);\r
162                 } else {\r
163                     idmat.addElement(i, j, 1);\r
164                     objmat.addElement(i, j, 1);\r
165                 }\r
166             }\r
167         }\r
168 \r
169         addMouseMotionListener(this);\r
170         addMouseListener(this);\r
171 \r
172       /*\r
173        SequenceGroup sg = new SequenceGroup("PDB",\r
174                                             null, true,true,false,\r
175                                             sequence.findIndex(seqstart-1),\r
176                                             sequence.findIndex(seqend-1));\r
177        sg.addSequence(sequence, false);\r
178        sg.setOutlineColour(Color.black);\r
179        seqcanvas.getViewport().getAlignment().addGroup(sg);\r
180        */\r
181 \r
182         findCentre();\r
183         findWidth();\r
184 \r
185         scale = findScale();\r
186 \r
187         updateSeqColours();\r
188     }\r
189 \r
190     public void deleteBonds() {\r
191         scale = 0;\r
192         maxwidth = 0;\r
193 \r
194         width[0] = 0;\r
195         width[1] = 0;\r
196         width[2] = 0;\r
197 \r
198         centre[0] = 0;\r
199         centre[1] = 0;\r
200         centre[2] = 0;\r
201 \r
202         for (int i = 0; i < pdb.chains.size(); i++) {\r
203             ((PDBChain) pdb.chains.elementAt(i)).bonds = null;\r
204         }\r
205     }\r
206 \r
207     public void findWidth() {\r
208         float[] max = new float[3];\r
209         float[] min = new float[3];\r
210 \r
211         max[0] = (float) -1e30;\r
212         max[1] = (float) -1e30;\r
213         max[2] = (float) -1e30;\r
214 \r
215         min[0] = (float) 1e30;\r
216         min[1] = (float) 1e30;\r
217         min[2] = (float) 1e30;\r
218 \r
219         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
220             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
221                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
222 \r
223                 for (int i = 0; i < bonds.size(); i++) {\r
224                     Bond tmp = (Bond) bonds.elementAt(i);\r
225 \r
226                     if (tmp.start[0] >= max[0]) {\r
227                         max[0] = tmp.start[0];\r
228                     }\r
229 \r
230                     if (tmp.start[1] >= max[1]) {\r
231                         max[1] = tmp.start[1];\r
232                     }\r
233 \r
234                     if (tmp.start[2] >= max[2]) {\r
235                         max[2] = tmp.start[2];\r
236                     }\r
237 \r
238                     if (tmp.start[0] <= min[0]) {\r
239                         min[0] = tmp.start[0];\r
240                     }\r
241 \r
242                     if (tmp.start[1] <= min[1]) {\r
243                         min[1] = tmp.start[1];\r
244                     }\r
245 \r
246                     if (tmp.start[2] <= min[2]) {\r
247                         min[2] = tmp.start[2];\r
248                     }\r
249 \r
250                     if (tmp.end[0] >= max[0]) {\r
251                         max[0] = tmp.end[0];\r
252                     }\r
253 \r
254                     if (tmp.end[1] >= max[1]) {\r
255                         max[1] = tmp.end[1];\r
256                     }\r
257 \r
258                     if (tmp.end[2] >= max[2]) {\r
259                         max[2] = tmp.end[2];\r
260                     }\r
261 \r
262                     if (tmp.end[0] <= min[0]) {\r
263                         min[0] = tmp.end[0];\r
264                     }\r
265 \r
266                     if (tmp.end[1] <= min[1]) {\r
267                         min[1] = tmp.end[1];\r
268                     }\r
269 \r
270                     if (tmp.end[2] <= min[2]) {\r
271                         min[2] = tmp.end[2];\r
272                     }\r
273                 }\r
274             }\r
275         }\r
276 \r
277         width[0] = (float) Math.abs(max[0] - min[0]);\r
278         width[1] = (float) Math.abs(max[1] - min[1]);\r
279         width[2] = (float) Math.abs(max[2] - min[2]);\r
280 \r
281         maxwidth = width[0];\r
282 \r
283         if (width[1] > width[0]) {\r
284             maxwidth = width[1];\r
285         }\r
286 \r
287         if (width[2] > width[1]) {\r
288             maxwidth = width[2];\r
289         }\r
290 \r
291        // System.out.println("Maxwidth = " + maxwidth);\r
292     }\r
293 \r
294     public float findScale() {\r
295         int dim;\r
296         int width;\r
297         int height;\r
298 \r
299         if (getSize().width != 0) {\r
300             width = getSize().width;\r
301             height = getSize().height;\r
302         } else {\r
303             width = prefsize.width;\r
304             height = prefsize.height;\r
305         }\r
306 \r
307         if (width < height) {\r
308             dim = width;\r
309         } else {\r
310             dim = height;\r
311         }\r
312 \r
313         return (float) (dim / (1.5d * maxwidth));\r
314     }\r
315 \r
316     public void findCentre() {\r
317         float xtot = 0;\r
318         float ytot = 0;\r
319         float ztot = 0;\r
320 \r
321         int bsize = 0;\r
322 \r
323         //Find centre coordinate\r
324         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
325             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
326                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
327 \r
328                 bsize += bonds.size();\r
329 \r
330                 for (int i = 0; i < bonds.size(); i++) {\r
331                     xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +\r
332                         ((Bond) bonds.elementAt(i)).end[0];\r
333 \r
334                     ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +\r
335                         ((Bond) bonds.elementAt(i)).end[1];\r
336 \r
337                     ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +\r
338                         ((Bond) bonds.elementAt(i)).end[2];\r
339                 }\r
340             }\r
341         }\r
342 \r
343         centre[0] = xtot / (2 * (float) bsize);\r
344         centre[1] = ytot / (2 * (float) bsize);\r
345         centre[2] = ztot / (2 * (float) bsize);\r
346     }\r
347 \r
348     public void paint(Graphics g)\r
349     {\r
350 \r
351       if(pdb==null)\r
352       {\r
353         g.setColor(Color.black);\r
354         g.setFont(new Font("Verdana", Font.BOLD, 14));\r
355         g.drawString("Error Parsing Pasted PDB data!!", 50, getSize().height/2);\r
356         return;\r
357       }\r
358 \r
359 \r
360         //Only create the image at the beginning -\r
361         //this saves much memory usage\r
362         if ((img == null) || (prefsize.width != getSize().width) ||\r
363                 (prefsize.height != getSize().height)) {\r
364             prefsize.width = getSize().width;\r
365             prefsize.height = getSize().height;\r
366 \r
367             scale = findScale();\r
368             img = createImage(prefsize.width, prefsize.height);\r
369             ig = img.getGraphics();\r
370 \r
371             redrawneeded = true;\r
372         }\r
373 \r
374 \r
375         if (redrawneeded)\r
376         {\r
377           drawAll(ig, prefsize.width, prefsize.height);\r
378           redrawneeded = false;\r
379         }\r
380         if(appletToolTip!=null)\r
381         {\r
382           ig.setColor(Color.red);\r
383           ig.drawString(appletToolTip, toolx, tooly);\r
384         }\r
385 \r
386         g.drawImage(img, 0, 0, this);\r
387 \r
388 \r
389     }\r
390 \r
391     public void drawAll(Graphics g, int width, int height)\r
392     {\r
393       g.setColor(Color.black);\r
394       g.fillRect(0, 0, width, height);\r
395       drawScene(g);\r
396       drawLabels(g);\r
397     }\r
398 \r
399 \r
400     public void updateSeqColours()\r
401     {\r
402       if(bysequence && pdb!=null)\r
403       {\r
404         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
405         {\r
406           colourBySequence((PDBChain) pdb.chains.elementAt(ii));\r
407         }\r
408       }\r
409 \r
410       redrawneeded=true;\r
411       repaint();\r
412     }\r
413 \r
414     // This method has been taken out of PDBChain to allow\r
415     // Applet and Application specific sequence renderers to be used\r
416     void colourBySequence(PDBChain chain)\r
417     {\r
418       for (int i = 0; i < chain.bonds.size(); i++)\r
419       {\r
420         Bond tmp = (Bond) chain.bonds.elementAt(i);\r
421 \r
422         if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&\r
423             (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))\r
424         {\r
425           int pos = chain.seqstart +\r
426               (tmp.at1.resNumber - chain.pdbstart - chain.offset);\r
427 \r
428           int index = sequence.findIndex(pos);\r
429 \r
430           tmp.startCol = sr.findSequenceColour(sequence, index);\r
431 \r
432         //  tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);\r
433 \r
434         }\r
435         else\r
436         {\r
437           tmp.startCol = Color.gray;\r
438         }\r
439 \r
440         if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&\r
441             (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))\r
442         {\r
443           int pos = chain.seqstart +\r
444               (tmp.at2.resNumber - chain.pdbstart - chain.offset);\r
445           int index = sequence.findIndex(pos);\r
446 \r
447           tmp.endCol = sr.findSequenceColour( sequence, index);\r
448       //    tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);\r
449         }\r
450         else\r
451         {\r
452           tmp.endCol = Color.gray;\r
453         }\r
454       }\r
455     }\r
456 \r
457 \r
458     public void drawScene(Graphics g) {\r
459         // Sort the bonds by z coord\r
460         Vector bonds = new Vector();\r
461 \r
462         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
463         {\r
464           if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)\r
465           {\r
466             Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;\r
467 \r
468             for (int i = 0; i < tmp.size(); i++)\r
469             {\r
470               bonds.addElement(tmp.elementAt(i));\r
471             }\r
472           }\r
473         }\r
474 \r
475         if (zbuffer) {\r
476             Zsort.Zsort(bonds);\r
477         }\r
478 \r
479         for (int i = 0; i < bonds.size(); i++) {\r
480             Bond tmpBond = (Bond) bonds.elementAt(i);\r
481 \r
482             xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
483                 (getSize().width / 2));\r
484             ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
485                 (getSize().height / 2));\r
486 \r
487             xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +\r
488                 (getSize().width / 2));\r
489             yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +\r
490                 (getSize().height / 2));\r
491 \r
492             xmid = (xend + xstart) / 2;\r
493             ymid = (yend + ystart) / 2;\r
494 \r
495             if (depthcue && !bymolecule) {\r
496                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
497                     g.setColor(tmpBond.startCol.darker().darker());\r
498                     drawLine(g, xstart, ystart, xmid, ymid);\r
499 \r
500                     g.setColor(tmpBond.endCol.darker().darker());\r
501                     drawLine(g, xmid, ymid, xend, yend);\r
502                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
503                     g.setColor(tmpBond.startCol.darker());\r
504                     drawLine(g, xstart, ystart, xmid, ymid);\r
505 \r
506                     g.setColor(tmpBond.endCol.darker());\r
507                     drawLine(g, xmid, ymid, xend, yend);\r
508                 } else {\r
509                     g.setColor(tmpBond.startCol);\r
510                     drawLine(g, xstart, ystart, xmid, ymid);\r
511 \r
512                     g.setColor(tmpBond.endCol);\r
513                     drawLine(g, xmid, ymid, xend, yend);\r
514                 }\r
515             } else if (depthcue && bymolecule) {\r
516                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
517                     g.setColor(Color.green.darker().darker());\r
518                     drawLine(g, xstart, ystart, xend, yend);\r
519                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
520                     g.setColor(Color.green.darker());\r
521                     drawLine(g, xstart, ystart, xend, yend);\r
522                 } else {\r
523                     g.setColor(Color.green);\r
524                     drawLine(g, xstart, ystart, xend, yend);\r
525                 }\r
526             } else if (!depthcue && !bymolecule) {\r
527                 g.setColor(tmpBond.startCol);\r
528                 drawLine(g, xstart, ystart, xmid, ymid);\r
529                 g.setColor(tmpBond.endCol);\r
530                 drawLine(g, xmid, ymid, xend, yend);\r
531             } else {\r
532                 drawLine(g, xstart, ystart, xend, yend);\r
533             }\r
534         }\r
535     }\r
536 \r
537     public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {\r
538         if (!wire) {\r
539             if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {\r
540                 g.drawLine(x1, y1, x2, y2);\r
541                 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);\r
542                 g.drawLine(x1, y1 - 1, x2, y2 - 1);\r
543             } else {\r
544                 g.setColor(g.getColor().brighter());\r
545                 g.drawLine(x1, y1, x2, y2);\r
546                 g.drawLine(x1 + 1, y1, x2 + 1, y2);\r
547                 g.drawLine(x1 - 1, y1, x2 - 1, y2);\r
548             }\r
549         } else {\r
550             g.drawLine(x1, y1, x2, y2);\r
551         }\r
552     }\r
553 \r
554     public Dimension minimumsize() {\r
555         return prefsize;\r
556     }\r
557 \r
558     public Dimension preferredsize() {\r
559         return prefsize;\r
560     }\r
561 \r
562     public void doKeyPressed(KeyEvent evt)\r
563     {\r
564       if (evt.getKeyCode() == KeyEvent.VK_UP)\r
565       {\r
566         scale = (float) (scale * 1.1);\r
567         redrawneeded = true;\r
568         repaint();\r
569       }\r
570       else if (evt.getKeyCode() == KeyEvent.VK_DOWN)\r
571       {\r
572         scale = (float) (scale * 0.9);\r
573         redrawneeded = true;\r
574         repaint();\r
575       }\r
576     }\r
577 \r
578     public void mousePressed(MouseEvent e) {\r
579         myAtom fatom = findAtom(e.getX(), e.getY());\r
580         if(fatom!=null)\r
581         {\r
582           fatom.isSelected = !fatom.isSelected;\r
583           redrawneeded = true;\r
584           repaint();\r
585         }\r
586         mx = e.getX();\r
587         my = e.getY();\r
588         omx = mx;\r
589         omy = my;\r
590         dragging = false;\r
591     }\r
592 \r
593     public void mouseMoved(MouseEvent e) {\r
594 \r
595         myAtom fatom = findAtom(e.getX(), e.getY());\r
596 \r
597         if(foundchain!=-1)\r
598         {\r
599           PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
600           int pos = chain.seqstart +\r
601               (fatom.resNumber - chain.pdbstart - chain.offset)+1;\r
602 \r
603           int index = seqcanvas.getViewport().getAlignment().findIndex(sequence);\r
604 \r
605           seqcanvas.highlightSearchResults(new int[]{index, pos, pos});\r
606         }\r
607         else\r
608           seqcanvas.highlightSearchResults(null);\r
609 \r
610         if (fatom != null) {\r
611             toolx = e.getX();\r
612             tooly = e.getY();\r
613             appletToolTip = fatom.resNumber+" "+ fatom.resName;\r
614             redrawneeded = true;\r
615             repaint();\r
616         } else {\r
617             appletToolTip = null;\r
618             redrawneeded = true;\r
619             repaint();\r
620         }\r
621     }\r
622 \r
623     public void mouseClicked(MouseEvent e) {\r
624     }\r
625 \r
626     public void mouseEntered(MouseEvent e) {\r
627     }\r
628 \r
629     public void mouseExited(MouseEvent e) {\r
630     }\r
631 \r
632     public void mouseDragged(MouseEvent evt) {\r
633         int x = evt.getX();\r
634         int y = evt.getY();\r
635         mx = x;\r
636         my = y;\r
637 \r
638         MCMatrix objmat = new MCMatrix(3, 3);\r
639         objmat.setIdentity();\r
640 \r
641         if ((evt.getModifiers() & Event.META_MASK) != 0) {\r
642             objmat.rotatez((float) ((mx - omx)));\r
643         } else {\r
644             objmat.rotatex((float) ((my - omy)));\r
645             objmat.rotatey((float) ((omx - mx)));\r
646         }\r
647 \r
648         //Alter the bonds\r
649         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
650             Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
651 \r
652             for (int i = 0; i < bonds.size(); i++) {\r
653                 Bond tmpBond = (Bond) bonds.elementAt(i);\r
654 \r
655                 //Translate the bond so the centre is 0,0,0\r
656                 tmpBond.translate(-centre[0], -centre[1], -centre[2]);\r
657 \r
658                 //Now apply the rotation matrix\r
659                 tmpBond.start = objmat.vectorMultiply(tmpBond.start);\r
660                 tmpBond.end = objmat.vectorMultiply(tmpBond.end);\r
661 \r
662                 //Now translate back again\r
663                 tmpBond.translate(centre[0], centre[1], centre[2]);\r
664             }\r
665         }\r
666 \r
667         objmat = null;\r
668 \r
669         omx = mx;\r
670         omy = my;\r
671 \r
672         dragging = true;\r
673 \r
674         redrawneeded = true;\r
675 \r
676         repaint();\r
677     }\r
678 \r
679     public void mouseReleased(MouseEvent evt) {\r
680         dragging = false;\r
681         return;\r
682     }\r
683 \r
684     void drawLabels(Graphics g) {\r
685 \r
686         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
687         {\r
688             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
689 \r
690             if (chain.isVisible)\r
691             {\r
692                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
693 \r
694                 for (int i = 0; i < bonds.size(); i++)\r
695                 {\r
696                     Bond tmpBond = (Bond) bonds.elementAt(i);\r
697 \r
698                     if (tmpBond.at1.isSelected)\r
699                     {\r
700                         labelAtom(g, tmpBond, 1);\r
701                     }\r
702 \r
703                     if (tmpBond.at2.isSelected)\r
704                     {\r
705 \r
706                         labelAtom(g, tmpBond, 2);\r
707                     }\r
708                 }\r
709             }\r
710         }\r
711     }\r
712 \r
713     public void labelAtom(Graphics g, Bond b, int n) {\r
714         g.setFont(font);\r
715 \r
716         if (n == 1) {\r
717             int xstart = (int) (((b.start[0] - centre[0]) * scale) +\r
718                 (getSize().width / 2));\r
719             int ystart = (int) (((b.start[1] - centre[1]) * scale) +\r
720                 (getSize().height / 2));\r
721 \r
722             g.setColor(Color.red);\r
723             g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);\r
724         }\r
725 \r
726         if (n == 2) {\r
727             int xstart = (int) (((b.end[0] - centre[0]) * scale) +\r
728                 (getSize().width / 2));\r
729             int ystart = (int) (((b.end[1] - centre[1]) * scale) +\r
730                 (getSize().height / 2));\r
731 \r
732             g.setColor(Color.red);\r
733             g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);\r
734         }\r
735     }\r
736 \r
737     int foundchain = -1;\r
738     public myAtom findAtom(int x, int y) {\r
739         myAtom fatom = null;\r
740 \r
741         foundchain = -1;\r
742 \r
743         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
744             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
745 \r
746             if (chain.isVisible) {\r
747                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
748 \r
749                 for (int i = 0; i < bonds.size(); i++) {\r
750                     Bond tmpBond = (Bond) bonds.elementAt(i);\r
751 \r
752                     int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
753                         (getSize().width / 2));\r
754 \r
755                     if (Math.abs(truex - x) <= 2) {\r
756                         int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
757                             (getSize().height / 2));\r
758 \r
759                         if (Math.abs(truey - y) <= 2)\r
760                         {\r
761                             fatom = tmpBond.at1;\r
762                             foundchain = ii;\r
763                             break;\r
764                         }\r
765                     }\r
766                 }\r
767             }\r
768 \r
769             if (fatom != null) //)&& chain.ds != null)\r
770              {\r
771                 chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
772             }\r
773         }\r
774 \r
775         return fatom;\r
776     }\r
777 \r
778     public void update(Graphics g)\r
779     {\r
780       paint(g);\r
781     }\r
782 }\r