2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
26 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
28 import java.awt.event.*;
31 import jalview.analysis.*;
32 import jalview.datamodel.*;
34 import jalview.structure.*;
36 public class PDBCanvas extends JPanel implements MouseListener,
37 MouseMotionListener, StructureListener
39 boolean redrawneeded = true;
61 float[] centre = new float[3];
63 float[] width = new float[3];
73 boolean bysequence = true;
75 boolean depthcue = true;
79 boolean bymolecule = false;
81 boolean zbuffer = true;
97 Font font = new Font("Helvetica", Font.PLAIN, 10);
99 jalview.gui.SeqCanvas seqcanvas;
101 public SequenceI[] sequence;
103 final StringBuffer mappingDetails = new StringBuffer();
109 boolean pdbAction = false;
111 boolean seqColoursReady = false;
113 jalview.gui.FeatureRenderer fr;
115 Color backgroundColour = Color.black;
119 StructureSelectionManager ssm;
123 void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
124 AlignmentPanel ap, String protocol)
127 this.pdbentry = pdbentry;
130 ssm = ap.av.getStructureSelectionManager();
134 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
136 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
137 pdbentry.setFile("INLINE" + pdb.id);
139 } catch (Exception ex)
141 ex.printStackTrace();
147 errorMessage = "Error loading file: " + pdbentry.getId();
150 pdbentry.setId(pdb.id);
152 ssm.addStructureViewerListener(this);
163 // JUST DEAL WITH ONE SEQUENCE FOR NOW
164 SequenceI sequence = seq[0];
166 for (int i = 0; i < pdb.chains.size(); i++)
169 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
170 + ((PDBChain) pdb.chains.elementAt(i)).sequence
171 .getSequenceAsString());
172 mappingDetails.append("\nNo of residues = "
173 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
176 // Now lets compare the sequences to get
177 // the start and end points.
178 // Align the sequence to the pdb
179 AlignSeq as = new AlignSeq(sequence,
180 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
181 as.calcScoreMatrix();
183 PrintStream ps = new PrintStream(System.out)
185 public void print(String x)
187 mappingDetails.append(x);
190 public void println()
192 mappingDetails.append("\n");
196 as.printAlignment(ps);
198 if (as.maxscore > max)
203 pdbstart = as.seq2start;
205 seqstart = as.seq1start + sequence.getStart() - 1;
206 seqend = as.seq1end + sequence.getEnd() - 1;
209 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
210 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
213 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
215 mainchain.pdbstart = pdbstart;
216 mainchain.pdbend = pdbend;
217 mainchain.seqstart = seqstart;
218 mainchain.seqend = seqend;
219 mainchain.isVisible = true;
222 this.prefsize = new Dimension(getSize().width, getSize().height);
224 addMouseMotionListener(this);
225 addMouseListener(this);
227 addKeyListener(new KeyAdapter()
229 public void keyPressed(KeyEvent evt)
242 ToolTipManager.sharedInstance().registerComponent(this);
243 ToolTipManager.sharedInstance().setInitialDelay(0);
244 ToolTipManager.sharedInstance().setDismissDelay(10000);
251 seqColoursReady = false;
252 // Sort the bonds by z coord
253 visiblebonds = new Vector();
255 for (int ii = 0; ii < pdb.chains.size(); ii++)
257 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
259 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
261 for (int i = 0; i < tmp.size(); i++)
263 visiblebonds.addElement(tmp.elementAt(i));
269 seqColoursReady = true;
274 public void findWidth()
276 float[] max = new float[3];
277 float[] min = new float[3];
279 max[0] = (float) -1e30;
280 max[1] = (float) -1e30;
281 max[2] = (float) -1e30;
283 min[0] = (float) 1e30;
284 min[1] = (float) 1e30;
285 min[2] = (float) 1e30;
287 for (int ii = 0; ii < pdb.chains.size(); ii++)
289 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
291 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
293 for (int i = 0; i < bonds.size(); i++)
295 Bond tmp = (Bond) bonds.elementAt(i);
297 if (tmp.start[0] >= max[0])
299 max[0] = tmp.start[0];
302 if (tmp.start[1] >= max[1])
304 max[1] = tmp.start[1];
307 if (tmp.start[2] >= max[2])
309 max[2] = tmp.start[2];
312 if (tmp.start[0] <= min[0])
314 min[0] = tmp.start[0];
317 if (tmp.start[1] <= min[1])
319 min[1] = tmp.start[1];
322 if (tmp.start[2] <= min[2])
324 min[2] = tmp.start[2];
327 if (tmp.end[0] >= max[0])
332 if (tmp.end[1] >= max[1])
337 if (tmp.end[2] >= max[2])
342 if (tmp.end[0] <= min[0])
347 if (tmp.end[1] <= min[1])
352 if (tmp.end[2] <= min[2])
360 * System.out.println("xmax " + max[0] + " min " + min[0]);
361 * System.out.println("ymax " + max[1] + " min " + min[1]);
362 * System.out.println("zmax " + max[2] + " min " + min[2]);
365 width[0] = (float) Math.abs(max[0] - min[0]);
366 width[1] = (float) Math.abs(max[1] - min[1]);
367 width[2] = (float) Math.abs(max[2] - min[2]);
371 if (width[1] > width[0])
376 if (width[2] > width[1])
381 // System.out.println("Maxwidth = " + maxwidth);
384 public float findScale()
393 height = getHeight();
397 width = prefsize.width;
398 height = prefsize.height;
410 return (float) (dim / (1.5d * maxwidth));
413 public void findCentre()
421 // Find centre coordinate
422 for (int ii = 0; ii < pdb.chains.size(); ii++)
424 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
426 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
428 bsize += bonds.size();
430 for (int i = 0; i < bonds.size(); i++)
432 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
433 + ((Bond) bonds.elementAt(i)).end[0];
435 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
436 + ((Bond) bonds.elementAt(i)).end[1];
438 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
439 + ((Bond) bonds.elementAt(i)).end[2];
444 centre[0] = xtot / (2 * (float) bsize);
445 centre[1] = ytot / (2 * (float) bsize);
446 centre[2] = ztot / (2 * (float) bsize);
449 public void paintComponent(Graphics g)
451 super.paintComponent(g);
453 if (!seqColoursReady || errorMessage != null)
455 g.setColor(Color.black);
456 g.setFont(new Font("Verdana", Font.BOLD, 14));
457 g.drawString(errorMessage == null ? "Retrieving PDB data...."
458 : errorMessage, 20, getHeight() / 2);
462 // Only create the image at the beginning -
463 // this saves much memory usage
464 if ((img == null) || (prefsize.width != getWidth())
465 || (prefsize.height != getHeight()))
468 prefsize.width = getWidth();
469 prefsize.height = getHeight();
472 img = createImage(prefsize.width, prefsize.height);
473 ig = img.getGraphics();
474 Graphics2D ig2 = (Graphics2D) ig;
476 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
477 RenderingHints.VALUE_ANTIALIAS_ON);
484 drawAll(ig, prefsize.width, prefsize.height);
485 redrawneeded = false;
488 g.drawImage(img, 0, 0, this);
493 public void drawAll(Graphics g, int width, int height)
495 g.setColor(backgroundColour);
496 g.fillRect(0, 0, width, height);
501 public void updateSeqColours()
514 // This method has been taken out of PDBChain to allow
515 // Applet and Application specific sequence renderers to be used
516 void colourBySequence()
518 SequenceRenderer sr = new SequenceRenderer(ap.av);
520 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
522 boolean showFeatures = false;
523 if (ap.av.getShowSequenceFeatures())
527 fr = new FeatureRenderer(ap);
530 fr.transferSettings(ap.alignFrame.getFeatureRenderer());
536 if (bysequence && pdb != null)
538 for (int ii = 0; ii < pdb.chains.size(); ii++)
540 chain = (PDBChain) pdb.chains.elementAt(ii);
542 for (int i = 0; i < chain.bonds.size(); i++)
544 Bond tmp = (Bond) chain.bonds.elementAt(i);
545 tmp.startCol = Color.lightGray;
546 tmp.endCol = Color.lightGray;
547 if (chain != mainchain)
552 for (int s = 0; s < sequence.length; s++)
554 for (int m = 0; m < mapping.length; m++)
556 if (mapping[m].getSequence() == sequence[s])
558 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
561 pos = sequence[s].findIndex(pos);
562 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
565 tmp.startCol = fr.findFeatureColour(tmp.startCol,
569 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
572 pos = sequence[s].findIndex(pos);
573 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
576 tmp.endCol = fr.findFeatureColour(tmp.endCol,
591 public void drawScene(Graphics g)
600 zsort.Zsort(visiblebonds);
604 for (int i = 0; i < visiblebonds.size(); i++)
606 tmpBond = (Bond) visiblebonds.elementAt(i);
608 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
609 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
611 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
612 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
614 xmid = (xend + xstart) / 2;
615 ymid = (yend + ystart) / 2;
616 if (depthcue && !bymolecule)
618 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
621 g.setColor(tmpBond.startCol.darker().darker());
622 drawLine(g, xstart, ystart, xmid, ymid);
623 g.setColor(tmpBond.endCol.darker().darker());
624 drawLine(g, xmid, ymid, xend, yend);
627 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
629 g.setColor(tmpBond.startCol.darker());
630 drawLine(g, xstart, ystart, xmid, ymid);
632 g.setColor(tmpBond.endCol.darker());
633 drawLine(g, xmid, ymid, xend, yend);
637 g.setColor(tmpBond.startCol);
638 drawLine(g, xstart, ystart, xmid, ymid);
640 g.setColor(tmpBond.endCol);
641 drawLine(g, xmid, ymid, xend, yend);
644 else if (depthcue && bymolecule)
646 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
648 g.setColor(Color.green.darker().darker());
649 drawLine(g, xstart, ystart, xend, yend);
651 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
653 g.setColor(Color.green.darker());
654 drawLine(g, xstart, ystart, xend, yend);
658 g.setColor(Color.green);
659 drawLine(g, xstart, ystart, xend, yend);
662 else if (!depthcue && !bymolecule)
664 g.setColor(tmpBond.startCol);
665 drawLine(g, xstart, ystart, xmid, ymid);
666 g.setColor(tmpBond.endCol);
667 drawLine(g, xmid, ymid, xend, yend);
671 drawLine(g, xstart, ystart, xend, yend);
674 if (highlightBond1 != null && highlightBond1 == tmpBond)
676 g.setColor(tmpBond.endCol.brighter().brighter().brighter()
678 drawLine(g, xmid, ymid, xend, yend);
681 if (highlightBond2 != null && highlightBond2 == tmpBond)
683 g.setColor(tmpBond.startCol.brighter().brighter().brighter()
685 drawLine(g, xstart, ystart, xmid, ymid);
692 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
696 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
698 g.drawLine(x1, y1, x2, y2);
699 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
700 g.drawLine(x1, y1 - 1, x2, y2 - 1);
704 g.setColor(g.getColor().brighter());
705 g.drawLine(x1, y1, x2, y2);
706 g.drawLine(x1 + 1, y1, x2 + 1, y2);
707 g.drawLine(x1 - 1, y1, x2 - 1, y2);
712 g.drawLine(x1, y1, x2, y2);
716 public Dimension minimumsize()
721 public Dimension preferredsize()
726 public void keyPressed(KeyEvent evt)
728 if (evt.getKeyCode() == KeyEvent.VK_UP)
730 scale = (float) (scale * 1.1);
734 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
736 scale = (float) (scale * 0.9);
742 public void mousePressed(MouseEvent e)
745 Atom fatom = findAtom(e.getX(), e.getY());
748 fatom.isSelected = !fatom.isSelected;
752 if (foundchain != -1)
754 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
755 if (chain == mainchain)
757 if (fatom.alignmentMapping != -1)
759 if (highlightRes == null)
761 highlightRes = new Vector();
764 if (highlightRes.contains(fatom.alignmentMapping + ""))
766 highlightRes.remove(fatom.alignmentMapping + "");
770 highlightRes.add(fatom.alignmentMapping + "");
784 public void mouseMoved(MouseEvent e)
787 if (highlightBond1 != null)
789 highlightBond1.at2.isSelected = false;
790 highlightBond2.at1.isSelected = false;
791 highlightBond1 = null;
792 highlightBond2 = null;
795 Atom fatom = findAtom(e.getX(), e.getY());
797 PDBChain chain = null;
798 if (foundchain != -1)
800 chain = (PDBChain) pdb.chains.elementAt(foundchain);
801 if (chain == mainchain)
803 mouseOverStructure(fatom.resNumber, chain.id);
809 this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
814 mouseOverStructure(-1, chain != null ? chain.id : null);
815 this.setToolTipText(null);
819 public void mouseClicked(MouseEvent e)
823 public void mouseEntered(MouseEvent e)
827 public void mouseExited(MouseEvent e)
831 public void mouseDragged(MouseEvent evt)
838 MCMatrix objmat = new MCMatrix(3, 3);
839 objmat.setIdentity();
841 if ((evt.getModifiers() & Event.META_MASK) != 0)
843 objmat.rotatez((float) ((mx - omx)));
847 objmat.rotatex((float) ((my - omy)));
848 objmat.rotatey((float) ((omx - mx)));
852 for (int ii = 0; ii < pdb.chains.size(); ii++)
854 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
856 for (int i = 0; i < bonds.size(); i++)
858 Bond tmpBond = (Bond) bonds.elementAt(i);
860 // Translate the bond so the centre is 0,0,0
861 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
863 // Now apply the rotation matrix
864 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
865 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
867 // Now translate back again
868 tmpBond.translate(centre[0], centre[1], centre[2]);
884 public void mouseReleased(MouseEvent evt)
890 void drawLabels(Graphics g)
893 for (int ii = 0; ii < pdb.chains.size(); ii++)
895 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
899 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
901 for (int i = 0; i < bonds.size(); i++)
903 Bond tmpBond = (Bond) bonds.elementAt(i);
905 if (tmpBond.at1.isSelected)
907 labelAtom(g, tmpBond, 1);
910 if (tmpBond.at2.isSelected)
913 labelAtom(g, tmpBond, 2);
920 public void labelAtom(Graphics g, Bond b, int n)
923 g.setColor(Color.red);
926 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
927 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
929 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
934 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
935 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
937 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
943 public Atom findAtom(int x, int y)
949 for (int ii = 0; ii < pdb.chains.size(); ii++)
951 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
957 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
959 for (int i = 0; i < bonds.size(); i++)
961 tmpBond = (Bond) bonds.elementAt(i);
963 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
965 if (Math.abs(truex - x) <= 2)
967 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
969 if (Math.abs(truey - y) <= 2)
978 // Still here? Maybe its the last bond
980 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
982 if (Math.abs(truex - x) <= 2)
984 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
986 if (Math.abs(truey - y) <= 2)
996 if (fatom != null) // )&& chain.ds != null)
998 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1005 Bond highlightBond1, highlightBond2;
1007 public void highlightRes(int ii)
1009 if (!seqColoursReady)
1014 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1021 for (index = 0; index < mainchain.bonds.size(); index++)
1023 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1024 if (tmpBond.at1.alignmentMapping == ii - 1)
1026 if (highlightBond1 != null)
1028 highlightBond1.at2.isSelected = false;
1031 if (highlightBond2 != null)
1033 highlightBond2.at1.isSelected = false;
1036 highlightBond1 = null;
1037 highlightBond2 = null;
1041 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1042 highlightBond1.at2.isSelected = true;
1045 if (index != mainchain.bonds.size())
1047 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1048 highlightBond2.at1.isSelected = true;
1055 redrawneeded = true;
1059 public void setAllchainsVisible(boolean b)
1061 for (int ii = 0; ii < pdb.chains.size(); ii++)
1063 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1064 chain.isVisible = b;
1066 mainchain.isVisible = true;
1071 // ////////////////////////////////
1072 // /StructureListener
1073 public String[] getPdbFile()
1076 { pdbentry.getFile() };
1081 public void mouseOverStructure(int pdbResNum, String chain)
1083 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1084 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1086 lastMessage = pdbResNum + chain;
1089 StringBuffer resetLastRes = new StringBuffer();
1091 StringBuffer eval = new StringBuffer();
1093 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1096 if (!seqColoursReady)
1101 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1108 for (index = 0; index < mainchain.bonds.size(); index++)
1110 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1111 if (tmpBond.at1.atomIndex == atomIndex)
1113 if (highlightBond1 != null)
1115 highlightBond1.at2.isSelected = false;
1118 if (highlightBond2 != null)
1120 highlightBond2.at1.isSelected = false;
1123 highlightBond1 = null;
1124 highlightBond2 = null;
1128 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1129 highlightBond1.at2.isSelected = true;
1132 if (index != mainchain.bonds.size())
1134 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1135 highlightBond2.at1.isSelected = true;
1142 redrawneeded = true;
1146 public Color getColour(int atomIndex, int pdbResNum, String chain,
1150 // if (!pdbfile.equals(pdbentry.getFile()))
1153 // return new Color(viewer.getAtomArgb(atomIndex));
1156 public void updateColours(Object source)
1159 redrawneeded = true;
1164 public void releaseReferences(Object svl)
1166 // TODO Auto-generated method stub