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;
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, DataSourceType protocol)
149 this.pdbentry = pdbentry;
152 ssm = ap.av.getStructureSelectionManager();
156 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol,
159 if (protocol.equals(jalview.io.DataSourceType.PASTE))
161 pdbentry.setFile("INLINE" + pdb.getId());
164 } catch (Exception ex)
166 ex.printStackTrace();
172 errorMessage = "Error loading file: " + pdbentry.getId();
175 pdbentry.setId(pdb.getId());
177 ssm.addStructureViewerListener(this);
188 // JUST DEAL WITH ONE SEQUENCE FOR NOW
189 SequenceI sequence = seq[0];
191 for (int i = 0; i < pdb.getChains().size(); i++)
195 .append("\n\nPDB Sequence is :\nSequence = "
196 + pdb.getChains().elementAt(i).sequence
197 .getSequenceAsString());
198 mappingDetails.append("\nNo of residues = "
199 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
201 // Now lets compare the sequences to get
202 // the start and end points.
203 // Align the sequence to the pdb
204 AlignSeq as = new AlignSeq(sequence,
205 pdb.getChains().elementAt(i).sequence, "pep");
206 as.calcScoreMatrix();
208 PrintStream ps = new PrintStream(System.out)
211 public void print(String x)
213 mappingDetails.append(x);
217 public void println()
219 mappingDetails.append("\n");
223 as.printAlignment(ps);
225 if (as.maxscore > max)
230 pdbstart = as.seq2start;
232 seqstart = as.seq1start + sequence.getStart() - 1;
233 seqend = as.seq1end + sequence.getEnd() - 1;
236 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
237 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
240 mainchain = pdb.getChains().elementAt(maxchain);
242 mainchain.pdbstart = pdbstart;
243 mainchain.pdbend = pdbend;
244 mainchain.seqstart = seqstart;
245 mainchain.seqend = seqend;
246 mainchain.isVisible = true;
249 this.prefsize = new Dimension(getSize().width, getSize().height);
251 addMouseMotionListener(this);
252 addMouseListener(this);
254 addKeyListener(new KeyAdapter()
257 public void keyPressed(KeyEvent evt)
270 ToolTipManager.sharedInstance().registerComponent(this);
271 ToolTipManager.sharedInstance().setInitialDelay(0);
272 ToolTipManager.sharedInstance().setDismissDelay(10000);
275 Vector<Bond> visiblebonds;
279 seqColoursReady = false;
280 // Sort the bonds by z coord
281 visiblebonds = new Vector<Bond>();
283 for (PDBChain chain : pdb.getChains())
287 for (Bond bond : chain.bonds)
289 visiblebonds.addElement(bond);
295 seqColoursReady = true;
300 public void findWidth()
302 float[] max = new float[3];
303 float[] min = new float[3];
305 max[0] = (float) -1e30;
306 max[1] = (float) -1e30;
307 max[2] = (float) -1e30;
309 min[0] = (float) 1e30;
310 min[1] = (float) 1e30;
311 min[2] = (float) 1e30;
313 for (PDBChain chain : pdb.getChains())
317 for (Bond tmp : chain.bonds)
319 if (tmp.start[0] >= max[0])
321 max[0] = tmp.start[0];
324 if (tmp.start[1] >= max[1])
326 max[1] = tmp.start[1];
329 if (tmp.start[2] >= max[2])
331 max[2] = tmp.start[2];
334 if (tmp.start[0] <= min[0])
336 min[0] = tmp.start[0];
339 if (tmp.start[1] <= min[1])
341 min[1] = tmp.start[1];
344 if (tmp.start[2] <= min[2])
346 min[2] = tmp.start[2];
349 if (tmp.end[0] >= max[0])
354 if (tmp.end[1] >= max[1])
359 if (tmp.end[2] >= max[2])
364 if (tmp.end[0] <= min[0])
369 if (tmp.end[1] <= min[1])
374 if (tmp.end[2] <= min[2])
382 * System.out.println("xmax " + max[0] + " min " + min[0]);
383 * System.out.println("ymax " + max[1] + " min " + min[1]);
384 * System.out.println("zmax " + max[2] + " min " + min[2]);
387 width[0] = Math.abs(max[0] - min[0]);
388 width[1] = Math.abs(max[1] - min[1]);
389 width[2] = Math.abs(max[2] - min[2]);
393 if (width[1] > width[0])
398 if (width[2] > width[1])
403 // System.out.println("Maxwidth = " + maxwidth);
406 public float findScale()
415 height = getHeight();
419 width = prefsize.width;
420 height = prefsize.height;
432 return (float) (dim / (1.5d * maxwidth));
435 public void findCentre()
443 // Find centre coordinate
444 for (PDBChain chain : pdb.getChains())
448 bsize += chain.bonds.size();
450 for (Bond bond : chain.bonds)
452 xtot = xtot + bond.start[0] + bond.end[0];
453 ytot = ytot + bond.start[1] + bond.end[1];
454 ztot = ztot + bond.start[2] + bond.end[2];
459 centre[0] = xtot / (2 * (float) bsize);
460 centre[1] = ytot / (2 * (float) bsize);
461 centre[2] = ztot / (2 * (float) bsize);
465 public void paintComponent(Graphics g)
467 super.paintComponent(g);
469 if (!seqColoursReady || errorMessage != null)
471 g.setColor(Color.black);
472 g.setFont(new Font("Verdana", Font.BOLD, 14));
473 g.drawString(errorMessage == null ? "Retrieving PDB data...."
474 : errorMessage, 20, getHeight() / 2);
478 // Only create the image at the beginning -
479 // this saves much memory usage
480 if ((img == null) || (prefsize.width != getWidth())
481 || (prefsize.height != getHeight()))
484 prefsize.width = getWidth();
485 prefsize.height = getHeight();
488 img = createImage(prefsize.width, prefsize.height);
489 ig = img.getGraphics();
490 Graphics2D ig2 = (Graphics2D) ig;
492 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
493 RenderingHints.VALUE_ANTIALIAS_ON);
500 drawAll(ig, prefsize.width, prefsize.height);
501 redrawneeded = false;
504 g.drawImage(img, 0, 0, this);
509 public void drawAll(Graphics g, int width, int height)
511 g.setColor(backgroundColour);
512 g.fillRect(0, 0, width, height);
517 public void updateSeqColours()
530 // This method has been taken out of PDBChain to allow
531 // Applet and Application specific sequence renderers to be used
532 void colourBySequence()
534 SequenceRenderer sr = new SequenceRenderer(ap.av);
536 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
538 boolean showFeatures = false;
539 if (ap.av.isShowSequenceFeatures())
543 fr = new FeatureRenderer(ap);
546 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
551 FeatureColourFinder finder = new FeatureColourFinder(fr);
553 if (bysequence && pdb != null)
555 for (int ii = 0; ii < pdb.getChains().size(); ii++)
557 chain = pdb.getChains().elementAt(ii);
559 for (int i = 0; i < chain.bonds.size(); i++)
561 Bond tmp = chain.bonds.elementAt(i);
562 tmp.startCol = Color.lightGray;
563 tmp.endCol = Color.lightGray;
564 if (chain != mainchain)
569 for (int s = 0; s < sequence.length; s++)
571 for (int m = 0; m < mapping.length; m++)
573 if (mapping[m].getSequence() == sequence[s])
575 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
578 pos = sequence[s].findIndex(pos);
579 tmp.startCol = sr.getResidueColour(sequence[s], pos,
582 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
585 pos = sequence[s].findIndex(pos);
587 .getResidueColour(sequence[s], pos, finder);
600 public void drawScene(Graphics g)
609 zsort.sort(visiblebonds);
613 for (int i = 0; i < visiblebonds.size(); i++)
615 tmpBond = visiblebonds.elementAt(i);
617 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
618 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
620 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
621 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
623 xmid = (xend + xstart) / 2;
624 ymid = (yend + ystart) / 2;
625 if (depthcue && !bymolecule)
627 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
630 g.setColor(tmpBond.startCol.darker().darker());
631 drawLine(g, xstart, ystart, xmid, ymid);
632 g.setColor(tmpBond.endCol.darker().darker());
633 drawLine(g, xmid, ymid, xend, yend);
636 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
638 g.setColor(tmpBond.startCol.darker());
639 drawLine(g, xstart, ystart, xmid, ymid);
641 g.setColor(tmpBond.endCol.darker());
642 drawLine(g, xmid, ymid, xend, yend);
646 g.setColor(tmpBond.startCol);
647 drawLine(g, xstart, ystart, xmid, ymid);
649 g.setColor(tmpBond.endCol);
650 drawLine(g, xmid, ymid, xend, yend);
653 else if (depthcue && bymolecule)
655 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
657 g.setColor(Color.green.darker().darker());
658 drawLine(g, xstart, ystart, xend, yend);
660 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
662 g.setColor(Color.green.darker());
663 drawLine(g, xstart, ystart, xend, yend);
667 g.setColor(Color.green);
668 drawLine(g, xstart, ystart, xend, yend);
671 else if (!depthcue && !bymolecule)
673 g.setColor(tmpBond.startCol);
674 drawLine(g, xstart, ystart, xmid, ymid);
675 g.setColor(tmpBond.endCol);
676 drawLine(g, xmid, ymid, xend, yend);
680 drawLine(g, xstart, ystart, xend, yend);
683 if (highlightBond1 != null && highlightBond1 == tmpBond)
685 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
687 drawLine(g, xmid, ymid, xend, yend);
690 if (highlightBond2 != null && highlightBond2 == tmpBond)
692 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
694 drawLine(g, xstart, ystart, xmid, ymid);
701 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
705 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
707 g.drawLine(x1, y1, x2, y2);
708 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
709 g.drawLine(x1, y1 - 1, x2, y2 - 1);
713 g.setColor(g.getColor().brighter());
714 g.drawLine(x1, y1, x2, y2);
715 g.drawLine(x1 + 1, y1, x2 + 1, y2);
716 g.drawLine(x1 - 1, y1, x2 - 1, y2);
721 g.drawLine(x1, y1, x2, y2);
725 public Dimension minimumsize()
730 public Dimension preferredsize()
735 public void keyPressed(KeyEvent evt)
737 if (evt.getKeyCode() == KeyEvent.VK_UP)
739 scale = (float) (scale * 1.1);
743 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
745 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.getChains().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);
797 public void mouseMoved(MouseEvent e)
800 if (highlightBond1 != null)
802 highlightBond1.at2.isSelected = false;
803 highlightBond2.at1.isSelected = false;
804 highlightBond1 = null;
805 highlightBond2 = null;
808 Atom fatom = findAtom(e.getX(), e.getY());
810 PDBChain chain = null;
811 if (foundchain != -1)
813 chain = pdb.getChains().elementAt(foundchain);
814 if (chain == mainchain)
816 mouseOverStructure(fatom.resNumber, chain.id);
822 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
827 mouseOverStructure(-1, chain != null ? chain.id : null);
828 this.setToolTipText(null);
833 public void mouseClicked(MouseEvent e)
838 public void mouseEntered(MouseEvent e)
843 public void mouseExited(MouseEvent e)
848 public void mouseDragged(MouseEvent evt)
855 MCMatrix objmat = new MCMatrix(3, 3);
856 objmat.setIdentity();
858 if ((evt.getModifiers() & Event.META_MASK) != 0)
860 objmat.rotatez(((mx - omx)));
864 objmat.rotatex(((my - omy)));
865 objmat.rotatey(((omx - mx)));
869 for (PDBChain chain : pdb.getChains())
871 for (Bond tmpBond : chain.bonds)
873 // Translate the bond so the centre is 0,0,0
874 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
876 // Now apply the rotation matrix
877 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
878 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
880 // Now translate back again
881 tmpBond.translate(centre[0], centre[1], centre[2]);
898 public void mouseReleased(MouseEvent evt)
904 void drawLabels(Graphics g)
907 for (PDBChain chain : pdb.getChains())
911 for (Bond tmpBond : chain.bonds)
913 if (tmpBond.at1.isSelected)
915 labelAtom(g, tmpBond, 1);
918 if (tmpBond.at2.isSelected)
920 labelAtom(g, tmpBond, 2);
927 public void labelAtom(Graphics g, Bond b, int n)
930 g.setColor(Color.red);
933 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
934 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
936 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
941 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
942 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
944 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
950 public Atom findAtom(int x, int y)
956 for (int ii = 0; ii < pdb.getChains().size(); ii++)
958 PDBChain chain = pdb.getChains().elementAt(ii);
964 for (Bond bond : chain.bonds)
968 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
970 if (Math.abs(truex - x) <= 2)
972 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
974 if (Math.abs(truey - y) <= 2)
983 // Still here? Maybe its the last bond
985 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
987 if (Math.abs(truex - x) <= 2)
989 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
991 if (Math.abs(truey - y) <= 2)
1001 if (fatom != null) // )&& chain.ds != null)
1002 { // dead code? value of chain is either overwritten or discarded
1003 chain = pdb.getChains().elementAt(foundchain);
1010 Bond highlightBond1, highlightBond2;
1012 public void highlightRes(int ii)
1014 if (!seqColoursReady)
1019 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1026 for (index = 0; index < mainchain.bonds.size(); index++)
1028 tmpBond = mainchain.bonds.elementAt(index);
1029 if (tmpBond.at1.alignmentMapping == ii - 1)
1031 if (highlightBond1 != null)
1033 highlightBond1.at2.isSelected = false;
1036 if (highlightBond2 != null)
1038 highlightBond2.at1.isSelected = false;
1041 highlightBond1 = null;
1042 highlightBond2 = null;
1046 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1047 highlightBond1.at2.isSelected = true;
1050 if (index != mainchain.bonds.size())
1052 highlightBond2 = mainchain.bonds.elementAt(index);
1053 highlightBond2.at1.isSelected = true;
1060 redrawneeded = true;
1064 public void setAllchainsVisible(boolean b)
1066 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1068 PDBChain chain = pdb.getChains().elementAt(ii);
1069 chain.isVisible = b;
1071 mainchain.isVisible = true;
1076 // ////////////////////////////////
1077 // /StructureListener
1079 public String[] getStructureFiles()
1081 return new String[] { pdbentry.getFile() };
1086 public void mouseOverStructure(int pdbResNum, String chain)
1088 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1090 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1093 lastMessage = pdbResNum + chain;
1096 StringBuffer resetLastRes = new StringBuffer();
1098 StringBuffer eval = new StringBuffer();
1101 * Highlight the specified atoms in the structure.
1106 public void highlightAtoms(List<AtomSpec> atoms)
1108 if (!seqColoursReady)
1113 for (AtomSpec atom : atoms)
1115 int atomIndex = atom.getAtomIndex();
1116 if (highlightRes != null
1117 && highlightRes.contains((atomIndex - 1) + ""))
1122 highlightAtom(atomIndex);
1125 redrawneeded = true;
1130 * Highlight the atom at the specified index.
1134 protected void highlightAtom(int atomIndex)
1138 for (index = 0; index < mainchain.bonds.size(); index++)
1140 tmpBond = mainchain.bonds.elementAt(index);
1141 if (tmpBond.at1.atomIndex == atomIndex)
1143 if (highlightBond1 != null)
1145 highlightBond1.at2.isSelected = false;
1148 if (highlightBond2 != null)
1150 highlightBond2.at1.isSelected = false;
1153 highlightBond1 = null;
1154 highlightBond2 = null;
1158 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1159 highlightBond1.at2.isSelected = true;
1162 if (index != mainchain.bonds.size())
1164 highlightBond2 = mainchain.bonds.elementAt(index);
1165 highlightBond2.at1.isSelected = true;
1173 public Color getColour(int atomIndex, int pdbResNum, String chain,
1177 // if (!pdbfile.equals(pdbentry.getFile()))
1180 // return new Color(viewer.getAtomArgb(atomIndex));
1184 public void updateColours(Object source)
1187 redrawneeded = true;
1192 public void releaseReferences(Object svl)
1194 // TODO Auto-generated method stub
1199 public boolean isListeningFor(SequenceI seq)
1201 if (sequence != null)
1203 for (SequenceI s : sequence)