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.StructureFile;
30 import jalview.structure.AtomSpec;
31 import jalview.structure.StructureListener;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureSelectionManager;
35 import java.awt.Color;
36 import java.awt.Dimension;
37 import java.awt.Event;
39 import java.awt.Graphics;
40 import java.awt.Graphics2D;
41 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
42 import java.awt.Image;
43 import java.awt.RenderingHints;
44 import java.awt.event.KeyAdapter;
45 import java.awt.event.KeyEvent;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
49 import java.io.PrintStream;
50 import java.util.List;
51 import java.util.Vector;
53 import javax.swing.JPanel;
54 import javax.swing.ToolTipManager;
56 public class PDBCanvas extends JPanel implements MouseListener,
57 MouseMotionListener, StructureListener
59 boolean redrawneeded = true;
69 public StructureFile pdb;
81 float[] centre = new float[3];
83 float[] width = new float[3];
93 boolean bysequence = true;
95 boolean depthcue = true;
99 boolean bymolecule = false;
101 boolean zbuffer = true;
117 Font font = new Font("Helvetica", Font.PLAIN, 10);
119 jalview.gui.SeqCanvas seqcanvas;
121 public SequenceI[] sequence;
123 final StringBuffer mappingDetails = new StringBuffer();
127 Vector<String> highlightRes;
129 boolean pdbAction = false;
131 boolean seqColoursReady = false;
133 jalview.renderer.seqfeatures.FeatureRenderer fr;
135 Color backgroundColour = Color.black;
139 StructureSelectionManager ssm;
143 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
144 AlignmentPanel ap, String protocol)
147 this.pdbentry = pdbentry;
150 ssm = ap.av.getStructureSelectionManager();
154 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
156 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
158 pdbentry.setFile("INLINE" + pdb.getId());
161 } catch (Exception ex)
163 ex.printStackTrace();
169 errorMessage = "Error loading file: " + pdbentry.getId();
172 pdbentry.setId(pdb.getId());
174 ssm.addStructureViewerListener(this);
185 // JUST DEAL WITH ONE SEQUENCE FOR NOW
186 SequenceI sequence = seq[0];
188 for (int i = 0; i < pdb.getChains().size(); i++)
191 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
192 + pdb.getChains().elementAt(i).sequence.getSequenceAsString());
193 mappingDetails.append("\nNo of residues = "
194 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
196 // Now lets compare the sequences to get
197 // the start and end points.
198 // Align the sequence to the pdb
199 AlignSeq as = new AlignSeq(sequence,
200 pdb.getChains().elementAt(i).sequence, "pep");
201 as.calcScoreMatrix();
203 PrintStream ps = new PrintStream(System.out)
206 public void print(String x)
208 mappingDetails.append(x);
212 public void println()
214 mappingDetails.append("\n");
218 as.printAlignment(ps);
220 if (as.maxscore > max)
225 pdbstart = as.seq2start;
227 seqstart = as.seq1start + sequence.getStart() - 1;
228 seqend = as.seq1end + sequence.getEnd() - 1;
231 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
232 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
235 mainchain = pdb.getChains().elementAt(maxchain);
237 mainchain.pdbstart = pdbstart;
238 mainchain.pdbend = pdbend;
239 mainchain.seqstart = seqstart;
240 mainchain.seqend = seqend;
241 mainchain.isVisible = true;
244 this.prefsize = new Dimension(getSize().width, getSize().height);
246 addMouseMotionListener(this);
247 addMouseListener(this);
249 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.getChains())
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.getChains())
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.getChains())
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);
460 public void paintComponent(Graphics g)
462 super.paintComponent(g);
464 if (!seqColoursReady || errorMessage != null)
466 g.setColor(Color.black);
467 g.setFont(new Font("Verdana", Font.BOLD, 14));
468 g.drawString(errorMessage == null ? "Retrieving PDB data...."
469 : errorMessage, 20, getHeight() / 2);
473 // Only create the image at the beginning -
474 // this saves much memory usage
475 if ((img == null) || (prefsize.width != getWidth())
476 || (prefsize.height != getHeight()))
479 prefsize.width = getWidth();
480 prefsize.height = getHeight();
483 img = createImage(prefsize.width, prefsize.height);
484 ig = img.getGraphics();
485 Graphics2D ig2 = (Graphics2D) ig;
487 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
488 RenderingHints.VALUE_ANTIALIAS_ON);
495 drawAll(ig, prefsize.width, prefsize.height);
496 redrawneeded = false;
499 g.drawImage(img, 0, 0, this);
504 public void drawAll(Graphics g, int width, int height)
506 g.setColor(backgroundColour);
507 g.fillRect(0, 0, width, height);
512 public void updateSeqColours()
525 // This method has been taken out of PDBChain to allow
526 // Applet and Application specific sequence renderers to be used
527 void colourBySequence()
529 SequenceRenderer sr = new SequenceRenderer(ap.av);
531 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
533 boolean showFeatures = false;
534 if (ap.av.isShowSequenceFeatures())
538 fr = new FeatureRenderer(ap);
541 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
547 if (bysequence && pdb != null)
549 for (int ii = 0; ii < pdb.getChains().size(); ii++)
551 chain = pdb.getChains().elementAt(ii);
553 for (int i = 0; i < chain.bonds.size(); i++)
555 Bond tmp = chain.bonds.elementAt(i);
556 tmp.startCol = Color.lightGray;
557 tmp.endCol = Color.lightGray;
558 if (chain != mainchain)
563 for (int s = 0; s < sequence.length; s++)
565 for (int m = 0; m < mapping.length; m++)
567 if (mapping[m].getSequence() == sequence[s])
569 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
572 pos = sequence[s].findIndex(pos);
573 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
576 tmp.startCol = fr.findFeatureColour(tmp.startCol,
580 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
583 pos = sequence[s].findIndex(pos);
584 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
587 tmp.endCol = fr.findFeatureColour(tmp.endCol,
602 public void drawScene(Graphics g)
611 zsort.sort(visiblebonds);
615 for (int i = 0; i < visiblebonds.size(); i++)
617 tmpBond = visiblebonds.elementAt(i);
619 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
620 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
622 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
623 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (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)
687 g.setColor(tmpBond.endCol.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);
824 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
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.getModifiers() & Event.META_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) + (getWidth() / 2));
936 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
938 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
943 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
944 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
946 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
952 public Atom findAtom(int x, int y)
958 for (int ii = 0; ii < pdb.getChains().size(); ii++)
960 PDBChain chain = pdb.getChains().elementAt(ii);
966 for (Bond bond : chain.bonds)
970 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
972 if (Math.abs(truex - x) <= 2)
974 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
976 if (Math.abs(truey - y) <= 2)
985 // Still here? Maybe its the last bond
987 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
989 if (Math.abs(truex - x) <= 2)
991 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
993 if (Math.abs(truey - y) <= 2)
1003 if (fatom != null) // )&& chain.ds != null)
1004 { // dead code? value of chain is either overwritten or discarded
1005 chain = pdb.getChains().elementAt(foundchain);
1012 Bond highlightBond1, highlightBond2;
1014 public void highlightRes(int ii)
1016 if (!seqColoursReady)
1021 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1028 for (index = 0; index < mainchain.bonds.size(); index++)
1030 tmpBond = mainchain.bonds.elementAt(index);
1031 if (tmpBond.at1.alignmentMapping == ii - 1)
1033 if (highlightBond1 != null)
1035 highlightBond1.at2.isSelected = false;
1038 if (highlightBond2 != null)
1040 highlightBond2.at1.isSelected = false;
1043 highlightBond1 = null;
1044 highlightBond2 = null;
1048 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1049 highlightBond1.at2.isSelected = true;
1052 if (index != mainchain.bonds.size())
1054 highlightBond2 = mainchain.bonds.elementAt(index);
1055 highlightBond2.at1.isSelected = true;
1062 redrawneeded = true;
1066 public void setAllchainsVisible(boolean b)
1068 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1070 PDBChain chain = pdb.getChains().elementAt(ii);
1071 chain.isVisible = b;
1073 mainchain.isVisible = true;
1078 // ////////////////////////////////
1079 // /StructureListener
1081 public String[] getPdbFile()
1083 return new String[] { pdbentry.getFile() };
1088 public void mouseOverStructure(int pdbResNum, String chain)
1090 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1092 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1095 lastMessage = pdbResNum + chain;
1098 StringBuffer resetLastRes = new StringBuffer();
1100 StringBuffer eval = new StringBuffer();
1103 * Highlight the specified atoms in the structure.
1108 public void highlightAtoms(List<AtomSpec> atoms)
1110 if (!seqColoursReady)
1115 for (AtomSpec atom : atoms)
1117 int atomIndex = atom.getAtomIndex();
1118 if (highlightRes != null
1119 && highlightRes.contains((atomIndex - 1) + ""))
1124 highlightAtom(atomIndex);
1127 redrawneeded = true;
1132 * Highlight the atom at the specified index.
1136 protected void highlightAtom(int atomIndex)
1140 for (index = 0; index < mainchain.bonds.size(); index++)
1142 tmpBond = mainchain.bonds.elementAt(index);
1143 if (tmpBond.at1.atomIndex == atomIndex)
1145 if (highlightBond1 != null)
1147 highlightBond1.at2.isSelected = false;
1150 if (highlightBond2 != null)
1152 highlightBond2.at1.isSelected = false;
1155 highlightBond1 = null;
1156 highlightBond2 = null;
1160 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1161 highlightBond1.at2.isSelected = true;
1164 if (index != mainchain.bonds.size())
1166 highlightBond2 = mainchain.bonds.elementAt(index);
1167 highlightBond2.at1.isSelected = true;
1175 public Color getColour(int atomIndex, int pdbResNum, String chain,
1179 // if (!pdbfile.equals(pdbentry.getFile()))
1182 // return new Color(viewer.getAtomArgb(atomIndex));
1186 public void updateColours(Object source)
1189 redrawneeded = true;
1194 public void releaseReferences(Object svl)
1196 // TODO Auto-generated method stub
1201 public boolean isListeningFor(SequenceI seq)
1203 if (sequence != null)
1205 for (SequenceI s : sequence)