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
71 jalview.gui.SeqCanvas seqcanvas;
\r
72 public Sequence sequence;
\r
73 final StringBuffer mappingDetails = new StringBuffer();
\r
76 public PDBCanvas(jalview.gui.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.setPDBCanvas(this);
\r
86 public void setPDBFile(PDBfile pdb)
\r
97 for (int i = 0; i < pdb.chains.size(); i++)
\r
100 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence());
\r
101 mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");
\r
103 // Now lets compare the sequences to get
\r
104 // the start and end points.
\r
105 // Align the sequence to the pdb
\r
106 AlignSeq as = new AlignSeq(sequence,
\r
107 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
\r
108 as.calcScoreMatrix();
\r
109 as.traceAlignment();
\r
110 PrintStream ps = new PrintStream(System.out)
\r
112 public void print(String x) {
\r
113 mappingDetails.append(x);
\r
115 public void println()
\r
117 mappingDetails.append("\n");
\r
121 as.printAlignment(ps);
\r
123 if (as.maxscore > max) {
\r
127 pdbstart = as.seq2start;
\r
128 pdbend = as.seq2end;
\r
129 seqstart = as.seq1start + sequence.getStart()-1;
\r
130 seqend = as.seq1end + sequence.getEnd()-1;
\r
133 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
\r
134 mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);
\r
137 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
\r
139 mainchain.pdbstart = pdbstart;
\r
140 mainchain.pdbend = pdbend;
\r
141 mainchain.seqstart = seqstart;
\r
142 mainchain.seqend = seqend;
\r
143 mainchain.isVisible = true;
\r
144 mainchain.sequence = sequence;
\r
147 this.prefsize = new Dimension(getWidth(), getHeight());
\r
149 //Initialize the matrices to identity
\r
150 for (int i = 0; i < 3; i++) {
\r
151 for (int j = 0; j < 3; j++) {
\r
153 idmat.addElement(i, j, 0);
\r
154 objmat.addElement(i, j, 0);
\r
156 idmat.addElement(i, j, 1);
\r
157 objmat.addElement(i, j, 1);
\r
162 addMouseMotionListener(this);
\r
163 addMouseListener(this);
\r
165 addMouseWheelListener(new MouseWheelListener()
\r
167 public void mouseWheelMoved(MouseWheelEvent e)
\r
169 if (e.getWheelRotation() > 0)
\r
171 scale = (float) (scale * 1.1);
\r
172 redrawneeded = true;
\r
178 scale = (float) (scale * 0.9);
\r
179 redrawneeded = true;
\r
186 SequenceGroup sg = new SequenceGroup("PDB",
\r
187 null, true,true,false,
\r
188 sequence.findIndex(seqstart-1),
\r
189 sequence.findIndex(seqend-1));
\r
190 sg.addSequence(sequence, false);
\r
191 sg.setOutlineColour(Color.black);
\r
192 seqcanvas.getViewport().getAlignment().addGroup(sg);
\r
198 scale = findScale();
\r
201 updateSeqColours();
\r
202 ToolTipManager.sharedInstance().registerComponent(this);
\r
203 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
204 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
208 public void deleteBonds() {
\r
220 for (int i = 0; i < pdb.chains.size(); i++) {
\r
221 ((PDBChain) pdb.chains.elementAt(i)).bonds = null;
\r
225 public void findWidth() {
\r
226 float[] max = new float[3];
\r
227 float[] min = new float[3];
\r
229 max[0] = (float) -1e30;
\r
230 max[1] = (float) -1e30;
\r
231 max[2] = (float) -1e30;
\r
233 min[0] = (float) 1e30;
\r
234 min[1] = (float) 1e30;
\r
235 min[2] = (float) 1e30;
\r
237 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
238 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
239 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
241 for (int i = 0; i < bonds.size(); i++) {
\r
242 Bond tmp = (Bond) bonds.elementAt(i);
\r
244 if (tmp.start[0] >= max[0]) {
\r
245 max[0] = tmp.start[0];
\r
248 if (tmp.start[1] >= max[1]) {
\r
249 max[1] = tmp.start[1];
\r
252 if (tmp.start[2] >= max[2]) {
\r
253 max[2] = tmp.start[2];
\r
256 if (tmp.start[0] <= min[0]) {
\r
257 min[0] = tmp.start[0];
\r
260 if (tmp.start[1] <= min[1]) {
\r
261 min[1] = tmp.start[1];
\r
264 if (tmp.start[2] <= min[2]) {
\r
265 min[2] = tmp.start[2];
\r
268 if (tmp.end[0] >= max[0]) {
\r
269 max[0] = tmp.end[0];
\r
272 if (tmp.end[1] >= max[1]) {
\r
273 max[1] = tmp.end[1];
\r
276 if (tmp.end[2] >= max[2]) {
\r
277 max[2] = tmp.end[2];
\r
280 if (tmp.end[0] <= min[0]) {
\r
281 min[0] = tmp.end[0];
\r
284 if (tmp.end[1] <= min[1]) {
\r
285 min[1] = tmp.end[1];
\r
288 if (tmp.end[2] <= min[2]) {
\r
289 min[2] = tmp.end[2];
\r
295 System.out.println("xmax " + max[0] + " min " + min[0]);
\r
296 System.out.println("ymax " + max[1] + " min " + min[1]);
\r
297 System.out.println("zmax " + max[2] + " min " + min[2]);*/
\r
299 width[0] = (float) Math.abs(max[0] - min[0]);
\r
300 width[1] = (float) Math.abs(max[1] - min[1]);
\r
301 width[2] = (float) Math.abs(max[2] - min[2]);
\r
303 maxwidth = width[0];
\r
305 if (width[1] > width[0]) {
\r
306 maxwidth = width[1];
\r
309 if (width[2] > width[1]) {
\r
310 maxwidth = width[2];
\r
313 // System.out.println("Maxwidth = " + maxwidth);
\r
316 public float findScale() {
\r
321 if (getWidth() != 0) {
\r
322 width = getWidth();
\r
323 height = getHeight();
\r
325 width = prefsize.width;
\r
326 height = prefsize.height;
\r
329 if (width < height) {
\r
335 return (float) (dim / (1.5d * maxwidth));
\r
338 public void findCentre() {
\r
345 //Find centre coordinate
\r
346 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
347 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
348 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
350 bsize += bonds.size();
\r
352 for (int i = 0; i < bonds.size(); i++) {
\r
353 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
\r
354 ((Bond) bonds.elementAt(i)).end[0];
\r
356 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
\r
357 ((Bond) bonds.elementAt(i)).end[1];
\r
359 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
\r
360 ((Bond) bonds.elementAt(i)).end[2];
\r
365 centre[0] = xtot / (2 * (float) bsize);
\r
366 centre[1] = ytot / (2 * (float) bsize);
\r
367 centre[2] = ztot / (2 * (float) bsize);
\r
370 public void paintComponent(Graphics g) {
\r
372 super.paintComponent(g);
\r
376 g.setColor(Color.black);
\r
377 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
378 g.drawString("Retrieving PDB data....", 20, getHeight()/2);
\r
383 //Only create the image at the beginning -
\r
384 //this saves much memory usage
\r
385 if ((img == null) || (prefsize.width != getWidth()) ||
\r
386 (prefsize.height != getHeight())) {
\r
387 prefsize.width = getWidth();
\r
388 prefsize.height = getHeight();
\r
390 scale = findScale();
\r
391 img = createImage(prefsize.width, prefsize.height);
\r
392 ig = img.getGraphics();
\r
393 Graphics2D ig2 = (Graphics2D) ig;
\r
395 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
396 RenderingHints.VALUE_ANTIALIAS_ON);
\r
399 redrawneeded = true;
\r
405 drawAll(ig, prefsize.width, prefsize.height);
\r
406 redrawneeded = false;
\r
409 g.drawImage(img, 0, 0, this);
\r
412 public void drawAll(Graphics g, int width, int height)
\r
414 g.setColor(Color.black);
\r
415 g.fillRect(0, 0, width, height);
\r
421 public void updateSeqColours()
\r
423 if(bysequence && pdb!=null)
\r
425 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
427 colourBySequence( (PDBChain) pdb.chains.elementAt(ii) );
\r
436 // This method has been taken out of PDBChain to allow
\r
437 // Applet and Application specific sequence renderers to be used
\r
438 void colourBySequence(PDBChain chain)
\r
440 for (int i = 0; i < chain.bonds.size(); i++)
\r
442 Bond tmp = (Bond) chain.bonds.elementAt(i);
\r
444 if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
445 (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
\r
447 int pos = chain.seqstart +
\r
448 (tmp.at1.resNumber - chain.pdbstart - chain.offset);
\r
450 int index = sequence.findIndex(pos);
\r
452 tmp.startCol = sr.findSequenceColour(tmp.startCol, sequence, index);
\r
454 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
\r
459 tmp.startCol = Color.gray;
\r
462 if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
463 (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
\r
465 int pos = chain.seqstart +
\r
466 (tmp.at2.resNumber - chain.pdbstart - chain.offset);
\r
467 int index = sequence.findIndex(pos);
\r
469 tmp.endCol = sr.findSequenceColour(tmp.endCol, sequence, index);
\r
470 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
\r
474 tmp.endCol = Color.gray;
\r
480 public void drawScene(Graphics g) {
\r
481 // Sort the bonds by z coord
\r
482 Vector bonds = new Vector();
\r
484 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
486 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
\r
488 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
490 for (int i = 0; i < tmp.size(); i++)
\r
492 bonds.addElement(tmp.elementAt(i));
\r
498 Zsort.Zsort(bonds);
\r
502 for (int i = 0; i < bonds.size(); i++) {
\r
503 tmpBond = (Bond) bonds.elementAt(i);
\r
505 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
507 ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
508 (getHeight() / 2));
\r
510 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +
\r
512 yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +
\r
513 (getHeight() / 2));
\r
515 xmid = (xend + xstart) / 2;
\r
516 ymid = (yend + ystart) / 2;
\r
518 if (depthcue && !bymolecule) {
\r
519 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
520 g.setColor(tmpBond.startCol.darker().darker());
\r
521 drawLine(g, xstart, ystart, xmid, ymid);
\r
523 g.setColor(tmpBond.endCol.darker().darker());
\r
524 drawLine(g, xmid, ymid, xend, yend);
\r
525 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
526 g.setColor(tmpBond.startCol.darker());
\r
527 drawLine(g, xstart, ystart, xmid, ymid);
\r
529 g.setColor(tmpBond.endCol.darker());
\r
530 drawLine(g, xmid, ymid, xend, yend);
\r
532 g.setColor(tmpBond.startCol);
\r
533 drawLine(g, xstart, ystart, xmid, ymid);
\r
535 g.setColor(tmpBond.endCol);
\r
536 drawLine(g, xmid, ymid, xend, yend);
\r
538 } else if (depthcue && bymolecule) {
\r
539 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
540 g.setColor(Color.green.darker().darker());
\r
541 drawLine(g, xstart, ystart, xend, yend);
\r
542 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
543 g.setColor(Color.green.darker());
\r
544 drawLine(g, xstart, ystart, xend, yend);
\r
546 g.setColor(Color.green);
\r
547 drawLine(g, xstart, ystart, xend, yend);
\r
549 } else if (!depthcue && !bymolecule) {
\r
550 g.setColor(tmpBond.startCol);
\r
551 drawLine(g, xstart, ystart, xmid, ymid);
\r
552 g.setColor(tmpBond.endCol);
\r
553 drawLine(g, xmid, ymid, xend, yend);
\r
555 drawLine(g, xstart, ystart, xend, yend);
\r
558 if(highlightBond1!=null && highlightBond1==tmpBond)
\r
560 g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter());
\r
561 drawLine(g, xmid, ymid, xend, yend);
\r
564 if(highlightBond2!=null && highlightBond2==tmpBond)
\r
566 g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter());
\r
567 drawLine(g, xstart, ystart, xmid, ymid);
\r
575 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
\r
577 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
\r
578 g.drawLine(x1, y1, x2, y2);
\r
579 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
\r
580 g.drawLine(x1, y1 - 1, x2, y2 - 1);
\r
582 g.setColor(g.getColor().brighter());
\r
583 g.drawLine(x1, y1, x2, y2);
\r
584 g.drawLine(x1 + 1, y1, x2 + 1, y2);
\r
585 g.drawLine(x1 - 1, y1, x2 - 1, y2);
\r
588 g.drawLine(x1, y1, x2, y2);
\r
592 public Dimension minimumsize() {
\r
596 public Dimension preferredsize() {
\r
600 public void keyPressed(KeyEvent evt)
\r
602 if (evt.getKeyCode() == KeyEvent.VK_UP)
\r
604 scale = (float) (scale * 1.1);
\r
605 redrawneeded = true;
\r
608 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
\r
610 scale = (float) (scale * 0.9);
\r
611 redrawneeded = true;
\r
616 public void mousePressed(MouseEvent e) {
\r
617 Atom fatom = findAtom(e.getX(), e.getY());
\r
620 fatom.isSelected = !fatom.isSelected;
\r
621 redrawneeded = true;
\r
631 public void mouseMoved(MouseEvent e) {
\r
633 Atom fatom = findAtom(e.getX(), e.getY());
\r
635 PDBChain chain = null;
\r
638 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
639 if(chain == mainchain)
\r
641 int pos = chain.seqstart +
\r
642 (fatom.resNumber - chain.pdbstart - chain.offset) + 1;
\r
644 int index = seqcanvas.getViewport().getAlignment().findIndex(
\r
647 seqcanvas.highlightSearchResults(new int[]
\r
648 {index, pos, pos});
\r
652 seqcanvas.highlightSearchResults(null);
\r
656 this.setToolTipText(chain.id+":"+ fatom.resNumber+" "+ fatom.resName);
\r
659 this.setToolTipText("");
\r
663 public void mouseClicked(MouseEvent e) {
\r
666 public void mouseEntered(MouseEvent e) {
\r
669 public void mouseExited(MouseEvent e) {
\r
672 public void mouseDragged(MouseEvent evt) {
\r
673 int x = evt.getX();
\r
674 int y = evt.getY();
\r
678 MCMatrix objmat = new MCMatrix(3, 3);
\r
679 objmat.setIdentity();
\r
681 if ((evt.getModifiers() & Event.META_MASK) != 0) {
\r
682 objmat.rotatez((float) ((mx - omx)));
\r
684 objmat.rotatex((float) ((my - omy)));
\r
685 objmat.rotatey((float) ((omx - mx)));
\r
689 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
690 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
692 for (int i = 0; i < bonds.size(); i++) {
\r
693 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
695 //Translate the bond so the centre is 0,0,0
\r
696 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
\r
698 //Now apply the rotation matrix
\r
699 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
\r
700 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
\r
702 //Now translate back again
\r
703 tmpBond.translate(centre[0], centre[1], centre[2]);
\r
714 redrawneeded = true;
\r
719 public void mouseReleased(MouseEvent evt) {
\r
724 void drawLabels(Graphics g) {
\r
726 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
728 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
730 if (chain.isVisible)
\r
732 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
734 for (int i = 0; i < bonds.size(); i++)
\r
736 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
738 if (tmpBond.at1.isSelected)
\r
740 labelAtom(g, tmpBond, 1);
\r
743 if (tmpBond.at2.isSelected)
\r
746 labelAtom(g, tmpBond, 2);
\r
753 public void labelAtom(Graphics g, Bond b, int n) {
\r
757 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
\r
759 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
\r
760 (getHeight() / 2));
\r
762 g.setColor(Color.red);
\r
763 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
\r
767 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
\r
769 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
\r
770 (getHeight() / 2));
\r
772 g.setColor(Color.red);
\r
773 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
\r
777 int foundchain = -1;
\r
778 public Atom findAtom(int x, int y) {
\r
783 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
785 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
789 if (chain.isVisible)
\r
791 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
793 for (int i = 0; i < bonds.size(); i++)
\r
795 tmpBond = (Bond) bonds.elementAt(i);
\r
797 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
800 if (Math.abs(truex - x) <= 2)
\r
802 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
803 (getHeight() / 2));
\r
805 if (Math.abs(truey - y) <= 2)
\r
807 fatom = tmpBond.at1;
\r
814 // Still here? Maybe its the last bond
\r
816 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
\r
819 if (Math.abs(truex - x) <= 2)
\r
821 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
\r
822 (getHeight() / 2));
\r
824 if (Math.abs(truey - y) <= 2)
\r
826 fatom = tmpBond.at2;
\r
834 if (fatom != null) //)&& chain.ds != null)
\r
836 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
843 Bond highlightBond1, highlightBond2;
\r
844 public void highlightRes(int ii)
\r
846 highlightBond1 = null;
\r
847 highlightBond2 = null;
\r
849 int index = ii - mainchain.seqstart;
\r
854 if(index<=mainchain.bonds.size())
\r
858 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
\r
861 if(index!=mainchain.bonds.size())
\r
862 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
\r
865 redrawneeded = true;
\r
869 public void setAllchainsVisible(boolean b)
\r
871 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
873 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
874 chain.isVisible = b;
\r
876 mainchain.isVisible = true;
\r