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
33 import javax.swing.*;
\r
36 public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListener
\r
38 MCMatrix idmat = new MCMatrix(3, 3);
\r
39 MCMatrix objmat = new MCMatrix(3, 3);
\r
40 boolean redrawneeded = true;
\r
50 float[] centre = new float[3];
\r
51 float[] width = new float[3];
\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
68 Font font = new Font("Helvetica", Font.PLAIN, 10);
\r
69 jalview.gui.SequenceRenderer sr;
\r
70 jalview.gui.FeatureRenderer fr;
\r
77 public void setPDBFile(PDBfile pdb, Sequence sequence,
\r
78 jalview.gui.SequenceRenderer sr,
\r
79 jalview.gui.FeatureRenderer fr)
\r
92 for (int i = 0; i < pdb.chains.size(); i++) {
\r
93 // Now lets compare the sequences to get
\r
94 // the start and end points.
\r
95 // Align the sequence to the pdb
\r
96 AlignSeq as = new AlignSeq(sequence,
\r
97 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
\r
98 as.calcScoreMatrix();
\r
99 as.traceAlignment();
\r
100 as.printAlignment();
\r
103 if (as.maxscore > max) {
\r
107 pdbstart = as.seq2start;
\r
108 pdbend = as.seq2end;
\r
109 seqstart = as.seq1start + sequence.getStart()-1;
\r
110 seqend = as.seq1end + sequence.getEnd()-1;
\r
113 System.out.println("PDB start/end " + pdbstart + " " + pdbend);
\r
114 System.out.println("SEQ start/end "+ seqstart + " " + seqend);
\r
117 ((PDBChain) pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;
\r
118 ((PDBChain) pdb.chains.elementAt(maxchain)).pdbend = pdbend;
\r
119 ((PDBChain) pdb.chains.elementAt(maxchain)).seqstart = seqstart;
\r
120 ((PDBChain) pdb.chains.elementAt(maxchain)).seqend = seqend;
\r
121 ((PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;
\r
122 ((PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;
\r
125 this.prefsize = new Dimension(getWidth(), getHeight());
\r
127 //Initialize the matrices to identity
\r
128 for (int i = 0; i < 3; i++) {
\r
129 for (int j = 0; j < 3; j++) {
\r
131 idmat.addElement(i, j, 0);
\r
132 objmat.addElement(i, j, 0);
\r
134 idmat.addElement(i, j, 1);
\r
135 objmat.addElement(i, j, 1);
\r
140 addMouseMotionListener(this);
\r
141 addMouseListener(this);
\r
146 scale = findScale();
\r
148 System.out.println("Scale factor = " + scale);
\r
150 updateSeqColours();
\r
151 ToolTipManager.sharedInstance().registerComponent(this);
\r
152 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
153 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
157 public void deleteBonds() {
\r
169 for (int i = 0; i < pdb.chains.size(); i++) {
\r
170 ((PDBChain) pdb.chains.elementAt(i)).bonds = null;
\r
174 public void findWidth() {
\r
175 float[] max = new float[3];
\r
176 float[] min = new float[3];
\r
178 max[0] = (float) -1e30;
\r
179 max[1] = (float) -1e30;
\r
180 max[2] = (float) -1e30;
\r
182 min[0] = (float) 1e30;
\r
183 min[1] = (float) 1e30;
\r
184 min[2] = (float) 1e30;
\r
186 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
187 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
188 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
190 for (int i = 0; i < bonds.size(); i++) {
\r
191 Bond tmp = (Bond) bonds.elementAt(i);
\r
193 if (tmp.start[0] >= max[0]) {
\r
194 max[0] = tmp.start[0];
\r
197 if (tmp.start[1] >= max[1]) {
\r
198 max[1] = tmp.start[1];
\r
201 if (tmp.start[2] >= max[2]) {
\r
202 max[2] = tmp.start[2];
\r
205 if (tmp.start[0] <= min[0]) {
\r
206 min[0] = tmp.start[0];
\r
209 if (tmp.start[1] <= min[1]) {
\r
210 min[1] = tmp.start[1];
\r
213 if (tmp.start[2] <= min[2]) {
\r
214 min[2] = tmp.start[2];
\r
217 if (tmp.end[0] >= max[0]) {
\r
218 max[0] = tmp.end[0];
\r
221 if (tmp.end[1] >= max[1]) {
\r
222 max[1] = tmp.end[1];
\r
225 if (tmp.end[2] >= max[2]) {
\r
226 max[2] = tmp.end[2];
\r
229 if (tmp.end[0] <= min[0]) {
\r
230 min[0] = tmp.end[0];
\r
233 if (tmp.end[1] <= min[1]) {
\r
234 min[1] = tmp.end[1];
\r
237 if (tmp.end[2] <= min[2]) {
\r
238 min[2] = tmp.end[2];
\r
244 System.out.println("xmax " + max[0] + " min " + min[0]);
\r
245 System.out.println("ymax " + max[1] + " min " + min[1]);
\r
246 System.out.println("zmax " + max[2] + " min " + min[2]);
\r
248 width[0] = (float) Math.abs(max[0] - min[0]);
\r
249 width[1] = (float) Math.abs(max[1] - min[1]);
\r
250 width[2] = (float) Math.abs(max[2] - min[2]);
\r
252 maxwidth = width[0];
\r
254 if (width[1] > width[0]) {
\r
255 maxwidth = width[1];
\r
258 if (width[2] > width[1]) {
\r
259 maxwidth = width[2];
\r
262 System.out.println("Maxwidth = " + maxwidth);
\r
265 public float findScale() {
\r
270 if (getWidth() != 0) {
\r
271 width = getWidth();
\r
272 height = getHeight();
\r
274 width = prefsize.width;
\r
275 height = prefsize.height;
\r
278 if (width < height) {
\r
284 return (float) (dim / (1.5d * maxwidth));
\r
287 public void findCentre() {
\r
294 //Find centre coordinate
\r
295 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
296 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
297 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
299 bsize += bonds.size();
\r
301 for (int i = 0; i < bonds.size(); i++) {
\r
302 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
\r
303 ((Bond) bonds.elementAt(i)).end[0];
\r
305 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
\r
306 ((Bond) bonds.elementAt(i)).end[1];
\r
308 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
\r
309 ((Bond) bonds.elementAt(i)).end[2];
\r
314 centre[0] = xtot / (2 * (float) bsize);
\r
315 centre[1] = ytot / (2 * (float) bsize);
\r
316 centre[2] = ztot / (2 * (float) bsize);
\r
319 public void paintComponent(Graphics g) {
\r
323 g.setColor(Color.black);
\r
324 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
325 g.drawString("Retrieving PDB data....", 20, getHeight()/2);
\r
331 //Only create the image at the beginning -
\r
332 //this saves much memory usage
\r
333 if ((img == null) || (prefsize.width != getWidth()) ||
\r
334 (prefsize.height != getHeight())) {
\r
335 prefsize.width = getWidth();
\r
336 prefsize.height = getHeight();
\r
338 scale = findScale();
\r
339 img = createImage(prefsize.width, prefsize.height);
\r
340 ig = img.getGraphics();
\r
341 Graphics2D ig2 = (Graphics2D) ig;
\r
343 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
344 RenderingHints.VALUE_ANTIALIAS_ON);
\r
347 redrawneeded = true;
\r
353 drawAll(ig, prefsize.width, prefsize.height);
\r
354 redrawneeded = false;
\r
357 g.drawImage(img, 0, 0, this);
\r
360 public void drawAll(Graphics g, int width, int height)
\r
362 g.setColor(Color.black);
\r
363 g.fillRect(0, 0, width, height);
\r
369 public void updateSeqColours()
\r
373 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
375 ( (PDBChain) pdb.chains.elementAt(ii)).colourBySequence(sr, fr);
\r
384 public void drawScene(Graphics g) {
\r
385 // Sort the bonds by z coord
\r
386 Vector bonds = new Vector();
\r
388 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
390 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
\r
392 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
394 for (int i = 0; i < tmp.size(); i++)
\r
396 bonds.addElement(tmp.elementAt(i));
\r
402 Zsort.Zsort(bonds);
\r
405 for (int i = 0; i < bonds.size(); i++) {
\r
406 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
408 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
410 ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
411 (getHeight() / 2));
\r
413 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +
\r
415 yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +
\r
416 (getHeight() / 2));
\r
418 xmid = (xend + xstart) / 2;
\r
419 ymid = (yend + ystart) / 2;
\r
421 if (depthcue && !bymolecule) {
\r
422 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
423 g.setColor(tmpBond.startCol.darker().darker());
\r
424 drawLine(g, xstart, ystart, xmid, ymid);
\r
426 g.setColor(tmpBond.endCol.darker().darker());
\r
427 drawLine(g, xmid, ymid, xend, yend);
\r
428 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
429 g.setColor(tmpBond.startCol.darker());
\r
430 drawLine(g, xstart, ystart, xmid, ymid);
\r
432 g.setColor(tmpBond.endCol.darker());
\r
433 drawLine(g, xmid, ymid, xend, yend);
\r
435 g.setColor(tmpBond.startCol);
\r
436 drawLine(g, xstart, ystart, xmid, ymid);
\r
438 g.setColor(tmpBond.endCol);
\r
439 drawLine(g, xmid, ymid, xend, yend);
\r
441 } else if (depthcue && bymolecule) {
\r
442 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
443 g.setColor(Color.green.darker().darker());
\r
444 drawLine(g, xstart, ystart, xend, yend);
\r
445 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
446 g.setColor(Color.green.darker());
\r
447 drawLine(g, xstart, ystart, xend, yend);
\r
449 g.setColor(Color.green);
\r
450 drawLine(g, xstart, ystart, xend, yend);
\r
452 } else if (!depthcue && !bymolecule) {
\r
453 g.setColor(tmpBond.startCol);
\r
454 drawLine(g, xstart, ystart, xmid, ymid);
\r
455 g.setColor(tmpBond.endCol);
\r
456 drawLine(g, xmid, ymid, xend, yend);
\r
458 drawLine(g, xstart, ystart, xend, yend);
\r
463 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
\r
465 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
\r
466 g.drawLine(x1, y1, x2, y2);
\r
467 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
\r
468 g.drawLine(x1, y1 - 1, x2, y2 - 1);
\r
470 g.setColor(g.getColor().brighter());
\r
471 g.drawLine(x1, y1, x2, y2);
\r
472 g.drawLine(x1 + 1, y1, x2 + 1, y2);
\r
473 g.drawLine(x1 - 1, y1, x2 - 1, y2);
\r
476 g.drawLine(x1, y1, x2, y2);
\r
480 public Dimension minimumsize() {
\r
484 public Dimension preferredsize() {
\r
488 public void keyPressed(KeyEvent evt) {
\r
489 int key = evt.getKeyChar();
\r
490 System.out.println(key);
\r
491 if (evt.getKeyCode() == KeyEvent.VK_UP)
\r
493 scale = (float) (scale * 1.1);
\r
494 redrawneeded = true;
\r
497 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
\r
499 scale = (float) (scale * 0.9);
\r
500 redrawneeded = true;
\r
505 public void mousePressed(MouseEvent e) {
\r
506 myAtom fatom = findAtom(e.getX(), e.getY());
\r
509 fatom.isSelected = !fatom.isSelected;
\r
510 redrawneeded = true;
\r
520 public void mouseMoved(MouseEvent e) {
\r
522 myAtom fatom = findAtom(e.getX(), e.getY());
\r
524 if (fatom != null) {
\r
525 this.setToolTipText(fatom.resNumber+" "+ fatom.resName);
\r
527 this.setToolTipText("");
\r
531 public void mouseClicked(MouseEvent e) {
\r
534 public void mouseEntered(MouseEvent e) {
\r
537 public void mouseExited(MouseEvent e) {
\r
540 public void mouseDragged(MouseEvent evt) {
\r
541 int x = evt.getX();
\r
542 int y = evt.getY();
\r
546 MCMatrix objmat = new MCMatrix(3, 3);
\r
547 objmat.setIdentity();
\r
549 if ((evt.getModifiers() & Event.META_MASK) != 0) {
\r
550 objmat.rotatez((float) ((mx - omx)));
\r
552 objmat.rotatex((float) ((my - omy)));
\r
553 objmat.rotatey((float) ((omx - mx)));
\r
557 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
558 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
560 for (int i = 0; i < bonds.size(); i++) {
\r
561 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
563 //Translate the bond so the centre is 0,0,0
\r
564 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
\r
566 //Now apply the rotation matrix
\r
567 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
\r
568 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
\r
570 //Now translate back again
\r
571 tmpBond.translate(centre[0], centre[1], centre[2]);
\r
582 redrawneeded = true;
\r
587 public void mouseReleased(MouseEvent evt) {
\r
592 void drawLabels(Graphics g) {
\r
594 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
595 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
597 if (chain.isVisible) {
\r
598 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
600 for (int i = 0; i < bonds.size(); i++) {
\r
601 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
603 if (tmpBond.at1.isSelected) {
\r
604 labelAtom(g, tmpBond, 1);
\r
607 if (tmpBond.at2.isSelected) {
\r
608 labelAtom(g, tmpBond, 2);
\r
615 public void labelAtom(Graphics g, Bond b, int n) {
\r
619 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
\r
621 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
\r
622 (getHeight() / 2));
\r
624 g.setColor(Color.red);
\r
625 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
\r
629 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
\r
631 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
\r
632 (getHeight() / 2));
\r
634 g.setColor(Color.red);
\r
635 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
\r
639 public myAtom findAtom(int x, int y) {
\r
640 myAtom fatom = null;
\r
642 int foundchain = -1;
\r
644 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
645 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
647 if (chain.isVisible) {
\r
648 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
650 for (int i = 0; i < bonds.size(); i++) {
\r
651 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
653 int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
656 if (Math.abs(truex - x) <= 2) {
\r
657 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
658 (getHeight() / 2));
\r
660 if (Math.abs(truey - y) <= 2)
\r
662 fatom = tmpBond.at1;
\r
669 if (fatom != null) //)&& chain.ds != null)
\r
671 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
674 // int tmp = chain.ds.seqstart + fatom.resNumber - chain.offset;
\r
675 // int pos = chain.ds.findIndex(tmp);
\r
676 // System.out.println("Found seq " + chain.ds.name + " " + tmp + " " + pos);
\r
683 public void update(Graphics g) {
\r