2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
25 import java.awt.event.*;
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
30 import jalview.appletgui.*;
31 import jalview.structure.*;
33 public class AppletPDBCanvas extends Panel implements MouseListener,
34 MouseMotionListener, StructureListener
37 MCMatrix idmat = new MCMatrix(3, 3);
39 MCMatrix objmat = new MCMatrix(3, 3);
41 boolean redrawneeded = true;
63 float[] centre = new float[3];
65 float[] width = new float[3];
75 boolean bysequence = true;
77 boolean depthcue = true;
81 boolean bymolecule = false;
83 boolean zbuffer = true;
99 Font font = new Font("Helvetica", Font.PLAIN, 10);
101 public SequenceI[] sequence;
103 final StringBuffer mappingDetails = new StringBuffer();
105 String appletToolTip = null;
113 boolean pdbAction = false;
115 Bond highlightBond1, highlightBond2;
117 boolean errorLoading = false;
119 boolean seqColoursReady = false;
125 StructureSelectionManager ssm;
127 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
128 String[] chains, AlignmentPanel ap, String protocol)
132 this.pdbentry = pdbentry;
135 ssm = StructureSelectionManager.getStructureSelectionManager();
139 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
141 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
142 pdbentry.setFile("INLINE" + pdb.id);
144 } catch (Exception ex)
146 ex.printStackTrace();
150 pdbentry.setId(pdb.id);
152 ssm.addStructureViewerListener(this);
163 // JUST DEAL WITH ONE SEQUENCE FOR NOW
164 SequenceI sequence = seq[0];
166 for (int i = 0; i < pdb.chains.size(); i++)
169 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
170 + ((PDBChain) pdb.chains.elementAt(i)).sequence
171 .getSequenceAsString());
172 mappingDetails.append("\nNo of residues = "
173 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
176 // Now lets compare the sequences to get
177 // the start and end points.
178 // Align the sequence to the pdb
179 AlignSeq as = new AlignSeq(sequence, ((PDBChain) pdb.chains
180 .elementAt(i)).sequence, "pep");
181 as.calcScoreMatrix();
183 PrintStream ps = new PrintStream(System.out)
185 public void print(String x)
187 mappingDetails.append(x);
190 public void println()
192 mappingDetails.append("\n");
196 as.printAlignment(ps);
198 if (as.maxscore > max)
203 pdbstart = as.seq2start;
205 seqstart = as.seq1start + sequence.getStart() - 1;
206 seqend = as.seq1end + sequence.getEnd() - 1;
209 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
210 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
213 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
215 mainchain.pdbstart = pdbstart;
216 mainchain.pdbend = pdbend;
217 mainchain.seqstart = seqstart;
218 mainchain.seqend = seqend;
219 mainchain.isVisible = true;
220 // mainchain.makeExactMapping(maxAlignseq, sequence);
221 // mainchain.transferRESNUMFeatures(sequence, null);
223 this.prefsize = new Dimension(getSize().width, getSize().height);
225 // Initialize the matrices to identity
226 for (int i = 0; i < 3; i++)
228 for (int j = 0; j < 3; j++)
232 idmat.addElement(i, j, 0);
233 objmat.addElement(i, j, 0);
237 idmat.addElement(i, j, 1);
238 objmat.addElement(i, j, 1);
243 addMouseMotionListener(this);
244 addMouseListener(this);
246 addKeyListener(new KeyAdapter()
248 public void keyPressed(KeyEvent evt)
266 seqColoursReady = false;
267 // Sort the bonds by z coord
268 visiblebonds = new Vector();
270 for (int ii = 0; ii < pdb.chains.size(); ii++)
272 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
274 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
276 for (int i = 0; i < tmp.size(); i++)
278 visiblebonds.addElement(tmp.elementAt(i));
282 seqColoursReady = true;
288 public void findWidth()
290 float[] max = new float[3];
291 float[] min = new float[3];
293 max[0] = (float) -1e30;
294 max[1] = (float) -1e30;
295 max[2] = (float) -1e30;
297 min[0] = (float) 1e30;
298 min[1] = (float) 1e30;
299 min[2] = (float) 1e30;
301 for (int ii = 0; ii < pdb.chains.size(); ii++)
303 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
305 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
307 for (int i = 0; i < bonds.size(); i++)
309 Bond tmp = (Bond) bonds.elementAt(i);
311 if (tmp.start[0] >= max[0])
313 max[0] = tmp.start[0];
316 if (tmp.start[1] >= max[1])
318 max[1] = tmp.start[1];
321 if (tmp.start[2] >= max[2])
323 max[2] = tmp.start[2];
326 if (tmp.start[0] <= min[0])
328 min[0] = tmp.start[0];
331 if (tmp.start[1] <= min[1])
333 min[1] = tmp.start[1];
336 if (tmp.start[2] <= min[2])
338 min[2] = tmp.start[2];
341 if (tmp.end[0] >= max[0])
346 if (tmp.end[1] >= max[1])
351 if (tmp.end[2] >= max[2])
356 if (tmp.end[0] <= min[0])
361 if (tmp.end[1] <= min[1])
366 if (tmp.end[2] <= min[2])
374 width[0] = (float) Math.abs(max[0] - min[0]);
375 width[1] = (float) Math.abs(max[1] - min[1]);
376 width[2] = (float) Math.abs(max[2] - min[2]);
380 if (width[1] > width[0])
385 if (width[2] > width[1])
390 // System.out.println("Maxwidth = " + maxwidth);
393 public float findScale()
399 if (getSize().width != 0)
401 width = getSize().width;
402 height = getSize().height;
406 width = prefsize.width;
407 height = prefsize.height;
419 return (float) (dim / (1.5d * maxwidth));
422 public void findCentre()
430 // Find centre coordinate
431 for (int ii = 0; ii < pdb.chains.size(); ii++)
433 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
435 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
437 bsize += bonds.size();
439 for (int i = 0; i < bonds.size(); i++)
441 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
442 + ((Bond) bonds.elementAt(i)).end[0];
444 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
445 + ((Bond) bonds.elementAt(i)).end[1];
447 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
448 + ((Bond) bonds.elementAt(i)).end[2];
453 centre[0] = xtot / (2 * (float) bsize);
454 centre[1] = ytot / (2 * (float) bsize);
455 centre[2] = ztot / (2 * (float) bsize);
458 public void paint(Graphics g)
463 g.setColor(Color.white);
464 g.fillRect(0, 0, getSize().width, getSize().height);
465 g.setColor(Color.black);
466 g.setFont(new Font("Verdana", Font.BOLD, 14));
467 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
471 if (!seqColoursReady)
473 g.setColor(Color.black);
474 g.setFont(new Font("Verdana", Font.BOLD, 14));
475 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
479 // Only create the image at the beginning -
480 // this saves much memory usage
481 if ((img == null) || (prefsize.width != getSize().width)
482 || (prefsize.height != getSize().height))
487 prefsize.width = getSize().width;
488 prefsize.height = getSize().height;
491 img = createImage(prefsize.width, prefsize.height);
492 ig = img.getGraphics();
495 } catch (Exception ex)
497 ex.printStackTrace();
503 drawAll(ig, prefsize.width, prefsize.height);
504 redrawneeded = false;
506 if (appletToolTip != null)
508 ig.setColor(Color.red);
509 ig.drawString(appletToolTip, toolx, tooly);
512 g.drawImage(img, 0, 0, this);
517 public void drawAll(Graphics g, int width, int height)
519 ig.setColor(Color.black);
520 ig.fillRect(0, 0, width, height);
525 public void setColours(jalview.schemes.ColourSchemeI cs)
533 // This method has been taken out of PDBChain to allow
534 // Applet and Application specific sequence renderers to be used
535 void colourBySequence()
537 SequenceRenderer sr = new SequenceRenderer(ap.av);
539 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
541 boolean showFeatures = false;
542 if (ap.av.getShowSequenceFeatures())
546 fr = new jalview.appletgui.FeatureRenderer(ap.av);
549 fr.transferSettings(ap.getFeatureRenderer());
555 if (bysequence && pdb != null)
557 for (int ii = 0; ii < pdb.chains.size(); ii++)
559 chain = (PDBChain) pdb.chains.elementAt(ii);
561 for (int i = 0; i < chain.bonds.size(); i++)
563 Bond tmp = (Bond) chain.bonds.elementAt(i);
564 tmp.startCol = Color.lightGray;
565 tmp.endCol = Color.lightGray;
566 if (chain != mainchain)
571 for (int s = 0; s < sequence.length; s++)
573 for (int m = 0; m < mapping.length; m++)
575 if (mapping[m].getSequence() == sequence[s])
577 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
580 pos = sequence[s].findIndex(pos);
581 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
584 tmp.startCol = fr.findFeatureColour(tmp.startCol,
588 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
591 pos = sequence[s].findIndex(pos);
592 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
595 tmp.endCol = fr.findFeatureColour(tmp.endCol,
610 public void drawScene(Graphics g)
619 zsort.Zsort(visiblebonds);
623 for (int i = 0; i < visiblebonds.size(); i++)
625 tmpBond = (Bond) visiblebonds.elementAt(i);
627 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
628 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
630 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
631 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
633 xmid = (xend + xstart) / 2;
634 ymid = (yend + ystart) / 2;
636 if (depthcue && !bymolecule)
638 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
640 g.setColor(tmpBond.startCol.darker().darker());
641 drawLine(g, xstart, ystart, xmid, ymid);
643 g.setColor(tmpBond.endCol.darker().darker());
644 drawLine(g, xmid, ymid, xend, yend);
646 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
648 g.setColor(tmpBond.startCol.darker());
649 drawLine(g, xstart, ystart, xmid, ymid);
651 g.setColor(tmpBond.endCol.darker());
652 drawLine(g, xmid, ymid, xend, yend);
656 g.setColor(tmpBond.startCol);
657 drawLine(g, xstart, ystart, xmid, ymid);
659 g.setColor(tmpBond.endCol);
660 drawLine(g, xmid, ymid, xend, yend);
664 else if (depthcue && bymolecule)
666 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
668 g.setColor(Color.green.darker().darker());
669 drawLine(g, xstart, ystart, xend, yend);
671 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
673 g.setColor(Color.green.darker());
674 drawLine(g, xstart, ystart, xend, yend);
678 g.setColor(Color.green);
679 drawLine(g, xstart, ystart, xend, yend);
682 else if (!depthcue && !bymolecule)
684 g.setColor(tmpBond.startCol);
685 drawLine(g, xstart, ystart, xmid, ymid);
686 g.setColor(tmpBond.endCol);
687 drawLine(g, xmid, ymid, xend, yend);
691 drawLine(g, xstart, ystart, xend, yend);
694 if (highlightBond1 != null && highlightBond1 == tmpBond)
696 g.setColor(Color.white);
697 drawLine(g, xmid, ymid, xend, yend);
700 if (highlightBond2 != null && highlightBond2 == tmpBond)
702 g.setColor(Color.white);
703 drawLine(g, xstart, ystart, xmid, ymid);
709 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
713 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
715 g.drawLine(x1, y1, x2, y2);
716 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
717 g.drawLine(x1, y1 - 1, x2, y2 - 1);
721 g.setColor(g.getColor().brighter());
722 g.drawLine(x1, y1, x2, y2);
723 g.drawLine(x1 + 1, y1, x2 + 1, y2);
724 g.drawLine(x1 - 1, y1, x2 - 1, y2);
729 g.drawLine(x1, y1, x2, y2);
733 public Dimension minimumsize()
738 public Dimension preferredsize()
743 public void doKeyPressed(KeyEvent evt)
745 if (evt.getKeyCode() == KeyEvent.VK_UP)
747 scale = (float) (scale * 1.1);
751 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
753 scale = (float) (scale * 0.9);
759 public void mousePressed(MouseEvent e)
762 Atom fatom = findAtom(e.getX(), e.getY());
765 fatom.isSelected = !fatom.isSelected;
769 if (foundchain != -1)
771 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
772 if (chain == mainchain)
774 if (fatom.alignmentMapping != -1)
776 if (highlightRes == null)
778 highlightRes = new Vector();
781 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
783 highlightRes.removeElement(fatom.alignmentMapping + "");
787 highlightRes.addElement(fatom.alignmentMapping + "");
801 public void mouseMoved(MouseEvent e)
804 if (highlightBond1 != null)
806 highlightBond1.at2.isSelected = false;
807 highlightBond2.at1.isSelected = false;
808 highlightBond1 = null;
809 highlightBond2 = null;
812 Atom fatom = findAtom(e.getX(), e.getY());
814 PDBChain chain = null;
815 if (foundchain != -1)
817 chain = (PDBChain) pdb.chains.elementAt(foundchain);
818 if (chain == mainchain)
820 mouseOverStructure(fatom.resNumber, chain.id);
829 appletToolTip = chain.id + ":" + fatom.resNumber + " "
836 mouseOverStructure(-1, chain != null ? chain.id : null);
837 appletToolTip = null;
843 public void mouseClicked(MouseEvent e)
847 public void mouseEntered(MouseEvent e)
851 public void mouseExited(MouseEvent e)
855 public void mouseDragged(MouseEvent evt)
862 MCMatrix objmat = new MCMatrix(3, 3);
863 objmat.setIdentity();
865 if ((evt.getModifiers() & Event.META_MASK) != 0)
867 objmat.rotatez((float) ((mx - omx)));
871 objmat.rotatex((float) ((omy - my)));
872 objmat.rotatey((float) ((omx - mx)));
876 for (int ii = 0; ii < pdb.chains.size(); ii++)
878 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
880 for (int i = 0; i < bonds.size(); i++)
882 Bond tmpBond = (Bond) bonds.elementAt(i);
884 // Translate the bond so the centre is 0,0,0
885 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
887 // Now apply the rotation matrix
888 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
889 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
891 // Now translate back again
892 tmpBond.translate(centre[0], centre[1], centre[2]);
908 public void mouseReleased(MouseEvent evt)
914 void drawLabels(Graphics g)
917 for (int ii = 0; ii < pdb.chains.size(); ii++)
919 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
923 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
925 for (int i = 0; i < bonds.size(); i++)
927 Bond tmpBond = (Bond) bonds.elementAt(i);
929 if (tmpBond.at1.isSelected)
931 labelAtom(g, tmpBond, 1);
934 if (tmpBond.at2.isSelected)
937 labelAtom(g, tmpBond, 2);
944 public void labelAtom(Graphics g, Bond b, int n)
950 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
951 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
953 g.setColor(Color.red);
954 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
959 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
960 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
962 g.setColor(Color.red);
963 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
969 public Atom findAtom(int x, int y)
975 for (int ii = 0; ii < pdb.chains.size(); ii++)
977 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
983 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
985 for (int i = 0; i < bonds.size(); i++)
987 tmpBond = (Bond) bonds.elementAt(i);
989 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
991 if (Math.abs(truex - x) <= 2)
993 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
995 if (Math.abs(truey - y) <= 2)
1004 // Still here? Maybe its the last bond
1006 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1008 if (Math.abs(truex - x) <= 2)
1010 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1012 if (Math.abs(truey - y) <= 2)
1014 fatom = tmpBond.at2;
1022 if (fatom != null) // )&& chain.ds != null)
1024 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1031 public void update(Graphics g)
1036 public void highlightRes(int ii)
1038 if (!seqColoursReady)
1043 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1050 for (index = 0; index < mainchain.bonds.size(); index++)
1052 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1053 if (tmpBond.at1.alignmentMapping == ii - 1)
1055 if (highlightBond1 != null)
1057 highlightBond1.at2.isSelected = false;
1060 if (highlightBond2 != null)
1062 highlightBond2.at1.isSelected = false;
1065 highlightBond1 = null;
1066 highlightBond2 = null;
1070 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1071 highlightBond1.at2.isSelected = true;
1074 if (index != mainchain.bonds.size())
1076 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1077 highlightBond2.at1.isSelected = true;
1084 redrawneeded = true;
1088 public void setAllchainsVisible(boolean b)
1090 for (int ii = 0; ii < pdb.chains.size(); ii++)
1092 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1093 chain.isVisible = b;
1095 mainchain.isVisible = true;
1100 // ////////////////////////////////
1101 // /StructureListener
1102 public String getPdbFile()
1104 return pdbentry.getFile();
1109 public void mouseOverStructure(int pdbResNum, String chain)
1111 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1112 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1114 lastMessage = pdbResNum + chain;
1117 StringBuffer resetLastRes = new StringBuffer();
1119 StringBuffer eval = new StringBuffer();
1121 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1124 if (!seqColoursReady)
1129 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1136 for (index = 0; index < mainchain.bonds.size(); index++)
1138 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1139 if (tmpBond.at1.atomIndex == atomIndex)
1141 if (highlightBond1 != null)
1143 highlightBond1.at2.isSelected = false;
1146 if (highlightBond2 != null)
1148 highlightBond2.at1.isSelected = false;
1151 highlightBond1 = null;
1152 highlightBond2 = null;
1156 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1157 highlightBond1.at2.isSelected = true;
1160 if (index != mainchain.bonds.size())
1162 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1163 highlightBond2.at1.isSelected = true;
1170 redrawneeded = true;
1174 public Color getColour(int atomIndex, int pdbResNum, String chain,
1178 // if (!pdbfile.equals(pdbentry.getFile()))
1181 // return new Color(viewer.getAtomArgb(atomIndex));
1184 public void updateColours(Object source)
1187 redrawneeded = true;