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);
121 AlignSeq maxAlignseq = null;
123 //JUST DEAL WITH ONE SEQUENCE FOR NOW
124 SequenceI sequence = seq[0];
126 for (int i = 0; i < pdb.chains.size(); i++)
129 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
130 ( (PDBChain) pdb.chains.elementAt(i)).sequence.
131 getSequenceAsString());
132 mappingDetails.append("\nNo of residues = " +
133 ( (PDBChain) pdb.chains.elementAt(i)).residues.size() +
136 // Now lets compare the sequences to get
137 // the start and end points.
138 // Align the sequence to the pdb
139 AlignSeq as = new AlignSeq(sequence,
140 ( (PDBChain) pdb.chains.elementAt(i)).sequence,
142 as.calcScoreMatrix();
144 PrintStream ps = new PrintStream(System.out)
146 public void print(String x)
148 mappingDetails.append(x);
151 public void println()
153 mappingDetails.append("\n");
157 as.printAlignment(ps);
159 if (as.maxscore > max)
164 pdbstart = as.seq2start;
166 seqstart = as.seq1start + sequence.getStart() - 1;
167 seqend = as.seq1end + sequence.getEnd() - 1;
171 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
172 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
175 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
177 mainchain.pdbstart = pdbstart;
178 mainchain.pdbend = pdbend;
179 mainchain.seqstart = seqstart;
180 mainchain.seqend = seqend;
181 mainchain.isVisible = true;
182 // mainchain.makeExactMapping(maxAlignseq, sequence);
183 // mainchain.transferRESNUMFeatures(sequence, null);
185 this.prefsize = new Dimension(getSize().width, getSize().height);
187 //Initialize the matrices to identity
188 for (int i = 0; i < 3; i++)
190 for (int j = 0; j < 3; j++)
194 idmat.addElement(i, j, 0);
195 objmat.addElement(i, j, 0);
199 idmat.addElement(i, j, 1);
200 objmat.addElement(i, j, 1);
205 addMouseMotionListener(this);
206 addMouseListener(this);
208 addKeyListener(new KeyAdapter()
210 public void keyPressed(KeyEvent evt)
227 seqColoursReady = false;
228 // Sort the bonds by z coord
229 visiblebonds = new Vector();
231 for (int ii = 0; ii < pdb.chains.size(); ii++)
233 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
235 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
237 for (int i = 0; i < tmp.size(); i++)
239 visiblebonds.addElement(tmp.elementAt(i));
243 seqColoursReady = true;
249 public void findWidth()
251 float[] max = new float[3];
252 float[] min = new float[3];
254 max[0] = (float) - 1e30;
255 max[1] = (float) - 1e30;
256 max[2] = (float) - 1e30;
258 min[0] = (float) 1e30;
259 min[1] = (float) 1e30;
260 min[2] = (float) 1e30;
262 for (int ii = 0; ii < pdb.chains.size(); ii++)
264 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
266 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
268 for (int i = 0; i < bonds.size(); i++)
270 Bond tmp = (Bond) bonds.elementAt(i);
272 if (tmp.start[0] >= max[0])
274 max[0] = tmp.start[0];
277 if (tmp.start[1] >= max[1])
279 max[1] = tmp.start[1];
282 if (tmp.start[2] >= max[2])
284 max[2] = tmp.start[2];
287 if (tmp.start[0] <= min[0])
289 min[0] = tmp.start[0];
292 if (tmp.start[1] <= min[1])
294 min[1] = tmp.start[1];
297 if (tmp.start[2] <= min[2])
299 min[2] = tmp.start[2];
302 if (tmp.end[0] >= max[0])
307 if (tmp.end[1] >= max[1])
312 if (tmp.end[2] >= max[2])
317 if (tmp.end[0] <= min[0])
322 if (tmp.end[1] <= min[1])
327 if (tmp.end[2] <= min[2])
335 width[0] = (float) Math.abs(max[0] - min[0]);
336 width[1] = (float) Math.abs(max[1] - min[1]);
337 width[2] = (float) Math.abs(max[2] - min[2]);
341 if (width[1] > width[0])
346 if (width[2] > width[1])
351 // System.out.println("Maxwidth = " + maxwidth);
354 public float findScale()
360 if (getSize().width != 0)
362 width = getSize().width;
363 height = getSize().height;
367 width = prefsize.width;
368 height = prefsize.height;
380 return (float) (dim / (1.5d * maxwidth));
383 public void findCentre()
391 //Find centre coordinate
392 for (int ii = 0; ii < pdb.chains.size(); ii++)
394 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
396 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
398 bsize += bonds.size();
400 for (int i = 0; i < bonds.size(); i++)
402 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
403 ( (Bond) bonds.elementAt(i)).end[0];
405 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
406 ( (Bond) bonds.elementAt(i)).end[1];
408 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
409 ( (Bond) bonds.elementAt(i)).end[2];
414 centre[0] = xtot / (2 * (float) bsize);
415 centre[1] = ytot / (2 * (float) bsize);
416 centre[2] = ztot / (2 * (float) bsize);
419 public void paint(Graphics g)
424 g.setColor(Color.white);
425 g.fillRect(0, 0, getSize().width, getSize().height);
426 g.setColor(Color.black);
427 g.setFont(new Font("Verdana", Font.BOLD, 14));
428 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
432 if (!seqColoursReady)
434 g.setColor(Color.black);
435 g.setFont(new Font("Verdana", Font.BOLD, 14));
436 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
440 //Only create the image at the beginning -
441 //this saves much memory usage
442 if ( (img == null) || (prefsize.width != getSize().width) ||
443 (prefsize.height != getSize().height))
448 prefsize.width = getSize().width;
449 prefsize.height = getSize().height;
452 img = createImage(prefsize.width, prefsize.height);
453 ig = img.getGraphics();
459 ex.printStackTrace();
465 drawAll(ig, prefsize.width, prefsize.height);
466 redrawneeded = false;
468 if (appletToolTip != null)
470 ig.setColor(Color.red);
471 ig.drawString(appletToolTip, toolx, tooly);
474 g.drawImage(img, 0, 0, this);
479 public void drawAll(Graphics g, int width, int height)
481 ig.setColor(Color.black);
482 ig.fillRect(0, 0, width, height);
487 public void setColours(jalview.schemes.ColourSchemeI cs)
497 // This method has been taken out of PDBChain to allow
498 // Applet and Application specific sequence renderers to be used
499 void colourBySequence()
501 SequenceRenderer sr = ap.getSequenceRenderer();
503 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
505 boolean showFeatures = false;
506 if (ap.av.getShowSequenceFeatures())
510 fr = new jalview.appletgui.FeatureRenderer(ap.av);
513 fr.transferSettings(ap.getFeatureRenderer());
519 if (bysequence && pdb != null)
521 for (int ii = 0; ii < pdb.chains.size(); ii++)
523 chain = (PDBChain) pdb.chains.elementAt(ii);
525 for (int i = 0; i < chain.bonds.size(); i++)
527 Bond tmp = (Bond) chain.bonds.elementAt(i);
528 tmp.startCol = Color.lightGray;
529 tmp.endCol = Color.lightGray;
530 if (chain != mainchain)
535 for (int s = 0; s < sequence.length; s++)
537 for (int m = 0; m < mapping.length; m++)
539 if (mapping[m].getSequence() == sequence[s])
541 int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
544 pos = sequence[s].findIndex(pos);
545 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
548 tmp.startCol = fr.findFeatureColour(tmp.startCol,
553 pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
556 pos = sequence[s].findIndex(pos);
557 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
560 tmp.endCol = fr.findFeatureColour(tmp.endCol,
575 public void drawScene(Graphics g)
584 zsort.Zsort(visiblebonds);
588 for (int i = 0; i < visiblebonds.size(); i++)
590 tmpBond = (Bond) visiblebonds.elementAt(i);
592 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
593 (getSize().width / 2));
594 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
595 (getSize().height / 2));
597 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
598 (getSize().width / 2));
599 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
600 (getSize().height / 2));
602 xmid = (xend + xstart) / 2;
603 ymid = (yend + ystart) / 2;
605 if (depthcue && !bymolecule)
607 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
609 g.setColor(tmpBond.startCol.darker().darker());
610 drawLine(g, xstart, ystart, xmid, ymid);
612 g.setColor(tmpBond.endCol.darker().darker());
613 drawLine(g, xmid, ymid, xend, yend);
615 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
617 g.setColor(tmpBond.startCol.darker());
618 drawLine(g, xstart, ystart, xmid, ymid);
620 g.setColor(tmpBond.endCol.darker());
621 drawLine(g, xmid, ymid, xend, yend);
625 g.setColor(tmpBond.startCol);
626 drawLine(g, xstart, ystart, xmid, ymid);
628 g.setColor(tmpBond.endCol);
629 drawLine(g, xmid, ymid, xend, yend);
633 else if (depthcue && bymolecule)
635 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
637 g.setColor(Color.green.darker().darker());
638 drawLine(g, xstart, ystart, xend, yend);
640 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
642 g.setColor(Color.green.darker());
643 drawLine(g, xstart, ystart, xend, yend);
647 g.setColor(Color.green);
648 drawLine(g, xstart, ystart, xend, yend);
651 else if (!depthcue && !bymolecule)
653 g.setColor(tmpBond.startCol);
654 drawLine(g, xstart, ystart, xmid, ymid);
655 g.setColor(tmpBond.endCol);
656 drawLine(g, xmid, ymid, xend, yend);
660 drawLine(g, xstart, ystart, xend, yend);
663 if (highlightBond1 != null && highlightBond1 == tmpBond)
665 g.setColor(Color.white);
666 drawLine(g, xmid, ymid, xend, yend);
669 if (highlightBond2 != null && highlightBond2 == tmpBond)
671 g.setColor(Color.white);
672 drawLine(g, xstart, ystart, xmid, ymid);
678 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
682 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
684 g.drawLine(x1, y1, x2, y2);
685 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
686 g.drawLine(x1, y1 - 1, x2, y2 - 1);
690 g.setColor(g.getColor().brighter());
691 g.drawLine(x1, y1, x2, y2);
692 g.drawLine(x1 + 1, y1, x2 + 1, y2);
693 g.drawLine(x1 - 1, y1, x2 - 1, y2);
698 g.drawLine(x1, y1, x2, y2);
702 public Dimension minimumsize()
707 public Dimension preferredsize()
712 public void doKeyPressed(KeyEvent evt)
714 if (evt.getKeyCode() == KeyEvent.VK_UP)
716 scale = (float) (scale * 1.1);
720 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
722 scale = (float) (scale * 0.9);
728 public void mousePressed(MouseEvent e)
731 Atom fatom = findAtom(e.getX(), e.getY());
734 fatom.isSelected = !fatom.isSelected;
738 if (foundchain != -1)
740 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
741 if (chain == mainchain)
743 if (fatom.alignmentMapping != -1)
745 if (highlightRes == null)
747 highlightRes = new Vector();
750 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
752 highlightRes.removeElement(fatom.alignmentMapping + "");
756 highlightRes.addElement(fatom.alignmentMapping + "");
770 public void mouseMoved(MouseEvent e)
773 if (highlightBond1 != null)
775 highlightBond1.at2.isSelected = false;
776 highlightBond2.at1.isSelected = false;
777 highlightBond1 = null;
778 highlightBond2 = null;
781 Atom fatom = findAtom(e.getX(), e.getY());
783 PDBChain chain = null;
784 if (foundchain != -1)
786 chain = (PDBChain) pdb.chains.elementAt(foundchain);
787 if (chain == mainchain)
789 mouseOverStructure(fatom.resNumber, chain.id);
798 appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
804 mouseOverStructure(-1, chain!=null?chain.id:null);
805 appletToolTip = null;
811 public void mouseClicked(MouseEvent e)
815 public void mouseEntered(MouseEvent e)
819 public void mouseExited(MouseEvent e)
823 public void mouseDragged(MouseEvent evt)
830 MCMatrix objmat = new MCMatrix(3, 3);
831 objmat.setIdentity();
833 if ( (evt.getModifiers() & Event.META_MASK) != 0)
835 objmat.rotatez( (float) ( (mx - omx)));
839 objmat.rotatex( (float) ( (my - omy)));
840 objmat.rotatey( (float) ( (omx - mx)));
844 for (int ii = 0; ii < pdb.chains.size(); ii++)
846 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
848 for (int i = 0; i < bonds.size(); i++)
850 Bond tmpBond = (Bond) bonds.elementAt(i);
852 //Translate the bond so the centre is 0,0,0
853 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
855 //Now apply the rotation matrix
856 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
857 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
859 //Now translate back again
860 tmpBond.translate(centre[0], centre[1], centre[2]);
876 public void mouseReleased(MouseEvent evt)
882 void drawLabels(Graphics g)
885 for (int ii = 0; ii < pdb.chains.size(); ii++)
887 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
891 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
893 for (int i = 0; i < bonds.size(); i++)
895 Bond tmpBond = (Bond) bonds.elementAt(i);
897 if (tmpBond.at1.isSelected)
899 labelAtom(g, tmpBond, 1);
902 if (tmpBond.at2.isSelected)
905 labelAtom(g, tmpBond, 2);
912 public void labelAtom(Graphics g, Bond b, int n)
918 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
919 (getSize().width / 2));
920 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
921 (getSize().height / 2));
923 g.setColor(Color.red);
924 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
929 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
930 (getSize().width / 2));
931 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
932 (getSize().height / 2));
934 g.setColor(Color.red);
935 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
940 public Atom findAtom(int x, int y)
946 for (int ii = 0; ii < pdb.chains.size(); ii++)
948 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
954 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
956 for (int i = 0; i < bonds.size(); i++)
958 tmpBond = (Bond) bonds.elementAt(i);
960 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
961 (getSize().width / 2));
963 if (Math.abs(truex - x) <= 2)
965 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
966 (getSize().height / 2));
968 if (Math.abs(truey - y) <= 2)
977 // Still here? Maybe its the last bond
979 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
980 (getSize().width / 2));
982 if (Math.abs(truex - x) <= 2)
984 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
985 (getSize().height / 2));
987 if (Math.abs(truey - y) <= 2)
997 if (fatom != null) //)&& chain.ds != null)
999 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1006 public void update(Graphics g)
1011 public void highlightRes(int ii)
1013 if (!seqColoursReady)
1018 if (highlightRes != null
1019 && highlightRes.contains( (ii - 1) + ""))
1026 for (index = 0; index < mainchain.bonds.size(); index++)
1028 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1029 if (tmpBond.at1.alignmentMapping == ii - 1)
1031 if (highlightBond1 != null)
1033 highlightBond1.at2.isSelected = false;
1036 if (highlightBond2 != null)
1038 highlightBond2.at1.isSelected = false;
1041 highlightBond1 = null;
1042 highlightBond2 = null;
1046 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1047 highlightBond1.at2.isSelected = true;
1050 if (index != mainchain.bonds.size())
1052 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1053 highlightBond2.at1.isSelected = true;
1060 redrawneeded = true;
1064 public void setAllchainsVisible(boolean b)
1066 for (int ii = 0; ii < pdb.chains.size(); ii++)
1068 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1069 chain.isVisible = b;
1071 mainchain.isVisible = true;
1077 //////////////////////////////////
1078 ///StructureListener
1079 public String getPdbFile()
1086 public void mouseOverStructure(int pdbResNum, String chain)
1088 if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
1089 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1091 lastMessage = pdbResNum+chain;
1094 StringBuffer resetLastRes = new StringBuffer();
1095 StringBuffer eval = new StringBuffer();
1097 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
1099 if (!seqColoursReady)
1104 if (highlightRes != null
1105 && highlightRes.contains( (atomIndex - 1) + ""))
1112 for (index = 0; index < mainchain.bonds.size(); index++)
1114 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1115 if (tmpBond.at1.atomIndex == atomIndex)
1117 if (highlightBond1 != null)
1119 highlightBond1.at2.isSelected = false;
1122 if (highlightBond2 != null)
1124 highlightBond2.at1.isSelected = false;
1127 highlightBond1 = null;
1128 highlightBond2 = null;
1132 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1133 highlightBond1.at2.isSelected = true;
1136 if (index != mainchain.bonds.size())
1138 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1139 highlightBond2.at1.isSelected = true;
1146 redrawneeded = true;
1151 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
1154 // if (!pdbfile.equals(pdbentry.getFile()))
1157 //return new Color(viewer.getAtomArgb(atomIndex));
1160 public void updateColours(Object source)
1162 AlignmentPanel ap = (AlignmentPanel) source;
1164 redrawneeded = true;