2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
26 import java.awt.event.*;
29 import jalview.analysis.*;
30 import jalview.datamodel.*;
32 public class PDBCanvas
33 extends JPanel implements MouseListener, MouseMotionListener
35 MCMatrix idmat = new MCMatrix(3, 3);
36 MCMatrix objmat = new MCMatrix(3, 3);
37 boolean redrawneeded = true;
47 float[] centre = new float[3];
48 float[] width = new float[3];
53 boolean bysequence = true;
54 boolean depthcue = true;
56 boolean bymolecule = false;
57 boolean zbuffer = true;
65 Font font = new Font("Helvetica", Font.PLAIN, 10);
66 jalview.gui.SeqCanvas seqcanvas;
67 public Sequence sequence;
68 final StringBuffer mappingDetails = new StringBuffer();
71 boolean pdbAction = false;
72 boolean seqColoursReady = false;
73 jalview.gui.FeatureRenderer fr;
74 Color backgroundColour = Color.black;
76 public PDBCanvas(jalview.gui.SeqCanvas seqcanvas, Sequence seq)
78 this.seqcanvas = seqcanvas;
82 public void setPDBFile(PDBfile pdb)
90 AlignSeq maxAlignseq = null;
92 for (int i = 0; i < pdb.chains.size(); i++)
95 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
96 ( (PDBChain) pdb.chains.elementAt(i)).sequence.
97 getSequenceAsString());
98 mappingDetails.append("\nNo of residues = " +
99 ( (PDBChain) pdb.chains.elementAt(i)).residues.size() +
102 // Now lets compare the sequences to get
103 // the start and end points.
104 // Align the sequence to the pdb
105 AlignSeq as = new AlignSeq(sequence,
106 ( (PDBChain) pdb.chains.elementAt(i)).sequence,
108 as.calcScoreMatrix();
110 PrintStream ps = new PrintStream(System.out)
112 public void print(String x)
114 mappingDetails.append(x);
117 public void println()
119 mappingDetails.append("\n");
123 as.printAlignment(ps);
125 if (as.maxscore > max)
129 pdbstart = as.seq2start;
131 seqstart = as.seq1start + sequence.getStart() - 1;
132 seqend = as.seq1end + sequence.getEnd() - 1;
136 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
137 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
140 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
142 mainchain.pdbstart = pdbstart;
143 mainchain.pdbend = pdbend;
144 mainchain.seqstart = seqstart;
145 mainchain.seqend = seqend;
146 mainchain.isVisible = true;
147 mainchain.makeExactMapping(maxAlignseq, sequence);
148 mainchain.transferRESNUMFeatures(sequence, null);
149 seqcanvas.getFeatureRenderer().featuresAdded();
151 this.prefsize = new Dimension(getWidth(), getHeight());
153 //Initialize the matrices to identity
154 for (int i = 0; i < 3; i++)
156 for (int j = 0; j < 3; j++)
160 idmat.addElement(i, j, 0);
161 objmat.addElement(i, j, 0);
165 idmat.addElement(i, j, 1);
166 objmat.addElement(i, j, 1);
171 addMouseMotionListener(this);
172 addMouseListener(this);
174 addMouseWheelListener(new MouseWheelListener()
176 public void mouseWheelMoved(MouseWheelEvent e)
178 if (e.getWheelRotation() > 0)
180 scale = (float) (scale * 1.1);
187 scale = (float) (scale * 0.9);
201 ToolTipManager.sharedInstance().registerComponent(this);
202 ToolTipManager.sharedInstance().setInitialDelay(0);
203 ToolTipManager.sharedInstance().setDismissDelay(10000);
209 seqColoursReady = false;
210 // Sort the bonds by z coord
211 visiblebonds = new Vector();
213 for (int ii = 0; ii < pdb.chains.size(); ii++)
215 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
217 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
219 for (int i = 0; i < tmp.size(); i++)
221 visiblebonds.addElement(tmp.elementAt(i));
227 seqColoursReady = true;
232 public void findWidth()
234 float[] max = new float[3];
235 float[] min = new float[3];
237 max[0] = (float) - 1e30;
238 max[1] = (float) - 1e30;
239 max[2] = (float) - 1e30;
241 min[0] = (float) 1e30;
242 min[1] = (float) 1e30;
243 min[2] = (float) 1e30;
245 for (int ii = 0; ii < pdb.chains.size(); ii++)
247 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
249 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
251 for (int i = 0; i < bonds.size(); i++)
253 Bond tmp = (Bond) bonds.elementAt(i);
255 if (tmp.start[0] >= max[0])
257 max[0] = tmp.start[0];
260 if (tmp.start[1] >= max[1])
262 max[1] = tmp.start[1];
265 if (tmp.start[2] >= max[2])
267 max[2] = tmp.start[2];
270 if (tmp.start[0] <= min[0])
272 min[0] = tmp.start[0];
275 if (tmp.start[1] <= min[1])
277 min[1] = tmp.start[1];
280 if (tmp.start[2] <= min[2])
282 min[2] = tmp.start[2];
285 if (tmp.end[0] >= max[0])
290 if (tmp.end[1] >= max[1])
295 if (tmp.end[2] >= max[2])
300 if (tmp.end[0] <= min[0])
305 if (tmp.end[1] <= min[1])
310 if (tmp.end[2] <= min[2])
318 System.out.println("xmax " + max[0] + " min " + min[0]);
319 System.out.println("ymax " + max[1] + " min " + min[1]);
320 System.out.println("zmax " + max[2] + " min " + min[2]);*/
322 width[0] = (float) Math.abs(max[0] - min[0]);
323 width[1] = (float) Math.abs(max[1] - min[1]);
324 width[2] = (float) Math.abs(max[2] - min[2]);
328 if (width[1] > width[0])
333 if (width[2] > width[1])
338 // System.out.println("Maxwidth = " + maxwidth);
341 public float findScale()
350 height = getHeight();
354 width = prefsize.width;
355 height = prefsize.height;
367 return (float) (dim / (1.5d * maxwidth));
370 public void findCentre()
378 //Find centre coordinate
379 for (int ii = 0; ii < pdb.chains.size(); ii++)
381 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
383 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
385 bsize += bonds.size();
387 for (int i = 0; i < bonds.size(); i++)
389 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
390 ( (Bond) bonds.elementAt(i)).end[0];
392 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
393 ( (Bond) bonds.elementAt(i)).end[1];
395 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
396 ( (Bond) bonds.elementAt(i)).end[2];
401 centre[0] = xtot / (2 * (float) bsize);
402 centre[1] = ytot / (2 * (float) bsize);
403 centre[2] = ztot / (2 * (float) bsize);
406 public void paintComponent(Graphics g)
408 super.paintComponent(g);
410 if (!seqColoursReady)
412 g.setColor(Color.black);
413 g.setFont(new Font("Verdana", Font.BOLD, 14));
414 g.drawString("Retrieving PDB data....", 20, getHeight() / 2);
418 //Only create the image at the beginning -
419 //this saves much memory usage
421 || (prefsize.width != getWidth())
422 || (prefsize.height != getHeight()))
425 prefsize.width = getWidth();
426 prefsize.height = getHeight();
429 img = createImage(prefsize.width, prefsize.height);
430 ig = img.getGraphics();
431 Graphics2D ig2 = (Graphics2D) ig;
433 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
434 RenderingHints.VALUE_ANTIALIAS_ON);
441 drawAll(ig, prefsize.width, prefsize.height);
442 redrawneeded = false;
445 g.drawImage(img, 0, 0, this);
450 public void drawAll(Graphics g, int width, int height)
452 g.setColor(backgroundColour);
453 g.fillRect(0, 0, width, height);
458 public void updateSeqColours()
465 // System.out.println("update seq colours");
466 if (bysequence && pdb != null)
468 for (int ii = 0; ii < pdb.chains.size(); ii++)
470 colourBySequence( (PDBChain) pdb.chains.elementAt(ii));
478 int findTrueIndex(int pos)
480 // returns the alignment position for a residue
481 int j = sequence.getStart(); // first position in PDB atom coordinate sequence
484 while ( (i < sequence.getLength()) && (j <= pos + 1))
486 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
501 return sequence.getEnd() + 1;
509 // This method has been taken out of PDBChain to allow
510 // Applet and Application specific sequence renderers to be used
511 void colourBySequence(PDBChain chain)
513 // System.out.println("colour by seq");
514 boolean showFeatures = false;
516 //NO LONGER USED SINCE APPJMOL
517 // if (seqcanvas.getViewport().getShowSequenceFeatures())
522 //NO LONGER USED SINCE APPJMOL
523 // fr = new jalview.gui.FeatureRenderer(seqcanvas.getViewport());
526 fr.transferSettings(seqcanvas.getFeatureRenderer());
530 for (int i = 0; i < chain.bonds.size(); i++)
532 tmp = (Bond) chain.bonds.elementAt(i);
534 if (chain != mainchain)
539 //if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
540 // (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
542 int index = findTrueIndex(tmp.at1.alignmentMapping);
543 //sequence.findIndex(tmp.at1.alignmentMapping);
546 tmp.startCol = seqcanvas.getSequenceRenderer().
547 getResidueBoxColour(sequence, index);
551 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
554 if (tmp.startCol == null)
556 tmp.startCol = Color.white;
561 //if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
562 // (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
565 int index = findTrueIndex(tmp.at2.alignmentMapping);
566 //sequence.findIndex( tmp.at2.alignmentMapping );
569 tmp.endCol = seqcanvas.getSequenceRenderer().
570 getResidueBoxColour(sequence, index);
574 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
577 if (tmp.endCol == null)
579 tmp.endCol = Color.white;
587 public void drawScene(Graphics g)
596 zsort.Zsort(visiblebonds);
600 for (int i = 0; i < visiblebonds.size(); i++)
602 tmpBond = (Bond) visiblebonds.elementAt(i);
604 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
606 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
609 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
611 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
614 xmid = (xend + xstart) / 2;
615 ymid = (yend + ystart) / 2;
616 if (depthcue && !bymolecule)
618 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
621 g.setColor(tmpBond.startCol.darker().darker());
622 drawLine(g, xstart, ystart, xmid, ymid);
623 g.setColor(tmpBond.endCol.darker().darker());
624 drawLine(g, xmid, ymid, xend, yend);
627 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
629 g.setColor(tmpBond.startCol.darker());
630 drawLine(g, xstart, ystart, xmid, ymid);
632 g.setColor(tmpBond.endCol.darker());
633 drawLine(g, xmid, ymid, xend, yend);
637 g.setColor(tmpBond.startCol);
638 drawLine(g, xstart, ystart, xmid, ymid);
640 g.setColor(tmpBond.endCol);
641 drawLine(g, xmid, ymid, xend, yend);
644 else if (depthcue && bymolecule)
646 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
648 g.setColor(Color.green.darker().darker());
649 drawLine(g, xstart, ystart, xend, yend);
651 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
653 g.setColor(Color.green.darker());
654 drawLine(g, xstart, ystart, xend, yend);
658 g.setColor(Color.green);
659 drawLine(g, xstart, ystart, xend, yend);
662 else if (!depthcue && !bymolecule)
664 g.setColor(tmpBond.startCol);
665 drawLine(g, xstart, ystart, xmid, ymid);
666 g.setColor(tmpBond.endCol);
667 drawLine(g, xmid, ymid, xend, yend);
671 drawLine(g, xstart, ystart, xend, yend);
674 if (highlightBond1 != null && highlightBond1 == tmpBond)
676 g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter());
677 drawLine(g, xmid, ymid, xend, yend);
680 if (highlightBond2 != null && highlightBond2 == tmpBond)
682 g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter());
683 drawLine(g, xstart, ystart, xmid, ymid);
690 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
694 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
696 g.drawLine(x1, y1, x2, y2);
697 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
698 g.drawLine(x1, y1 - 1, x2, y2 - 1);
702 g.setColor(g.getColor().brighter());
703 g.drawLine(x1, y1, x2, y2);
704 g.drawLine(x1 + 1, y1, x2 + 1, y2);
705 g.drawLine(x1 - 1, y1, x2 - 1, y2);
710 g.drawLine(x1, y1, x2, y2);
714 public Dimension minimumsize()
719 public Dimension preferredsize()
724 public void keyPressed(KeyEvent evt)
726 if (evt.getKeyCode() == KeyEvent.VK_UP)
728 scale = (float) (scale * 1.1);
732 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
734 scale = (float) (scale * 0.9);
740 public void mousePressed(MouseEvent e)
743 Atom fatom = findAtom(e.getX(), e.getY());
746 fatom.isSelected = !fatom.isSelected;
750 if (foundchain != -1)
752 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
753 if (chain == mainchain)
755 if (fatom.alignmentMapping != -1)
757 if (highlightRes == null)
759 highlightRes = new Vector();
762 if (highlightRes.contains(fatom.alignmentMapping + ""))
764 highlightRes.remove(fatom.alignmentMapping + "");
768 highlightRes.add(fatom.alignmentMapping + "");
782 public void mouseMoved(MouseEvent e)
785 if (highlightBond1 != null)
787 highlightBond1.at2.isSelected = false;
788 highlightBond2.at1.isSelected = false;
789 highlightBond1 = null;
790 highlightBond2 = null;
793 Atom fatom = findAtom(e.getX(), e.getY());
795 PDBChain chain = null;
796 if (foundchain != -1)
798 chain = (PDBChain) pdb.chains.elementAt(foundchain);
799 if (chain == mainchain)
801 highlightSeqcanvas(fatom.alignmentMapping);
807 this.setToolTipText(chain.id + ":" + fatom.resNumber + " " +
812 highlightSeqcanvas( -1);
813 this.setToolTipText(null);
817 void highlightSeqcanvas(int pos)
819 SearchResults searchResults = new SearchResults();
820 if (highlightRes != null)
822 for (int i = 0; i < highlightRes.size(); i++)
824 int a = Integer.parseInt(highlightRes.elementAt(
827 searchResults.addResult(sequence, a, a);
833 searchResults.addResult(sequence, pos + 1, pos + 1);
836 seqcanvas.highlightSearchResults(searchResults);
839 public void mouseClicked(MouseEvent e)
842 public void mouseEntered(MouseEvent e)
845 public void mouseExited(MouseEvent e)
848 public void mouseDragged(MouseEvent evt)
855 MCMatrix objmat = new MCMatrix(3, 3);
856 objmat.setIdentity();
858 if ( (evt.getModifiers() & Event.META_MASK) != 0)
860 objmat.rotatez( (float) ( (mx - omx)));
864 objmat.rotatex( (float) ( (my - omy)));
865 objmat.rotatey( (float) ( (omx - mx)));
869 for (int ii = 0; ii < pdb.chains.size(); ii++)
871 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
873 for (int i = 0; i < bonds.size(); i++)
875 Bond tmpBond = (Bond) bonds.elementAt(i);
877 //Translate the bond so the centre is 0,0,0
878 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
880 //Now apply the rotation matrix
881 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
882 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
884 //Now translate back again
885 tmpBond.translate(centre[0], centre[1], centre[2]);
901 public void mouseReleased(MouseEvent evt)
907 void drawLabels(Graphics g)
910 for (int ii = 0; ii < pdb.chains.size(); ii++)
912 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
916 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
918 for (int i = 0; i < bonds.size(); i++)
920 Bond tmpBond = (Bond) bonds.elementAt(i);
922 if (tmpBond.at1.isSelected)
924 labelAtom(g, tmpBond, 1);
927 if (tmpBond.at2.isSelected)
930 labelAtom(g, tmpBond, 2);
937 public void labelAtom(Graphics g, Bond b, int n)
940 g.setColor(Color.red);
943 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
945 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
948 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
953 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
955 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
958 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
963 public Atom findAtom(int x, int y)
969 for (int ii = 0; ii < pdb.chains.size(); ii++)
971 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
977 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
979 for (int i = 0; i < bonds.size(); i++)
981 tmpBond = (Bond) bonds.elementAt(i);
983 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
986 if (Math.abs(truex - x) <= 2)
988 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
991 if (Math.abs(truey - y) <= 2)
1000 // Still here? Maybe its the last bond
1002 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
1005 if (Math.abs(truex - x) <= 2)
1007 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
1010 if (Math.abs(truey - y) <= 2)
1012 fatom = tmpBond.at2;
1020 if (fatom != null) //)&& chain.ds != null)
1022 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1029 Bond highlightBond1, highlightBond2;
1030 public void highlightRes(int ii)
1032 if (!seqColoursReady)
1037 if (highlightRes != null
1038 && highlightRes.contains( (ii - 1) + ""))
1045 for (index = 0; index < mainchain.bonds.size(); index++)
1047 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1048 if (tmpBond.at1.alignmentMapping == ii - 1)
1050 if (highlightBond1 != null)
1052 highlightBond1.at2.isSelected = false;
1055 if (highlightBond2 != null)
1057 highlightBond2.at1.isSelected = false;
1060 highlightBond1 = null;
1061 highlightBond2 = null;
1065 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1066 highlightBond1.at2.isSelected = true;
1069 if (index != mainchain.bonds.size())
1071 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1072 highlightBond2.at1.isSelected = true;
1079 redrawneeded = true;
1083 public void setAllchainsVisible(boolean b)
1085 for (int ii = 0; ii < pdb.chains.size(); ii++)
1087 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1088 chain.isVisible = b;
1090 mainchain.isVisible = true;