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.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.MessageManager;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Event;
41 import java.awt.Graphics;
42 import java.awt.Image;
43 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
44 import java.awt.Panel;
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 public class AppletPDBCanvas extends Panel implements MouseListener,
55 MouseMotionListener, StructureListener
58 MCMatrix idmat = new MCMatrix(3, 3);
60 MCMatrix objmat = new MCMatrix(3, 3);
62 boolean redrawneeded = true;
72 public StructureFile pdb;
84 float[] centre = new float[3];
86 float[] width = new float[3];
96 boolean bysequence = true;
98 boolean depthcue = true;
100 boolean wire = false;
102 boolean bymolecule = false;
104 boolean zbuffer = true;
120 Font font = new Font("Helvetica", Font.PLAIN, 10);
122 public SequenceI[] sequence;
124 final StringBuffer mappingDetails = new StringBuffer();
126 String appletToolTip = null;
132 Vector<String> highlightRes;
134 boolean pdbAction = false;
136 Bond highlightBond1, highlightBond2;
138 boolean errorLoading = false;
140 boolean seqColoursReady = false;
146 StructureSelectionManager ssm;
148 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
149 String[] chains, AlignmentPanel ap, DataSourceType protocol)
153 this.pdbentry = pdbentry;
156 ssm = StructureSelectionManager
157 .getStructureSelectionManager(ap.av.applet);
161 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
163 if (protocol == DataSourceType.PASTE)
165 pdbentry.setFile("INLINE" + pdb.getId());
168 } catch (Exception ex)
170 ex.printStackTrace();
174 pdbentry.setId(pdb.getId());
176 ssm.addStructureViewerListener(this);
187 // JUST DEAL WITH ONE SEQUENCE FOR NOW
188 SequenceI sequence = seq[0];
190 for (int i = 0; i < pdb.getChains().size(); i++)
193 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
194 + pdb.getChains().elementAt(i).sequence.getSequenceAsString());
195 mappingDetails.append("\nNo of residues = "
196 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
198 // Now lets compare the sequences to get
199 // the start and end points.
200 // Align the sequence to the pdb
201 // TODO: DNa/Pep switch
202 AlignSeq as = new AlignSeq(sequence,
203 pdb.getChains().elementAt(i).sequence,
204 pdb.getChains().elementAt(i).isNa ? AlignSeq.DNA : AlignSeq.PEP);
205 as.calcScoreMatrix();
207 PrintStream ps = new PrintStream(System.out)
210 public void print(String x)
212 mappingDetails.append(x);
216 public void println()
218 mappingDetails.append("\n");
222 as.printAlignment(ps);
224 if (as.maxscore > max)
229 pdbstart = as.seq2start;
231 seqstart = as.seq1start + sequence.getStart() - 1;
232 seqend = as.seq1end + sequence.getEnd() - 1;
235 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
236 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
239 mainchain = pdb.getChains().elementAt(maxchain);
241 mainchain.pdbstart = pdbstart;
242 mainchain.pdbend = pdbend;
243 mainchain.seqstart = seqstart;
244 mainchain.seqend = seqend;
245 mainchain.isVisible = true;
246 // mainchain.makeExactMapping(maxAlignseq, sequence);
247 // mainchain.transferRESNUMFeatures(sequence, null);
249 this.prefsize = new Dimension(getSize().width, getSize().height);
251 // Initialize the matrices to identity
252 for (int i = 0; i < 3; i++)
254 for (int j = 0; j < 3; j++)
258 idmat.addElement(i, j, 0);
259 objmat.addElement(i, j, 0);
263 idmat.addElement(i, j, 1);
264 objmat.addElement(i, j, 1);
269 addMouseMotionListener(this);
270 addMouseListener(this);
272 addKeyListener(new KeyAdapter()
275 public void keyPressed(KeyEvent evt)
289 Vector<Bond> visiblebonds;
293 seqColoursReady = false;
294 // Sort the bonds by z coord
295 visiblebonds = new Vector<Bond>();
297 for (int ii = 0; ii < pdb.getChains().size(); ii++)
299 if (pdb.getChains().elementAt(ii).isVisible)
301 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
303 for (int i = 0; i < tmp.size(); i++)
305 visiblebonds.addElement(tmp.elementAt(i));
309 seqColoursReady = true;
315 public void findWidth()
317 float[] max = new float[3];
318 float[] min = new float[3];
320 max[0] = (float) -1e30;
321 max[1] = (float) -1e30;
322 max[2] = (float) -1e30;
324 min[0] = (float) 1e30;
325 min[1] = (float) 1e30;
326 min[2] = (float) 1e30;
328 for (int ii = 0; ii < pdb.getChains().size(); ii++)
330 if (pdb.getChains().elementAt(ii).isVisible)
332 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
334 for (Bond tmp : bonds)
336 if (tmp.start[0] >= max[0])
338 max[0] = tmp.start[0];
341 if (tmp.start[1] >= max[1])
343 max[1] = tmp.start[1];
346 if (tmp.start[2] >= max[2])
348 max[2] = tmp.start[2];
351 if (tmp.start[0] <= min[0])
353 min[0] = tmp.start[0];
356 if (tmp.start[1] <= min[1])
358 min[1] = tmp.start[1];
361 if (tmp.start[2] <= min[2])
363 min[2] = tmp.start[2];
366 if (tmp.end[0] >= max[0])
371 if (tmp.end[1] >= max[1])
376 if (tmp.end[2] >= max[2])
381 if (tmp.end[0] <= min[0])
386 if (tmp.end[1] <= min[1])
391 if (tmp.end[2] <= min[2])
399 width[0] = Math.abs(max[0] - min[0]);
400 width[1] = Math.abs(max[1] - min[1]);
401 width[2] = Math.abs(max[2] - min[2]);
405 if (width[1] > width[0])
410 if (width[2] > width[1])
415 // System.out.println("Maxwidth = " + maxwidth);
418 public float findScale()
424 if (getSize().width != 0)
426 width = getSize().width;
427 height = getSize().height;
431 width = prefsize.width;
432 height = prefsize.height;
444 return (float) (dim / (1.5d * maxwidth));
447 public void findCentre()
455 // Find centre coordinate
456 for (int ii = 0; ii < pdb.getChains().size(); ii++)
458 if (pdb.getChains().elementAt(ii).isVisible)
460 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
462 bsize += bonds.size();
466 xtot = xtot + b.start[0] + b.end[0];
467 ytot = ytot + b.start[1] + b.end[1];
468 ztot = ztot + b.start[2] + b.end[2];
473 centre[0] = xtot / (2 * (float) bsize);
474 centre[1] = ytot / (2 * (float) bsize);
475 centre[2] = ztot / (2 * (float) bsize);
479 public void paint(Graphics g)
484 g.setColor(Color.white);
485 g.fillRect(0, 0, getSize().width, getSize().height);
486 g.setColor(Color.black);
487 g.setFont(new Font("Verdana", Font.BOLD, 14));
489 MessageManager.getString("label.error_loading_pdb_data"), 50,
490 getSize().height / 2);
494 if (!seqColoursReady)
496 g.setColor(Color.black);
497 g.setFont(new Font("Verdana", Font.BOLD, 14));
498 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
499 getSize().height / 2);
503 // Only create the image at the beginning -
504 // this saves much memory usage
505 if ((img == null) || (prefsize.width != getSize().width)
506 || (prefsize.height != getSize().height))
511 prefsize.width = getSize().width;
512 prefsize.height = getSize().height;
515 img = createImage(prefsize.width, prefsize.height);
516 ig = img.getGraphics();
519 } catch (Exception ex)
521 ex.printStackTrace();
527 drawAll(ig, prefsize.width, prefsize.height);
528 redrawneeded = false;
530 if (appletToolTip != null)
532 ig.setColor(Color.red);
533 ig.drawString(appletToolTip, toolx, tooly);
536 g.drawImage(img, 0, 0, this);
541 public void drawAll(Graphics g, int width, int height)
543 ig.setColor(Color.black);
544 ig.fillRect(0, 0, width, height);
549 public void setColours(jalview.schemes.ColourSchemeI cs)
557 // This method has been taken out of PDBChain to allow
558 // Applet and Application specific sequence renderers to be used
559 void colourBySequence()
561 SequenceRenderer sr = new SequenceRenderer(ap.av);
563 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
565 boolean showFeatures = false;
566 if (ap.av.isShowSequenceFeatures())
570 fr = new jalview.appletgui.FeatureRenderer(ap.av);
573 fr.transferSettings(ap.getFeatureRenderer());
579 if (bysequence && pdb != null)
581 for (int ii = 0; ii < pdb.getChains().size(); ii++)
583 chain = pdb.getChains().elementAt(ii);
585 for (int i = 0; i < chain.bonds.size(); i++)
587 Bond tmp = chain.bonds.elementAt(i);
588 tmp.startCol = Color.lightGray;
589 tmp.endCol = Color.lightGray;
590 if (chain != mainchain)
595 for (int s = 0; s < sequence.length; s++)
597 for (int m = 0; m < mapping.length; m++)
599 if (mapping[m].getSequence() == sequence[s])
601 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
604 pos = sequence[s].findIndex(pos);
605 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
608 tmp.startCol = fr.findFeatureColour(tmp.startCol,
612 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
615 pos = sequence[s].findIndex(pos);
616 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
619 tmp.endCol = fr.findFeatureColour(tmp.endCol,
634 public void drawScene(Graphics g)
643 zsort.sort(visiblebonds);
647 for (int i = 0; i < visiblebonds.size(); i++)
649 tmpBond = visiblebonds.elementAt(i);
651 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
652 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
654 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
655 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
657 xmid = (xend + xstart) / 2;
658 ymid = (yend + ystart) / 2;
660 if (depthcue && !bymolecule)
662 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
664 g.setColor(tmpBond.startCol.darker().darker());
665 drawLine(g, xstart, ystart, xmid, ymid);
667 g.setColor(tmpBond.endCol.darker().darker());
668 drawLine(g, xmid, ymid, xend, yend);
670 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
672 g.setColor(tmpBond.startCol.darker());
673 drawLine(g, xstart, ystart, xmid, ymid);
675 g.setColor(tmpBond.endCol.darker());
676 drawLine(g, xmid, ymid, xend, yend);
680 g.setColor(tmpBond.startCol);
681 drawLine(g, xstart, ystart, xmid, ymid);
683 g.setColor(tmpBond.endCol);
684 drawLine(g, xmid, ymid, xend, yend);
688 else if (depthcue && bymolecule)
690 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
692 g.setColor(Color.green.darker().darker());
693 drawLine(g, xstart, ystart, xend, yend);
695 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
697 g.setColor(Color.green.darker());
698 drawLine(g, xstart, ystart, xend, yend);
702 g.setColor(Color.green);
703 drawLine(g, xstart, ystart, xend, yend);
706 else if (!depthcue && !bymolecule)
708 g.setColor(tmpBond.startCol);
709 drawLine(g, xstart, ystart, xmid, ymid);
710 g.setColor(tmpBond.endCol);
711 drawLine(g, xmid, ymid, xend, yend);
715 drawLine(g, xstart, ystart, xend, yend);
718 if (highlightBond1 != null && highlightBond1 == tmpBond)
720 g.setColor(Color.white);
721 drawLine(g, xmid, ymid, xend, yend);
724 if (highlightBond2 != null && highlightBond2 == tmpBond)
726 g.setColor(Color.white);
727 drawLine(g, xstart, ystart, xmid, ymid);
733 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
737 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
739 g.drawLine(x1, y1, x2, y2);
740 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
741 g.drawLine(x1, y1 - 1, x2, y2 - 1);
745 g.setColor(g.getColor().brighter());
746 g.drawLine(x1, y1, x2, y2);
747 g.drawLine(x1 + 1, y1, x2 + 1, y2);
748 g.drawLine(x1 - 1, y1, x2 - 1, y2);
753 g.drawLine(x1, y1, x2, y2);
757 public Dimension minimumsize()
762 public Dimension preferredsize()
767 public void doKeyPressed(KeyEvent evt)
769 if (evt.getKeyCode() == KeyEvent.VK_UP)
771 scale = (float) (scale * 1.1);
775 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
777 scale = (float) (scale * 0.9);
784 public void mousePressed(MouseEvent e)
787 Atom fatom = findAtom(e.getX(), e.getY());
790 fatom.isSelected = !fatom.isSelected;
794 if (foundchain != -1)
796 PDBChain chain = pdb.getChains().elementAt(foundchain);
797 if (chain == mainchain)
799 if (fatom.alignmentMapping != -1)
801 if (highlightRes == null)
803 highlightRes = new Vector<String>();
806 final String atomString = Integer
807 .toString(fatom.alignmentMapping);
808 if (highlightRes.contains(atomString))
810 highlightRes.removeElement(atomString);
814 highlightRes.addElement(atomString);
829 public void mouseMoved(MouseEvent e)
832 if (highlightBond1 != null)
834 highlightBond1.at2.isSelected = false;
835 highlightBond2.at1.isSelected = false;
836 highlightBond1 = null;
837 highlightBond2 = null;
840 Atom fatom = findAtom(e.getX(), e.getY());
842 PDBChain chain = null;
843 if (foundchain != -1)
845 chain = pdb.getChains().elementAt(foundchain);
846 if (chain == mainchain)
848 mouseOverStructure(fatom.resNumber, chain.id);
857 appletToolTip = chain.id + ":" + fatom.resNumber + " "
864 mouseOverStructure(-1, chain != null ? chain.id : null);
865 appletToolTip = null;
872 public void mouseClicked(MouseEvent e)
877 public void mouseEntered(MouseEvent e)
882 public void mouseExited(MouseEvent e)
887 public void mouseDragged(MouseEvent evt)
894 MCMatrix objmat = new MCMatrix(3, 3);
895 objmat.setIdentity();
897 if ((evt.getModifiers() & Event.META_MASK) != 0)
899 objmat.rotatez(((mx - omx)));
903 objmat.rotatex(((omy - my)));
904 objmat.rotatey(((omx - mx)));
908 for (PDBChain chain : pdb.getChains())
910 for (Bond tmpBond : chain.bonds)
912 // Translate the bond so the centre is 0,0,0
913 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
915 // Now apply the rotation matrix
916 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
917 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
919 // Now translate back again
920 tmpBond.translate(centre[0], centre[1], centre[2]);
937 public void mouseReleased(MouseEvent evt)
943 void drawLabels(Graphics g)
946 for (PDBChain chain : pdb.getChains())
950 for (Bond tmpBond : chain.bonds)
952 if (tmpBond.at1.isSelected)
954 labelAtom(g, tmpBond, 1);
957 if (tmpBond.at2.isSelected)
959 labelAtom(g, tmpBond, 2);
966 public void labelAtom(Graphics g, Bond b, int n)
972 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
973 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
975 g.setColor(Color.red);
976 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
981 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
982 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
984 g.setColor(Color.red);
985 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
991 public Atom findAtom(int x, int y)
997 for (int ii = 0; ii < pdb.getChains().size(); ii++)
999 PDBChain chain = pdb.getChains().elementAt(ii);
1001 Bond tmpBond = null;
1003 if (chain.isVisible)
1005 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
1007 for (int i = 0; i < bonds.size(); i++)
1009 tmpBond = bonds.elementAt(i);
1011 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
1013 if (Math.abs(truex - x) <= 2)
1015 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1017 if (Math.abs(truey - y) <= 2)
1019 fatom = tmpBond.at1;
1026 // Still here? Maybe its the last bond
1028 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1030 if (Math.abs(truex - x) <= 2)
1032 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1034 if (Math.abs(truey - y) <= 2)
1036 fatom = tmpBond.at2;
1044 if (fatom != null) // )&& chain.ds != null)
1046 chain = pdb.getChains().elementAt(foundchain);
1054 public void update(Graphics g)
1059 public void highlightRes(int ii)
1061 if (!seqColoursReady)
1066 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1073 for (index = 0; index < mainchain.bonds.size(); index++)
1075 tmpBond = mainchain.bonds.elementAt(index);
1076 if (tmpBond.at1.alignmentMapping == ii - 1)
1078 if (highlightBond1 != null)
1080 highlightBond1.at2.isSelected = false;
1083 if (highlightBond2 != null)
1085 highlightBond2.at1.isSelected = false;
1088 highlightBond1 = null;
1089 highlightBond2 = null;
1093 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1094 highlightBond1.at2.isSelected = true;
1097 if (index != mainchain.bonds.size())
1099 highlightBond2 = mainchain.bonds.elementAt(index);
1100 highlightBond2.at1.isSelected = true;
1107 redrawneeded = true;
1111 public void setAllchainsVisible(boolean b)
1113 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1115 PDBChain chain = pdb.getChains().elementAt(ii);
1116 chain.isVisible = b;
1118 mainchain.isVisible = true;
1123 // ////////////////////////////////
1124 // /StructureListener
1126 public String[] getPdbFile()
1128 return new String[] { pdbentry.getFile() };
1133 public void mouseOverStructure(int pdbResNum, String chain)
1135 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1137 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1140 lastMessage = pdbResNum + chain;
1143 StringBuffer resetLastRes = new StringBuffer();
1145 StringBuffer eval = new StringBuffer();
1148 * Highlight the specified atoms in the structure.
1153 public void highlightAtoms(List<AtomSpec> atoms)
1155 if (!seqColoursReady)
1159 for (AtomSpec atom : atoms)
1161 int atomIndex = atom.getAtomIndex();
1163 if (highlightRes != null
1164 && highlightRes.contains((atomIndex - 1) + ""))
1169 highlightAtom(atomIndex);
1172 redrawneeded = true;
1179 protected void highlightAtom(int atomIndex)
1183 for (index = 0; index < mainchain.bonds.size(); index++)
1185 tmpBond = mainchain.bonds.elementAt(index);
1186 if (tmpBond.at1.atomIndex == atomIndex)
1188 if (highlightBond1 != null)
1190 highlightBond1.at2.isSelected = false;
1193 if (highlightBond2 != null)
1195 highlightBond2.at1.isSelected = false;
1198 highlightBond1 = null;
1199 highlightBond2 = null;
1203 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1204 highlightBond1.at2.isSelected = true;
1207 if (index != mainchain.bonds.size())
1209 highlightBond2 = mainchain.bonds.elementAt(index);
1210 highlightBond2.at1.isSelected = true;
1218 public Color getColour(int atomIndex, int pdbResNum, String chain,
1222 // if (!pdbfile.equals(pdbentry.getFile()))
1225 // return new Color(viewer.getAtomArgb(atomIndex));
1229 public void updateColours(Object source)
1232 redrawneeded = true;
1237 public void releaseReferences(Object svl)
1239 // TODO Auto-generated method stub
1244 public boolean isListeningFor(SequenceI seq)
1246 if (sequence != null)
1248 for (SequenceI s : sequence)