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.SeqCanvas;
29 import jalview.gui.SequenceRenderer;
30 import jalview.io.AppletFormatAdapter;
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;
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);
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, String protocol)
148 this.pdbentry = pdbentry;
151 ssm = ap.av.getStructureSelectionManager();
155 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
157 if (protocol.equals(AppletFormatAdapter.PASTE))
159 pdbentry.setFile("INLINE" + pdb.id);
162 } catch (Exception ex)
164 ex.printStackTrace();
170 errorMessage = "Error loading file: " + pdbentry.getId();
173 pdbentry.setId(pdb.id);
175 ssm.addStructureViewerListener(this);
186 // JUST DEAL WITH ONE SEQUENCE FOR NOW
187 SequenceI sequence = seq[0];
189 for (int i = 0; i < pdb.chains.size(); i++)
192 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
193 + pdb.chains.elementAt(i).sequence
194 .getSequenceAsString());
195 mappingDetails.append("\nNo of residues = "
196 + pdb.chains.elementAt(i).residues.size()
199 // Now lets compare the sequences to get
200 // the start and end points.
201 // Align the sequence to the pdb
202 AlignSeq as = new AlignSeq(sequence,
203 pdb.chains.elementAt(i).sequence, "pep");
204 as.calcScoreMatrix();
206 PrintStream ps = new PrintStream(System.out)
208 public void print(String x)
210 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.chains.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()
252 public void keyPressed(KeyEvent evt)
265 ToolTipManager.sharedInstance().registerComponent(this);
266 ToolTipManager.sharedInstance().setInitialDelay(0);
267 ToolTipManager.sharedInstance().setDismissDelay(10000);
270 Vector<Bond> visiblebonds;
274 seqColoursReady = false;
275 // Sort the bonds by z coord
276 visiblebonds = new Vector<Bond>();
278 for (PDBChain chain : pdb.chains)
282 for (Bond bond : chain.bonds)
284 visiblebonds.addElement(bond);
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 (PDBChain chain : pdb.chains)
312 for (Bond tmp : chain.bonds)
314 if (tmp.start[0] >= max[0])
316 max[0] = tmp.start[0];
319 if (tmp.start[1] >= max[1])
321 max[1] = tmp.start[1];
324 if (tmp.start[2] >= max[2])
326 max[2] = tmp.start[2];
329 if (tmp.start[0] <= min[0])
331 min[0] = tmp.start[0];
334 if (tmp.start[1] <= min[1])
336 min[1] = tmp.start[1];
339 if (tmp.start[2] <= min[2])
341 min[2] = tmp.start[2];
344 if (tmp.end[0] >= max[0])
349 if (tmp.end[1] >= max[1])
354 if (tmp.end[2] >= max[2])
359 if (tmp.end[0] <= min[0])
364 if (tmp.end[1] <= min[1])
369 if (tmp.end[2] <= min[2])
377 * System.out.println("xmax " + max[0] + " min " + min[0]);
378 * System.out.println("ymax " + max[1] + " min " + min[1]);
379 * System.out.println("zmax " + max[2] + " min " + min[2]);
382 width[0] = Math.abs(max[0] - min[0]);
383 width[1] = Math.abs(max[1] - min[1]);
384 width[2] = Math.abs(max[2] - min[2]);
388 if (width[1] > width[0])
393 if (width[2] > width[1])
398 // System.out.println("Maxwidth = " + maxwidth);
401 public float findScale()
410 height = getHeight();
414 width = prefsize.width;
415 height = prefsize.height;
427 return (float) (dim / (1.5d * maxwidth));
430 public void findCentre()
438 // Find centre coordinate
439 for (PDBChain chain : pdb.chains)
443 bsize += chain.bonds.size();
445 for (Bond bond : chain.bonds)
447 xtot = xtot + bond.start[0] + bond.end[0];
448 ytot = ytot + bond.start[1] + bond.end[1];
449 ztot = ztot + bond.start[2] + bond.end[2];
454 centre[0] = xtot / (2 * (float) bsize);
455 centre[1] = ytot / (2 * (float) bsize);
456 centre[2] = ztot / (2 * (float) bsize);
459 public void paintComponent(Graphics g)
461 super.paintComponent(g);
463 if (!seqColoursReady || errorMessage != null)
465 g.setColor(Color.black);
466 g.setFont(new Font("Verdana", Font.BOLD, 14));
467 g.drawString(errorMessage == null ? "Retrieving PDB data...."
468 : errorMessage, 20, getHeight() / 2);
472 // Only create the image at the beginning -
473 // this saves much memory usage
474 if ((img == null) || (prefsize.width != getWidth())
475 || (prefsize.height != getHeight()))
478 prefsize.width = getWidth();
479 prefsize.height = getHeight();
482 img = createImage(prefsize.width, prefsize.height);
483 ig = img.getGraphics();
484 Graphics2D ig2 = (Graphics2D) ig;
486 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
487 RenderingHints.VALUE_ANTIALIAS_ON);
494 drawAll(ig, prefsize.width, prefsize.height);
495 redrawneeded = false;
498 g.drawImage(img, 0, 0, this);
503 public void drawAll(Graphics g, int width, int height)
505 g.setColor(backgroundColour);
506 g.fillRect(0, 0, width, height);
511 public void updateSeqColours()
524 // This method has been taken out of PDBChain to allow
525 // Applet and Application specific sequence renderers to be used
526 void colourBySequence()
528 SequenceRenderer sr = new SequenceRenderer(ap.av);
530 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
532 boolean showFeatures = false;
533 if (ap.av.isShowSequenceFeatures())
537 fr = new FeatureRenderer(ap);
540 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
546 if (bysequence && pdb != null)
548 for (int ii = 0; ii < pdb.chains.size(); ii++)
550 chain = pdb.chains.elementAt(ii);
552 for (int i = 0; i < chain.bonds.size(); i++)
554 Bond tmp = chain.bonds.elementAt(i);
555 tmp.startCol = Color.lightGray;
556 tmp.endCol = Color.lightGray;
557 if (chain != mainchain)
562 for (int s = 0; s < sequence.length; s++)
564 for (int m = 0; m < mapping.length; m++)
566 if (mapping[m].getSequence() == sequence[s])
568 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
571 pos = sequence[s].findIndex(pos);
572 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
575 tmp.startCol = fr.findFeatureColour(tmp.startCol,
579 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
582 pos = sequence[s].findIndex(pos);
583 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
586 tmp.endCol = fr.findFeatureColour(tmp.endCol,
601 public void drawScene(Graphics g)
610 zsort.sort(visiblebonds);
614 for (int i = 0; i < visiblebonds.size(); i++)
616 tmpBond = visiblebonds.elementAt(i);
618 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
619 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
621 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
622 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
624 xmid = (xend + xstart) / 2;
625 ymid = (yend + ystart) / 2;
626 if (depthcue && !bymolecule)
628 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
631 g.setColor(tmpBond.startCol.darker().darker());
632 drawLine(g, xstart, ystart, xmid, ymid);
633 g.setColor(tmpBond.endCol.darker().darker());
634 drawLine(g, xmid, ymid, xend, yend);
637 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
639 g.setColor(tmpBond.startCol.darker());
640 drawLine(g, xstart, ystart, xmid, ymid);
642 g.setColor(tmpBond.endCol.darker());
643 drawLine(g, xmid, ymid, xend, yend);
647 g.setColor(tmpBond.startCol);
648 drawLine(g, xstart, ystart, xmid, ymid);
650 g.setColor(tmpBond.endCol);
651 drawLine(g, xmid, ymid, xend, yend);
654 else if (depthcue && bymolecule)
656 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
658 g.setColor(Color.green.darker().darker());
659 drawLine(g, xstart, ystart, xend, yend);
661 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
663 g.setColor(Color.green.darker());
664 drawLine(g, xstart, ystart, xend, yend);
668 g.setColor(Color.green);
669 drawLine(g, xstart, ystart, xend, yend);
672 else if (!depthcue && !bymolecule)
674 g.setColor(tmpBond.startCol);
675 drawLine(g, xstart, ystart, xmid, ymid);
676 g.setColor(tmpBond.endCol);
677 drawLine(g, xmid, ymid, xend, yend);
681 drawLine(g, xstart, ystart, xend, yend);
684 if (highlightBond1 != null && highlightBond1 == tmpBond)
686 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
688 drawLine(g, xmid, ymid, xend, yend);
691 if (highlightBond2 != null && highlightBond2 == tmpBond)
693 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
695 drawLine(g, xstart, ystart, xmid, ymid);
702 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
706 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
708 g.drawLine(x1, y1, x2, y2);
709 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
710 g.drawLine(x1, y1 - 1, x2, y2 - 1);
714 g.setColor(g.getColor().brighter());
715 g.drawLine(x1, y1, x2, y2);
716 g.drawLine(x1 + 1, y1, x2 + 1, y2);
717 g.drawLine(x1 - 1, y1, x2 - 1, y2);
722 g.drawLine(x1, y1, x2, y2);
726 public Dimension minimumsize()
731 public Dimension preferredsize()
736 public void keyPressed(KeyEvent evt)
738 if (evt.getKeyCode() == KeyEvent.VK_UP)
740 scale = (float) (scale * 1.1);
744 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
746 scale = (float) (scale * 0.9);
752 public void mousePressed(MouseEvent e)
755 Atom fatom = findAtom(e.getX(), e.getY());
758 fatom.isSelected = !fatom.isSelected;
762 if (foundchain != -1)
764 PDBChain chain = pdb.chains.elementAt(foundchain);
765 if (chain == mainchain)
767 if (fatom.alignmentMapping != -1)
769 if (highlightRes == null)
771 highlightRes = new Vector<String>();
774 final String atomString = Integer
775 .toString(fatom.alignmentMapping);
776 if (highlightRes.contains(atomString))
778 highlightRes.remove(atomString);
782 highlightRes.add(atomString);
796 public void mouseMoved(MouseEvent e)
799 if (highlightBond1 != null)
801 highlightBond1.at2.isSelected = false;
802 highlightBond2.at1.isSelected = false;
803 highlightBond1 = null;
804 highlightBond2 = null;
807 Atom fatom = findAtom(e.getX(), e.getY());
809 PDBChain chain = null;
810 if (foundchain != -1)
812 chain = pdb.chains.elementAt(foundchain);
813 if (chain == mainchain)
815 mouseOverStructure(fatom.resNumber, chain.id);
821 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
826 mouseOverStructure(-1, chain != null ? chain.id : null);
827 this.setToolTipText(null);
831 public void mouseClicked(MouseEvent e)
835 public void mouseEntered(MouseEvent e)
839 public void mouseExited(MouseEvent e)
843 public void mouseDragged(MouseEvent evt)
850 MCMatrix objmat = new MCMatrix(3, 3);
851 objmat.setIdentity();
853 if ((evt.getModifiers() & Event.META_MASK) != 0)
855 objmat.rotatez(((mx - omx)));
859 objmat.rotatex(((my - omy)));
860 objmat.rotatey(((omx - mx)));
864 for (PDBChain chain : pdb.chains)
866 for (Bond tmpBond : chain.bonds)
868 // Translate the bond so the centre is 0,0,0
869 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
871 // Now apply the rotation matrix
872 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
873 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
875 // Now translate back again
876 tmpBond.translate(centre[0], centre[1], centre[2]);
892 public void mouseReleased(MouseEvent evt)
898 void drawLabels(Graphics g)
901 for (PDBChain chain : pdb.chains)
905 for (Bond tmpBond : chain.bonds)
907 if (tmpBond.at1.isSelected)
909 labelAtom(g, tmpBond, 1);
912 if (tmpBond.at2.isSelected)
914 labelAtom(g, tmpBond, 2);
921 public void labelAtom(Graphics g, Bond b, int n)
924 g.setColor(Color.red);
927 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
928 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
930 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
935 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
936 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
938 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
944 public Atom findAtom(int x, int y)
950 for (int ii = 0; ii < pdb.chains.size(); ii++)
952 PDBChain chain = pdb.chains.elementAt(ii);
958 for (Bond bond : chain.bonds)
962 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
964 if (Math.abs(truex - x) <= 2)
966 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
968 if (Math.abs(truey - y) <= 2)
977 // Still here? Maybe its the last bond
979 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
981 if (Math.abs(truex - x) <= 2)
983 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
985 if (Math.abs(truey - y) <= 2)
995 if (fatom != null) // )&& chain.ds != null)
996 { // dead code? value of chain is either overwritten or discarded
997 chain = pdb.chains.elementAt(foundchain);
1004 Bond highlightBond1, highlightBond2;
1006 public void highlightRes(int ii)
1008 if (!seqColoursReady)
1013 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1020 for (index = 0; index < mainchain.bonds.size(); index++)
1022 tmpBond = mainchain.bonds.elementAt(index);
1023 if (tmpBond.at1.alignmentMapping == ii - 1)
1025 if (highlightBond1 != null)
1027 highlightBond1.at2.isSelected = false;
1030 if (highlightBond2 != null)
1032 highlightBond2.at1.isSelected = false;
1035 highlightBond1 = null;
1036 highlightBond2 = null;
1040 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1041 highlightBond1.at2.isSelected = true;
1044 if (index != mainchain.bonds.size())
1046 highlightBond2 = mainchain.bonds.elementAt(index);
1047 highlightBond2.at1.isSelected = true;
1054 redrawneeded = true;
1058 public void setAllchainsVisible(boolean b)
1060 for (int ii = 0; ii < pdb.chains.size(); ii++)
1062 PDBChain chain = pdb.chains.elementAt(ii);
1063 chain.isVisible = b;
1065 mainchain.isVisible = true;
1070 // ////////////////////////////////
1071 // /StructureListener
1072 public String[] getPdbFile()
1075 { pdbentry.getFile() };
1080 public void mouseOverStructure(int pdbResNum, String chain)
1082 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1084 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1087 lastMessage = pdbResNum + chain;
1090 StringBuffer resetLastRes = new StringBuffer();
1092 StringBuffer eval = new StringBuffer();
1095 * Highlight the specified atoms in the structure.
1100 public void highlightAtoms(List<AtomSpec> atoms)
1102 if (!seqColoursReady)
1107 for (AtomSpec atom : atoms)
1109 int atomIndex = atom.getAtomIndex();
1110 if (highlightRes != null
1111 && highlightRes.contains((atomIndex - 1) + ""))
1116 highlightAtom(atomIndex);
1119 redrawneeded = true;
1124 * Highlight the atom at the specified index.
1128 protected void highlightAtom(int atomIndex)
1132 for (index = 0; index < mainchain.bonds.size(); index++)
1134 tmpBond = mainchain.bonds.elementAt(index);
1135 if (tmpBond.at1.atomIndex == atomIndex)
1137 if (highlightBond1 != null)
1139 highlightBond1.at2.isSelected = false;
1142 if (highlightBond2 != null)
1144 highlightBond2.at1.isSelected = false;
1147 highlightBond1 = null;
1148 highlightBond2 = null;
1152 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1153 highlightBond1.at2.isSelected = true;
1156 if (index != mainchain.bonds.size())
1158 highlightBond2 = mainchain.bonds.elementAt(index);
1159 highlightBond2.at1.isSelected = true;
1167 public Color getColour(int atomIndex, int pdbResNum, String chain,
1171 // if (!pdbfile.equals(pdbentry.getFile()))
1174 // return new Color(viewer.getAtomArgb(atomIndex));
1177 public void updateColours(Object source)
1180 redrawneeded = true;
1185 public void releaseReferences(Object svl)
1187 // TODO Auto-generated method stub
1192 public boolean isListeningFor(SequenceI seq)
1194 if (sequence != null)
1196 for (SequenceI s : sequence)