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.renderer.seqfeatures.FeatureColourFinder;
32 import jalview.structure.AtomSpec;
33 import jalview.structure.StructureListener;
34 import jalview.structure.StructureMapping;
35 import jalview.structure.StructureSelectionManager;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.event.InputEvent;import java.awt.Font;
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
58 implements MouseListener, 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,
158 if (protocol.equals(jalview.io.DataSourceType.PASTE))
160 pdbentry.setFile("INLINE" + pdb.getId());
163 } catch (Exception ex)
165 ex.printStackTrace();
171 errorMessage = "Error loading file: " + pdbentry.getId();
174 pdbentry.setId(pdb.getId());
176 ssm.addStructureViewerListener(this);
187 // JUST DEAL WITH ONE SEQUENCE FOR NOW
188 SequenceI sequence = seq[0];
190 for (int i = 0; i < pdb.getChains().size(); i++)
193 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
194 + pdb.getChains().elementAt(i).sequence
195 .getSequenceAsString());
196 mappingDetails.append("\nNo of residues = "
197 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
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.getChains().elementAt(i).sequence, "pep");
204 as.calcScoreMatrix();
206 PrintStream ps = new PrintStream(System.out)
209 public void print(String x)
211 mappingDetails.append(x);
215 public void println()
217 mappingDetails.append("\n");
221 as.printAlignment(ps);
223 if (as.maxscore > max)
228 pdbstart = as.seq2start;
230 seqstart = as.seq1start + sequence.getStart() - 1;
231 seqend = as.seq1end + sequence.getEnd() - 1;
234 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
235 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
238 mainchain = pdb.getChains().elementAt(maxchain);
240 mainchain.pdbstart = pdbstart;
241 mainchain.pdbend = pdbend;
242 mainchain.seqstart = seqstart;
243 mainchain.seqend = seqend;
244 mainchain.isVisible = true;
247 this.prefsize = new Dimension(getSize().width, getSize().height);
249 addMouseMotionListener(this);
250 addMouseListener(this);
252 addKeyListener(new KeyAdapter()
255 public void keyPressed(KeyEvent evt)
268 ToolTipManager.sharedInstance().registerComponent(this);
269 ToolTipManager.sharedInstance().setInitialDelay(0);
270 ToolTipManager.sharedInstance().setDismissDelay(10000);
273 Vector<Bond> visiblebonds;
277 seqColoursReady = false;
278 // Sort the bonds by z coord
279 visiblebonds = new Vector<Bond>();
281 for (PDBChain chain : pdb.getChains())
285 for (Bond bond : chain.bonds)
287 visiblebonds.addElement(bond);
293 seqColoursReady = true;
298 public void findWidth()
300 float[] max = new float[3];
301 float[] min = new float[3];
303 max[0] = (float) -1e30;
304 max[1] = (float) -1e30;
305 max[2] = (float) -1e30;
307 min[0] = (float) 1e30;
308 min[1] = (float) 1e30;
309 min[2] = (float) 1e30;
311 for (PDBChain chain : pdb.getChains())
315 for (Bond tmp : chain.bonds)
317 if (tmp.start[0] >= max[0])
319 max[0] = tmp.start[0];
322 if (tmp.start[1] >= max[1])
324 max[1] = tmp.start[1];
327 if (tmp.start[2] >= max[2])
329 max[2] = tmp.start[2];
332 if (tmp.start[0] <= min[0])
334 min[0] = tmp.start[0];
337 if (tmp.start[1] <= min[1])
339 min[1] = tmp.start[1];
342 if (tmp.start[2] <= min[2])
344 min[2] = tmp.start[2];
347 if (tmp.end[0] >= max[0])
352 if (tmp.end[1] >= max[1])
357 if (tmp.end[2] >= max[2])
362 if (tmp.end[0] <= min[0])
367 if (tmp.end[1] <= min[1])
372 if (tmp.end[2] <= min[2])
380 * System.out.println("xmax " + max[0] + " min " + min[0]);
381 * System.out.println("ymax " + max[1] + " min " + min[1]);
382 * System.out.println("zmax " + max[2] + " min " + min[2]);
385 width[0] = Math.abs(max[0] - min[0]);
386 width[1] = Math.abs(max[1] - min[1]);
387 width[2] = Math.abs(max[2] - min[2]);
391 if (width[1] > width[0])
396 if (width[2] > width[1])
401 // System.out.println("Maxwidth = " + maxwidth);
404 public float findScale()
413 height = getHeight();
417 width = prefsize.width;
418 height = prefsize.height;
430 return (float) (dim / (1.5d * maxwidth));
433 public void findCentre()
441 // Find centre coordinate
442 for (PDBChain chain : pdb.getChains())
446 bsize += chain.bonds.size();
448 for (Bond bond : chain.bonds)
450 xtot = xtot + bond.start[0] + bond.end[0];
451 ytot = ytot + bond.start[1] + bond.end[1];
452 ztot = ztot + bond.start[2] + bond.end[2];
457 centre[0] = xtot / (2 * (float) bsize);
458 centre[1] = ytot / (2 * (float) bsize);
459 centre[2] = ztot / (2 * (float) bsize);
463 public void paintComponent(Graphics g)
465 super.paintComponent(g);
467 if (!seqColoursReady || errorMessage != null)
469 g.setColor(Color.black);
470 g.setFont(new Font("Verdana", Font.BOLD, 14));
471 g.drawString(errorMessage == null ? "Retrieving PDB data...."
472 : errorMessage, 20, getHeight() / 2);
476 // Only create the image at the beginning -
477 // this saves much memory usage
478 if ((img == null) || (prefsize.width != getWidth())
479 || (prefsize.height != getHeight()))
482 prefsize.width = getWidth();
483 prefsize.height = getHeight();
486 img = createImage(prefsize.width, prefsize.height);
487 ig = img.getGraphics();
488 Graphics2D ig2 = (Graphics2D) ig;
490 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
491 RenderingHints.VALUE_ANTIALIAS_ON);
498 drawAll(ig, prefsize.width, prefsize.height);
499 redrawneeded = false;
502 g.drawImage(img, 0, 0, this);
507 public void drawAll(Graphics g, int width, int height)
509 g.setColor(backgroundColour);
510 g.fillRect(0, 0, width, height);
515 public void updateSeqColours()
528 // This method has been taken out of PDBChain to allow
529 // Applet and Application specific sequence renderers to be used
530 void colourBySequence()
532 SequenceRenderer sr = new SequenceRenderer(ap.av);
534 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
536 boolean showFeatures = false;
537 if (ap.av.isShowSequenceFeatures())
541 fr = new FeatureRenderer(ap);
544 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
549 FeatureColourFinder finder = new FeatureColourFinder(fr);
551 if (bysequence && pdb != null)
553 for (int ii = 0; ii < pdb.getChains().size(); ii++)
555 chain = pdb.getChains().elementAt(ii);
557 for (int i = 0; i < chain.bonds.size(); i++)
559 Bond tmp = chain.bonds.elementAt(i);
560 tmp.startCol = Color.lightGray;
561 tmp.endCol = Color.lightGray;
562 if (chain != mainchain)
567 for (int s = 0; s < sequence.length; s++)
569 for (int m = 0; m < mapping.length; m++)
571 if (mapping[m].getSequence() == sequence[s])
573 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
576 pos = sequence[s].findIndex(pos);
577 tmp.startCol = sr.getResidueColour(sequence[s], pos,
580 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
583 pos = sequence[s].findIndex(pos);
584 tmp.endCol = sr.getResidueColour(sequence[s], pos,
598 public void drawScene(Graphics g)
607 zsort.sort(visiblebonds);
611 for (int i = 0; i < visiblebonds.size(); i++)
613 tmpBond = visiblebonds.elementAt(i);
615 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale)
617 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale)
618 + (getHeight() / 2));
620 xend = (int) (((tmpBond.end[0] - centre[0]) * scale)
622 yend = (int) (((centre[1] - tmpBond.end[1]) * scale)
623 + (getHeight() / 2));
625 xmid = (xend + xstart) / 2;
626 ymid = (yend + ystart) / 2;
627 if (depthcue && !bymolecule)
629 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
632 g.setColor(tmpBond.startCol.darker().darker());
633 drawLine(g, xstart, ystart, xmid, ymid);
634 g.setColor(tmpBond.endCol.darker().darker());
635 drawLine(g, xmid, ymid, xend, yend);
638 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
640 g.setColor(tmpBond.startCol.darker());
641 drawLine(g, xstart, ystart, xmid, ymid);
643 g.setColor(tmpBond.endCol.darker());
644 drawLine(g, xmid, ymid, xend, yend);
648 g.setColor(tmpBond.startCol);
649 drawLine(g, xstart, ystart, xmid, ymid);
651 g.setColor(tmpBond.endCol);
652 drawLine(g, xmid, ymid, xend, yend);
655 else if (depthcue && bymolecule)
657 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
659 g.setColor(Color.green.darker().darker());
660 drawLine(g, xstart, ystart, xend, yend);
662 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
664 g.setColor(Color.green.darker());
665 drawLine(g, xstart, ystart, xend, yend);
669 g.setColor(Color.green);
670 drawLine(g, xstart, ystart, xend, yend);
673 else if (!depthcue && !bymolecule)
675 g.setColor(tmpBond.startCol);
676 drawLine(g, xstart, ystart, xmid, ymid);
677 g.setColor(tmpBond.endCol);
678 drawLine(g, xmid, ymid, xend, yend);
682 drawLine(g, xstart, ystart, xend, yend);
685 if (highlightBond1 != null && highlightBond1 == tmpBond)
688 tmpBond.endCol.brighter().brighter().brighter().brighter());
689 drawLine(g, xmid, ymid, xend, yend);
692 if (highlightBond2 != null && highlightBond2 == tmpBond)
694 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
696 drawLine(g, xstart, ystart, xmid, ymid);
703 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
707 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
709 g.drawLine(x1, y1, x2, y2);
710 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
711 g.drawLine(x1, y1 - 1, x2, y2 - 1);
715 g.setColor(g.getColor().brighter());
716 g.drawLine(x1, y1, x2, y2);
717 g.drawLine(x1 + 1, y1, x2 + 1, y2);
718 g.drawLine(x1 - 1, y1, x2 - 1, y2);
723 g.drawLine(x1, y1, x2, y2);
727 public Dimension minimumsize()
732 public Dimension preferredsize()
737 public void keyPressed(KeyEvent evt)
739 if (evt.getKeyCode() == KeyEvent.VK_UP)
741 scale = (float) (scale * 1.1);
745 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
747 scale = (float) (scale * 0.9);
754 public void mousePressed(MouseEvent e)
757 Atom fatom = findAtom(e.getX(), e.getY());
760 fatom.isSelected = !fatom.isSelected;
764 if (foundchain != -1)
766 PDBChain chain = pdb.getChains().elementAt(foundchain);
767 if (chain == mainchain)
769 if (fatom.alignmentMapping != -1)
771 if (highlightRes == null)
773 highlightRes = new Vector<String>();
776 final String atomString = Integer
777 .toString(fatom.alignmentMapping);
778 if (highlightRes.contains(atomString))
780 highlightRes.remove(atomString);
784 highlightRes.add(atomString);
799 public void mouseMoved(MouseEvent e)
802 if (highlightBond1 != null)
804 highlightBond1.at2.isSelected = false;
805 highlightBond2.at1.isSelected = false;
806 highlightBond1 = null;
807 highlightBond2 = null;
810 Atom fatom = findAtom(e.getX(), e.getY());
812 PDBChain chain = null;
813 if (foundchain != -1)
815 chain = pdb.getChains().elementAt(foundchain);
816 if (chain == mainchain)
818 mouseOverStructure(fatom.resNumber, chain.id);
825 chain.id + ":" + fatom.resNumber + " " + fatom.resName);
829 mouseOverStructure(-1, chain != null ? chain.id : null);
830 this.setToolTipText(null);
835 public void mouseClicked(MouseEvent e)
840 public void mouseEntered(MouseEvent e)
845 public void mouseExited(MouseEvent e)
850 public void mouseDragged(MouseEvent evt)
857 MCMatrix objmat = new MCMatrix(3, 3);
858 objmat.setIdentity();
860 if ((evt.getModifiersEx() & InputEvent.META_DOWN_MASK) != 0)
862 objmat.rotatez(((mx - omx)));
866 objmat.rotatex(((my - omy)));
867 objmat.rotatey(((omx - mx)));
871 for (PDBChain chain : pdb.getChains())
873 for (Bond tmpBond : chain.bonds)
875 // Translate the bond so the centre is 0,0,0
876 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
878 // Now apply the rotation matrix
879 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
880 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
882 // Now translate back again
883 tmpBond.translate(centre[0], centre[1], centre[2]);
900 public void mouseReleased(MouseEvent evt)
906 void drawLabels(Graphics g)
909 for (PDBChain chain : pdb.getChains())
913 for (Bond tmpBond : chain.bonds)
915 if (tmpBond.at1.isSelected)
917 labelAtom(g, tmpBond, 1);
920 if (tmpBond.at2.isSelected)
922 labelAtom(g, tmpBond, 2);
929 public void labelAtom(Graphics g, Bond b, int n)
932 g.setColor(Color.red);
935 int xstart = (int) (((b.start[0] - centre[0]) * scale)
937 int ystart = (int) (((centre[1] - b.start[1]) * scale)
938 + (getHeight() / 2));
940 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
945 int xstart = (int) (((b.end[0] - centre[0]) * scale)
947 int ystart = (int) (((centre[1] - b.end[1]) * scale)
948 + (getHeight() / 2));
950 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
956 public Atom findAtom(int x, int y)
962 for (int ii = 0; ii < pdb.getChains().size(); ii++)
964 PDBChain chain = pdb.getChains().elementAt(ii);
970 for (Bond bond : chain.bonds)
974 truex = (int) (((tmpBond.start[0] - centre[0]) * scale)
977 if (Math.abs(truex - x) <= 2)
979 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale)
980 + (getHeight() / 2));
982 if (Math.abs(truey - y) <= 2)
991 // Still here? Maybe its the last bond
993 truex = (int) (((tmpBond.end[0] - centre[0]) * scale)
996 if (Math.abs(truex - x) <= 2)
998 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale)
999 + (getHeight() / 2));
1001 if (Math.abs(truey - y) <= 2)
1003 fatom = tmpBond.at2;
1011 if (fatom != null) // )&& chain.ds != null)
1012 { // dead code? value of chain is either overwritten or discarded
1013 chain = pdb.getChains().elementAt(foundchain);
1020 Bond highlightBond1, highlightBond2;
1022 public void highlightRes(int ii)
1024 if (!seqColoursReady)
1029 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1036 for (index = 0; index < mainchain.bonds.size(); index++)
1038 tmpBond = mainchain.bonds.elementAt(index);
1039 if (tmpBond.at1.alignmentMapping == ii - 1)
1041 if (highlightBond1 != null)
1043 highlightBond1.at2.isSelected = false;
1046 if (highlightBond2 != null)
1048 highlightBond2.at1.isSelected = false;
1051 highlightBond1 = null;
1052 highlightBond2 = null;
1056 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1057 highlightBond1.at2.isSelected = true;
1060 if (index != mainchain.bonds.size())
1062 highlightBond2 = mainchain.bonds.elementAt(index);
1063 highlightBond2.at1.isSelected = true;
1070 redrawneeded = true;
1074 public void setAllchainsVisible(boolean b)
1076 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1078 PDBChain chain = pdb.getChains().elementAt(ii);
1079 chain.isVisible = b;
1081 mainchain.isVisible = true;
1086 // ////////////////////////////////
1087 // /StructureListener
1089 public String[] getStructureFiles()
1091 return new String[] { pdbentry.getFile() };
1096 public void mouseOverStructure(int pdbResNum, String chain)
1098 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1100 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1103 lastMessage = pdbResNum + chain;
1106 StringBuffer resetLastRes = new StringBuffer();
1108 StringBuffer eval = new StringBuffer();
1111 * Highlight the specified atoms in the structure.
1116 public void highlightAtoms(List<AtomSpec> atoms)
1118 if (!seqColoursReady)
1123 for (AtomSpec atom : atoms)
1125 int atomIndex = atom.getAtomIndex();
1126 if (highlightRes != null
1127 && highlightRes.contains((atomIndex - 1) + ""))
1132 highlightAtom(atomIndex);
1135 redrawneeded = true;
1140 * Highlight the atom at the specified index.
1144 protected void highlightAtom(int atomIndex)
1148 for (index = 0; index < mainchain.bonds.size(); index++)
1150 tmpBond = mainchain.bonds.elementAt(index);
1151 if (tmpBond.at1.atomIndex == atomIndex)
1153 if (highlightBond1 != null)
1155 highlightBond1.at2.isSelected = false;
1158 if (highlightBond2 != null)
1160 highlightBond2.at1.isSelected = false;
1163 highlightBond1 = null;
1164 highlightBond2 = null;
1168 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1169 highlightBond1.at2.isSelected = true;
1172 if (index != mainchain.bonds.size())
1174 highlightBond2 = mainchain.bonds.elementAt(index);
1175 highlightBond2.at1.isSelected = true;
1183 public Color getColour(int atomIndex, int pdbResNum, String chain,
1187 // if (!pdbfile.equals(pdbentry.getFile()))
1190 // return new Color(viewer.getAtomArgb(atomIndex));
1194 public void updateColours(Object source)
1197 redrawneeded = true;
1202 public void releaseReferences(Object svl)
1204 // TODO Auto-generated method stub
1209 public boolean isListeningFor(SequenceI seq)
1211 if (sequence != null)
1213 for (SequenceI s : sequence)