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 import jalview.analysis.AlignSeq;
\r
23 import jalview.datamodel.*;
\r
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
\r
27 import java.awt.event.*;
\r
35 public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener
\r
38 MCMatrix idmat = new MCMatrix(3, 3);
\r
39 MCMatrix objmat = new MCMatrix(3, 3);
\r
40 boolean redrawneeded = true;
\r
50 float[] centre = new float[3];
\r
51 float[] width = new float[3];
\r
56 boolean bysequence = true;
\r
57 boolean depthcue = true;
\r
58 boolean wire = false;
\r
59 boolean bymolecule = false;
\r
60 boolean zbuffer = true;
\r
68 Font font = new Font("Helvetica", Font.PLAIN, 10);
\r
69 jalview.appletgui.SequenceRenderer sr;
\r
70 jalview.appletgui.FeatureRenderer fr;
\r
71 jalview.appletgui.SeqCanvas seqcanvas;
\r
72 public Sequence sequence;
\r
73 final StringBuffer mappingDetails = new StringBuffer();
\r
74 String appletToolTip = null;
\r
78 public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)
\r
80 this.seqcanvas = seqcanvas;
\r
81 this.sequence = seq;
\r
82 sr = seqcanvas.getSequenceRenderer();
\r
83 fr = seqcanvas.getFeatureRenderer();
\r
85 seqcanvas.setPDBCanvas(this);
\r
86 addKeyListener(new KeyAdapter()
\r
89 public void keyPressed(KeyEvent evt)
\r
96 Bond highlightBond1, highlightBond2;
\r
97 public void highlightRes(int ii)
\r
99 highlightBond1 = null;
\r
100 highlightBond2 = null;
\r
102 int index = ii - mainchain.seqstart;
\r
106 if(index<=mainchain.bonds.size())
\r
110 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
\r
113 if(index!=mainchain.bonds.size())
\r
114 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
\r
117 redrawneeded = true;
\r
122 public void setPDBFile(PDBfile pdb)
\r
133 for (int i = 0; i < pdb.chains.size(); i++)
\r
136 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence());
\r
137 mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");
\r
139 // Now lets compare the sequences to get
\r
140 // the start and end points.
\r
141 // Align the sequence to the pdb
\r
142 AlignSeq as = new AlignSeq(sequence,
\r
143 ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
\r
144 as.calcScoreMatrix();
\r
145 as.traceAlignment();
\r
146 PrintStream ps = new PrintStream(System.out)
\r
148 public void print(String x) {
\r
149 mappingDetails.append(x);
\r
151 public void println()
\r
153 mappingDetails.append("\n");
\r
157 as.printAlignment(ps);
\r
159 if (as.maxscore > max) {
\r
163 pdbstart = as.seq2start;
\r
164 pdbend = as.seq2end;
\r
165 seqstart = as.seq1start + sequence.getStart()-1;
\r
166 seqend = as.seq1end + sequence.getEnd()-1;
\r
169 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
\r
170 mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);
\r
173 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
\r
175 mainchain.pdbstart = pdbstart;
\r
176 mainchain.pdbend = pdbend;
\r
177 mainchain.seqstart = seqstart;
\r
178 mainchain.seqend = seqend;
\r
179 mainchain.isVisible = true;
\r
180 mainchain.sequence = sequence;
\r
183 this.prefsize = new Dimension(getSize().width, getSize().height);
\r
185 //Initialize the matrices to identity
\r
186 for (int i = 0; i < 3; i++) {
\r
187 for (int j = 0; j < 3; j++) {
\r
189 idmat.addElement(i, j, 0);
\r
190 objmat.addElement(i, j, 0);
\r
192 idmat.addElement(i, j, 1);
\r
193 objmat.addElement(i, j, 1);
\r
198 addMouseMotionListener(this);
\r
199 addMouseListener(this);
\r
202 SequenceGroup sg = new SequenceGroup("PDB",
\r
203 null, true,true,false,
\r
204 sequence.findIndex(seqstart-1),
\r
205 sequence.findIndex(seqend-1));
\r
206 sg.addSequence(sequence, false);
\r
207 sg.setOutlineColour(Color.black);
\r
208 seqcanvas.getViewport().getAlignment().addGroup(sg);
\r
214 scale = findScale();
\r
216 updateSeqColours();
\r
219 public void deleteBonds() {
\r
231 for (int i = 0; i < pdb.chains.size(); i++) {
\r
232 ((PDBChain) pdb.chains.elementAt(i)).bonds = null;
\r
236 public void findWidth() {
\r
237 float[] max = new float[3];
\r
238 float[] min = new float[3];
\r
240 max[0] = (float) -1e30;
\r
241 max[1] = (float) -1e30;
\r
242 max[2] = (float) -1e30;
\r
244 min[0] = (float) 1e30;
\r
245 min[1] = (float) 1e30;
\r
246 min[2] = (float) 1e30;
\r
248 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
249 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
250 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
252 for (int i = 0; i < bonds.size(); i++) {
\r
253 Bond tmp = (Bond) bonds.elementAt(i);
\r
255 if (tmp.start[0] >= max[0]) {
\r
256 max[0] = tmp.start[0];
\r
259 if (tmp.start[1] >= max[1]) {
\r
260 max[1] = tmp.start[1];
\r
263 if (tmp.start[2] >= max[2]) {
\r
264 max[2] = tmp.start[2];
\r
267 if (tmp.start[0] <= min[0]) {
\r
268 min[0] = tmp.start[0];
\r
271 if (tmp.start[1] <= min[1]) {
\r
272 min[1] = tmp.start[1];
\r
275 if (tmp.start[2] <= min[2]) {
\r
276 min[2] = tmp.start[2];
\r
279 if (tmp.end[0] >= max[0]) {
\r
280 max[0] = tmp.end[0];
\r
283 if (tmp.end[1] >= max[1]) {
\r
284 max[1] = tmp.end[1];
\r
287 if (tmp.end[2] >= max[2]) {
\r
288 max[2] = tmp.end[2];
\r
291 if (tmp.end[0] <= min[0]) {
\r
292 min[0] = tmp.end[0];
\r
295 if (tmp.end[1] <= min[1]) {
\r
296 min[1] = tmp.end[1];
\r
299 if (tmp.end[2] <= min[2]) {
\r
300 min[2] = tmp.end[2];
\r
306 width[0] = (float) Math.abs(max[0] - min[0]);
\r
307 width[1] = (float) Math.abs(max[1] - min[1]);
\r
308 width[2] = (float) Math.abs(max[2] - min[2]);
\r
310 maxwidth = width[0];
\r
312 if (width[1] > width[0]) {
\r
313 maxwidth = width[1];
\r
316 if (width[2] > width[1]) {
\r
317 maxwidth = width[2];
\r
320 // System.out.println("Maxwidth = " + maxwidth);
\r
323 public float findScale() {
\r
328 if (getSize().width != 0) {
\r
329 width = getSize().width;
\r
330 height = getSize().height;
\r
332 width = prefsize.width;
\r
333 height = prefsize.height;
\r
336 if (width < height) {
\r
342 return (float) (dim / (1.5d * maxwidth));
\r
345 public void findCentre() {
\r
352 //Find centre coordinate
\r
353 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
354 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {
\r
355 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
357 bsize += bonds.size();
\r
359 for (int i = 0; i < bonds.size(); i++) {
\r
360 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +
\r
361 ((Bond) bonds.elementAt(i)).end[0];
\r
363 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +
\r
364 ((Bond) bonds.elementAt(i)).end[1];
\r
366 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +
\r
367 ((Bond) bonds.elementAt(i)).end[2];
\r
372 centre[0] = xtot / (2 * (float) bsize);
\r
373 centre[1] = ytot / (2 * (float) bsize);
\r
374 centre[2] = ztot / (2 * (float) bsize);
\r
377 public void paint(Graphics g)
\r
382 g.setColor(Color.black);
\r
383 g.setFont(new Font("Verdana", Font.BOLD, 14));
\r
384 g.drawString("Error Parsing Pasted PDB data!!", 50, getSize().height/2);
\r
389 //Only create the image at the beginning -
\r
390 //this saves much memory usage
\r
391 if ((img == null) || (prefsize.width != getSize().width) ||
\r
392 (prefsize.height != getSize().height)) {
\r
394 try{ prefsize.width = getSize().width;
\r
395 prefsize.height = getSize().height;
\r
397 scale = findScale();
\r
398 img = createImage(prefsize.width, prefsize.height);
\r
399 ig = img.getGraphics();
\r
401 redrawneeded = true;
\r
402 }catch(Exception ex)
\r
404 ex.printStackTrace();
\r
405 System.out.println(getSize());
\r
412 drawAll(ig, prefsize.width, prefsize.height);
\r
413 redrawneeded = false;
\r
415 if(appletToolTip!=null)
\r
417 ig.setColor(Color.red);
\r
418 ig.drawString(appletToolTip, toolx, tooly);
\r
421 g.drawImage(img, 0, 0, this);
\r
426 public void drawAll(Graphics g, int width, int height)
\r
428 g.setColor(Color.black);
\r
429 g.fillRect(0, 0, width, height);
\r
435 public void updateSeqColours()
\r
437 if(bysequence && pdb!=null)
\r
439 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
441 colourBySequence((PDBChain) pdb.chains.elementAt(ii));
\r
449 // This method has been taken out of PDBChain to allow
\r
450 // Applet and Application specific sequence renderers to be used
\r
451 void colourBySequence(PDBChain chain)
\r
453 for (int i = 0; i < chain.bonds.size(); i++)
\r
455 Bond tmp = (Bond) chain.bonds.elementAt(i);
\r
456 if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
457 (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
\r
460 int pos = chain.seqstart +
\r
461 (tmp.at1.resNumber - chain.pdbstart - chain.offset);
\r
463 int index = sequence.findIndex(pos);
\r
465 if(jalview.util.Comparison.isGap((sequence.getCharAt(index))))
\r
470 tmp.startCol = sr.findSequenceColour(sequence, index);
\r
472 // tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
\r
477 tmp.startCol = Color.gray;
\r
480 if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
\r
481 (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))
\r
483 int pos = chain.seqstart +
\r
484 (tmp.at2.resNumber - chain.pdbstart - chain.offset);
\r
485 int index = sequence.findIndex(pos);
\r
486 if (jalview.util.Comparison.isGap( (sequence.getCharAt(index))))
\r
492 tmp.endCol = sr.findSequenceColour(sequence, index);
\r
493 // tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
\r
497 tmp.endCol = Color.gray;
\r
503 public void drawScene(Graphics g) {
\r
504 // Sort the bonds by z coord
\r
505 Vector bonds = new Vector();
\r
507 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
509 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
\r
511 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
513 for (int i = 0; i < tmp.size(); i++)
\r
515 bonds.addElement(tmp.elementAt(i));
\r
521 Zsort.Zsort(bonds);
\r
524 for (int i = 0; i < bonds.size(); i++) {
\r
525 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
528 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
529 (getSize().width / 2));
\r
530 ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
531 (getSize().height / 2));
\r
533 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +
\r
534 (getSize().width / 2));
\r
535 yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +
\r
536 (getSize().height / 2));
\r
538 xmid = (xend + xstart) / 2;
\r
539 ymid = (yend + ystart) / 2;
\r
541 if (depthcue && !bymolecule)
\r
543 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
544 g.setColor(tmpBond.startCol.darker().darker());
\r
545 drawLine(g, xstart, ystart, xmid, ymid);
\r
547 g.setColor(tmpBond.endCol.darker().darker());
\r
548 drawLine(g, xmid, ymid, xend, yend);
\r
549 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
550 g.setColor(tmpBond.startCol.darker());
\r
551 drawLine(g, xstart, ystart, xmid, ymid);
\r
553 g.setColor(tmpBond.endCol.darker());
\r
554 drawLine(g, xmid, ymid, xend, yend);
\r
556 g.setColor(tmpBond.startCol);
\r
557 drawLine(g, xstart, ystart, xmid, ymid);
\r
559 g.setColor(tmpBond.endCol);
\r
560 drawLine(g, xmid, ymid, xend, yend);
\r
562 } else if (depthcue && bymolecule) {
\r
563 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {
\r
564 g.setColor(Color.green.darker().darker());
\r
565 drawLine(g, xstart, ystart, xend, yend);
\r
566 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {
\r
567 g.setColor(Color.green.darker());
\r
568 drawLine(g, xstart, ystart, xend, yend);
\r
570 g.setColor(Color.green);
\r
571 drawLine(g, xstart, ystart, xend, yend);
\r
573 } else if (!depthcue && !bymolecule) {
\r
574 g.setColor(tmpBond.startCol);
\r
575 drawLine(g, xstart, ystart, xmid, ymid);
\r
576 g.setColor(tmpBond.endCol);
\r
577 drawLine(g, xmid, ymid, xend, yend);
\r
579 drawLine(g, xstart, ystart, xend, yend);
\r
582 if(highlightBond1!=null && highlightBond1==tmpBond)
\r
584 g.setColor(Color.white);
\r
585 drawLine(g, xmid, ymid, xend, yend);
\r
588 if(highlightBond2!=null && highlightBond2==tmpBond)
\r
590 g.setColor(Color.white);
\r
591 drawLine(g, xstart, ystart, xmid, ymid);
\r
597 public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
\r
599 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {
\r
600 g.drawLine(x1, y1, x2, y2);
\r
601 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
\r
602 g.drawLine(x1, y1 - 1, x2, y2 - 1);
\r
604 g.setColor(g.getColor().brighter());
\r
605 g.drawLine(x1, y1, x2, y2);
\r
606 g.drawLine(x1 + 1, y1, x2 + 1, y2);
\r
607 g.drawLine(x1 - 1, y1, x2 - 1, y2);
\r
610 g.drawLine(x1, y1, x2, y2);
\r
614 public Dimension minimumsize() {
\r
618 public Dimension preferredsize() {
\r
622 public void doKeyPressed(KeyEvent evt)
\r
624 if (evt.getKeyCode() == KeyEvent.VK_UP)
\r
626 scale = (float) (scale * 1.1);
\r
627 redrawneeded = true;
\r
630 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
\r
632 scale = (float) (scale * 0.9);
\r
633 redrawneeded = true;
\r
638 public void mousePressed(MouseEvent e) {
\r
639 Atom fatom = findAtom(e.getX(), e.getY());
\r
642 fatom.isSelected = !fatom.isSelected;
\r
643 redrawneeded = true;
\r
653 public void mouseMoved(MouseEvent e) {
\r
655 Atom fatom = findAtom(e.getX(), e.getY());
\r
657 PDBChain chain = null;
\r
660 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
661 if(chain == mainchain)
\r
663 int pos = chain.seqstart +
\r
664 (fatom.resNumber - chain.pdbstart - chain.offset) + 1;
\r
666 int index = seqcanvas.getViewport().getAlignment().findIndex(
\r
669 seqcanvas.highlightSearchResults(new int[]
\r
670 {index, pos, pos});
\r
674 seqcanvas.highlightSearchResults(null);
\r
676 if (fatom != null) {
\r
680 appletToolTip = chain.id+":"+ fatom.resNumber+" "+ fatom.resName;
\r
681 redrawneeded = true;
\r
684 appletToolTip = null;
\r
685 redrawneeded = true;
\r
690 public void mouseClicked(MouseEvent e) {
\r
693 public void mouseEntered(MouseEvent e) {
\r
696 public void mouseExited(MouseEvent e) {
\r
699 public void mouseDragged(MouseEvent evt) {
\r
700 int x = evt.getX();
\r
701 int y = evt.getY();
\r
705 MCMatrix objmat = new MCMatrix(3, 3);
\r
706 objmat.setIdentity();
\r
708 if ((evt.getModifiers() & Event.META_MASK) != 0) {
\r
709 objmat.rotatez((float) ((mx - omx)));
\r
711 objmat.rotatex((float) ((my - omy)));
\r
712 objmat.rotatey((float) ((omx - mx)));
\r
716 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
717 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
719 for (int i = 0; i < bonds.size(); i++) {
\r
720 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
722 //Translate the bond so the centre is 0,0,0
\r
723 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
\r
725 //Now apply the rotation matrix
\r
726 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
\r
727 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
\r
729 //Now translate back again
\r
730 tmpBond.translate(centre[0], centre[1], centre[2]);
\r
741 redrawneeded = true;
\r
746 public void mouseReleased(MouseEvent evt) {
\r
751 void drawLabels(Graphics g) {
\r
753 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
755 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
757 if (chain.isVisible)
\r
759 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
761 for (int i = 0; i < bonds.size(); i++)
\r
763 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
765 if (tmpBond.at1.isSelected)
\r
767 labelAtom(g, tmpBond, 1);
\r
770 if (tmpBond.at2.isSelected)
\r
773 labelAtom(g, tmpBond, 2);
\r
780 public void labelAtom(Graphics g, Bond b, int n) {
\r
784 int xstart = (int) (((b.start[0] - centre[0]) * scale) +
\r
785 (getSize().width / 2));
\r
786 int ystart = (int) (((b.start[1] - centre[1]) * scale) +
\r
787 (getSize().height / 2));
\r
789 g.setColor(Color.red);
\r
790 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
\r
794 int xstart = (int) (((b.end[0] - centre[0]) * scale) +
\r
795 (getSize().width / 2));
\r
796 int ystart = (int) (((b.end[1] - centre[1]) * scale) +
\r
797 (getSize().height / 2));
\r
799 g.setColor(Color.red);
\r
800 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
\r
804 int foundchain = -1;
\r
805 public Atom findAtom(int x, int y) {
\r
810 for (int ii = 0; ii < pdb.chains.size(); ii++) {
\r
811 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
813 if (chain.isVisible) {
\r
814 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
\r
816 for (int i = 0; i < bonds.size(); i++) {
\r
817 Bond tmpBond = (Bond) bonds.elementAt(i);
\r
819 int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +
\r
820 (getSize().width / 2));
\r
822 if (Math.abs(truex - x) <= 2) {
\r
823 int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +
\r
824 (getSize().height / 2));
\r
826 if (Math.abs(truey - y) <= 2)
\r
828 fatom = tmpBond.at1;
\r
836 if (fatom != null) //)&& chain.ds != null)
\r
838 chain = (PDBChain) pdb.chains.elementAt(foundchain);
\r
845 public void update(Graphics g)
\r
850 public void setAllchainsVisible(boolean b)
\r
852 for (int ii = 0; ii < pdb.chains.size(); ii++)
\r
854 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
\r
855 chain.isVisible = b;
\r
857 mainchain.isVisible = true;
\r