2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.*;
32 import jalview.util.MessageManager;
34 public class AppletPDBCanvas extends Panel implements MouseListener,
35 MouseMotionListener, StructureListener
38 MCMatrix idmat = new MCMatrix(3, 3);
40 MCMatrix objmat = new MCMatrix(3, 3);
42 boolean redrawneeded = true;
64 float[] centre = new float[3];
66 float[] width = new float[3];
76 boolean bysequence = true;
78 boolean depthcue = true;
82 boolean bymolecule = false;
84 boolean zbuffer = true;
100 Font font = new Font("Helvetica", Font.PLAIN, 10);
102 public SequenceI[] sequence;
104 final StringBuffer mappingDetails = new StringBuffer();
106 String appletToolTip = null;
114 boolean pdbAction = false;
116 Bond highlightBond1, highlightBond2;
118 boolean errorLoading = false;
120 boolean seqColoursReady = false;
126 StructureSelectionManager ssm;
128 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
129 String[] chains, AlignmentPanel ap, String protocol)
133 this.pdbentry = pdbentry;
136 ssm = StructureSelectionManager
137 .getStructureSelectionManager(ap.av.applet);
141 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
143 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
144 pdbentry.setFile("INLINE" + pdb.id);
146 } catch (Exception ex)
148 ex.printStackTrace();
152 pdbentry.setId(pdb.id);
154 ssm.addStructureViewerListener(this);
165 // JUST DEAL WITH ONE SEQUENCE FOR NOW
166 SequenceI sequence = seq[0];
168 for (int i = 0; i < pdb.chains.size(); i++)
171 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
172 + ((PDBChain) pdb.chains.elementAt(i)).sequence
173 .getSequenceAsString());
174 mappingDetails.append("\nNo of residues = "
175 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
178 // Now lets compare the sequences to get
179 // the start and end points.
180 // Align the sequence to the pdb
181 // TODO: DNa/Pep switch
182 AlignSeq as = new AlignSeq(sequence,
183 ((PDBChain) pdb.chains.elementAt(i)).sequence,
184 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
186 as.calcScoreMatrix();
188 PrintStream ps = new PrintStream(System.out)
190 public void print(String x)
192 mappingDetails.append(x);
195 public void println()
197 mappingDetails.append("\n");
201 as.printAlignment(ps);
203 if (as.maxscore > max)
208 pdbstart = as.seq2start;
210 seqstart = as.seq1start + sequence.getStart() - 1;
211 seqend = as.seq1end + sequence.getEnd() - 1;
214 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
215 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
218 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
220 mainchain.pdbstart = pdbstart;
221 mainchain.pdbend = pdbend;
222 mainchain.seqstart = seqstart;
223 mainchain.seqend = seqend;
224 mainchain.isVisible = true;
225 // mainchain.makeExactMapping(maxAlignseq, sequence);
226 // mainchain.transferRESNUMFeatures(sequence, null);
228 this.prefsize = new Dimension(getSize().width, getSize().height);
230 // Initialize the matrices to identity
231 for (int i = 0; i < 3; i++)
233 for (int j = 0; j < 3; j++)
237 idmat.addElement(i, j, 0);
238 objmat.addElement(i, j, 0);
242 idmat.addElement(i, j, 1);
243 objmat.addElement(i, j, 1);
248 addMouseMotionListener(this);
249 addMouseListener(this);
251 addKeyListener(new KeyAdapter()
253 public void keyPressed(KeyEvent evt)
271 seqColoursReady = false;
272 // Sort the bonds by z coord
273 visiblebonds = new Vector();
275 for (int ii = 0; ii < pdb.chains.size(); ii++)
277 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
279 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
281 for (int i = 0; i < tmp.size(); i++)
283 visiblebonds.addElement(tmp.elementAt(i));
287 seqColoursReady = true;
293 public void findWidth()
295 float[] max = new float[3];
296 float[] min = new float[3];
298 max[0] = (float) -1e30;
299 max[1] = (float) -1e30;
300 max[2] = (float) -1e30;
302 min[0] = (float) 1e30;
303 min[1] = (float) 1e30;
304 min[2] = (float) 1e30;
306 for (int ii = 0; ii < pdb.chains.size(); ii++)
308 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
310 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
312 for (int i = 0; i < bonds.size(); i++)
314 Bond tmp = (Bond) bonds.elementAt(i);
316 if (tmp.start[0] >= max[0])
318 max[0] = tmp.start[0];
321 if (tmp.start[1] >= max[1])
323 max[1] = tmp.start[1];
326 if (tmp.start[2] >= max[2])
328 max[2] = tmp.start[2];
331 if (tmp.start[0] <= min[0])
333 min[0] = tmp.start[0];
336 if (tmp.start[1] <= min[1])
338 min[1] = tmp.start[1];
341 if (tmp.start[2] <= min[2])
343 min[2] = tmp.start[2];
346 if (tmp.end[0] >= max[0])
351 if (tmp.end[1] >= max[1])
356 if (tmp.end[2] >= max[2])
361 if (tmp.end[0] <= min[0])
366 if (tmp.end[1] <= min[1])
371 if (tmp.end[2] <= min[2])
379 width[0] = (float) Math.abs(max[0] - min[0]);
380 width[1] = (float) Math.abs(max[1] - min[1]);
381 width[2] = (float) Math.abs(max[2] - min[2]);
385 if (width[1] > width[0])
390 if (width[2] > width[1])
395 // System.out.println("Maxwidth = " + maxwidth);
398 public float findScale()
404 if (getSize().width != 0)
406 width = getSize().width;
407 height = getSize().height;
411 width = prefsize.width;
412 height = prefsize.height;
424 return (float) (dim / (1.5d * maxwidth));
427 public void findCentre()
435 // Find centre coordinate
436 for (int ii = 0; ii < pdb.chains.size(); ii++)
438 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
440 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
442 bsize += bonds.size();
444 for (int i = 0; i < bonds.size(); i++)
446 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
447 + ((Bond) bonds.elementAt(i)).end[0];
449 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
450 + ((Bond) bonds.elementAt(i)).end[1];
452 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
453 + ((Bond) bonds.elementAt(i)).end[2];
458 centre[0] = xtot / (2 * (float) bsize);
459 centre[1] = ytot / (2 * (float) bsize);
460 centre[2] = ztot / (2 * (float) bsize);
463 public void paint(Graphics g)
468 g.setColor(Color.white);
469 g.fillRect(0, 0, getSize().width, getSize().height);
470 g.setColor(Color.black);
471 g.setFont(new Font("Verdana", Font.BOLD, 14));
472 g.drawString(MessageManager.getString("label.error_loading_pdb_data"), 50, getSize().height / 2);
476 if (!seqColoursReady)
478 g.setColor(Color.black);
479 g.setFont(new Font("Verdana", Font.BOLD, 14));
480 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50, getSize().height / 2);
484 // Only create the image at the beginning -
485 // this saves much memory usage
486 if ((img == null) || (prefsize.width != getSize().width)
487 || (prefsize.height != getSize().height))
492 prefsize.width = getSize().width;
493 prefsize.height = getSize().height;
496 img = createImage(prefsize.width, prefsize.height);
497 ig = img.getGraphics();
500 } catch (Exception ex)
502 ex.printStackTrace();
508 drawAll(ig, prefsize.width, prefsize.height);
509 redrawneeded = false;
511 if (appletToolTip != null)
513 ig.setColor(Color.red);
514 ig.drawString(appletToolTip, toolx, tooly);
517 g.drawImage(img, 0, 0, this);
522 public void drawAll(Graphics g, int width, int height)
524 ig.setColor(Color.black);
525 ig.fillRect(0, 0, width, height);
530 public void setColours(jalview.schemes.ColourSchemeI cs)
538 // This method has been taken out of PDBChain to allow
539 // Applet and Application specific sequence renderers to be used
540 void colourBySequence()
542 SequenceRenderer sr = new SequenceRenderer(ap.av);
544 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
546 boolean showFeatures = false;
547 if (ap.av.getShowSequenceFeatures())
551 fr = new jalview.appletgui.FeatureRenderer(ap.av);
554 fr.transferSettings(ap.getFeatureRenderer());
560 if (bysequence && pdb != null)
562 for (int ii = 0; ii < pdb.chains.size(); ii++)
564 chain = (PDBChain) pdb.chains.elementAt(ii);
566 for (int i = 0; i < chain.bonds.size(); i++)
568 Bond tmp = (Bond) chain.bonds.elementAt(i);
569 tmp.startCol = Color.lightGray;
570 tmp.endCol = Color.lightGray;
571 if (chain != mainchain)
576 for (int s = 0; s < sequence.length; s++)
578 for (int m = 0; m < mapping.length; m++)
580 if (mapping[m].getSequence() == sequence[s])
582 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
585 pos = sequence[s].findIndex(pos);
586 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
589 tmp.startCol = fr.findFeatureColour(tmp.startCol,
593 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
596 pos = sequence[s].findIndex(pos);
597 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
600 tmp.endCol = fr.findFeatureColour(tmp.endCol,
615 public void drawScene(Graphics g)
624 zsort.Zsort(visiblebonds);
628 for (int i = 0; i < visiblebonds.size(); i++)
630 tmpBond = (Bond) visiblebonds.elementAt(i);
632 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
633 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
635 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
636 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
638 xmid = (xend + xstart) / 2;
639 ymid = (yend + ystart) / 2;
641 if (depthcue && !bymolecule)
643 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
645 g.setColor(tmpBond.startCol.darker().darker());
646 drawLine(g, xstart, ystart, xmid, ymid);
648 g.setColor(tmpBond.endCol.darker().darker());
649 drawLine(g, xmid, ymid, xend, yend);
651 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
653 g.setColor(tmpBond.startCol.darker());
654 drawLine(g, xstart, ystart, xmid, ymid);
656 g.setColor(tmpBond.endCol.darker());
657 drawLine(g, xmid, ymid, xend, yend);
661 g.setColor(tmpBond.startCol);
662 drawLine(g, xstart, ystart, xmid, ymid);
664 g.setColor(tmpBond.endCol);
665 drawLine(g, xmid, ymid, xend, yend);
669 else if (depthcue && bymolecule)
671 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
673 g.setColor(Color.green.darker().darker());
674 drawLine(g, xstart, ystart, xend, yend);
676 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
678 g.setColor(Color.green.darker());
679 drawLine(g, xstart, ystart, xend, yend);
683 g.setColor(Color.green);
684 drawLine(g, xstart, ystart, xend, yend);
687 else if (!depthcue && !bymolecule)
689 g.setColor(tmpBond.startCol);
690 drawLine(g, xstart, ystart, xmid, ymid);
691 g.setColor(tmpBond.endCol);
692 drawLine(g, xmid, ymid, xend, yend);
696 drawLine(g, xstart, ystart, xend, yend);
699 if (highlightBond1 != null && highlightBond1 == tmpBond)
701 g.setColor(Color.white);
702 drawLine(g, xmid, ymid, xend, yend);
705 if (highlightBond2 != null && highlightBond2 == tmpBond)
707 g.setColor(Color.white);
708 drawLine(g, xstart, ystart, xmid, ymid);
714 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
718 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
720 g.drawLine(x1, y1, x2, y2);
721 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
722 g.drawLine(x1, y1 - 1, x2, y2 - 1);
726 g.setColor(g.getColor().brighter());
727 g.drawLine(x1, y1, x2, y2);
728 g.drawLine(x1 + 1, y1, x2 + 1, y2);
729 g.drawLine(x1 - 1, y1, x2 - 1, y2);
734 g.drawLine(x1, y1, x2, y2);
738 public Dimension minimumsize()
743 public Dimension preferredsize()
748 public void doKeyPressed(KeyEvent evt)
750 if (evt.getKeyCode() == KeyEvent.VK_UP)
752 scale = (float) (scale * 1.1);
756 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
758 scale = (float) (scale * 0.9);
764 public void mousePressed(MouseEvent e)
767 Atom fatom = findAtom(e.getX(), e.getY());
770 fatom.isSelected = !fatom.isSelected;
774 if (foundchain != -1)
776 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
777 if (chain == mainchain)
779 if (fatom.alignmentMapping != -1)
781 if (highlightRes == null)
783 highlightRes = new Vector();
786 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
788 highlightRes.removeElement(fatom.alignmentMapping + "");
792 highlightRes.addElement(fatom.alignmentMapping + "");
806 public void mouseMoved(MouseEvent e)
809 if (highlightBond1 != null)
811 highlightBond1.at2.isSelected = false;
812 highlightBond2.at1.isSelected = false;
813 highlightBond1 = null;
814 highlightBond2 = null;
817 Atom fatom = findAtom(e.getX(), e.getY());
819 PDBChain chain = null;
820 if (foundchain != -1)
822 chain = (PDBChain) pdb.chains.elementAt(foundchain);
823 if (chain == mainchain)
825 mouseOverStructure(fatom.resNumber, chain.id);
834 appletToolTip = chain.id + ":" + fatom.resNumber + " "
841 mouseOverStructure(-1, chain != null ? chain.id : null);
842 appletToolTip = null;
848 public void mouseClicked(MouseEvent e)
852 public void mouseEntered(MouseEvent e)
856 public void mouseExited(MouseEvent e)
860 public void mouseDragged(MouseEvent evt)
867 MCMatrix objmat = new MCMatrix(3, 3);
868 objmat.setIdentity();
870 if ((evt.getModifiers() & Event.META_MASK) != 0)
872 objmat.rotatez((float) ((mx - omx)));
876 objmat.rotatex((float) ((omy - my)));
877 objmat.rotatey((float) ((omx - mx)));
881 for (int ii = 0; ii < pdb.chains.size(); ii++)
883 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
885 for (int i = 0; i < bonds.size(); i++)
887 Bond tmpBond = (Bond) bonds.elementAt(i);
889 // Translate the bond so the centre is 0,0,0
890 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
892 // Now apply the rotation matrix
893 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
894 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
896 // Now translate back again
897 tmpBond.translate(centre[0], centre[1], centre[2]);
913 public void mouseReleased(MouseEvent evt)
919 void drawLabels(Graphics g)
922 for (int ii = 0; ii < pdb.chains.size(); ii++)
924 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
928 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
930 for (int i = 0; i < bonds.size(); i++)
932 Bond tmpBond = (Bond) bonds.elementAt(i);
934 if (tmpBond.at1.isSelected)
936 labelAtom(g, tmpBond, 1);
939 if (tmpBond.at2.isSelected)
942 labelAtom(g, tmpBond, 2);
949 public void labelAtom(Graphics g, Bond b, int n)
955 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
956 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
958 g.setColor(Color.red);
959 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
964 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
965 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
967 g.setColor(Color.red);
968 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
974 public Atom findAtom(int x, int y)
980 for (int ii = 0; ii < pdb.chains.size(); ii++)
982 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
988 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
990 for (int i = 0; i < bonds.size(); i++)
992 tmpBond = (Bond) bonds.elementAt(i);
994 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
996 if (Math.abs(truex - x) <= 2)
998 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1000 if (Math.abs(truey - y) <= 2)
1002 fatom = tmpBond.at1;
1009 // Still here? Maybe its the last bond
1011 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1013 if (Math.abs(truex - x) <= 2)
1015 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1017 if (Math.abs(truey - y) <= 2)
1019 fatom = tmpBond.at2;
1027 if (fatom != null) // )&& chain.ds != null)
1029 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1036 public void update(Graphics g)
1041 public void highlightRes(int ii)
1043 if (!seqColoursReady)
1048 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1055 for (index = 0; index < mainchain.bonds.size(); index++)
1057 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1058 if (tmpBond.at1.alignmentMapping == ii - 1)
1060 if (highlightBond1 != null)
1062 highlightBond1.at2.isSelected = false;
1065 if (highlightBond2 != null)
1067 highlightBond2.at1.isSelected = false;
1070 highlightBond1 = null;
1071 highlightBond2 = null;
1075 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1076 highlightBond1.at2.isSelected = true;
1079 if (index != mainchain.bonds.size())
1081 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1082 highlightBond2.at1.isSelected = true;
1089 redrawneeded = true;
1093 public void setAllchainsVisible(boolean b)
1095 for (int ii = 0; ii < pdb.chains.size(); ii++)
1097 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1098 chain.isVisible = b;
1100 mainchain.isVisible = true;
1105 // ////////////////////////////////
1106 // /StructureListener
1107 public String[] getPdbFile()
1110 { pdbentry.getFile() };
1115 public void mouseOverStructure(int pdbResNum, String chain)
1117 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1118 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1120 lastMessage = pdbResNum + chain;
1123 StringBuffer resetLastRes = new StringBuffer();
1125 StringBuffer eval = new StringBuffer();
1127 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1130 if (!seqColoursReady)
1135 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1142 for (index = 0; index < mainchain.bonds.size(); index++)
1144 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1145 if (tmpBond.at1.atomIndex == atomIndex)
1147 if (highlightBond1 != null)
1149 highlightBond1.at2.isSelected = false;
1152 if (highlightBond2 != null)
1154 highlightBond2.at1.isSelected = false;
1157 highlightBond1 = null;
1158 highlightBond2 = null;
1162 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1163 highlightBond1.at2.isSelected = true;
1166 if (index != mainchain.bonds.size())
1168 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1169 highlightBond2.at1.isSelected = true;
1176 redrawneeded = true;
1180 public Color getColour(int atomIndex, int pdbResNum, String chain,
1184 // if (!pdbfile.equals(pdbentry.getFile()))
1187 // return new Color(viewer.getAtomArgb(atomIndex));
1190 public void updateColours(Object source)
1193 redrawneeded = true;
1198 public void releaseReferences(Object svl)
1200 // TODO Auto-generated method stub