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.structure.AtomSpec;
30 import jalview.structure.StructureListener;
31 import jalview.structure.StructureMapping;
32 import jalview.structure.StructureSelectionManager;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Event;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
41 import java.awt.Image;
42 import java.awt.RenderingHints;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.io.PrintStream;
49 import java.util.List;
50 import java.util.Vector;
52 import javax.swing.JPanel;
53 import javax.swing.ToolTipManager;
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.getSequenceAsString());
192 mappingDetails.append("\nNo of residues = "
193 + pdb.chains.elementAt(i).residues.size() + "\n\n");
195 // Now lets compare the sequences to get
196 // the start and end points.
197 // Align the sequence to the pdb
198 AlignSeq as = new AlignSeq(sequence,
199 pdb.chains.elementAt(i).sequence, "pep");
200 as.calcScoreMatrix();
202 PrintStream ps = new PrintStream(System.out)
204 public void print(String x)
206 mappingDetails.append(x);
209 public void println()
211 mappingDetails.append("\n");
215 as.printAlignment(ps);
217 if (as.maxscore > max)
222 pdbstart = as.seq2start;
224 seqstart = as.seq1start + sequence.getStart() - 1;
225 seqend = as.seq1end + sequence.getEnd() - 1;
228 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
229 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
232 mainchain = pdb.chains.elementAt(maxchain);
234 mainchain.pdbstart = pdbstart;
235 mainchain.pdbend = pdbend;
236 mainchain.seqstart = seqstart;
237 mainchain.seqend = seqend;
238 mainchain.isVisible = true;
241 this.prefsize = new Dimension(getSize().width, getSize().height);
243 addMouseMotionListener(this);
244 addMouseListener(this);
246 addKeyListener(new KeyAdapter()
248 public void keyPressed(KeyEvent evt)
261 ToolTipManager.sharedInstance().registerComponent(this);
262 ToolTipManager.sharedInstance().setInitialDelay(0);
263 ToolTipManager.sharedInstance().setDismissDelay(10000);
266 Vector<Bond> visiblebonds;
270 seqColoursReady = false;
271 // Sort the bonds by z coord
272 visiblebonds = new Vector<Bond>();
274 for (PDBChain chain : pdb.chains)
278 for (Bond bond : chain.bonds)
280 visiblebonds.addElement(bond);
286 seqColoursReady = true;
291 public void findWidth()
293 float[] max = new float[3];
294 float[] min = new float[3];
296 max[0] = (float) -1e30;
297 max[1] = (float) -1e30;
298 max[2] = (float) -1e30;
300 min[0] = (float) 1e30;
301 min[1] = (float) 1e30;
302 min[2] = (float) 1e30;
304 for (PDBChain chain : pdb.chains)
308 for (Bond tmp : chain.bonds)
310 if (tmp.start[0] >= max[0])
312 max[0] = tmp.start[0];
315 if (tmp.start[1] >= max[1])
317 max[1] = tmp.start[1];
320 if (tmp.start[2] >= max[2])
322 max[2] = tmp.start[2];
325 if (tmp.start[0] <= min[0])
327 min[0] = tmp.start[0];
330 if (tmp.start[1] <= min[1])
332 min[1] = tmp.start[1];
335 if (tmp.start[2] <= min[2])
337 min[2] = tmp.start[2];
340 if (tmp.end[0] >= max[0])
345 if (tmp.end[1] >= max[1])
350 if (tmp.end[2] >= max[2])
355 if (tmp.end[0] <= min[0])
360 if (tmp.end[1] <= min[1])
365 if (tmp.end[2] <= min[2])
373 * System.out.println("xmax " + max[0] + " min " + min[0]);
374 * System.out.println("ymax " + max[1] + " min " + min[1]);
375 * System.out.println("zmax " + max[2] + " min " + min[2]);
378 width[0] = Math.abs(max[0] - min[0]);
379 width[1] = Math.abs(max[1] - min[1]);
380 width[2] = Math.abs(max[2] - min[2]);
384 if (width[1] > width[0])
389 if (width[2] > width[1])
394 // System.out.println("Maxwidth = " + maxwidth);
397 public float findScale()
406 height = getHeight();
410 width = prefsize.width;
411 height = prefsize.height;
423 return (float) (dim / (1.5d * maxwidth));
426 public void findCentre()
434 // Find centre coordinate
435 for (PDBChain chain : pdb.chains)
439 bsize += chain.bonds.size();
441 for (Bond bond : chain.bonds)
443 xtot = xtot + bond.start[0] + bond.end[0];
444 ytot = ytot + bond.start[1] + bond.end[1];
445 ztot = ztot + bond.start[2] + bond.end[2];
450 centre[0] = xtot / (2 * (float) bsize);
451 centre[1] = ytot / (2 * (float) bsize);
452 centre[2] = ztot / (2 * (float) bsize);
455 public void paintComponent(Graphics g)
457 super.paintComponent(g);
459 if (!seqColoursReady || errorMessage != null)
461 g.setColor(Color.black);
462 g.setFont(new Font("Verdana", Font.BOLD, 14));
463 g.drawString(errorMessage == null ? "Retrieving PDB data...."
464 : errorMessage, 20, getHeight() / 2);
468 // Only create the image at the beginning -
469 // this saves much memory usage
470 if ((img == null) || (prefsize.width != getWidth())
471 || (prefsize.height != getHeight()))
474 prefsize.width = getWidth();
475 prefsize.height = getHeight();
478 img = createImage(prefsize.width, prefsize.height);
479 ig = img.getGraphics();
480 Graphics2D ig2 = (Graphics2D) ig;
482 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
483 RenderingHints.VALUE_ANTIALIAS_ON);
490 drawAll(ig, prefsize.width, prefsize.height);
491 redrawneeded = false;
494 g.drawImage(img, 0, 0, this);
499 public void drawAll(Graphics g, int width, int height)
501 g.setColor(backgroundColour);
502 g.fillRect(0, 0, width, height);
507 public void updateSeqColours()
520 // This method has been taken out of PDBChain to allow
521 // Applet and Application specific sequence renderers to be used
522 void colourBySequence()
524 SequenceRenderer sr = new SequenceRenderer(ap.av);
526 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
528 boolean showFeatures = false;
529 if (ap.av.isShowSequenceFeatures())
533 fr = new FeatureRenderer(ap);
536 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
542 if (bysequence && pdb != null)
544 for (int ii = 0; ii < pdb.chains.size(); ii++)
546 chain = pdb.chains.elementAt(ii);
548 for (int i = 0; i < chain.bonds.size(); i++)
550 Bond tmp = chain.bonds.elementAt(i);
551 tmp.startCol = Color.lightGray;
552 tmp.endCol = Color.lightGray;
553 if (chain != mainchain)
558 for (int s = 0; s < sequence.length; s++)
560 for (int m = 0; m < mapping.length; m++)
562 if (mapping[m].getSequence() == sequence[s])
564 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
567 pos = sequence[s].findIndex(pos);
568 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
571 tmp.startCol = fr.findFeatureColour(tmp.startCol,
575 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
578 pos = sequence[s].findIndex(pos);
579 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
582 tmp.endCol = fr.findFeatureColour(tmp.endCol,
597 public void drawScene(Graphics g)
606 zsort.sort(visiblebonds);
610 for (int i = 0; i < visiblebonds.size(); i++)
612 tmpBond = visiblebonds.elementAt(i);
614 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
615 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
617 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
618 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
620 xmid = (xend + xstart) / 2;
621 ymid = (yend + ystart) / 2;
622 if (depthcue && !bymolecule)
624 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
627 g.setColor(tmpBond.startCol.darker().darker());
628 drawLine(g, xstart, ystart, xmid, ymid);
629 g.setColor(tmpBond.endCol.darker().darker());
630 drawLine(g, xmid, ymid, xend, yend);
633 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
635 g.setColor(tmpBond.startCol.darker());
636 drawLine(g, xstart, ystart, xmid, ymid);
638 g.setColor(tmpBond.endCol.darker());
639 drawLine(g, xmid, ymid, xend, yend);
643 g.setColor(tmpBond.startCol);
644 drawLine(g, xstart, ystart, xmid, ymid);
646 g.setColor(tmpBond.endCol);
647 drawLine(g, xmid, ymid, xend, yend);
650 else if (depthcue && bymolecule)
652 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
654 g.setColor(Color.green.darker().darker());
655 drawLine(g, xstart, ystart, xend, yend);
657 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
659 g.setColor(Color.green.darker());
660 drawLine(g, xstart, ystart, xend, yend);
664 g.setColor(Color.green);
665 drawLine(g, xstart, ystart, xend, yend);
668 else if (!depthcue && !bymolecule)
670 g.setColor(tmpBond.startCol);
671 drawLine(g, xstart, ystart, xmid, ymid);
672 g.setColor(tmpBond.endCol);
673 drawLine(g, xmid, ymid, xend, yend);
677 drawLine(g, xstart, ystart, xend, yend);
680 if (highlightBond1 != null && highlightBond1 == tmpBond)
682 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
684 drawLine(g, xmid, ymid, xend, yend);
687 if (highlightBond2 != null && highlightBond2 == tmpBond)
689 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
691 drawLine(g, xstart, ystart, xmid, ymid);
698 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
702 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
704 g.drawLine(x1, y1, x2, y2);
705 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
706 g.drawLine(x1, y1 - 1, x2, y2 - 1);
710 g.setColor(g.getColor().brighter());
711 g.drawLine(x1, y1, x2, y2);
712 g.drawLine(x1 + 1, y1, x2 + 1, y2);
713 g.drawLine(x1 - 1, y1, x2 - 1, y2);
718 g.drawLine(x1, y1, x2, y2);
722 public Dimension minimumsize()
727 public Dimension preferredsize()
732 public void keyPressed(KeyEvent evt)
734 if (evt.getKeyCode() == KeyEvent.VK_UP)
736 scale = (float) (scale * 1.1);
740 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
742 scale = (float) (scale * 0.9);
748 public void mousePressed(MouseEvent e)
751 Atom fatom = findAtom(e.getX(), e.getY());
754 fatom.isSelected = !fatom.isSelected;
758 if (foundchain != -1)
760 PDBChain chain = pdb.chains.elementAt(foundchain);
761 if (chain == mainchain)
763 if (fatom.alignmentMapping != -1)
765 if (highlightRes == null)
767 highlightRes = new Vector<String>();
770 final String atomString = Integer
771 .toString(fatom.alignmentMapping);
772 if (highlightRes.contains(atomString))
774 highlightRes.remove(atomString);
778 highlightRes.add(atomString);
792 public void mouseMoved(MouseEvent e)
795 if (highlightBond1 != null)
797 highlightBond1.at2.isSelected = false;
798 highlightBond2.at1.isSelected = false;
799 highlightBond1 = null;
800 highlightBond2 = null;
803 Atom fatom = findAtom(e.getX(), e.getY());
805 PDBChain chain = null;
806 if (foundchain != -1)
808 chain = pdb.chains.elementAt(foundchain);
809 if (chain == mainchain)
811 mouseOverStructure(fatom.resNumber, chain.id);
817 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
822 mouseOverStructure(-1, chain != null ? chain.id : null);
823 this.setToolTipText(null);
827 public void mouseClicked(MouseEvent e)
831 public void mouseEntered(MouseEvent e)
835 public void mouseExited(MouseEvent e)
839 public void mouseDragged(MouseEvent evt)
846 MCMatrix objmat = new MCMatrix(3, 3);
847 objmat.setIdentity();
849 if ((evt.getModifiers() & Event.META_MASK) != 0)
851 objmat.rotatez(((mx - omx)));
855 objmat.rotatex(((my - omy)));
856 objmat.rotatey(((omx - mx)));
860 for (PDBChain chain : pdb.chains)
862 for (Bond tmpBond : chain.bonds)
864 // Translate the bond so the centre is 0,0,0
865 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
867 // Now apply the rotation matrix
868 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
869 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
871 // Now translate back again
872 tmpBond.translate(centre[0], centre[1], centre[2]);
888 public void mouseReleased(MouseEvent evt)
894 void drawLabels(Graphics g)
897 for (PDBChain chain : pdb.chains)
901 for (Bond tmpBond : chain.bonds)
903 if (tmpBond.at1.isSelected)
905 labelAtom(g, tmpBond, 1);
908 if (tmpBond.at2.isSelected)
910 labelAtom(g, tmpBond, 2);
917 public void labelAtom(Graphics g, Bond b, int n)
920 g.setColor(Color.red);
923 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
924 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
926 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
931 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
932 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
934 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
940 public Atom findAtom(int x, int y)
946 for (int ii = 0; ii < pdb.chains.size(); ii++)
948 PDBChain chain = pdb.chains.elementAt(ii);
954 for (Bond bond : chain.bonds)
958 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
960 if (Math.abs(truex - x) <= 2)
962 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
964 if (Math.abs(truey - y) <= 2)
973 // Still here? Maybe its the last bond
975 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
977 if (Math.abs(truex - x) <= 2)
979 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
981 if (Math.abs(truey - y) <= 2)
991 if (fatom != null) // )&& chain.ds != null)
992 { // dead code? value of chain is either overwritten or discarded
993 chain = pdb.chains.elementAt(foundchain);
1000 Bond highlightBond1, highlightBond2;
1002 public void highlightRes(int ii)
1004 if (!seqColoursReady)
1009 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1016 for (index = 0; index < mainchain.bonds.size(); index++)
1018 tmpBond = mainchain.bonds.elementAt(index);
1019 if (tmpBond.at1.alignmentMapping == ii - 1)
1021 if (highlightBond1 != null)
1023 highlightBond1.at2.isSelected = false;
1026 if (highlightBond2 != null)
1028 highlightBond2.at1.isSelected = false;
1031 highlightBond1 = null;
1032 highlightBond2 = null;
1036 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1037 highlightBond1.at2.isSelected = true;
1040 if (index != mainchain.bonds.size())
1042 highlightBond2 = mainchain.bonds.elementAt(index);
1043 highlightBond2.at1.isSelected = true;
1050 redrawneeded = true;
1054 public void setAllchainsVisible(boolean b)
1056 for (int ii = 0; ii < pdb.chains.size(); ii++)
1058 PDBChain chain = pdb.chains.elementAt(ii);
1059 chain.isVisible = b;
1061 mainchain.isVisible = true;
1066 // ////////////////////////////////
1067 // /StructureListener
1068 public String[] getPdbFile()
1070 return new String[] { pdbentry.getFile() };
1075 public void mouseOverStructure(int pdbResNum, String chain)
1077 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1079 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1082 lastMessage = pdbResNum + chain;
1085 StringBuffer resetLastRes = new StringBuffer();
1087 StringBuffer eval = new StringBuffer();
1090 * Highlight the specified atoms in the structure.
1095 public void highlightAtoms(List<AtomSpec> atoms)
1097 if (!seqColoursReady)
1102 for (AtomSpec atom : atoms)
1104 int atomIndex = atom.getAtomIndex();
1105 if (highlightRes != null
1106 && highlightRes.contains((atomIndex - 1) + ""))
1111 highlightAtom(atomIndex);
1114 redrawneeded = true;
1119 * Highlight the atom at the specified index.
1123 protected void highlightAtom(int atomIndex)
1127 for (index = 0; index < mainchain.bonds.size(); index++)
1129 tmpBond = mainchain.bonds.elementAt(index);
1130 if (tmpBond.at1.atomIndex == atomIndex)
1132 if (highlightBond1 != null)
1134 highlightBond1.at2.isSelected = false;
1137 if (highlightBond2 != null)
1139 highlightBond2.at1.isSelected = false;
1142 highlightBond1 = null;
1143 highlightBond2 = null;
1147 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1148 highlightBond1.at2.isSelected = true;
1151 if (index != mainchain.bonds.size())
1153 highlightBond2 = mainchain.bonds.elementAt(index);
1154 highlightBond2.at1.isSelected = true;
1162 public Color getColour(int atomIndex, int pdbResNum, String chain,
1166 // if (!pdbfile.equals(pdbentry.getFile()))
1169 // return new Color(viewer.getAtomArgb(atomIndex));
1172 public void updateColours(Object source)
1175 redrawneeded = true;
1180 public void releaseReferences(Object svl)
1182 // TODO Auto-generated method stub
1187 public boolean isListeningFor(SequenceI seq)
1189 if (sequence != null)
1191 for (SequenceI s : sequence)