2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
28 import java.awt.event.*;
30 import jalview.analysis.*;
31 import jalview.datamodel.*;
33 import jalview.appletgui.*;
34 import jalview.structure.*;
35 import jalview.util.MessageManager;
37 public class AppletPDBCanvas extends Panel implements MouseListener,
38 MouseMotionListener, StructureListener
41 MCMatrix idmat = new MCMatrix(3, 3);
43 MCMatrix objmat = new MCMatrix(3, 3);
45 boolean redrawneeded = true;
67 float[] centre = new float[3];
69 float[] width = new float[3];
79 boolean bysequence = true;
81 boolean depthcue = true;
85 boolean bymolecule = false;
87 boolean zbuffer = true;
103 Font font = new Font("Helvetica", Font.PLAIN, 10);
105 public SequenceI[] sequence;
107 final StringBuffer mappingDetails = new StringBuffer();
109 String appletToolTip = null;
117 boolean pdbAction = false;
119 Bond highlightBond1, highlightBond2;
121 boolean errorLoading = false;
123 boolean seqColoursReady = false;
129 StructureSelectionManager ssm;
131 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
132 String[] chains, AlignmentPanel ap, String protocol)
136 this.pdbentry = pdbentry;
139 ssm = StructureSelectionManager
140 .getStructureSelectionManager(ap.av.applet);
144 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
146 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
147 pdbentry.setFile("INLINE" + pdb.id);
149 } catch (Exception ex)
151 ex.printStackTrace();
155 pdbentry.setId(pdb.id);
157 ssm.addStructureViewerListener(this);
168 // JUST DEAL WITH ONE SEQUENCE FOR NOW
169 SequenceI sequence = seq[0];
171 for (int i = 0; i < pdb.chains.size(); i++)
174 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
175 + ((PDBChain) pdb.chains.elementAt(i)).sequence
176 .getSequenceAsString());
177 mappingDetails.append("\nNo of residues = "
178 + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
181 // Now lets compare the sequences to get
182 // the start and end points.
183 // Align the sequence to the pdb
184 // TODO: DNa/Pep switch
185 AlignSeq as = new AlignSeq(sequence,
186 ((PDBChain) pdb.chains.elementAt(i)).sequence,
187 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
189 as.calcScoreMatrix();
191 PrintStream ps = new PrintStream(System.out)
193 public void print(String x)
195 mappingDetails.append(x);
198 public void println()
200 mappingDetails.append("\n");
204 as.printAlignment(ps);
206 if (as.maxscore > max)
211 pdbstart = as.seq2start;
213 seqstart = as.seq1start + sequence.getStart() - 1;
214 seqend = as.seq1end + sequence.getEnd() - 1;
217 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
218 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
221 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
223 mainchain.pdbstart = pdbstart;
224 mainchain.pdbend = pdbend;
225 mainchain.seqstart = seqstart;
226 mainchain.seqend = seqend;
227 mainchain.isVisible = true;
228 // mainchain.makeExactMapping(maxAlignseq, sequence);
229 // mainchain.transferRESNUMFeatures(sequence, null);
231 this.prefsize = new Dimension(getSize().width, getSize().height);
233 // Initialize the matrices to identity
234 for (int i = 0; i < 3; i++)
236 for (int j = 0; j < 3; j++)
240 idmat.addElement(i, j, 0);
241 objmat.addElement(i, j, 0);
245 idmat.addElement(i, j, 1);
246 objmat.addElement(i, j, 1);
251 addMouseMotionListener(this);
252 addMouseListener(this);
254 addKeyListener(new KeyAdapter()
256 public void keyPressed(KeyEvent evt)
274 seqColoursReady = false;
275 // Sort the bonds by z coord
276 visiblebonds = new Vector();
278 for (int ii = 0; ii < pdb.chains.size(); ii++)
280 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
282 Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
284 for (int i = 0; i < tmp.size(); i++)
286 visiblebonds.addElement(tmp.elementAt(i));
290 seqColoursReady = true;
296 public void findWidth()
298 float[] max = new float[3];
299 float[] min = new float[3];
301 max[0] = (float) -1e30;
302 max[1] = (float) -1e30;
303 max[2] = (float) -1e30;
305 min[0] = (float) 1e30;
306 min[1] = (float) 1e30;
307 min[2] = (float) 1e30;
309 for (int ii = 0; ii < pdb.chains.size(); ii++)
311 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
313 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
315 for (int i = 0; i < bonds.size(); i++)
317 Bond tmp = (Bond) bonds.elementAt(i);
319 if (tmp.start[0] >= max[0])
321 max[0] = tmp.start[0];
324 if (tmp.start[1] >= max[1])
326 max[1] = tmp.start[1];
329 if (tmp.start[2] >= max[2])
331 max[2] = tmp.start[2];
334 if (tmp.start[0] <= min[0])
336 min[0] = tmp.start[0];
339 if (tmp.start[1] <= min[1])
341 min[1] = tmp.start[1];
344 if (tmp.start[2] <= min[2])
346 min[2] = tmp.start[2];
349 if (tmp.end[0] >= max[0])
354 if (tmp.end[1] >= max[1])
359 if (tmp.end[2] >= max[2])
364 if (tmp.end[0] <= min[0])
369 if (tmp.end[1] <= min[1])
374 if (tmp.end[2] <= min[2])
382 width[0] = (float) Math.abs(max[0] - min[0]);
383 width[1] = (float) Math.abs(max[1] - min[1]);
384 width[2] = (float) Math.abs(max[2] - min[2]);
388 if (width[1] > width[0])
393 if (width[2] > width[1])
398 // System.out.println("Maxwidth = " + maxwidth);
401 public float findScale()
407 if (getSize().width != 0)
409 width = getSize().width;
410 height = getSize().height;
414 width = prefsize.width;
415 height = prefsize.height;
427 return (float) (dim / (1.5d * maxwidth));
430 public void findCentre()
438 // Find centre coordinate
439 for (int ii = 0; ii < pdb.chains.size(); ii++)
441 if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
443 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
445 bsize += bonds.size();
447 for (int i = 0; i < bonds.size(); i++)
449 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
450 + ((Bond) bonds.elementAt(i)).end[0];
452 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
453 + ((Bond) bonds.elementAt(i)).end[1];
455 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
456 + ((Bond) bonds.elementAt(i)).end[2];
461 centre[0] = xtot / (2 * (float) bsize);
462 centre[1] = ytot / (2 * (float) bsize);
463 centre[2] = ztot / (2 * (float) bsize);
466 public void paint(Graphics g)
471 g.setColor(Color.white);
472 g.fillRect(0, 0, getSize().width, getSize().height);
473 g.setColor(Color.black);
474 g.setFont(new Font("Verdana", Font.BOLD, 14));
476 MessageManager.getString("label.error_loading_pdb_data"), 50,
477 getSize().height / 2);
481 if (!seqColoursReady)
483 g.setColor(Color.black);
484 g.setFont(new Font("Verdana", Font.BOLD, 14));
485 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
486 getSize().height / 2);
490 // Only create the image at the beginning -
491 // this saves much memory usage
492 if ((img == null) || (prefsize.width != getSize().width)
493 || (prefsize.height != getSize().height))
498 prefsize.width = getSize().width;
499 prefsize.height = getSize().height;
502 img = createImage(prefsize.width, prefsize.height);
503 ig = img.getGraphics();
506 } catch (Exception ex)
508 ex.printStackTrace();
514 drawAll(ig, prefsize.width, prefsize.height);
515 redrawneeded = false;
517 if (appletToolTip != null)
519 ig.setColor(Color.red);
520 ig.drawString(appletToolTip, toolx, tooly);
523 g.drawImage(img, 0, 0, this);
528 public void drawAll(Graphics g, int width, int height)
530 ig.setColor(Color.black);
531 ig.fillRect(0, 0, width, height);
536 public void setColours(jalview.schemes.ColourSchemeI cs)
544 // This method has been taken out of PDBChain to allow
545 // Applet and Application specific sequence renderers to be used
546 void colourBySequence()
548 SequenceRenderer sr = new SequenceRenderer(ap.av);
550 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
552 boolean showFeatures = false;
553 if (ap.av.getShowSequenceFeatures())
557 fr = new jalview.appletgui.FeatureRenderer(ap.av);
560 fr.transferSettings(ap.getFeatureRenderer());
566 if (bysequence && pdb != null)
568 for (int ii = 0; ii < pdb.chains.size(); ii++)
570 chain = (PDBChain) pdb.chains.elementAt(ii);
572 for (int i = 0; i < chain.bonds.size(); i++)
574 Bond tmp = (Bond) chain.bonds.elementAt(i);
575 tmp.startCol = Color.lightGray;
576 tmp.endCol = Color.lightGray;
577 if (chain != mainchain)
582 for (int s = 0; s < sequence.length; s++)
584 for (int m = 0; m < mapping.length; m++)
586 if (mapping[m].getSequence() == sequence[s])
588 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
591 pos = sequence[s].findIndex(pos);
592 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
595 tmp.startCol = fr.findFeatureColour(tmp.startCol,
599 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
602 pos = sequence[s].findIndex(pos);
603 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
606 tmp.endCol = fr.findFeatureColour(tmp.endCol,
621 public void drawScene(Graphics g)
630 zsort.Zsort(visiblebonds);
634 for (int i = 0; i < visiblebonds.size(); i++)
636 tmpBond = (Bond) visiblebonds.elementAt(i);
638 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
639 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
641 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
642 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
644 xmid = (xend + xstart) / 2;
645 ymid = (yend + ystart) / 2;
647 if (depthcue && !bymolecule)
649 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
651 g.setColor(tmpBond.startCol.darker().darker());
652 drawLine(g, xstart, ystart, xmid, ymid);
654 g.setColor(tmpBond.endCol.darker().darker());
655 drawLine(g, xmid, ymid, xend, yend);
657 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
659 g.setColor(tmpBond.startCol.darker());
660 drawLine(g, xstart, ystart, xmid, ymid);
662 g.setColor(tmpBond.endCol.darker());
663 drawLine(g, xmid, ymid, xend, yend);
667 g.setColor(tmpBond.startCol);
668 drawLine(g, xstart, ystart, xmid, ymid);
670 g.setColor(tmpBond.endCol);
671 drawLine(g, xmid, ymid, xend, yend);
675 else if (depthcue && bymolecule)
677 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
679 g.setColor(Color.green.darker().darker());
680 drawLine(g, xstart, ystart, xend, yend);
682 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
684 g.setColor(Color.green.darker());
685 drawLine(g, xstart, ystart, xend, yend);
689 g.setColor(Color.green);
690 drawLine(g, xstart, ystart, xend, yend);
693 else if (!depthcue && !bymolecule)
695 g.setColor(tmpBond.startCol);
696 drawLine(g, xstart, ystart, xmid, ymid);
697 g.setColor(tmpBond.endCol);
698 drawLine(g, xmid, ymid, xend, yend);
702 drawLine(g, xstart, ystart, xend, yend);
705 if (highlightBond1 != null && highlightBond1 == tmpBond)
707 g.setColor(Color.white);
708 drawLine(g, xmid, ymid, xend, yend);
711 if (highlightBond2 != null && highlightBond2 == tmpBond)
713 g.setColor(Color.white);
714 drawLine(g, xstart, ystart, xmid, ymid);
720 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
724 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
726 g.drawLine(x1, y1, x2, y2);
727 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
728 g.drawLine(x1, y1 - 1, x2, y2 - 1);
732 g.setColor(g.getColor().brighter());
733 g.drawLine(x1, y1, x2, y2);
734 g.drawLine(x1 + 1, y1, x2 + 1, y2);
735 g.drawLine(x1 - 1, y1, x2 - 1, y2);
740 g.drawLine(x1, y1, x2, y2);
744 public Dimension minimumsize()
749 public Dimension preferredsize()
754 public void doKeyPressed(KeyEvent evt)
756 if (evt.getKeyCode() == KeyEvent.VK_UP)
758 scale = (float) (scale * 1.1);
762 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
764 scale = (float) (scale * 0.9);
770 public void mousePressed(MouseEvent e)
773 Atom fatom = findAtom(e.getX(), e.getY());
776 fatom.isSelected = !fatom.isSelected;
780 if (foundchain != -1)
782 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
783 if (chain == mainchain)
785 if (fatom.alignmentMapping != -1)
787 if (highlightRes == null)
789 highlightRes = new Vector();
792 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
794 highlightRes.removeElement(fatom.alignmentMapping + "");
798 highlightRes.addElement(fatom.alignmentMapping + "");
812 public void mouseMoved(MouseEvent e)
815 if (highlightBond1 != null)
817 highlightBond1.at2.isSelected = false;
818 highlightBond2.at1.isSelected = false;
819 highlightBond1 = null;
820 highlightBond2 = null;
823 Atom fatom = findAtom(e.getX(), e.getY());
825 PDBChain chain = null;
826 if (foundchain != -1)
828 chain = (PDBChain) pdb.chains.elementAt(foundchain);
829 if (chain == mainchain)
831 mouseOverStructure(fatom.resNumber, chain.id);
840 appletToolTip = chain.id + ":" + fatom.resNumber + " "
847 mouseOverStructure(-1, chain != null ? chain.id : null);
848 appletToolTip = null;
854 public void mouseClicked(MouseEvent e)
858 public void mouseEntered(MouseEvent e)
862 public void mouseExited(MouseEvent e)
866 public void mouseDragged(MouseEvent evt)
873 MCMatrix objmat = new MCMatrix(3, 3);
874 objmat.setIdentity();
876 if ((evt.getModifiers() & Event.META_MASK) != 0)
878 objmat.rotatez((float) ((mx - omx)));
882 objmat.rotatex((float) ((omy - my)));
883 objmat.rotatey((float) ((omx - mx)));
887 for (int ii = 0; ii < pdb.chains.size(); ii++)
889 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
891 for (int i = 0; i < bonds.size(); i++)
893 Bond tmpBond = (Bond) bonds.elementAt(i);
895 // Translate the bond so the centre is 0,0,0
896 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
898 // Now apply the rotation matrix
899 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
900 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
902 // Now translate back again
903 tmpBond.translate(centre[0], centre[1], centre[2]);
919 public void mouseReleased(MouseEvent evt)
925 void drawLabels(Graphics g)
928 for (int ii = 0; ii < pdb.chains.size(); ii++)
930 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
934 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
936 for (int i = 0; i < bonds.size(); i++)
938 Bond tmpBond = (Bond) bonds.elementAt(i);
940 if (tmpBond.at1.isSelected)
942 labelAtom(g, tmpBond, 1);
945 if (tmpBond.at2.isSelected)
948 labelAtom(g, tmpBond, 2);
955 public void labelAtom(Graphics g, Bond b, int n)
961 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
962 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
964 g.setColor(Color.red);
965 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
970 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
971 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
973 g.setColor(Color.red);
974 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
980 public Atom findAtom(int x, int y)
986 for (int ii = 0; ii < pdb.chains.size(); ii++)
988 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
994 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
996 for (int i = 0; i < bonds.size(); i++)
998 tmpBond = (Bond) bonds.elementAt(i);
1000 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
1002 if (Math.abs(truex - x) <= 2)
1004 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1006 if (Math.abs(truey - y) <= 2)
1008 fatom = tmpBond.at1;
1015 // Still here? Maybe its the last bond
1017 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1019 if (Math.abs(truex - x) <= 2)
1021 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1023 if (Math.abs(truey - y) <= 2)
1025 fatom = tmpBond.at2;
1033 if (fatom != null) // )&& chain.ds != null)
1035 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1042 public void update(Graphics g)
1047 public void highlightRes(int ii)
1049 if (!seqColoursReady)
1054 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1061 for (index = 0; index < mainchain.bonds.size(); index++)
1063 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1064 if (tmpBond.at1.alignmentMapping == ii - 1)
1066 if (highlightBond1 != null)
1068 highlightBond1.at2.isSelected = false;
1071 if (highlightBond2 != null)
1073 highlightBond2.at1.isSelected = false;
1076 highlightBond1 = null;
1077 highlightBond2 = null;
1081 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1082 highlightBond1.at2.isSelected = true;
1085 if (index != mainchain.bonds.size())
1087 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1088 highlightBond2.at1.isSelected = true;
1095 redrawneeded = true;
1099 public void setAllchainsVisible(boolean b)
1101 for (int ii = 0; ii < pdb.chains.size(); ii++)
1103 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1104 chain.isVisible = b;
1106 mainchain.isVisible = true;
1111 // ////////////////////////////////
1112 // /StructureListener
1113 public String[] getPdbFile()
1116 { pdbentry.getFile() };
1121 public void mouseOverStructure(int pdbResNum, String chain)
1123 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1124 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1126 lastMessage = pdbResNum + chain;
1129 StringBuffer resetLastRes = new StringBuffer();
1131 StringBuffer eval = new StringBuffer();
1133 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1136 if (!seqColoursReady)
1141 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1148 for (index = 0; index < mainchain.bonds.size(); index++)
1150 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1151 if (tmpBond.at1.atomIndex == atomIndex)
1153 if (highlightBond1 != null)
1155 highlightBond1.at2.isSelected = false;
1158 if (highlightBond2 != null)
1160 highlightBond2.at1.isSelected = false;
1163 highlightBond1 = null;
1164 highlightBond2 = null;
1168 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1169 highlightBond1.at2.isSelected = true;
1172 if (index != mainchain.bonds.size())
1174 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1175 highlightBond2.at1.isSelected = true;
1182 redrawneeded = true;
1186 public Color getColour(int atomIndex, int pdbResNum, String chain,
1190 // if (!pdbfile.equals(pdbentry.getFile()))
1193 // return new Color(viewer.getAtomArgb(atomIndex));
1196 public void updateColours(Object source)
1199 redrawneeded = true;
1204 public void releaseReferences(Object svl)
1206 // TODO Auto-generated method stub