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.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
36 import java.awt.Color;
37 import java.awt.Dimension;
38 import java.awt.Event;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
43 import java.awt.Image;
44 import java.awt.RenderingHints;
45 import java.awt.event.KeyAdapter;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.io.PrintStream;
51 import java.util.List;
52 import java.util.Vector;
54 import javax.swing.JPanel;
55 import javax.swing.ToolTipManager;
57 public class PDBCanvas extends JPanel implements MouseListener,
58 MouseMotionListener, StructureListener
60 boolean redrawneeded = true;
70 public StructureFile pdb;
82 float[] centre = new float[3];
84 float[] width = new float[3];
94 boolean bysequence = true;
96 boolean depthcue = true;
100 boolean bymolecule = false;
102 boolean zbuffer = true;
118 Font font = new Font("Helvetica", Font.PLAIN, 10);
120 jalview.gui.SeqCanvas seqcanvas;
122 public SequenceI[] sequence;
124 final StringBuffer mappingDetails = new StringBuffer();
128 Vector<String> highlightRes;
130 boolean pdbAction = false;
132 boolean seqColoursReady = false;
134 jalview.renderer.seqfeatures.FeatureRenderer fr;
136 Color backgroundColour = Color.black;
140 StructureSelectionManager ssm;
144 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
145 AlignmentPanel ap, DataSourceType protocol)
148 this.pdbentry = pdbentry;
151 ssm = ap.av.getStructureSelectionManager();
155 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol,
158 if (protocol.equals(jalview.io.DataSourceType.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++)
194 .append("\n\nPDB Sequence is :\nSequence = "
195 + pdb.getChains().elementAt(i).sequence
196 .getSequenceAsString());
197 mappingDetails.append("\nNo of residues = "
198 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
200 // Now lets compare the sequences to get
201 // the start and end points.
202 // Align the sequence to the pdb
203 AlignSeq as = new AlignSeq(sequence,
204 pdb.getChains().elementAt(i).sequence, "pep");
205 as.calcScoreMatrix();
207 PrintStream ps = new PrintStream(System.out)
210 public void print(String x)
212 mappingDetails.append(x);
216 public void println()
218 mappingDetails.append("\n");
222 as.printAlignment(ps);
224 if (as.maxscore > max)
229 pdbstart = as.seq2start;
231 seqstart = as.seq1start + sequence.getStart() - 1;
232 seqend = as.seq1end + sequence.getEnd() - 1;
235 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
236 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
239 mainchain = pdb.getChains().elementAt(maxchain);
241 mainchain.pdbstart = pdbstart;
242 mainchain.pdbend = pdbend;
243 mainchain.seqstart = seqstart;
244 mainchain.seqend = seqend;
245 mainchain.isVisible = true;
248 this.prefsize = new Dimension(getSize().width, getSize().height);
250 addMouseMotionListener(this);
251 addMouseListener(this);
253 addKeyListener(new KeyAdapter()
256 public void keyPressed(KeyEvent evt)
269 ToolTipManager.sharedInstance().registerComponent(this);
270 ToolTipManager.sharedInstance().setInitialDelay(0);
271 ToolTipManager.sharedInstance().setDismissDelay(10000);
274 Vector<Bond> visiblebonds;
278 seqColoursReady = false;
279 // Sort the bonds by z coord
280 visiblebonds = new Vector<Bond>();
282 for (PDBChain chain : pdb.getChains())
286 for (Bond bond : chain.bonds)
288 visiblebonds.addElement(bond);
294 seqColoursReady = true;
299 public void findWidth()
301 float[] max = new float[3];
302 float[] min = new float[3];
304 max[0] = (float) -1e30;
305 max[1] = (float) -1e30;
306 max[2] = (float) -1e30;
308 min[0] = (float) 1e30;
309 min[1] = (float) 1e30;
310 min[2] = (float) 1e30;
312 for (PDBChain chain : pdb.getChains())
316 for (Bond tmp : chain.bonds)
318 if (tmp.start[0] >= max[0])
320 max[0] = tmp.start[0];
323 if (tmp.start[1] >= max[1])
325 max[1] = tmp.start[1];
328 if (tmp.start[2] >= max[2])
330 max[2] = tmp.start[2];
333 if (tmp.start[0] <= min[0])
335 min[0] = tmp.start[0];
338 if (tmp.start[1] <= min[1])
340 min[1] = tmp.start[1];
343 if (tmp.start[2] <= min[2])
345 min[2] = tmp.start[2];
348 if (tmp.end[0] >= max[0])
353 if (tmp.end[1] >= max[1])
358 if (tmp.end[2] >= max[2])
363 if (tmp.end[0] <= min[0])
368 if (tmp.end[1] <= min[1])
373 if (tmp.end[2] <= min[2])
381 * System.out.println("xmax " + max[0] + " min " + min[0]);
382 * System.out.println("ymax " + max[1] + " min " + min[1]);
383 * System.out.println("zmax " + max[2] + " min " + min[2]);
386 width[0] = Math.abs(max[0] - min[0]);
387 width[1] = Math.abs(max[1] - min[1]);
388 width[2] = Math.abs(max[2] - min[2]);
392 if (width[1] > width[0])
397 if (width[2] > width[1])
402 // System.out.println("Maxwidth = " + maxwidth);
405 public float findScale()
414 height = getHeight();
418 width = prefsize.width;
419 height = prefsize.height;
431 return (float) (dim / (1.5d * maxwidth));
434 public void findCentre()
442 // Find centre coordinate
443 for (PDBChain chain : pdb.getChains())
447 bsize += chain.bonds.size();
449 for (Bond bond : chain.bonds)
451 xtot = xtot + bond.start[0] + bond.end[0];
452 ytot = ytot + bond.start[1] + bond.end[1];
453 ztot = ztot + bond.start[2] + bond.end[2];
458 centre[0] = xtot / (2 * (float) bsize);
459 centre[1] = ytot / (2 * (float) bsize);
460 centre[2] = ztot / (2 * (float) bsize);
464 public void paintComponent(Graphics g)
466 super.paintComponent(g);
468 if (!seqColoursReady || errorMessage != null)
470 g.setColor(Color.black);
471 g.setFont(new Font("Verdana", Font.BOLD, 14));
472 g.drawString(errorMessage == null ? "Retrieving PDB data...."
473 : errorMessage, 20, getHeight() / 2);
477 // Only create the image at the beginning -
478 // this saves much memory usage
479 if ((img == null) || (prefsize.width != getWidth())
480 || (prefsize.height != getHeight()))
483 prefsize.width = getWidth();
484 prefsize.height = getHeight();
487 img = createImage(prefsize.width, prefsize.height);
488 ig = img.getGraphics();
489 Graphics2D ig2 = (Graphics2D) ig;
491 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
492 RenderingHints.VALUE_ANTIALIAS_ON);
499 drawAll(ig, prefsize.width, prefsize.height);
500 redrawneeded = false;
503 g.drawImage(img, 0, 0, this);
508 public void drawAll(Graphics g, int width, int height)
510 g.setColor(backgroundColour);
511 g.fillRect(0, 0, width, height);
516 public void updateSeqColours()
529 // This method has been taken out of PDBChain to allow
530 // Applet and Application specific sequence renderers to be used
531 void colourBySequence()
533 SequenceRenderer sr = new SequenceRenderer(ap.av);
535 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
537 boolean showFeatures = false;
538 if (ap.av.isShowSequenceFeatures())
542 fr = new FeatureRenderer(ap);
545 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
551 if (bysequence && pdb != null)
553 for (int ii = 0; ii < pdb.getChains().size(); ii++)
555 chain = pdb.getChains().elementAt(ii);
557 for (int i = 0; i < chain.bonds.size(); i++)
559 Bond tmp = chain.bonds.elementAt(i);
560 tmp.startCol = Color.lightGray;
561 tmp.endCol = Color.lightGray;
562 if (chain != mainchain)
567 for (int s = 0; s < sequence.length; s++)
569 for (int m = 0; m < mapping.length; m++)
571 if (mapping[m].getSequence() == sequence[s])
573 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
576 pos = sequence[s].findIndex(pos);
577 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
580 tmp.startCol = fr.findFeatureColour(tmp.startCol,
584 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
587 pos = sequence[s].findIndex(pos);
588 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
591 tmp.endCol = fr.findFeatureColour(tmp.endCol,
606 public void drawScene(Graphics g)
615 zsort.sort(visiblebonds);
619 for (int i = 0; i < visiblebonds.size(); i++)
621 tmpBond = visiblebonds.elementAt(i);
623 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
624 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
626 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
627 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
629 xmid = (xend + xstart) / 2;
630 ymid = (yend + ystart) / 2;
631 if (depthcue && !bymolecule)
633 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
636 g.setColor(tmpBond.startCol.darker().darker());
637 drawLine(g, xstart, ystart, xmid, ymid);
638 g.setColor(tmpBond.endCol.darker().darker());
639 drawLine(g, xmid, ymid, xend, yend);
642 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
644 g.setColor(tmpBond.startCol.darker());
645 drawLine(g, xstart, ystart, xmid, ymid);
647 g.setColor(tmpBond.endCol.darker());
648 drawLine(g, xmid, ymid, xend, yend);
652 g.setColor(tmpBond.startCol);
653 drawLine(g, xstart, ystart, xmid, ymid);
655 g.setColor(tmpBond.endCol);
656 drawLine(g, xmid, ymid, xend, yend);
659 else if (depthcue && bymolecule)
661 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
663 g.setColor(Color.green.darker().darker());
664 drawLine(g, xstart, ystart, xend, yend);
666 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
668 g.setColor(Color.green.darker());
669 drawLine(g, xstart, ystart, xend, yend);
673 g.setColor(Color.green);
674 drawLine(g, xstart, ystart, xend, yend);
677 else if (!depthcue && !bymolecule)
679 g.setColor(tmpBond.startCol);
680 drawLine(g, xstart, ystart, xmid, ymid);
681 g.setColor(tmpBond.endCol);
682 drawLine(g, xmid, ymid, xend, yend);
686 drawLine(g, xstart, ystart, xend, yend);
689 if (highlightBond1 != null && highlightBond1 == tmpBond)
691 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
693 drawLine(g, xmid, ymid, xend, yend);
696 if (highlightBond2 != null && highlightBond2 == tmpBond)
698 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
700 drawLine(g, xstart, ystart, xmid, ymid);
707 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
711 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
713 g.drawLine(x1, y1, x2, y2);
714 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
715 g.drawLine(x1, y1 - 1, x2, y2 - 1);
719 g.setColor(g.getColor().brighter());
720 g.drawLine(x1, y1, x2, y2);
721 g.drawLine(x1 + 1, y1, x2 + 1, y2);
722 g.drawLine(x1 - 1, y1, x2 - 1, y2);
727 g.drawLine(x1, y1, x2, y2);
731 public Dimension minimumsize()
736 public Dimension preferredsize()
741 public void keyPressed(KeyEvent evt)
743 if (evt.getKeyCode() == KeyEvent.VK_UP)
745 scale = (float) (scale * 1.1);
749 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
751 scale = (float) (scale * 0.9);
758 public void mousePressed(MouseEvent e)
761 Atom fatom = findAtom(e.getX(), e.getY());
764 fatom.isSelected = !fatom.isSelected;
768 if (foundchain != -1)
770 PDBChain chain = pdb.getChains().elementAt(foundchain);
771 if (chain == mainchain)
773 if (fatom.alignmentMapping != -1)
775 if (highlightRes == null)
777 highlightRes = new Vector<String>();
780 final String atomString = Integer
781 .toString(fatom.alignmentMapping);
782 if (highlightRes.contains(atomString))
784 highlightRes.remove(atomString);
788 highlightRes.add(atomString);
803 public void mouseMoved(MouseEvent e)
806 if (highlightBond1 != null)
808 highlightBond1.at2.isSelected = false;
809 highlightBond2.at1.isSelected = false;
810 highlightBond1 = null;
811 highlightBond2 = null;
814 Atom fatom = findAtom(e.getX(), e.getY());
816 PDBChain chain = null;
817 if (foundchain != -1)
819 chain = pdb.getChains().elementAt(foundchain);
820 if (chain == mainchain)
822 mouseOverStructure(fatom.resNumber, chain.id);
828 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
833 mouseOverStructure(-1, chain != null ? chain.id : null);
834 this.setToolTipText(null);
839 public void mouseClicked(MouseEvent e)
844 public void mouseEntered(MouseEvent e)
849 public void mouseExited(MouseEvent e)
854 public void mouseDragged(MouseEvent evt)
861 MCMatrix objmat = new MCMatrix(3, 3);
862 objmat.setIdentity();
864 if ((evt.getModifiers() & Event.META_MASK) != 0)
866 objmat.rotatez(((mx - omx)));
870 objmat.rotatex(((my - omy)));
871 objmat.rotatey(((omx - mx)));
875 for (PDBChain chain : pdb.getChains())
877 for (Bond tmpBond : chain.bonds)
879 // Translate the bond so the centre is 0,0,0
880 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
882 // Now apply the rotation matrix
883 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
884 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
886 // Now translate back again
887 tmpBond.translate(centre[0], centre[1], centre[2]);
904 public void mouseReleased(MouseEvent evt)
910 void drawLabels(Graphics g)
913 for (PDBChain chain : pdb.getChains())
917 for (Bond tmpBond : chain.bonds)
919 if (tmpBond.at1.isSelected)
921 labelAtom(g, tmpBond, 1);
924 if (tmpBond.at2.isSelected)
926 labelAtom(g, tmpBond, 2);
933 public void labelAtom(Graphics g, Bond b, int n)
936 g.setColor(Color.red);
939 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
940 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
942 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
947 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
948 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
950 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
956 public Atom findAtom(int x, int y)
962 for (int ii = 0; ii < pdb.getChains().size(); ii++)
964 PDBChain chain = pdb.getChains().elementAt(ii);
970 for (Bond bond : chain.bonds)
974 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
976 if (Math.abs(truex - x) <= 2)
978 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
980 if (Math.abs(truey - y) <= 2)
989 // Still here? Maybe its the last bond
991 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
993 if (Math.abs(truex - x) <= 2)
995 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
997 if (Math.abs(truey - y) <= 2)
1007 if (fatom != null) // )&& chain.ds != null)
1008 { // dead code? value of chain is either overwritten or discarded
1009 chain = pdb.getChains().elementAt(foundchain);
1016 Bond highlightBond1, highlightBond2;
1018 public void highlightRes(int ii)
1020 if (!seqColoursReady)
1025 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1032 for (index = 0; index < mainchain.bonds.size(); index++)
1034 tmpBond = mainchain.bonds.elementAt(index);
1035 if (tmpBond.at1.alignmentMapping == ii - 1)
1037 if (highlightBond1 != null)
1039 highlightBond1.at2.isSelected = false;
1042 if (highlightBond2 != null)
1044 highlightBond2.at1.isSelected = false;
1047 highlightBond1 = null;
1048 highlightBond2 = null;
1052 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1053 highlightBond1.at2.isSelected = true;
1056 if (index != mainchain.bonds.size())
1058 highlightBond2 = mainchain.bonds.elementAt(index);
1059 highlightBond2.at1.isSelected = true;
1066 redrawneeded = true;
1070 public void setAllchainsVisible(boolean b)
1072 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1074 PDBChain chain = pdb.getChains().elementAt(ii);
1075 chain.isVisible = b;
1077 mainchain.isVisible = true;
1082 // ////////////////////////////////
1083 // /StructureListener
1085 public String[] getStructureFiles()
1087 return new String[] { pdbentry.getFile() };
1092 public void mouseOverStructure(int pdbResNum, String chain)
1094 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1096 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1099 lastMessage = pdbResNum + chain;
1102 StringBuffer resetLastRes = new StringBuffer();
1104 StringBuffer eval = new StringBuffer();
1107 * Highlight the specified atoms in the structure.
1112 public void highlightAtoms(List<AtomSpec> atoms)
1114 if (!seqColoursReady)
1119 for (AtomSpec atom : atoms)
1121 int atomIndex = atom.getAtomIndex();
1122 if (highlightRes != null
1123 && highlightRes.contains((atomIndex - 1) + ""))
1128 highlightAtom(atomIndex);
1131 redrawneeded = true;
1136 * Highlight the atom at the specified index.
1140 protected void highlightAtom(int atomIndex)
1144 for (index = 0; index < mainchain.bonds.size(); index++)
1146 tmpBond = mainchain.bonds.elementAt(index);
1147 if (tmpBond.at1.atomIndex == atomIndex)
1149 if (highlightBond1 != null)
1151 highlightBond1.at2.isSelected = false;
1154 if (highlightBond2 != null)
1156 highlightBond2.at1.isSelected = false;
1159 highlightBond1 = null;
1160 highlightBond2 = null;
1164 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1165 highlightBond1.at2.isSelected = true;
1168 if (index != mainchain.bonds.size())
1170 highlightBond2 = mainchain.bonds.elementAt(index);
1171 highlightBond2.at1.isSelected = true;
1179 public Color getColour(int atomIndex, int pdbResNum, String chain,
1183 // if (!pdbfile.equals(pdbentry.getFile()))
1186 // return new Color(viewer.getAtomArgb(atomIndex));
1190 public void updateColours(Object source)
1193 redrawneeded = true;
1198 public void releaseReferences(Object svl)
1200 // TODO Auto-generated method stub
1205 public boolean isListeningFor(SequenceI seq)
1207 if (sequence != null)
1209 for (SequenceI s : sequence)