2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
\r
23 import java.awt.event.*;
\r
26 import javax.swing.*;
\r
27 import jalview.analysis.AlignSeq;
\r
28 import jalview.datamodel.*;
\r
30 public class rotCanvas extends JPanel implements KeyListener,
\r
32 MouseMotionListener {
\r
33 MCMatrix idmat = new MCMatrix(3,3);
\r
34 MCMatrix objmat = new MCMatrix(3,3);
\r
36 boolean redrawneeded =true;
\r
52 float centre[] = new float[3];
\r
53 float width[] = new float[3];
\r
61 boolean depthcue = true;
\r
62 boolean wire = false;
\r
63 boolean bymolecule = false;
\r
64 boolean zbuffer = true;
\r
75 Font font = new Font("Helvetica",Font.PLAIN,10);
\r
77 public rotCanvas(PDBfile pdb, Sequence sequence, jalview.gui.AlignViewport av) throws IOException {
\r
81 int pdbstart=0, pdbend=0, seqstart =0, seqend=0;
\r
84 for (int i=0; i < pdb.chains.size(); i++) {
\r
85 // Now lets compare the sequences to get
\r
86 // the start and end points.
\r
88 java.util.StringTokenizer str = new java.util.StringTokenizer(sequence.getSequence(), ".");
\r
89 String newString = "";
\r
91 while (str.hasMoreTokens()) {
\r
92 newString += str.nextToken();
\r
94 // Align the sequence to the pdb
\r
95 AlignSeq as = new AlignSeq(sequence,((PDBChain)pdb.chains.elementAt(i)).sequence,"pep");
\r
96 as.calcScoreMatrix();
\r
97 as.traceAlignment();
\r
98 as.printAlignment();
\r
100 if (as.maxscore > max) {
\r
104 pdbstart = as.seq2start;
\r
105 pdbend = as.seq2end;
\r
106 seqstart = as.seq1start - 1 ;
\r
107 seqend = as.seq1end -1;
\r
110 System.out.println("PDB start/end " + pdbstart + " " + pdbend);
\r
111 System.out.println("SEQ start/end " + seqstart + " " + seqend);
\r
115 ( (PDBChain)pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;
\r
116 ( (PDBChain)pdb.chains.elementAt(maxchain)).pdbend = pdbend;
\r
117 ( (PDBChain)pdb.chains.elementAt(maxchain)).seqstart = seqstart;
\r
118 ( (PDBChain)pdb.chains.elementAt(maxchain)).seqend = seqend;
\r
119 ( (PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;
\r
120 ( (PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;
\r
121 ( (PDBChain)pdb.chains.elementAt(maxchain)).colourBySequence(av, sequence);
\r
126 this.prefsize = new Dimension( getWidth(), getHeight());
\r
128 //Initialize the matrices to identity
\r
130 for (int i = 0; i < 3; i++) {
\r
131 for (int j = 0; j < 3 ; j++) {
\r
133 idmat.addElement(i,j,0);
\r
134 objmat.addElement(i,j,0);
\r
136 idmat.addElement(i,j,1);
\r
137 objmat.addElement(i,j,1);
\r
142 addMouseMotionListener(this);
\r
143 addMouseListener(this);
\r
144 addKeyListener(this);
\r
147 ToolTipManager.sharedInstance().registerComponent(this);
\r
151 public void addPDBfile() {
\r
155 scale = findScale();
\r
157 System.out.println("Scale factor = " + scale);
\r
160 public void deleteBonds() {
\r
172 for (int i=0; i < pdb.chains.size(); i++) {
\r
173 ((PDBChain)pdb.chains.elementAt(i)).bonds = null;
\r
177 public void findWidth() {
\r
178 float max[] = new float[3];
\r
179 float min[] = new float[3];
\r
181 max[0] = (float)-1e30;
\r
182 max[1] = (float)-1e30;
\r
183 max[2] = (float)-1e30;
\r
185 min[0] = (float)1e30;
\r
186 min[1] = (float)1e30;
\r
187 min[2] = (float)1e30;
\r
189 for (int ii=0; ii < pdb.chains.size(); ii++) {
\r
190 if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {
\r
192 Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
194 for (int i = 0; i < bonds.size(); i++) {
\r
195 Bond tmp = (Bond)bonds.elementAt(i);
\r
197 if (tmp.start[0] >= max[0])
\r
198 max[0] = tmp.start[0];
\r
199 if (tmp.start[1] >= max[1])
\r
200 max[1] = tmp.start[1];
\r
201 if (tmp.start[2] >= max[2])
\r
202 max[2] = tmp.start[2];
\r
203 if (tmp.start[0] <= min[0])
\r
204 min[0] = tmp.start[0];
\r
205 if (tmp.start[1] <= min[1])
\r
206 min[1] = tmp.start[1];
\r
207 if (tmp.start[2] <= min[2])
\r
208 min[2] = tmp.start[2];
\r
210 if (tmp.end[0] >= max[0])
\r
211 max[0] = tmp.end[0];
\r
212 if (tmp.end[1] >= max[1])
\r
213 max[1] = tmp.end[1];
\r
214 if (tmp.end[2] >= max[2])
\r
215 max[2] = tmp.end[2];
\r
216 if (tmp.end[0] <= min[0])
\r
217 min[0] = tmp.end[0];
\r
218 if (tmp.end[1] <= min[1])
\r
219 min[1] = tmp.end[1];
\r
220 if (tmp.end[2] <= min[2])
\r
221 min[2] = tmp.end[2];
\r
226 System.out.println("xmax " + max[0] + " min " + min[0]);
\r
227 System.out.println("ymax " + max[1] + " min " + min[1]);
\r
228 System.out.println("zmax " + max[2] + " min " + min[2]);
\r
230 width[0] = (float)Math.abs(max[0] - min[0]);
\r
231 width[1] = (float)Math.abs(max[1] - min[1]);
\r
232 width[2] = (float)Math.abs(max[2] - min[2]);
\r
234 maxwidth = width[0];
\r
236 if (width[1] > width[0])
\r
237 maxwidth = width[1];
\r
238 if (width[2] > width[1])
\r
239 maxwidth = width[2];
\r
241 System.out.println("Maxwidth = " + maxwidth);
\r
244 public float findScale() {
\r
245 int dim, width, height;
\r
247 if ( getWidth() != 0) {
\r
248 width = getWidth();
\r
249 height = getHeight();
\r
252 width = prefsize.width;
\r
253 height = prefsize.height;
\r
256 if (width < height) {
\r
262 return (float)(dim/(1.5d*maxwidth));
\r
267 public void findCentre() {
\r
273 //Find centre coordinate
\r
275 for (int ii = 0; ii < pdb.chains.size() ; ii++) {
\r
276 if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {
\r
278 Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
280 bsize += bonds.size();
\r
282 for (int i = 0; i < bonds.size(); i++) {
\r
284 xtot = xtot + ((Bond)bonds.elementAt(i)).start[0] +
\r
285 ((Bond)bonds.elementAt(i)).end[0];
\r
287 ytot = ytot + ((Bond)bonds.elementAt(i)).start[1] +
\r
288 ((Bond)bonds.elementAt(i)).end[1];
\r
290 ztot = ztot + ((Bond)bonds.elementAt(i)).start[2] +
\r
291 ((Bond)bonds.elementAt(i)).end[2];
\r
296 centre[0] = xtot / (2 * (float)bsize);
\r
297 centre[1] = ytot / (2 * (float)bsize);
\r
298 centre[2] = ztot / (2 * (float)bsize);
\r
301 public void paint(Graphics g) {
\r
302 //Only create the image at the beginning -
\r
303 //this saves much memory usage
\r
304 if ((img == null) || (prefsize.width != getWidth()) || (prefsize.height != getHeight())) {
\r
305 prefsize.width = getWidth();
\r
306 prefsize.height = getHeight();
\r
308 scale = findScale();
\r
309 img = createImage(prefsize.width,prefsize.height);
\r
310 ig = img.getGraphics();
\r
312 redrawneeded = true;
\r
315 if (redrawneeded == true) {
\r
316 drawBackground(ig,Color.black);
\r
318 redrawneeded = false;
\r
320 ig = img.getGraphics();
\r
323 g.drawImage(img,0,0,this);
\r
326 public void drawBackground(Graphics g, Color col) {
\r
328 g.fillRect(0,0,prefsize.width,prefsize.height);
\r
332 public void drawScene(Graphics g) {
\r
333 // Sort the bonds by z coord
\r
335 Vector bonds = new Vector();
\r
338 for (int ii = 0; ii < pdb.chains.size() ; ii++) {
\r
339 if (((PDBChain)pdb.chains.elementAt(ii)).isVisible) {
\r
341 Vector tmp = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
343 for (int i=0; i < tmp.size(); i++) {
\r
344 bonds.addElement(tmp.elementAt(i));
\r
350 Zsort.Zsort(bonds);
\r
353 for (int i = 0; i < bonds.size(); i++) {
\r
354 Bond tmpBond = (Bond)bonds.elementAt(i);
\r
356 xstart = (int)((tmpBond.start[0] - centre[0])*scale + getWidth()/2);
\r
357 ystart = (int)((tmpBond.start[1] - centre[1])*scale + getHeight()/2);
\r
359 xend = (int)((tmpBond.end[0] - centre[0])*scale + getWidth()/2);
\r
360 yend = (int)((tmpBond.end[1] - centre[1])*scale + getHeight()/2);
\r
362 xmid = (xend+xstart)/2;
\r
363 ymid = (yend+ystart)/2;
\r
366 if (depthcue && !bymolecule) {
\r
368 if (tmpBond.start[2] < (centre[2] - maxwidth/6)) {
\r
369 g.setColor(tmpBond.startCol.darker().darker());
\r
370 drawLine(g,xstart,ystart,xmid,ymid);
\r
372 g.setColor(tmpBond.endCol.darker().darker());
\r
373 drawLine(g,xmid,ymid,xend,yend);
\r
374 } else if (tmpBond.start[2] < (centre[2]+maxwidth/6)) {
\r
375 g.setColor(tmpBond.startCol.darker());
\r
376 drawLine(g,xstart,ystart,xmid,ymid);
\r
378 g.setColor(tmpBond.endCol.darker());
\r
379 drawLine(g,xmid,ymid,xend,yend);
\r
381 g.setColor(tmpBond.startCol);
\r
382 drawLine(g,xstart,ystart,xmid,ymid);
\r
384 g.setColor(tmpBond.endCol);
\r
385 drawLine(g,xmid,ymid,xend,yend);
\r
387 } else if (depthcue && bymolecule) {
\r
388 if (tmpBond.start[2] < (centre[2] - maxwidth/6)) {
\r
389 g.setColor(Color.green.darker().darker());
\r
390 drawLine(g,xstart,ystart,xend,yend);
\r
392 } else if (tmpBond.start[2] < (centre[2]+maxwidth/6)) {
\r
393 g.setColor(Color.green.darker());
\r
394 drawLine(g,xstart,ystart,xend,yend);
\r
397 g.setColor(Color.green);
\r
398 drawLine(g,xstart,ystart,xend,yend);
\r
401 } else if (!depthcue && !bymolecule) {
\r
402 g.setColor(tmpBond.startCol);
\r
403 drawLine(g,xstart,ystart,xmid,ymid);
\r
404 g.setColor(tmpBond.endCol);
\r
405 drawLine(g,xmid,ymid,xend,yend);
\r
408 drawLine(g,xstart,ystart,xend,yend);
\r
413 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
\r
416 if (((float)Math.abs(y2-y1)/(float)Math.abs(x2-x1)) < 0.5) {
\r
418 g.drawLine(x1,y1,x2,y2);
\r
419 g.drawLine(x1+1,y1+1,x2+1,y2+1);
\r
420 g.drawLine(x1,y1-1,x2,y2-1);
\r
422 g.setColor(g.getColor().brighter());
\r
423 g.drawLine(x1,y1,x2,y2);
\r
424 g.drawLine(x1+1,y1,x2+1,y2);
\r
425 g.drawLine(x1-1,y1,x2-1,y2);
\r
429 g.drawLine(x1,y1,x2,y2);
\r
433 public Dimension minimumsize() {
\r
436 public Dimension preferredsize() {
\r
440 public void keyTyped(KeyEvent evt) { }
\r
441 public void keyReleased(KeyEvent evt) { }
\r
442 public void keyPressed(KeyEvent evt) {
\r
443 int key = evt.getKeyChar();
\r
445 if (evt.getKeyCode() == KeyEvent.VK_UP) {
\r
446 scale = (float)(scale * 1.1);
\r
447 redrawneeded = true;
\r
450 } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
\r
451 scale = (float)(scale * 0.9);
\r
452 redrawneeded = true;
\r
455 } else if (key == 'w') {
\r
458 System.out.println("wireframe " + wire);
\r
459 redrawneeded = true;
\r
462 } else if (key == 'd') {
\r
463 depthcue = !depthcue;
\r
464 System.out.println("Depth cueing is " + depthcue);
\r
465 redrawneeded = true;
\r
468 } else if (key == 'm') {
\r
469 bymolecule = !bymolecule;
\r
470 System.out.println("Bymolecule is " + bymolecule);
\r
471 redrawneeded = true;
\r
474 } else if (key == 'z') {
\r
475 zbuffer = !zbuffer;
\r
476 System.out.println("Z buffering is " + zbuffer);
\r
477 redrawneeded = true;
\r
480 } else if (key == 'c') {
\r
481 bymolecule = false;
\r
482 pdb.setChainColours();
\r
483 System.out.println("Colouring by chain");
\r
484 redrawneeded = true;
\r
487 } else if (key == 'h') {
\r
488 bymolecule = false;
\r
489 pdb.setHydrophobicityColours();
\r
490 System.out.println("Colouring by hydrophobicity");
\r
491 redrawneeded = true;
\r
494 } else if (key == 'q') {
\r
495 bymolecule = false;
\r
496 pdb.setChargeColours();
\r
497 System.out.println("Colouring charges and cysteines");
\r
498 redrawneeded = true;
\r
505 public void mousePressed(MouseEvent e)
\r
515 public void mouseMoved(MouseEvent e)
\r
517 myAtom fatom = null;
\r
518 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
521 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
522 if (chain.isVisible)
\r
524 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
525 for (int i = 0; i < bonds.size(); i++)
\r
527 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
528 int truex = (int) ( (tmpBond.start[0] - centre[0]) * scale +
\r
530 if (Math.abs(truex - e.getX()) <= 2)
\r
532 int truey = (int) ( (tmpBond.start[1] - centre[1]) * scale +
\r
534 if (Math.abs(truey - e.getY()) <= 2)
\r
535 fatom = tmpBond.at1;
\r
542 this.setToolTipText(fatom.resName);
\r
545 this.setToolTipText(null);
\r
548 public void mouseClicked(MouseEvent e) { }
\r
549 public void mouseEntered(MouseEvent e) { }
\r
550 public void mouseExited(MouseEvent e) { }
\r
552 public void mouseDragged(MouseEvent evt) {
\r
553 int x = evt.getX();
\r
554 int y = evt.getY();
\r
558 MCMatrix objmat = new MCMatrix(3,3);
\r
559 objmat.setIdentity();
\r
561 if ((evt.getModifiers() & Event.META_MASK) != 0) {
\r
562 objmat.rotatez((float)((mx-omx)));
\r
564 objmat.rotatex((float)((my-omy)));
\r
565 objmat.rotatey((float)((omx-mx)));
\r
569 for (int ii = 0; ii < pdb.chains.size() ; ii++) {
\r
570 Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
572 for (int i = 0; i < bonds.size(); i++) {
\r
574 Bond tmpBond = (Bond)bonds.elementAt(i);
\r
576 //Translate the bond so the centre is 0,0,0
\r
577 tmpBond.translate(-centre[0],-centre[1],-centre[2]);
\r
579 //Now apply the rotation matrix
\r
580 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
\r
581 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
\r
583 //Now translate back again
\r
584 tmpBond.translate(centre[0],centre[1],centre[2]);
\r
593 redrawneeded = true;
\r
595 paint(this.getGraphics());
\r
601 public void mouseReleased(MouseEvent evt) {
\r
602 int x = evt.getX();
\r
603 int y = evt.getY();
\r
606 myAtom tmp = findAtom(x,y);
\r
612 public void drawLabels() {
\r
613 redrawneeded = true;
\r
614 paint(this.getGraphics());
\r
616 for (int ii = 0; ii < pdb.chains.size() ; ii++) {
\r
618 PDBChain chain = (PDBChain)pdb.chains.elementAt(ii);
\r
620 if (chain.isVisible) {
\r
622 Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
624 for (int i = 0; i < bonds.size(); i++) {
\r
625 Bond tmpBond = (Bond)bonds.elementAt(i);
\r
627 if (tmpBond.at1.isSelected) {
\r
628 labelAtom(img.getGraphics(),tmpBond,1);
\r
631 if (tmpBond.at2.isSelected) {
\r
632 labelAtom(img.getGraphics(),tmpBond,2);
\r
639 this.getGraphics().drawImage(img,0,0,this);
\r
645 public void labelAtom(Graphics g,Bond b, int n) {
\r
651 int xstart = (int)((b.start[0] - centre[0])*scale + getWidth()/2);
\r
652 int ystart = (int)((b.start[1] - centre[1])*scale + getHeight()/2);
\r
654 g.setColor(Color.red);
\r
655 g.drawString(b.at1.resName + "-" + b.at1.resNumber,xstart,ystart);
\r
660 int xstart = (int)((b.end[0] - centre[0])*scale + getWidth()/2);
\r
661 int ystart = (int)((b.end[1] - centre[1])*scale + getHeight()/2);
\r
663 g.setColor(Color.red);
\r
664 g.drawString(b.at2.resName + "-" + b.at2.resNumber,xstart,ystart);
\r
669 public myAtom findAtom(int x, int y) {
\r
670 myAtom fatom = null;
\r
672 int foundchain = -1;
\r
674 for (int ii = 0; ii < pdb.chains.size() ; ii++) {
\r
676 PDBChain chain = (PDBChain)pdb.chains.elementAt(ii);
\r
678 if (chain.isVisible) {
\r
680 Vector bonds = ((PDBChain)pdb.chains.elementAt(ii)).bonds;
\r
682 for (int i = 0; i < bonds.size(); i++) {
\r
683 Bond tmpBond = (Bond)bonds.elementAt(i);
\r
685 int truex = (int)((tmpBond.start[0] - centre[0])*scale + getWidth()/2);
\r
687 if (Math.abs(truex - x) <= 2) {
\r
689 int truey = (int)((tmpBond.start[1] - centre[1])*scale + getHeight()/2);
\r
691 if (Math.abs(truey - y) <= 2) {
\r
693 System.out.println("Found match");
\r
694 System.out.println(x + " " + y);
\r
695 System.out.println(truex + " " + truey);
\r
696 System.out.println(tmpBond.start[0] + " " + tmpBond.start[1]);
\r
697 System.out.println("Atom 1 = " + tmpBond.at1.resName + " " +
\r
698 tmpBond.at1.resNumber + " " + tmpBond.at1.chain);
\r
699 fatom = tmpBond.at1;
\r
700 fatom.isSelected = !fatom.isSelected;
\r
707 if (fatom != null) //)&& chain.ds != null)
\r
709 chain = (PDBChain)pdb.chains.elementAt(foundchain);
\r
711 // int tmp = chain.ds.seqstart + fatom.resNumber - chain.offset;
\r
712 // int pos = chain.ds.findIndex(tmp);
\r
713 // System.out.println("Found seq " + chain.ds.name + " " + tmp + " " + pos);
\r
720 public void update(Graphics g) {
\r