Has its own featureRenderer
[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 \r
38     MCMatrix idmat = new MCMatrix(3, 3);\r
39     MCMatrix objmat = new MCMatrix(3, 3);\r
40     boolean redrawneeded = true;\r
41     int omx = 0;\r
42     int mx = 0;\r
43     int omy = 0;\r
44     int my = 0;\r
45     public PDBfile pdb;\r
46     int bsize;\r
47     Image img;\r
48     Graphics ig;\r
49     Dimension prefsize;\r
50     float[] centre = new float[3];\r
51     float[] width = new float[3];\r
52     float maxwidth;\r
53     float scale;\r
54     String inStr;\r
55     String inType;\r
56     boolean bysequence = true;\r
57     boolean depthcue = true;\r
58     boolean wire = false;\r
59     boolean bymolecule = false;\r
60     boolean zbuffer = true;\r
61     boolean dragging;\r
62     int xstart;\r
63     int xend;\r
64     int ystart;\r
65     int yend;\r
66     int xmid;\r
67     int ymid;\r
68     Font font = new Font("Helvetica", Font.PLAIN, 10);\r
69     jalview.appletgui.SeqCanvas seqcanvas;\r
70     public Sequence sequence;\r
71     final StringBuffer mappingDetails = new StringBuffer();\r
72     String appletToolTip = null;\r
73     int toolx, tooly;\r
74     PDBChain mainchain;\r
75     Vector highlightRes;\r
76     boolean pdbAction = false;\r
77     Bond highlightBond1, highlightBond2;\r
78     boolean errorLoading = false;\r
79     boolean seqColoursReady = false;\r
80     jalview.appletgui.FeatureRenderer fr;\r
81 \r
82     public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)\r
83     {\r
84       this.seqcanvas = seqcanvas;\r
85       this.sequence = seq;\r
86 \r
87       seqcanvas.setPDBCanvas(this);\r
88       addKeyListener(new KeyAdapter()\r
89       {\r
90 \r
91         public void keyPressed(KeyEvent evt)\r
92         {\r
93           doKeyPressed(evt);\r
94         }\r
95       });\r
96     }\r
97 \r
98 \r
99   public void setPDBFile(PDBfile pdb)\r
100    {\r
101         int max = -10;\r
102         int maxchain = -1;\r
103         int pdbstart = 0;\r
104         int pdbend = 0;\r
105         int seqstart = 0;\r
106         int seqend = 0;\r
107         AlignSeq maxAlignseq = null;;\r
108 \r
109         for (int i = 0; i < pdb.chains.size(); i++)\r
110         {\r
111 \r
112           mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence());\r
113           mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");\r
114 \r
115             // Now lets compare the sequences to get\r
116             // the start and end points.\r
117             // Align the sequence to the pdb\r
118             AlignSeq as = new AlignSeq(sequence,\r
119                     ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");\r
120             as.calcScoreMatrix();\r
121             as.traceAlignment();\r
122             PrintStream ps = new PrintStream(System.out)\r
123            {\r
124               public void print(String x) {\r
125                    mappingDetails.append(x);\r
126                }\r
127                public void println()\r
128                {\r
129                  mappingDetails.append("\n");\r
130                }\r
131             };\r
132 \r
133             as.printAlignment(ps);\r
134 \r
135             if (as.maxscore > max) {\r
136                 max = as.maxscore;\r
137                 maxchain = i;\r
138 \r
139                 pdbstart = as.seq2start;\r
140                 pdbend = as.seq2end;\r
141                 seqstart = as.seq1start + sequence.getStart()-1;\r
142                 seqend = as.seq1end + sequence.getEnd()-1;\r
143                 maxAlignseq = as;\r
144             }\r
145 \r
146             mappingDetails.append("\nPDB start/end "  + pdbstart + " " + pdbend);\r
147             mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);\r
148         }\r
149 \r
150         mainchain = (PDBChain) pdb.chains.elementAt(maxchain);\r
151 \r
152         mainchain.pdbstart = pdbstart;\r
153         mainchain.pdbend = pdbend;\r
154         mainchain.seqstart = seqstart;\r
155         mainchain.seqend = seqend;\r
156         mainchain.isVisible = true;\r
157         mainchain.makeExactMapping(maxAlignseq, sequence);\r
158 \r
159         this.pdb = pdb;\r
160         this.prefsize = new Dimension(getSize().width, getSize().height);\r
161 \r
162         //Initialize the matrices to identity\r
163         for (int i = 0; i < 3; i++) {\r
164             for (int j = 0; j < 3; j++) {\r
165                 if (i != j) {\r
166                     idmat.addElement(i, j, 0);\r
167                     objmat.addElement(i, j, 0);\r
168                 } else {\r
169                     idmat.addElement(i, j, 1);\r
170                     objmat.addElement(i, j, 1);\r
171                 }\r
172             }\r
173         }\r
174 \r
175         addMouseMotionListener(this);\r
176         addMouseListener(this);\r
177 \r
178 \r
179         findCentre();\r
180         findWidth();\r
181 \r
182         setupBonds();\r
183 \r
184         scale = findScale();\r
185     }\r
186 \r
187 \r
188     Vector visiblebonds;\r
189     void setupBonds()\r
190     {\r
191       seqColoursReady = false;\r
192       // Sort the bonds by z coord\r
193       visiblebonds = new Vector();\r
194 \r
195       for (int ii = 0; ii < pdb.chains.size(); ii++)\r
196       {\r
197         if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)\r
198         {\r
199           Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;\r
200 \r
201           for (int i = 0; i < tmp.size(); i++)\r
202           {\r
203             visiblebonds.addElement(tmp.elementAt(i));\r
204           }\r
205         }\r
206       }\r
207       updateSeqColours();\r
208       seqColoursReady = true;\r
209       redrawneeded = true;\r
210       repaint();\r
211     }\r
212 \r
213 \r
214     public void findWidth() {\r
215         float[] max = new float[3];\r
216         float[] min = new float[3];\r
217 \r
218         max[0] = (float) -1e30;\r
219         max[1] = (float) -1e30;\r
220         max[2] = (float) -1e30;\r
221 \r
222         min[0] = (float) 1e30;\r
223         min[1] = (float) 1e30;\r
224         min[2] = (float) 1e30;\r
225 \r
226         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
227             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
228                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
229 \r
230                 for (int i = 0; i < bonds.size(); i++) {\r
231                     Bond tmp = (Bond) bonds.elementAt(i);\r
232 \r
233                     if (tmp.start[0] >= max[0]) {\r
234                         max[0] = tmp.start[0];\r
235                     }\r
236 \r
237                     if (tmp.start[1] >= max[1]) {\r
238                         max[1] = tmp.start[1];\r
239                     }\r
240 \r
241                     if (tmp.start[2] >= max[2]) {\r
242                         max[2] = tmp.start[2];\r
243                     }\r
244 \r
245                     if (tmp.start[0] <= min[0]) {\r
246                         min[0] = tmp.start[0];\r
247                     }\r
248 \r
249                     if (tmp.start[1] <= min[1]) {\r
250                         min[1] = tmp.start[1];\r
251                     }\r
252 \r
253                     if (tmp.start[2] <= min[2]) {\r
254                         min[2] = tmp.start[2];\r
255                     }\r
256 \r
257                     if (tmp.end[0] >= max[0]) {\r
258                         max[0] = tmp.end[0];\r
259                     }\r
260 \r
261                     if (tmp.end[1] >= max[1]) {\r
262                         max[1] = tmp.end[1];\r
263                     }\r
264 \r
265                     if (tmp.end[2] >= max[2]) {\r
266                         max[2] = tmp.end[2];\r
267                     }\r
268 \r
269                     if (tmp.end[0] <= min[0]) {\r
270                         min[0] = tmp.end[0];\r
271                     }\r
272 \r
273                     if (tmp.end[1] <= min[1]) {\r
274                         min[1] = tmp.end[1];\r
275                     }\r
276 \r
277                     if (tmp.end[2] <= min[2]) {\r
278                         min[2] = tmp.end[2];\r
279                     }\r
280                 }\r
281             }\r
282         }\r
283 \r
284         width[0] = (float) Math.abs(max[0] - min[0]);\r
285         width[1] = (float) Math.abs(max[1] - min[1]);\r
286         width[2] = (float) Math.abs(max[2] - min[2]);\r
287 \r
288         maxwidth = width[0];\r
289 \r
290         if (width[1] > width[0]) {\r
291             maxwidth = width[1];\r
292         }\r
293 \r
294         if (width[2] > width[1]) {\r
295             maxwidth = width[2];\r
296         }\r
297 \r
298        // System.out.println("Maxwidth = " + maxwidth);\r
299     }\r
300 \r
301     public float findScale() {\r
302         int dim;\r
303         int width;\r
304         int height;\r
305 \r
306         if (getSize().width != 0) {\r
307             width = getSize().width;\r
308             height = getSize().height;\r
309         } else {\r
310             width = prefsize.width;\r
311             height = prefsize.height;\r
312         }\r
313 \r
314         if (width < height) {\r
315             dim = width;\r
316         } else {\r
317             dim = height;\r
318         }\r
319 \r
320         return (float) (dim / (1.5d * maxwidth));\r
321     }\r
322 \r
323     public void findCentre() {\r
324         float xtot = 0;\r
325         float ytot = 0;\r
326         float ztot = 0;\r
327 \r
328         int bsize = 0;\r
329 \r
330         //Find centre coordinate\r
331         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
332             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
333                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
334 \r
335                 bsize += bonds.size();\r
336 \r
337                 for (int i = 0; i < bonds.size(); i++) {\r
338                     xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +\r
339                         ((Bond) bonds.elementAt(i)).end[0];\r
340 \r
341                     ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +\r
342                         ((Bond) bonds.elementAt(i)).end[1];\r
343 \r
344                     ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +\r
345                         ((Bond) bonds.elementAt(i)).end[2];\r
346                 }\r
347             }\r
348         }\r
349 \r
350         centre[0] = xtot / (2 * (float) bsize);\r
351         centre[1] = ytot / (2 * (float) bsize);\r
352         centre[2] = ztot / (2 * (float) bsize);\r
353     }\r
354 \r
355     public void paint(Graphics g)\r
356     {\r
357 \r
358       if(errorLoading)\r
359       {\r
360         g.setColor(Color.white);\r
361         g.fillRect(0,0,getSize().width, getSize().height);\r
362         g.setColor(Color.black);\r
363         g.setFont(new Font("Verdana", Font.BOLD, 14));\r
364         g.drawString("Error loading PDB data!!", 50, getSize().height/2);\r
365         return;\r
366       }\r
367 \r
368       if( !seqColoursReady )\r
369       {\r
370         g.setColor(Color.black);\r
371         g.setFont(new Font("Verdana", Font.BOLD, 14));\r
372         g.drawString("Fetching PDB data...", 50, getSize().height/2);\r
373         return;\r
374       }\r
375 \r
376 \r
377 \r
378         //Only create the image at the beginning -\r
379         //this saves much memory usage\r
380         if ((img == null) || (prefsize.width != getSize().width) ||\r
381                 (prefsize.height != getSize().height)) {\r
382 \r
383          try{     prefsize.width = getSize().width;\r
384            prefsize.height = getSize().height;\r
385 \r
386            scale = findScale();\r
387            img = createImage(prefsize.width, prefsize.height);\r
388            ig = img.getGraphics();\r
389 \r
390            redrawneeded = true;\r
391          }catch(Exception ex)\r
392          {\r
393            ex.printStackTrace();\r
394          }\r
395         }\r
396 \r
397 \r
398         if (redrawneeded)\r
399         {\r
400           drawAll(ig, prefsize.width, prefsize.height);\r
401           redrawneeded = false;\r
402         }\r
403         if(appletToolTip!=null)\r
404         {\r
405           ig.setColor(Color.red);\r
406           ig.drawString(appletToolTip, toolx, tooly);\r
407         }\r
408 \r
409         g.drawImage(img, 0, 0, this);\r
410 \r
411         pdbAction = false;\r
412     }\r
413 \r
414     public void drawAll(Graphics g, int width, int height)\r
415     {\r
416       ig.setColor(Color.black);\r
417       ig.fillRect(0, 0, width, height);\r
418       drawScene(ig);\r
419       drawLabels(ig);\r
420     }\r
421 \r
422    void setColours(jalview.schemes.ColourSchemeI cs)\r
423    {\r
424      bysequence = false;\r
425      pdb.setColours(cs);\r
426      redrawneeded = true;\r
427      repaint();\r
428    }\r
429     public void updateSeqColours()\r
430     {\r
431       if (pdbAction)\r
432       {\r
433         return;\r
434       }\r
435 \r
436       if(bysequence && pdb!=null)\r
437       {\r
438         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
439         {\r
440           colourBySequence((PDBChain) pdb.chains.elementAt(ii));\r
441         }\r
442       }\r
443 \r
444       redrawneeded=true;\r
445       repaint();\r
446     }\r
447 \r
448 \r
449     int findTrueIndex(int pos)\r
450     {\r
451       // returns the alignment position for a residue\r
452       int j = sequence.getStart();\r
453       int i = 0;\r
454 \r
455       while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) && (j <= pos+1))\r
456       {\r
457         if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))\r
458         {\r
459           j++;\r
460         }\r
461 \r
462         i++;\r
463       }\r
464 \r
465       if(i>1)\r
466          i--;\r
467 \r
468       if ( (j == sequence.getEnd()) && (j < pos))\r
469       {\r
470         return sequence.getEnd() + 1;\r
471       }\r
472       else\r
473       {\r
474         return i;\r
475       }\r
476     }\r
477 \r
478 \r
479     // This method has been taken out of PDBChain to allow\r
480     // Applet and Application specific sequence renderers to be used\r
481     void colourBySequence(PDBChain chain)\r
482     {\r
483       boolean showFeatures = false;\r
484 \r
485       if(seqcanvas.getViewport().getShowSequenceFeatures())\r
486         {\r
487           if(fr==null)\r
488           {\r
489             fr = new jalview.appletgui.FeatureRenderer(seqcanvas.getViewport());\r
490           }\r
491           fr.transferSettings( seqcanvas.getFeatureRenderer());\r
492           showFeatures = true;\r
493         }\r
494 \r
495       for (int i = 0; i < chain.bonds.size(); i++)\r
496       {\r
497         Bond tmp = (Bond) chain.bonds.elementAt(i);\r
498         tmp.startCol = Color.lightGray;\r
499         tmp.endCol = Color.lightGray;\r
500         if(chain!=mainchain)\r
501           continue;\r
502 \r
503         if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&\r
504             (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))\r
505         {\r
506 \r
507           int index = findTrueIndex(tmp.at1.alignmentMapping);\r
508                 //sequence.findIndex(tmp.at1.alignmentMapping);\r
509             if (index != -1)\r
510             {\r
511               tmp.startCol = seqcanvas.getSequenceRenderer().\r
512                   getResidueBoxColour( sequence, index);\r
513 \r
514               if(showFeatures)\r
515               tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);\r
516             }\r
517         }\r
518 \r
519         int index =  findTrueIndex(tmp.at2.alignmentMapping);\r
520             //sequence.findIndex( tmp.at2.alignmentMapping );\r
521         if (index != -1)\r
522         {\r
523           tmp.endCol = seqcanvas.getSequenceRenderer().\r
524               getResidueBoxColour( sequence, index);\r
525 \r
526           if(showFeatures)\r
527           tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);\r
528         }\r
529       }\r
530     }\r
531 \r
532 \r
533     Zsort zsort;\r
534     public void drawScene(Graphics g)\r
535     {\r
536         if (zbuffer)\r
537         {\r
538           if(zsort ==null)\r
539             zsort = new Zsort();\r
540 \r
541           zsort.Zsort(visiblebonds);\r
542         }\r
543 \r
544 \r
545         Bond tmpBond=null;\r
546         for (int i = 0; i < visiblebonds.size(); i++)\r
547         {\r
548             tmpBond = (Bond) visiblebonds.elementAt(i);\r
549 \r
550 \r
551             xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
552                 (getSize().width / 2));\r
553             ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
554                 (getSize().height / 2));\r
555 \r
556             xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +\r
557                 (getSize().width / 2));\r
558             yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +\r
559                 (getSize().height / 2));\r
560 \r
561             xmid = (xend + xstart) / 2;\r
562             ymid = (yend + ystart) / 2;\r
563 \r
564             if (depthcue && !bymolecule)\r
565             {\r
566                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
567                     g.setColor(tmpBond.startCol.darker().darker());\r
568                     drawLine(g, xstart, ystart, xmid, ymid);\r
569 \r
570                     g.setColor(tmpBond.endCol.darker().darker());\r
571                     drawLine(g, xmid, ymid, xend, yend);\r
572                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
573                     g.setColor(tmpBond.startCol.darker());\r
574                     drawLine(g, xstart, ystart, xmid, ymid);\r
575 \r
576                     g.setColor(tmpBond.endCol.darker());\r
577                     drawLine(g, xmid, ymid, xend, yend);\r
578                 } else {\r
579                     g.setColor(tmpBond.startCol);\r
580                     drawLine(g, xstart, ystart, xmid, ymid);\r
581 \r
582                     g.setColor(tmpBond.endCol);\r
583                     drawLine(g, xmid, ymid, xend, yend);\r
584                 }\r
585 \r
586             } else if (depthcue && bymolecule) {\r
587                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
588                     g.setColor(Color.green.darker().darker());\r
589                     drawLine(g, xstart, ystart, xend, yend);\r
590                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
591                     g.setColor(Color.green.darker());\r
592                     drawLine(g, xstart, ystart, xend, yend);\r
593                 } else {\r
594                     g.setColor(Color.green);\r
595                     drawLine(g, xstart, ystart, xend, yend);\r
596                 }\r
597             } else if (!depthcue && !bymolecule) {\r
598                 g.setColor(tmpBond.startCol);\r
599                 drawLine(g, xstart, ystart, xmid, ymid);\r
600                 g.setColor(tmpBond.endCol);\r
601                 drawLine(g, xmid, ymid, xend, yend);\r
602             } else {\r
603                 drawLine(g, xstart, ystart, xend, yend);\r
604             }\r
605 \r
606             if(highlightBond1!=null && highlightBond1==tmpBond)\r
607             {\r
608               g.setColor(Color.white);\r
609               drawLine(g, xmid, ymid, xend, yend);\r
610             }\r
611 \r
612             if(highlightBond2!=null && highlightBond2==tmpBond)\r
613             {\r
614               g.setColor(Color.white);\r
615               drawLine(g, xstart, ystart, xmid, ymid);\r
616             }\r
617 \r
618         }\r
619     }\r
620 \r
621     public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {\r
622         if (!wire) {\r
623             if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {\r
624                 g.drawLine(x1, y1, x2, y2);\r
625                 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);\r
626                 g.drawLine(x1, y1 - 1, x2, y2 - 1);\r
627             } else {\r
628                 g.setColor(g.getColor().brighter());\r
629                 g.drawLine(x1, y1, x2, y2);\r
630                 g.drawLine(x1 + 1, y1, x2 + 1, y2);\r
631                 g.drawLine(x1 - 1, y1, x2 - 1, y2);\r
632             }\r
633         } else {\r
634             g.drawLine(x1, y1, x2, y2);\r
635         }\r
636     }\r
637 \r
638     public Dimension minimumsize() {\r
639         return prefsize;\r
640     }\r
641 \r
642     public Dimension preferredsize() {\r
643         return prefsize;\r
644     }\r
645 \r
646     public void doKeyPressed(KeyEvent evt)\r
647     {\r
648       if (evt.getKeyCode() == KeyEvent.VK_UP)\r
649       {\r
650         scale = (float) (scale * 1.1);\r
651         redrawneeded = true;\r
652         repaint();\r
653       }\r
654       else if (evt.getKeyCode() == KeyEvent.VK_DOWN)\r
655       {\r
656         scale = (float) (scale * 0.9);\r
657         redrawneeded = true;\r
658         repaint();\r
659       }\r
660     }\r
661 \r
662     public void mousePressed(MouseEvent e) {\r
663       pdbAction = true;\r
664       Atom fatom = findAtom(e.getX(), e.getY());\r
665       if(fatom!=null)\r
666       {\r
667         fatom.isSelected = !fatom.isSelected;\r
668 \r
669         redrawneeded = true;\r
670         repaint();\r
671         if (foundchain != -1)\r
672         {\r
673           PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
674           if (chain == mainchain)\r
675           {\r
676             if (fatom.alignmentMapping != -1)\r
677             {\r
678               if (highlightRes == null)\r
679                 highlightRes = new Vector();\r
680 \r
681               if (highlightRes.contains(fatom.alignmentMapping+"" + ""))\r
682                 highlightRes.removeElement(fatom.alignmentMapping + "");\r
683               else\r
684                 highlightRes.addElement(fatom.alignmentMapping + "");\r
685             }\r
686           }\r
687         }\r
688 \r
689         }\r
690         mx = e.getX();\r
691         my = e.getY();\r
692         omx = mx;\r
693         omy = my;\r
694         dragging = false;\r
695     }\r
696 \r
697     public void mouseMoved(MouseEvent e) {\r
698       pdbAction = true;\r
699       if(highlightBond1!=null)\r
700       {\r
701         highlightBond1.at2.isSelected = false;\r
702         highlightBond2.at1.isSelected = false;\r
703         highlightBond1 = null;\r
704         highlightBond2 = null;\r
705       }\r
706 \r
707         Atom fatom = findAtom(e.getX(), e.getY());\r
708 \r
709         PDBChain chain = null;\r
710         if(foundchain!=-1)\r
711         {\r
712           chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
713           if(chain == mainchain)\r
714           {\r
715             highlightSeqcanvas( fatom.alignmentMapping );\r
716           }\r
717         }\r
718 \r
719         if (fatom != null) {\r
720             toolx = e.getX();\r
721             tooly = e.getY();\r
722 \r
723             appletToolTip = chain.id+":"+ fatom.resNumber+" "+ fatom.resName;\r
724             redrawneeded = true;\r
725             repaint();\r
726         } else {\r
727             highlightSeqcanvas( -1);\r
728             appletToolTip = null;\r
729             redrawneeded = true;\r
730             repaint();\r
731         }\r
732     }\r
733 \r
734 \r
735     void highlightSeqcanvas(int pos)\r
736     {\r
737       SearchResults searchResults = new SearchResults();\r
738       if(highlightRes!=null)\r
739       {\r
740         for (int i = 0; i < highlightRes.size(); i++)\r
741         {\r
742           int a = Integer.parseInt(highlightRes.elementAt(\r
743               i).toString())+1;\r
744 \r
745           searchResults.addResult(sequence, a, a);\r
746         }\r
747       }\r
748 \r
749       if(pos!=-1)\r
750       {\r
751         searchResults.addResult(sequence, pos+1, pos+1);\r
752       }\r
753 \r
754       seqcanvas.highlightSearchResults(searchResults);\r
755     }\r
756 \r
757 \r
758     public void mouseClicked(MouseEvent e) {\r
759     }\r
760 \r
761     public void mouseEntered(MouseEvent e) {\r
762     }\r
763 \r
764     public void mouseExited(MouseEvent e) {\r
765     }\r
766 \r
767     public void mouseDragged(MouseEvent evt) {\r
768         int x = evt.getX();\r
769         int y = evt.getY();\r
770         mx = x;\r
771         my = y;\r
772 \r
773         MCMatrix objmat = new MCMatrix(3, 3);\r
774         objmat.setIdentity();\r
775 \r
776         if ((evt.getModifiers() & Event.META_MASK) != 0) {\r
777             objmat.rotatez((float) ((mx - omx)));\r
778         } else {\r
779             objmat.rotatex((float) ((my - omy)));\r
780             objmat.rotatey((float) ((omx - mx)));\r
781         }\r
782 \r
783         //Alter the bonds\r
784         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
785             Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
786 \r
787             for (int i = 0; i < bonds.size(); i++) {\r
788                 Bond tmpBond = (Bond) bonds.elementAt(i);\r
789 \r
790                 //Translate the bond so the centre is 0,0,0\r
791                 tmpBond.translate(-centre[0], -centre[1], -centre[2]);\r
792 \r
793                 //Now apply the rotation matrix\r
794                 tmpBond.start = objmat.vectorMultiply(tmpBond.start);\r
795                 tmpBond.end = objmat.vectorMultiply(tmpBond.end);\r
796 \r
797                 //Now translate back again\r
798                 tmpBond.translate(centre[0], centre[1], centre[2]);\r
799             }\r
800         }\r
801 \r
802         objmat = null;\r
803 \r
804         omx = mx;\r
805         omy = my;\r
806 \r
807         dragging = true;\r
808 \r
809         redrawneeded = true;\r
810 \r
811         repaint();\r
812     }\r
813 \r
814     public void mouseReleased(MouseEvent evt) {\r
815         dragging = false;\r
816         return;\r
817     }\r
818 \r
819     void drawLabels(Graphics g) {\r
820 \r
821         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
822         {\r
823             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
824 \r
825             if (chain.isVisible)\r
826             {\r
827                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
828 \r
829                 for (int i = 0; i < bonds.size(); i++)\r
830                 {\r
831                     Bond tmpBond = (Bond) bonds.elementAt(i);\r
832 \r
833                     if (tmpBond.at1.isSelected)\r
834                     {\r
835                         labelAtom(g, tmpBond, 1);\r
836                     }\r
837 \r
838                     if (tmpBond.at2.isSelected)\r
839                     {\r
840 \r
841                         labelAtom(g, tmpBond, 2);\r
842                     }\r
843                 }\r
844             }\r
845         }\r
846     }\r
847 \r
848     public void labelAtom(Graphics g, Bond b, int n) {\r
849         g.setFont(font);\r
850 \r
851         if (n == 1) {\r
852             int xstart = (int) (((b.start[0] - centre[0]) * scale) +\r
853                 (getSize().width / 2));\r
854             int ystart = (int) (((b.start[1] - centre[1]) * scale) +\r
855                 (getSize().height / 2));\r
856 \r
857             g.setColor(Color.red);\r
858             g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);\r
859         }\r
860 \r
861         if (n == 2) {\r
862             int xstart = (int) (((b.end[0] - centre[0]) * scale) +\r
863                 (getSize().width / 2));\r
864             int ystart = (int) (((b.end[1] - centre[1]) * scale) +\r
865                 (getSize().height / 2));\r
866 \r
867             g.setColor(Color.red);\r
868             g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);\r
869         }\r
870     }\r
871 \r
872     int foundchain = -1;\r
873     public Atom findAtom(int x, int y) {\r
874         Atom fatom = null;\r
875 \r
876         foundchain = -1;\r
877 \r
878         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
879         {\r
880             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
881             int truex;\r
882             Bond tmpBond=null;\r
883 \r
884             if (chain.isVisible)\r
885             {\r
886                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
887 \r
888                 for (int i = 0; i < bonds.size(); i++)\r
889                 {\r
890                     tmpBond = (Bond) bonds.elementAt(i);\r
891 \r
892                     truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
893                         (getSize().width / 2));\r
894 \r
895                     if (Math.abs(truex - x) <= 2)\r
896                     {\r
897                         int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
898                             (getSize().height / 2));\r
899 \r
900                         if (Math.abs(truey - y) <= 2)\r
901                         {\r
902                             fatom = tmpBond.at1;\r
903                             foundchain = ii;\r
904                             break;\r
905                         }\r
906                     }\r
907                 }\r
908 \r
909                 // Still here? Maybe its the last bond\r
910 \r
911                 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +\r
912                                (getSize().width / 2));\r
913 \r
914                 if (Math.abs(truex - x) <= 2)\r
915                 {\r
916                   int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +\r
917                                      (getSize().height / 2));\r
918 \r
919                   if (Math.abs(truey - y) <= 2)\r
920                   {\r
921                     fatom = tmpBond.at2;\r
922                     foundchain = ii;\r
923                     break;\r
924                   }\r
925                 }\r
926 \r
927             }\r
928 \r
929             if (fatom != null) //)&& chain.ds != null)\r
930              {\r
931                 chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
932             }\r
933         }\r
934 \r
935         return fatom;\r
936     }\r
937 \r
938     public void update(Graphics g)\r
939     {\r
940       paint(g);\r
941     }\r
942 \r
943     public void highlightRes(int ii)\r
944    {\r
945      if(!seqColoursReady)\r
946        return;\r
947 \r
948      if (highlightRes != null\r
949          && highlightRes.contains((ii-1) + ""))\r
950      {\r
951        return;\r
952      }\r
953 \r
954      int index = -1;\r
955      Bond tmpBond;\r
956      for(index=0; index<mainchain.bonds.size(); index++)\r
957      {\r
958        tmpBond = (Bond) mainchain.bonds.elementAt(index);\r
959        if (tmpBond.at1.alignmentMapping == ii - 1)\r
960        {\r
961          if (highlightBond1 != null)\r
962            highlightBond1.at2.isSelected = false;\r
963 \r
964          if (highlightBond2 != null)\r
965            highlightBond2.at1.isSelected = false;\r
966 \r
967          highlightBond1 = null;\r
968          highlightBond2 = null;\r
969 \r
970          if (index > 0)\r
971          {\r
972            highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);\r
973            highlightBond1.at2.isSelected = true;\r
974          }\r
975 \r
976          if (index != mainchain.bonds.size())\r
977          {\r
978            highlightBond2 = (Bond) mainchain.bonds.elementAt(index);\r
979            highlightBond2.at1.isSelected = true;\r
980          }\r
981 \r
982          break;\r
983        }\r
984      }\r
985 \r
986      redrawneeded = true;\r
987      repaint();\r
988    }\r
989 \r
990 \r
991     public void setAllchainsVisible(boolean b)\r
992     {\r
993       for (int ii = 0; ii < pdb.chains.size(); ii++)\r
994       {\r
995         PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
996         chain.isVisible = b;\r
997       }\r
998       mainchain.isVisible = true;\r
999       findCentre();\r
1000       setupBonds();\r
1001     }\r
1002 \r
1003 }\r