2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
26 import java.awt.event.*;
28 import jalview.analysis.*;
29 import jalview.datamodel.*;
31 import jalview.appletgui.*;
32 import jalview.structure.*;
34 public class AppletPDBCanvas extends Panel implements MouseListener,
35 MouseMotionListener, StructureListener
38 MCMatrix idmat = new MCMatrix(3, 3);
40 MCMatrix objmat = new MCMatrix(3, 3);
42 boolean redrawneeded = true;
64 float[] centre = new float[3];
66 float[] width = new float[3];
76 boolean bysequence = true;
78 boolean depthcue = true;
82 boolean bymolecule = false;
84 boolean zbuffer = true;
100 Font font = new Font("Helvetica", Font.PLAIN, 10);
102 public SequenceI[] sequence;
104 final StringBuffer mappingDetails = new StringBuffer();
106 String appletToolTip = null;
114 boolean pdbAction = false;
116 Bond highlightBond1, highlightBond2;
118 boolean errorLoading = false;
120 boolean seqColoursReady = false;
126 StructureSelectionManager ssm;
128 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
129 String[] chains, AlignmentPanel ap, String protocol)
133 this.pdbentry = pdbentry;
136 ssm = StructureSelectionManager.getStructureSelectionManager();
140 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
142 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
143 pdbentry.setFile("INLINE" + pdb.id);
145 } catch (Exception ex)
147 ex.printStackTrace();
151 pdbentry.setId(pdb.id);
153 ssm.addStructureViewerListener(this);
164 // JUST DEAL WITH ONE SEQUENCE FOR NOW
165 SequenceI sequence = seq[0];
167 for (int i = 0; i < pdb.chains.size(); i++)
170 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
171 + ((PDBChain) pdb.chains.elementAt(i)).sequence
172 .getSequenceAsString());
173 mappingDetails.append("\nNo of residues = "
174 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
177 // Now lets compare the sequences to get
178 // the start and end points.
179 // Align the sequence to the pdb
180 AlignSeq as = new AlignSeq(sequence, ((PDBChain) pdb.chains
181 .elementAt(i)).sequence, "pep");
182 as.calcScoreMatrix();
184 PrintStream ps = new PrintStream(System.out)
186 public void print(String x)
188 mappingDetails.append(x);
191 public void println()
193 mappingDetails.append("\n");
197 as.printAlignment(ps);
199 if (as.maxscore > max)
204 pdbstart = as.seq2start;
206 seqstart = as.seq1start + sequence.getStart() - 1;
207 seqend = as.seq1end + sequence.getEnd() - 1;
210 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
211 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
214 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
216 mainchain.pdbstart = pdbstart;
217 mainchain.pdbend = pdbend;
218 mainchain.seqstart = seqstart;
219 mainchain.seqend = seqend;
220 mainchain.isVisible = true;
221 // mainchain.makeExactMapping(maxAlignseq, sequence);
222 // mainchain.transferRESNUMFeatures(sequence, null);
224 this.prefsize = new Dimension(getSize().width, getSize().height);
226 // Initialize the matrices to identity
227 for (int i = 0; i < 3; i++)
229 for (int j = 0; j < 3; j++)
233 idmat.addElement(i, j, 0);
234 objmat.addElement(i, j, 0);
238 idmat.addElement(i, j, 1);
239 objmat.addElement(i, j, 1);
244 addMouseMotionListener(this);
245 addMouseListener(this);
247 addKeyListener(new KeyAdapter()
249 public void keyPressed(KeyEvent evt)
267 seqColoursReady = false;
268 // Sort the bonds by z coord
269 visiblebonds = new Vector();
271 for (int ii = 0; ii < pdb.chains.size(); ii++)
273 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
275 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
277 for (int i = 0; i < tmp.size(); i++)
279 visiblebonds.addElement(tmp.elementAt(i));
283 seqColoursReady = true;
289 public void findWidth()
291 float[] max = new float[3];
292 float[] min = new float[3];
294 max[0] = (float) -1e30;
295 max[1] = (float) -1e30;
296 max[2] = (float) -1e30;
298 min[0] = (float) 1e30;
299 min[1] = (float) 1e30;
300 min[2] = (float) 1e30;
302 for (int ii = 0; ii < pdb.chains.size(); ii++)
304 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
306 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
308 for (int i = 0; i < bonds.size(); i++)
310 Bond tmp = (Bond) bonds.elementAt(i);
312 if (tmp.start[0] >= max[0])
314 max[0] = tmp.start[0];
317 if (tmp.start[1] >= max[1])
319 max[1] = tmp.start[1];
322 if (tmp.start[2] >= max[2])
324 max[2] = tmp.start[2];
327 if (tmp.start[0] <= min[0])
329 min[0] = tmp.start[0];
332 if (tmp.start[1] <= min[1])
334 min[1] = tmp.start[1];
337 if (tmp.start[2] <= min[2])
339 min[2] = tmp.start[2];
342 if (tmp.end[0] >= max[0])
347 if (tmp.end[1] >= max[1])
352 if (tmp.end[2] >= max[2])
357 if (tmp.end[0] <= min[0])
362 if (tmp.end[1] <= min[1])
367 if (tmp.end[2] <= min[2])
375 width[0] = (float) Math.abs(max[0] - min[0]);
376 width[1] = (float) Math.abs(max[1] - min[1]);
377 width[2] = (float) Math.abs(max[2] - min[2]);
381 if (width[1] > width[0])
386 if (width[2] > width[1])
391 // System.out.println("Maxwidth = " + maxwidth);
394 public float findScale()
400 if (getSize().width != 0)
402 width = getSize().width;
403 height = getSize().height;
407 width = prefsize.width;
408 height = prefsize.height;
420 return (float) (dim / (1.5d * maxwidth));
423 public void findCentre()
431 // Find centre coordinate
432 for (int ii = 0; ii < pdb.chains.size(); ii++)
434 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
436 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
438 bsize += bonds.size();
440 for (int i = 0; i < bonds.size(); i++)
442 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
443 + ((Bond) bonds.elementAt(i)).end[0];
445 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
446 + ((Bond) bonds.elementAt(i)).end[1];
448 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
449 + ((Bond) bonds.elementAt(i)).end[2];
454 centre[0] = xtot / (2 * (float) bsize);
455 centre[1] = ytot / (2 * (float) bsize);
456 centre[2] = ztot / (2 * (float) bsize);
459 public void paint(Graphics g)
464 g.setColor(Color.white);
465 g.fillRect(0, 0, getSize().width, getSize().height);
466 g.setColor(Color.black);
467 g.setFont(new Font("Verdana", Font.BOLD, 14));
468 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
472 if (!seqColoursReady)
474 g.setColor(Color.black);
475 g.setFont(new Font("Verdana", Font.BOLD, 14));
476 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
480 // Only create the image at the beginning -
481 // this saves much memory usage
482 if ((img == null) || (prefsize.width != getSize().width)
483 || (prefsize.height != getSize().height))
488 prefsize.width = getSize().width;
489 prefsize.height = getSize().height;
492 img = createImage(prefsize.width, prefsize.height);
493 ig = img.getGraphics();
496 } catch (Exception ex)
498 ex.printStackTrace();
504 drawAll(ig, prefsize.width, prefsize.height);
505 redrawneeded = false;
507 if (appletToolTip != null)
509 ig.setColor(Color.red);
510 ig.drawString(appletToolTip, toolx, tooly);
513 g.drawImage(img, 0, 0, this);
518 public void drawAll(Graphics g, int width, int height)
520 ig.setColor(Color.black);
521 ig.fillRect(0, 0, width, height);
526 public void setColours(jalview.schemes.ColourSchemeI cs)
534 // This method has been taken out of PDBChain to allow
535 // Applet and Application specific sequence renderers to be used
536 void colourBySequence()
538 SequenceRenderer sr = new SequenceRenderer(ap.av);
540 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
542 boolean showFeatures = false;
543 if (ap.av.getShowSequenceFeatures())
547 fr = new jalview.appletgui.FeatureRenderer(ap.av);
550 fr.transferSettings(ap.getFeatureRenderer());
556 if (bysequence && pdb != null)
558 for (int ii = 0; ii < pdb.chains.size(); ii++)
560 chain = (PDBChain) pdb.chains.elementAt(ii);
562 for (int i = 0; i < chain.bonds.size(); i++)
564 Bond tmp = (Bond) chain.bonds.elementAt(i);
565 tmp.startCol = Color.lightGray;
566 tmp.endCol = Color.lightGray;
567 if (chain != mainchain)
572 for (int s = 0; s < sequence.length; s++)
574 for (int m = 0; m < mapping.length; m++)
576 if (mapping[m].getSequence() == sequence[s])
578 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
581 pos = sequence[s].findIndex(pos);
582 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
585 tmp.startCol = fr.findFeatureColour(tmp.startCol,
589 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
592 pos = sequence[s].findIndex(pos);
593 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
596 tmp.endCol = fr.findFeatureColour(tmp.endCol,
611 public void drawScene(Graphics g)
620 zsort.Zsort(visiblebonds);
624 for (int i = 0; i < visiblebonds.size(); i++)
626 tmpBond = (Bond) visiblebonds.elementAt(i);
628 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
629 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
631 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
632 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
634 xmid = (xend + xstart) / 2;
635 ymid = (yend + ystart) / 2;
637 if (depthcue && !bymolecule)
639 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
641 g.setColor(tmpBond.startCol.darker().darker());
642 drawLine(g, xstart, ystart, xmid, ymid);
644 g.setColor(tmpBond.endCol.darker().darker());
645 drawLine(g, xmid, ymid, xend, yend);
647 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
649 g.setColor(tmpBond.startCol.darker());
650 drawLine(g, xstart, ystart, xmid, ymid);
652 g.setColor(tmpBond.endCol.darker());
653 drawLine(g, xmid, ymid, xend, yend);
657 g.setColor(tmpBond.startCol);
658 drawLine(g, xstart, ystart, xmid, ymid);
660 g.setColor(tmpBond.endCol);
661 drawLine(g, xmid, ymid, xend, yend);
665 else if (depthcue && bymolecule)
667 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
669 g.setColor(Color.green.darker().darker());
670 drawLine(g, xstart, ystart, xend, yend);
672 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
674 g.setColor(Color.green.darker());
675 drawLine(g, xstart, ystart, xend, yend);
679 g.setColor(Color.green);
680 drawLine(g, xstart, ystart, xend, yend);
683 else if (!depthcue && !bymolecule)
685 g.setColor(tmpBond.startCol);
686 drawLine(g, xstart, ystart, xmid, ymid);
687 g.setColor(tmpBond.endCol);
688 drawLine(g, xmid, ymid, xend, yend);
692 drawLine(g, xstart, ystart, xend, yend);
695 if (highlightBond1 != null && highlightBond1 == tmpBond)
697 g.setColor(Color.white);
698 drawLine(g, xmid, ymid, xend, yend);
701 if (highlightBond2 != null && highlightBond2 == tmpBond)
703 g.setColor(Color.white);
704 drawLine(g, xstart, ystart, xmid, ymid);
710 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
714 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
716 g.drawLine(x1, y1, x2, y2);
717 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
718 g.drawLine(x1, y1 - 1, x2, y2 - 1);
722 g.setColor(g.getColor().brighter());
723 g.drawLine(x1, y1, x2, y2);
724 g.drawLine(x1 + 1, y1, x2 + 1, y2);
725 g.drawLine(x1 - 1, y1, x2 - 1, y2);
730 g.drawLine(x1, y1, x2, y2);
734 public Dimension minimumsize()
739 public Dimension preferredsize()
744 public void doKeyPressed(KeyEvent evt)
746 if (evt.getKeyCode() == KeyEvent.VK_UP)
748 scale = (float) (scale * 1.1);
752 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
754 scale = (float) (scale * 0.9);
760 public void mousePressed(MouseEvent e)
763 Atom fatom = findAtom(e.getX(), e.getY());
766 fatom.isSelected = !fatom.isSelected;
770 if (foundchain != -1)
772 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
773 if (chain == mainchain)
775 if (fatom.alignmentMapping != -1)
777 if (highlightRes == null)
779 highlightRes = new Vector();
782 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
784 highlightRes.removeElement(fatom.alignmentMapping + "");
788 highlightRes.addElement(fatom.alignmentMapping + "");
802 public void mouseMoved(MouseEvent e)
805 if (highlightBond1 != null)
807 highlightBond1.at2.isSelected = false;
808 highlightBond2.at1.isSelected = false;
809 highlightBond1 = null;
810 highlightBond2 = null;
813 Atom fatom = findAtom(e.getX(), e.getY());
815 PDBChain chain = null;
816 if (foundchain != -1)
818 chain = (PDBChain) pdb.chains.elementAt(foundchain);
819 if (chain == mainchain)
821 mouseOverStructure(fatom.resNumber, chain.id);
830 appletToolTip = chain.id + ":" + fatom.resNumber + " "
837 mouseOverStructure(-1, chain != null ? chain.id : null);
838 appletToolTip = null;
844 public void mouseClicked(MouseEvent e)
848 public void mouseEntered(MouseEvent e)
852 public void mouseExited(MouseEvent e)
856 public void mouseDragged(MouseEvent evt)
863 MCMatrix objmat = new MCMatrix(3, 3);
864 objmat.setIdentity();
866 if ((evt.getModifiers() & Event.META_MASK) != 0)
868 objmat.rotatez((float) ((mx - omx)));
872 objmat.rotatex((float) ((omy - my)));
873 objmat.rotatey((float) ((omx - mx)));
877 for (int ii = 0; ii < pdb.chains.size(); ii++)
879 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
881 for (int i = 0; i < bonds.size(); i++)
883 Bond tmpBond = (Bond) bonds.elementAt(i);
885 // Translate the bond so the centre is 0,0,0
886 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
888 // Now apply the rotation matrix
889 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
890 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
892 // Now translate back again
893 tmpBond.translate(centre[0], centre[1], centre[2]);
909 public void mouseReleased(MouseEvent evt)
915 void drawLabels(Graphics g)
918 for (int ii = 0; ii < pdb.chains.size(); ii++)
920 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
924 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
926 for (int i = 0; i < bonds.size(); i++)
928 Bond tmpBond = (Bond) bonds.elementAt(i);
930 if (tmpBond.at1.isSelected)
932 labelAtom(g, tmpBond, 1);
935 if (tmpBond.at2.isSelected)
938 labelAtom(g, tmpBond, 2);
945 public void labelAtom(Graphics g, Bond b, int n)
951 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
952 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
954 g.setColor(Color.red);
955 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
960 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
961 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
963 g.setColor(Color.red);
964 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
970 public Atom findAtom(int x, int y)
976 for (int ii = 0; ii < pdb.chains.size(); ii++)
978 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
984 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
986 for (int i = 0; i < bonds.size(); i++)
988 tmpBond = (Bond) bonds.elementAt(i);
990 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
992 if (Math.abs(truex - x) <= 2)
994 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
996 if (Math.abs(truey - y) <= 2)
1005 // Still here? Maybe its the last bond
1007 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1009 if (Math.abs(truex - x) <= 2)
1011 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1013 if (Math.abs(truey - y) <= 2)
1015 fatom = tmpBond.at2;
1023 if (fatom != null) // )&& chain.ds != null)
1025 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1032 public void update(Graphics g)
1037 public void highlightRes(int ii)
1039 if (!seqColoursReady)
1044 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1051 for (index = 0; index < mainchain.bonds.size(); index++)
1053 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1054 if (tmpBond.at1.alignmentMapping == ii - 1)
1056 if (highlightBond1 != null)
1058 highlightBond1.at2.isSelected = false;
1061 if (highlightBond2 != null)
1063 highlightBond2.at1.isSelected = false;
1066 highlightBond1 = null;
1067 highlightBond2 = null;
1071 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1072 highlightBond1.at2.isSelected = true;
1075 if (index != mainchain.bonds.size())
1077 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1078 highlightBond2.at1.isSelected = true;
1085 redrawneeded = true;
1089 public void setAllchainsVisible(boolean b)
1091 for (int ii = 0; ii < pdb.chains.size(); ii++)
1093 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1094 chain.isVisible = b;
1096 mainchain.isVisible = true;
1101 // ////////////////////////////////
1102 // /StructureListener
1103 public String getPdbFile()
1105 return pdbentry.getFile();
1110 public void mouseOverStructure(int pdbResNum, String chain)
1112 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1113 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1115 lastMessage = pdbResNum + chain;
1118 StringBuffer resetLastRes = new StringBuffer();
1120 StringBuffer eval = new StringBuffer();
1122 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1125 if (!seqColoursReady)
1130 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1137 for (index = 0; index < mainchain.bonds.size(); index++)
1139 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1140 if (tmpBond.at1.atomIndex == atomIndex)
1142 if (highlightBond1 != null)
1144 highlightBond1.at2.isSelected = false;
1147 if (highlightBond2 != null)
1149 highlightBond2.at1.isSelected = false;
1152 highlightBond1 = null;
1153 highlightBond2 = null;
1157 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1158 highlightBond1.at2.isSelected = true;
1161 if (index != mainchain.bonds.size())
1163 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1164 highlightBond2.at1.isSelected = true;
1171 redrawneeded = true;
1175 public Color getColour(int atomIndex, int pdbResNum, String chain,
1179 // if (!pdbfile.equals(pdbentry.getFile()))
1182 // return new Color(viewer.getAtomArgb(atomIndex));
1185 public void updateColours(Object source)
1188 redrawneeded = true;