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, chains, pdbentry.getFile(), protocol, null);
164 if (protocol == DataSourceType.PASTE)
166 pdbentry.setFile("INLINE" + pdb.getId());
169 } catch (Exception ex)
171 ex.printStackTrace();
175 pdbentry.setId(pdb.getId());
177 ssm.addStructureViewerListener(this);
188 // JUST DEAL WITH ONE SEQUENCE FOR NOW
189 SequenceI sequence = seq[0];
191 for (int i = 0; i < pdb.getChains().size(); i++)
194 mappingDetails.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 // TODO: DNa/Pep switch
204 AlignSeq as = new AlignSeq(sequence,
205 pdb.getChains().elementAt(i).sequence,
206 pdb.getChains().elementAt(i).isNa ? AlignSeq.DNA
208 as.calcScoreMatrix();
210 PrintStream ps = new PrintStream(System.out)
213 public void print(String x)
215 mappingDetails.append(x);
219 public void println()
221 mappingDetails.append("\n");
225 as.printAlignment(ps);
227 if (as.maxscore > max)
232 pdbstart = as.seq2start;
234 seqstart = as.seq1start + sequence.getStart() - 1;
235 seqend = as.seq1end + sequence.getEnd() - 1;
238 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
239 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
242 mainchain = pdb.getChains().elementAt(maxchain);
244 mainchain.pdbstart = pdbstart;
245 mainchain.pdbend = pdbend;
246 mainchain.seqstart = seqstart;
247 mainchain.seqend = seqend;
248 mainchain.isVisible = true;
249 // mainchain.makeExactMapping(maxAlignseq, sequence);
250 // mainchain.transferRESNUMFeatures(sequence, null);
252 this.prefsize = new Dimension(getSize().width, getSize().height);
254 // Initialize the matrices to identity
255 for (int i = 0; i < 3; i++)
257 for (int j = 0; j < 3; j++)
261 idmat.addElement(i, j, 0);
262 objmat.addElement(i, j, 0);
266 idmat.addElement(i, j, 1);
267 objmat.addElement(i, j, 1);
272 addMouseMotionListener(this);
273 addMouseListener(this);
275 addKeyListener(new KeyAdapter()
278 public void keyPressed(KeyEvent evt)
292 Vector<Bond> visiblebonds;
296 seqColoursReady = false;
297 // Sort the bonds by z coord
298 visiblebonds = new Vector<Bond>();
300 for (int ii = 0; ii < pdb.getChains().size(); ii++)
302 if (pdb.getChains().elementAt(ii).isVisible)
304 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
306 for (int i = 0; i < tmp.size(); i++)
308 visiblebonds.addElement(tmp.elementAt(i));
312 seqColoursReady = true;
318 public void findWidth()
320 float[] max = new float[3];
321 float[] min = new float[3];
323 max[0] = (float) -1e30;
324 max[1] = (float) -1e30;
325 max[2] = (float) -1e30;
327 min[0] = (float) 1e30;
328 min[1] = (float) 1e30;
329 min[2] = (float) 1e30;
331 for (int ii = 0; ii < pdb.getChains().size(); ii++)
333 if (pdb.getChains().elementAt(ii).isVisible)
335 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
337 for (Bond tmp : bonds)
339 if (tmp.start[0] >= max[0])
341 max[0] = tmp.start[0];
344 if (tmp.start[1] >= max[1])
346 max[1] = tmp.start[1];
349 if (tmp.start[2] >= max[2])
351 max[2] = tmp.start[2];
354 if (tmp.start[0] <= min[0])
356 min[0] = tmp.start[0];
359 if (tmp.start[1] <= min[1])
361 min[1] = tmp.start[1];
364 if (tmp.start[2] <= min[2])
366 min[2] = tmp.start[2];
369 if (tmp.end[0] >= max[0])
374 if (tmp.end[1] >= max[1])
379 if (tmp.end[2] >= max[2])
384 if (tmp.end[0] <= min[0])
389 if (tmp.end[1] <= min[1])
394 if (tmp.end[2] <= min[2])
402 width[0] = Math.abs(max[0] - min[0]);
403 width[1] = Math.abs(max[1] - min[1]);
404 width[2] = Math.abs(max[2] - min[2]);
408 if (width[1] > width[0])
413 if (width[2] > width[1])
418 // System.out.println("Maxwidth = " + maxwidth);
421 public float findScale()
427 if (getSize().width != 0)
429 width = getSize().width;
430 height = getSize().height;
434 width = prefsize.width;
435 height = prefsize.height;
447 return (float) (dim / (1.5d * maxwidth));
450 public void findCentre()
458 // Find centre coordinate
459 for (int ii = 0; ii < pdb.getChains().size(); ii++)
461 if (pdb.getChains().elementAt(ii).isVisible)
463 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
465 bsize += bonds.size();
469 xtot = xtot + b.start[0] + b.end[0];
470 ytot = ytot + b.start[1] + b.end[1];
471 ztot = ztot + b.start[2] + b.end[2];
476 centre[0] = xtot / (2 * (float) bsize);
477 centre[1] = ytot / (2 * (float) bsize);
478 centre[2] = ztot / (2 * (float) bsize);
482 public void paint(Graphics g)
487 g.setColor(Color.white);
488 g.fillRect(0, 0, getSize().width, getSize().height);
489 g.setColor(Color.black);
490 g.setFont(new Font("Verdana", Font.BOLD, 14));
491 g.drawString(MessageManager.getString("label.error_loading_pdb_data"),
492 50, getSize().height / 2);
496 if (!seqColoursReady)
498 g.setColor(Color.black);
499 g.setFont(new Font("Verdana", Font.BOLD, 14));
500 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
501 getSize().height / 2);
505 // Only create the image at the beginning -
506 // this saves much memory usage
507 if ((img == null) || (prefsize.width != getSize().width)
508 || (prefsize.height != getSize().height))
513 prefsize.width = getSize().width;
514 prefsize.height = getSize().height;
517 img = createImage(prefsize.width, prefsize.height);
518 ig = img.getGraphics();
521 } catch (Exception ex)
523 ex.printStackTrace();
529 drawAll(ig, prefsize.width, prefsize.height);
530 redrawneeded = false;
532 if (appletToolTip != null)
534 ig.setColor(Color.red);
535 ig.drawString(appletToolTip, toolx, tooly);
538 g.drawImage(img, 0, 0, this);
543 public void drawAll(Graphics g, int width, int height)
545 ig.setColor(Color.black);
546 ig.fillRect(0, 0, width, height);
551 public void setColours(jalview.schemes.ColourSchemeI cs)
559 // This method has been taken out of PDBChain to allow
560 // Applet and Application specific sequence renderers to be used
561 void colourBySequence()
563 SequenceRenderer sr = new SequenceRenderer(ap.av);
565 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
567 boolean showFeatures = false;
568 if (ap.av.isShowSequenceFeatures())
572 fr = new jalview.appletgui.FeatureRenderer(ap.av);
575 fr.transferSettings(ap.getFeatureRenderer());
580 FeatureColourFinder finder = new FeatureColourFinder(fr);
583 if (bysequence && pdb != null)
585 for (int ii = 0; ii < pdb.getChains().size(); ii++)
587 chain = pdb.getChains().elementAt(ii);
589 for (int i = 0; i < chain.bonds.size(); i++)
591 Bond tmp = chain.bonds.elementAt(i);
592 tmp.startCol = Color.lightGray;
593 tmp.endCol = Color.lightGray;
594 if (chain != mainchain)
599 for (int s = 0; s < sequence.length; s++)
601 for (int m = 0; m < mapping.length; m++)
603 if (mapping[m].getSequence() == sequence[s])
605 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
608 pos = sequence[s].findIndex(pos);
609 tmp.startCol = sr.getResidueColour(sequence[s], pos,
612 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
615 pos = sequence[s].findIndex(pos);
616 tmp.endCol = sr.getResidueColour(sequence[s], pos,
629 public void drawScene(Graphics g)
638 zsort.sort(visiblebonds);
642 for (int i = 0; i < visiblebonds.size(); i++)
644 tmpBond = visiblebonds.elementAt(i);
646 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale)
647 + (getSize().width / 2));
648 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale)
649 + (getSize().height / 2));
651 xend = (int) (((tmpBond.end[0] - centre[0]) * scale)
652 + (getSize().width / 2));
653 yend = (int) (((centre[1] - tmpBond.end[1]) * scale)
654 + (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)
972 + (getSize().width / 2));
973 int ystart = (int) (((centre[1] - b.start[1]) * scale)
974 + (getSize().height / 2));
976 g.setColor(Color.red);
977 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
982 int xstart = (int) (((b.end[0] - centre[0]) * scale)
983 + (getSize().width / 2));
984 int ystart = (int) (((centre[1] - b.end[1]) * scale)
985 + (getSize().height / 2));
987 g.setColor(Color.red);
988 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
994 public Atom findAtom(int x, int y)
1000 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1002 PDBChain chain = pdb.getChains().elementAt(ii);
1004 Bond tmpBond = null;
1006 if (chain.isVisible)
1008 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
1010 for (int i = 0; i < bonds.size(); i++)
1012 tmpBond = bonds.elementAt(i);
1014 truex = (int) (((tmpBond.start[0] - centre[0]) * scale)
1015 + (getSize().width / 2));
1017 if (Math.abs(truex - x) <= 2)
1019 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale)
1020 + (getSize().height / 2));
1022 if (Math.abs(truey - y) <= 2)
1024 fatom = tmpBond.at1;
1031 // Still here? Maybe its the last bond
1033 truex = (int) (((tmpBond.end[0] - centre[0]) * scale)
1034 + (getSize().width / 2));
1036 if (Math.abs(truex - x) <= 2)
1038 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale)
1039 + (getSize().height / 2));
1041 if (Math.abs(truey - y) <= 2)
1043 fatom = tmpBond.at2;
1051 if (fatom != null) // )&& chain.ds != null)
1053 chain = pdb.getChains().elementAt(foundchain);
1061 public void update(Graphics g)
1066 public void highlightRes(int ii)
1068 if (!seqColoursReady)
1073 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1080 for (index = 0; index < mainchain.bonds.size(); index++)
1082 tmpBond = mainchain.bonds.elementAt(index);
1083 if (tmpBond.at1.alignmentMapping == ii - 1)
1085 if (highlightBond1 != null)
1087 highlightBond1.at2.isSelected = false;
1090 if (highlightBond2 != null)
1092 highlightBond2.at1.isSelected = false;
1095 highlightBond1 = null;
1096 highlightBond2 = null;
1100 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1101 highlightBond1.at2.isSelected = true;
1104 if (index != mainchain.bonds.size())
1106 highlightBond2 = mainchain.bonds.elementAt(index);
1107 highlightBond2.at1.isSelected = true;
1114 redrawneeded = true;
1118 public void setAllchainsVisible(boolean b)
1120 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1122 PDBChain chain = pdb.getChains().elementAt(ii);
1123 chain.isVisible = b;
1125 mainchain.isVisible = true;
1130 // ////////////////////////////////
1131 // /StructureListener
1133 public String[] getStructureFiles()
1135 return new String[] { pdbentry.getFile() };
1140 public void mouseOverStructure(int pdbResNum, String chain)
1142 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1144 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1147 lastMessage = pdbResNum + chain;
1150 StringBuffer resetLastRes = new StringBuffer();
1152 StringBuffer eval = new StringBuffer();
1155 * Highlight the specified atoms in the structure.
1160 public void highlightAtoms(List<AtomSpec> atoms)
1162 if (!seqColoursReady)
1166 for (AtomSpec atom : atoms)
1168 int atomIndex = atom.getAtomIndex();
1170 if (highlightRes != null
1171 && highlightRes.contains((atomIndex - 1) + ""))
1176 highlightAtom(atomIndex);
1179 redrawneeded = true;
1186 protected void highlightAtom(int atomIndex)
1190 for (index = 0; index < mainchain.bonds.size(); index++)
1192 tmpBond = mainchain.bonds.elementAt(index);
1193 if (tmpBond.at1.atomIndex == atomIndex)
1195 if (highlightBond1 != null)
1197 highlightBond1.at2.isSelected = false;
1200 if (highlightBond2 != null)
1202 highlightBond2.at1.isSelected = false;
1205 highlightBond1 = null;
1206 highlightBond2 = null;
1210 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1211 highlightBond1.at2.isSelected = true;
1214 if (index != mainchain.bonds.size())
1216 highlightBond2 = mainchain.bonds.elementAt(index);
1217 highlightBond2.at1.isSelected = true;
1225 public Color getColour(int atomIndex, int pdbResNum, String chain,
1229 // if (!pdbfile.equals(pdbentry.getFile()))
1232 // return new Color(viewer.getAtomArgb(atomIndex));
1236 public void updateColours(Object source)
1239 redrawneeded = true;
1244 public void releaseReferences(Object svl)
1246 // TODO Auto-generated method stub
1251 public boolean isListeningFor(SequenceI seq)
1253 if (sequence != null)
1255 for (SequenceI s : sequence)