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));
475 g.drawString(MessageManager.getString("label.error_loading_pdb_data"), 50, getSize().height / 2);
479 if (!seqColoursReady)
481 g.setColor(Color.black);
482 g.setFont(new Font("Verdana", Font.BOLD, 14));
483 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50, getSize().height / 2);
487 // Only create the image at the beginning -
488 // this saves much memory usage
489 if ((img == null) || (prefsize.width != getSize().width)
490 || (prefsize.height != getSize().height))
495 prefsize.width = getSize().width;
496 prefsize.height = getSize().height;
499 img = createImage(prefsize.width, prefsize.height);
500 ig = img.getGraphics();
503 } catch (Exception ex)
505 ex.printStackTrace();
511 drawAll(ig, prefsize.width, prefsize.height);
512 redrawneeded = false;
514 if (appletToolTip != null)
516 ig.setColor(Color.red);
517 ig.drawString(appletToolTip, toolx, tooly);
520 g.drawImage(img, 0, 0, this);
525 public void drawAll(Graphics g, int width, int height)
527 ig.setColor(Color.black);
528 ig.fillRect(0, 0, width, height);
533 public void setColours(jalview.schemes.ColourSchemeI cs)
541 // This method has been taken out of PDBChain to allow
542 // Applet and Application specific sequence renderers to be used
543 void colourBySequence()
545 SequenceRenderer sr = new SequenceRenderer(ap.av);
547 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
549 boolean showFeatures = false;
550 if (ap.av.getShowSequenceFeatures())
554 fr = new jalview.appletgui.FeatureRenderer(ap.av);
557 fr.transferSettings(ap.getFeatureRenderer());
563 if (bysequence && pdb != null)
565 for (int ii = 0; ii < pdb.chains.size(); ii++)
567 chain = (PDBChain) pdb.chains.elementAt(ii);
569 for (int i = 0; i < chain.bonds.size(); i++)
571 Bond tmp = (Bond) chain.bonds.elementAt(i);
572 tmp.startCol = Color.lightGray;
573 tmp.endCol = Color.lightGray;
574 if (chain != mainchain)
579 for (int s = 0; s < sequence.length; s++)
581 for (int m = 0; m < mapping.length; m++)
583 if (mapping[m].getSequence() == sequence[s])
585 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
588 pos = sequence[s].findIndex(pos);
589 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
592 tmp.startCol = fr.findFeatureColour(tmp.startCol,
596 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
599 pos = sequence[s].findIndex(pos);
600 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
603 tmp.endCol = fr.findFeatureColour(tmp.endCol,
618 public void drawScene(Graphics g)
627 zsort.Zsort(visiblebonds);
631 for (int i = 0; i < visiblebonds.size(); i++)
633 tmpBond = (Bond) visiblebonds.elementAt(i);
635 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
636 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
638 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
639 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
641 xmid = (xend + xstart) / 2;
642 ymid = (yend + ystart) / 2;
644 if (depthcue && !bymolecule)
646 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
648 g.setColor(tmpBond.startCol.darker().darker());
649 drawLine(g, xstart, ystart, xmid, ymid);
651 g.setColor(tmpBond.endCol.darker().darker());
652 drawLine(g, xmid, ymid, xend, yend);
654 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
656 g.setColor(tmpBond.startCol.darker());
657 drawLine(g, xstart, ystart, xmid, ymid);
659 g.setColor(tmpBond.endCol.darker());
660 drawLine(g, xmid, ymid, xend, yend);
664 g.setColor(tmpBond.startCol);
665 drawLine(g, xstart, ystart, xmid, ymid);
667 g.setColor(tmpBond.endCol);
668 drawLine(g, xmid, ymid, xend, yend);
672 else if (depthcue && bymolecule)
674 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
676 g.setColor(Color.green.darker().darker());
677 drawLine(g, xstart, ystart, xend, yend);
679 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
681 g.setColor(Color.green.darker());
682 drawLine(g, xstart, ystart, xend, yend);
686 g.setColor(Color.green);
687 drawLine(g, xstart, ystart, xend, yend);
690 else if (!depthcue && !bymolecule)
692 g.setColor(tmpBond.startCol);
693 drawLine(g, xstart, ystart, xmid, ymid);
694 g.setColor(tmpBond.endCol);
695 drawLine(g, xmid, ymid, xend, yend);
699 drawLine(g, xstart, ystart, xend, yend);
702 if (highlightBond1 != null && highlightBond1 == tmpBond)
704 g.setColor(Color.white);
705 drawLine(g, xmid, ymid, xend, yend);
708 if (highlightBond2 != null && highlightBond2 == tmpBond)
710 g.setColor(Color.white);
711 drawLine(g, xstart, ystart, xmid, ymid);
717 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
721 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
723 g.drawLine(x1, y1, x2, y2);
724 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
725 g.drawLine(x1, y1 - 1, x2, y2 - 1);
729 g.setColor(g.getColor().brighter());
730 g.drawLine(x1, y1, x2, y2);
731 g.drawLine(x1 + 1, y1, x2 + 1, y2);
732 g.drawLine(x1 - 1, y1, x2 - 1, y2);
737 g.drawLine(x1, y1, x2, y2);
741 public Dimension minimumsize()
746 public Dimension preferredsize()
751 public void doKeyPressed(KeyEvent evt)
753 if (evt.getKeyCode() == KeyEvent.VK_UP)
755 scale = (float) (scale * 1.1);
759 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
761 scale = (float) (scale * 0.9);
767 public void mousePressed(MouseEvent e)
770 Atom fatom = findAtom(e.getX(), e.getY());
773 fatom.isSelected = !fatom.isSelected;
777 if (foundchain != -1)
779 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
780 if (chain == mainchain)
782 if (fatom.alignmentMapping != -1)
784 if (highlightRes == null)
786 highlightRes = new Vector();
789 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
791 highlightRes.removeElement(fatom.alignmentMapping + "");
795 highlightRes.addElement(fatom.alignmentMapping + "");
809 public void mouseMoved(MouseEvent e)
812 if (highlightBond1 != null)
814 highlightBond1.at2.isSelected = false;
815 highlightBond2.at1.isSelected = false;
816 highlightBond1 = null;
817 highlightBond2 = null;
820 Atom fatom = findAtom(e.getX(), e.getY());
822 PDBChain chain = null;
823 if (foundchain != -1)
825 chain = (PDBChain) pdb.chains.elementAt(foundchain);
826 if (chain == mainchain)
828 mouseOverStructure(fatom.resNumber, chain.id);
837 appletToolTip = chain.id + ":" + fatom.resNumber + " "
844 mouseOverStructure(-1, chain != null ? chain.id : null);
845 appletToolTip = null;
851 public void mouseClicked(MouseEvent e)
855 public void mouseEntered(MouseEvent e)
859 public void mouseExited(MouseEvent e)
863 public void mouseDragged(MouseEvent evt)
870 MCMatrix objmat = new MCMatrix(3, 3);
871 objmat.setIdentity();
873 if ((evt.getModifiers() & Event.META_MASK) != 0)
875 objmat.rotatez((float) ((mx - omx)));
879 objmat.rotatex((float) ((omy - my)));
880 objmat.rotatey((float) ((omx - mx)));
884 for (int ii = 0; ii < pdb.chains.size(); ii++)
886 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
888 for (int i = 0; i < bonds.size(); i++)
890 Bond tmpBond = (Bond) bonds.elementAt(i);
892 // Translate the bond so the centre is 0,0,0
893 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
895 // Now apply the rotation matrix
896 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
897 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
899 // Now translate back again
900 tmpBond.translate(centre[0], centre[1], centre[2]);
916 public void mouseReleased(MouseEvent evt)
922 void drawLabels(Graphics g)
925 for (int ii = 0; ii < pdb.chains.size(); ii++)
927 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
931 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
933 for (int i = 0; i < bonds.size(); i++)
935 Bond tmpBond = (Bond) bonds.elementAt(i);
937 if (tmpBond.at1.isSelected)
939 labelAtom(g, tmpBond, 1);
942 if (tmpBond.at2.isSelected)
945 labelAtom(g, tmpBond, 2);
952 public void labelAtom(Graphics g, Bond b, int n)
958 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
959 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
961 g.setColor(Color.red);
962 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
967 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
968 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
970 g.setColor(Color.red);
971 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
977 public Atom findAtom(int x, int y)
983 for (int ii = 0; ii < pdb.chains.size(); ii++)
985 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
991 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
993 for (int i = 0; i < bonds.size(); i++)
995 tmpBond = (Bond) bonds.elementAt(i);
997 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
999 if (Math.abs(truex - x) <= 2)
1001 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1003 if (Math.abs(truey - y) <= 2)
1005 fatom = tmpBond.at1;
1012 // Still here? Maybe its the last bond
1014 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1016 if (Math.abs(truex - x) <= 2)
1018 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1020 if (Math.abs(truey - y) <= 2)
1022 fatom = tmpBond.at2;
1030 if (fatom != null) // )&& chain.ds != null)
1032 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1039 public void update(Graphics g)
1044 public void highlightRes(int ii)
1046 if (!seqColoursReady)
1051 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1058 for (index = 0; index < mainchain.bonds.size(); index++)
1060 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1061 if (tmpBond.at1.alignmentMapping == ii - 1)
1063 if (highlightBond1 != null)
1065 highlightBond1.at2.isSelected = false;
1068 if (highlightBond2 != null)
1070 highlightBond2.at1.isSelected = false;
1073 highlightBond1 = null;
1074 highlightBond2 = null;
1078 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1079 highlightBond1.at2.isSelected = true;
1082 if (index != mainchain.bonds.size())
1084 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1085 highlightBond2.at1.isSelected = true;
1092 redrawneeded = true;
1096 public void setAllchainsVisible(boolean b)
1098 for (int ii = 0; ii < pdb.chains.size(); ii++)
1100 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1101 chain.isVisible = b;
1103 mainchain.isVisible = true;
1108 // ////////////////////////////////
1109 // /StructureListener
1110 public String[] getPdbFile()
1113 { pdbentry.getFile() };
1118 public void mouseOverStructure(int pdbResNum, String chain)
1120 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1121 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1123 lastMessage = pdbResNum + chain;
1126 StringBuffer resetLastRes = new StringBuffer();
1128 StringBuffer eval = new StringBuffer();
1130 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
1133 if (!seqColoursReady)
1138 if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
1145 for (index = 0; index < mainchain.bonds.size(); index++)
1147 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1148 if (tmpBond.at1.atomIndex == atomIndex)
1150 if (highlightBond1 != null)
1152 highlightBond1.at2.isSelected = false;
1155 if (highlightBond2 != null)
1157 highlightBond2.at1.isSelected = false;
1160 highlightBond1 = null;
1161 highlightBond2 = null;
1165 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1166 highlightBond1.at2.isSelected = true;
1169 if (index != mainchain.bonds.size())
1171 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1172 highlightBond2.at1.isSelected = true;
1179 redrawneeded = true;
1183 public Color getColour(int atomIndex, int pdbResNum, String chain,
1187 // if (!pdbfile.equals(pdbentry.getFile()))
1190 // return new Color(viewer.getAtomArgb(atomIndex));
1193 public void updateColours(Object source)
1196 redrawneeded = true;
1201 public void releaseReferences(Object svl)
1203 // TODO Auto-generated method stub