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.schemes.Colour;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.ColorUtils;
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 implements MouseListener,
56 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, String protocol)
154 this.pdbentry = pdbentry;
157 ssm = StructureSelectionManager
158 .getStructureSelectionManager(ap.av.applet);
162 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
164 if (protocol.equals(jalview.io.AppletFormatAdapter.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.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 : AlignSeq.PEP);
206 as.calcScoreMatrix();
208 PrintStream ps = new PrintStream(System.out)
211 public void print(String x)
213 mappingDetails.append(x);
217 public void println()
219 mappingDetails.append("\n");
223 as.printAlignment(ps);
225 if (as.maxscore > max)
230 pdbstart = as.seq2start;
232 seqstart = as.seq1start + sequence.getStart() - 1;
233 seqend = as.seq1end + sequence.getEnd() - 1;
236 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
237 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
240 mainchain = pdb.getChains().elementAt(maxchain);
242 mainchain.pdbstart = pdbstart;
243 mainchain.pdbend = pdbend;
244 mainchain.seqstart = seqstart;
245 mainchain.seqend = seqend;
246 mainchain.isVisible = true;
247 // mainchain.makeExactMapping(maxAlignseq, sequence);
248 // mainchain.transferRESNUMFeatures(sequence, null);
250 this.prefsize = new Dimension(getSize().width, getSize().height);
252 // Initialize the matrices to identity
253 for (int i = 0; i < 3; i++)
255 for (int j = 0; j < 3; j++)
259 idmat.addElement(i, j, 0);
260 objmat.addElement(i, j, 0);
264 idmat.addElement(i, j, 1);
265 objmat.addElement(i, j, 1);
270 addMouseMotionListener(this);
271 addMouseListener(this);
273 addKeyListener(new KeyAdapter()
276 public void keyPressed(KeyEvent evt)
290 Vector<Bond> visiblebonds;
294 seqColoursReady = false;
295 // Sort the bonds by z coord
296 visiblebonds = new Vector<Bond>();
298 for (int ii = 0; ii < pdb.getChains().size(); ii++)
300 if (pdb.getChains().elementAt(ii).isVisible)
302 Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
304 for (int i = 0; i < tmp.size(); i++)
306 visiblebonds.addElement(tmp.elementAt(i));
310 seqColoursReady = true;
316 public void findWidth()
318 float[] max = new float[3];
319 float[] min = new float[3];
321 max[0] = (float) -1e30;
322 max[1] = (float) -1e30;
323 max[2] = (float) -1e30;
325 min[0] = (float) 1e30;
326 min[1] = (float) 1e30;
327 min[2] = (float) 1e30;
329 for (int ii = 0; ii < pdb.getChains().size(); ii++)
331 if (pdb.getChains().elementAt(ii).isVisible)
333 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
335 for (Bond tmp : bonds)
337 if (tmp.start[0] >= max[0])
339 max[0] = tmp.start[0];
342 if (tmp.start[1] >= max[1])
344 max[1] = tmp.start[1];
347 if (tmp.start[2] >= max[2])
349 max[2] = tmp.start[2];
352 if (tmp.start[0] <= min[0])
354 min[0] = tmp.start[0];
357 if (tmp.start[1] <= min[1])
359 min[1] = tmp.start[1];
362 if (tmp.start[2] <= min[2])
364 min[2] = tmp.start[2];
367 if (tmp.end[0] >= max[0])
372 if (tmp.end[1] >= max[1])
377 if (tmp.end[2] >= max[2])
382 if (tmp.end[0] <= min[0])
387 if (tmp.end[1] <= min[1])
392 if (tmp.end[2] <= min[2])
400 width[0] = Math.abs(max[0] - min[0]);
401 width[1] = Math.abs(max[1] - min[1]);
402 width[2] = Math.abs(max[2] - min[2]);
406 if (width[1] > width[0])
411 if (width[2] > width[1])
416 // System.out.println("Maxwidth = " + maxwidth);
419 public float findScale()
425 if (getSize().width != 0)
427 width = getSize().width;
428 height = getSize().height;
432 width = prefsize.width;
433 height = prefsize.height;
445 return (float) (dim / (1.5d * maxwidth));
448 public void findCentre()
456 // Find centre coordinate
457 for (int ii = 0; ii < pdb.getChains().size(); ii++)
459 if (pdb.getChains().elementAt(ii).isVisible)
461 Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
463 bsize += bonds.size();
467 xtot = xtot + b.start[0] + b.end[0];
468 ytot = ytot + b.start[1] + b.end[1];
469 ztot = ztot + b.start[2] + b.end[2];
474 centre[0] = xtot / (2 * (float) bsize);
475 centre[1] = ytot / (2 * (float) bsize);
476 centre[2] = ztot / (2 * (float) bsize);
480 public void paint(Graphics g)
485 g.setColor(Color.white);
486 g.fillRect(0, 0, getSize().width, getSize().height);
487 g.setColor(Color.black);
488 g.setFont(new Font("Verdana", Font.BOLD, 14));
490 MessageManager.getString("label.error_loading_pdb_data"), 50,
491 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());
580 if (bysequence && pdb != null)
582 for (int ii = 0; ii < pdb.getChains().size(); ii++)
584 chain = pdb.getChains().elementAt(ii);
586 for (int i = 0; i < chain.bonds.size(); i++)
588 Bond tmp = chain.bonds.elementAt(i);
589 tmp.startCol = Colour.lightGray;
590 tmp.endCol = Colour.lightGray;
591 if (chain != mainchain)
596 for (int s = 0; s < sequence.length; s++)
598 for (int m = 0; m < mapping.length; m++)
600 if (mapping[m].getSequence() == sequence[s])
602 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
605 pos = sequence[s].findIndex(pos);
606 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
609 tmp.startCol = fr.findFeatureColour(tmp.startCol,
613 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
616 pos = sequence[s].findIndex(pos);
617 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
620 tmp.endCol = fr.findFeatureColour(tmp.endCol,
635 public void drawScene(Graphics g)
644 zsort.sort(visiblebonds);
648 for (int i = 0; i < visiblebonds.size(); i++)
650 tmpBond = visiblebonds.elementAt(i);
652 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
653 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
655 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
656 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (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(ColorUtils.getColor(tmpBond.startCol).darker()
667 drawLine(g, xstart, ystart, xmid, ymid);
669 g.setColor(ColorUtils.getColor(tmpBond.endCol).darker().darker());
670 drawLine(g, xmid, ymid, xend, yend);
672 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
674 g.setColor(ColorUtils.getColor(tmpBond.startCol).darker());
675 drawLine(g, xstart, ystart, xmid, ymid);
677 g.setColor(ColorUtils.getColor(tmpBond.endCol).darker());
678 drawLine(g, xmid, ymid, xend, yend);
682 g.setColor(ColorUtils.getColor(tmpBond.startCol));
683 drawLine(g, xstart, ystart, xmid, ymid);
685 g.setColor(ColorUtils.getColor(tmpBond.endCol));
686 drawLine(g, xmid, ymid, xend, yend);
690 else if (depthcue && bymolecule)
692 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
694 g.setColor(Color.green.darker().darker());
695 drawLine(g, xstart, ystart, xend, yend);
697 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
699 g.setColor(Color.green.darker());
700 drawLine(g, xstart, ystart, xend, yend);
704 g.setColor(Color.green);
705 drawLine(g, xstart, ystart, xend, yend);
708 else if (!depthcue && !bymolecule)
710 g.setColor(ColorUtils.getColor(tmpBond.startCol));
711 drawLine(g, xstart, ystart, xmid, ymid);
712 g.setColor(ColorUtils.getColor(tmpBond.endCol));
713 drawLine(g, xmid, ymid, xend, yend);
717 drawLine(g, xstart, ystart, xend, yend);
720 if (highlightBond1 != null && highlightBond1 == tmpBond)
722 g.setColor(Color.white);
723 drawLine(g, xmid, ymid, xend, yend);
726 if (highlightBond2 != null && highlightBond2 == tmpBond)
728 g.setColor(Color.white);
729 drawLine(g, xstart, ystart, xmid, ymid);
735 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
739 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
741 g.drawLine(x1, y1, x2, y2);
742 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
743 g.drawLine(x1, y1 - 1, x2, y2 - 1);
747 g.setColor(g.getColor().brighter());
748 g.drawLine(x1, y1, x2, y2);
749 g.drawLine(x1 + 1, y1, x2 + 1, y2);
750 g.drawLine(x1 - 1, y1, x2 - 1, y2);
755 g.drawLine(x1, y1, x2, y2);
759 public Dimension minimumsize()
764 public Dimension preferredsize()
769 public void doKeyPressed(KeyEvent evt)
771 if (evt.getKeyCode() == KeyEvent.VK_UP)
773 scale = (float) (scale * 1.1);
777 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
779 scale = (float) (scale * 0.9);
786 public void mousePressed(MouseEvent e)
789 Atom fatom = findAtom(e.getX(), e.getY());
792 fatom.isSelected = !fatom.isSelected;
796 if (foundchain != -1)
798 PDBChain chain = pdb.getChains().elementAt(foundchain);
799 if (chain == mainchain)
801 if (fatom.alignmentMapping != -1)
803 if (highlightRes == null)
805 highlightRes = new Vector<String>();
808 final String atomString = Integer
809 .toString(fatom.alignmentMapping);
810 if (highlightRes.contains(atomString))
812 highlightRes.removeElement(atomString);
816 highlightRes.addElement(atomString);
831 public void mouseMoved(MouseEvent e)
834 if (highlightBond1 != null)
836 highlightBond1.at2.isSelected = false;
837 highlightBond2.at1.isSelected = false;
838 highlightBond1 = null;
839 highlightBond2 = null;
842 Atom fatom = findAtom(e.getX(), e.getY());
844 PDBChain chain = null;
845 if (foundchain != -1)
847 chain = pdb.getChains().elementAt(foundchain);
848 if (chain == mainchain)
850 mouseOverStructure(fatom.resNumber, chain.id);
859 appletToolTip = chain.id + ":" + fatom.resNumber + " "
866 mouseOverStructure(-1, chain != null ? chain.id : null);
867 appletToolTip = null;
874 public void mouseClicked(MouseEvent e)
879 public void mouseEntered(MouseEvent e)
884 public void mouseExited(MouseEvent e)
889 public void mouseDragged(MouseEvent evt)
896 MCMatrix objmat = new MCMatrix(3, 3);
897 objmat.setIdentity();
899 if ((evt.getModifiers() & Event.META_MASK) != 0)
901 objmat.rotatez(((mx - omx)));
905 objmat.rotatex(((omy - my)));
906 objmat.rotatey(((omx - mx)));
910 for (PDBChain chain : pdb.getChains())
912 for (Bond tmpBond : chain.bonds)
914 // Translate the bond so the centre is 0,0,0
915 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
917 // Now apply the rotation matrix
918 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
919 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
921 // Now translate back again
922 tmpBond.translate(centre[0], centre[1], centre[2]);
939 public void mouseReleased(MouseEvent evt)
945 void drawLabels(Graphics g)
948 for (PDBChain chain : pdb.getChains())
952 for (Bond tmpBond : chain.bonds)
954 if (tmpBond.at1.isSelected)
956 labelAtom(g, tmpBond, 1);
959 if (tmpBond.at2.isSelected)
961 labelAtom(g, tmpBond, 2);
968 public void labelAtom(Graphics g, Bond b, int n)
974 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
975 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
977 g.setColor(Color.red);
978 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
983 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
984 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (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) + (getSize().width / 2));
1015 if (Math.abs(truex - x) <= 2)
1017 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1019 if (Math.abs(truey - y) <= 2)
1021 fatom = tmpBond.at1;
1028 // Still here? Maybe its the last bond
1030 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1032 if (Math.abs(truex - x) <= 2)
1034 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1036 if (Math.abs(truey - y) <= 2)
1038 fatom = tmpBond.at2;
1046 if (fatom != null) // )&& chain.ds != null)
1048 chain = pdb.getChains().elementAt(foundchain);
1056 public void update(Graphics g)
1061 public void highlightRes(int ii)
1063 if (!seqColoursReady)
1068 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1075 for (index = 0; index < mainchain.bonds.size(); index++)
1077 tmpBond = mainchain.bonds.elementAt(index);
1078 if (tmpBond.at1.alignmentMapping == ii - 1)
1080 if (highlightBond1 != null)
1082 highlightBond1.at2.isSelected = false;
1085 if (highlightBond2 != null)
1087 highlightBond2.at1.isSelected = false;
1090 highlightBond1 = null;
1091 highlightBond2 = null;
1095 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1096 highlightBond1.at2.isSelected = true;
1099 if (index != mainchain.bonds.size())
1101 highlightBond2 = mainchain.bonds.elementAt(index);
1102 highlightBond2.at1.isSelected = true;
1109 redrawneeded = true;
1113 public void setAllchainsVisible(boolean b)
1115 for (int ii = 0; ii < pdb.getChains().size(); ii++)
1117 PDBChain chain = pdb.getChains().elementAt(ii);
1118 chain.isVisible = b;
1120 mainchain.isVisible = true;
1125 // ////////////////////////////////
1126 // /StructureListener
1128 public String[] getPdbFile()
1130 return new String[] { pdbentry.getFile() };
1135 public void mouseOverStructure(int pdbResNum, String chain)
1137 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1139 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1142 lastMessage = pdbResNum + chain;
1145 StringBuffer resetLastRes = new StringBuffer();
1147 StringBuffer eval = new StringBuffer();
1150 * Highlight the specified atoms in the structure.
1155 public void highlightAtoms(List<AtomSpec> atoms)
1157 if (!seqColoursReady)
1161 for (AtomSpec atom : atoms)
1163 int atomIndex = atom.getAtomIndex();
1165 if (highlightRes != null
1166 && highlightRes.contains((atomIndex - 1) + ""))
1171 highlightAtom(atomIndex);
1174 redrawneeded = true;
1181 protected void highlightAtom(int atomIndex)
1185 for (index = 0; index < mainchain.bonds.size(); index++)
1187 tmpBond = mainchain.bonds.elementAt(index);
1188 if (tmpBond.at1.atomIndex == atomIndex)
1190 if (highlightBond1 != null)
1192 highlightBond1.at2.isSelected = false;
1195 if (highlightBond2 != null)
1197 highlightBond2.at1.isSelected = false;
1200 highlightBond1 = null;
1201 highlightBond2 = null;
1205 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1206 highlightBond1.at2.isSelected = true;
1209 if (index != mainchain.bonds.size())
1211 highlightBond2 = mainchain.bonds.elementAt(index);
1212 highlightBond2.at1.isSelected = true;
1220 public Color getColour(int atomIndex, int pdbResNum, String chain,
1224 // if (!pdbfile.equals(pdbentry.getFile()))
1227 // return new Color(viewer.getAtomArgb(atomIndex));
1231 public void updateColours(Object source)
1234 redrawneeded = true;
1239 public void releaseReferences(Object svl)
1241 // TODO Auto-generated method stub
1246 public boolean isListeningFor(SequenceI seq)
1248 if (sequence != null)
1250 for (SequenceI s : sequence)