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.InputEvent;import java.awt.Font;
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
55 implements MouseListener, 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, null);
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
195 .getSequenceAsString());
196 mappingDetails.append("\nNo of residues = "
197 + pdb.getChains().elementAt(i).residues.size() + "\n\n");
199 // Now lets compare the sequences to get
200 // the start and end points.
201 // Align the sequence to the pdb
202 // TODO: DNa/Pep switch
203 AlignSeq as = new AlignSeq(sequence,
204 pdb.getChains().elementAt(i).sequence,
205 pdb.getChains().elementAt(i).isNa ? AlignSeq.DNA
207 as.calcScoreMatrix();
209 PrintStream ps = new PrintStream(System.out)
212 public void print(String x)
214 mappingDetails.append(x);
218 public void println()
220 mappingDetails.append("\n");
224 as.printAlignment(ps);
226 if (as.maxscore > max)
231 pdbstart = as.seq2start;
233 seqstart = as.seq1start + sequence.getStart() - 1;
234 seqend = as.seq1end + sequence.getEnd() - 1;
237 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
238 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
241 mainchain = pdb.getChains().elementAt(maxchain);
243 mainchain.pdbstart = pdbstart;
244 mainchain.pdbend = pdbend;
245 mainchain.seqstart = seqstart;
246 mainchain.seqend = seqend;
247 mainchain.isVisible = true;
248 // mainchain.makeExactMapping(maxAlignseq, sequence);
249 // mainchain.transferRESNUMFeatures(sequence, null);
251 this.prefsize = new Dimension(getSize().width, getSize().height);
253 // Initialize the matrices to identity
254 for (int i = 0; i < 3; i++)
256 for (int j = 0; j < 3; j++)
260 idmat.addElement(i, j, 0);
261 objmat.addElement(i, j, 0);
265 idmat.addElement(i, j, 1);
266 objmat.addElement(i, j, 1);
271 addMouseMotionListener(this);
272 addMouseListener(this);
274 addKeyListener(new KeyAdapter()
277 public void keyPressed(KeyEvent evt)
291 Vector<Bond> visiblebonds;
295 seqColoursReady = false;
296 // Sort the bonds by z coord
297 visiblebonds = new Vector<Bond>();
299 for (int ii = 0; ii < pdb.getChains().size(); ii++)
301 if (pdb.getChains().elementAt(ii).isVisible)
303 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
305 for (int i = 0; i < tmp.size(); i++)
307 visiblebonds.addElement(tmp.elementAt(i));
311 seqColoursReady = true;
317 public void findWidth()
319 float[] max = new float[3];
320 float[] min = new float[3];
322 max[0] = (float) -1e30;
323 max[1] = (float) -1e30;
324 max[2] = (float) -1e30;
326 min[0] = (float) 1e30;
327 min[1] = (float) 1e30;
328 min[2] = (float) 1e30;
330 for (int ii = 0; ii < pdb.getChains().size(); ii++)
332 if (pdb.getChains().elementAt(ii).isVisible)
334 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
336 for (Bond tmp : bonds)
338 if (tmp.start[0] >= max[0])
340 max[0] = tmp.start[0];
343 if (tmp.start[1] >= max[1])
345 max[1] = tmp.start[1];
348 if (tmp.start[2] >= max[2])
350 max[2] = tmp.start[2];
353 if (tmp.start[0] <= min[0])
355 min[0] = tmp.start[0];
358 if (tmp.start[1] <= min[1])
360 min[1] = tmp.start[1];
363 if (tmp.start[2] <= min[2])
365 min[2] = tmp.start[2];
368 if (tmp.end[0] >= max[0])
373 if (tmp.end[1] >= max[1])
378 if (tmp.end[2] >= max[2])
383 if (tmp.end[0] <= min[0])
388 if (tmp.end[1] <= min[1])
393 if (tmp.end[2] <= min[2])
401 width[0] = Math.abs(max[0] - min[0]);
402 width[1] = Math.abs(max[1] - min[1]);
403 width[2] = Math.abs(max[2] - min[2]);
407 if (width[1] > width[0])
412 if (width[2] > width[1])
417 // System.out.println("Maxwidth = " + maxwidth);
420 public float findScale()
426 if (getSize().width != 0)
428 width = getSize().width;
429 height = getSize().height;
433 width = prefsize.width;
434 height = prefsize.height;
446 return (float) (dim / (1.5d * maxwidth));
449 public void findCentre()
457 // Find centre coordinate
458 for (int ii = 0; ii < pdb.getChains().size(); ii++)
460 if (pdb.getChains().elementAt(ii).isVisible)
462 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
464 bsize += bonds.size();
468 xtot = xtot + b.start[0] + b.end[0];
469 ytot = ytot + b.start[1] + b.end[1];
470 ztot = ztot + b.start[2] + b.end[2];
475 centre[0] = xtot / (2 * (float) bsize);
476 centre[1] = ytot / (2 * (float) bsize);
477 centre[2] = ztot / (2 * (float) bsize);
481 public void paint(Graphics g)
486 g.setColor(Color.white);
487 g.fillRect(0, 0, getSize().width, getSize().height);
488 g.setColor(Color.black);
489 g.setFont(new Font("Verdana", Font.BOLD, 14));
490 g.drawString(MessageManager.getString("label.error_loading_pdb_data"),
491 50, getSize().height / 2);
495 if (!seqColoursReady)
497 g.setColor(Color.black);
498 g.setFont(new Font("Verdana", Font.BOLD, 14));
499 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
500 getSize().height / 2);
504 // Only create the image at the beginning -
505 // this saves much memory usage
506 if ((img == null) || (prefsize.width != getSize().width)
507 || (prefsize.height != getSize().height))
512 prefsize.width = getSize().width;
513 prefsize.height = getSize().height;
516 img = createImage(prefsize.width, prefsize.height);
517 ig = img.getGraphics();
520 } catch (Exception ex)
522 ex.printStackTrace();
528 drawAll(ig, prefsize.width, prefsize.height);
529 redrawneeded = false;
531 if (appletToolTip != null)
533 ig.setColor(Color.red);
534 ig.drawString(appletToolTip, toolx, tooly);
537 g.drawImage(img, 0, 0, this);
542 public void drawAll(Graphics g, int width, int height)
544 ig.setColor(Color.black);
545 ig.fillRect(0, 0, width, height);
550 public void setColours(jalview.schemes.ColourSchemeI cs)
558 // This method has been taken out of PDBChain to allow
559 // Applet and Application specific sequence renderers to be used
560 void colourBySequence()
562 SequenceRenderer sr = new SequenceRenderer(ap.av);
564 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
566 boolean showFeatures = false;
567 if (ap.av.isShowSequenceFeatures())
571 fr = new jalview.appletgui.FeatureRenderer(ap.av);
574 fr.transferSettings(ap.getFeatureRenderer());
579 FeatureColourFinder finder = new FeatureColourFinder(fr);
582 if (bysequence && pdb != null)
584 for (int ii = 0; ii < pdb.getChains().size(); ii++)
586 chain = pdb.getChains().elementAt(ii);
588 for (int i = 0; i < chain.bonds.size(); i++)
590 Bond tmp = chain.bonds.elementAt(i);
591 tmp.startCol = Color.lightGray;
592 tmp.endCol = Color.lightGray;
593 if (chain != mainchain)
598 for (int s = 0; s < sequence.length; s++)
600 for (int m = 0; m < mapping.length; m++)
602 if (mapping[m].getSequence() == sequence[s])
604 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
607 pos = sequence[s].findIndex(pos);
608 tmp.startCol = sr.getResidueColour(sequence[s], pos,
611 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
614 pos = sequence[s].findIndex(pos);
615 tmp.endCol = sr.getResidueColour(sequence[s], pos,
628 public void drawScene(Graphics g)
637 zsort.sort(visiblebonds);
641 for (int i = 0; i < visiblebonds.size(); i++)
643 tmpBond = visiblebonds.elementAt(i);
645 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale)
646 + (getSize().width / 2));
647 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale)
648 + (getSize().height / 2));
650 xend = (int) (((tmpBond.end[0] - centre[0]) * scale)
651 + (getSize().width / 2));
652 yend = (int) (((centre[1] - tmpBond.end[1]) * scale)
653 + (getSize().height / 2));
655 xmid = (xend + xstart) / 2;
656 ymid = (yend + ystart) / 2;
658 if (depthcue && !bymolecule)
660 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
662 g.setColor(tmpBond.startCol.darker().darker());
663 drawLine(g, xstart, ystart, xmid, ymid);
665 g.setColor(tmpBond.endCol.darker().darker());
666 drawLine(g, xmid, ymid, xend, yend);
668 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
670 g.setColor(tmpBond.startCol.darker());
671 drawLine(g, xstart, ystart, xmid, ymid);
673 g.setColor(tmpBond.endCol.darker());
674 drawLine(g, xmid, ymid, xend, yend);
678 g.setColor(tmpBond.startCol);
679 drawLine(g, xstart, ystart, xmid, ymid);
681 g.setColor(tmpBond.endCol);
682 drawLine(g, xmid, ymid, xend, yend);
686 else if (depthcue && bymolecule)
688 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
690 g.setColor(Color.green.darker().darker());
691 drawLine(g, xstart, ystart, xend, yend);
693 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
695 g.setColor(Color.green.darker());
696 drawLine(g, xstart, ystart, xend, yend);
700 g.setColor(Color.green);
701 drawLine(g, xstart, ystart, xend, yend);
704 else if (!depthcue && !bymolecule)
706 g.setColor(tmpBond.startCol);
707 drawLine(g, xstart, ystart, xmid, ymid);
708 g.setColor(tmpBond.endCol);
709 drawLine(g, xmid, ymid, xend, yend);
713 drawLine(g, xstart, ystart, xend, yend);
716 if (highlightBond1 != null && highlightBond1 == tmpBond)
718 g.setColor(Color.white);
719 drawLine(g, xmid, ymid, xend, yend);
722 if (highlightBond2 != null && highlightBond2 == tmpBond)
724 g.setColor(Color.white);
725 drawLine(g, xstart, ystart, xmid, ymid);
731 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
735 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
737 g.drawLine(x1, y1, x2, y2);
738 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
739 g.drawLine(x1, y1 - 1, x2, y2 - 1);
743 g.setColor(g.getColor().brighter());
744 g.drawLine(x1, y1, x2, y2);
745 g.drawLine(x1 + 1, y1, x2 + 1, y2);
746 g.drawLine(x1 - 1, y1, x2 - 1, y2);
751 g.drawLine(x1, y1, x2, y2);
755 public Dimension minimumsize()
760 public Dimension preferredsize()
765 public void doKeyPressed(KeyEvent evt)
767 if (evt.getKeyCode() == KeyEvent.VK_UP)
769 scale = (float) (scale * 1.1);
773 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
775 scale = (float) (scale * 0.9);
782 public void mousePressed(MouseEvent e)
785 Atom fatom = findAtom(e.getX(), e.getY());
788 fatom.isSelected = !fatom.isSelected;
792 if (foundchain != -1)
794 PDBChain chain = pdb.getChains().elementAt(foundchain);
795 if (chain == mainchain)
797 if (fatom.alignmentMapping != -1)
799 if (highlightRes == null)
801 highlightRes = new Vector<String>();
804 final String atomString = Integer
805 .toString(fatom.alignmentMapping);
806 if (highlightRes.contains(atomString))
808 highlightRes.removeElement(atomString);
812 highlightRes.addElement(atomString);
827 public void mouseMoved(MouseEvent e)
830 if (highlightBond1 != null)
832 highlightBond1.at2.isSelected = false;
833 highlightBond2.at1.isSelected = false;
834 highlightBond1 = null;
835 highlightBond2 = null;
838 Atom fatom = findAtom(e.getX(), e.getY());
840 PDBChain chain = null;
841 if (foundchain != -1)
843 chain = pdb.getChains().elementAt(foundchain);
844 if (chain == mainchain)
846 mouseOverStructure(fatom.resNumber, chain.id);
855 appletToolTip = chain.id + ":" + fatom.resNumber + " "
862 mouseOverStructure(-1, chain != null ? chain.id : null);
863 appletToolTip = null;
870 public void mouseClicked(MouseEvent e)
875 public void mouseEntered(MouseEvent e)
880 public void mouseExited(MouseEvent e)
885 public void mouseDragged(MouseEvent evt)
892 MCMatrix objmat = new MCMatrix(3, 3);
893 objmat.setIdentity();
895 if ((evt.getModifiersEx() & InputEvent.META_DOWN_MASK) != 0)
897 objmat.rotatez(((mx - omx)));
901 objmat.rotatex(((omy - my)));
902 objmat.rotatey(((omx - mx)));
906 for (PDBChain chain : pdb.getChains())
908 for (Bond tmpBond : chain.bonds)
910 // Translate the bond so the centre is 0,0,0
911 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
913 // Now apply the rotation matrix
914 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
915 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
917 // Now translate back again
918 tmpBond.translate(centre[0], centre[1], centre[2]);
935 public void mouseReleased(MouseEvent evt)
941 void drawLabels(Graphics g)
944 for (PDBChain chain : pdb.getChains())
948 for (Bond tmpBond : chain.bonds)
950 if (tmpBond.at1.isSelected)
952 labelAtom(g, tmpBond, 1);
955 if (tmpBond.at2.isSelected)
957 labelAtom(g, tmpBond, 2);
964 public void labelAtom(Graphics g, Bond b, int n)
970 int xstart = (int) (((b.start[0] - centre[0]) * scale)
971 + (getSize().width / 2));
972 int ystart = (int) (((centre[1] - b.start[1]) * scale)
973 + (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)
982 + (getSize().width / 2));
983 int ystart = (int) (((centre[1] - b.end[1]) * scale)
984 + (getSize().height / 2));
986 g.setColor(Color.red);
987 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
993 public Atom findAtom(int x, int y)
999 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1001 PDBChain chain = pdb.getChains().elementAt(ii);
1003 Bond tmpBond = null;
1005 if (chain.isVisible)
1007 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
1009 for (int i = 0; i < bonds.size(); i++)
1011 tmpBond = bonds.elementAt(i);
1013 truex = (int) (((tmpBond.start[0] - centre[0]) * scale)
1014 + (getSize().width / 2));
1016 if (Math.abs(truex - x) <= 2)
1018 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale)
1019 + (getSize().height / 2));
1021 if (Math.abs(truey - y) <= 2)
1023 fatom = tmpBond.at1;
1030 // Still here? Maybe its the last bond
1032 truex = (int) (((tmpBond.end[0] - centre[0]) * scale)
1033 + (getSize().width / 2));
1035 if (Math.abs(truex - x) <= 2)
1037 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale)
1038 + (getSize().height / 2));
1040 if (Math.abs(truey - y) <= 2)
1042 fatom = tmpBond.at2;
1050 if (fatom != null) // )&& chain.ds != null)
1052 chain = pdb.getChains().elementAt(foundchain);
1060 public void update(Graphics g)
1065 public void highlightRes(int ii)
1067 if (!seqColoursReady)
1072 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1079 for (index = 0; index < mainchain.bonds.size(); index++)
1081 tmpBond = mainchain.bonds.elementAt(index);
1082 if (tmpBond.at1.alignmentMapping == ii - 1)
1084 if (highlightBond1 != null)
1086 highlightBond1.at2.isSelected = false;
1089 if (highlightBond2 != null)
1091 highlightBond2.at1.isSelected = false;
1094 highlightBond1 = null;
1095 highlightBond2 = null;
1099 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1100 highlightBond1.at2.isSelected = true;
1103 if (index != mainchain.bonds.size())
1105 highlightBond2 = mainchain.bonds.elementAt(index);
1106 highlightBond2.at1.isSelected = true;
1113 redrawneeded = true;
1117 public void setAllchainsVisible(boolean b)
1119 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1121 PDBChain chain = pdb.getChains().elementAt(ii);
1122 chain.isVisible = b;
1124 mainchain.isVisible = true;
1129 // ////////////////////////////////
1130 // /StructureListener
1132 public String[] getStructureFiles()
1134 return new String[] { pdbentry.getFile() };
1139 public void mouseOverStructure(int pdbResNum, String chain)
1141 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1143 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1146 lastMessage = pdbResNum + chain;
1149 StringBuffer resetLastRes = new StringBuffer();
1151 StringBuffer eval = new StringBuffer();
1154 * Highlight the specified atoms in the structure.
1159 public void highlightAtoms(List<AtomSpec> atoms)
1161 if (!seqColoursReady)
1165 for (AtomSpec atom : atoms)
1167 int atomIndex = atom.getAtomIndex();
1169 if (highlightRes != null
1170 && highlightRes.contains((atomIndex - 1) + ""))
1175 highlightAtom(atomIndex);
1178 redrawneeded = true;
1185 protected void highlightAtom(int atomIndex)
1189 for (index = 0; index < mainchain.bonds.size(); index++)
1191 tmpBond = mainchain.bonds.elementAt(index);
1192 if (tmpBond.at1.atomIndex == atomIndex)
1194 if (highlightBond1 != null)
1196 highlightBond1.at2.isSelected = false;
1199 if (highlightBond2 != null)
1201 highlightBond2.at1.isSelected = false;
1204 highlightBond1 = null;
1205 highlightBond2 = null;
1209 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1210 highlightBond1.at2.isSelected = true;
1213 if (index != mainchain.bonds.size())
1215 highlightBond2 = mainchain.bonds.elementAt(index);
1216 highlightBond2.at1.isSelected = true;
1224 public Color getColour(int atomIndex, int pdbResNum, String chain,
1228 // if (!pdbfile.equals(pdbentry.getFile()))
1231 // return new Color(viewer.getAtomArgb(atomIndex));
1235 public void updateColours(Object source)
1238 redrawneeded = true;
1243 public void releaseReferences(Object svl)
1245 // TODO Auto-generated method stub
1250 public boolean isListeningFor(SequenceI seq)
1252 if (sequence != null)
1254 for (SequenceI s : sequence)