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,
91 this.pdbentry = pdbentry;
94 ssm = StructureSelectionManager.getStructureSelectionManager();
97 pdb = ssm.setMapping(seq, pdbentry.getFile(), protocol);
99 if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
100 pdbentry.setFile("INLINE"+pdb.id);
104 ex.printStackTrace();
108 pdbentry.setId(pdb.id);
110 ssm.addStructureViewerListener(this);
120 AlignSeq maxAlignseq = null;
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;
170 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
171 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
174 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
176 mainchain.pdbstart = pdbstart;
177 mainchain.pdbend = pdbend;
178 mainchain.seqstart = seqstart;
179 mainchain.seqend = seqend;
180 mainchain.isVisible = true;
181 // mainchain.makeExactMapping(maxAlignseq, sequence);
182 // mainchain.transferRESNUMFeatures(sequence, null);
184 this.prefsize = new Dimension(getSize().width, getSize().height);
186 //Initialize the matrices to identity
187 for (int i = 0; i < 3; i++)
189 for (int j = 0; j < 3; j++)
193 idmat.addElement(i, j, 0);
194 objmat.addElement(i, j, 0);
198 idmat.addElement(i, j, 1);
199 objmat.addElement(i, j, 1);
204 addMouseMotionListener(this);
205 addMouseListener(this);
207 addKeyListener(new KeyAdapter()
209 public void keyPressed(KeyEvent evt)
226 seqColoursReady = false;
227 // Sort the bonds by z coord
228 visiblebonds = new Vector();
230 for (int ii = 0; ii < pdb.chains.size(); ii++)
232 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
234 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
236 for (int i = 0; i < tmp.size(); i++)
238 visiblebonds.addElement(tmp.elementAt(i));
242 seqColoursReady = true;
248 public void findWidth()
250 float[] max = new float[3];
251 float[] min = new float[3];
253 max[0] = (float) - 1e30;
254 max[1] = (float) - 1e30;
255 max[2] = (float) - 1e30;
257 min[0] = (float) 1e30;
258 min[1] = (float) 1e30;
259 min[2] = (float) 1e30;
261 for (int ii = 0; ii < pdb.chains.size(); ii++)
263 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
265 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
267 for (int i = 0; i < bonds.size(); i++)
269 Bond tmp = (Bond) bonds.elementAt(i);
271 if (tmp.start[0] >= max[0])
273 max[0] = tmp.start[0];
276 if (tmp.start[1] >= max[1])
278 max[1] = tmp.start[1];
281 if (tmp.start[2] >= max[2])
283 max[2] = tmp.start[2];
286 if (tmp.start[0] <= min[0])
288 min[0] = tmp.start[0];
291 if (tmp.start[1] <= min[1])
293 min[1] = tmp.start[1];
296 if (tmp.start[2] <= min[2])
298 min[2] = tmp.start[2];
301 if (tmp.end[0] >= max[0])
306 if (tmp.end[1] >= max[1])
311 if (tmp.end[2] >= max[2])
316 if (tmp.end[0] <= min[0])
321 if (tmp.end[1] <= min[1])
326 if (tmp.end[2] <= min[2])
334 width[0] = (float) Math.abs(max[0] - min[0]);
335 width[1] = (float) Math.abs(max[1] - min[1]);
336 width[2] = (float) Math.abs(max[2] - min[2]);
340 if (width[1] > width[0])
345 if (width[2] > width[1])
350 // System.out.println("Maxwidth = " + maxwidth);
353 public float findScale()
359 if (getSize().width != 0)
361 width = getSize().width;
362 height = getSize().height;
366 width = prefsize.width;
367 height = prefsize.height;
379 return (float) (dim / (1.5d * maxwidth));
382 public void findCentre()
390 //Find centre coordinate
391 for (int ii = 0; ii < pdb.chains.size(); ii++)
393 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
395 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
397 bsize += bonds.size();
399 for (int i = 0; i < bonds.size(); i++)
401 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
402 ( (Bond) bonds.elementAt(i)).end[0];
404 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
405 ( (Bond) bonds.elementAt(i)).end[1];
407 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
408 ( (Bond) bonds.elementAt(i)).end[2];
413 centre[0] = xtot / (2 * (float) bsize);
414 centre[1] = ytot / (2 * (float) bsize);
415 centre[2] = ztot / (2 * (float) bsize);
418 public void paint(Graphics g)
423 g.setColor(Color.white);
424 g.fillRect(0, 0, getSize().width, getSize().height);
425 g.setColor(Color.black);
426 g.setFont(new Font("Verdana", Font.BOLD, 14));
427 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
431 if (!seqColoursReady)
433 g.setColor(Color.black);
434 g.setFont(new Font("Verdana", Font.BOLD, 14));
435 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
439 //Only create the image at the beginning -
440 //this saves much memory usage
441 if ( (img == null) || (prefsize.width != getSize().width) ||
442 (prefsize.height != getSize().height))
447 prefsize.width = getSize().width;
448 prefsize.height = getSize().height;
451 img = createImage(prefsize.width, prefsize.height);
452 ig = img.getGraphics();
458 ex.printStackTrace();
464 drawAll(ig, prefsize.width, prefsize.height);
465 redrawneeded = false;
467 if (appletToolTip != null)
469 ig.setColor(Color.red);
470 ig.drawString(appletToolTip, toolx, tooly);
473 g.drawImage(img, 0, 0, this);
478 public void drawAll(Graphics g, int width, int height)
480 ig.setColor(Color.black);
481 ig.fillRect(0, 0, width, height);
486 public void setColours(jalview.schemes.ColourSchemeI cs)
496 // This method has been taken out of PDBChain to allow
497 // Applet and Application specific sequence renderers to be used
498 void colourBySequence()
500 SequenceRenderer sr = ap.getSequenceRenderer();
502 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
504 boolean showFeatures = false;
505 if (ap.av.getShowSequenceFeatures())
509 fr = new jalview.appletgui.FeatureRenderer(ap.av);
512 fr.transferSettings(ap.getFeatureRenderer());
518 if (bysequence && pdb != null)
520 for (int ii = 0; ii < pdb.chains.size(); ii++)
522 chain = (PDBChain) pdb.chains.elementAt(ii);
524 for (int i = 0; i < chain.bonds.size(); i++)
526 Bond tmp = (Bond) chain.bonds.elementAt(i);
527 tmp.startCol = Color.lightGray;
528 tmp.endCol = Color.lightGray;
529 if (chain != mainchain)
534 for (int s = 0; s < sequence.length; s++)
536 for (int m = 0; m < mapping.length; m++)
538 if (mapping[m].getSequence() == sequence[s])
540 int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
543 pos = sequence[s].findIndex(pos);
544 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
547 tmp.startCol = fr.findFeatureColour(tmp.startCol,
552 pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
555 pos = sequence[s].findIndex(pos);
556 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
559 tmp.endCol = fr.findFeatureColour(tmp.endCol,
574 public void drawScene(Graphics g)
583 zsort.Zsort(visiblebonds);
587 for (int i = 0; i < visiblebonds.size(); i++)
589 tmpBond = (Bond) visiblebonds.elementAt(i);
591 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
592 (getSize().width / 2));
593 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
594 (getSize().height / 2));
596 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
597 (getSize().width / 2));
598 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
599 (getSize().height / 2));
601 xmid = (xend + xstart) / 2;
602 ymid = (yend + ystart) / 2;
604 if (depthcue && !bymolecule)
606 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
608 g.setColor(tmpBond.startCol.darker().darker());
609 drawLine(g, xstart, ystart, xmid, ymid);
611 g.setColor(tmpBond.endCol.darker().darker());
612 drawLine(g, xmid, ymid, xend, yend);
614 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
616 g.setColor(tmpBond.startCol.darker());
617 drawLine(g, xstart, ystart, xmid, ymid);
619 g.setColor(tmpBond.endCol.darker());
620 drawLine(g, xmid, ymid, xend, yend);
624 g.setColor(tmpBond.startCol);
625 drawLine(g, xstart, ystart, xmid, ymid);
627 g.setColor(tmpBond.endCol);
628 drawLine(g, xmid, ymid, xend, yend);
632 else if (depthcue && bymolecule)
634 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
636 g.setColor(Color.green.darker().darker());
637 drawLine(g, xstart, ystart, xend, yend);
639 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
641 g.setColor(Color.green.darker());
642 drawLine(g, xstart, ystart, xend, yend);
646 g.setColor(Color.green);
647 drawLine(g, xstart, ystart, xend, yend);
650 else if (!depthcue && !bymolecule)
652 g.setColor(tmpBond.startCol);
653 drawLine(g, xstart, ystart, xmid, ymid);
654 g.setColor(tmpBond.endCol);
655 drawLine(g, xmid, ymid, xend, yend);
659 drawLine(g, xstart, ystart, xend, yend);
662 if (highlightBond1 != null && highlightBond1 == tmpBond)
664 g.setColor(Color.white);
665 drawLine(g, xmid, ymid, xend, yend);
668 if (highlightBond2 != null && highlightBond2 == tmpBond)
670 g.setColor(Color.white);
671 drawLine(g, xstart, ystart, xmid, ymid);
677 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
681 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
683 g.drawLine(x1, y1, x2, y2);
684 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
685 g.drawLine(x1, y1 - 1, x2, y2 - 1);
689 g.setColor(g.getColor().brighter());
690 g.drawLine(x1, y1, x2, y2);
691 g.drawLine(x1 + 1, y1, x2 + 1, y2);
692 g.drawLine(x1 - 1, y1, x2 - 1, y2);
697 g.drawLine(x1, y1, x2, y2);
701 public Dimension minimumsize()
706 public Dimension preferredsize()
711 public void doKeyPressed(KeyEvent evt)
713 if (evt.getKeyCode() == KeyEvent.VK_UP)
715 scale = (float) (scale * 1.1);
719 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
721 scale = (float) (scale * 0.9);
727 public void mousePressed(MouseEvent e)
730 Atom fatom = findAtom(e.getX(), e.getY());
733 fatom.isSelected = !fatom.isSelected;
737 if (foundchain != -1)
739 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
740 if (chain == mainchain)
742 if (fatom.alignmentMapping != -1)
744 if (highlightRes == null)
746 highlightRes = new Vector();
749 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
751 highlightRes.removeElement(fatom.alignmentMapping + "");
755 highlightRes.addElement(fatom.alignmentMapping + "");
769 public void mouseMoved(MouseEvent e)
772 if (highlightBond1 != null)
774 highlightBond1.at2.isSelected = false;
775 highlightBond2.at1.isSelected = false;
776 highlightBond1 = null;
777 highlightBond2 = null;
780 Atom fatom = findAtom(e.getX(), e.getY());
782 PDBChain chain = null;
783 if (foundchain != -1)
785 chain = (PDBChain) pdb.chains.elementAt(foundchain);
786 if (chain == mainchain)
788 mouseOverStructure(fatom.resNumber, chain.id);
797 appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
803 mouseOverStructure(-1, chain!=null?chain.id:null);
804 appletToolTip = null;
810 public void mouseClicked(MouseEvent e)
814 public void mouseEntered(MouseEvent e)
818 public void mouseExited(MouseEvent e)
822 public void mouseDragged(MouseEvent evt)
829 MCMatrix objmat = new MCMatrix(3, 3);
830 objmat.setIdentity();
832 if ( (evt.getModifiers() & Event.META_MASK) != 0)
834 objmat.rotatez( (float) ( (mx - omx)));
838 objmat.rotatex( (float) ( (my - omy)));
839 objmat.rotatey( (float) ( (omx - mx)));
843 for (int ii = 0; ii < pdb.chains.size(); ii++)
845 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
847 for (int i = 0; i < bonds.size(); i++)
849 Bond tmpBond = (Bond) bonds.elementAt(i);
851 //Translate the bond so the centre is 0,0,0
852 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
854 //Now apply the rotation matrix
855 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
856 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
858 //Now translate back again
859 tmpBond.translate(centre[0], centre[1], centre[2]);
875 public void mouseReleased(MouseEvent evt)
881 void drawLabels(Graphics g)
884 for (int ii = 0; ii < pdb.chains.size(); ii++)
886 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
890 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
892 for (int i = 0; i < bonds.size(); i++)
894 Bond tmpBond = (Bond) bonds.elementAt(i);
896 if (tmpBond.at1.isSelected)
898 labelAtom(g, tmpBond, 1);
901 if (tmpBond.at2.isSelected)
904 labelAtom(g, tmpBond, 2);
911 public void labelAtom(Graphics g, Bond b, int n)
917 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
918 (getSize().width / 2));
919 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
920 (getSize().height / 2));
922 g.setColor(Color.red);
923 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
928 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
929 (getSize().width / 2));
930 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
931 (getSize().height / 2));
933 g.setColor(Color.red);
934 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
939 public Atom findAtom(int x, int y)
945 for (int ii = 0; ii < pdb.chains.size(); ii++)
947 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
953 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
955 for (int i = 0; i < bonds.size(); i++)
957 tmpBond = (Bond) bonds.elementAt(i);
959 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
960 (getSize().width / 2));
962 if (Math.abs(truex - x) <= 2)
964 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
965 (getSize().height / 2));
967 if (Math.abs(truey - y) <= 2)
976 // Still here? Maybe its the last bond
978 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
979 (getSize().width / 2));
981 if (Math.abs(truex - x) <= 2)
983 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
984 (getSize().height / 2));
986 if (Math.abs(truey - y) <= 2)
996 if (fatom != null) //)&& chain.ds != null)
998 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1005 public void update(Graphics g)
1010 public void highlightRes(int ii)
1012 if (!seqColoursReady)
1017 if (highlightRes != null
1018 && highlightRes.contains( (ii - 1) + ""))
1025 for (index = 0; index < mainchain.bonds.size(); index++)
1027 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1028 if (tmpBond.at1.alignmentMapping == ii - 1)
1030 if (highlightBond1 != null)
1032 highlightBond1.at2.isSelected = false;
1035 if (highlightBond2 != null)
1037 highlightBond2.at1.isSelected = false;
1040 highlightBond1 = null;
1041 highlightBond2 = null;
1045 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1046 highlightBond1.at2.isSelected = true;
1049 if (index != mainchain.bonds.size())
1051 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1052 highlightBond2.at1.isSelected = true;
1059 redrawneeded = true;
1063 public void setAllchainsVisible(boolean b)
1065 for (int ii = 0; ii < pdb.chains.size(); ii++)
1067 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1068 chain.isVisible = b;
1070 mainchain.isVisible = true;
1076 //////////////////////////////////
1077 ///StructureListener
1078 public String getPdbFile()
1085 public void mouseOverStructure(int pdbResNum, String chain)
1087 if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
1088 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1090 lastMessage = pdbResNum+chain;
1093 StringBuffer resetLastRes = new StringBuffer();
1094 StringBuffer eval = new StringBuffer();
1096 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
1098 if (!seqColoursReady)
1103 if (highlightRes != null
1104 && highlightRes.contains( (atomIndex - 1) + ""))
1111 for (index = 0; index < mainchain.bonds.size(); index++)
1113 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1114 if (tmpBond.at1.atomIndex == atomIndex)
1116 if (highlightBond1 != null)
1118 highlightBond1.at2.isSelected = false;
1121 if (highlightBond2 != null)
1123 highlightBond2.at1.isSelected = false;
1126 highlightBond1 = null;
1127 highlightBond2 = null;
1131 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1132 highlightBond1.at2.isSelected = true;
1135 if (index != mainchain.bonds.size())
1137 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1138 highlightBond2.at1.isSelected = true;
1145 redrawneeded = true;
1150 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
1153 // if (!pdbfile.equals(pdbentry.getFile()))
1156 //return new Color(viewer.getAtomArgb(atomIndex));
1159 public void updateColours(Object source)
1161 AlignmentPanel ap = (AlignmentPanel) source;
1163 redrawneeded = true;