2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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(ap.av.applet);
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 // TODO: DNa/Pep switch
180 AlignSeq as = new AlignSeq(sequence,
181 ((PDBChain) pdb.chains.elementAt(i)).sequence,
182 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
184 as.calcScoreMatrix();
186 PrintStream ps = new PrintStream(System.out)
188 public void print(String x)
190 mappingDetails.append(x);
193 public void println()
195 mappingDetails.append("\n");
199 as.printAlignment(ps);
201 if (as.maxscore > max)
206 pdbstart = as.seq2start;
208 seqstart = as.seq1start + sequence.getStart() - 1;
209 seqend = as.seq1end + sequence.getEnd() - 1;
212 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
213 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
216 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
218 mainchain.pdbstart = pdbstart;
219 mainchain.pdbend = pdbend;
220 mainchain.seqstart = seqstart;
221 mainchain.seqend = seqend;
222 mainchain.isVisible = true;
223 // mainchain.makeExactMapping(maxAlignseq, sequence);
224 // mainchain.transferRESNUMFeatures(sequence, null);
226 this.prefsize = new Dimension(getSize().width, getSize().height);
228 // Initialize the matrices to identity
229 for (int i = 0; i < 3; i++)
231 for (int j = 0; j < 3; j++)
235 idmat.addElement(i, j, 0);
236 objmat.addElement(i, j, 0);
240 idmat.addElement(i, j, 1);
241 objmat.addElement(i, j, 1);
246 addMouseMotionListener(this);
247 addMouseListener(this);
249 addKeyListener(new KeyAdapter()
251 public void keyPressed(KeyEvent evt)
269 seqColoursReady = false;
270 // Sort the bonds by z coord
271 visiblebonds = new Vector();
273 for (int ii = 0; ii < pdb.chains.size(); ii++)
275 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
277 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
279 for (int i = 0; i < tmp.size(); i++)
281 visiblebonds.addElement(tmp.elementAt(i));
285 seqColoursReady = true;
291 public void findWidth()
293 float[] max = new float[3];
294 float[] min = new float[3];
296 max[0] = (float) -1e30;
297 max[1] = (float) -1e30;
298 max[2] = (float) -1e30;
300 min[0] = (float) 1e30;
301 min[1] = (float) 1e30;
302 min[2] = (float) 1e30;
304 for (int ii = 0; ii < pdb.chains.size(); ii++)
306 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
308 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
310 for (int i = 0; i < bonds.size(); i++)
312 Bond tmp = (Bond) bonds.elementAt(i);
314 if (tmp.start[0] >= max[0])
316 max[0] = tmp.start[0];
319 if (tmp.start[1] >= max[1])
321 max[1] = tmp.start[1];
324 if (tmp.start[2] >= max[2])
326 max[2] = tmp.start[2];
329 if (tmp.start[0] <= min[0])
331 min[0] = tmp.start[0];
334 if (tmp.start[1] <= min[1])
336 min[1] = tmp.start[1];
339 if (tmp.start[2] <= min[2])
341 min[2] = tmp.start[2];
344 if (tmp.end[0] >= max[0])
349 if (tmp.end[1] >= max[1])
354 if (tmp.end[2] >= max[2])
359 if (tmp.end[0] <= min[0])
364 if (tmp.end[1] <= min[1])
369 if (tmp.end[2] <= min[2])
377 width[0] = (float) Math.abs(max[0] - min[0]);
378 width[1] = (float) Math.abs(max[1] - min[1]);
379 width[2] = (float) Math.abs(max[2] - min[2]);
383 if (width[1] > width[0])
388 if (width[2] > width[1])
393 // System.out.println("Maxwidth = " + maxwidth);
396 public float findScale()
402 if (getSize().width != 0)
404 width = getSize().width;
405 height = getSize().height;
409 width = prefsize.width;
410 height = prefsize.height;
422 return (float) (dim / (1.5d * maxwidth));
425 public void findCentre()
433 // Find centre coordinate
434 for (int ii = 0; ii < pdb.chains.size(); ii++)
436 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
438 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
440 bsize += bonds.size();
442 for (int i = 0; i < bonds.size(); i++)
444 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
445 + ((Bond) bonds.elementAt(i)).end[0];
447 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
448 + ((Bond) bonds.elementAt(i)).end[1];
450 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
451 + ((Bond) bonds.elementAt(i)).end[2];
456 centre[0] = xtot / (2 * (float) bsize);
457 centre[1] = ytot / (2 * (float) bsize);
458 centre[2] = ztot / (2 * (float) bsize);
461 public void paint(Graphics g)
466 g.setColor(Color.white);
467 g.fillRect(0, 0, getSize().width, getSize().height);
468 g.setColor(Color.black);
469 g.setFont(new Font("Verdana", Font.BOLD, 14));
470 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
474 if (!seqColoursReady)
476 g.setColor(Color.black);
477 g.setFont(new Font("Verdana", Font.BOLD, 14));
478 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
482 // Only create the image at the beginning -
483 // this saves much memory usage
484 if ((img == null) || (prefsize.width != getSize().width)
485 || (prefsize.height != getSize().height))
490 prefsize.width = getSize().width;
491 prefsize.height = getSize().height;
494 img = createImage(prefsize.width, prefsize.height);
495 ig = img.getGraphics();
498 } catch (Exception ex)
500 ex.printStackTrace();
506 drawAll(ig, prefsize.width, prefsize.height);
507 redrawneeded = false;
509 if (appletToolTip != null)
511 ig.setColor(Color.red);
512 ig.drawString(appletToolTip, toolx, tooly);
515 g.drawImage(img, 0, 0, this);
520 public void drawAll(Graphics g, int width, int height)
522 ig.setColor(Color.black);
523 ig.fillRect(0, 0, width, height);
528 public void setColours(jalview.schemes.ColourSchemeI cs)
536 // This method has been taken out of PDBChain to allow
537 // Applet and Application specific sequence renderers to be used
538 void colourBySequence()
540 SequenceRenderer sr = new SequenceRenderer(ap.av);
542 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
544 boolean showFeatures = false;
545 if (ap.av.getShowSequenceFeatures())
549 fr = new jalview.appletgui.FeatureRenderer(ap.av);
552 fr.transferSettings(ap.getFeatureRenderer());
558 if (bysequence && pdb != null)
560 for (int ii = 0; ii < pdb.chains.size(); ii++)
562 chain = (PDBChain) pdb.chains.elementAt(ii);
564 for (int i = 0; i < chain.bonds.size(); i++)
566 Bond tmp = (Bond) chain.bonds.elementAt(i);
567 tmp.startCol = Color.lightGray;
568 tmp.endCol = Color.lightGray;
569 if (chain != mainchain)
574 for (int s = 0; s < sequence.length; s++)
576 for (int m = 0; m < mapping.length; m++)
578 if (mapping[m].getSequence() == sequence[s])
580 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
583 pos = sequence[s].findIndex(pos);
584 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
587 tmp.startCol = fr.findFeatureColour(tmp.startCol,
591 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
594 pos = sequence[s].findIndex(pos);
595 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
598 tmp.endCol = fr.findFeatureColour(tmp.endCol,
613 public void drawScene(Graphics g)
622 zsort.Zsort(visiblebonds);
626 for (int i = 0; i < visiblebonds.size(); i++)
628 tmpBond = (Bond) visiblebonds.elementAt(i);
630 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
631 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
633 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
634 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
636 xmid = (xend + xstart) / 2;
637 ymid = (yend + ystart) / 2;
639 if (depthcue && !bymolecule)
641 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
643 g.setColor(tmpBond.startCol.darker().darker());
644 drawLine(g, xstart, ystart, xmid, ymid);
646 g.setColor(tmpBond.endCol.darker().darker());
647 drawLine(g, xmid, ymid, xend, yend);
649 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
651 g.setColor(tmpBond.startCol.darker());
652 drawLine(g, xstart, ystart, xmid, ymid);
654 g.setColor(tmpBond.endCol.darker());
655 drawLine(g, xmid, ymid, xend, yend);
659 g.setColor(tmpBond.startCol);
660 drawLine(g, xstart, ystart, xmid, ymid);
662 g.setColor(tmpBond.endCol);
663 drawLine(g, xmid, ymid, xend, yend);
667 else if (depthcue && bymolecule)
669 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
671 g.setColor(Color.green.darker().darker());
672 drawLine(g, xstart, ystart, xend, yend);
674 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
676 g.setColor(Color.green.darker());
677 drawLine(g, xstart, ystart, xend, yend);
681 g.setColor(Color.green);
682 drawLine(g, xstart, ystart, xend, yend);
685 else if (!depthcue && !bymolecule)
687 g.setColor(tmpBond.startCol);
688 drawLine(g, xstart, ystart, xmid, ymid);
689 g.setColor(tmpBond.endCol);
690 drawLine(g, xmid, ymid, xend, yend);
694 drawLine(g, xstart, ystart, xend, yend);
697 if (highlightBond1 != null && highlightBond1 == tmpBond)
699 g.setColor(Color.white);
700 drawLine(g, xmid, ymid, xend, yend);
703 if (highlightBond2 != null && highlightBond2 == tmpBond)
705 g.setColor(Color.white);
706 drawLine(g, xstart, ystart, xmid, ymid);
712 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
716 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
718 g.drawLine(x1, y1, x2, y2);
719 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
720 g.drawLine(x1, y1 - 1, x2, y2 - 1);
724 g.setColor(g.getColor().brighter());
725 g.drawLine(x1, y1, x2, y2);
726 g.drawLine(x1 + 1, y1, x2 + 1, y2);
727 g.drawLine(x1 - 1, y1, x2 - 1, y2);
732 g.drawLine(x1, y1, x2, y2);
736 public Dimension minimumsize()
741 public Dimension preferredsize()
746 public void doKeyPressed(KeyEvent evt)
748 if (evt.getKeyCode() == KeyEvent.VK_UP)
750 scale = (float) (scale * 1.1);
754 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
756 scale = (float) (scale * 0.9);
762 public void mousePressed(MouseEvent e)
765 Atom fatom = findAtom(e.getX(), e.getY());
768 fatom.isSelected = !fatom.isSelected;
772 if (foundchain != -1)
774 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
775 if (chain == mainchain)
777 if (fatom.alignmentMapping != -1)
779 if (highlightRes == null)
781 highlightRes = new Vector();
784 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
786 highlightRes.removeElement(fatom.alignmentMapping + "");
790 highlightRes.addElement(fatom.alignmentMapping + "");
804 public void mouseMoved(MouseEvent e)
807 if (highlightBond1 != null)
809 highlightBond1.at2.isSelected = false;
810 highlightBond2.at1.isSelected = false;
811 highlightBond1 = null;
812 highlightBond2 = null;
815 Atom fatom = findAtom(e.getX(), e.getY());
817 PDBChain chain = null;
818 if (foundchain != -1)
820 chain = (PDBChain) pdb.chains.elementAt(foundchain);
821 if (chain == mainchain)
823 mouseOverStructure(fatom.resNumber, chain.id);
832 appletToolTip = chain.id + ":" + fatom.resNumber + " "
839 mouseOverStructure(-1, chain != null ? chain.id : null);
840 appletToolTip = null;
846 public void mouseClicked(MouseEvent e)
850 public void mouseEntered(MouseEvent e)
854 public void mouseExited(MouseEvent e)
858 public void mouseDragged(MouseEvent evt)
865 MCMatrix objmat = new MCMatrix(3, 3);
866 objmat.setIdentity();
868 if ((evt.getModifiers() & Event.META_MASK) != 0)
870 objmat.rotatez((float) ((mx - omx)));
874 objmat.rotatex((float) ((omy - my)));
875 objmat.rotatey((float) ((omx - mx)));
879 for (int ii = 0; ii < pdb.chains.size(); ii++)
881 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
883 for (int i = 0; i < bonds.size(); i++)
885 Bond tmpBond = (Bond) bonds.elementAt(i);
887 // Translate the bond so the centre is 0,0,0
888 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
890 // Now apply the rotation matrix
891 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
892 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
894 // Now translate back again
895 tmpBond.translate(centre[0], centre[1], centre[2]);
911 public void mouseReleased(MouseEvent evt)
917 void drawLabels(Graphics g)
920 for (int ii = 0; ii < pdb.chains.size(); ii++)
922 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
926 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
928 for (int i = 0; i < bonds.size(); i++)
930 Bond tmpBond = (Bond) bonds.elementAt(i);
932 if (tmpBond.at1.isSelected)
934 labelAtom(g, tmpBond, 1);
937 if (tmpBond.at2.isSelected)
940 labelAtom(g, tmpBond, 2);
947 public void labelAtom(Graphics g, Bond b, int n)
953 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
954 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
956 g.setColor(Color.red);
957 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
962 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
963 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
965 g.setColor(Color.red);
966 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
972 public Atom findAtom(int x, int y)
978 for (int ii = 0; ii < pdb.chains.size(); ii++)
980 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
986 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
988 for (int i = 0; i < bonds.size(); i++)
990 tmpBond = (Bond) bonds.elementAt(i);
992 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
994 if (Math.abs(truex - x) <= 2)
996 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
998 if (Math.abs(truey - y) <= 2)
1000 fatom = tmpBond.at1;
1007 // Still here? Maybe its the last bond
1009 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1011 if (Math.abs(truex - x) <= 2)
1013 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1015 if (Math.abs(truey - y) <= 2)
1017 fatom = tmpBond.at2;
1025 if (fatom != null) // )&& chain.ds != null)
1027 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1034 public void update(Graphics g)
1039 public void highlightRes(int ii)
1041 if (!seqColoursReady)
1046 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1053 for (index = 0; index < mainchain.bonds.size(); index++)
1055 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1056 if (tmpBond.at1.alignmentMapping == ii - 1)
1058 if (highlightBond1 != null)
1060 highlightBond1.at2.isSelected = false;
1063 if (highlightBond2 != null)
1065 highlightBond2.at1.isSelected = false;
1068 highlightBond1 = null;
1069 highlightBond2 = null;
1073 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1074 highlightBond1.at2.isSelected = true;
1077 if (index != mainchain.bonds.size())
1079 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1080 highlightBond2.at1.isSelected = true;
1087 redrawneeded = true;
1091 public void setAllchainsVisible(boolean b)
1093 for (int ii = 0; ii < pdb.chains.size(); ii++)
1095 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1096 chain.isVisible = b;
1098 mainchain.isVisible = true;
1103 // ////////////////////////////////
1104 // /StructureListener
1105 public String[] getPdbFile()
1108 { pdbentry.getFile() };
1113 public void mouseOverStructure(int pdbResNum, String chain)
1115 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1116 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1118 lastMessage = pdbResNum + chain;
1121 StringBuffer resetLastRes = new StringBuffer();
1123 StringBuffer eval = new StringBuffer();
1125 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1128 if (!seqColoursReady)
1133 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1140 for (index = 0; index < mainchain.bonds.size(); index++)
1142 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1143 if (tmpBond.at1.atomIndex == atomIndex)
1145 if (highlightBond1 != null)
1147 highlightBond1.at2.isSelected = false;
1150 if (highlightBond2 != null)
1152 highlightBond2.at1.isSelected = false;
1155 highlightBond1 = null;
1156 highlightBond2 = null;
1160 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1161 highlightBond1.at2.isSelected = true;
1164 if (index != mainchain.bonds.size())
1166 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1167 highlightBond2.at1.isSelected = true;
1174 redrawneeded = true;
1178 public Color getColour(int atomIndex, int pdbResNum, String chain,
1182 // if (!pdbfile.equals(pdbentry.getFile()))
1185 // return new Color(viewer.getAtomArgb(atomIndex));
1188 public void updateColours(Object source)
1191 redrawneeded = true;
1196 public void releaseReferences(Object svl)
1198 // TODO Auto-generated method stub