2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignmentPanel;
27 import jalview.gui.FeatureRenderer;
28 import jalview.gui.SequenceRenderer;
29 import jalview.structure.AtomSpec;
30 import jalview.structure.StructureListener;
31 import jalview.structure.StructureMapping;
32 import jalview.structure.StructureSelectionManager;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Event;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
41 import java.awt.Image;
42 import java.awt.RenderingHints;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.io.PrintStream;
49 import java.util.List;
50 import java.util.Vector;
52 import javax.swing.JPanel;
53 import javax.swing.ToolTipManager;
55 public class PDBCanvas extends JPanel implements MouseListener,
56 MouseMotionListener, StructureListener
58 boolean redrawneeded = true;
80 float[] centre = new float[3];
82 float[] width = new float[3];
92 boolean bysequence = true;
94 boolean depthcue = true;
98 boolean bymolecule = false;
100 boolean zbuffer = true;
116 Font font = new Font("Helvetica", Font.PLAIN, 10);
118 jalview.gui.SeqCanvas seqcanvas;
120 public SequenceI[] sequence;
122 final StringBuffer mappingDetails = new StringBuffer();
128 boolean pdbAction = false;
130 boolean seqColoursReady = false;
132 jalview.gui.FeatureRenderer fr;
134 Color backgroundColour = Color.black;
138 StructureSelectionManager ssm;
142 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
143 AlignmentPanel ap, String protocol)
146 this.pdbentry = pdbentry;
149 ssm = ap.av.getStructureSelectionManager();
153 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
155 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
157 pdbentry.setFile("INLINE" + pdb.id);
160 } catch (Exception ex)
162 ex.printStackTrace();
168 errorMessage = "Error loading file: " + pdbentry.getId();
171 pdbentry.setId(pdb.id);
173 ssm.addStructureViewerListener(this);
184 // JUST DEAL WITH ONE SEQUENCE FOR NOW
185 SequenceI sequence = seq[0];
187 for (int i = 0; i < pdb.chains.size(); i++)
190 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
191 + pdb.chains.elementAt(i).sequence
192 .getSequenceAsString());
193 mappingDetails.append("\nNo of residues = "
194 + pdb.chains.elementAt(i).residues.size()
197 // Now lets compare the sequences to get
198 // the start and end points.
199 // Align the sequence to the pdb
200 AlignSeq as = new AlignSeq(sequence,
201 pdb.chains.elementAt(i).sequence, "pep");
202 as.calcScoreMatrix();
204 PrintStream ps = new PrintStream(System.out)
206 public void print(String x)
208 mappingDetails.append(x);
211 public void println()
213 mappingDetails.append("\n");
217 as.printAlignment(ps);
219 if (as.maxscore > max)
224 pdbstart = as.seq2start;
226 seqstart = as.seq1start + sequence.getStart() - 1;
227 seqend = as.seq1end + sequence.getEnd() - 1;
230 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
231 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
234 mainchain = pdb.chains.elementAt(maxchain);
236 mainchain.pdbstart = pdbstart;
237 mainchain.pdbend = pdbend;
238 mainchain.seqstart = seqstart;
239 mainchain.seqend = seqend;
240 mainchain.isVisible = true;
243 this.prefsize = new Dimension(getSize().width, getSize().height);
245 addMouseMotionListener(this);
246 addMouseListener(this);
248 addKeyListener(new KeyAdapter()
250 public void keyPressed(KeyEvent evt)
263 ToolTipManager.sharedInstance().registerComponent(this);
264 ToolTipManager.sharedInstance().setInitialDelay(0);
265 ToolTipManager.sharedInstance().setDismissDelay(10000);
272 seqColoursReady = false;
273 // Sort the bonds by z coord
274 visiblebonds = new Vector();
276 for (int ii = 0; ii < pdb.chains.size(); ii++)
278 if (pdb.chains.elementAt(ii).isVisible)
280 Vector tmp = pdb.chains.elementAt(ii).bonds;
282 for (int i = 0; i < tmp.size(); i++)
284 visiblebonds.addElement(tmp.elementAt(i));
290 seqColoursReady = true;
295 public void findWidth()
297 float[] max = new float[3];
298 float[] min = new float[3];
300 max[0] = (float) -1e30;
301 max[1] = (float) -1e30;
302 max[2] = (float) -1e30;
304 min[0] = (float) 1e30;
305 min[1] = (float) 1e30;
306 min[2] = (float) 1e30;
308 for (int ii = 0; ii < pdb.chains.size(); ii++)
310 if (pdb.chains.elementAt(ii).isVisible)
312 Vector bonds = pdb.chains.elementAt(ii).bonds;
314 for (int i = 0; i < bonds.size(); i++)
316 Bond tmp = (Bond) bonds.elementAt(i);
318 if (tmp.start[0] >= max[0])
320 max[0] = tmp.start[0];
323 if (tmp.start[1] >= max[1])
325 max[1] = tmp.start[1];
328 if (tmp.start[2] >= max[2])
330 max[2] = tmp.start[2];
333 if (tmp.start[0] <= min[0])
335 min[0] = tmp.start[0];
338 if (tmp.start[1] <= min[1])
340 min[1] = tmp.start[1];
343 if (tmp.start[2] <= min[2])
345 min[2] = tmp.start[2];
348 if (tmp.end[0] >= max[0])
353 if (tmp.end[1] >= max[1])
358 if (tmp.end[2] >= max[2])
363 if (tmp.end[0] <= min[0])
368 if (tmp.end[1] <= min[1])
373 if (tmp.end[2] <= min[2])
381 * System.out.println("xmax " + max[0] + " min " + min[0]);
382 * System.out.println("ymax " + max[1] + " min " + min[1]);
383 * System.out.println("zmax " + max[2] + " min " + min[2]);
386 width[0] = Math.abs(max[0] - min[0]);
387 width[1] = Math.abs(max[1] - min[1]);
388 width[2] = Math.abs(max[2] - min[2]);
392 if (width[1] > width[0])
397 if (width[2] > width[1])
402 // System.out.println("Maxwidth = " + maxwidth);
405 public float findScale()
414 height = getHeight();
418 width = prefsize.width;
419 height = prefsize.height;
431 return (float) (dim / (1.5d * maxwidth));
434 public void findCentre()
442 // Find centre coordinate
443 for (int ii = 0; ii < pdb.chains.size(); ii++)
445 if (pdb.chains.elementAt(ii).isVisible)
447 Vector bonds = pdb.chains.elementAt(ii).bonds;
449 bsize += bonds.size();
451 for (int i = 0; i < bonds.size(); i++)
453 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
454 + ((Bond) bonds.elementAt(i)).end[0];
456 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
457 + ((Bond) bonds.elementAt(i)).end[1];
459 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
460 + ((Bond) bonds.elementAt(i)).end[2];
465 centre[0] = xtot / (2 * (float) bsize);
466 centre[1] = ytot / (2 * (float) bsize);
467 centre[2] = ztot / (2 * (float) bsize);
470 public void paintComponent(Graphics g)
472 super.paintComponent(g);
474 if (!seqColoursReady || errorMessage != null)
476 g.setColor(Color.black);
477 g.setFont(new Font("Verdana", Font.BOLD, 14));
478 g.drawString(errorMessage == null ? "Retrieving PDB data...."
479 : errorMessage, 20, getHeight() / 2);
483 // Only create the image at the beginning -
484 // this saves much memory usage
485 if ((img == null) || (prefsize.width != getWidth())
486 || (prefsize.height != getHeight()))
489 prefsize.width = getWidth();
490 prefsize.height = getHeight();
493 img = createImage(prefsize.width, prefsize.height);
494 ig = img.getGraphics();
495 Graphics2D ig2 = (Graphics2D) ig;
497 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
498 RenderingHints.VALUE_ANTIALIAS_ON);
505 drawAll(ig, prefsize.width, prefsize.height);
506 redrawneeded = false;
509 g.drawImage(img, 0, 0, this);
514 public void drawAll(Graphics g, int width, int height)
516 g.setColor(backgroundColour);
517 g.fillRect(0, 0, width, height);
522 public void updateSeqColours()
535 // This method has been taken out of PDBChain to allow
536 // Applet and Application specific sequence renderers to be used
537 void colourBySequence()
539 SequenceRenderer sr = new SequenceRenderer(ap.av);
541 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
543 boolean showFeatures = false;
544 if (ap.av.getShowSequenceFeatures())
548 fr = new FeatureRenderer(ap);
551 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
557 if (bysequence && pdb != null)
559 for (int ii = 0; ii < pdb.chains.size(); ii++)
561 chain = pdb.chains.elementAt(ii);
563 for (int i = 0; i < chain.bonds.size(); i++)
565 Bond tmp = (Bond) chain.bonds.elementAt(i);
566 tmp.startCol = Color.lightGray;
567 tmp.endCol = Color.lightGray;
568 if (chain != mainchain)
573 for (int s = 0; s < sequence.length; s++)
575 for (int m = 0; m < mapping.length; m++)
577 if (mapping[m].getSequence() == sequence[s])
579 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
582 pos = sequence[s].findIndex(pos);
583 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
586 tmp.startCol = fr.findFeatureColour(tmp.startCol,
590 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
593 pos = sequence[s].findIndex(pos);
594 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
597 tmp.endCol = fr.findFeatureColour(tmp.endCol,
612 public void drawScene(Graphics g)
621 zsort.Zsort(visiblebonds);
625 for (int i = 0; i < visiblebonds.size(); i++)
627 tmpBond = (Bond) visiblebonds.elementAt(i);
629 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
630 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
632 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
633 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
635 xmid = (xend + xstart) / 2;
636 ymid = (yend + ystart) / 2;
637 if (depthcue && !bymolecule)
639 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
642 g.setColor(tmpBond.startCol.darker().darker());
643 drawLine(g, xstart, ystart, xmid, ymid);
644 g.setColor(tmpBond.endCol.darker().darker());
645 drawLine(g, xmid, ymid, xend, yend);
648 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
650 g.setColor(tmpBond.startCol.darker());
651 drawLine(g, xstart, ystart, xmid, ymid);
653 g.setColor(tmpBond.endCol.darker());
654 drawLine(g, xmid, ymid, xend, yend);
658 g.setColor(tmpBond.startCol);
659 drawLine(g, xstart, ystart, xmid, ymid);
661 g.setColor(tmpBond.endCol);
662 drawLine(g, xmid, ymid, xend, yend);
665 else if (depthcue && bymolecule)
667 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
669 g.setColor(Color.green.darker().darker());
670 drawLine(g, xstart, ystart, xend, yend);
672 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
674 g.setColor(Color.green.darker());
675 drawLine(g, xstart, ystart, xend, yend);
679 g.setColor(Color.green);
680 drawLine(g, xstart, ystart, xend, yend);
683 else if (!depthcue && !bymolecule)
685 g.setColor(tmpBond.startCol);
686 drawLine(g, xstart, ystart, xmid, ymid);
687 g.setColor(tmpBond.endCol);
688 drawLine(g, xmid, ymid, xend, yend);
692 drawLine(g, xstart, ystart, xend, yend);
695 if (highlightBond1 != null && highlightBond1 == tmpBond)
697 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
699 drawLine(g, xmid, ymid, xend, yend);
702 if (highlightBond2 != null && highlightBond2 == tmpBond)
704 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
706 drawLine(g, xstart, ystart, xmid, ymid);
713 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
717 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
719 g.drawLine(x1, y1, x2, y2);
720 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
721 g.drawLine(x1, y1 - 1, x2, y2 - 1);
725 g.setColor(g.getColor().brighter());
726 g.drawLine(x1, y1, x2, y2);
727 g.drawLine(x1 + 1, y1, x2 + 1, y2);
728 g.drawLine(x1 - 1, y1, x2 - 1, y2);
733 g.drawLine(x1, y1, x2, y2);
737 public Dimension minimumsize()
742 public Dimension preferredsize()
747 public void keyPressed(KeyEvent evt)
749 if (evt.getKeyCode() == KeyEvent.VK_UP)
751 scale = (float) (scale * 1.1);
755 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
757 scale = (float) (scale * 0.9);
763 public void mousePressed(MouseEvent e)
766 Atom fatom = findAtom(e.getX(), e.getY());
769 fatom.isSelected = !fatom.isSelected;
773 if (foundchain != -1)
775 PDBChain chain = pdb.chains.elementAt(foundchain);
776 if (chain == mainchain)
778 if (fatom.alignmentMapping != -1)
780 if (highlightRes == null)
782 highlightRes = new Vector();
785 if (highlightRes.contains(fatom.alignmentMapping + ""))
787 highlightRes.remove(fatom.alignmentMapping + "");
791 highlightRes.add(fatom.alignmentMapping + "");
805 public void mouseMoved(MouseEvent e)
808 if (highlightBond1 != null)
810 highlightBond1.at2.isSelected = false;
811 highlightBond2.at1.isSelected = false;
812 highlightBond1 = null;
813 highlightBond2 = null;
816 Atom fatom = findAtom(e.getX(), e.getY());
818 PDBChain chain = null;
819 if (foundchain != -1)
821 chain = pdb.chains.elementAt(foundchain);
822 if (chain == mainchain)
824 mouseOverStructure(fatom.resNumber, chain.id);
830 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
835 mouseOverStructure(-1, chain != null ? chain.id : null);
836 this.setToolTipText(null);
840 public void mouseClicked(MouseEvent e)
844 public void mouseEntered(MouseEvent e)
848 public void mouseExited(MouseEvent e)
852 public void mouseDragged(MouseEvent evt)
859 MCMatrix objmat = new MCMatrix(3, 3);
860 objmat.setIdentity();
862 if ((evt.getModifiers() & Event.META_MASK) != 0)
864 objmat.rotatez(((mx - omx)));
868 objmat.rotatex(((my - omy)));
869 objmat.rotatey(((omx - mx)));
873 for (int ii = 0; ii < pdb.chains.size(); ii++)
875 Vector bonds = pdb.chains.elementAt(ii).bonds;
877 for (int i = 0; i < bonds.size(); i++)
879 Bond tmpBond = (Bond) bonds.elementAt(i);
881 // Translate the bond so the centre is 0,0,0
882 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
884 // Now apply the rotation matrix
885 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
886 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
888 // Now translate back again
889 tmpBond.translate(centre[0], centre[1], centre[2]);
905 public void mouseReleased(MouseEvent evt)
911 void drawLabels(Graphics g)
914 for (int ii = 0; ii < pdb.chains.size(); ii++)
916 PDBChain chain = pdb.chains.elementAt(ii);
920 Vector bonds = pdb.chains.elementAt(ii).bonds;
922 for (int i = 0; i < bonds.size(); i++)
924 Bond tmpBond = (Bond) bonds.elementAt(i);
926 if (tmpBond.at1.isSelected)
928 labelAtom(g, tmpBond, 1);
931 if (tmpBond.at2.isSelected)
934 labelAtom(g, tmpBond, 2);
941 public void labelAtom(Graphics g, Bond b, int n)
944 g.setColor(Color.red);
947 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
948 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
950 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
955 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
956 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
958 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
964 public Atom findAtom(int x, int y)
970 for (int ii = 0; ii < pdb.chains.size(); ii++)
972 PDBChain chain = pdb.chains.elementAt(ii);
978 Vector bonds = pdb.chains.elementAt(ii).bonds;
980 for (int i = 0; i < bonds.size(); i++)
982 tmpBond = (Bond) bonds.elementAt(i);
984 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
986 if (Math.abs(truex - x) <= 2)
988 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
990 if (Math.abs(truey - y) <= 2)
999 // Still here? Maybe its the last bond
1001 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
1003 if (Math.abs(truex - x) <= 2)
1005 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
1007 if (Math.abs(truey - y) <= 2)
1009 fatom = tmpBond.at2;
1017 if (fatom != null) // )&& chain.ds != null)
1019 chain = pdb.chains.elementAt(foundchain);
1026 Bond highlightBond1, highlightBond2;
1028 public void highlightRes(int ii)
1030 if (!seqColoursReady)
1035 if (highlightRes != null && 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 = pdb.chains.elementAt(ii);
1085 chain.isVisible = b;
1087 mainchain.isVisible = true;
1092 // ////////////////////////////////
1093 // /StructureListener
1094 public String[] getPdbFile()
1097 { pdbentry.getFile() };
1102 public void mouseOverStructure(int pdbResNum, String chain)
1104 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1106 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1109 lastMessage = pdbResNum + chain;
1112 StringBuffer resetLastRes = new StringBuffer();
1114 StringBuffer eval = new StringBuffer();
1117 * Highlight the specified atoms in the structure.
1122 public void highlightAtoms(List<AtomSpec> atoms)
1124 if (!seqColoursReady)
1129 for (AtomSpec atom : atoms)
1131 int atomIndex = atom.getAtomIndex();
1132 if (highlightRes != null
1133 && highlightRes.contains((atomIndex - 1) + ""))
1138 highlightAtom(atomIndex);
1141 redrawneeded = true;
1146 * Highlight the atom at the specified index.
1150 protected void highlightAtom(int atomIndex)
1154 for (index = 0; index < mainchain.bonds.size(); index++)
1156 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1157 if (tmpBond.at1.atomIndex == atomIndex)
1159 if (highlightBond1 != null)
1161 highlightBond1.at2.isSelected = false;
1164 if (highlightBond2 != null)
1166 highlightBond2.at1.isSelected = false;
1169 highlightBond1 = null;
1170 highlightBond2 = null;
1174 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1175 highlightBond1.at2.isSelected = true;
1178 if (index != mainchain.bonds.size())
1180 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1181 highlightBond2.at1.isSelected = true;
1189 public Color getColour(int atomIndex, int pdbResNum, String chain,
1193 // if (!pdbfile.equals(pdbentry.getFile()))
1196 // return new Color(viewer.getAtomArgb(atomIndex));
1199 public void updateColours(Object source)
1202 redrawneeded = true;
1207 public void releaseReferences(Object svl)
1209 // TODO Auto-generated method stub