2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
21 import jalview.analysis.AlignSeq;
23 import jalview.datamodel.*;
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
27 import java.awt.event.*;
36 public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListener
38 MCMatrix idmat = new MCMatrix(3, 3);
39 MCMatrix objmat = new MCMatrix(3, 3);
40 boolean redrawneeded = true;
50 float[] centre = new float[3];
51 float[] width = new float[3];
56 boolean bysequence = true;
57 boolean depthcue = true;
59 boolean bymolecule = false;
60 boolean zbuffer = true;
68 Font font = new Font("Helvetica", Font.PLAIN, 10);
69 jalview.gui.SeqCanvas seqcanvas;
70 public Sequence sequence;
71 final StringBuffer mappingDetails = new StringBuffer();
74 boolean pdbAction = false;
75 boolean seqColoursReady = false;
76 jalview.gui.FeatureRenderer fr;
77 Color backgroundColour = Color.black;
79 public PDBCanvas(jalview.gui.SeqCanvas seqcanvas, Sequence seq)
81 this.seqcanvas = seqcanvas;
83 seqcanvas.setPDBCanvas(this);
86 public void setPDBFile(PDBfile pdb)
94 AlignSeq maxAlignseq = null;
96 for (int i = 0; i < pdb.chains.size(); i++)
99 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequenceAsString());
100 mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");
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, "pep");
107 as.calcScoreMatrix();
109 PrintStream ps = new PrintStream(System.out)
111 public void print(String x) {
112 mappingDetails.append(x);
114 public void println()
116 mappingDetails.append("\n");
120 as.printAlignment(ps);
124 if (as.maxscore > max)
128 pdbstart = as.seq2start;
130 seqstart = as.seq1start + sequence.getStart()-1;
131 seqend = as.seq1end + sequence.getEnd()-1;
135 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
136 mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);
139 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
141 mainchain.pdbstart = pdbstart;
142 mainchain.pdbend = pdbend;
143 mainchain.seqstart = seqstart;
144 mainchain.seqend = seqend;
145 mainchain.isVisible = true;
146 mainchain.makeExactMapping(maxAlignseq, sequence);
149 this.prefsize = new Dimension(getWidth(), getHeight());
151 //Initialize the matrices to identity
152 for (int i = 0; i < 3; i++) {
153 for (int j = 0; j < 3; j++) {
155 idmat.addElement(i, j, 0);
156 objmat.addElement(i, j, 0);
158 idmat.addElement(i, j, 1);
159 objmat.addElement(i, j, 1);
164 addMouseMotionListener(this);
165 addMouseListener(this);
167 addMouseWheelListener(new MouseWheelListener()
169 public void mouseWheelMoved(MouseWheelEvent e)
171 if (e.getWheelRotation() > 0)
173 scale = (float) (scale * 1.1);
180 scale = (float) (scale * 0.9);
195 ToolTipManager.sharedInstance().registerComponent(this);
196 ToolTipManager.sharedInstance().setInitialDelay(0);
197 ToolTipManager.sharedInstance().setDismissDelay(10000);
204 seqColoursReady = false;
205 // Sort the bonds by z coord
206 visiblebonds = new Vector();
208 for (int ii = 0; ii < pdb.chains.size(); ii++)
210 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
212 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
214 for (int i = 0; i < tmp.size(); i++)
216 visiblebonds.addElement(tmp.elementAt(i));
222 seqColoursReady = true;
228 public void findWidth() {
229 float[] max = new float[3];
230 float[] min = new float[3];
232 max[0] = (float) -1e30;
233 max[1] = (float) -1e30;
234 max[2] = (float) -1e30;
236 min[0] = (float) 1e30;
237 min[1] = (float) 1e30;
238 min[2] = (float) 1e30;
240 for (int ii = 0; ii < pdb.chains.size(); ii++) {
241 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
242 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
244 for (int i = 0; i < bonds.size(); i++) {
245 Bond tmp = (Bond) bonds.elementAt(i);
247 if (tmp.start[0] >= max[0]) {
248 max[0] = tmp.start[0];
251 if (tmp.start[1] >= max[1]) {
252 max[1] = tmp.start[1];
255 if (tmp.start[2] >= max[2]) {
256 max[2] = tmp.start[2];
259 if (tmp.start[0] <= min[0]) {
260 min[0] = tmp.start[0];
263 if (tmp.start[1] <= min[1]) {
264 min[1] = tmp.start[1];
267 if (tmp.start[2] <= min[2]) {
268 min[2] = tmp.start[2];
271 if (tmp.end[0] >= max[0]) {
275 if (tmp.end[1] >= max[1]) {
279 if (tmp.end[2] >= max[2]) {
283 if (tmp.end[0] <= min[0]) {
287 if (tmp.end[1] <= min[1]) {
291 if (tmp.end[2] <= min[2]) {
298 System.out.println("xmax " + max[0] + " min " + min[0]);
299 System.out.println("ymax " + max[1] + " min " + min[1]);
300 System.out.println("zmax " + max[2] + " min " + min[2]);*/
302 width[0] = (float) Math.abs(max[0] - min[0]);
303 width[1] = (float) Math.abs(max[1] - min[1]);
304 width[2] = (float) Math.abs(max[2] - min[2]);
308 if (width[1] > width[0]) {
312 if (width[2] > width[1]) {
316 // System.out.println("Maxwidth = " + maxwidth);
319 public float findScale() {
324 if (getWidth() != 0) {
326 height = getHeight();
328 width = prefsize.width;
329 height = prefsize.height;
332 if (width < height) {
338 return (float) (dim / (1.5d * maxwidth));
341 public void findCentre() {
348 //Find centre coordinate
349 for (int ii = 0; ii < pdb.chains.size(); ii++) {
350 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
351 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
353 bsize += bonds.size();
355 for (int i = 0; i < bonds.size(); i++) {
356 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
357 ((Bond) bonds.elementAt(i)).end[0];
359 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
360 ((Bond) bonds.elementAt(i)).end[1];
362 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
363 ((Bond) bonds.elementAt(i)).end[2];
368 centre[0] = xtot / (2 * (float) bsize);
369 centre[1] = ytot / (2 * (float) bsize);
370 centre[2] = ztot / (2 * (float) bsize);
373 public void paintComponent(Graphics g)
375 super.paintComponent(g);
379 g.setColor(Color.black);
380 g.setFont(new Font("Verdana", Font.BOLD, 14));
381 g.drawString("Retrieving PDB data....", 20, getHeight()/2);
386 //Only create the image at the beginning -
387 //this saves much memory usage
389 || (prefsize.width != getWidth())
390 || (prefsize.height != getHeight()))
393 prefsize.width = getWidth();
394 prefsize.height = getHeight();
397 img = createImage(prefsize.width, prefsize.height);
398 ig = img.getGraphics();
399 Graphics2D ig2 = (Graphics2D) ig;
401 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
402 RenderingHints.VALUE_ANTIALIAS_ON);
410 drawAll(ig, prefsize.width, prefsize.height);
411 redrawneeded = false;
414 g.drawImage(img, 0, 0, this);
419 public void drawAll(Graphics g, int width, int height)
421 g.setColor(backgroundColour);
422 g.fillRect(0, 0, width, height);
428 public void updateSeqColours()
435 // System.out.println("update seq colours");
436 if(bysequence && pdb!=null)
438 for (int ii = 0; ii < pdb.chains.size(); ii++)
440 colourBySequence( (PDBChain) pdb.chains.elementAt(ii));
448 int findTrueIndex(int pos)
450 // returns the alignment position for a residue
451 int j = sequence.getStart();
454 while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) && (j <= pos+1))
456 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
467 if ( (j == sequence.getEnd()) && (j < pos))
469 return sequence.getEnd() + 1;
477 // This method has been taken out of PDBChain to allow
478 // Applet and Application specific sequence renderers to be used
479 void colourBySequence(PDBChain chain)
481 // System.out.println("colour by seq");
482 boolean showFeatures = false;
483 if(seqcanvas.getViewport().getShowSequenceFeatures())
487 fr = new jalview.gui.FeatureRenderer(seqcanvas.getViewport());
489 fr.transferSettings( seqcanvas.getFeatureRenderer() );
493 for (int i = 0; i < chain.bonds.size(); i++)
495 tmp = (Bond) chain.bonds.elementAt(i);
500 if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
501 (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
503 int index = findTrueIndex(tmp.at1.alignmentMapping);
504 //sequence.findIndex(tmp.at1.alignmentMapping);
507 tmp.startCol = seqcanvas.getSequenceRenderer().
508 getResidueBoxColour( sequence, index);
511 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
513 if(tmp.startCol==null)
514 tmp.startCol = Color.white;
518 if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
519 (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
522 int index = findTrueIndex(tmp.at2.alignmentMapping);
523 //sequence.findIndex( tmp.at2.alignmentMapping );
526 tmp.endCol = seqcanvas.getSequenceRenderer().
527 getResidueBoxColour( sequence, index);
530 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
533 tmp.endCol = Color.white;
541 public void drawScene(Graphics g)
548 zsort.Zsort(visiblebonds);
552 for (int i = 0; i < visiblebonds.size(); i++)
554 tmpBond = (Bond) visiblebonds.elementAt(i);
556 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
558 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
561 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
563 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
566 xmid = (xend + xstart) / 2;
567 ymid = (yend + ystart) / 2;
568 if (depthcue && !bymolecule)
570 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
573 g.setColor(tmpBond.startCol.darker().darker());
574 drawLine(g, xstart, ystart, xmid, ymid);
575 g.setColor(tmpBond.endCol.darker().darker());
576 drawLine(g, xmid, ymid, xend, yend);
579 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
581 g.setColor(tmpBond.startCol.darker());
582 drawLine(g, xstart, ystart, xmid, ymid);
584 g.setColor(tmpBond.endCol.darker());
585 drawLine(g, xmid, ymid, xend, yend);
589 g.setColor(tmpBond.startCol);
590 drawLine(g, xstart, ystart, xmid, ymid);
592 g.setColor(tmpBond.endCol);
593 drawLine(g, xmid, ymid, xend, yend);
596 else if (depthcue && bymolecule)
598 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
600 g.setColor(Color.green.darker().darker());
601 drawLine(g, xstart, ystart, xend, yend);
603 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
605 g.setColor(Color.green.darker());
606 drawLine(g, xstart, ystart, xend, yend);
610 g.setColor(Color.green);
611 drawLine(g, xstart, ystart, xend, yend);
614 else if (!depthcue && !bymolecule)
616 g.setColor(tmpBond.startCol);
617 drawLine(g, xstart, ystart, xmid, ymid);
618 g.setColor(tmpBond.endCol);
619 drawLine(g, xmid, ymid, xend, yend);
623 drawLine(g, xstart, ystart, xend, yend);
626 if (highlightBond1 != null && highlightBond1 == tmpBond)
628 g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter());
629 drawLine(g, xmid, ymid, xend, yend);
632 if (highlightBond2 != null && highlightBond2 == tmpBond)
634 g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter());
635 drawLine(g, xstart, ystart, xmid, ymid);
643 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
645 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
646 g.drawLine(x1, y1, x2, y2);
647 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
648 g.drawLine(x1, y1 - 1, x2, y2 - 1);
650 g.setColor(g.getColor().brighter());
651 g.drawLine(x1, y1, x2, y2);
652 g.drawLine(x1 + 1, y1, x2 + 1, y2);
653 g.drawLine(x1 - 1, y1, x2 - 1, y2);
656 g.drawLine(x1, y1, x2, y2);
660 public Dimension minimumsize() {
664 public Dimension preferredsize() {
668 public void keyPressed(KeyEvent evt)
670 if (evt.getKeyCode() == KeyEvent.VK_UP)
672 scale = (float) (scale * 1.1);
676 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
678 scale = (float) (scale * 0.9);
684 public void mousePressed(MouseEvent e)
687 Atom fatom = findAtom(e.getX(), e.getY());
690 fatom.isSelected = !fatom.isSelected;
694 if (foundchain != -1)
696 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
697 if (chain == mainchain)
699 if (fatom.alignmentMapping != -1)
701 if (highlightRes == null)
702 highlightRes = new Vector();
704 if (highlightRes.contains(fatom.alignmentMapping+""))
705 highlightRes.remove(fatom.alignmentMapping + "");
707 highlightRes.add(fatom.alignmentMapping + "");
720 public void mouseMoved(MouseEvent e)
723 if(highlightBond1!=null)
725 highlightBond1.at2.isSelected = false;
726 highlightBond2.at1.isSelected = false;
727 highlightBond1 = null;
728 highlightBond2 = null;
731 Atom fatom = findAtom(e.getX(), e.getY());
733 PDBChain chain = null;
736 chain = (PDBChain) pdb.chains.elementAt(foundchain);
737 if(chain == mainchain)
739 highlightSeqcanvas( fatom.alignmentMapping );
745 this.setToolTipText(chain.id+":"+ fatom.resNumber+" "+ fatom.resName);
748 highlightSeqcanvas( -1);
749 this.setToolTipText(null);
754 void highlightSeqcanvas(int pos)
756 SearchResults searchResults = new SearchResults();
757 if(highlightRes!=null)
759 for (int i = 0; i < highlightRes.size(); i++)
761 int a = Integer.parseInt(highlightRes.elementAt(
764 searchResults.addResult(sequence, a, a);
770 searchResults.addResult(sequence, pos+1, pos+1);
773 seqcanvas.highlightSearchResults(searchResults);
777 public void mouseClicked(MouseEvent e) { }
779 public void mouseEntered(MouseEvent e) { }
781 public void mouseExited(MouseEvent e) { }
783 public void mouseDragged(MouseEvent evt)
791 MCMatrix objmat = new MCMatrix(3, 3);
792 objmat.setIdentity();
794 if ((evt.getModifiers() & Event.META_MASK) != 0) {
795 objmat.rotatez((float) ((mx - omx)));
797 objmat.rotatex((float) ((my - omy)));
798 objmat.rotatey((float) ((omx - mx)));
802 for (int ii = 0; ii < pdb.chains.size(); ii++) {
803 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
805 for (int i = 0; i < bonds.size(); i++) {
806 Bond tmpBond = (Bond) bonds.elementAt(i);
808 //Translate the bond so the centre is 0,0,0
809 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
811 //Now apply the rotation matrix
812 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
813 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
815 //Now translate back again
816 tmpBond.translate(centre[0], centre[1], centre[2]);
832 public void mouseReleased(MouseEvent evt)
838 void drawLabels(Graphics g) {
840 for (int ii = 0; ii < pdb.chains.size(); ii++)
842 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
846 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
848 for (int i = 0; i < bonds.size(); i++)
850 Bond tmpBond = (Bond) bonds.elementAt(i);
852 if (tmpBond.at1.isSelected)
854 labelAtom(g, tmpBond, 1);
857 if (tmpBond.at2.isSelected)
860 labelAtom(g, tmpBond, 2);
867 public void labelAtom(Graphics g, Bond b, int n) {
869 g.setColor(Color.red);
872 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
874 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
877 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
881 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
883 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
886 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
891 public Atom findAtom(int x, int y) {
896 for (int ii = 0; ii < pdb.chains.size(); ii++)
898 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
904 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
906 for (int i = 0; i < bonds.size(); i++)
908 tmpBond = (Bond) bonds.elementAt(i);
910 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
913 if (Math.abs(truex - x) <= 2)
915 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
918 if (Math.abs(truey - y) <= 2)
927 // Still here? Maybe its the last bond
929 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
932 if (Math.abs(truex - x) <= 2)
934 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
937 if (Math.abs(truey - y) <= 2)
947 if (fatom != null) //)&& chain.ds != null)
949 chain = (PDBChain) pdb.chains.elementAt(foundchain);
956 Bond highlightBond1, highlightBond2;
957 public void highlightRes(int ii)
959 if( !seqColoursReady )
962 if (highlightRes != null
963 && highlightRes.contains((ii-1) + ""))
970 for(index=0; index<mainchain.bonds.size(); index++)
972 tmpBond = (Bond) mainchain.bonds.elementAt(index);
973 if (tmpBond.at1.alignmentMapping == ii - 1)
975 if (highlightBond1 != null)
976 highlightBond1.at2.isSelected = false;
978 if (highlightBond2 != null)
979 highlightBond2.at1.isSelected = false;
981 highlightBond1 = null;
982 highlightBond2 = null;
986 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
987 highlightBond1.at2.isSelected = true;
990 if (index != mainchain.bonds.size())
992 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
993 highlightBond2.at1.isSelected = true;
1000 redrawneeded = true;
1004 public void setAllchainsVisible(boolean b)
1006 for (int ii = 0; ii < pdb.chains.size(); ii++)
1008 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1009 chain.isVisible = b;
1011 mainchain.isVisible = true;