2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
21 import jalview.analysis.AlignSeq;
23 import jalview.datamodel.*;
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
27 import java.awt.event.*;
35 public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener
38 MCMatrix idmat = new MCMatrix(3, 3);
39 MCMatrix objmat = new MCMatrix(3, 3);
40 boolean redrawneeded = true;
50 float[] centre = new float[3];
51 float[] width = new float[3];
56 boolean bysequence = true;
57 boolean depthcue = true;
59 boolean bymolecule = false;
60 boolean zbuffer = true;
68 Font font = new Font("Helvetica", Font.PLAIN, 10);
69 jalview.appletgui.SeqCanvas seqcanvas;
70 public Sequence 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;
80 jalview.appletgui.FeatureRenderer fr;
82 public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)
84 this.seqcanvas = seqcanvas;
87 seqcanvas.setPDBCanvas(this);
88 addKeyListener(new KeyAdapter()
91 public void keyPressed(KeyEvent evt)
99 public void setPDBFile(PDBfile pdb)
107 AlignSeq maxAlignseq = null;;
109 for (int i = 0; i < pdb.chains.size(); i++)
112 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequenceAsString());
113 mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");
115 // Now lets compare the sequences to get
116 // the start and end points.
117 // Align the sequence to the pdb
118 AlignSeq as = new AlignSeq(sequence,
119 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
120 as.calcScoreMatrix();
122 PrintStream ps = new PrintStream(System.out)
124 public void print(String x) {
125 mappingDetails.append(x);
127 public void println()
129 mappingDetails.append("\n");
133 as.printAlignment(ps);
135 if (as.maxscore > max) {
139 pdbstart = as.seq2start;
141 seqstart = as.seq1start + sequence.getStart()-1;
142 seqend = as.seq1end + sequence.getEnd()-1;
146 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
147 mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);
150 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
152 mainchain.pdbstart = pdbstart;
153 mainchain.pdbend = pdbend;
154 mainchain.seqstart = seqstart;
155 mainchain.seqend = seqend;
156 mainchain.isVisible = true;
157 mainchain.makeExactMapping(maxAlignseq, sequence);
158 mainchain.transferRESNUMFeatures(sequence, null);
160 this.prefsize = new Dimension(getSize().width, getSize().height);
162 //Initialize the matrices to identity
163 for (int i = 0; i < 3; i++) {
164 for (int j = 0; j < 3; j++) {
166 idmat.addElement(i, j, 0);
167 objmat.addElement(i, j, 0);
169 idmat.addElement(i, j, 1);
170 objmat.addElement(i, j, 1);
175 addMouseMotionListener(this);
176 addMouseListener(this);
191 seqColoursReady = false;
192 // Sort the bonds by z coord
193 visiblebonds = new Vector();
195 for (int ii = 0; ii < pdb.chains.size(); ii++)
197 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
199 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
201 for (int i = 0; i < tmp.size(); i++)
203 visiblebonds.addElement(tmp.elementAt(i));
208 seqColoursReady = true;
214 public void findWidth() {
215 float[] max = new float[3];
216 float[] min = new float[3];
218 max[0] = (float) -1e30;
219 max[1] = (float) -1e30;
220 max[2] = (float) -1e30;
222 min[0] = (float) 1e30;
223 min[1] = (float) 1e30;
224 min[2] = (float) 1e30;
226 for (int ii = 0; ii < pdb.chains.size(); ii++) {
227 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
228 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
230 for (int i = 0; i < bonds.size(); i++) {
231 Bond tmp = (Bond) bonds.elementAt(i);
233 if (tmp.start[0] >= max[0]) {
234 max[0] = tmp.start[0];
237 if (tmp.start[1] >= max[1]) {
238 max[1] = tmp.start[1];
241 if (tmp.start[2] >= max[2]) {
242 max[2] = tmp.start[2];
245 if (tmp.start[0] <= min[0]) {
246 min[0] = tmp.start[0];
249 if (tmp.start[1] <= min[1]) {
250 min[1] = tmp.start[1];
253 if (tmp.start[2] <= min[2]) {
254 min[2] = tmp.start[2];
257 if (tmp.end[0] >= max[0]) {
261 if (tmp.end[1] >= max[1]) {
265 if (tmp.end[2] >= max[2]) {
269 if (tmp.end[0] <= min[0]) {
273 if (tmp.end[1] <= min[1]) {
277 if (tmp.end[2] <= min[2]) {
284 width[0] = (float) Math.abs(max[0] - min[0]);
285 width[1] = (float) Math.abs(max[1] - min[1]);
286 width[2] = (float) Math.abs(max[2] - min[2]);
290 if (width[1] > width[0]) {
294 if (width[2] > width[1]) {
298 // System.out.println("Maxwidth = " + maxwidth);
301 public float findScale() {
306 if (getSize().width != 0) {
307 width = getSize().width;
308 height = getSize().height;
310 width = prefsize.width;
311 height = prefsize.height;
314 if (width < height) {
320 return (float) (dim / (1.5d * maxwidth));
323 public void findCentre() {
330 //Find centre coordinate
331 for (int ii = 0; ii < pdb.chains.size(); ii++) {
332 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
333 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
335 bsize += bonds.size();
337 for (int i = 0; i < bonds.size(); i++) {
338 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
339 ((Bond) bonds.elementAt(i)).end[0];
341 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
342 ((Bond) bonds.elementAt(i)).end[1];
344 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
345 ((Bond) bonds.elementAt(i)).end[2];
350 centre[0] = xtot / (2 * (float) bsize);
351 centre[1] = ytot / (2 * (float) bsize);
352 centre[2] = ztot / (2 * (float) bsize);
355 public void paint(Graphics g)
360 g.setColor(Color.white);
361 g.fillRect(0,0,getSize().width, getSize().height);
362 g.setColor(Color.black);
363 g.setFont(new Font("Verdana", Font.BOLD, 14));
364 g.drawString("Error loading PDB data!!", 50, getSize().height/2);
368 if( !seqColoursReady )
370 g.setColor(Color.black);
371 g.setFont(new Font("Verdana", Font.BOLD, 14));
372 g.drawString("Fetching PDB data...", 50, getSize().height/2);
378 //Only create the image at the beginning -
379 //this saves much memory usage
380 if ((img == null) || (prefsize.width != getSize().width) ||
381 (prefsize.height != getSize().height)) {
383 try{ prefsize.width = getSize().width;
384 prefsize.height = getSize().height;
387 img = createImage(prefsize.width, prefsize.height);
388 ig = img.getGraphics();
393 ex.printStackTrace();
400 drawAll(ig, prefsize.width, prefsize.height);
401 redrawneeded = false;
403 if(appletToolTip!=null)
405 ig.setColor(Color.red);
406 ig.drawString(appletToolTip, toolx, tooly);
409 g.drawImage(img, 0, 0, this);
414 public void drawAll(Graphics g, int width, int height)
416 ig.setColor(Color.black);
417 ig.fillRect(0, 0, width, height);
422 void setColours(jalview.schemes.ColourSchemeI cs)
429 public void updateSeqColours()
436 if(bysequence && pdb!=null)
438 for (int ii = 0; ii < pdb.chains.size(); ii++)
440 colourBySequence((PDBChain) pdb.chains.elementAt(ii));
449 int findTrueIndex(int pos)
451 // returns the alignment position for a residue
452 int j = sequence.getStart();
455 while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) && (j <= pos+1))
457 if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
468 if ( (j == sequence.getEnd()) && (j < pos))
470 return sequence.getEnd() + 1;
479 // This method has been taken out of PDBChain to allow
480 // Applet and Application specific sequence renderers to be used
481 void colourBySequence(PDBChain chain)
483 boolean showFeatures = false;
485 if(seqcanvas.getViewport().getShowSequenceFeatures())
489 fr = new jalview.appletgui.FeatureRenderer(seqcanvas.getViewport());
491 fr.transferSettings( seqcanvas.getFeatureRenderer());
495 for (int i = 0; i < chain.bonds.size(); i++)
497 Bond tmp = (Bond) chain.bonds.elementAt(i);
498 tmp.startCol = Color.lightGray;
499 tmp.endCol = Color.lightGray;
503 if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
504 (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
507 int index = findTrueIndex(tmp.at1.alignmentMapping);
508 //sequence.findIndex(tmp.at1.alignmentMapping);
511 tmp.startCol = seqcanvas.getSequenceRenderer().
512 getResidueBoxColour( sequence, index);
515 tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
519 int index = findTrueIndex(tmp.at2.alignmentMapping);
520 //sequence.findIndex( tmp.at2.alignmentMapping );
523 tmp.endCol = seqcanvas.getSequenceRenderer().
524 getResidueBoxColour( sequence, index);
527 tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
534 public void drawScene(Graphics g)
541 zsort.Zsort(visiblebonds);
546 for (int i = 0; i < visiblebonds.size(); i++)
548 tmpBond = (Bond) visiblebonds.elementAt(i);
551 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +
552 (getSize().width / 2));
553 ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +
554 (getSize().height / 2));
556 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +
557 (getSize().width / 2));
558 yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +
559 (getSize().height / 2));
561 xmid = (xend + xstart) / 2;
562 ymid = (yend + ystart) / 2;
564 if (depthcue && !bymolecule)
566 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
567 g.setColor(tmpBond.startCol.darker().darker());
568 drawLine(g, xstart, ystart, xmid, ymid);
570 g.setColor(tmpBond.endCol.darker().darker());
571 drawLine(g, xmid, ymid, xend, yend);
572 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
573 g.setColor(tmpBond.startCol.darker());
574 drawLine(g, xstart, ystart, xmid, ymid);
576 g.setColor(tmpBond.endCol.darker());
577 drawLine(g, xmid, ymid, xend, yend);
579 g.setColor(tmpBond.startCol);
580 drawLine(g, xstart, ystart, xmid, ymid);
582 g.setColor(tmpBond.endCol);
583 drawLine(g, xmid, ymid, xend, yend);
586 } else if (depthcue && bymolecule) {
587 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
588 g.setColor(Color.green.darker().darker());
589 drawLine(g, xstart, ystart, xend, yend);
590 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
591 g.setColor(Color.green.darker());
592 drawLine(g, xstart, ystart, xend, yend);
594 g.setColor(Color.green);
595 drawLine(g, xstart, ystart, xend, yend);
597 } else if (!depthcue && !bymolecule) {
598 g.setColor(tmpBond.startCol);
599 drawLine(g, xstart, ystart, xmid, ymid);
600 g.setColor(tmpBond.endCol);
601 drawLine(g, xmid, ymid, xend, yend);
603 drawLine(g, xstart, ystart, xend, yend);
606 if(highlightBond1!=null && highlightBond1==tmpBond)
608 g.setColor(Color.white);
609 drawLine(g, xmid, ymid, xend, yend);
612 if(highlightBond2!=null && highlightBond2==tmpBond)
614 g.setColor(Color.white);
615 drawLine(g, xstart, ystart, xmid, ymid);
621 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
623 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
624 g.drawLine(x1, y1, x2, y2);
625 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
626 g.drawLine(x1, y1 - 1, x2, y2 - 1);
628 g.setColor(g.getColor().brighter());
629 g.drawLine(x1, y1, x2, y2);
630 g.drawLine(x1 + 1, y1, x2 + 1, y2);
631 g.drawLine(x1 - 1, y1, x2 - 1, y2);
634 g.drawLine(x1, y1, x2, y2);
638 public Dimension minimumsize() {
642 public Dimension preferredsize() {
646 public void doKeyPressed(KeyEvent evt)
648 if (evt.getKeyCode() == KeyEvent.VK_UP)
650 scale = (float) (scale * 1.1);
654 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
656 scale = (float) (scale * 0.9);
662 public void mousePressed(MouseEvent e) {
664 Atom fatom = findAtom(e.getX(), e.getY());
667 fatom.isSelected = !fatom.isSelected;
671 if (foundchain != -1)
673 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
674 if (chain == mainchain)
676 if (fatom.alignmentMapping != -1)
678 if (highlightRes == null)
679 highlightRes = new Vector();
681 if (highlightRes.contains(fatom.alignmentMapping+"" + ""))
682 highlightRes.removeElement(fatom.alignmentMapping + "");
684 highlightRes.addElement(fatom.alignmentMapping + "");
697 public void mouseMoved(MouseEvent e) {
699 if(highlightBond1!=null)
701 highlightBond1.at2.isSelected = false;
702 highlightBond2.at1.isSelected = false;
703 highlightBond1 = null;
704 highlightBond2 = null;
707 Atom fatom = findAtom(e.getX(), e.getY());
709 PDBChain chain = null;
712 chain = (PDBChain) pdb.chains.elementAt(foundchain);
713 if(chain == mainchain)
715 highlightSeqcanvas( fatom.alignmentMapping );
723 appletToolTip = chain.id+":"+ fatom.resNumber+" "+ fatom.resName;
727 highlightSeqcanvas( -1);
728 appletToolTip = null;
735 void highlightSeqcanvas(int pos)
737 SearchResults searchResults = new SearchResults();
738 if(highlightRes!=null)
740 for (int i = 0; i < highlightRes.size(); i++)
742 int a = Integer.parseInt(highlightRes.elementAt(
745 searchResults.addResult(sequence, a, a);
751 searchResults.addResult(sequence, pos+1, pos+1);
754 seqcanvas.highlightSearchResults(searchResults);
758 public void mouseClicked(MouseEvent e) {
761 public void mouseEntered(MouseEvent e) {
764 public void mouseExited(MouseEvent e) {
767 public void mouseDragged(MouseEvent evt) {
773 MCMatrix objmat = new MCMatrix(3, 3);
774 objmat.setIdentity();
776 if ((evt.getModifiers() & Event.META_MASK) != 0) {
777 objmat.rotatez((float) ((mx - omx)));
779 objmat.rotatex((float) ((my - omy)));
780 objmat.rotatey((float) ((omx - mx)));
784 for (int ii = 0; ii < pdb.chains.size(); ii++) {
785 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
787 for (int i = 0; i < bonds.size(); i++) {
788 Bond tmpBond = (Bond) bonds.elementAt(i);
790 //Translate the bond so the centre is 0,0,0
791 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
793 //Now apply the rotation matrix
794 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
795 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
797 //Now translate back again
798 tmpBond.translate(centre[0], centre[1], centre[2]);
814 public void mouseReleased(MouseEvent evt) {
819 void drawLabels(Graphics g) {
821 for (int ii = 0; ii < pdb.chains.size(); ii++)
823 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
827 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
829 for (int i = 0; i < bonds.size(); i++)
831 Bond tmpBond = (Bond) bonds.elementAt(i);
833 if (tmpBond.at1.isSelected)
835 labelAtom(g, tmpBond, 1);
838 if (tmpBond.at2.isSelected)
841 labelAtom(g, tmpBond, 2);
848 public void labelAtom(Graphics g, Bond b, int n) {
852 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
853 (getSize().width / 2));
854 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
855 (getSize().height / 2));
857 g.setColor(Color.red);
858 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
862 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
863 (getSize().width / 2));
864 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
865 (getSize().height / 2));
867 g.setColor(Color.red);
868 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
873 public Atom findAtom(int x, int y) {
878 for (int ii = 0; ii < pdb.chains.size(); ii++)
880 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
886 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
888 for (int i = 0; i < bonds.size(); i++)
890 tmpBond = (Bond) bonds.elementAt(i);
892 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
893 (getSize().width / 2));
895 if (Math.abs(truex - x) <= 2)
897 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
898 (getSize().height / 2));
900 if (Math.abs(truey - y) <= 2)
909 // Still here? Maybe its the last bond
911 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
912 (getSize().width / 2));
914 if (Math.abs(truex - x) <= 2)
916 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
917 (getSize().height / 2));
919 if (Math.abs(truey - y) <= 2)
929 if (fatom != null) //)&& chain.ds != null)
931 chain = (PDBChain) pdb.chains.elementAt(foundchain);
938 public void update(Graphics g)
943 public void highlightRes(int ii)
948 if (highlightRes != null
949 && highlightRes.contains((ii-1) + ""))
956 for(index=0; index<mainchain.bonds.size(); index++)
958 tmpBond = (Bond) mainchain.bonds.elementAt(index);
959 if (tmpBond.at1.alignmentMapping == ii - 1)
961 if (highlightBond1 != null)
962 highlightBond1.at2.isSelected = false;
964 if (highlightBond2 != null)
965 highlightBond2.at1.isSelected = false;
967 highlightBond1 = null;
968 highlightBond2 = null;
972 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
973 highlightBond1.at2.isSelected = true;
976 if (index != mainchain.bonds.size())
978 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
979 highlightBond2.at1.isSelected = true;
991 public void setAllchainsVisible(boolean b)
993 for (int ii = 0; ii < pdb.chains.size(); ii++)
995 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
998 mainchain.isVisible = true;