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 // fr.featuresAdded();
102 ex.printStackTrace();
106 pdbentry.setId(pdb.id);
108 ssm.addStructureViewerListener(this);
110 colourBySequence(ap.getSequenceRenderer(),
111 ap.av.getShowSequenceFeatures() ?
112 ap.getFeatureRenderer() : null);
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;
243 colourBySequence(ap.getSequenceRenderer(),
244 ap.av.getShowSequenceFeatures() ?
245 ap.getFeatureRenderer() : null);
250 public void findWidth()
252 float[] max = new float[3];
253 float[] min = new float[3];
255 max[0] = (float) - 1e30;
256 max[1] = (float) - 1e30;
257 max[2] = (float) - 1e30;
259 min[0] = (float) 1e30;
260 min[1] = (float) 1e30;
261 min[2] = (float) 1e30;
263 for (int ii = 0; ii < pdb.chains.size(); ii++)
265 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
267 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
269 for (int i = 0; i < bonds.size(); i++)
271 Bond tmp = (Bond) bonds.elementAt(i);
273 if (tmp.start[0] >= max[0])
275 max[0] = tmp.start[0];
278 if (tmp.start[1] >= max[1])
280 max[1] = tmp.start[1];
283 if (tmp.start[2] >= max[2])
285 max[2] = tmp.start[2];
288 if (tmp.start[0] <= min[0])
290 min[0] = tmp.start[0];
293 if (tmp.start[1] <= min[1])
295 min[1] = tmp.start[1];
298 if (tmp.start[2] <= min[2])
300 min[2] = tmp.start[2];
303 if (tmp.end[0] >= max[0])
308 if (tmp.end[1] >= max[1])
313 if (tmp.end[2] >= max[2])
318 if (tmp.end[0] <= min[0])
323 if (tmp.end[1] <= min[1])
328 if (tmp.end[2] <= min[2])
336 width[0] = (float) Math.abs(max[0] - min[0]);
337 width[1] = (float) Math.abs(max[1] - min[1]);
338 width[2] = (float) Math.abs(max[2] - min[2]);
342 if (width[1] > width[0])
347 if (width[2] > width[1])
352 // System.out.println("Maxwidth = " + maxwidth);
355 public float findScale()
361 if (getSize().width != 0)
363 width = getSize().width;
364 height = getSize().height;
368 width = prefsize.width;
369 height = prefsize.height;
381 return (float) (dim / (1.5d * maxwidth));
384 public void findCentre()
392 //Find centre coordinate
393 for (int ii = 0; ii < pdb.chains.size(); ii++)
395 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
397 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
399 bsize += bonds.size();
401 for (int i = 0; i < bonds.size(); i++)
403 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
404 ( (Bond) bonds.elementAt(i)).end[0];
406 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
407 ( (Bond) bonds.elementAt(i)).end[1];
409 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
410 ( (Bond) bonds.elementAt(i)).end[2];
415 centre[0] = xtot / (2 * (float) bsize);
416 centre[1] = ytot / (2 * (float) bsize);
417 centre[2] = ztot / (2 * (float) bsize);
420 public void paint(Graphics g)
425 g.setColor(Color.white);
426 g.fillRect(0, 0, getSize().width, getSize().height);
427 g.setColor(Color.black);
428 g.setFont(new Font("Verdana", Font.BOLD, 14));
429 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
433 if (!seqColoursReady)
435 g.setColor(Color.black);
436 g.setFont(new Font("Verdana", Font.BOLD, 14));
437 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
441 //Only create the image at the beginning -
442 //this saves much memory usage
443 if ( (img == null) || (prefsize.width != getSize().width) ||
444 (prefsize.height != getSize().height))
449 prefsize.width = getSize().width;
450 prefsize.height = getSize().height;
453 img = createImage(prefsize.width, prefsize.height);
454 ig = img.getGraphics();
460 ex.printStackTrace();
466 drawAll(ig, prefsize.width, prefsize.height);
467 redrawneeded = false;
469 if (appletToolTip != null)
471 ig.setColor(Color.red);
472 ig.drawString(appletToolTip, toolx, tooly);
475 g.drawImage(img, 0, 0, this);
480 public void drawAll(Graphics g, int width, int height)
482 ig.setColor(Color.black);
483 ig.fillRect(0, 0, width, height);
488 public void setColours(jalview.schemes.ColourSchemeI cs)
498 // This method has been taken out of PDBChain to allow
499 // Applet and Application specific sequence renderers to be used
500 void colourBySequence(SequenceRenderer sr, FeatureRenderer fr)
502 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
504 boolean showFeatures = false;
509 fr = new jalview.appletgui.FeatureRenderer(ap.av);
511 fr.transferSettings(fr);
516 if (bysequence && pdb != null)
518 for (int ii = 0; ii < pdb.chains.size(); ii++)
520 chain = (PDBChain) pdb.chains.elementAt(ii);
522 for (int i = 0; i < chain.bonds.size(); i++)
524 Bond tmp = (Bond) chain.bonds.elementAt(i);
525 tmp.startCol = Color.lightGray;
526 tmp.endCol = Color.lightGray;
527 if (chain != mainchain)
532 for (int s = 0; s < sequence.length; s++)
534 for (int m = 0; m < mapping.length; m++)
536 if (mapping[m].getSequence() == sequence[s])
538 int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
541 pos = sequence[s].findIndex(pos);
542 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
545 tmp.startCol = fr.findFeatureColour(tmp.startCol,
550 pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
553 pos = sequence[s].findIndex(pos);
554 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
557 tmp.endCol = fr.findFeatureColour(tmp.endCol,
572 public void drawScene(Graphics g)
581 zsort.Zsort(visiblebonds);
585 for (int i = 0; i < visiblebonds.size(); i++)
587 tmpBond = (Bond) visiblebonds.elementAt(i);
589 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
590 (getSize().width / 2));
591 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
592 (getSize().height / 2));
594 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
595 (getSize().width / 2));
596 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
597 (getSize().height / 2));
599 xmid = (xend + xstart) / 2;
600 ymid = (yend + ystart) / 2;
602 if (depthcue && !bymolecule)
604 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
606 g.setColor(tmpBond.startCol.darker().darker());
607 drawLine(g, xstart, ystart, xmid, ymid);
609 g.setColor(tmpBond.endCol.darker().darker());
610 drawLine(g, xmid, ymid, xend, yend);
612 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
614 g.setColor(tmpBond.startCol.darker());
615 drawLine(g, xstart, ystart, xmid, ymid);
617 g.setColor(tmpBond.endCol.darker());
618 drawLine(g, xmid, ymid, xend, yend);
622 g.setColor(tmpBond.startCol);
623 drawLine(g, xstart, ystart, xmid, ymid);
625 g.setColor(tmpBond.endCol);
626 drawLine(g, xmid, ymid, xend, yend);
630 else if (depthcue && bymolecule)
632 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
634 g.setColor(Color.green.darker().darker());
635 drawLine(g, xstart, ystart, xend, yend);
637 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
639 g.setColor(Color.green.darker());
640 drawLine(g, xstart, ystart, xend, yend);
644 g.setColor(Color.green);
645 drawLine(g, xstart, ystart, xend, yend);
648 else if (!depthcue && !bymolecule)
650 g.setColor(tmpBond.startCol);
651 drawLine(g, xstart, ystart, xmid, ymid);
652 g.setColor(tmpBond.endCol);
653 drawLine(g, xmid, ymid, xend, yend);
657 drawLine(g, xstart, ystart, xend, yend);
660 if (highlightBond1 != null && highlightBond1 == tmpBond)
662 g.setColor(Color.white);
663 drawLine(g, xmid, ymid, xend, yend);
666 if (highlightBond2 != null && highlightBond2 == tmpBond)
668 g.setColor(Color.white);
669 drawLine(g, xstart, ystart, xmid, ymid);
675 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
679 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
681 g.drawLine(x1, y1, x2, y2);
682 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
683 g.drawLine(x1, y1 - 1, x2, y2 - 1);
687 g.setColor(g.getColor().brighter());
688 g.drawLine(x1, y1, x2, y2);
689 g.drawLine(x1 + 1, y1, x2 + 1, y2);
690 g.drawLine(x1 - 1, y1, x2 - 1, y2);
695 g.drawLine(x1, y1, x2, y2);
699 public Dimension minimumsize()
704 public Dimension preferredsize()
709 public void doKeyPressed(KeyEvent evt)
711 if (evt.getKeyCode() == KeyEvent.VK_UP)
713 scale = (float) (scale * 1.1);
717 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
719 scale = (float) (scale * 0.9);
725 public void mousePressed(MouseEvent e)
728 Atom fatom = findAtom(e.getX(), e.getY());
731 fatom.isSelected = !fatom.isSelected;
735 if (foundchain != -1)
737 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
738 if (chain == mainchain)
740 if (fatom.alignmentMapping != -1)
742 if (highlightRes == null)
744 highlightRes = new Vector();
747 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
749 highlightRes.removeElement(fatom.alignmentMapping + "");
753 highlightRes.addElement(fatom.alignmentMapping + "");
767 public void mouseMoved(MouseEvent e)
770 if (highlightBond1 != null)
772 highlightBond1.at2.isSelected = false;
773 highlightBond2.at1.isSelected = false;
774 highlightBond1 = null;
775 highlightBond2 = null;
778 Atom fatom = findAtom(e.getX(), e.getY());
780 PDBChain chain = null;
781 if (foundchain != -1)
783 chain = (PDBChain) pdb.chains.elementAt(foundchain);
784 if (chain == mainchain)
786 mouseOverStructure(fatom.resNumber, chain.id);
795 appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
801 mouseOverStructure(-1, chain!=null?chain.id:null);
802 appletToolTip = null;
808 public void mouseClicked(MouseEvent e)
812 public void mouseEntered(MouseEvent e)
816 public void mouseExited(MouseEvent e)
820 public void mouseDragged(MouseEvent evt)
827 MCMatrix objmat = new MCMatrix(3, 3);
828 objmat.setIdentity();
830 if ( (evt.getModifiers() & Event.META_MASK) != 0)
832 objmat.rotatez( (float) ( (mx - omx)));
836 objmat.rotatex( (float) ( (my - omy)));
837 objmat.rotatey( (float) ( (omx - mx)));
841 for (int ii = 0; ii < pdb.chains.size(); ii++)
843 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
845 for (int i = 0; i < bonds.size(); i++)
847 Bond tmpBond = (Bond) bonds.elementAt(i);
849 //Translate the bond so the centre is 0,0,0
850 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
852 //Now apply the rotation matrix
853 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
854 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
856 //Now translate back again
857 tmpBond.translate(centre[0], centre[1], centre[2]);
873 public void mouseReleased(MouseEvent evt)
879 void drawLabels(Graphics g)
882 for (int ii = 0; ii < pdb.chains.size(); ii++)
884 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
888 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
890 for (int i = 0; i < bonds.size(); i++)
892 Bond tmpBond = (Bond) bonds.elementAt(i);
894 if (tmpBond.at1.isSelected)
896 labelAtom(g, tmpBond, 1);
899 if (tmpBond.at2.isSelected)
902 labelAtom(g, tmpBond, 2);
909 public void labelAtom(Graphics g, Bond b, int n)
915 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
916 (getSize().width / 2));
917 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
918 (getSize().height / 2));
920 g.setColor(Color.red);
921 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
926 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
927 (getSize().width / 2));
928 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
929 (getSize().height / 2));
931 g.setColor(Color.red);
932 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
937 public Atom findAtom(int x, int y)
943 for (int ii = 0; ii < pdb.chains.size(); ii++)
945 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
951 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
953 for (int i = 0; i < bonds.size(); i++)
955 tmpBond = (Bond) bonds.elementAt(i);
957 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
958 (getSize().width / 2));
960 if (Math.abs(truex - x) <= 2)
962 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
963 (getSize().height / 2));
965 if (Math.abs(truey - y) <= 2)
974 // Still here? Maybe its the last bond
976 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
977 (getSize().width / 2));
979 if (Math.abs(truex - x) <= 2)
981 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
982 (getSize().height / 2));
984 if (Math.abs(truey - y) <= 2)
994 if (fatom != null) //)&& chain.ds != null)
996 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1003 public void update(Graphics g)
1008 public void highlightRes(int ii)
1010 if (!seqColoursReady)
1015 if (highlightRes != null
1016 && highlightRes.contains( (ii - 1) + ""))
1023 for (index = 0; index < mainchain.bonds.size(); index++)
1025 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1026 if (tmpBond.at1.alignmentMapping == ii - 1)
1028 if (highlightBond1 != null)
1030 highlightBond1.at2.isSelected = false;
1033 if (highlightBond2 != null)
1035 highlightBond2.at1.isSelected = false;
1038 highlightBond1 = null;
1039 highlightBond2 = null;
1043 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1044 highlightBond1.at2.isSelected = true;
1047 if (index != mainchain.bonds.size())
1049 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1050 highlightBond2.at1.isSelected = true;
1057 redrawneeded = true;
1061 public void setAllchainsVisible(boolean b)
1063 for (int ii = 0; ii < pdb.chains.size(); ii++)
1065 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1066 chain.isVisible = b;
1068 mainchain.isVisible = true;
1074 //////////////////////////////////
1075 ///StructureListener
1076 public String getPdbFile()
1083 public void mouseOverStructure(int pdbResNum, String chain)
1085 if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
1086 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1088 lastMessage = pdbResNum+chain;
1091 StringBuffer resetLastRes = new StringBuffer();
1092 StringBuffer eval = new StringBuffer();
1094 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
1096 if (!seqColoursReady)
1101 if (highlightRes != null
1102 && highlightRes.contains( (atomIndex - 1) + ""))
1109 for (index = 0; index < mainchain.bonds.size(); index++)
1111 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1112 if (tmpBond.at1.atomIndex == atomIndex)
1114 if (highlightBond1 != null)
1116 highlightBond1.at2.isSelected = false;
1119 if (highlightBond2 != null)
1121 highlightBond2.at1.isSelected = false;
1124 highlightBond1 = null;
1125 highlightBond2 = null;
1129 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1130 highlightBond1.at2.isSelected = true;
1133 if (index != mainchain.bonds.size())
1135 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1136 highlightBond2.at1.isSelected = true;
1143 redrawneeded = true;
1147 public void updateColours(Object source)
1149 AlignmentPanel ap = (AlignmentPanel) source;
1150 colourBySequence(ap.getSequenceRenderer(),
1151 ap.av.getShowSequenceFeatures() ?
1152 ap.getFeatureRenderer() : null);
1153 redrawneeded = true;