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.schemes.Colour;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.ColorUtils;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Event;
41 import java.awt.Graphics;
42 import java.awt.Graphics2D;
43 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
44 import java.awt.Image;
45 import java.awt.RenderingHints;
46 import java.awt.event.KeyAdapter;
47 import java.awt.event.KeyEvent;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.io.PrintStream;
52 import java.util.List;
53 import java.util.Vector;
55 import javax.swing.JPanel;
56 import javax.swing.ToolTipManager;
58 public class PDBCanvas extends JPanel implements MouseListener,
59 MouseMotionListener, StructureListener
61 boolean redrawneeded = true;
71 public StructureFile pdb;
83 float[] centre = new float[3];
85 float[] width = new float[3];
95 boolean bysequence = true;
97 boolean depthcue = true;
101 boolean bymolecule = false;
103 boolean zbuffer = true;
119 Font font = new Font("Helvetica", Font.PLAIN, 10);
121 jalview.gui.SeqCanvas seqcanvas;
123 public SequenceI[] sequence;
125 final StringBuffer mappingDetails = new StringBuffer();
129 Vector<String> highlightRes;
131 boolean pdbAction = false;
133 boolean seqColoursReady = false;
135 jalview.renderer.seqfeatures.FeatureRenderer fr;
137 Color backgroundColour = Color.black;
141 StructureSelectionManager ssm;
145 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
146 AlignmentPanel ap, String protocol)
149 this.pdbentry = pdbentry;
152 ssm = ap.av.getStructureSelectionManager();
156 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
158 if (protocol.equals(jalview.io.AppletFormatAdapter.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.getSequenceAsString());
195 mappingDetails.append("\nNo of residues = "
196 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
198 // Now lets compare the sequences to get
199 // the start and end points.
200 // Align the sequence to the pdb
201 AlignSeq as = new AlignSeq(sequence,
202 pdb.getChains().elementAt(i).sequence, "pep");
203 as.calcScoreMatrix();
205 PrintStream ps = new PrintStream(System.out)
208 public void print(String x)
210 mappingDetails.append(x);
214 public void println()
216 mappingDetails.append("\n");
220 as.printAlignment(ps);
222 if (as.maxscore > max)
227 pdbstart = as.seq2start;
229 seqstart = as.seq1start + sequence.getStart() - 1;
230 seqend = as.seq1end + sequence.getEnd() - 1;
233 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
234 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
237 mainchain = pdb.getChains().elementAt(maxchain);
239 mainchain.pdbstart = pdbstart;
240 mainchain.pdbend = pdbend;
241 mainchain.seqstart = seqstart;
242 mainchain.seqend = seqend;
243 mainchain.isVisible = true;
246 this.prefsize = new Dimension(getSize().width, getSize().height);
248 addMouseMotionListener(this);
249 addMouseListener(this);
251 addKeyListener(new KeyAdapter()
254 public void keyPressed(KeyEvent evt)
267 ToolTipManager.sharedInstance().registerComponent(this);
268 ToolTipManager.sharedInstance().setInitialDelay(0);
269 ToolTipManager.sharedInstance().setDismissDelay(10000);
272 Vector<Bond> visiblebonds;
276 seqColoursReady = false;
277 // Sort the bonds by z coord
278 visiblebonds = new Vector<Bond>();
280 for (PDBChain chain : pdb.getChains())
284 for (Bond bond : chain.bonds)
286 visiblebonds.addElement(bond);
292 seqColoursReady = true;
297 public void findWidth()
299 float[] max = new float[3];
300 float[] min = new float[3];
302 max[0] = (float) -1e30;
303 max[1] = (float) -1e30;
304 max[2] = (float) -1e30;
306 min[0] = (float) 1e30;
307 min[1] = (float) 1e30;
308 min[2] = (float) 1e30;
310 for (PDBChain chain : pdb.getChains())
314 for (Bond tmp : chain.bonds)
316 if (tmp.start[0] >= max[0])
318 max[0] = tmp.start[0];
321 if (tmp.start[1] >= max[1])
323 max[1] = tmp.start[1];
326 if (tmp.start[2] >= max[2])
328 max[2] = tmp.start[2];
331 if (tmp.start[0] <= min[0])
333 min[0] = tmp.start[0];
336 if (tmp.start[1] <= min[1])
338 min[1] = tmp.start[1];
341 if (tmp.start[2] <= min[2])
343 min[2] = tmp.start[2];
346 if (tmp.end[0] >= max[0])
351 if (tmp.end[1] >= max[1])
356 if (tmp.end[2] >= max[2])
361 if (tmp.end[0] <= min[0])
366 if (tmp.end[1] <= min[1])
371 if (tmp.end[2] <= min[2])
379 * System.out.println("xmax " + max[0] + " min " + min[0]);
380 * System.out.println("ymax " + max[1] + " min " + min[1]);
381 * System.out.println("zmax " + max[2] + " min " + min[2]);
384 width[0] = Math.abs(max[0] - min[0]);
385 width[1] = Math.abs(max[1] - min[1]);
386 width[2] = Math.abs(max[2] - min[2]);
390 if (width[1] > width[0])
395 if (width[2] > width[1])
400 // System.out.println("Maxwidth = " + maxwidth);
403 public float findScale()
412 height = getHeight();
416 width = prefsize.width;
417 height = prefsize.height;
429 return (float) (dim / (1.5d * maxwidth));
432 public void findCentre()
440 // Find centre coordinate
441 for (PDBChain chain : pdb.getChains())
445 bsize += chain.bonds.size();
447 for (Bond bond : chain.bonds)
449 xtot = xtot + bond.start[0] + bond.end[0];
450 ytot = ytot + bond.start[1] + bond.end[1];
451 ztot = ztot + bond.start[2] + bond.end[2];
456 centre[0] = xtot / (2 * (float) bsize);
457 centre[1] = ytot / (2 * (float) bsize);
458 centre[2] = ztot / (2 * (float) bsize);
462 public void paintComponent(Graphics g)
464 super.paintComponent(g);
466 if (!seqColoursReady || errorMessage != null)
468 g.setColor(Color.black);
469 g.setFont(new Font("Verdana", Font.BOLD, 14));
470 g.drawString(errorMessage == null ? "Retrieving PDB data...."
471 : errorMessage, 20, getHeight() / 2);
475 // Only create the image at the beginning -
476 // this saves much memory usage
477 if ((img == null) || (prefsize.width != getWidth())
478 || (prefsize.height != getHeight()))
481 prefsize.width = getWidth();
482 prefsize.height = getHeight();
485 img = createImage(prefsize.width, prefsize.height);
486 ig = img.getGraphics();
487 Graphics2D ig2 = (Graphics2D) ig;
489 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
490 RenderingHints.VALUE_ANTIALIAS_ON);
497 drawAll(ig, prefsize.width, prefsize.height);
498 redrawneeded = false;
501 g.drawImage(img, 0, 0, this);
506 public void drawAll(Graphics g, int width, int height)
508 g.setColor(backgroundColour);
509 g.fillRect(0, 0, width, height);
514 public void updateSeqColours()
527 // This method has been taken out of PDBChain to allow
528 // Applet and Application specific sequence renderers to be used
529 void colourBySequence()
531 SequenceRenderer sr = new SequenceRenderer(ap.av);
533 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
535 boolean showFeatures = false;
536 if (ap.av.isShowSequenceFeatures())
540 fr = new FeatureRenderer(ap);
543 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
549 if (bysequence && pdb != null)
551 for (int ii = 0; ii < pdb.getChains().size(); ii++)
553 chain = pdb.getChains().elementAt(ii);
555 for (int i = 0; i < chain.bonds.size(); i++)
557 Bond tmp = chain.bonds.elementAt(i);
558 tmp.startCol = Colour.lightGray;
559 tmp.endCol = Colour.lightGray;
560 if (chain != mainchain)
565 for (int s = 0; s < sequence.length; s++)
567 for (int m = 0; m < mapping.length; m++)
569 if (mapping[m].getSequence() == sequence[s])
571 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
574 pos = sequence[s].findIndex(pos);
575 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
578 tmp.startCol = fr.findFeatureColour(tmp.startCol,
582 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
585 pos = sequence[s].findIndex(pos);
586 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
589 tmp.endCol = fr.findFeatureColour(tmp.endCol,
604 public void drawScene(Graphics g)
613 zsort.sort(visiblebonds);
617 for (int i = 0; i < visiblebonds.size(); i++)
619 tmpBond = visiblebonds.elementAt(i);
621 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
622 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
624 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
625 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
627 xmid = (xend + xstart) / 2;
628 ymid = (yend + ystart) / 2;
629 if (depthcue && !bymolecule)
631 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
634 g.setColor(ColorUtils.getColor(tmpBond.startCol).darker()
636 drawLine(g, xstart, ystart, xmid, ymid);
637 g.setColor(ColorUtils.getColor(tmpBond.endCol).darker().darker());
638 drawLine(g, xmid, ymid, xend, yend);
641 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
643 g.setColor(ColorUtils.getColor(tmpBond.startCol).darker());
644 drawLine(g, xstart, ystart, xmid, ymid);
646 g.setColor(ColorUtils.getColor(tmpBond.endCol).darker());
647 drawLine(g, xmid, ymid, xend, yend);
651 g.setColor(ColorUtils.getColor(tmpBond.startCol));
652 drawLine(g, xstart, ystart, xmid, ymid);
654 g.setColor(ColorUtils.getColor(tmpBond.endCol));
655 drawLine(g, xmid, ymid, xend, yend);
658 else if (depthcue && bymolecule)
660 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
662 g.setColor(Color.green.darker().darker());
663 drawLine(g, xstart, ystart, xend, yend);
665 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
667 g.setColor(Color.green.darker());
668 drawLine(g, xstart, ystart, xend, yend);
672 g.setColor(Color.green);
673 drawLine(g, xstart, ystart, xend, yend);
676 else if (!depthcue && !bymolecule)
678 g.setColor(ColorUtils.getColor(tmpBond.startCol));
679 drawLine(g, xstart, ystart, xmid, ymid);
680 g.setColor(ColorUtils.getColor(tmpBond.endCol));
681 drawLine(g, xmid, ymid, xend, yend);
685 drawLine(g, xstart, ystart, xend, yend);
688 if (highlightBond1 != null && highlightBond1 == tmpBond)
690 g.setColor(ColorUtils.getColor(tmpBond.endCol).brighter()
691 .brighter().brighter()
693 drawLine(g, xmid, ymid, xend, yend);
696 if (highlightBond2 != null && highlightBond2 == tmpBond)
698 g.setColor(ColorUtils.getColor(tmpBond.startCol).brighter()
699 .brighter().brighter()
701 drawLine(g, xstart, ystart, xmid, ymid);
708 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
712 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
714 g.drawLine(x1, y1, x2, y2);
715 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
716 g.drawLine(x1, y1 - 1, x2, y2 - 1);
720 g.setColor(g.getColor().brighter());
721 g.drawLine(x1, y1, x2, y2);
722 g.drawLine(x1 + 1, y1, x2 + 1, y2);
723 g.drawLine(x1 - 1, y1, x2 - 1, y2);
728 g.drawLine(x1, y1, x2, y2);
732 public Dimension minimumsize()
737 public Dimension preferredsize()
742 public void keyPressed(KeyEvent evt)
744 if (evt.getKeyCode() == KeyEvent.VK_UP)
746 scale = (float) (scale * 1.1);
750 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
752 scale = (float) (scale * 0.9);
759 public void mousePressed(MouseEvent e)
762 Atom fatom = findAtom(e.getX(), e.getY());
765 fatom.isSelected = !fatom.isSelected;
769 if (foundchain != -1)
771 PDBChain chain = pdb.getChains().elementAt(foundchain);
772 if (chain == mainchain)
774 if (fatom.alignmentMapping != -1)
776 if (highlightRes == null)
778 highlightRes = new Vector<String>();
781 final String atomString = Integer
782 .toString(fatom.alignmentMapping);
783 if (highlightRes.contains(atomString))
785 highlightRes.remove(atomString);
789 highlightRes.add(atomString);
804 public void mouseMoved(MouseEvent e)
807 if (highlightBond1 != null)
809 highlightBond1.at2.isSelected = false;
810 highlightBond2.at1.isSelected = false;
811 highlightBond1 = null;
812 highlightBond2 = null;
815 Atom fatom = findAtom(e.getX(), e.getY());
817 PDBChain chain = null;
818 if (foundchain != -1)
820 chain = pdb.getChains().elementAt(foundchain);
821 if (chain == mainchain)
823 mouseOverStructure(fatom.resNumber, chain.id);
829 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
834 mouseOverStructure(-1, chain != null ? chain.id : null);
835 this.setToolTipText(null);
840 public void mouseClicked(MouseEvent e)
845 public void mouseEntered(MouseEvent e)
850 public void mouseExited(MouseEvent e)
855 public void mouseDragged(MouseEvent evt)
862 MCMatrix objmat = new MCMatrix(3, 3);
863 objmat.setIdentity();
865 if ((evt.getModifiers() & Event.META_MASK) != 0)
867 objmat.rotatez(((mx - omx)));
871 objmat.rotatex(((my - omy)));
872 objmat.rotatey(((omx - mx)));
876 for (PDBChain chain : pdb.getChains())
878 for (Bond tmpBond : chain.bonds)
880 // Translate the bond so the centre is 0,0,0
881 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
883 // Now apply the rotation matrix
884 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
885 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
887 // Now translate back again
888 tmpBond.translate(centre[0], centre[1], centre[2]);
905 public void mouseReleased(MouseEvent evt)
911 void drawLabels(Graphics g)
914 for (PDBChain chain : pdb.getChains())
918 for (Bond tmpBond : chain.bonds)
920 if (tmpBond.at1.isSelected)
922 labelAtom(g, tmpBond, 1);
925 if (tmpBond.at2.isSelected)
927 labelAtom(g, tmpBond, 2);
934 public void labelAtom(Graphics g, Bond b, int n)
937 g.setColor(Color.red);
940 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
941 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
943 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
948 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
949 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
951 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
957 public Atom findAtom(int x, int y)
963 for (int ii = 0; ii < pdb.getChains().size(); ii++)
965 PDBChain chain = pdb.getChains().elementAt(ii);
971 for (Bond bond : chain.bonds)
975 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
977 if (Math.abs(truex - x) <= 2)
979 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
981 if (Math.abs(truey - y) <= 2)
990 // Still here? Maybe its the last bond
992 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
994 if (Math.abs(truex - x) <= 2)
996 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
998 if (Math.abs(truey - y) <= 2)
1000 fatom = tmpBond.at2;
1008 if (fatom != null) // )&& chain.ds != null)
1009 { // dead code? value of chain is either overwritten or discarded
1010 chain = pdb.getChains().elementAt(foundchain);
1017 Bond highlightBond1, highlightBond2;
1019 public void highlightRes(int ii)
1021 if (!seqColoursReady)
1026 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1033 for (index = 0; index < mainchain.bonds.size(); index++)
1035 tmpBond = mainchain.bonds.elementAt(index);
1036 if (tmpBond.at1.alignmentMapping == ii - 1)
1038 if (highlightBond1 != null)
1040 highlightBond1.at2.isSelected = false;
1043 if (highlightBond2 != null)
1045 highlightBond2.at1.isSelected = false;
1048 highlightBond1 = null;
1049 highlightBond2 = null;
1053 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1054 highlightBond1.at2.isSelected = true;
1057 if (index != mainchain.bonds.size())
1059 highlightBond2 = mainchain.bonds.elementAt(index);
1060 highlightBond2.at1.isSelected = true;
1067 redrawneeded = true;
1071 public void setAllchainsVisible(boolean b)
1073 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1075 PDBChain chain = pdb.getChains().elementAt(ii);
1076 chain.isVisible = b;
1078 mainchain.isVisible = true;
1083 // ////////////////////////////////
1084 // /StructureListener
1086 public String[] getPdbFile()
1088 return new String[] { pdbentry.getFile() };
1093 public void mouseOverStructure(int pdbResNum, String chain)
1095 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1097 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1100 lastMessage = pdbResNum + chain;
1103 StringBuffer resetLastRes = new StringBuffer();
1105 StringBuffer eval = new StringBuffer();
1108 * Highlight the specified atoms in the structure.
1113 public void highlightAtoms(List<AtomSpec> atoms)
1115 if (!seqColoursReady)
1120 for (AtomSpec atom : atoms)
1122 int atomIndex = atom.getAtomIndex();
1123 if (highlightRes != null
1124 && highlightRes.contains((atomIndex - 1) + ""))
1129 highlightAtom(atomIndex);
1132 redrawneeded = true;
1137 * Highlight the atom at the specified index.
1141 protected void highlightAtom(int atomIndex)
1145 for (index = 0; index < mainchain.bonds.size(); index++)
1147 tmpBond = mainchain.bonds.elementAt(index);
1148 if (tmpBond.at1.atomIndex == atomIndex)
1150 if (highlightBond1 != null)
1152 highlightBond1.at2.isSelected = false;
1155 if (highlightBond2 != null)
1157 highlightBond2.at1.isSelected = false;
1160 highlightBond1 = null;
1161 highlightBond2 = null;
1165 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1166 highlightBond1.at2.isSelected = true;
1169 if (index != mainchain.bonds.size())
1171 highlightBond2 = mainchain.bonds.elementAt(index);
1172 highlightBond2.at1.isSelected = true;
1180 public Color getColour(int atomIndex, int pdbResNum, String chain,
1184 // if (!pdbfile.equals(pdbentry.getFile()))
1187 // return new Color(viewer.getAtomArgb(atomIndex));
1191 public void updateColours(Object source)
1194 redrawneeded = true;
1199 public void releaseReferences(Object svl)
1201 // TODO Auto-generated method stub
1206 public boolean isListeningFor(SequenceI seq)
1208 if (sequence != null)
1210 for (SequenceI s : sequence)