2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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 import jalview.structure.*;
34 public class PDBCanvas extends JPanel implements MouseListener,
35 MouseMotionListener, StructureListener
37 boolean redrawneeded = true;
59 float[] centre = new float[3];
61 float[] width = new float[3];
71 boolean bysequence = true;
73 boolean depthcue = true;
77 boolean bymolecule = false;
79 boolean zbuffer = true;
95 Font font = new Font("Helvetica", Font.PLAIN, 10);
97 jalview.gui.SeqCanvas seqcanvas;
99 public SequenceI[] sequence;
101 final StringBuffer mappingDetails = new StringBuffer();
107 boolean pdbAction = false;
109 boolean seqColoursReady = false;
111 jalview.gui.FeatureRenderer fr;
113 Color backgroundColour = Color.black;
117 StructureSelectionManager ssm;
121 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
122 AlignmentPanel ap, String protocol)
125 this.pdbentry = pdbentry;
128 ssm = ap.av.getStructureSelectionManager();
132 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
134 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
135 pdbentry.setFile("INLINE" + pdb.id);
137 } catch (Exception ex)
139 ex.printStackTrace();
145 errorMessage = "Error loading file: " + pdbentry.getId();
148 pdbentry.setId(pdb.id);
150 ssm.addStructureViewerListener(this);
161 // JUST DEAL WITH ONE SEQUENCE FOR NOW
162 SequenceI sequence = seq[0];
164 for (int i = 0; i < pdb.chains.size(); i++)
167 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
168 + ((PDBChain) pdb.chains.elementAt(i)).sequence
169 .getSequenceAsString());
170 mappingDetails.append("\nNo of residues = "
171 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
174 // Now lets compare the sequences to get
175 // the start and end points.
176 // Align the sequence to the pdb
177 AlignSeq as = new AlignSeq(sequence,
178 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
179 as.calcScoreMatrix();
181 PrintStream ps = new PrintStream(System.out)
183 public void print(String x)
185 mappingDetails.append(x);
188 public void println()
190 mappingDetails.append("\n");
194 as.printAlignment(ps);
196 if (as.maxscore > max)
201 pdbstart = as.seq2start;
203 seqstart = as.seq1start + sequence.getStart() - 1;
204 seqend = as.seq1end + sequence.getEnd() - 1;
207 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
208 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
211 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
213 mainchain.pdbstart = pdbstart;
214 mainchain.pdbend = pdbend;
215 mainchain.seqstart = seqstart;
216 mainchain.seqend = seqend;
217 mainchain.isVisible = true;
220 this.prefsize = new Dimension(getSize().width, getSize().height);
222 addMouseMotionListener(this);
223 addMouseListener(this);
225 addKeyListener(new KeyAdapter()
227 public void keyPressed(KeyEvent evt)
240 ToolTipManager.sharedInstance().registerComponent(this);
241 ToolTipManager.sharedInstance().setInitialDelay(0);
242 ToolTipManager.sharedInstance().setDismissDelay(10000);
249 seqColoursReady = false;
250 // Sort the bonds by z coord
251 visiblebonds = new Vector();
253 for (int ii = 0; ii < pdb.chains.size(); ii++)
255 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
257 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
259 for (int i = 0; i < tmp.size(); i++)
261 visiblebonds.addElement(tmp.elementAt(i));
267 seqColoursReady = true;
272 public void findWidth()
274 float[] max = new float[3];
275 float[] min = new float[3];
277 max[0] = (float) -1e30;
278 max[1] = (float) -1e30;
279 max[2] = (float) -1e30;
281 min[0] = (float) 1e30;
282 min[1] = (float) 1e30;
283 min[2] = (float) 1e30;
285 for (int ii = 0; ii < pdb.chains.size(); ii++)
287 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
289 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
291 for (int i = 0; i < bonds.size(); i++)
293 Bond tmp = (Bond) bonds.elementAt(i);
295 if (tmp.start[0] >= max[0])
297 max[0] = tmp.start[0];
300 if (tmp.start[1] >= max[1])
302 max[1] = tmp.start[1];
305 if (tmp.start[2] >= max[2])
307 max[2] = tmp.start[2];
310 if (tmp.start[0] <= min[0])
312 min[0] = tmp.start[0];
315 if (tmp.start[1] <= min[1])
317 min[1] = tmp.start[1];
320 if (tmp.start[2] <= min[2])
322 min[2] = tmp.start[2];
325 if (tmp.end[0] >= max[0])
330 if (tmp.end[1] >= max[1])
335 if (tmp.end[2] >= max[2])
340 if (tmp.end[0] <= min[0])
345 if (tmp.end[1] <= min[1])
350 if (tmp.end[2] <= min[2])
358 * System.out.println("xmax " + max[0] + " min " + min[0]);
359 * System.out.println("ymax " + max[1] + " min " + min[1]);
360 * System.out.println("zmax " + max[2] + " min " + min[2]);
363 width[0] = (float) Math.abs(max[0] - min[0]);
364 width[1] = (float) Math.abs(max[1] - min[1]);
365 width[2] = (float) Math.abs(max[2] - min[2]);
369 if (width[1] > width[0])
374 if (width[2] > width[1])
379 // System.out.println("Maxwidth = " + maxwidth);
382 public float findScale()
391 height = getHeight();
395 width = prefsize.width;
396 height = prefsize.height;
408 return (float) (dim / (1.5d * maxwidth));
411 public void findCentre()
419 // Find centre coordinate
420 for (int ii = 0; ii < pdb.chains.size(); ii++)
422 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
424 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
426 bsize += bonds.size();
428 for (int i = 0; i < bonds.size(); i++)
430 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
431 + ((Bond) bonds.elementAt(i)).end[0];
433 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
434 + ((Bond) bonds.elementAt(i)).end[1];
436 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
437 + ((Bond) bonds.elementAt(i)).end[2];
442 centre[0] = xtot / (2 * (float) bsize);
443 centre[1] = ytot / (2 * (float) bsize);
444 centre[2] = ztot / (2 * (float) bsize);
447 public void paintComponent(Graphics g)
449 super.paintComponent(g);
451 if (!seqColoursReady || errorMessage != null)
453 g.setColor(Color.black);
454 g.setFont(new Font("Verdana", Font.BOLD, 14));
455 g.drawString(errorMessage == null ? "Retrieving PDB data...."
456 : errorMessage, 20, getHeight() / 2);
460 // Only create the image at the beginning -
461 // this saves much memory usage
462 if ((img == null) || (prefsize.width != getWidth())
463 || (prefsize.height != getHeight()))
466 prefsize.width = getWidth();
467 prefsize.height = getHeight();
470 img = createImage(prefsize.width, prefsize.height);
471 ig = img.getGraphics();
472 Graphics2D ig2 = (Graphics2D) ig;
474 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
475 RenderingHints.VALUE_ANTIALIAS_ON);
482 drawAll(ig, prefsize.width, prefsize.height);
483 redrawneeded = false;
486 g.drawImage(img, 0, 0, this);
491 public void drawAll(Graphics g, int width, int height)
493 g.setColor(backgroundColour);
494 g.fillRect(0, 0, width, height);
499 public void updateSeqColours()
512 // This method has been taken out of PDBChain to allow
513 // Applet and Application specific sequence renderers to be used
514 void colourBySequence()
516 SequenceRenderer sr = new SequenceRenderer(ap.av);
518 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
520 boolean showFeatures = false;
521 if (ap.av.getShowSequenceFeatures())
525 fr = new FeatureRenderer(ap);
528 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
534 if (bysequence && pdb != null)
536 for (int ii = 0; ii < pdb.chains.size(); ii++)
538 chain = (PDBChain) pdb.chains.elementAt(ii);
540 for (int i = 0; i < chain.bonds.size(); i++)
542 Bond tmp = (Bond) chain.bonds.elementAt(i);
543 tmp.startCol = Color.lightGray;
544 tmp.endCol = Color.lightGray;
545 if (chain != mainchain)
550 for (int s = 0; s < sequence.length; s++)
552 for (int m = 0; m < mapping.length; m++)
554 if (mapping[m].getSequence() == sequence[s])
556 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
559 pos = sequence[s].findIndex(pos);
560 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
563 tmp.startCol = fr.findFeatureColour(tmp.startCol,
567 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
570 pos = sequence[s].findIndex(pos);
571 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
574 tmp.endCol = fr.findFeatureColour(tmp.endCol,
589 public void drawScene(Graphics g)
598 zsort.Zsort(visiblebonds);
602 for (int i = 0; i < visiblebonds.size(); i++)
604 tmpBond = (Bond) visiblebonds.elementAt(i);
606 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
607 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
609 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
610 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
612 xmid = (xend + xstart) / 2;
613 ymid = (yend + ystart) / 2;
614 if (depthcue && !bymolecule)
616 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
619 g.setColor(tmpBond.startCol.darker().darker());
620 drawLine(g, xstart, ystart, xmid, ymid);
621 g.setColor(tmpBond.endCol.darker().darker());
622 drawLine(g, xmid, ymid, xend, yend);
625 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
627 g.setColor(tmpBond.startCol.darker());
628 drawLine(g, xstart, ystart, xmid, ymid);
630 g.setColor(tmpBond.endCol.darker());
631 drawLine(g, xmid, ymid, xend, yend);
635 g.setColor(tmpBond.startCol);
636 drawLine(g, xstart, ystart, xmid, ymid);
638 g.setColor(tmpBond.endCol);
639 drawLine(g, xmid, ymid, xend, yend);
642 else if (depthcue && bymolecule)
644 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
646 g.setColor(Color.green.darker().darker());
647 drawLine(g, xstart, ystart, xend, yend);
649 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
651 g.setColor(Color.green.darker());
652 drawLine(g, xstart, ystart, xend, yend);
656 g.setColor(Color.green);
657 drawLine(g, xstart, ystart, xend, yend);
660 else if (!depthcue && !bymolecule)
662 g.setColor(tmpBond.startCol);
663 drawLine(g, xstart, ystart, xmid, ymid);
664 g.setColor(tmpBond.endCol);
665 drawLine(g, xmid, ymid, xend, yend);
669 drawLine(g, xstart, ystart, xend, yend);
672 if (highlightBond1 != null && highlightBond1 == tmpBond)
674 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
676 drawLine(g, xmid, ymid, xend, yend);
679 if (highlightBond2 != null && highlightBond2 == tmpBond)
681 g.setColor(tmpBond.startCol.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 mouseOverStructure(fatom.resNumber, chain.id);
807 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
812 mouseOverStructure(-1, chain != null ? chain.id : null);
813 this.setToolTipText(null);
817 public void mouseClicked(MouseEvent e)
821 public void mouseEntered(MouseEvent e)
825 public void mouseExited(MouseEvent e)
829 public void mouseDragged(MouseEvent evt)
836 MCMatrix objmat = new MCMatrix(3, 3);
837 objmat.setIdentity();
839 if ((evt.getModifiers() & Event.META_MASK) != 0)
841 objmat.rotatez((float) ((mx - omx)));
845 objmat.rotatex((float) ((my - omy)));
846 objmat.rotatey((float) ((omx - mx)));
850 for (int ii = 0; ii < pdb.chains.size(); ii++)
852 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
854 for (int i = 0; i < bonds.size(); i++)
856 Bond tmpBond = (Bond) bonds.elementAt(i);
858 // Translate the bond so the centre is 0,0,0
859 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
861 // Now apply the rotation matrix
862 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
863 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
865 // Now translate back again
866 tmpBond.translate(centre[0], centre[1], centre[2]);
882 public void mouseReleased(MouseEvent evt)
888 void drawLabels(Graphics g)
891 for (int ii = 0; ii < pdb.chains.size(); ii++)
893 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
897 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
899 for (int i = 0; i < bonds.size(); i++)
901 Bond tmpBond = (Bond) bonds.elementAt(i);
903 if (tmpBond.at1.isSelected)
905 labelAtom(g, tmpBond, 1);
908 if (tmpBond.at2.isSelected)
911 labelAtom(g, tmpBond, 2);
918 public void labelAtom(Graphics g, Bond b, int n)
921 g.setColor(Color.red);
924 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
925 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
927 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
932 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
933 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
935 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
941 public Atom findAtom(int x, int y)
947 for (int ii = 0; ii < pdb.chains.size(); ii++)
949 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
955 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
957 for (int i = 0; i < bonds.size(); i++)
959 tmpBond = (Bond) bonds.elementAt(i);
961 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
963 if (Math.abs(truex - x) <= 2)
965 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
967 if (Math.abs(truey - y) <= 2)
976 // Still here? Maybe its the last bond
978 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
980 if (Math.abs(truex - x) <= 2)
982 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
984 if (Math.abs(truey - y) <= 2)
994 if (fatom != null) // )&& chain.ds != null)
996 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1003 Bond highlightBond1, highlightBond2;
1005 public void highlightRes(int ii)
1007 if (!seqColoursReady)
1012 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1019 for (index = 0; index < mainchain.bonds.size(); index++)
1021 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1022 if (tmpBond.at1.alignmentMapping == ii - 1)
1024 if (highlightBond1 != null)
1026 highlightBond1.at2.isSelected = false;
1029 if (highlightBond2 != null)
1031 highlightBond2.at1.isSelected = false;
1034 highlightBond1 = null;
1035 highlightBond2 = null;
1039 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1040 highlightBond1.at2.isSelected = true;
1043 if (index != mainchain.bonds.size())
1045 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1046 highlightBond2.at1.isSelected = true;
1053 redrawneeded = true;
1057 public void setAllchainsVisible(boolean b)
1059 for (int ii = 0; ii < pdb.chains.size(); ii++)
1061 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1062 chain.isVisible = b;
1064 mainchain.isVisible = true;
1069 // ////////////////////////////////
1070 // /StructureListener
1071 public String[] getPdbFile()
1074 { pdbentry.getFile() };
1079 public void mouseOverStructure(int pdbResNum, String chain)
1081 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1082 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1084 lastMessage = pdbResNum + chain;
1087 StringBuffer resetLastRes = new StringBuffer();
1089 StringBuffer eval = new StringBuffer();
1091 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1094 if (!seqColoursReady)
1099 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1106 for (index = 0; index < mainchain.bonds.size(); index++)
1108 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1109 if (tmpBond.at1.atomIndex == atomIndex)
1111 if (highlightBond1 != null)
1113 highlightBond1.at2.isSelected = false;
1116 if (highlightBond2 != null)
1118 highlightBond2.at1.isSelected = false;
1121 highlightBond1 = null;
1122 highlightBond2 = null;
1126 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1127 highlightBond1.at2.isSelected = true;
1130 if (index != mainchain.bonds.size())
1132 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1133 highlightBond2.at1.isSelected = true;
1140 redrawneeded = true;
1144 public Color getColour(int atomIndex, int pdbResNum, String chain,
1148 // if (!pdbfile.equals(pdbentry.getFile()))
1151 // return new Color(viewer.getAtomArgb(atomIndex));
1154 public void updateColours(Object source)
1157 redrawneeded = true;
1162 public void releaseReferences(Object svl)
1164 // TODO Auto-generated method stub