a1651290b8b0bdd3593bc2f939bc20609b173c35
[jalview.git] / src / MCview / rotCanvas.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 \r
20 package MCview;\r
21 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug\r
22 import java.awt.*;\r
23 import java.awt.event.*;\r
24 import java.util.*;\r
25 import java.io.*;\r
26 import javax.swing.*;\r
27 import jalview.analysis.AlignSeq;\r
28 import jalview.datamodel.*;\r
29 \r
30 public class rotCanvas extends JPanel implements KeyListener,\r
31                                                  MouseListener,\r
32                                                  MouseMotionListener {\r
33   MCMatrix  idmat  = new MCMatrix(3,3);\r
34   MCMatrix  objmat = new MCMatrix(3,3);\r
35 \r
36   boolean redrawneeded =true;\r
37 \r
38   int omx = 0;\r
39   int mx = 0;\r
40   int omy = 0;\r
41   int my = 0;\r
42 \r
43   public PDBfile pdb;\r
44 \r
45   int bsize;\r
46 \r
47   Image img;\r
48   Graphics ig;\r
49 \r
50   Dimension prefsize;\r
51 \r
52   float centre[] = new float[3];\r
53   float width[]  = new float[3];\r
54 \r
55   float maxwidth;\r
56   float scale;\r
57 \r
58   String inStr;\r
59   String inType;\r
60 \r
61   boolean depthcue   = true;\r
62   boolean wire       = false;\r
63   boolean bymolecule = false;\r
64   boolean zbuffer    = true;\r
65 \r
66   boolean dragging;\r
67 \r
68   int xstart;\r
69   int xend;\r
70   int ystart;\r
71   int yend;\r
72   int xmid;\r
73   int ymid;\r
74 \r
75   Font font = new Font("Helvetica",Font.PLAIN,10);\r
76 \r
77   public rotCanvas(PDBfile pdb, Sequence sequence, jalview.gui.AlignViewport av) throws IOException {\r
78 \r
79     int max = -10;\r
80     int maxchain = -1;\r
81     int pdbstart=0, pdbend=0,  seqstart =0,  seqend=0;\r
82 \r
83 \r
84     for (int i=0; i < pdb.chains.size(); i++) {\r
85       // Now lets compare the sequences to get\r
86       // the start and end points.\r
87 \r
88     java.util.StringTokenizer str = new java.util.StringTokenizer(sequence.getSequence(), ".");\r
89     String newString = "";\r
90 \r
91     while (str.hasMoreTokens()) {\r
92         newString += str.nextToken();\r
93     }\r
94       // Align the sequence to the pdb\r
95       AlignSeq as = new AlignSeq(sequence,((PDBChain)pdb.chains.elementAt(i)).sequence,"pep");\r
96       as.calcScoreMatrix();\r
97       as.traceAlignment();\r
98       as.printAlignment();\r
99 \r
100       if (as.maxscore > max) {\r
101         max = as.maxscore;\r
102         maxchain = i;\r
103 \r
104         pdbstart = as.seq2start;\r
105         pdbend = as.seq2end;\r
106         seqstart = as.seq1start  - 1 ;\r
107         seqend = as.seq1end  -1;\r
108       }\r
109 \r
110       System.out.println("PDB start/end " + pdbstart + " " + pdbend);\r
111       System.out.println("SEQ start/end " + seqstart + " " + seqend);\r
112     }\r
113 \r
114 \r
115     ( (PDBChain)pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;\r
116     ( (PDBChain)pdb.chains.elementAt(maxchain)).pdbend = pdbend;\r
117     ( (PDBChain)pdb.chains.elementAt(maxchain)).seqstart = seqstart;\r
118      ( (PDBChain)pdb.chains.elementAt(maxchain)).seqend = seqend;\r
119      ( (PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;\r
120      ( (PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;\r
121      ( (PDBChain)pdb.chains.elementAt(maxchain)).colourBySequence(av, sequence);\r
122 \r
123 \r
124 \r
125     this.pdb      = pdb;\r
126     this.prefsize = new Dimension( getWidth(), getHeight());\r
127 \r
128     //Initialize the matrices to identity\r
129 \r
130     for (int i = 0; i < 3; i++) {\r
131       for (int j = 0; j < 3 ; j++) {\r
132         if (i != j) {\r
133           idmat.addElement(i,j,0);\r
134           objmat.addElement(i,j,0);\r
135         } else {\r
136           idmat.addElement(i,j,1);\r
137           objmat.addElement(i,j,1);\r
138         }\r
139       }\r
140     }\r
141 \r
142     addMouseMotionListener(this);\r
143     addMouseListener(this);\r
144     addKeyListener(this);\r
145 \r
146     addPDBfile();\r
147     ToolTipManager.sharedInstance().registerComponent(this);\r
148 \r
149   }\r
150 \r
151   public void addPDBfile() {\r
152     findCentre();\r
153     findWidth();\r
154 \r
155     scale = findScale();\r
156 \r
157     System.out.println("Scale factor = " + scale);\r
158   }\r
159 \r
160   public void deleteBonds() {\r
161     scale     = 0;\r
162     maxwidth  = 0;\r
163 \r
164     width[0] = 0;\r
165     width[1] = 0;\r
166     width[2] = 0;\r
167 \r
168     centre[0] = 0;\r
169     centre[1] = 0;\r
170     centre[2] = 0;\r
171 \r
172     for (int i=0; i < pdb.chains.size(); i++) {\r
173       ((PDBChain)pdb.chains.elementAt(i)).bonds = null;\r
174     }\r
175   }\r
176 \r
177   public void findWidth() {\r
178     float max[] = new float[3];\r
179     float min[] = new float[3];\r
180 \r
181     max[0] = (float)-1e30;\r
182     max[1] = (float)-1e30;\r
183     max[2] = (float)-1e30;\r
184 \r
185     min[0] = (float)1e30;\r
186     min[1] = (float)1e30;\r
187     min[2] = (float)1e30;\r
188 \r
189     for (int ii=0; ii < pdb.chains.size(); ii++) {\r
190       if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {\r
191 \r
192         Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
193 \r
194         for (int i = 0; i < bonds.size(); i++) {\r
195           Bond tmp = (Bond)bonds.elementAt(i);\r
196 \r
197           if (tmp.start[0] >= max[0])\r
198             max[0] = tmp.start[0];\r
199           if (tmp.start[1] >= max[1])\r
200             max[1] = tmp.start[1];\r
201           if (tmp.start[2] >= max[2])\r
202             max[2] = tmp.start[2];\r
203           if (tmp.start[0] <= min[0])\r
204             min[0] = tmp.start[0];\r
205           if (tmp.start[1] <= min[1])\r
206             min[1] = tmp.start[1];\r
207           if (tmp.start[2] <= min[2])\r
208             min[2] = tmp.start[2];\r
209 \r
210           if (tmp.end[0] >= max[0])\r
211             max[0] = tmp.end[0];\r
212           if (tmp.end[1] >= max[1])\r
213             max[1] = tmp.end[1];\r
214           if (tmp.end[2] >= max[2])\r
215             max[2] = tmp.end[2];\r
216           if (tmp.end[0] <= min[0])\r
217             min[0] = tmp.end[0];\r
218           if (tmp.end[1] <= min[1])\r
219             min[1] = tmp.end[1];\r
220           if (tmp.end[2] <= min[2])\r
221             min[2] = tmp.end[2];\r
222         }\r
223       }\r
224     }\r
225 \r
226     System.out.println("xmax " + max[0] + " min " + min[0]);\r
227     System.out.println("ymax " + max[1] + " min " + min[1]);\r
228     System.out.println("zmax " + max[2] + " min " + min[2]);\r
229 \r
230     width[0] = (float)Math.abs(max[0] - min[0]);\r
231     width[1] = (float)Math.abs(max[1] - min[1]);\r
232     width[2] = (float)Math.abs(max[2] - min[2]);\r
233 \r
234     maxwidth = width[0];\r
235 \r
236     if (width[1] > width[0])\r
237       maxwidth = width[1];\r
238     if (width[2] > width[1])\r
239       maxwidth = width[2];\r
240 \r
241     System.out.println("Maxwidth = " + maxwidth);\r
242   }\r
243 \r
244   public float findScale() {\r
245     int dim, width, height;\r
246 \r
247     if ( getWidth() != 0) {\r
248       width  = getWidth();\r
249       height = getHeight();\r
250 \r
251     } else {\r
252       width  = prefsize.width;\r
253       height = prefsize.height;\r
254     }\r
255 \r
256     if (width < height) {\r
257       dim = width;\r
258     } else {\r
259       dim = height;\r
260     }\r
261 \r
262     return (float)(dim/(1.5d*maxwidth));\r
263   }\r
264 \r
265 \r
266 \r
267   public void findCentre() {\r
268     float xtot = 0;\r
269     float ytot = 0;\r
270     float ztot = 0;\r
271 \r
272     int bsize  = 0;\r
273     //Find centre coordinate\r
274 \r
275     for (int ii = 0; ii < pdb.chains.size() ; ii++) {\r
276       if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {\r
277 \r
278         Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
279 \r
280         bsize += bonds.size();\r
281 \r
282         for (int i = 0; i < bonds.size(); i++) {\r
283 \r
284           xtot = xtot + ((Bond)bonds.elementAt(i)).start[0] +\r
285                  ((Bond)bonds.elementAt(i)).end[0];\r
286 \r
287           ytot = ytot + ((Bond)bonds.elementAt(i)).start[1] +\r
288                  ((Bond)bonds.elementAt(i)).end[1];\r
289 \r
290           ztot = ztot + ((Bond)bonds.elementAt(i)).start[2] +\r
291                  ((Bond)bonds.elementAt(i)).end[2];\r
292         }\r
293       }\r
294     }\r
295 \r
296     centre[0] = xtot / (2 * (float)bsize);\r
297     centre[1] = ytot / (2 * (float)bsize);\r
298     centre[2] = ztot / (2 * (float)bsize);\r
299   }\r
300 \r
301   public void paint(Graphics g) {\r
302     //Only create the image at the beginning -\r
303     //this saves much memory usage\r
304     if ((img == null) || (prefsize.width != getWidth()) || (prefsize.height != getHeight())) {\r
305       prefsize.width  = getWidth();\r
306       prefsize.height = getHeight();\r
307 \r
308       scale = findScale();\r
309       img   = createImage(prefsize.width,prefsize.height);\r
310       ig    = img.getGraphics();\r
311 \r
312       redrawneeded = true;\r
313     }\r
314 \r
315     if (redrawneeded == true) {\r
316       drawBackground(ig,Color.black);\r
317       drawScene(ig);\r
318       redrawneeded = false;\r
319     } else {\r
320       ig = img.getGraphics();\r
321     }\r
322 \r
323     g.drawImage(img,0,0,this);\r
324   }\r
325 \r
326   public void drawBackground(Graphics g, Color col) {\r
327     g.setColor(col);\r
328     g.fillRect(0,0,prefsize.width,prefsize.height);\r
329   }\r
330 \r
331 \r
332   public void drawScene(Graphics g) {\r
333     // Sort the bonds by z coord\r
334 \r
335     Vector bonds = new Vector();\r
336 \r
337 \r
338     for (int ii = 0; ii < pdb.chains.size() ; ii++) {\r
339       if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {\r
340 \r
341         Vector tmp = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
342 \r
343         for (int i=0; i < tmp.size(); i++) {\r
344           bonds.addElement(tmp.elementAt(i));\r
345         }\r
346       }\r
347     }\r
348 \r
349     if (zbuffer) {\r
350       Zsort.Zsort(bonds);\r
351     }\r
352 \r
353     for (int i = 0; i < bonds.size(); i++) {\r
354       Bond tmpBond = (Bond)bonds.elementAt(i);\r
355 \r
356       xstart = (int)((tmpBond.start[0] - centre[0])*scale + getWidth()/2);\r
357       ystart = (int)((tmpBond.start[1] - centre[1])*scale + getHeight()/2);\r
358 \r
359       xend = (int)((tmpBond.end[0] - centre[0])*scale + getWidth()/2);\r
360       yend = (int)((tmpBond.end[1] - centre[1])*scale + getHeight()/2);\r
361 \r
362       xmid = (xend+xstart)/2;\r
363       ymid = (yend+ystart)/2;\r
364 \r
365 \r
366       if (depthcue && !bymolecule) {\r
367 \r
368         if (tmpBond.start[2] < (centre[2] - maxwidth/6))  {\r
369           g.setColor(tmpBond.startCol.darker().darker());\r
370           drawLine(g,xstart,ystart,xmid,ymid);\r
371 \r
372           g.setColor(tmpBond.endCol.darker().darker());\r
373           drawLine(g,xmid,ymid,xend,yend);\r
374         } else if (tmpBond.start[2] < (centre[2]+maxwidth/6)) {\r
375           g.setColor(tmpBond.startCol.darker());\r
376           drawLine(g,xstart,ystart,xmid,ymid);\r
377 \r
378           g.setColor(tmpBond.endCol.darker());\r
379           drawLine(g,xmid,ymid,xend,yend);\r
380         } else {\r
381           g.setColor(tmpBond.startCol);\r
382           drawLine(g,xstart,ystart,xmid,ymid);\r
383 \r
384           g.setColor(tmpBond.endCol);\r
385           drawLine(g,xmid,ymid,xend,yend);\r
386         }\r
387       } else if (depthcue && bymolecule) {\r
388         if (tmpBond.start[2] < (centre[2] - maxwidth/6))  {\r
389           g.setColor(Color.green.darker().darker());\r
390           drawLine(g,xstart,ystart,xend,yend);\r
391 \r
392         } else if (tmpBond.start[2] < (centre[2]+maxwidth/6)) {\r
393           g.setColor(Color.green.darker());\r
394           drawLine(g,xstart,ystart,xend,yend);\r
395 \r
396         } else {\r
397           g.setColor(Color.green);\r
398           drawLine(g,xstart,ystart,xend,yend);\r
399 \r
400         }\r
401       } else if (!depthcue && !bymolecule) {\r
402         g.setColor(tmpBond.startCol);\r
403         drawLine(g,xstart,ystart,xmid,ymid);\r
404         g.setColor(tmpBond.endCol);\r
405         drawLine(g,xmid,ymid,xend,yend);\r
406 \r
407       } else {\r
408         drawLine(g,xstart,ystart,xend,yend);\r
409       }\r
410     }\r
411   }\r
412 \r
413   public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {\r
414     if (!wire) {\r
415 \r
416       if (((float)Math.abs(y2-y1)/(float)Math.abs(x2-x1)) < 0.5) {\r
417 \r
418         g.drawLine(x1,y1,x2,y2);\r
419         g.drawLine(x1+1,y1+1,x2+1,y2+1);\r
420         g.drawLine(x1,y1-1,x2,y2-1);\r
421       } else {\r
422         g.setColor(g.getColor().brighter());\r
423         g.drawLine(x1,y1,x2,y2);\r
424         g.drawLine(x1+1,y1,x2+1,y2);\r
425         g.drawLine(x1-1,y1,x2-1,y2);\r
426 \r
427       }\r
428     } else {\r
429       g.drawLine(x1,y1,x2,y2);\r
430     }\r
431   }\r
432 \r
433   public Dimension minimumsize() {\r
434     return prefsize;\r
435   }\r
436   public Dimension preferredsize() {\r
437     return prefsize;\r
438   }\r
439 \r
440   public void keyTyped(KeyEvent evt) { }\r
441   public void keyReleased(KeyEvent evt) { }\r
442   public void keyPressed(KeyEvent evt) {\r
443     int key = evt.getKeyChar();\r
444 \r
445     if (evt.getKeyCode() == KeyEvent.VK_UP) {\r
446       scale        = (float)(scale * 1.1);\r
447       redrawneeded = true;\r
448       repaint();\r
449 \r
450     } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {\r
451       scale        = (float)(scale * 0.9);\r
452       redrawneeded = true;\r
453       repaint();\r
454 \r
455     } else if (key == 'w') {\r
456 \r
457       wire = !wire;\r
458       System.out.println("wireframe " + wire);\r
459       redrawneeded = true;\r
460       repaint();\r
461 \r
462     } else if (key == 'd') {\r
463       depthcue = !depthcue;\r
464       System.out.println("Depth cueing is " + depthcue);\r
465       redrawneeded = true;\r
466       repaint();\r
467 \r
468     } else if (key == 'm') {\r
469       bymolecule = !bymolecule;\r
470       System.out.println("Bymolecule is " + bymolecule);\r
471       redrawneeded = true;\r
472       repaint();\r
473 \r
474     } else if (key == 'z') {\r
475       zbuffer = !zbuffer;\r
476       System.out.println("Z buffering is " + zbuffer);\r
477       redrawneeded = true;\r
478       repaint();\r
479 \r
480     } else if (key == 'c') {\r
481       bymolecule = false;\r
482       pdb.setChainColours();\r
483       System.out.println("Colouring by chain");\r
484       redrawneeded = true;\r
485       repaint();\r
486 \r
487     } else if (key == 'h') {\r
488       bymolecule = false;\r
489       pdb.setHydrophobicityColours();\r
490       System.out.println("Colouring by hydrophobicity");\r
491       redrawneeded = true;\r
492       repaint();\r
493 \r
494     } else if (key == 'q') {\r
495       bymolecule = false;\r
496       pdb.setChargeColours();\r
497       System.out.println("Colouring charges and cysteines");\r
498       redrawneeded = true;\r
499       repaint();\r
500 \r
501     }\r
502     return;\r
503   }\r
504 \r
505   public void mousePressed(MouseEvent e)\r
506   {\r
507 \r
508       mx = e.getX();\r
509       my = e.getY();\r
510       omx = mx;\r
511       omy = my;\r
512       dragging = false;\r
513 }\r
514 \r
515   public void mouseMoved(MouseEvent e)\r
516   {\r
517     myAtom fatom = null;\r
518     for (int ii = 0; ii < pdb.chains.size(); ii++)\r
519     {\r
520 \r
521       PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
522       if (chain.isVisible)\r
523       {\r
524         Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;\r
525         for (int i = 0; i < bonds.size(); i++)\r
526         {\r
527           Bond tmpBond = (Bond) bonds.elementAt(i);\r
528           int truex = (int) ( (tmpBond.start[0] - centre[0]) * scale +\r
529                              getWidth() / 2);\r
530           if (Math.abs(truex - e.getX()) <= 2)\r
531           {\r
532             int truey = (int) ( (tmpBond.start[1] - centre[1]) * scale +\r
533                                getHeight() / 2);\r
534             if (Math.abs(truey - e.getY()) <= 2)\r
535               fatom = tmpBond.at1;\r
536           }\r
537         }\r
538       }\r
539     }\r
540     if(fatom!=null)\r
541      {\r
542        this.setToolTipText(fatom.resName);\r
543      }\r
544      else\r
545         this.setToolTipText(null);\r
546   }\r
547 \r
548   public void mouseClicked(MouseEvent e) { }\r
549   public void mouseEntered(MouseEvent e) { }\r
550   public void mouseExited(MouseEvent e) { }\r
551 \r
552   public void mouseDragged(MouseEvent evt) {\r
553     int x = evt.getX();\r
554     int y = evt.getY();\r
555     mx = x;\r
556     my = y;\r
557 \r
558     MCMatrix objmat = new MCMatrix(3,3);\r
559     objmat.setIdentity();\r
560 \r
561     if ((evt.getModifiers()  & Event.META_MASK) != 0) {\r
562       objmat.rotatez((float)((mx-omx)));\r
563     } else {\r
564       objmat.rotatex((float)((my-omy)));\r
565       objmat.rotatey((float)((omx-mx)));\r
566     }\r
567 \r
568     //Alter the bonds\r
569     for (int ii = 0; ii < pdb.chains.size() ; ii++) {\r
570       Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
571 \r
572       for (int i = 0; i < bonds.size(); i++) {\r
573 \r
574         Bond tmpBond = (Bond)bonds.elementAt(i);\r
575 \r
576         //Translate the bond so the centre is 0,0,0\r
577         tmpBond.translate(-centre[0],-centre[1],-centre[2]);\r
578 \r
579         //Now apply the rotation matrix\r
580         tmpBond.start = objmat.vectorMultiply(tmpBond.start);\r
581         tmpBond.end   = objmat.vectorMultiply(tmpBond.end);\r
582 \r
583         //Now translate back again\r
584         tmpBond.translate(centre[0],centre[1],centre[2]);\r
585       }\r
586     }\r
587 \r
588     objmat = null;\r
589 \r
590     omx    = mx;\r
591     omy    = my;\r
592 \r
593     redrawneeded = true;\r
594 \r
595     paint(this.getGraphics());\r
596 \r
597     dragging = true;\r
598     return;\r
599   }\r
600 \r
601   public void mouseReleased(MouseEvent evt) {\r
602     int x = evt.getX();\r
603     int y = evt.getY();\r
604 \r
605     if (!dragging) {\r
606       myAtom tmp = findAtom(x,y);\r
607     }\r
608     drawLabels();\r
609     return;\r
610   }\r
611 \r
612   public void drawLabels() {\r
613     redrawneeded = true;\r
614     paint(this.getGraphics());\r
615 \r
616     for (int ii = 0; ii < pdb.chains.size() ; ii++) {\r
617 \r
618       PDBChain chain = (PDBChain)pdb.chains.elementAt(ii);\r
619 \r
620       if (chain.isVisible) {\r
621 \r
622         Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
623 \r
624         for (int i = 0; i < bonds.size(); i++) {\r
625           Bond tmpBond = (Bond)bonds.elementAt(i);\r
626 \r
627           if (tmpBond.at1.isSelected) {\r
628             labelAtom(img.getGraphics(),tmpBond,1);\r
629           }\r
630 \r
631           if (tmpBond.at2.isSelected) {\r
632             labelAtom(img.getGraphics(),tmpBond,2);\r
633           }\r
634 \r
635         }\r
636       }\r
637     }\r
638 \r
639     this.getGraphics().drawImage(img,0,0,this);\r
640 \r
641     dragging = false;\r
642 \r
643   }\r
644 \r
645   public void labelAtom(Graphics g,Bond b, int n) {\r
646 \r
647     g.setFont(font);\r
648 \r
649     if (n ==1) {\r
650 \r
651       int xstart = (int)((b.start[0] - centre[0])*scale + getWidth()/2);\r
652       int ystart = (int)((b.start[1] - centre[1])*scale + getHeight()/2);\r
653 \r
654       g.setColor(Color.red);\r
655       g.drawString(b.at1.resName + "-" + b.at1.resNumber,xstart,ystart);\r
656     }\r
657 \r
658     if (n ==2) {\r
659 \r
660       int xstart = (int)((b.end[0] - centre[0])*scale + getWidth()/2);\r
661       int ystart = (int)((b.end[1] - centre[1])*scale + getHeight()/2);\r
662 \r
663       g.setColor(Color.red);\r
664       g.drawString(b.at2.resName + "-" + b.at2.resNumber,xstart,ystart);\r
665     }\r
666   }\r
667 \r
668 \r
669   public myAtom findAtom(int x, int y) {\r
670     myAtom fatom = null;\r
671 \r
672     int foundchain = -1;\r
673 \r
674     for (int ii = 0; ii < pdb.chains.size() ; ii++) {\r
675 \r
676       PDBChain chain = (PDBChain)pdb.chains.elementAt(ii);\r
677 \r
678       if (chain.isVisible) {\r
679 \r
680         Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;\r
681 \r
682         for (int i = 0; i < bonds.size(); i++) {\r
683           Bond tmpBond = (Bond)bonds.elementAt(i);\r
684 \r
685           int truex = (int)((tmpBond.start[0] - centre[0])*scale + getWidth()/2);\r
686 \r
687           if (Math.abs(truex - x) <= 2) {\r
688 \r
689             int truey = (int)((tmpBond.start[1] - centre[1])*scale + getHeight()/2);\r
690 \r
691             if (Math.abs(truey - y) <= 2) {\r
692 \r
693               System.out.println("Found match");\r
694               System.out.println(x + " " + y);\r
695               System.out.println(truex + " " + truey);\r
696               System.out.println(tmpBond.start[0] + " " + tmpBond.start[1]);\r
697               System.out.println("Atom 1 = " + tmpBond.at1.resName + " " +\r
698                                  tmpBond.at1.resNumber + " " + tmpBond.at1.chain);\r
699               fatom = tmpBond.at1;\r
700               fatom.isSelected = !fatom.isSelected;\r
701               foundchain = ii;\r
702             }\r
703           }\r
704         }\r
705       }\r
706 \r
707       if (fatom != null) //)&& chain.ds != null)\r
708       {\r
709         chain = (PDBChain)pdb.chains.elementAt(foundchain);\r
710         // SMJS TODO\r
711         // int tmp = chain.ds.seqstart + fatom.resNumber - chain.offset;\r
712         // int pos = chain.ds.findIndex(tmp);\r
713         // System.out.println("Found seq " + chain.ds.name + " "  + tmp + " " + pos);\r
714       }\r
715 \r
716     }\r
717     return fatom;\r
718   }\r
719 \r
720   public void update(Graphics g) {\r
721     paint(g);\r
722   }\r
723 \r
724 }\r