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;
515 if (seqcanvas.getViewport().getShowSequenceFeatures())
520 fr = new jalview.gui.FeatureRenderer(seqcanvas.getViewport());
523 fr.transferSettings(seqcanvas.getFeatureRenderer());
527 for (int i = 0; i < chain.bonds.size(); i++)
529 tmp = (Bond) chain.bonds.elementAt(i);
531 if (chain != mainchain)
536 //if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
537 // (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
539 int index = findTrueIndex(tmp.at1.alignmentMapping);
540 //sequence.findIndex(tmp.at1.alignmentMapping);
543 tmp.startCol = seqcanvas.getSequenceRenderer().
544 getResidueBoxColour(sequence, index);
548 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
551 if (tmp.startCol == null)
553 tmp.startCol = Color.white;
558 //if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
559 // (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
562 int index = findTrueIndex(tmp.at2.alignmentMapping);
563 //sequence.findIndex( tmp.at2.alignmentMapping );
566 tmp.endCol = seqcanvas.getSequenceRenderer().
567 getResidueBoxColour(sequence, index);
571 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
574 if (tmp.endCol == null)
576 tmp.endCol = Color.white;
584 public void drawScene(Graphics g)
593 zsort.Zsort(visiblebonds);
597 for (int i = 0; i < visiblebonds.size(); i++)
599 tmpBond = (Bond) visiblebonds.elementAt(i);
601 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
603 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
606 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
608 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
611 xmid = (xend + xstart) / 2;
612 ymid = (yend + ystart) / 2;
613 if (depthcue && !bymolecule)
615 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
618 g.setColor(tmpBond.startCol.darker().darker());
619 drawLine(g, xstart, ystart, xmid, ymid);
620 g.setColor(tmpBond.endCol.darker().darker());
621 drawLine(g, xmid, ymid, xend, yend);
624 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
626 g.setColor(tmpBond.startCol.darker());
627 drawLine(g, xstart, ystart, xmid, ymid);
629 g.setColor(tmpBond.endCol.darker());
630 drawLine(g, xmid, ymid, xend, yend);
634 g.setColor(tmpBond.startCol);
635 drawLine(g, xstart, ystart, xmid, ymid);
637 g.setColor(tmpBond.endCol);
638 drawLine(g, xmid, ymid, xend, yend);
641 else if (depthcue && bymolecule)
643 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
645 g.setColor(Color.green.darker().darker());
646 drawLine(g, xstart, ystart, xend, yend);
648 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
650 g.setColor(Color.green.darker());
651 drawLine(g, xstart, ystart, xend, yend);
655 g.setColor(Color.green);
656 drawLine(g, xstart, ystart, xend, yend);
659 else if (!depthcue && !bymolecule)
661 g.setColor(tmpBond.startCol);
662 drawLine(g, xstart, ystart, xmid, ymid);
663 g.setColor(tmpBond.endCol);
664 drawLine(g, xmid, ymid, xend, yend);
668 drawLine(g, xstart, ystart, xend, yend);
671 if (highlightBond1 != null && highlightBond1 == tmpBond)
673 g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter());
674 drawLine(g, xmid, ymid, xend, yend);
677 if (highlightBond2 != null && highlightBond2 == tmpBond)
679 g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter());
680 drawLine(g, xstart, ystart, xmid, ymid);
687 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
691 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
693 g.drawLine(x1, y1, x2, y2);
694 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
695 g.drawLine(x1, y1 - 1, x2, y2 - 1);
699 g.setColor(g.getColor().brighter());
700 g.drawLine(x1, y1, x2, y2);
701 g.drawLine(x1 + 1, y1, x2 + 1, y2);
702 g.drawLine(x1 - 1, y1, x2 - 1, y2);
707 g.drawLine(x1, y1, x2, y2);
711 public Dimension minimumsize()
716 public Dimension preferredsize()
721 public void keyPressed(KeyEvent evt)
723 if (evt.getKeyCode() == KeyEvent.VK_UP)
725 scale = (float) (scale * 1.1);
729 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
731 scale = (float) (scale * 0.9);
737 public void mousePressed(MouseEvent e)
740 Atom fatom = findAtom(e.getX(), e.getY());
743 fatom.isSelected = !fatom.isSelected;
747 if (foundchain != -1)
749 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
750 if (chain == mainchain)
752 if (fatom.alignmentMapping != -1)
754 if (highlightRes == null)
756 highlightRes = new Vector();
759 if (highlightRes.contains(fatom.alignmentMapping + ""))
761 highlightRes.remove(fatom.alignmentMapping + "");
765 highlightRes.add(fatom.alignmentMapping + "");
779 public void mouseMoved(MouseEvent e)
782 if (highlightBond1 != null)
784 highlightBond1.at2.isSelected = false;
785 highlightBond2.at1.isSelected = false;
786 highlightBond1 = null;
787 highlightBond2 = null;
790 Atom fatom = findAtom(e.getX(), e.getY());
792 PDBChain chain = null;
793 if (foundchain != -1)
795 chain = (PDBChain) pdb.chains.elementAt(foundchain);
796 if (chain == mainchain)
798 highlightSeqcanvas(fatom.alignmentMapping);
804 this.setToolTipText(chain.id + ":" + fatom.resNumber + " " +
809 highlightSeqcanvas( -1);
810 this.setToolTipText(null);
814 void highlightSeqcanvas(int pos)
816 SearchResults searchResults = new SearchResults();
817 if (highlightRes != null)
819 for (int i = 0; i < highlightRes.size(); i++)
821 int a = Integer.parseInt(highlightRes.elementAt(
824 searchResults.addResult(sequence, a, a);
830 searchResults.addResult(sequence, pos + 1, pos + 1);
833 seqcanvas.highlightSearchResults(searchResults);
836 public void mouseClicked(MouseEvent e)
839 public void mouseEntered(MouseEvent e)
842 public void mouseExited(MouseEvent e)
845 public void mouseDragged(MouseEvent evt)
852 MCMatrix objmat = new MCMatrix(3, 3);
853 objmat.setIdentity();
855 if ( (evt.getModifiers() & Event.META_MASK) != 0)
857 objmat.rotatez( (float) ( (mx - omx)));
861 objmat.rotatex( (float) ( (my - omy)));
862 objmat.rotatey( (float) ( (omx - mx)));
866 for (int ii = 0; ii < pdb.chains.size(); ii++)
868 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
870 for (int i = 0; i < bonds.size(); i++)
872 Bond tmpBond = (Bond) bonds.elementAt(i);
874 //Translate the bond so the centre is 0,0,0
875 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
877 //Now apply the rotation matrix
878 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
879 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
881 //Now translate back again
882 tmpBond.translate(centre[0], centre[1], centre[2]);
898 public void mouseReleased(MouseEvent evt)
904 void drawLabels(Graphics g)
907 for (int ii = 0; ii < pdb.chains.size(); ii++)
909 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
913 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
915 for (int i = 0; i < bonds.size(); i++)
917 Bond tmpBond = (Bond) bonds.elementAt(i);
919 if (tmpBond.at1.isSelected)
921 labelAtom(g, tmpBond, 1);
924 if (tmpBond.at2.isSelected)
927 labelAtom(g, tmpBond, 2);
934 public void labelAtom(Graphics g, Bond b, int n)
937 g.setColor(Color.red);
940 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
942 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
945 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
950 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
952 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
955 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
960 public Atom findAtom(int x, int y)
966 for (int ii = 0; ii < pdb.chains.size(); ii++)
968 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
974 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
976 for (int i = 0; i < bonds.size(); i++)
978 tmpBond = (Bond) bonds.elementAt(i);
980 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
983 if (Math.abs(truex - x) <= 2)
985 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
988 if (Math.abs(truey - y) <= 2)
997 // Still here? Maybe its the last bond
999 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
1002 if (Math.abs(truex - x) <= 2)
1004 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
1007 if (Math.abs(truey - y) <= 2)
1009 fatom = tmpBond.at2;
1017 if (fatom != null) //)&& chain.ds != null)
1019 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1026 Bond highlightBond1, highlightBond2;
1027 public void highlightRes(int ii)
1029 if (!seqColoursReady)
1034 if (highlightRes != null
1035 && highlightRes.contains( (ii - 1) + ""))
1042 for (index = 0; index < mainchain.bonds.size(); index++)
1044 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1045 if (tmpBond.at1.alignmentMapping == ii - 1)
1047 if (highlightBond1 != null)
1049 highlightBond1.at2.isSelected = false;
1052 if (highlightBond2 != null)
1054 highlightBond2.at1.isSelected = false;
1057 highlightBond1 = null;
1058 highlightBond2 = null;
1062 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1063 highlightBond1.at2.isSelected = true;
1066 if (index != mainchain.bonds.size())
1068 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1069 highlightBond2.at1.isSelected = true;
1076 redrawneeded = true;
1080 public void setAllchainsVisible(boolean b)
1082 for (int ii = 0; ii < pdb.chains.size(); ii++)
1084 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1085 chain.isVisible = b;
1087 mainchain.isVisible = true;