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, AlignSeq.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);
147 mainchain.transferRESNUMFeatures(sequence, null);
148 seqcanvas.getFeatureRenderer().featuresAdded();
150 this.prefsize = new Dimension(getWidth(), getHeight());
152 //Initialize the matrices to identity
153 for (int i = 0; i < 3; i++) {
154 for (int j = 0; j < 3; j++) {
156 idmat.addElement(i, j, 0);
157 objmat.addElement(i, j, 0);
159 idmat.addElement(i, j, 1);
160 objmat.addElement(i, j, 1);
165 addMouseMotionListener(this);
166 addMouseListener(this);
168 addMouseWheelListener(new MouseWheelListener()
170 public void mouseWheelMoved(MouseWheelEvent e)
172 if (e.getWheelRotation() > 0)
174 scale = (float) (scale * 1.1);
181 scale = (float) (scale * 0.9);
196 ToolTipManager.sharedInstance().registerComponent(this);
197 ToolTipManager.sharedInstance().setInitialDelay(0);
198 ToolTipManager.sharedInstance().setDismissDelay(10000);
205 seqColoursReady = false;
206 // Sort the bonds by z coord
207 visiblebonds = new Vector();
209 for (int ii = 0; ii < pdb.chains.size(); ii++)
211 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
213 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
215 for (int i = 0; i < tmp.size(); i++)
217 visiblebonds.addElement(tmp.elementAt(i));
223 seqColoursReady = true;
229 public void findWidth() {
230 float[] max = new float[3];
231 float[] min = new float[3];
233 max[0] = (float) -1e30;
234 max[1] = (float) -1e30;
235 max[2] = (float) -1e30;
237 min[0] = (float) 1e30;
238 min[1] = (float) 1e30;
239 min[2] = (float) 1e30;
241 for (int ii = 0; ii < pdb.chains.size(); ii++) {
242 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
243 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
245 for (int i = 0; i < bonds.size(); i++) {
246 Bond tmp = (Bond) bonds.elementAt(i);
248 if (tmp.start[0] >= max[0]) {
249 max[0] = tmp.start[0];
252 if (tmp.start[1] >= max[1]) {
253 max[1] = tmp.start[1];
256 if (tmp.start[2] >= max[2]) {
257 max[2] = tmp.start[2];
260 if (tmp.start[0] <= min[0]) {
261 min[0] = tmp.start[0];
264 if (tmp.start[1] <= min[1]) {
265 min[1] = tmp.start[1];
268 if (tmp.start[2] <= min[2]) {
269 min[2] = tmp.start[2];
272 if (tmp.end[0] >= max[0]) {
276 if (tmp.end[1] >= max[1]) {
280 if (tmp.end[2] >= max[2]) {
284 if (tmp.end[0] <= min[0]) {
288 if (tmp.end[1] <= min[1]) {
292 if (tmp.end[2] <= min[2]) {
299 System.out.println("xmax " + max[0] + " min " + min[0]);
300 System.out.println("ymax " + max[1] + " min " + min[1]);
301 System.out.println("zmax " + max[2] + " min " + min[2]);*/
303 width[0] = (float) Math.abs(max[0] - min[0]);
304 width[1] = (float) Math.abs(max[1] - min[1]);
305 width[2] = (float) Math.abs(max[2] - min[2]);
309 if (width[1] > width[0]) {
313 if (width[2] > width[1]) {
317 // System.out.println("Maxwidth = " + maxwidth);
320 public float findScale() {
325 if (getWidth() != 0) {
327 height = getHeight();
329 width = prefsize.width;
330 height = prefsize.height;
333 if (width < height) {
339 return (float) (dim / (1.5d * maxwidth));
342 public void findCentre() {
349 //Find centre coordinate
350 for (int ii = 0; ii < pdb.chains.size(); ii++) {
351 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
352 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
354 bsize += bonds.size();
356 for (int i = 0; i < bonds.size(); i++) {
357 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
358 ((Bond) bonds.elementAt(i)).end[0];
360 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
361 ((Bond) bonds.elementAt(i)).end[1];
363 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
364 ((Bond) bonds.elementAt(i)).end[2];
369 centre[0] = xtot / (2 * (float) bsize);
370 centre[1] = ytot / (2 * (float) bsize);
371 centre[2] = ztot / (2 * (float) bsize);
374 public void paintComponent(Graphics g)
376 super.paintComponent(g);
380 g.setColor(Color.black);
381 g.setFont(new Font("Verdana", Font.BOLD, 14));
382 g.drawString("Retrieving PDB data....", 20, getHeight()/2);
387 //Only create the image at the beginning -
388 //this saves much memory usage
390 || (prefsize.width != getWidth())
391 || (prefsize.height != getHeight()))
394 prefsize.width = getWidth();
395 prefsize.height = getHeight();
398 img = createImage(prefsize.width, prefsize.height);
399 ig = img.getGraphics();
400 Graphics2D ig2 = (Graphics2D) ig;
402 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
403 RenderingHints.VALUE_ANTIALIAS_ON);
411 drawAll(ig, prefsize.width, prefsize.height);
412 redrawneeded = false;
415 g.drawImage(img, 0, 0, this);
420 public void drawAll(Graphics g, int width, int height)
422 g.setColor(backgroundColour);
423 g.fillRect(0, 0, width, height);
429 public void updateSeqColours()
436 // System.out.println("update seq colours");
437 if(bysequence && pdb!=null)
439 for (int ii = 0; ii < pdb.chains.size(); ii++)
441 colourBySequence( (PDBChain) pdb.chains.elementAt(ii));
449 int findTrueIndex(int pos)
451 // returns the alignment position for a residue
452 int j = sequence.getStart(); // first position in PDB atom coordinate sequence
455 while ( (i < sequence.getLength()) && (j <= pos+1))
457 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
470 return sequence.getEnd() + 1;
478 // This method has been taken out of PDBChain to allow
479 // Applet and Application specific sequence renderers to be used
480 void colourBySequence(PDBChain chain)
482 // System.out.println("colour by seq");
483 boolean showFeatures = false;
484 if(seqcanvas.getViewport().getShowSequenceFeatures())
488 fr = new jalview.gui.FeatureRenderer(seqcanvas.getViewport());
490 fr.transferSettings( seqcanvas.getFeatureRenderer() );
494 for (int i = 0; i < chain.bonds.size(); i++)
496 tmp = (Bond) chain.bonds.elementAt(i);
501 //if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
502 // (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
504 int index = findTrueIndex(tmp.at1.alignmentMapping);
505 //sequence.findIndex(tmp.at1.alignmentMapping);
508 tmp.startCol = seqcanvas.getSequenceRenderer().
509 getResidueBoxColour( sequence, index);
512 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
514 if(tmp.startCol==null)
515 tmp.startCol = Color.white;
519 //if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
520 // (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
523 int index = findTrueIndex(tmp.at2.alignmentMapping);
524 //sequence.findIndex( tmp.at2.alignmentMapping );
527 tmp.endCol = seqcanvas.getSequenceRenderer().
528 getResidueBoxColour( sequence, index);
531 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
534 tmp.endCol = Color.white;
542 public void drawScene(Graphics g)
549 zsort.Zsort(visiblebonds);
553 for (int i = 0; i < visiblebonds.size(); i++)
555 tmpBond = (Bond) visiblebonds.elementAt(i);
557 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
559 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
562 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
564 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
567 xmid = (xend + xstart) / 2;
568 ymid = (yend + ystart) / 2;
569 if (depthcue && !bymolecule)
571 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
574 g.setColor(tmpBond.startCol.darker().darker());
575 drawLine(g, xstart, ystart, xmid, ymid);
576 g.setColor(tmpBond.endCol.darker().darker());
577 drawLine(g, xmid, ymid, xend, yend);
580 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
582 g.setColor(tmpBond.startCol.darker());
583 drawLine(g, xstart, ystart, xmid, ymid);
585 g.setColor(tmpBond.endCol.darker());
586 drawLine(g, xmid, ymid, xend, yend);
590 g.setColor(tmpBond.startCol);
591 drawLine(g, xstart, ystart, xmid, ymid);
593 g.setColor(tmpBond.endCol);
594 drawLine(g, xmid, ymid, xend, yend);
597 else if (depthcue && bymolecule)
599 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
601 g.setColor(Color.green.darker().darker());
602 drawLine(g, xstart, ystart, xend, yend);
604 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
606 g.setColor(Color.green.darker());
607 drawLine(g, xstart, ystart, xend, yend);
611 g.setColor(Color.green);
612 drawLine(g, xstart, ystart, xend, yend);
615 else if (!depthcue && !bymolecule)
617 g.setColor(tmpBond.startCol);
618 drawLine(g, xstart, ystart, xmid, ymid);
619 g.setColor(tmpBond.endCol);
620 drawLine(g, xmid, ymid, xend, yend);
624 drawLine(g, xstart, ystart, xend, yend);
627 if (highlightBond1 != null && highlightBond1 == tmpBond)
629 g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter());
630 drawLine(g, xmid, ymid, xend, yend);
633 if (highlightBond2 != null && highlightBond2 == tmpBond)
635 g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter());
636 drawLine(g, xstart, ystart, xmid, ymid);
644 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
646 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
647 g.drawLine(x1, y1, x2, y2);
648 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
649 g.drawLine(x1, y1 - 1, x2, y2 - 1);
651 g.setColor(g.getColor().brighter());
652 g.drawLine(x1, y1, x2, y2);
653 g.drawLine(x1 + 1, y1, x2 + 1, y2);
654 g.drawLine(x1 - 1, y1, x2 - 1, y2);
657 g.drawLine(x1, y1, x2, y2);
661 public Dimension minimumsize() {
665 public Dimension preferredsize() {
669 public void keyPressed(KeyEvent evt)
671 if (evt.getKeyCode() == KeyEvent.VK_UP)
673 scale = (float) (scale * 1.1);
677 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
679 scale = (float) (scale * 0.9);
685 public void mousePressed(MouseEvent e)
688 Atom fatom = findAtom(e.getX(), e.getY());
691 fatom.isSelected = !fatom.isSelected;
695 if (foundchain != -1)
697 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
698 if (chain == mainchain)
700 if (fatom.alignmentMapping != -1)
702 if (highlightRes == null)
703 highlightRes = new Vector();
705 if (highlightRes.contains(fatom.alignmentMapping+""))
706 highlightRes.remove(fatom.alignmentMapping + "");
708 highlightRes.add(fatom.alignmentMapping + "");
721 public void mouseMoved(MouseEvent e)
724 if(highlightBond1!=null)
726 highlightBond1.at2.isSelected = false;
727 highlightBond2.at1.isSelected = false;
728 highlightBond1 = null;
729 highlightBond2 = null;
732 Atom fatom = findAtom(e.getX(), e.getY());
734 PDBChain chain = null;
737 chain = (PDBChain) pdb.chains.elementAt(foundchain);
738 if(chain == mainchain)
740 highlightSeqcanvas( fatom.alignmentMapping );
746 this.setToolTipText(chain.id+":"+ fatom.resNumber+" "+ fatom.resName);
749 highlightSeqcanvas( -1);
750 this.setToolTipText(null);
755 void highlightSeqcanvas(int pos)
757 SearchResults searchResults = new SearchResults();
758 if(highlightRes!=null)
760 for (int i = 0; i < highlightRes.size(); i++)
762 int a = Integer.parseInt(highlightRes.elementAt(
765 searchResults.addResult(sequence, a, a);
771 searchResults.addResult(sequence, pos+1, pos+1);
774 seqcanvas.highlightSearchResults(searchResults);
778 public void mouseClicked(MouseEvent e) { }
780 public void mouseEntered(MouseEvent e) { }
782 public void mouseExited(MouseEvent e) { }
784 public void mouseDragged(MouseEvent evt)
792 MCMatrix objmat = new MCMatrix(3, 3);
793 objmat.setIdentity();
795 if ((evt.getModifiers() & Event.META_MASK) != 0) {
796 objmat.rotatez((float) ((mx - omx)));
798 objmat.rotatex((float) ((my - omy)));
799 objmat.rotatey((float) ((omx - mx)));
803 for (int ii = 0; ii < pdb.chains.size(); ii++) {
804 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
806 for (int i = 0; i < bonds.size(); i++) {
807 Bond tmpBond = (Bond) bonds.elementAt(i);
809 //Translate the bond so the centre is 0,0,0
810 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
812 //Now apply the rotation matrix
813 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
814 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
816 //Now translate back again
817 tmpBond.translate(centre[0], centre[1], centre[2]);
833 public void mouseReleased(MouseEvent evt)
839 void drawLabels(Graphics g) {
841 for (int ii = 0; ii < pdb.chains.size(); ii++)
843 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
847 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
849 for (int i = 0; i < bonds.size(); i++)
851 Bond tmpBond = (Bond) bonds.elementAt(i);
853 if (tmpBond.at1.isSelected)
855 labelAtom(g, tmpBond, 1);
858 if (tmpBond.at2.isSelected)
861 labelAtom(g, tmpBond, 2);
868 public void labelAtom(Graphics g, Bond b, int n) {
870 g.setColor(Color.red);
873 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
875 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
878 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
882 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
884 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
887 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
892 public Atom findAtom(int x, int y) {
897 for (int ii = 0; ii < pdb.chains.size(); ii++)
899 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
905 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
907 for (int i = 0; i < bonds.size(); i++)
909 tmpBond = (Bond) bonds.elementAt(i);
911 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
914 if (Math.abs(truex - x) <= 2)
916 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
919 if (Math.abs(truey - y) <= 2)
928 // Still here? Maybe its the last bond
930 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
933 if (Math.abs(truex - x) <= 2)
935 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
938 if (Math.abs(truey - y) <= 2)
948 if (fatom != null) //)&& chain.ds != null)
950 chain = (PDBChain) pdb.chains.elementAt(foundchain);
957 Bond highlightBond1, highlightBond2;
958 public void highlightRes(int ii)
960 if( !seqColoursReady )
963 if (highlightRes != null
964 && highlightRes.contains((ii-1) + ""))
971 for(index=0; index<mainchain.bonds.size(); index++)
973 tmpBond = (Bond) mainchain.bonds.elementAt(index);
974 if (tmpBond.at1.alignmentMapping == ii - 1)
976 if (highlightBond1 != null)
977 highlightBond1.at2.isSelected = false;
979 if (highlightBond2 != null)
980 highlightBond2.at1.isSelected = false;
982 highlightBond1 = null;
983 highlightBond2 = null;
987 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
988 highlightBond1.at2.isSelected = true;
991 if (index != mainchain.bonds.size())
993 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
994 highlightBond2.at1.isSelected = true;
1001 redrawneeded = true;
1005 public void setAllchainsVisible(boolean b)
1007 for (int ii = 0; ii < pdb.chains.size(); ii++)
1009 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1010 chain.isVisible = b;
1012 mainchain.isVisible = true;