2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
35 extends Panel implements MouseListener, MouseMotionListener, StructureListener
38 MCMatrix idmat = new MCMatrix(3, 3);
39 MCMatrix objmat = new MCMatrix(3, 3);
40 boolean redrawneeded = true;
51 float[] centre = new float[3];
52 float[] width = new float[3];
57 boolean bysequence = true;
58 boolean depthcue = true;
60 boolean bymolecule = false;
61 boolean zbuffer = true;
69 Font font = new Font("Helvetica", Font.PLAIN, 10);
70 public SequenceI [] sequence;
71 final StringBuffer mappingDetails = new StringBuffer();
72 String appletToolTip = null;
76 boolean pdbAction = false;
77 Bond highlightBond1, highlightBond2;
78 boolean errorLoading = false;
79 boolean seqColoursReady = false;
82 StructureSelectionManager ssm;
84 public AppletPDBCanvas(PDBEntry pdbentry,
92 this.pdbentry = pdbentry;
95 ssm = StructureSelectionManager.getStructureSelectionManager();
98 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
100 if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
101 pdbentry.setFile("INLINE"+pdb.id);
105 ex.printStackTrace();
109 pdbentry.setId(pdb.id);
111 ssm.addStructureViewerListener(this);
122 //JUST DEAL WITH ONE SEQUENCE FOR NOW
123 SequenceI sequence = seq[0];
125 for (int i = 0; i < pdb.chains.size(); i++)
128 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
129 ( (PDBChain) pdb.chains.elementAt(i)).sequence.
130 getSequenceAsString());
131 mappingDetails.append("\nNo of residues = " +
132 ( (PDBChain) pdb.chains.elementAt(i)).residues.size() +
135 // Now lets compare the sequences to get
136 // the start and end points.
137 // Align the sequence to the pdb
138 AlignSeq as = new AlignSeq(sequence,
139 ( (PDBChain) pdb.chains.elementAt(i)).sequence,
141 as.calcScoreMatrix();
143 PrintStream ps = new PrintStream(System.out)
145 public void print(String x)
147 mappingDetails.append(x);
150 public void println()
152 mappingDetails.append("\n");
156 as.printAlignment(ps);
158 if (as.maxscore > max)
163 pdbstart = as.seq2start;
165 seqstart = as.seq1start + sequence.getStart() - 1;
166 seqend = as.seq1end + sequence.getEnd() - 1;
169 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
170 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
173 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
175 mainchain.pdbstart = pdbstart;
176 mainchain.pdbend = pdbend;
177 mainchain.seqstart = seqstart;
178 mainchain.seqend = seqend;
179 mainchain.isVisible = true;
180 // mainchain.makeExactMapping(maxAlignseq, sequence);
181 // mainchain.transferRESNUMFeatures(sequence, null);
183 this.prefsize = new Dimension(getSize().width, getSize().height);
185 //Initialize the matrices to identity
186 for (int i = 0; i < 3; i++)
188 for (int j = 0; j < 3; j++)
192 idmat.addElement(i, j, 0);
193 objmat.addElement(i, j, 0);
197 idmat.addElement(i, j, 1);
198 objmat.addElement(i, j, 1);
203 addMouseMotionListener(this);
204 addMouseListener(this);
206 addKeyListener(new KeyAdapter()
208 public void keyPressed(KeyEvent evt)
225 seqColoursReady = false;
226 // Sort the bonds by z coord
227 visiblebonds = new Vector();
229 for (int ii = 0; ii < pdb.chains.size(); ii++)
231 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
233 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
235 for (int i = 0; i < tmp.size(); i++)
237 visiblebonds.addElement(tmp.elementAt(i));
241 seqColoursReady = true;
247 public void findWidth()
249 float[] max = new float[3];
250 float[] min = new float[3];
252 max[0] = (float) - 1e30;
253 max[1] = (float) - 1e30;
254 max[2] = (float) - 1e30;
256 min[0] = (float) 1e30;
257 min[1] = (float) 1e30;
258 min[2] = (float) 1e30;
260 for (int ii = 0; ii < pdb.chains.size(); ii++)
262 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
264 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
266 for (int i = 0; i < bonds.size(); i++)
268 Bond tmp = (Bond) bonds.elementAt(i);
270 if (tmp.start[0] >= max[0])
272 max[0] = tmp.start[0];
275 if (tmp.start[1] >= max[1])
277 max[1] = tmp.start[1];
280 if (tmp.start[2] >= max[2])
282 max[2] = tmp.start[2];
285 if (tmp.start[0] <= min[0])
287 min[0] = tmp.start[0];
290 if (tmp.start[1] <= min[1])
292 min[1] = tmp.start[1];
295 if (tmp.start[2] <= min[2])
297 min[2] = tmp.start[2];
300 if (tmp.end[0] >= max[0])
305 if (tmp.end[1] >= max[1])
310 if (tmp.end[2] >= max[2])
315 if (tmp.end[0] <= min[0])
320 if (tmp.end[1] <= min[1])
325 if (tmp.end[2] <= min[2])
333 width[0] = (float) Math.abs(max[0] - min[0]);
334 width[1] = (float) Math.abs(max[1] - min[1]);
335 width[2] = (float) Math.abs(max[2] - min[2]);
339 if (width[1] > width[0])
344 if (width[2] > width[1])
349 // System.out.println("Maxwidth = " + maxwidth);
352 public float findScale()
358 if (getSize().width != 0)
360 width = getSize().width;
361 height = getSize().height;
365 width = prefsize.width;
366 height = prefsize.height;
378 return (float) (dim / (1.5d * maxwidth));
381 public void findCentre()
389 //Find centre coordinate
390 for (int ii = 0; ii < pdb.chains.size(); ii++)
392 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
394 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
396 bsize += bonds.size();
398 for (int i = 0; i < bonds.size(); i++)
400 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
401 ( (Bond) bonds.elementAt(i)).end[0];
403 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
404 ( (Bond) bonds.elementAt(i)).end[1];
406 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
407 ( (Bond) bonds.elementAt(i)).end[2];
412 centre[0] = xtot / (2 * (float) bsize);
413 centre[1] = ytot / (2 * (float) bsize);
414 centre[2] = ztot / (2 * (float) bsize);
417 public void paint(Graphics g)
422 g.setColor(Color.white);
423 g.fillRect(0, 0, getSize().width, getSize().height);
424 g.setColor(Color.black);
425 g.setFont(new Font("Verdana", Font.BOLD, 14));
426 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
430 if (!seqColoursReady)
432 g.setColor(Color.black);
433 g.setFont(new Font("Verdana", Font.BOLD, 14));
434 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
438 //Only create the image at the beginning -
439 //this saves much memory usage
440 if ( (img == null) || (prefsize.width != getSize().width) ||
441 (prefsize.height != getSize().height))
446 prefsize.width = getSize().width;
447 prefsize.height = getSize().height;
450 img = createImage(prefsize.width, prefsize.height);
451 ig = img.getGraphics();
457 ex.printStackTrace();
463 drawAll(ig, prefsize.width, prefsize.height);
464 redrawneeded = false;
466 if (appletToolTip != null)
468 ig.setColor(Color.red);
469 ig.drawString(appletToolTip, toolx, tooly);
472 g.drawImage(img, 0, 0, this);
477 public void drawAll(Graphics g, int width, int height)
479 ig.setColor(Color.black);
480 ig.fillRect(0, 0, width, height);
485 public void setColours(jalview.schemes.ColourSchemeI cs)
495 // This method has been taken out of PDBChain to allow
496 // Applet and Application specific sequence renderers to be used
497 void colourBySequence()
499 SequenceRenderer sr = new SequenceRenderer(ap.av);
501 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
503 boolean showFeatures = false;
504 if (ap.av.getShowSequenceFeatures())
508 fr = new jalview.appletgui.FeatureRenderer(ap.av);
511 fr.transferSettings(ap.getFeatureRenderer());
517 if (bysequence && pdb != null)
519 for (int ii = 0; ii < pdb.chains.size(); ii++)
521 chain = (PDBChain) pdb.chains.elementAt(ii);
523 for (int i = 0; i < chain.bonds.size(); i++)
525 Bond tmp = (Bond) chain.bonds.elementAt(i);
526 tmp.startCol = Color.lightGray;
527 tmp.endCol = Color.lightGray;
528 if (chain != mainchain)
533 for (int s = 0; s < sequence.length; s++)
535 for (int m = 0; m < mapping.length; m++)
537 if (mapping[m].getSequence() == sequence[s])
539 int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
542 pos = sequence[s].findIndex(pos);
543 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
546 tmp.startCol = fr.findFeatureColour(tmp.startCol,
551 pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
554 pos = sequence[s].findIndex(pos);
555 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
558 tmp.endCol = fr.findFeatureColour(tmp.endCol,
573 public void drawScene(Graphics g)
582 zsort.Zsort(visiblebonds);
586 for (int i = 0; i < visiblebonds.size(); i++)
588 tmpBond = (Bond) visiblebonds.elementAt(i);
590 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
591 (getSize().width / 2));
592 ystart = (int) ( ( (centre[1] - tmpBond.start[1] ) * scale) +
593 (getSize().height / 2));
595 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
596 (getSize().width / 2));
597 yend = (int) ( ( (centre[1] - tmpBond.end[1] ) * scale) +
598 (getSize().height / 2));
600 xmid = (xend + xstart) / 2;
601 ymid = (yend + ystart) / 2;
603 if (depthcue && !bymolecule)
605 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
607 g.setColor(tmpBond.startCol.darker().darker());
608 drawLine(g, xstart, ystart, xmid, ymid);
610 g.setColor(tmpBond.endCol.darker().darker());
611 drawLine(g, xmid, ymid, xend, yend);
613 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
615 g.setColor(tmpBond.startCol.darker());
616 drawLine(g, xstart, ystart, xmid, ymid);
618 g.setColor(tmpBond.endCol.darker());
619 drawLine(g, xmid, ymid, xend, yend);
623 g.setColor(tmpBond.startCol);
624 drawLine(g, xstart, ystart, xmid, ymid);
626 g.setColor(tmpBond.endCol);
627 drawLine(g, xmid, ymid, xend, yend);
631 else if (depthcue && bymolecule)
633 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
635 g.setColor(Color.green.darker().darker());
636 drawLine(g, xstart, ystart, xend, yend);
638 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
640 g.setColor(Color.green.darker());
641 drawLine(g, xstart, ystart, xend, yend);
645 g.setColor(Color.green);
646 drawLine(g, xstart, ystart, xend, yend);
649 else if (!depthcue && !bymolecule)
651 g.setColor(tmpBond.startCol);
652 drawLine(g, xstart, ystart, xmid, ymid);
653 g.setColor(tmpBond.endCol);
654 drawLine(g, xmid, ymid, xend, yend);
658 drawLine(g, xstart, ystart, xend, yend);
661 if (highlightBond1 != null && highlightBond1 == tmpBond)
663 g.setColor(Color.white);
664 drawLine(g, xmid, ymid, xend, yend);
667 if (highlightBond2 != null && highlightBond2 == tmpBond)
669 g.setColor(Color.white);
670 drawLine(g, xstart, ystart, xmid, ymid);
676 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
680 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
682 g.drawLine(x1, y1, x2, y2);
683 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
684 g.drawLine(x1, y1 - 1, x2, y2 - 1);
688 g.setColor(g.getColor().brighter());
689 g.drawLine(x1, y1, x2, y2);
690 g.drawLine(x1 + 1, y1, x2 + 1, y2);
691 g.drawLine(x1 - 1, y1, x2 - 1, y2);
696 g.drawLine(x1, y1, x2, y2);
700 public Dimension minimumsize()
705 public Dimension preferredsize()
710 public void doKeyPressed(KeyEvent evt)
712 if (evt.getKeyCode() == KeyEvent.VK_UP)
714 scale = (float) (scale * 1.1);
718 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
720 scale = (float) (scale * 0.9);
726 public void mousePressed(MouseEvent e)
729 Atom fatom = findAtom(e.getX(), e.getY());
732 fatom.isSelected = !fatom.isSelected;
736 if (foundchain != -1)
738 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
739 if (chain == mainchain)
741 if (fatom.alignmentMapping != -1)
743 if (highlightRes == null)
745 highlightRes = new Vector();
748 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
750 highlightRes.removeElement(fatom.alignmentMapping + "");
754 highlightRes.addElement(fatom.alignmentMapping + "");
768 public void mouseMoved(MouseEvent e)
771 if (highlightBond1 != null)
773 highlightBond1.at2.isSelected = false;
774 highlightBond2.at1.isSelected = false;
775 highlightBond1 = null;
776 highlightBond2 = null;
779 Atom fatom = findAtom(e.getX(), e.getY());
781 PDBChain chain = null;
782 if (foundchain != -1)
784 chain = (PDBChain) pdb.chains.elementAt(foundchain);
785 if (chain == mainchain)
787 mouseOverStructure(fatom.resNumber, chain.id);
796 appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
802 mouseOverStructure(-1, chain!=null?chain.id:null);
803 appletToolTip = null;
809 public void mouseClicked(MouseEvent e)
813 public void mouseEntered(MouseEvent e)
817 public void mouseExited(MouseEvent e)
821 public void mouseDragged(MouseEvent evt)
828 MCMatrix objmat = new MCMatrix(3, 3);
829 objmat.setIdentity();
831 if ( (evt.getModifiers() & Event.META_MASK) != 0)
833 objmat.rotatez( (float) ( (mx - omx)));
837 objmat.rotatex( (float) ( (omy - my)));
838 objmat.rotatey( (float) ( (omx - mx)));
842 for (int ii = 0; ii < pdb.chains.size(); ii++)
844 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
846 for (int i = 0; i < bonds.size(); i++)
848 Bond tmpBond = (Bond) bonds.elementAt(i);
850 //Translate the bond so the centre is 0,0,0
851 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
853 //Now apply the rotation matrix
854 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
855 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
857 //Now translate back again
858 tmpBond.translate(centre[0], centre[1], centre[2]);
874 public void mouseReleased(MouseEvent evt)
880 void drawLabels(Graphics g)
883 for (int ii = 0; ii < pdb.chains.size(); ii++)
885 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
889 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
891 for (int i = 0; i < bonds.size(); i++)
893 Bond tmpBond = (Bond) bonds.elementAt(i);
895 if (tmpBond.at1.isSelected)
897 labelAtom(g, tmpBond, 1);
900 if (tmpBond.at2.isSelected)
903 labelAtom(g, tmpBond, 2);
910 public void labelAtom(Graphics g, Bond b, int n)
916 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
917 (getSize().width / 2));
918 int ystart = (int) ( ( (centre[1] - b.start[1]) * scale) +
919 (getSize().height / 2));
921 g.setColor(Color.red);
922 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
927 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
928 (getSize().width / 2));
929 int ystart = (int) ( ( (centre[1] - b.end[1]) * scale) +
930 (getSize().height / 2));
932 g.setColor(Color.red);
933 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
938 public Atom findAtom(int x, int y)
944 for (int ii = 0; ii < pdb.chains.size(); ii++)
946 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
952 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
954 for (int i = 0; i < bonds.size(); i++)
956 tmpBond = (Bond) bonds.elementAt(i);
958 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
959 (getSize().width / 2));
961 if (Math.abs(truex - x) <= 2)
963 int truey = (int) ( ( (centre[1] - tmpBond.start[1] ) * scale) +
964 (getSize().height / 2));
966 if (Math.abs(truey - y) <= 2)
975 // Still here? Maybe its the last bond
977 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
978 (getSize().width / 2));
980 if (Math.abs(truex - x) <= 2)
982 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
983 (getSize().height / 2));
985 if (Math.abs(truey - y) <= 2)
995 if (fatom != null) //)&& chain.ds != null)
997 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1004 public void update(Graphics g)
1009 public void highlightRes(int ii)
1011 if (!seqColoursReady)
1016 if (highlightRes != null
1017 && highlightRes.contains( (ii - 1) + ""))
1024 for (index = 0; index < mainchain.bonds.size(); index++)
1026 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1027 if (tmpBond.at1.alignmentMapping == ii - 1)
1029 if (highlightBond1 != null)
1031 highlightBond1.at2.isSelected = false;
1034 if (highlightBond2 != null)
1036 highlightBond2.at1.isSelected = false;
1039 highlightBond1 = null;
1040 highlightBond2 = null;
1044 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1045 highlightBond1.at2.isSelected = true;
1048 if (index != mainchain.bonds.size())
1050 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1051 highlightBond2.at1.isSelected = true;
1058 redrawneeded = true;
1062 public void setAllchainsVisible(boolean b)
1064 for (int ii = 0; ii < pdb.chains.size(); ii++)
1066 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1067 chain.isVisible = b;
1069 mainchain.isVisible = true;
1075 //////////////////////////////////
1076 ///StructureListener
1077 public String getPdbFile()
1079 return pdbentry.getFile();
1084 public void mouseOverStructure(int pdbResNum, String chain)
1086 if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
1087 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1089 lastMessage = pdbResNum+chain;
1092 StringBuffer resetLastRes = new StringBuffer();
1093 StringBuffer eval = new StringBuffer();
1095 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
1097 if (!seqColoursReady)
1102 if (highlightRes != null
1103 && highlightRes.contains( (atomIndex - 1) + ""))
1110 for (index = 0; index < mainchain.bonds.size(); index++)
1112 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1113 if (tmpBond.at1.atomIndex == atomIndex)
1115 if (highlightBond1 != null)
1117 highlightBond1.at2.isSelected = false;
1120 if (highlightBond2 != null)
1122 highlightBond2.at1.isSelected = false;
1125 highlightBond1 = null;
1126 highlightBond2 = null;
1130 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1131 highlightBond1.at2.isSelected = true;
1134 if (index != mainchain.bonds.size())
1136 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1137 highlightBond2.at1.isSelected = true;
1144 redrawneeded = true;
1149 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
1152 // if (!pdbfile.equals(pdbentry.getFile()))
1155 //return new Color(viewer.getAtomArgb(atomIndex));
1158 public void updateColours(Object source)
1161 redrawneeded = true;