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.appletgui.AlignmentPanel;
25 import jalview.appletgui.FeatureRenderer;
26 import jalview.appletgui.SequenceRenderer;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.StructureFile;
30 import jalview.structure.AtomSpec;
31 import jalview.structure.StructureListener;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureSelectionManager;
34 import jalview.util.MessageManager;
36 import java.awt.Color;
37 import java.awt.Dimension;
38 import java.awt.Event;
40 import java.awt.Graphics;
41 import java.awt.Image;
42 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
43 import java.awt.Panel;
44 import java.awt.event.KeyAdapter;
45 import java.awt.event.KeyEvent;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
49 import java.io.PrintStream;
50 import java.util.List;
51 import java.util.Vector;
53 public class AppletPDBCanvas extends Panel implements MouseListener,
54 MouseMotionListener, StructureListener
57 MCMatrix idmat = new MCMatrix(3, 3);
59 MCMatrix objmat = new MCMatrix(3, 3);
61 boolean redrawneeded = true;
71 public StructureFile pdb;
83 float[] centre = new float[3];
85 float[] width = new float[3];
95 boolean bysequence = true;
97 boolean depthcue = true;
101 boolean bymolecule = false;
103 boolean zbuffer = true;
119 Font font = new Font("Helvetica", Font.PLAIN, 10);
121 public SequenceI[] sequence;
123 final StringBuffer mappingDetails = new StringBuffer();
125 String appletToolTip = null;
131 Vector<String> highlightRes;
133 boolean pdbAction = false;
135 Bond highlightBond1, highlightBond2;
137 boolean errorLoading = false;
139 boolean seqColoursReady = false;
145 StructureSelectionManager ssm;
147 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
148 String[] chains, AlignmentPanel ap, String protocol)
152 this.pdbentry = pdbentry;
155 ssm = StructureSelectionManager
156 .getStructureSelectionManager(ap.av.applet);
160 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
162 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
164 pdbentry.setFile("INLINE" + pdb.getId());
167 } catch (Exception ex)
169 ex.printStackTrace();
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++)
192 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
193 + pdb.getChains().elementAt(i).sequence.getSequenceAsString());
194 mappingDetails.append("\nNo of residues = "
195 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
197 // Now lets compare the sequences to get
198 // the start and end points.
199 // Align the sequence to the pdb
200 // TODO: DNa/Pep switch
201 AlignSeq as = new AlignSeq(sequence,
202 pdb.getChains().elementAt(i).sequence,
203 pdb.getChains().elementAt(i).isNa ? AlignSeq.DNA : AlignSeq.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;
245 // mainchain.makeExactMapping(maxAlignseq, sequence);
246 // mainchain.transferRESNUMFeatures(sequence, null);
248 this.prefsize = new Dimension(getSize().width, getSize().height);
250 // Initialize the matrices to identity
251 for (int i = 0; i < 3; i++)
253 for (int j = 0; j < 3; j++)
257 idmat.addElement(i, j, 0);
258 objmat.addElement(i, j, 0);
262 idmat.addElement(i, j, 1);
263 objmat.addElement(i, j, 1);
268 addMouseMotionListener(this);
269 addMouseListener(this);
271 addKeyListener(new KeyAdapter()
274 public void keyPressed(KeyEvent evt)
288 Vector<Bond> visiblebonds;
292 seqColoursReady = false;
293 // Sort the bonds by z coord
294 visiblebonds = new Vector<Bond>();
296 for (int ii = 0; ii < pdb.getChains().size(); ii++)
298 if (pdb.getChains().elementAt(ii).isVisible)
300 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
302 for (int i = 0; i < tmp.size(); i++)
304 visiblebonds.addElement(tmp.elementAt(i));
308 seqColoursReady = true;
314 public void findWidth()
316 float[] max = new float[3];
317 float[] min = new float[3];
319 max[0] = (float) -1e30;
320 max[1] = (float) -1e30;
321 max[2] = (float) -1e30;
323 min[0] = (float) 1e30;
324 min[1] = (float) 1e30;
325 min[2] = (float) 1e30;
327 for (int ii = 0; ii < pdb.getChains().size(); ii++)
329 if (pdb.getChains().elementAt(ii).isVisible)
331 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
333 for (Bond tmp : bonds)
335 if (tmp.start[0] >= max[0])
337 max[0] = tmp.start[0];
340 if (tmp.start[1] >= max[1])
342 max[1] = tmp.start[1];
345 if (tmp.start[2] >= max[2])
347 max[2] = tmp.start[2];
350 if (tmp.start[0] <= min[0])
352 min[0] = tmp.start[0];
355 if (tmp.start[1] <= min[1])
357 min[1] = tmp.start[1];
360 if (tmp.start[2] <= min[2])
362 min[2] = tmp.start[2];
365 if (tmp.end[0] >= max[0])
370 if (tmp.end[1] >= max[1])
375 if (tmp.end[2] >= max[2])
380 if (tmp.end[0] <= min[0])
385 if (tmp.end[1] <= min[1])
390 if (tmp.end[2] <= min[2])
398 width[0] = Math.abs(max[0] - min[0]);
399 width[1] = Math.abs(max[1] - min[1]);
400 width[2] = Math.abs(max[2] - min[2]);
404 if (width[1] > width[0])
409 if (width[2] > width[1])
414 // System.out.println("Maxwidth = " + maxwidth);
417 public float findScale()
423 if (getSize().width != 0)
425 width = getSize().width;
426 height = getSize().height;
430 width = prefsize.width;
431 height = prefsize.height;
443 return (float) (dim / (1.5d * maxwidth));
446 public void findCentre()
454 // Find centre coordinate
455 for (int ii = 0; ii < pdb.getChains().size(); ii++)
457 if (pdb.getChains().elementAt(ii).isVisible)
459 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
461 bsize += bonds.size();
465 xtot = xtot + b.start[0] + b.end[0];
466 ytot = ytot + b.start[1] + b.end[1];
467 ztot = ztot + b.start[2] + b.end[2];
472 centre[0] = xtot / (2 * (float) bsize);
473 centre[1] = ytot / (2 * (float) bsize);
474 centre[2] = ztot / (2 * (float) bsize);
478 public void paint(Graphics g)
483 g.setColor(Color.white);
484 g.fillRect(0, 0, getSize().width, getSize().height);
485 g.setColor(Color.black);
486 g.setFont(new Font("Verdana", Font.BOLD, 14));
488 MessageManager.getString("label.error_loading_pdb_data"), 50,
489 getSize().height / 2);
493 if (!seqColoursReady)
495 g.setColor(Color.black);
496 g.setFont(new Font("Verdana", Font.BOLD, 14));
497 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
498 getSize().height / 2);
502 // Only create the image at the beginning -
503 // this saves much memory usage
504 if ((img == null) || (prefsize.width != getSize().width)
505 || (prefsize.height != getSize().height))
510 prefsize.width = getSize().width;
511 prefsize.height = getSize().height;
514 img = createImage(prefsize.width, prefsize.height);
515 ig = img.getGraphics();
518 } catch (Exception ex)
520 ex.printStackTrace();
526 drawAll(ig, prefsize.width, prefsize.height);
527 redrawneeded = false;
529 if (appletToolTip != null)
531 ig.setColor(Color.red);
532 ig.drawString(appletToolTip, toolx, tooly);
535 g.drawImage(img, 0, 0, this);
540 public void drawAll(Graphics g, int width, int height)
542 ig.setColor(Color.black);
543 ig.fillRect(0, 0, width, height);
548 public void setColours(jalview.schemes.ColourSchemeI cs)
556 // This method has been taken out of PDBChain to allow
557 // Applet and Application specific sequence renderers to be used
558 void colourBySequence()
560 SequenceRenderer sr = new SequenceRenderer(ap.av);
562 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
564 boolean showFeatures = false;
565 if (ap.av.isShowSequenceFeatures())
569 fr = new jalview.appletgui.FeatureRenderer(ap.av);
572 fr.transferSettings(ap.getFeatureRenderer());
578 if (bysequence && pdb != null)
580 for (int ii = 0; ii < pdb.getChains().size(); ii++)
582 chain = pdb.getChains().elementAt(ii);
584 for (int i = 0; i < chain.bonds.size(); i++)
586 Bond tmp = chain.bonds.elementAt(i);
587 tmp.startCol = Color.lightGray;
588 tmp.endCol = Color.lightGray;
589 if (chain != mainchain)
594 for (int s = 0; s < sequence.length; s++)
596 for (int m = 0; m < mapping.length; m++)
598 if (mapping[m].getSequence() == sequence[s])
600 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
603 pos = sequence[s].findIndex(pos);
604 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
607 tmp.startCol = fr.findFeatureColour(tmp.startCol,
611 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
614 pos = sequence[s].findIndex(pos);
615 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
618 tmp.endCol = fr.findFeatureColour(tmp.endCol,
633 public void drawScene(Graphics g)
642 zsort.sort(visiblebonds);
646 for (int i = 0; i < visiblebonds.size(); i++)
648 tmpBond = visiblebonds.elementAt(i);
650 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
651 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
653 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
654 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
656 xmid = (xend + xstart) / 2;
657 ymid = (yend + ystart) / 2;
659 if (depthcue && !bymolecule)
661 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
663 g.setColor(tmpBond.startCol.darker().darker());
664 drawLine(g, xstart, ystart, xmid, ymid);
666 g.setColor(tmpBond.endCol.darker().darker());
667 drawLine(g, xmid, ymid, xend, yend);
669 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
671 g.setColor(tmpBond.startCol.darker());
672 drawLine(g, xstart, ystart, xmid, ymid);
674 g.setColor(tmpBond.endCol.darker());
675 drawLine(g, xmid, ymid, xend, yend);
679 g.setColor(tmpBond.startCol);
680 drawLine(g, xstart, ystart, xmid, ymid);
682 g.setColor(tmpBond.endCol);
683 drawLine(g, xmid, ymid, xend, yend);
687 else if (depthcue && bymolecule)
689 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
691 g.setColor(Color.green.darker().darker());
692 drawLine(g, xstart, ystart, xend, yend);
694 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
696 g.setColor(Color.green.darker());
697 drawLine(g, xstart, ystart, xend, yend);
701 g.setColor(Color.green);
702 drawLine(g, xstart, ystart, xend, yend);
705 else if (!depthcue && !bymolecule)
707 g.setColor(tmpBond.startCol);
708 drawLine(g, xstart, ystart, xmid, ymid);
709 g.setColor(tmpBond.endCol);
710 drawLine(g, xmid, ymid, xend, yend);
714 drawLine(g, xstart, ystart, xend, yend);
717 if (highlightBond1 != null && highlightBond1 == tmpBond)
719 g.setColor(Color.white);
720 drawLine(g, xmid, ymid, xend, yend);
723 if (highlightBond2 != null && highlightBond2 == tmpBond)
725 g.setColor(Color.white);
726 drawLine(g, xstart, ystart, xmid, ymid);
732 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
736 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
738 g.drawLine(x1, y1, x2, y2);
739 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
740 g.drawLine(x1, y1 - 1, x2, y2 - 1);
744 g.setColor(g.getColor().brighter());
745 g.drawLine(x1, y1, x2, y2);
746 g.drawLine(x1 + 1, y1, x2 + 1, y2);
747 g.drawLine(x1 - 1, y1, x2 - 1, y2);
752 g.drawLine(x1, y1, x2, y2);
756 public Dimension minimumsize()
761 public Dimension preferredsize()
766 public void doKeyPressed(KeyEvent evt)
768 if (evt.getKeyCode() == KeyEvent.VK_UP)
770 scale = (float) (scale * 1.1);
774 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
776 scale = (float) (scale * 0.9);
783 public void mousePressed(MouseEvent e)
786 Atom fatom = findAtom(e.getX(), e.getY());
789 fatom.isSelected = !fatom.isSelected;
793 if (foundchain != -1)
795 PDBChain chain = pdb.getChains().elementAt(foundchain);
796 if (chain == mainchain)
798 if (fatom.alignmentMapping != -1)
800 if (highlightRes == null)
802 highlightRes = new Vector<String>();
805 final String atomString = Integer
806 .toString(fatom.alignmentMapping);
807 if (highlightRes.contains(atomString))
809 highlightRes.removeElement(atomString);
813 highlightRes.addElement(atomString);
828 public void mouseMoved(MouseEvent e)
831 if (highlightBond1 != null)
833 highlightBond1.at2.isSelected = false;
834 highlightBond2.at1.isSelected = false;
835 highlightBond1 = null;
836 highlightBond2 = null;
839 Atom fatom = findAtom(e.getX(), e.getY());
841 PDBChain chain = null;
842 if (foundchain != -1)
844 chain = pdb.getChains().elementAt(foundchain);
845 if (chain == mainchain)
847 mouseOverStructure(fatom.resNumber, chain.id);
856 appletToolTip = chain.id + ":" + fatom.resNumber + " "
863 mouseOverStructure(-1, chain != null ? chain.id : null);
864 appletToolTip = null;
871 public void mouseClicked(MouseEvent e)
876 public void mouseEntered(MouseEvent e)
881 public void mouseExited(MouseEvent e)
886 public void mouseDragged(MouseEvent evt)
893 MCMatrix objmat = new MCMatrix(3, 3);
894 objmat.setIdentity();
896 if ((evt.getModifiers() & Event.META_MASK) != 0)
898 objmat.rotatez(((mx - omx)));
902 objmat.rotatex(((omy - my)));
903 objmat.rotatey(((omx - mx)));
907 for (PDBChain chain : pdb.getChains())
909 for (Bond tmpBond : chain.bonds)
911 // Translate the bond so the centre is 0,0,0
912 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
914 // Now apply the rotation matrix
915 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
916 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
918 // Now translate back again
919 tmpBond.translate(centre[0], centre[1], centre[2]);
936 public void mouseReleased(MouseEvent evt)
942 void drawLabels(Graphics g)
945 for (PDBChain chain : pdb.getChains())
949 for (Bond tmpBond : chain.bonds)
951 if (tmpBond.at1.isSelected)
953 labelAtom(g, tmpBond, 1);
956 if (tmpBond.at2.isSelected)
958 labelAtom(g, tmpBond, 2);
965 public void labelAtom(Graphics g, Bond b, int n)
971 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
972 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
974 g.setColor(Color.red);
975 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
980 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
981 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
983 g.setColor(Color.red);
984 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
990 public Atom findAtom(int x, int y)
996 for (int ii = 0; ii < pdb.getChains().size(); ii++)
998 PDBChain chain = pdb.getChains().elementAt(ii);
1000 Bond tmpBond = null;
1002 if (chain.isVisible)
1004 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
1006 for (int i = 0; i < bonds.size(); i++)
1008 tmpBond = bonds.elementAt(i);
1010 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
1012 if (Math.abs(truex - x) <= 2)
1014 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1016 if (Math.abs(truey - y) <= 2)
1018 fatom = tmpBond.at1;
1025 // Still here? Maybe its the last bond
1027 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1029 if (Math.abs(truex - x) <= 2)
1031 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1033 if (Math.abs(truey - y) <= 2)
1035 fatom = tmpBond.at2;
1043 if (fatom != null) // )&& chain.ds != null)
1045 chain = pdb.getChains().elementAt(foundchain);
1053 public void update(Graphics g)
1058 public void highlightRes(int ii)
1060 if (!seqColoursReady)
1065 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1072 for (index = 0; index < mainchain.bonds.size(); index++)
1074 tmpBond = mainchain.bonds.elementAt(index);
1075 if (tmpBond.at1.alignmentMapping == ii - 1)
1077 if (highlightBond1 != null)
1079 highlightBond1.at2.isSelected = false;
1082 if (highlightBond2 != null)
1084 highlightBond2.at1.isSelected = false;
1087 highlightBond1 = null;
1088 highlightBond2 = null;
1092 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1093 highlightBond1.at2.isSelected = true;
1096 if (index != mainchain.bonds.size())
1098 highlightBond2 = mainchain.bonds.elementAt(index);
1099 highlightBond2.at1.isSelected = true;
1106 redrawneeded = true;
1110 public void setAllchainsVisible(boolean b)
1112 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1114 PDBChain chain = pdb.getChains().elementAt(ii);
1115 chain.isVisible = b;
1117 mainchain.isVisible = true;
1122 // ////////////////////////////////
1123 // /StructureListener
1125 public String[] getPdbFile()
1127 return new String[] { pdbentry.getFile() };
1132 public void mouseOverStructure(int pdbResNum, String chain)
1134 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1136 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1139 lastMessage = pdbResNum + chain;
1142 StringBuffer resetLastRes = new StringBuffer();
1144 StringBuffer eval = new StringBuffer();
1147 * Highlight the specified atoms in the structure.
1152 public void highlightAtoms(List<AtomSpec> atoms)
1154 if (!seqColoursReady)
1158 for (AtomSpec atom : atoms)
1160 int atomIndex = atom.getAtomIndex();
1162 if (highlightRes != null
1163 && highlightRes.contains((atomIndex - 1) + ""))
1168 highlightAtom(atomIndex);
1171 redrawneeded = true;
1178 protected void highlightAtom(int atomIndex)
1182 for (index = 0; index < mainchain.bonds.size(); index++)
1184 tmpBond = mainchain.bonds.elementAt(index);
1185 if (tmpBond.at1.atomIndex == atomIndex)
1187 if (highlightBond1 != null)
1189 highlightBond1.at2.isSelected = false;
1192 if (highlightBond2 != null)
1194 highlightBond2.at1.isSelected = false;
1197 highlightBond1 = null;
1198 highlightBond2 = null;
1202 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1203 highlightBond1.at2.isSelected = true;
1206 if (index != mainchain.bonds.size())
1208 highlightBond2 = mainchain.bonds.elementAt(index);
1209 highlightBond2.at1.isSelected = true;
1217 public Color getColour(int atomIndex, int pdbResNum, String chain,
1221 // if (!pdbfile.equals(pdbentry.getFile()))
1224 // return new Color(viewer.getAtomArgb(atomIndex));
1228 public void updateColours(Object source)
1231 redrawneeded = true;
1236 public void releaseReferences(Object svl)
1238 // TODO Auto-generated method stub
1243 public boolean isListeningFor(SequenceI seq)
1245 if (sequence != null)
1247 for (SequenceI s : sequence)