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.DataSourceType;
30 import jalview.io.StructureFile;
31 import jalview.renderer.seqfeatures.FeatureColourFinder;
32 import jalview.structure.AtomSpec;
33 import jalview.structure.StructureListener;
34 import jalview.structure.StructureMapping;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.MessageManager;
38 import java.awt.Color;
39 import java.awt.Dimension;
40 import java.awt.Event;
42 import java.awt.Graphics;
43 import java.awt.Image;
44 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
45 import java.awt.Panel;
46 import java.awt.event.KeyAdapter;
47 import java.awt.event.KeyEvent;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.io.PrintStream;
52 import java.util.List;
53 import java.util.Vector;
55 public class AppletPDBCanvas extends Panel
56 implements MouseListener, MouseMotionListener, StructureListener
59 MCMatrix idmat = new MCMatrix(3, 3);
61 MCMatrix objmat = new MCMatrix(3, 3);
63 boolean redrawneeded = true;
73 public StructureFile pdb;
85 float[] centre = new float[3];
87 float[] width = new float[3];
97 boolean bysequence = true;
99 boolean depthcue = true;
101 boolean wire = false;
103 boolean bymolecule = false;
105 boolean zbuffer = true;
121 Font font = new Font("Helvetica", Font.PLAIN, 10);
123 public SequenceI[] sequence;
125 final StringBuffer mappingDetails = new StringBuffer();
127 String appletToolTip = null;
133 Vector<String> highlightRes;
135 boolean pdbAction = false;
137 Bond highlightBond1, highlightBond2;
139 boolean errorLoading = false;
141 boolean seqColoursReady = false;
147 StructureSelectionManager ssm;
149 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
150 String[] chains, AlignmentPanel ap, DataSourceType protocol)
154 this.pdbentry = pdbentry;
157 ssm = StructureSelectionManager
158 .getStructureSelectionManager(ap.av.applet);
162 pdb = ssm.setMapping(seq,
163 StructureSelectionManager.perChainList(chains),
164 pdbentry.getFile(), protocol);
166 if (protocol == DataSourceType.PASTE)
168 pdbentry.setFile("INLINE" + pdb.getId());
171 } catch (Exception ex)
173 ex.printStackTrace();
177 pdbentry.setId(pdb.getId());
179 ssm.addStructureViewerListener(this);
190 // JUST DEAL WITH ONE SEQUENCE FOR NOW
191 SequenceI sequence = seq[0];
193 for (int i = 0; i < pdb.getChains().size(); i++)
196 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
197 + pdb.getChains().elementAt(i).sequence
198 .getSequenceAsString());
199 mappingDetails.append("\nNo of residues = "
200 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
202 // Now lets compare the sequences to get
203 // the start and end points.
204 // Align the sequence to the pdb
205 // TODO: DNa/Pep switch
206 AlignSeq as = new AlignSeq(sequence,
207 pdb.getChains().elementAt(i).sequence,
208 pdb.getChains().elementAt(i).isNa ? AlignSeq.DNA
210 as.calcScoreMatrix();
212 PrintStream ps = new PrintStream(System.out)
215 public void print(String x)
217 mappingDetails.append(x);
221 public void println()
223 mappingDetails.append("\n");
227 as.printAlignment(ps);
229 if (as.maxscore > max)
234 pdbstart = as.seq2start;
236 seqstart = as.seq1start + sequence.getStart() - 1;
237 seqend = as.seq1end + sequence.getEnd() - 1;
240 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
241 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
244 mainchain = pdb.getChains().elementAt(maxchain);
246 mainchain.pdbstart = pdbstart;
247 mainchain.pdbend = pdbend;
248 mainchain.seqstart = seqstart;
249 mainchain.seqend = seqend;
250 mainchain.isVisible = true;
251 // mainchain.makeExactMapping(maxAlignseq, sequence);
252 // mainchain.transferRESNUMFeatures(sequence, null);
254 this.prefsize = new Dimension(getSize().width, getSize().height);
256 // Initialize the matrices to identity
257 for (int i = 0; i < 3; i++)
259 for (int j = 0; j < 3; j++)
263 idmat.addElement(i, j, 0);
264 objmat.addElement(i, j, 0);
268 idmat.addElement(i, j, 1);
269 objmat.addElement(i, j, 1);
274 addMouseMotionListener(this);
275 addMouseListener(this);
277 addKeyListener(new KeyAdapter()
280 public void keyPressed(KeyEvent evt)
294 Vector<Bond> visiblebonds;
298 seqColoursReady = false;
299 // Sort the bonds by z coord
300 visiblebonds = new Vector<Bond>();
302 for (int ii = 0; ii < pdb.getChains().size(); ii++)
304 if (pdb.getChains().elementAt(ii).isVisible)
306 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
308 for (int i = 0; i < tmp.size(); i++)
310 visiblebonds.addElement(tmp.elementAt(i));
314 seqColoursReady = true;
320 public void findWidth()
322 float[] max = new float[3];
323 float[] min = new float[3];
325 max[0] = (float) -1e30;
326 max[1] = (float) -1e30;
327 max[2] = (float) -1e30;
329 min[0] = (float) 1e30;
330 min[1] = (float) 1e30;
331 min[2] = (float) 1e30;
333 for (int ii = 0; ii < pdb.getChains().size(); ii++)
335 if (pdb.getChains().elementAt(ii).isVisible)
337 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
339 for (Bond tmp : bonds)
341 if (tmp.start[0] >= max[0])
343 max[0] = tmp.start[0];
346 if (tmp.start[1] >= max[1])
348 max[1] = tmp.start[1];
351 if (tmp.start[2] >= max[2])
353 max[2] = tmp.start[2];
356 if (tmp.start[0] <= min[0])
358 min[0] = tmp.start[0];
361 if (tmp.start[1] <= min[1])
363 min[1] = tmp.start[1];
366 if (tmp.start[2] <= min[2])
368 min[2] = tmp.start[2];
371 if (tmp.end[0] >= max[0])
376 if (tmp.end[1] >= max[1])
381 if (tmp.end[2] >= max[2])
386 if (tmp.end[0] <= min[0])
391 if (tmp.end[1] <= min[1])
396 if (tmp.end[2] <= min[2])
404 width[0] = Math.abs(max[0] - min[0]);
405 width[1] = Math.abs(max[1] - min[1]);
406 width[2] = Math.abs(max[2] - min[2]);
410 if (width[1] > width[0])
415 if (width[2] > width[1])
420 // System.out.println("Maxwidth = " + maxwidth);
423 public float findScale()
429 if (getSize().width != 0)
431 width = getSize().width;
432 height = getSize().height;
436 width = prefsize.width;
437 height = prefsize.height;
449 return (float) (dim / (1.5d * maxwidth));
452 public void findCentre()
460 // Find centre coordinate
461 for (int ii = 0; ii < pdb.getChains().size(); ii++)
463 if (pdb.getChains().elementAt(ii).isVisible)
465 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
467 bsize += bonds.size();
471 xtot = xtot + b.start[0] + b.end[0];
472 ytot = ytot + b.start[1] + b.end[1];
473 ztot = ztot + b.start[2] + b.end[2];
478 centre[0] = xtot / (2 * (float) bsize);
479 centre[1] = ytot / (2 * (float) bsize);
480 centre[2] = ztot / (2 * (float) bsize);
484 public void paint(Graphics g)
489 g.setColor(Color.white);
490 g.fillRect(0, 0, getSize().width, getSize().height);
491 g.setColor(Color.black);
492 g.setFont(new Font("Verdana", Font.BOLD, 14));
493 g.drawString(MessageManager.getString("label.error_loading_pdb_data"),
494 50, getSize().height / 2);
498 if (!seqColoursReady)
500 g.setColor(Color.black);
501 g.setFont(new Font("Verdana", Font.BOLD, 14));
502 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
503 getSize().height / 2);
507 // Only create the image at the beginning -
508 // this saves much memory usage
509 if ((img == null) || (prefsize.width != getSize().width)
510 || (prefsize.height != getSize().height))
515 prefsize.width = getSize().width;
516 prefsize.height = getSize().height;
519 img = createImage(prefsize.width, prefsize.height);
520 ig = img.getGraphics();
523 } catch (Exception ex)
525 ex.printStackTrace();
531 drawAll(ig, prefsize.width, prefsize.height);
532 redrawneeded = false;
534 if (appletToolTip != null)
536 ig.setColor(Color.red);
537 ig.drawString(appletToolTip, toolx, tooly);
540 g.drawImage(img, 0, 0, this);
545 public void drawAll(Graphics g, int width, int height)
547 ig.setColor(Color.black);
548 ig.fillRect(0, 0, width, height);
553 public void setColours(jalview.schemes.ColourSchemeI cs)
561 // This method has been taken out of PDBChain to allow
562 // Applet and Application specific sequence renderers to be used
563 void colourBySequence()
565 SequenceRenderer sr = new SequenceRenderer(ap.av);
567 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
569 boolean showFeatures = false;
570 if (ap.av.isShowSequenceFeatures())
574 fr = new jalview.appletgui.FeatureRenderer(ap.av);
577 fr.transferSettings(ap.getFeatureRenderer());
582 FeatureColourFinder finder = new FeatureColourFinder(fr);
585 if (bysequence && pdb != null)
587 for (int ii = 0; ii < pdb.getChains().size(); ii++)
589 chain = pdb.getChains().elementAt(ii);
591 for (int i = 0; i < chain.bonds.size(); i++)
593 Bond tmp = chain.bonds.elementAt(i);
594 tmp.startCol = Color.lightGray;
595 tmp.endCol = Color.lightGray;
596 if (chain != mainchain)
601 for (int s = 0; s < sequence.length; s++)
603 for (int m = 0; m < mapping.length; m++)
605 if (mapping[m].getSequence() == sequence[s])
607 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
610 pos = sequence[s].findIndex(pos);
611 tmp.startCol = sr.getResidueColour(sequence[s], pos,
614 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
617 pos = sequence[s].findIndex(pos);
618 tmp.endCol = sr.getResidueColour(sequence[s], pos,
631 public void drawScene(Graphics g)
640 zsort.sort(visiblebonds);
644 for (int i = 0; i < visiblebonds.size(); i++)
646 tmpBond = visiblebonds.elementAt(i);
648 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale)
649 + (getSize().width / 2));
650 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale)
651 + (getSize().height / 2));
653 xend = (int) (((tmpBond.end[0] - centre[0]) * scale)
654 + (getSize().width / 2));
655 yend = (int) (((centre[1] - tmpBond.end[1]) * scale)
656 + (getSize().height / 2));
658 xmid = (xend + xstart) / 2;
659 ymid = (yend + ystart) / 2;
661 if (depthcue && !bymolecule)
663 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
665 g.setColor(tmpBond.startCol.darker().darker());
666 drawLine(g, xstart, ystart, xmid, ymid);
668 g.setColor(tmpBond.endCol.darker().darker());
669 drawLine(g, xmid, ymid, xend, yend);
671 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
673 g.setColor(tmpBond.startCol.darker());
674 drawLine(g, xstart, ystart, xmid, ymid);
676 g.setColor(tmpBond.endCol.darker());
677 drawLine(g, xmid, ymid, xend, yend);
681 g.setColor(tmpBond.startCol);
682 drawLine(g, xstart, ystart, xmid, ymid);
684 g.setColor(tmpBond.endCol);
685 drawLine(g, xmid, ymid, xend, yend);
689 else if (depthcue && bymolecule)
691 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
693 g.setColor(Color.green.darker().darker());
694 drawLine(g, xstart, ystart, xend, yend);
696 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
698 g.setColor(Color.green.darker());
699 drawLine(g, xstart, ystart, xend, yend);
703 g.setColor(Color.green);
704 drawLine(g, xstart, ystart, xend, yend);
707 else if (!depthcue && !bymolecule)
709 g.setColor(tmpBond.startCol);
710 drawLine(g, xstart, ystart, xmid, ymid);
711 g.setColor(tmpBond.endCol);
712 drawLine(g, xmid, ymid, xend, yend);
716 drawLine(g, xstart, ystart, xend, yend);
719 if (highlightBond1 != null && highlightBond1 == tmpBond)
721 g.setColor(Color.white);
722 drawLine(g, xmid, ymid, xend, yend);
725 if (highlightBond2 != null && highlightBond2 == tmpBond)
727 g.setColor(Color.white);
728 drawLine(g, xstart, ystart, xmid, ymid);
734 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
738 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
740 g.drawLine(x1, y1, x2, y2);
741 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
742 g.drawLine(x1, y1 - 1, x2, y2 - 1);
746 g.setColor(g.getColor().brighter());
747 g.drawLine(x1, y1, x2, y2);
748 g.drawLine(x1 + 1, y1, x2 + 1, y2);
749 g.drawLine(x1 - 1, y1, x2 - 1, y2);
754 g.drawLine(x1, y1, x2, y2);
758 public Dimension minimumsize()
763 public Dimension preferredsize()
768 public void doKeyPressed(KeyEvent evt)
770 if (evt.getKeyCode() == KeyEvent.VK_UP)
772 scale = (float) (scale * 1.1);
776 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
778 scale = (float) (scale * 0.9);
785 public void mousePressed(MouseEvent e)
788 Atom fatom = findAtom(e.getX(), e.getY());
791 fatom.isSelected = !fatom.isSelected;
795 if (foundchain != -1)
797 PDBChain chain = pdb.getChains().elementAt(foundchain);
798 if (chain == mainchain)
800 if (fatom.alignmentMapping != -1)
802 if (highlightRes == null)
804 highlightRes = new Vector<String>();
807 final String atomString = Integer
808 .toString(fatom.alignmentMapping);
809 if (highlightRes.contains(atomString))
811 highlightRes.removeElement(atomString);
815 highlightRes.addElement(atomString);
830 public void mouseMoved(MouseEvent e)
833 if (highlightBond1 != null)
835 highlightBond1.at2.isSelected = false;
836 highlightBond2.at1.isSelected = false;
837 highlightBond1 = null;
838 highlightBond2 = null;
841 Atom fatom = findAtom(e.getX(), e.getY());
843 PDBChain chain = null;
844 if (foundchain != -1)
846 chain = pdb.getChains().elementAt(foundchain);
847 if (chain == mainchain)
849 mouseOverStructure(fatom.resNumber, chain.id);
858 appletToolTip = chain.id + ":" + fatom.resNumber + " "
865 mouseOverStructure(-1, chain != null ? chain.id : null);
866 appletToolTip = null;
873 public void mouseClicked(MouseEvent e)
878 public void mouseEntered(MouseEvent e)
883 public void mouseExited(MouseEvent e)
888 public void mouseDragged(MouseEvent evt)
895 MCMatrix objmat = new MCMatrix(3, 3);
896 objmat.setIdentity();
898 if ((evt.getModifiers() & Event.META_MASK) != 0)
900 objmat.rotatez(((mx - omx)));
904 objmat.rotatex(((omy - my)));
905 objmat.rotatey(((omx - mx)));
909 for (PDBChain chain : pdb.getChains())
911 for (Bond tmpBond : chain.bonds)
913 // Translate the bond so the centre is 0,0,0
914 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
916 // Now apply the rotation matrix
917 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
918 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
920 // Now translate back again
921 tmpBond.translate(centre[0], centre[1], centre[2]);
938 public void mouseReleased(MouseEvent evt)
944 void drawLabels(Graphics g)
947 for (PDBChain chain : pdb.getChains())
951 for (Bond tmpBond : chain.bonds)
953 if (tmpBond.at1.isSelected)
955 labelAtom(g, tmpBond, 1);
958 if (tmpBond.at2.isSelected)
960 labelAtom(g, tmpBond, 2);
967 public void labelAtom(Graphics g, Bond b, int n)
973 int xstart = (int) (((b.start[0] - centre[0]) * scale)
974 + (getSize().width / 2));
975 int ystart = (int) (((centre[1] - b.start[1]) * scale)
976 + (getSize().height / 2));
978 g.setColor(Color.red);
979 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
984 int xstart = (int) (((b.end[0] - centre[0]) * scale)
985 + (getSize().width / 2));
986 int ystart = (int) (((centre[1] - b.end[1]) * scale)
987 + (getSize().height / 2));
989 g.setColor(Color.red);
990 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
996 public Atom findAtom(int x, int y)
1002 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1004 PDBChain chain = pdb.getChains().elementAt(ii);
1006 Bond tmpBond = null;
1008 if (chain.isVisible)
1010 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
1012 for (int i = 0; i < bonds.size(); i++)
1014 tmpBond = bonds.elementAt(i);
1016 truex = (int) (((tmpBond.start[0] - centre[0]) * scale)
1017 + (getSize().width / 2));
1019 if (Math.abs(truex - x) <= 2)
1021 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale)
1022 + (getSize().height / 2));
1024 if (Math.abs(truey - y) <= 2)
1026 fatom = tmpBond.at1;
1033 // Still here? Maybe its the last bond
1035 truex = (int) (((tmpBond.end[0] - centre[0]) * scale)
1036 + (getSize().width / 2));
1038 if (Math.abs(truex - x) <= 2)
1040 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale)
1041 + (getSize().height / 2));
1043 if (Math.abs(truey - y) <= 2)
1045 fatom = tmpBond.at2;
1053 if (fatom != null) // )&& chain.ds != null)
1055 chain = pdb.getChains().elementAt(foundchain);
1063 public void update(Graphics g)
1068 public void highlightRes(int ii)
1070 if (!seqColoursReady)
1075 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1082 for (index = 0; index < mainchain.bonds.size(); index++)
1084 tmpBond = mainchain.bonds.elementAt(index);
1085 if (tmpBond.at1.alignmentMapping == ii - 1)
1087 if (highlightBond1 != null)
1089 highlightBond1.at2.isSelected = false;
1092 if (highlightBond2 != null)
1094 highlightBond2.at1.isSelected = false;
1097 highlightBond1 = null;
1098 highlightBond2 = null;
1102 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1103 highlightBond1.at2.isSelected = true;
1106 if (index != mainchain.bonds.size())
1108 highlightBond2 = mainchain.bonds.elementAt(index);
1109 highlightBond2.at1.isSelected = true;
1116 redrawneeded = true;
1120 public void setAllchainsVisible(boolean b)
1122 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1124 PDBChain chain = pdb.getChains().elementAt(ii);
1125 chain.isVisible = b;
1127 mainchain.isVisible = true;
1132 // ////////////////////////////////
1133 // /StructureListener
1135 public String[] getStructureFiles()
1137 return new String[] { pdbentry.getFile() };
1142 public void mouseOverStructure(int pdbResNum, String chain)
1144 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1146 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1149 lastMessage = pdbResNum + chain;
1152 StringBuffer resetLastRes = new StringBuffer();
1154 StringBuffer eval = new StringBuffer();
1157 * Highlight the specified atoms in the structure.
1162 public void highlightAtoms(List<AtomSpec> atoms)
1164 if (!seqColoursReady)
1168 for (AtomSpec atom : atoms)
1170 int atomIndex = atom.getAtomIndex();
1172 if (highlightRes != null
1173 && highlightRes.contains((atomIndex - 1) + ""))
1178 highlightAtom(atomIndex);
1181 redrawneeded = true;
1188 protected void highlightAtom(int atomIndex)
1192 for (index = 0; index < mainchain.bonds.size(); index++)
1194 tmpBond = mainchain.bonds.elementAt(index);
1195 if (tmpBond.at1.atomIndex == atomIndex)
1197 if (highlightBond1 != null)
1199 highlightBond1.at2.isSelected = false;
1202 if (highlightBond2 != null)
1204 highlightBond2.at1.isSelected = false;
1207 highlightBond1 = null;
1208 highlightBond2 = null;
1212 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1213 highlightBond1.at2.isSelected = true;
1216 if (index != mainchain.bonds.size())
1218 highlightBond2 = mainchain.bonds.elementAt(index);
1219 highlightBond2.at1.isSelected = true;
1227 public Color getColour(int atomIndex, int pdbResNum, String chain,
1231 // if (!pdbfile.equals(pdbentry.getFile()))
1234 // return new Color(viewer.getAtomArgb(atomIndex));
1238 public void updateColours(Object source)
1241 redrawneeded = true;
1246 public void releaseReferences(Object svl)
1248 // TODO Auto-generated method stub
1253 public boolean isListeningFor(SequenceI seq)
1255 if (sequence != null)
1257 for (SequenceI s : sequence)