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 java.awt.Color;
24 import java.awt.Dimension;
25 import java.awt.Event;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
30 import java.awt.Image;
31 import java.awt.RenderingHints;
32 import java.awt.event.KeyAdapter;
33 import java.awt.event.KeyEvent;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseListener;
36 import java.awt.event.MouseMotionListener;
37 import java.io.PrintStream;
38 import java.util.List;
39 import java.util.Vector;
41 import javax.swing.JPanel;
42 import javax.swing.ToolTipManager;
44 import jalview.analysis.AlignSeq;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.SequenceI;
47 import jalview.gui.AlignmentPanel;
48 import jalview.gui.FeatureRenderer;
49 import jalview.gui.SequenceRenderer;
50 import jalview.structure.AtomSpec;
51 import jalview.structure.StructureListener;
52 import jalview.structure.StructureMapping;
53 import jalview.structure.StructureSelectionManager;
55 public class PDBCanvas extends JPanel implements MouseListener,
56 MouseMotionListener, StructureListener
58 boolean redrawneeded = true;
80 float[] centre = new float[3];
82 float[] width = new float[3];
92 boolean bysequence = true;
94 boolean depthcue = true;
98 boolean bymolecule = false;
100 boolean zbuffer = true;
116 Font font = new Font("Helvetica", Font.PLAIN, 10);
118 jalview.gui.SeqCanvas seqcanvas;
120 public SequenceI[] sequence;
122 final StringBuffer mappingDetails = new StringBuffer();
126 Vector<String> highlightRes;
128 boolean pdbAction = false;
130 boolean seqColoursReady = false;
132 jalview.renderer.seqfeatures.FeatureRenderer fr;
134 Color backgroundColour = Color.black;
138 StructureSelectionManager ssm;
142 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
143 AlignmentPanel ap, String protocol)
146 this.pdbentry = pdbentry;
149 ssm = ap.av.getStructureSelectionManager();
153 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
155 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
157 pdbentry.setFile("INLINE" + pdb.id);
160 } catch (Exception ex)
162 ex.printStackTrace();
168 errorMessage = "Error loading file: " + pdbentry.getId();
171 pdbentry.setId(pdb.id);
173 ssm.addStructureViewerListener(this);
184 // JUST DEAL WITH ONE SEQUENCE FOR NOW
185 SequenceI sequence = seq[0];
187 for (int i = 0; i < pdb.chains.size(); i++)
190 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
191 + pdb.chains.elementAt(i).sequence
192 .getSequenceAsString());
193 mappingDetails.append("\nNo of residues = "
194 + pdb.chains.elementAt(i).residues.size()
197 // Now lets compare the sequences to get
198 // the start and end points.
199 // Align the sequence to the pdb
200 AlignSeq as = new AlignSeq(sequence,
201 pdb.chains.elementAt(i).sequence, "pep");
202 as.calcScoreMatrix();
204 PrintStream ps = new PrintStream(System.out)
206 public void print(String x)
208 mappingDetails.append(x);
211 public void println()
213 mappingDetails.append("\n");
217 as.printAlignment(ps);
219 if (as.maxscore > max)
224 pdbstart = as.seq2start;
226 seqstart = as.seq1start + sequence.getStart() - 1;
227 seqend = as.seq1end + sequence.getEnd() - 1;
230 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
231 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
234 mainchain = pdb.chains.elementAt(maxchain);
236 mainchain.pdbstart = pdbstart;
237 mainchain.pdbend = pdbend;
238 mainchain.seqstart = seqstart;
239 mainchain.seqend = seqend;
240 mainchain.isVisible = true;
243 this.prefsize = new Dimension(getSize().width, getSize().height);
245 addMouseMotionListener(this);
246 addMouseListener(this);
248 addKeyListener(new KeyAdapter()
250 public void keyPressed(KeyEvent evt)
263 ToolTipManager.sharedInstance().registerComponent(this);
264 ToolTipManager.sharedInstance().setInitialDelay(0);
265 ToolTipManager.sharedInstance().setDismissDelay(10000);
268 Vector<Bond> visiblebonds;
272 seqColoursReady = false;
273 // Sort the bonds by z coord
274 visiblebonds = new Vector<Bond>();
276 for (PDBChain chain : pdb.chains)
280 for (Bond bond : chain.bonds)
282 visiblebonds.addElement(bond);
288 seqColoursReady = true;
293 public void findWidth()
295 float[] max = new float[3];
296 float[] min = new float[3];
298 max[0] = (float) -1e30;
299 max[1] = (float) -1e30;
300 max[2] = (float) -1e30;
302 min[0] = (float) 1e30;
303 min[1] = (float) 1e30;
304 min[2] = (float) 1e30;
306 for (PDBChain chain : pdb.chains)
310 for (Bond tmp : chain.bonds)
312 if (tmp.start[0] >= max[0])
314 max[0] = tmp.start[0];
317 if (tmp.start[1] >= max[1])
319 max[1] = tmp.start[1];
322 if (tmp.start[2] >= max[2])
324 max[2] = tmp.start[2];
327 if (tmp.start[0] <= min[0])
329 min[0] = tmp.start[0];
332 if (tmp.start[1] <= min[1])
334 min[1] = tmp.start[1];
337 if (tmp.start[2] <= min[2])
339 min[2] = tmp.start[2];
342 if (tmp.end[0] >= max[0])
347 if (tmp.end[1] >= max[1])
352 if (tmp.end[2] >= max[2])
357 if (tmp.end[0] <= min[0])
362 if (tmp.end[1] <= min[1])
367 if (tmp.end[2] <= min[2])
375 * System.out.println("xmax " + max[0] + " min " + min[0]);
376 * System.out.println("ymax " + max[1] + " min " + min[1]);
377 * System.out.println("zmax " + max[2] + " min " + min[2]);
380 width[0] = Math.abs(max[0] - min[0]);
381 width[1] = Math.abs(max[1] - min[1]);
382 width[2] = Math.abs(max[2] - min[2]);
386 if (width[1] > width[0])
391 if (width[2] > width[1])
396 // System.out.println("Maxwidth = " + maxwidth);
399 public float findScale()
408 height = getHeight();
412 width = prefsize.width;
413 height = prefsize.height;
425 return (float) (dim / (1.5d * maxwidth));
428 public void findCentre()
436 // Find centre coordinate
437 for (PDBChain chain : pdb.chains)
441 bsize += chain.bonds.size();
443 for (Bond bond : chain.bonds)
445 xtot = xtot + bond.start[0] + bond.end[0];
446 ytot = ytot + bond.start[1] + bond.end[1];
447 ztot = ztot + bond.start[2] + bond.end[2];
452 centre[0] = xtot / (2 * (float) bsize);
453 centre[1] = ytot / (2 * (float) bsize);
454 centre[2] = ztot / (2 * (float) bsize);
457 public void paintComponent(Graphics g)
459 super.paintComponent(g);
461 if (!seqColoursReady || errorMessage != null)
463 g.setColor(Color.black);
464 g.setFont(new Font("Verdana", Font.BOLD, 14));
465 g.drawString(errorMessage == null ? "Retrieving PDB data...."
466 : errorMessage, 20, getHeight() / 2);
470 // Only create the image at the beginning -
471 // this saves much memory usage
472 if ((img == null) || (prefsize.width != getWidth())
473 || (prefsize.height != getHeight()))
476 prefsize.width = getWidth();
477 prefsize.height = getHeight();
480 img = createImage(prefsize.width, prefsize.height);
481 ig = img.getGraphics();
482 Graphics2D ig2 = (Graphics2D) ig;
484 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
485 RenderingHints.VALUE_ANTIALIAS_ON);
492 drawAll(ig, prefsize.width, prefsize.height);
493 redrawneeded = false;
496 g.drawImage(img, 0, 0, this);
501 public void drawAll(Graphics g, int width, int height)
503 g.setColor(backgroundColour);
504 g.fillRect(0, 0, width, height);
509 public void updateSeqColours()
522 // This method has been taken out of PDBChain to allow
523 // Applet and Application specific sequence renderers to be used
524 void colourBySequence()
526 SequenceRenderer sr = new SequenceRenderer(ap.av);
528 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
530 boolean showFeatures = false;
531 if (ap.av.isShowSequenceFeatures())
535 fr = new FeatureRenderer(ap);
538 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
544 if (bysequence && pdb != null)
546 for (int ii = 0; ii < pdb.chains.size(); ii++)
548 chain = pdb.chains.elementAt(ii);
550 for (int i = 0; i < chain.bonds.size(); i++)
552 Bond tmp = chain.bonds.elementAt(i);
553 tmp.startCol = Color.lightGray;
554 tmp.endCol = Color.lightGray;
555 if (chain != mainchain)
560 for (int s = 0; s < sequence.length; s++)
562 for (int m = 0; m < mapping.length; m++)
564 if (mapping[m].getSequence() == sequence[s])
566 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
569 pos = sequence[s].findIndex(pos);
570 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
573 tmp.startCol = fr.findFeatureColour(tmp.startCol,
577 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
580 pos = sequence[s].findIndex(pos);
581 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
584 tmp.endCol = fr.findFeatureColour(tmp.endCol,
599 public void drawScene(Graphics g)
608 zsort.sort(visiblebonds);
612 for (int i = 0; i < visiblebonds.size(); i++)
614 tmpBond = visiblebonds.elementAt(i);
616 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
617 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
619 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
620 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
622 xmid = (xend + xstart) / 2;
623 ymid = (yend + ystart) / 2;
624 if (depthcue && !bymolecule)
626 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
629 g.setColor(tmpBond.startCol.darker().darker());
630 drawLine(g, xstart, ystart, xmid, ymid);
631 g.setColor(tmpBond.endCol.darker().darker());
632 drawLine(g, xmid, ymid, xend, yend);
635 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
637 g.setColor(tmpBond.startCol.darker());
638 drawLine(g, xstart, ystart, xmid, ymid);
640 g.setColor(tmpBond.endCol.darker());
641 drawLine(g, xmid, ymid, xend, yend);
645 g.setColor(tmpBond.startCol);
646 drawLine(g, xstart, ystart, xmid, ymid);
648 g.setColor(tmpBond.endCol);
649 drawLine(g, xmid, ymid, xend, yend);
652 else if (depthcue && bymolecule)
654 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
656 g.setColor(Color.green.darker().darker());
657 drawLine(g, xstart, ystart, xend, yend);
659 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
661 g.setColor(Color.green.darker());
662 drawLine(g, xstart, ystart, xend, yend);
666 g.setColor(Color.green);
667 drawLine(g, xstart, ystart, xend, yend);
670 else if (!depthcue && !bymolecule)
672 g.setColor(tmpBond.startCol);
673 drawLine(g, xstart, ystart, xmid, ymid);
674 g.setColor(tmpBond.endCol);
675 drawLine(g, xmid, ymid, xend, yend);
679 drawLine(g, xstart, ystart, xend, yend);
682 if (highlightBond1 != null && highlightBond1 == tmpBond)
684 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
686 drawLine(g, xmid, ymid, xend, yend);
689 if (highlightBond2 != null && highlightBond2 == tmpBond)
691 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
693 drawLine(g, xstart, ystart, xmid, ymid);
700 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
704 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
706 g.drawLine(x1, y1, x2, y2);
707 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
708 g.drawLine(x1, y1 - 1, x2, y2 - 1);
712 g.setColor(g.getColor().brighter());
713 g.drawLine(x1, y1, x2, y2);
714 g.drawLine(x1 + 1, y1, x2 + 1, y2);
715 g.drawLine(x1 - 1, y1, x2 - 1, y2);
720 g.drawLine(x1, y1, x2, y2);
724 public Dimension minimumsize()
729 public Dimension preferredsize()
734 public void keyPressed(KeyEvent evt)
736 if (evt.getKeyCode() == KeyEvent.VK_UP)
738 scale = (float) (scale * 1.1);
742 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
744 scale = (float) (scale * 0.9);
750 public void mousePressed(MouseEvent e)
753 Atom fatom = findAtom(e.getX(), e.getY());
756 fatom.isSelected = !fatom.isSelected;
760 if (foundchain != -1)
762 PDBChain chain = pdb.chains.elementAt(foundchain);
763 if (chain == mainchain)
765 if (fatom.alignmentMapping != -1)
767 if (highlightRes == null)
769 highlightRes = new Vector<String>();
772 final String atomString = Integer
773 .toString(fatom.alignmentMapping);
774 if (highlightRes.contains(atomString))
776 highlightRes.remove(atomString);
780 highlightRes.add(atomString);
794 public void mouseMoved(MouseEvent e)
797 if (highlightBond1 != null)
799 highlightBond1.at2.isSelected = false;
800 highlightBond2.at1.isSelected = false;
801 highlightBond1 = null;
802 highlightBond2 = null;
805 Atom fatom = findAtom(e.getX(), e.getY());
807 PDBChain chain = null;
808 if (foundchain != -1)
810 chain = pdb.chains.elementAt(foundchain);
811 if (chain == mainchain)
813 mouseOverStructure(fatom.resNumber, chain.id);
819 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
824 mouseOverStructure(-1, chain != null ? chain.id : null);
825 this.setToolTipText(null);
829 public void mouseClicked(MouseEvent e)
833 public void mouseEntered(MouseEvent e)
837 public void mouseExited(MouseEvent e)
841 public void mouseDragged(MouseEvent evt)
848 MCMatrix objmat = new MCMatrix(3, 3);
849 objmat.setIdentity();
851 if ((evt.getModifiers() & Event.META_MASK) != 0)
853 objmat.rotatez(((mx - omx)));
857 objmat.rotatex(((my - omy)));
858 objmat.rotatey(((omx - mx)));
862 for (PDBChain chain : pdb.chains)
864 for (Bond tmpBond : chain.bonds)
866 // Translate the bond so the centre is 0,0,0
867 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
869 // Now apply the rotation matrix
870 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
871 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
873 // Now translate back again
874 tmpBond.translate(centre[0], centre[1], centre[2]);
890 public void mouseReleased(MouseEvent evt)
896 void drawLabels(Graphics g)
899 for (PDBChain chain : pdb.chains)
903 for (Bond tmpBond : chain.bonds)
905 if (tmpBond.at1.isSelected)
907 labelAtom(g, tmpBond, 1);
910 if (tmpBond.at2.isSelected)
912 labelAtom(g, tmpBond, 2);
919 public void labelAtom(Graphics g, Bond b, int n)
922 g.setColor(Color.red);
925 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
926 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
928 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
933 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
934 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
936 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
942 public Atom findAtom(int x, int y)
948 for (int ii = 0; ii < pdb.chains.size(); ii++)
950 PDBChain chain = pdb.chains.elementAt(ii);
956 for (Bond bond : chain.bonds)
960 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
962 if (Math.abs(truex - x) <= 2)
964 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
966 if (Math.abs(truey - y) <= 2)
975 // Still here? Maybe its the last bond
977 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
979 if (Math.abs(truex - x) <= 2)
981 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
983 if (Math.abs(truey - y) <= 2)
993 if (fatom != null) // )&& chain.ds != null)
994 { // dead code? value of chain is either overwritten or discarded
995 chain = pdb.chains.elementAt(foundchain);
1002 Bond highlightBond1, highlightBond2;
1004 public void highlightRes(int ii)
1006 if (!seqColoursReady)
1011 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1018 for (index = 0; index < mainchain.bonds.size(); index++)
1020 tmpBond = mainchain.bonds.elementAt(index);
1021 if (tmpBond.at1.alignmentMapping == ii - 1)
1023 if (highlightBond1 != null)
1025 highlightBond1.at2.isSelected = false;
1028 if (highlightBond2 != null)
1030 highlightBond2.at1.isSelected = false;
1033 highlightBond1 = null;
1034 highlightBond2 = null;
1038 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1039 highlightBond1.at2.isSelected = true;
1042 if (index != mainchain.bonds.size())
1044 highlightBond2 = mainchain.bonds.elementAt(index);
1045 highlightBond2.at1.isSelected = true;
1052 redrawneeded = true;
1056 public void setAllchainsVisible(boolean b)
1058 for (int ii = 0; ii < pdb.chains.size(); ii++)
1060 PDBChain chain = pdb.chains.elementAt(ii);
1061 chain.isVisible = b;
1063 mainchain.isVisible = true;
1068 // ////////////////////////////////
1069 // /StructureListener
1070 public String[] getPdbFile()
1073 { pdbentry.getFile() };
1078 public void mouseOverStructure(int pdbResNum, String chain)
1080 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1082 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1085 lastMessage = pdbResNum + chain;
1088 StringBuffer resetLastRes = new StringBuffer();
1090 StringBuffer eval = new StringBuffer();
1093 * Highlight the specified atoms in the structure.
1098 public void highlightAtoms(List<AtomSpec> atoms)
1100 if (!seqColoursReady)
1105 for (AtomSpec atom : atoms)
1107 int atomIndex = atom.getAtomIndex();
1108 if (highlightRes != null
1109 && highlightRes.contains((atomIndex - 1) + ""))
1114 highlightAtom(atomIndex);
1117 redrawneeded = true;
1122 * Highlight the atom at the specified index.
1126 protected void highlightAtom(int atomIndex)
1130 for (index = 0; index < mainchain.bonds.size(); index++)
1132 tmpBond = mainchain.bonds.elementAt(index);
1133 if (tmpBond.at1.atomIndex == atomIndex)
1135 if (highlightBond1 != null)
1137 highlightBond1.at2.isSelected = false;
1140 if (highlightBond2 != null)
1142 highlightBond2.at1.isSelected = false;
1145 highlightBond1 = null;
1146 highlightBond2 = null;
1150 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1151 highlightBond1.at2.isSelected = true;
1154 if (index != mainchain.bonds.size())
1156 highlightBond2 = mainchain.bonds.elementAt(index);
1157 highlightBond2.at1.isSelected = true;
1165 public Color getColour(int atomIndex, int pdbResNum, String chain,
1169 // if (!pdbfile.equals(pdbentry.getFile()))
1172 // return new Color(viewer.getAtomArgb(atomIndex));
1175 public void updateColours(Object source)
1178 redrawneeded = true;
1183 public void releaseReferences(Object svl)
1185 // TODO Auto-generated method stub
1190 public boolean isListeningFor(SequenceI seq)
1192 if (sequence != null)
1194 for (SequenceI s : sequence)