2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.io.DataSourceType;
30 import jalview.io.StructureFile;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
36 import java.awt.Color;
37 import java.awt.Dimension;
38 import java.awt.Event;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
43 import java.awt.Image;
44 import java.awt.RenderingHints;
45 import java.awt.event.KeyAdapter;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.io.PrintStream;
51 import java.util.List;
52 import java.util.Vector;
54 import javax.swing.JPanel;
55 import javax.swing.ToolTipManager;
57 public class PDBCanvas extends JPanel implements MouseListener,
58 MouseMotionListener, StructureListener
60 boolean redrawneeded = true;
70 public StructureFile pdb;
82 float[] centre = new float[3];
84 float[] width = new float[3];
94 boolean bysequence = true;
96 boolean depthcue = true;
100 boolean bymolecule = false;
102 boolean zbuffer = true;
118 Font font = new Font("Helvetica", Font.PLAIN, 10);
120 jalview.gui.SeqCanvas seqcanvas;
122 public SequenceI[] sequence;
124 final StringBuffer mappingDetails = new StringBuffer();
128 Vector<String> highlightRes;
130 boolean pdbAction = false;
132 boolean seqColoursReady = false;
134 jalview.renderer.seqfeatures.FeatureRenderer fr;
136 Color backgroundColour = Color.black;
140 StructureSelectionManager ssm;
144 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
145 AlignmentPanel ap, DataSourceType protocol)
148 this.pdbentry = pdbentry;
151 ssm = ap.av.getStructureSelectionManager();
155 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
157 if (protocol.equals(jalview.io.DataSourceType.PASTE))
159 pdbentry.setFile("INLINE" + pdb.getId());
162 } catch (Exception ex)
164 ex.printStackTrace();
170 errorMessage = "Error loading file: " + pdbentry.getId();
173 pdbentry.setId(pdb.getId());
175 ssm.addStructureViewerListener(this);
186 // JUST DEAL WITH ONE SEQUENCE FOR NOW
187 SequenceI sequence = seq[0];
189 for (int i = 0; i < pdb.getChains().size(); i++)
192 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
193 + pdb.getChains().elementAt(i).sequence.getSequenceAsString());
194 mappingDetails.append("\nNo of residues = "
195 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
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.getChains().elementAt(i).sequence, "pep");
202 as.calcScoreMatrix();
204 PrintStream ps = new PrintStream(System.out)
207 public void print(String x)
209 mappingDetails.append(x);
213 public void println()
215 mappingDetails.append("\n");
219 as.printAlignment(ps);
221 if (as.maxscore > max)
226 pdbstart = as.seq2start;
228 seqstart = as.seq1start + sequence.getStart() - 1;
229 seqend = as.seq1end + sequence.getEnd() - 1;
232 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
233 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
236 mainchain = pdb.getChains().elementAt(maxchain);
238 mainchain.pdbstart = pdbstart;
239 mainchain.pdbend = pdbend;
240 mainchain.seqstart = seqstart;
241 mainchain.seqend = seqend;
242 mainchain.isVisible = true;
245 this.prefsize = new Dimension(getSize().width, getSize().height);
247 addMouseMotionListener(this);
248 addMouseListener(this);
250 addKeyListener(new KeyAdapter()
253 public void keyPressed(KeyEvent evt)
266 ToolTipManager.sharedInstance().registerComponent(this);
267 ToolTipManager.sharedInstance().setInitialDelay(0);
268 ToolTipManager.sharedInstance().setDismissDelay(10000);
271 Vector<Bond> visiblebonds;
275 seqColoursReady = false;
276 // Sort the bonds by z coord
277 visiblebonds = new Vector<Bond>();
279 for (PDBChain chain : pdb.getChains())
283 for (Bond bond : chain.bonds)
285 visiblebonds.addElement(bond);
291 seqColoursReady = true;
296 public void findWidth()
298 float[] max = new float[3];
299 float[] min = new float[3];
301 max[0] = (float) -1e30;
302 max[1] = (float) -1e30;
303 max[2] = (float) -1e30;
305 min[0] = (float) 1e30;
306 min[1] = (float) 1e30;
307 min[2] = (float) 1e30;
309 for (PDBChain chain : pdb.getChains())
313 for (Bond tmp : chain.bonds)
315 if (tmp.start[0] >= max[0])
317 max[0] = tmp.start[0];
320 if (tmp.start[1] >= max[1])
322 max[1] = tmp.start[1];
325 if (tmp.start[2] >= max[2])
327 max[2] = tmp.start[2];
330 if (tmp.start[0] <= min[0])
332 min[0] = tmp.start[0];
335 if (tmp.start[1] <= min[1])
337 min[1] = tmp.start[1];
340 if (tmp.start[2] <= min[2])
342 min[2] = tmp.start[2];
345 if (tmp.end[0] >= max[0])
350 if (tmp.end[1] >= max[1])
355 if (tmp.end[2] >= max[2])
360 if (tmp.end[0] <= min[0])
365 if (tmp.end[1] <= min[1])
370 if (tmp.end[2] <= min[2])
378 * System.out.println("xmax " + max[0] + " min " + min[0]);
379 * System.out.println("ymax " + max[1] + " min " + min[1]);
380 * System.out.println("zmax " + max[2] + " min " + min[2]);
383 width[0] = Math.abs(max[0] - min[0]);
384 width[1] = Math.abs(max[1] - min[1]);
385 width[2] = Math.abs(max[2] - min[2]);
389 if (width[1] > width[0])
394 if (width[2] > width[1])
399 // System.out.println("Maxwidth = " + maxwidth);
402 public float findScale()
411 height = getHeight();
415 width = prefsize.width;
416 height = prefsize.height;
428 return (float) (dim / (1.5d * maxwidth));
431 public void findCentre()
439 // Find centre coordinate
440 for (PDBChain chain : pdb.getChains())
444 bsize += chain.bonds.size();
446 for (Bond bond : chain.bonds)
448 xtot = xtot + bond.start[0] + bond.end[0];
449 ytot = ytot + bond.start[1] + bond.end[1];
450 ztot = ztot + bond.start[2] + bond.end[2];
455 centre[0] = xtot / (2 * (float) bsize);
456 centre[1] = ytot / (2 * (float) bsize);
457 centre[2] = ztot / (2 * (float) bsize);
461 public void paintComponent(Graphics g)
463 super.paintComponent(g);
465 if (!seqColoursReady || errorMessage != null)
467 g.setColor(Color.black);
468 g.setFont(new Font("Verdana", Font.BOLD, 14));
469 g.drawString(errorMessage == null ? "Retrieving PDB data...."
470 : errorMessage, 20, getHeight() / 2);
474 // Only create the image at the beginning -
475 // this saves much memory usage
476 if ((img == null) || (prefsize.width != getWidth())
477 || (prefsize.height != getHeight()))
480 prefsize.width = getWidth();
481 prefsize.height = getHeight();
484 img = createImage(prefsize.width, prefsize.height);
485 ig = img.getGraphics();
486 Graphics2D ig2 = (Graphics2D) ig;
488 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
489 RenderingHints.VALUE_ANTIALIAS_ON);
496 drawAll(ig, prefsize.width, prefsize.height);
497 redrawneeded = false;
500 g.drawImage(img, 0, 0, this);
505 public void drawAll(Graphics g, int width, int height)
507 g.setColor(backgroundColour);
508 g.fillRect(0, 0, width, height);
513 public void updateSeqColours()
526 // This method has been taken out of PDBChain to allow
527 // Applet and Application specific sequence renderers to be used
528 void colourBySequence()
530 SequenceRenderer sr = new SequenceRenderer(ap.av);
532 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
534 boolean showFeatures = false;
535 if (ap.av.isShowSequenceFeatures())
539 fr = new FeatureRenderer(ap);
542 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
548 if (bysequence && pdb != null)
550 for (int ii = 0; ii < pdb.getChains().size(); ii++)
552 chain = pdb.getChains().elementAt(ii);
554 for (int i = 0; i < chain.bonds.size(); i++)
556 Bond tmp = chain.bonds.elementAt(i);
557 tmp.startCol = Color.lightGray;
558 tmp.endCol = Color.lightGray;
559 if (chain != mainchain)
564 for (int s = 0; s < sequence.length; s++)
566 for (int m = 0; m < mapping.length; m++)
568 if (mapping[m].getSequence() == sequence[s])
570 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
573 pos = sequence[s].findIndex(pos);
574 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
577 tmp.startCol = fr.findFeatureColour(tmp.startCol,
581 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
584 pos = sequence[s].findIndex(pos);
585 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
588 tmp.endCol = fr.findFeatureColour(tmp.endCol,
603 public void drawScene(Graphics g)
612 zsort.sort(visiblebonds);
616 for (int i = 0; i < visiblebonds.size(); i++)
618 tmpBond = visiblebonds.elementAt(i);
620 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
621 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
623 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
624 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
626 xmid = (xend + xstart) / 2;
627 ymid = (yend + ystart) / 2;
628 if (depthcue && !bymolecule)
630 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
633 g.setColor(tmpBond.startCol.darker().darker());
634 drawLine(g, xstart, ystart, xmid, ymid);
635 g.setColor(tmpBond.endCol.darker().darker());
636 drawLine(g, xmid, ymid, xend, yend);
639 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
641 g.setColor(tmpBond.startCol.darker());
642 drawLine(g, xstart, ystart, xmid, ymid);
644 g.setColor(tmpBond.endCol.darker());
645 drawLine(g, xmid, ymid, xend, yend);
649 g.setColor(tmpBond.startCol);
650 drawLine(g, xstart, ystart, xmid, ymid);
652 g.setColor(tmpBond.endCol);
653 drawLine(g, xmid, ymid, xend, yend);
656 else if (depthcue && bymolecule)
658 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
660 g.setColor(Color.green.darker().darker());
661 drawLine(g, xstart, ystart, xend, yend);
663 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
665 g.setColor(Color.green.darker());
666 drawLine(g, xstart, ystart, xend, yend);
670 g.setColor(Color.green);
671 drawLine(g, xstart, ystart, xend, yend);
674 else if (!depthcue && !bymolecule)
676 g.setColor(tmpBond.startCol);
677 drawLine(g, xstart, ystart, xmid, ymid);
678 g.setColor(tmpBond.endCol);
679 drawLine(g, xmid, ymid, xend, yend);
683 drawLine(g, xstart, ystart, xend, yend);
686 if (highlightBond1 != null && highlightBond1 == tmpBond)
688 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
690 drawLine(g, xmid, ymid, xend, yend);
693 if (highlightBond2 != null && highlightBond2 == tmpBond)
695 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
697 drawLine(g, xstart, ystart, xmid, ymid);
704 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
708 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
710 g.drawLine(x1, y1, x2, y2);
711 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
712 g.drawLine(x1, y1 - 1, x2, y2 - 1);
716 g.setColor(g.getColor().brighter());
717 g.drawLine(x1, y1, x2, y2);
718 g.drawLine(x1 + 1, y1, x2 + 1, y2);
719 g.drawLine(x1 - 1, y1, x2 - 1, y2);
724 g.drawLine(x1, y1, x2, y2);
728 public Dimension minimumsize()
733 public Dimension preferredsize()
738 public void keyPressed(KeyEvent evt)
740 if (evt.getKeyCode() == KeyEvent.VK_UP)
742 scale = (float) (scale * 1.1);
746 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
748 scale = (float) (scale * 0.9);
755 public void mousePressed(MouseEvent e)
758 Atom fatom = findAtom(e.getX(), e.getY());
761 fatom.isSelected = !fatom.isSelected;
765 if (foundchain != -1)
767 PDBChain chain = pdb.getChains().elementAt(foundchain);
768 if (chain == mainchain)
770 if (fatom.alignmentMapping != -1)
772 if (highlightRes == null)
774 highlightRes = new Vector<String>();
777 final String atomString = Integer
778 .toString(fatom.alignmentMapping);
779 if (highlightRes.contains(atomString))
781 highlightRes.remove(atomString);
785 highlightRes.add(atomString);
800 public void mouseMoved(MouseEvent e)
803 if (highlightBond1 != null)
805 highlightBond1.at2.isSelected = false;
806 highlightBond2.at1.isSelected = false;
807 highlightBond1 = null;
808 highlightBond2 = null;
811 Atom fatom = findAtom(e.getX(), e.getY());
813 PDBChain chain = null;
814 if (foundchain != -1)
816 chain = pdb.getChains().elementAt(foundchain);
817 if (chain == mainchain)
819 mouseOverStructure(fatom.resNumber, chain.id);
825 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
830 mouseOverStructure(-1, chain != null ? chain.id : null);
831 this.setToolTipText(null);
836 public void mouseClicked(MouseEvent e)
841 public void mouseEntered(MouseEvent e)
846 public void mouseExited(MouseEvent e)
851 public void mouseDragged(MouseEvent evt)
858 MCMatrix objmat = new MCMatrix(3, 3);
859 objmat.setIdentity();
861 if ((evt.getModifiers() & Event.META_MASK) != 0)
863 objmat.rotatez(((mx - omx)));
867 objmat.rotatex(((my - omy)));
868 objmat.rotatey(((omx - mx)));
872 for (PDBChain chain : pdb.getChains())
874 for (Bond tmpBond : chain.bonds)
876 // Translate the bond so the centre is 0,0,0
877 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
879 // Now apply the rotation matrix
880 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
881 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
883 // Now translate back again
884 tmpBond.translate(centre[0], centre[1], centre[2]);
901 public void mouseReleased(MouseEvent evt)
907 void drawLabels(Graphics g)
910 for (PDBChain chain : pdb.getChains())
914 for (Bond tmpBond : chain.bonds)
916 if (tmpBond.at1.isSelected)
918 labelAtom(g, tmpBond, 1);
921 if (tmpBond.at2.isSelected)
923 labelAtom(g, tmpBond, 2);
930 public void labelAtom(Graphics g, Bond b, int n)
933 g.setColor(Color.red);
936 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
937 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
939 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
944 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
945 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
947 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
953 public Atom findAtom(int x, int y)
959 for (int ii = 0; ii < pdb.getChains().size(); ii++)
961 PDBChain chain = pdb.getChains().elementAt(ii);
967 for (Bond bond : chain.bonds)
971 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
973 if (Math.abs(truex - x) <= 2)
975 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
977 if (Math.abs(truey - y) <= 2)
986 // Still here? Maybe its the last bond
988 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
990 if (Math.abs(truex - x) <= 2)
992 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
994 if (Math.abs(truey - y) <= 2)
1004 if (fatom != null) // )&& chain.ds != null)
1005 { // dead code? value of chain is either overwritten or discarded
1006 chain = pdb.getChains().elementAt(foundchain);
1013 Bond highlightBond1, highlightBond2;
1015 public void highlightRes(int ii)
1017 if (!seqColoursReady)
1022 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1029 for (index = 0; index < mainchain.bonds.size(); index++)
1031 tmpBond = mainchain.bonds.elementAt(index);
1032 if (tmpBond.at1.alignmentMapping == ii - 1)
1034 if (highlightBond1 != null)
1036 highlightBond1.at2.isSelected = false;
1039 if (highlightBond2 != null)
1041 highlightBond2.at1.isSelected = false;
1044 highlightBond1 = null;
1045 highlightBond2 = null;
1049 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1050 highlightBond1.at2.isSelected = true;
1053 if (index != mainchain.bonds.size())
1055 highlightBond2 = mainchain.bonds.elementAt(index);
1056 highlightBond2.at1.isSelected = true;
1063 redrawneeded = true;
1067 public void setAllchainsVisible(boolean b)
1069 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1071 PDBChain chain = pdb.getChains().elementAt(ii);
1072 chain.isVisible = b;
1074 mainchain.isVisible = true;
1079 // ////////////////////////////////
1080 // /StructureListener
1082 public String[] getPdbFile()
1084 return new String[] { pdbentry.getFile() };
1089 public void mouseOverStructure(int pdbResNum, String chain)
1091 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1093 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1096 lastMessage = pdbResNum + chain;
1099 StringBuffer resetLastRes = new StringBuffer();
1101 StringBuffer eval = new StringBuffer();
1104 * Highlight the specified atoms in the structure.
1109 public void highlightAtoms(List<AtomSpec> atoms)
1111 if (!seqColoursReady)
1116 for (AtomSpec atom : atoms)
1118 int atomIndex = atom.getAtomIndex();
1119 if (highlightRes != null
1120 && highlightRes.contains((atomIndex - 1) + ""))
1125 highlightAtom(atomIndex);
1128 redrawneeded = true;
1133 * Highlight the atom at the specified index.
1137 protected void highlightAtom(int atomIndex)
1141 for (index = 0; index < mainchain.bonds.size(); index++)
1143 tmpBond = mainchain.bonds.elementAt(index);
1144 if (tmpBond.at1.atomIndex == atomIndex)
1146 if (highlightBond1 != null)
1148 highlightBond1.at2.isSelected = false;
1151 if (highlightBond2 != null)
1153 highlightBond2.at1.isSelected = false;
1156 highlightBond1 = null;
1157 highlightBond2 = null;
1161 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1162 highlightBond1.at2.isSelected = true;
1165 if (index != mainchain.bonds.size())
1167 highlightBond2 = mainchain.bonds.elementAt(index);
1168 highlightBond2.at1.isSelected = true;
1176 public Color getColour(int atomIndex, int pdbResNum, String chain,
1180 // if (!pdbfile.equals(pdbentry.getFile()))
1183 // return new Color(viewer.getAtomArgb(atomIndex));
1187 public void updateColours(Object source)
1190 redrawneeded = true;
1195 public void releaseReferences(Object svl)
1197 // TODO Auto-generated method stub
1202 public boolean isListeningFor(SequenceI seq)
1204 if (sequence != null)
1206 for (SequenceI s : sequence)