2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
21 import jalview.analysis.AlignSeq;
\r
23 import jalview.datamodel.*;
\r
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
\r
27 import java.awt.event.*;
\r
35 public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener
\r
37 MCMatrix idmat = new MCMatrix(3, 3);
\r
38 MCMatrix objmat = new MCMatrix(3, 3);
\r
39 boolean redrawneeded = true;
\r
49 float[] centre = new float[3];
\r
50 float[] width = new float[3];
\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
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
72 final StringBuffer mappingDetails = new StringBuffer();
\r
73 String appletToolTip = null;
\r
76 public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)
\r
78 this.seqcanvas = seqcanvas;
\r
79 this.sequence = seq;
\r
80 sr = seqcanvas.getSequenceRenderer();
\r
81 fr = seqcanvas.getFeatureRenderer();
\r
83 seqcanvas.setPDBViewer(this);
\r
84 addKeyListener(new KeyAdapter()
\r
87 public void keyPressed(KeyEvent evt)
\r
95 public void setPDBFile(PDBfile pdb)
\r
106 for (int i = 0; i < pdb.chains.size(); i++)
\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
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
121 public void print(String x) {
\r
122 mappingDetails.append(x);
\r
124 public void println()
\r
126 mappingDetails.append("\n");
\r
130 as.printAlignment(ps);
\r
132 if (as.maxscore > max) {
\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
142 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
\r
143 mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);
\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
154 this.prefsize = new Dimension(getSize().width, getSize().height);
\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
160 idmat.addElement(i, j, 0);
\r
161 objmat.addElement(i, j, 0);
\r
163 idmat.addElement(i, j, 1);
\r
164 objmat.addElement(i, j, 1);
\r
169 addMouseMotionListener(this);
\r
170 addMouseListener(this);
\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
185 scale = findScale();
\r
187 updateSeqColours();
\r
190 public void deleteBonds() {
\r
202 for (int i = 0; i < pdb.chains.size(); i++) {
\r
203 ((PDBChain) pdb.chains.elementAt(i)).bonds = null;
\r
207 public void findWidth() {
\r
208 float[] max = new float[3];
\r
209 float[] min = new float[3];
\r
211 max[0] = (float) -1e30;
\r
212 max[1] = (float) -1e30;
\r
213 max[2] = (float) -1e30;
\r
215 min[0] = (float) 1e30;
\r
216 min[1] = (float) 1e30;
\r
217 min[2] = (float) 1e30;
\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
223 for (int i = 0; i < bonds.size(); i++) {
\r
224 Bond tmp = (Bond) bonds.elementAt(i);
\r
226 if (tmp.start[0] >= max[0]) {
\r
227 max[0] = tmp.start[0];
\r
230 if (tmp.start[1] >= max[1]) {
\r
231 max[1] = tmp.start[1];
\r
234 if (tmp.start[2] >= max[2]) {
\r
235 max[2] = tmp.start[2];
\r
238 if (tmp.start[0] <= min[0]) {
\r
239 min[0] = tmp.start[0];
\r
242 if (tmp.start[1] <= min[1]) {
\r
243 min[1] = tmp.start[1];
\r
246 if (tmp.start[2] <= min[2]) {
\r
247 min[2] = tmp.start[2];
\r
250 if (tmp.end[0] >= max[0]) {
\r
251 max[0] = tmp.end[0];
\r
254 if (tmp.end[1] >= max[1]) {
\r
255 max[1] = tmp.end[1];
\r
258 if (tmp.end[2] >= max[2]) {
\r
259 max[2] = tmp.end[2];
\r
262 if (tmp.end[0] <= min[0]) {
\r
263 min[0] = tmp.end[0];
\r
266 if (tmp.end[1] <= min[1]) {
\r
267 min[1] = tmp.end[1];
\r
270 if (tmp.end[2] <= min[2]) {
\r
271 min[2] = tmp.end[2];
\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
281 maxwidth = width[0];
\r
283 if (width[1] > width[0]) {
\r
284 maxwidth = width[1];
\r
287 if (width[2] > width[1]) {
\r
288 maxwidth = width[2];
\r
291 // System.out.println("Maxwidth = " + maxwidth);
\r
294 public float findScale() {
\r
299 if (getSize().width != 0) {
\r
300 width = getSize().width;
\r
301 height = getSize().height;
\r
303 width = prefsize.width;
\r
304 height = prefsize.height;
\r
307 if (width < height) {
\r
313 return (float) (dim / (1.5d * maxwidth));
\r
316 public void findCentre() {
\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
328 bsize += bonds.size();
\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
334 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
\r
335 ((Bond) bonds.elementAt(i)).end[1];
\r
337 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
\r
338 ((Bond) bonds.elementAt(i)).end[2];
\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
348 public void paint(Graphics g)
\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
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
367 scale = findScale();
\r
368 img = createImage(prefsize.width, prefsize.height);
\r
369 ig = img.getGraphics();
\r
371 redrawneeded = true;
\r
377 drawAll(ig, prefsize.width, prefsize.height);
\r
378 redrawneeded = false;
\r
380 if(appletToolTip!=null)
\r
382 ig.setColor(Color.red);
\r
383 ig.drawString(appletToolTip, toolx, tooly);
\r
386 g.drawImage(img, 0, 0, this);
\r
391 public void drawAll(Graphics g, int width, int height)
\r
393 g.setColor(Color.black);
\r
394 g.fillRect(0, 0, width, height);
\r
400 public void updateSeqColours()
\r
402 if(bysequence && pdb!=null)
\r
404 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
406 colourBySequence((PDBChain) pdb.chains.elementAt(ii));
\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
418 for (int i = 0; i < chain.bonds.size(); i++)
\r
420 Bond tmp = (Bond) chain.bonds.elementAt(i);
\r
422 if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
423 (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
\r
425 int pos = chain.seqstart +
\r
426 (tmp.at1.resNumber - chain.pdbstart - chain.offset);
\r
428 int index = sequence.findIndex(pos);
\r
430 tmp.startCol = sr.findSequenceColour(sequence, index);
\r
432 // tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
\r
437 tmp.startCol = Color.gray;
\r
440 if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
441 (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
\r
443 int pos = chain.seqstart +
\r
444 (tmp.at2.resNumber - chain.pdbstart - chain.offset);
\r
445 int index = sequence.findIndex(pos);
\r
447 tmp.endCol = sr.findSequenceColour( sequence, index);
\r
448 // tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
\r
452 tmp.endCol = Color.gray;
\r
458 public void drawScene(Graphics g) {
\r
459 // Sort the bonds by z coord
\r
460 Vector bonds = new Vector();
\r
462 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
464 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
\r
466 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
468 for (int i = 0; i < tmp.size(); i++)
\r
470 bonds.addElement(tmp.elementAt(i));
\r
476 Zsort.Zsort(bonds);
\r
479 for (int i = 0; i < bonds.size(); i++) {
\r
480 Bond tmpBond = (Bond) bonds.elementAt(i);
\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
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
492 xmid = (xend + xstart) / 2;
\r
493 ymid = (yend + ystart) / 2;
\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
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
506 g.setColor(tmpBond.endCol.darker());
\r
507 drawLine(g, xmid, ymid, xend, yend);
\r
509 g.setColor(tmpBond.startCol);
\r
510 drawLine(g, xstart, ystart, xmid, ymid);
\r
512 g.setColor(tmpBond.endCol);
\r
513 drawLine(g, xmid, ymid, xend, yend);
\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
523 g.setColor(Color.green);
\r
524 drawLine(g, xstart, ystart, xend, yend);
\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
532 drawLine(g, xstart, ystart, xend, yend);
\r
537 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
\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
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
550 g.drawLine(x1, y1, x2, y2);
\r
554 public Dimension minimumsize() {
\r
558 public Dimension preferredsize() {
\r
562 public void doKeyPressed(KeyEvent evt)
\r
564 if (evt.getKeyCode() == KeyEvent.VK_UP)
\r
566 scale = (float) (scale * 1.1);
\r
567 redrawneeded = true;
\r
570 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
\r
572 scale = (float) (scale * 0.9);
\r
573 redrawneeded = true;
\r
578 public void mousePressed(MouseEvent e) {
\r
579 myAtom fatom = findAtom(e.getX(), e.getY());
\r
582 fatom.isSelected = !fatom.isSelected;
\r
583 redrawneeded = true;
\r
593 public void mouseMoved(MouseEvent e) {
\r
595 myAtom fatom = findAtom(e.getX(), e.getY());
\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
603 int index = seqcanvas.getViewport().getAlignment().findIndex(sequence);
\r
605 seqcanvas.highlightSearchResults(new int[]{index, pos, pos});
\r
608 seqcanvas.highlightSearchResults(null);
\r
610 if (fatom != null) {
\r
613 appletToolTip = fatom.resNumber+" "+ fatom.resName;
\r
614 redrawneeded = true;
\r
617 appletToolTip = null;
\r
618 redrawneeded = true;
\r
623 public void mouseClicked(MouseEvent e) {
\r
626 public void mouseEntered(MouseEvent e) {
\r
629 public void mouseExited(MouseEvent e) {
\r
632 public void mouseDragged(MouseEvent evt) {
\r
633 int x = evt.getX();
\r
634 int y = evt.getY();
\r
638 MCMatrix objmat = new MCMatrix(3, 3);
\r
639 objmat.setIdentity();
\r
641 if ((evt.getModifiers() & Event.META_MASK) != 0) {
\r
642 objmat.rotatez((float) ((mx - omx)));
\r
644 objmat.rotatex((float) ((my - omy)));
\r
645 objmat.rotatey((float) ((omx - mx)));
\r
649 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
650 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
652 for (int i = 0; i < bonds.size(); i++) {
\r
653 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
655 //Translate the bond so the centre is 0,0,0
\r
656 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
\r
658 //Now apply the rotation matrix
\r
659 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
\r
660 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
\r
662 //Now translate back again
\r
663 tmpBond.translate(centre[0], centre[1], centre[2]);
\r
674 redrawneeded = true;
\r
679 public void mouseReleased(MouseEvent evt) {
\r
684 void drawLabels(Graphics g) {
\r
686 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
688 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
690 if (chain.isVisible)
\r
692 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
694 for (int i = 0; i < bonds.size(); i++)
\r
696 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
698 if (tmpBond.at1.isSelected)
\r
700 labelAtom(g, tmpBond, 1);
\r
703 if (tmpBond.at2.isSelected)
\r
706 labelAtom(g, tmpBond, 2);
\r
713 public void labelAtom(Graphics g, Bond b, int n) {
\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
722 g.setColor(Color.red);
\r
723 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
\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
732 g.setColor(Color.red);
\r
733 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
\r
737 int foundchain = -1;
\r
738 public myAtom findAtom(int x, int y) {
\r
739 myAtom fatom = null;
\r
743 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
744 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
746 if (chain.isVisible) {
\r
747 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
749 for (int i = 0; i < bonds.size(); i++) {
\r
750 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
752 int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
753 (getSize().width / 2));
\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
759 if (Math.abs(truey - y) <= 2)
\r
761 fatom = tmpBond.at1;
\r
769 if (fatom != null) //)&& chain.ds != null)
\r
771 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
778 public void update(Graphics g)
\r