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);
157 if (protocol.equals(jalview.io.DataSourceType.PASTE))
159 pdbentry.setFile("INLINE" + pdb.getId());
162 } catch (Exception ex)
164 ex.printStackTrace();
170 errorMessage = "Error loading file: " + pdbentry.getId();
173 pdbentry.setId(pdb.getId());
175 ssm.addStructureViewerListener(this);
186 // JUST DEAL WITH ONE SEQUENCE FOR NOW
187 SequenceI sequence = seq[0];
189 for (int i = 0; i < pdb.getChains().size(); i++)
193 .append("\n\nPDB Sequence is :\nSequence = "
194 + pdb.getChains().elementAt(i).sequence
195 .getSequenceAsString());
196 mappingDetails.append("\nNo of residues = "
197 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
199 // Now lets compare the sequences to get
200 // the start and end points.
201 // Align the sequence to the pdb
202 AlignSeq as = new AlignSeq(sequence,
203 pdb.getChains().elementAt(i).sequence, "pep");
204 as.calcScoreMatrix();
206 PrintStream ps = new PrintStream(System.out)
209 public void print(String x)
211 mappingDetails.append(x);
215 public void println()
217 mappingDetails.append("\n");
221 as.printAlignment(ps);
223 if (as.maxscore > max)
228 pdbstart = as.seq2start;
230 seqstart = as.seq1start + sequence.getStart() - 1;
231 seqend = as.seq1end + sequence.getEnd() - 1;
234 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
235 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
238 mainchain = pdb.getChains().elementAt(maxchain);
240 mainchain.pdbstart = pdbstart;
241 mainchain.pdbend = pdbend;
242 mainchain.seqstart = seqstart;
243 mainchain.seqend = seqend;
244 mainchain.isVisible = true;
247 this.prefsize = new Dimension(getSize().width, getSize().height);
249 addMouseMotionListener(this);
250 addMouseListener(this);
252 addKeyListener(new KeyAdapter()
255 public void keyPressed(KeyEvent evt)
268 ToolTipManager.sharedInstance().registerComponent(this);
269 ToolTipManager.sharedInstance().setInitialDelay(0);
270 ToolTipManager.sharedInstance().setDismissDelay(10000);
273 Vector<Bond> visiblebonds;
277 seqColoursReady = false;
278 // Sort the bonds by z coord
279 visiblebonds = new Vector<Bond>();
281 for (PDBChain chain : pdb.getChains())
285 for (Bond bond : chain.bonds)
287 visiblebonds.addElement(bond);
293 seqColoursReady = true;
298 public void findWidth()
300 float[] max = new float[3];
301 float[] min = new float[3];
303 max[0] = (float) -1e30;
304 max[1] = (float) -1e30;
305 max[2] = (float) -1e30;
307 min[0] = (float) 1e30;
308 min[1] = (float) 1e30;
309 min[2] = (float) 1e30;
311 for (PDBChain chain : pdb.getChains())
315 for (Bond tmp : chain.bonds)
317 if (tmp.start[0] >= max[0])
319 max[0] = tmp.start[0];
322 if (tmp.start[1] >= max[1])
324 max[1] = tmp.start[1];
327 if (tmp.start[2] >= max[2])
329 max[2] = tmp.start[2];
332 if (tmp.start[0] <= min[0])
334 min[0] = tmp.start[0];
337 if (tmp.start[1] <= min[1])
339 min[1] = tmp.start[1];
342 if (tmp.start[2] <= min[2])
344 min[2] = tmp.start[2];
347 if (tmp.end[0] >= max[0])
352 if (tmp.end[1] >= max[1])
357 if (tmp.end[2] >= max[2])
362 if (tmp.end[0] <= min[0])
367 if (tmp.end[1] <= min[1])
372 if (tmp.end[2] <= min[2])
380 * System.out.println("xmax " + max[0] + " min " + min[0]);
381 * System.out.println("ymax " + max[1] + " min " + min[1]);
382 * System.out.println("zmax " + max[2] + " min " + min[2]);
385 width[0] = Math.abs(max[0] - min[0]);
386 width[1] = Math.abs(max[1] - min[1]);
387 width[2] = Math.abs(max[2] - min[2]);
391 if (width[1] > width[0])
396 if (width[2] > width[1])
401 // System.out.println("Maxwidth = " + maxwidth);
404 public float findScale()
413 height = getHeight();
417 width = prefsize.width;
418 height = prefsize.height;
430 return (float) (dim / (1.5d * maxwidth));
433 public void findCentre()
441 // Find centre coordinate
442 for (PDBChain chain : pdb.getChains())
446 bsize += chain.bonds.size();
448 for (Bond bond : chain.bonds)
450 xtot = xtot + bond.start[0] + bond.end[0];
451 ytot = ytot + bond.start[1] + bond.end[1];
452 ztot = ztot + bond.start[2] + bond.end[2];
457 centre[0] = xtot / (2 * (float) bsize);
458 centre[1] = ytot / (2 * (float) bsize);
459 centre[2] = ztot / (2 * (float) bsize);
463 public void paintComponent(Graphics g)
465 super.paintComponent(g);
467 if (!seqColoursReady || errorMessage != null)
469 g.setColor(Color.black);
470 g.setFont(new Font("Verdana", Font.BOLD, 14));
471 g.drawString(errorMessage == null ? "Retrieving PDB data...."
472 : errorMessage, 20, getHeight() / 2);
476 // Only create the image at the beginning -
477 // this saves much memory usage
478 if ((img == null) || (prefsize.width != getWidth())
479 || (prefsize.height != getHeight()))
482 prefsize.width = getWidth();
483 prefsize.height = getHeight();
486 img = createImage(prefsize.width, prefsize.height);
487 ig = img.getGraphics();
488 Graphics2D ig2 = (Graphics2D) ig;
490 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
491 RenderingHints.VALUE_ANTIALIAS_ON);
498 drawAll(ig, prefsize.width, prefsize.height);
499 redrawneeded = false;
502 g.drawImage(img, 0, 0, this);
507 public void drawAll(Graphics g, int width, int height)
509 g.setColor(backgroundColour);
510 g.fillRect(0, 0, width, height);
515 public void updateSeqColours()
528 // This method has been taken out of PDBChain to allow
529 // Applet and Application specific sequence renderers to be used
530 void colourBySequence()
532 SequenceRenderer sr = new SequenceRenderer(ap.av);
534 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
536 boolean showFeatures = false;
537 if (ap.av.isShowSequenceFeatures())
541 fr = new FeatureRenderer(ap);
544 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
550 if (bysequence && pdb != null)
552 for (int ii = 0; ii < pdb.getChains().size(); ii++)
554 chain = pdb.getChains().elementAt(ii);
556 for (int i = 0; i < chain.bonds.size(); i++)
558 Bond tmp = chain.bonds.elementAt(i);
559 tmp.startCol = Color.lightGray;
560 tmp.endCol = Color.lightGray;
561 if (chain != mainchain)
566 for (int s = 0; s < sequence.length; s++)
568 for (int m = 0; m < mapping.length; m++)
570 if (mapping[m].getSequence() == sequence[s])
572 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
575 pos = sequence[s].findIndex(pos);
576 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
579 tmp.startCol = fr.findFeatureColour(tmp.startCol,
583 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
586 pos = sequence[s].findIndex(pos);
587 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
590 tmp.endCol = fr.findFeatureColour(tmp.endCol,
605 public void drawScene(Graphics g)
614 zsort.sort(visiblebonds);
618 for (int i = 0; i < visiblebonds.size(); i++)
620 tmpBond = visiblebonds.elementAt(i);
622 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
623 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
625 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
626 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
628 xmid = (xend + xstart) / 2;
629 ymid = (yend + ystart) / 2;
630 if (depthcue && !bymolecule)
632 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
635 g.setColor(tmpBond.startCol.darker().darker());
636 drawLine(g, xstart, ystart, xmid, ymid);
637 g.setColor(tmpBond.endCol.darker().darker());
638 drawLine(g, xmid, ymid, xend, yend);
641 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
643 g.setColor(tmpBond.startCol.darker());
644 drawLine(g, xstart, ystart, xmid, ymid);
646 g.setColor(tmpBond.endCol.darker());
647 drawLine(g, xmid, ymid, xend, yend);
651 g.setColor(tmpBond.startCol);
652 drawLine(g, xstart, ystart, xmid, ymid);
654 g.setColor(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(tmpBond.startCol);
679 drawLine(g, xstart, ystart, xmid, ymid);
680 g.setColor(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(tmpBond.endCol.brighter().brighter().brighter()
692 drawLine(g, xmid, ymid, xend, yend);
695 if (highlightBond2 != null && highlightBond2 == tmpBond)
697 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
699 drawLine(g, xstart, ystart, xmid, ymid);
706 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
710 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
712 g.drawLine(x1, y1, x2, y2);
713 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
714 g.drawLine(x1, y1 - 1, x2, y2 - 1);
718 g.setColor(g.getColor().brighter());
719 g.drawLine(x1, y1, x2, y2);
720 g.drawLine(x1 + 1, y1, x2 + 1, y2);
721 g.drawLine(x1 - 1, y1, x2 - 1, y2);
726 g.drawLine(x1, y1, x2, y2);
730 public Dimension minimumsize()
735 public Dimension preferredsize()
740 public void keyPressed(KeyEvent evt)
742 if (evt.getKeyCode() == KeyEvent.VK_UP)
744 scale = (float) (scale * 1.1);
748 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
750 scale = (float) (scale * 0.9);
757 public void mousePressed(MouseEvent e)
760 Atom fatom = findAtom(e.getX(), e.getY());
763 fatom.isSelected = !fatom.isSelected;
767 if (foundchain != -1)
769 PDBChain chain = pdb.getChains().elementAt(foundchain);
770 if (chain == mainchain)
772 if (fatom.alignmentMapping != -1)
774 if (highlightRes == null)
776 highlightRes = new Vector<String>();
779 final String atomString = Integer
780 .toString(fatom.alignmentMapping);
781 if (highlightRes.contains(atomString))
783 highlightRes.remove(atomString);
787 highlightRes.add(atomString);
802 public void mouseMoved(MouseEvent e)
805 if (highlightBond1 != null)
807 highlightBond1.at2.isSelected = false;
808 highlightBond2.at1.isSelected = false;
809 highlightBond1 = null;
810 highlightBond2 = null;
813 Atom fatom = findAtom(e.getX(), e.getY());
815 PDBChain chain = null;
816 if (foundchain != -1)
818 chain = pdb.getChains().elementAt(foundchain);
819 if (chain == mainchain)
821 mouseOverStructure(fatom.resNumber, chain.id);
827 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
832 mouseOverStructure(-1, chain != null ? chain.id : null);
833 this.setToolTipText(null);
838 public void mouseClicked(MouseEvent e)
843 public void mouseEntered(MouseEvent e)
848 public void mouseExited(MouseEvent e)
853 public void mouseDragged(MouseEvent evt)
860 MCMatrix objmat = new MCMatrix(3, 3);
861 objmat.setIdentity();
863 if ((evt.getModifiers() & Event.META_MASK) != 0)
865 objmat.rotatez(((mx - omx)));
869 objmat.rotatex(((my - omy)));
870 objmat.rotatey(((omx - mx)));
874 for (PDBChain chain : pdb.getChains())
876 for (Bond tmpBond : chain.bonds)
878 // Translate the bond so the centre is 0,0,0
879 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
881 // Now apply the rotation matrix
882 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
883 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
885 // Now translate back again
886 tmpBond.translate(centre[0], centre[1], centre[2]);
903 public void mouseReleased(MouseEvent evt)
909 void drawLabels(Graphics g)
912 for (PDBChain chain : pdb.getChains())
916 for (Bond tmpBond : chain.bonds)
918 if (tmpBond.at1.isSelected)
920 labelAtom(g, tmpBond, 1);
923 if (tmpBond.at2.isSelected)
925 labelAtom(g, tmpBond, 2);
932 public void labelAtom(Graphics g, Bond b, int n)
935 g.setColor(Color.red);
938 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
939 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
941 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
946 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
947 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
949 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
955 public Atom findAtom(int x, int y)
961 for (int ii = 0; ii < pdb.getChains().size(); ii++)
963 PDBChain chain = pdb.getChains().elementAt(ii);
969 for (Bond bond : chain.bonds)
973 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
975 if (Math.abs(truex - x) <= 2)
977 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
979 if (Math.abs(truey - y) <= 2)
988 // Still here? Maybe its the last bond
990 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
992 if (Math.abs(truex - x) <= 2)
994 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
996 if (Math.abs(truey - y) <= 2)
1006 if (fatom != null) // )&& chain.ds != null)
1007 { // dead code? value of chain is either overwritten or discarded
1008 chain = pdb.getChains().elementAt(foundchain);
1015 Bond highlightBond1, highlightBond2;
1017 public void highlightRes(int ii)
1019 if (!seqColoursReady)
1024 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1031 for (index = 0; index < mainchain.bonds.size(); index++)
1033 tmpBond = mainchain.bonds.elementAt(index);
1034 if (tmpBond.at1.alignmentMapping == ii - 1)
1036 if (highlightBond1 != null)
1038 highlightBond1.at2.isSelected = false;
1041 if (highlightBond2 != null)
1043 highlightBond2.at1.isSelected = false;
1046 highlightBond1 = null;
1047 highlightBond2 = null;
1051 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1052 highlightBond1.at2.isSelected = true;
1055 if (index != mainchain.bonds.size())
1057 highlightBond2 = mainchain.bonds.elementAt(index);
1058 highlightBond2.at1.isSelected = true;
1065 redrawneeded = true;
1069 public void setAllchainsVisible(boolean b)
1071 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1073 PDBChain chain = pdb.getChains().elementAt(ii);
1074 chain.isVisible = b;
1076 mainchain.isVisible = true;
1081 // ////////////////////////////////
1082 // /StructureListener
1084 public String[] getPdbFile()
1086 return new String[] { pdbentry.getFile() };
1091 public void mouseOverStructure(int pdbResNum, String chain)
1093 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1095 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1098 lastMessage = pdbResNum + chain;
1101 StringBuffer resetLastRes = new StringBuffer();
1103 StringBuffer eval = new StringBuffer();
1106 * Highlight the specified atoms in the structure.
1111 public void highlightAtoms(List<AtomSpec> atoms)
1113 if (!seqColoursReady)
1118 for (AtomSpec atom : atoms)
1120 int atomIndex = atom.getAtomIndex();
1121 if (highlightRes != null
1122 && highlightRes.contains((atomIndex - 1) + ""))
1127 highlightAtom(atomIndex);
1130 redrawneeded = true;
1135 * Highlight the atom at the specified index.
1139 protected void highlightAtom(int atomIndex)
1143 for (index = 0; index < mainchain.bonds.size(); index++)
1145 tmpBond = mainchain.bonds.elementAt(index);
1146 if (tmpBond.at1.atomIndex == atomIndex)
1148 if (highlightBond1 != null)
1150 highlightBond1.at2.isSelected = false;
1153 if (highlightBond2 != null)
1155 highlightBond2.at1.isSelected = false;
1158 highlightBond1 = null;
1159 highlightBond2 = null;
1163 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1164 highlightBond1.at2.isSelected = true;
1167 if (index != mainchain.bonds.size())
1169 highlightBond2 = mainchain.bonds.elementAt(index);
1170 highlightBond2.at1.isSelected = true;
1178 public Color getColour(int atomIndex, int pdbResNum, String chain,
1182 // if (!pdbfile.equals(pdbentry.getFile()))
1185 // return new Color(viewer.getAtomArgb(atomIndex));
1189 public void updateColours(Object source)
1192 redrawneeded = true;
1197 public void releaseReferences(Object svl)
1199 // TODO Auto-generated method stub
1204 public boolean isListeningFor(SequenceI seq)
1206 if (sequence != null)
1208 for (SequenceI s : sequence)