2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
23 import jalview.analysis.AlignSeq;
24 import jalview.appletgui.AlignmentPanel;
25 import jalview.appletgui.FeatureRenderer;
26 import jalview.appletgui.SequenceRenderer;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.AppletFormatAdapter;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.MessageManager;
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Event;
41 import java.awt.Graphics;
42 import java.awt.Image;
43 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
44 import java.awt.Panel;
45 import java.awt.event.KeyAdapter;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.io.PrintStream;
51 import java.util.List;
52 import java.util.Vector;
54 public class AppletPDBCanvas extends Panel implements MouseListener,
55 MouseMotionListener, StructureListener
58 MCMatrix idmat = new MCMatrix(3, 3);
60 MCMatrix objmat = new MCMatrix(3, 3);
62 boolean redrawneeded = true;
84 float[] centre = new float[3];
86 float[] width = new float[3];
96 boolean bysequence = true;
98 boolean depthcue = true;
100 boolean wire = false;
102 boolean bymolecule = false;
104 boolean zbuffer = true;
120 Font font = new Font("Helvetica", Font.PLAIN, 10);
122 public SequenceI[] sequence;
124 final StringBuffer mappingDetails = new StringBuffer();
126 String appletToolTip = null;
132 Vector<String> highlightRes;
134 boolean pdbAction = false;
136 Bond highlightBond1, highlightBond2;
138 boolean errorLoading = false;
140 boolean seqColoursReady = false;
146 StructureSelectionManager ssm;
148 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
149 String[] chains, AlignmentPanel ap, String protocol)
153 this.pdbentry = pdbentry;
156 ssm = StructureSelectionManager
157 .getStructureSelectionManager(ap.av.applet);
161 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
163 if (protocol.equals(AppletFormatAdapter.PASTE))
165 pdbentry.setFile("INLINE" + pdb.id);
168 } catch (Exception ex)
170 ex.printStackTrace();
174 pdbentry.setId(pdb.id);
176 ssm.addStructureViewerListener(this);
187 // JUST DEAL WITH ONE SEQUENCE FOR NOW
188 SequenceI sequence = seq[0];
190 for (int i = 0; i < pdb.chains.size(); i++)
193 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
194 + pdb.chains.elementAt(i).sequence
195 .getSequenceAsString());
196 mappingDetails.append("\nNo of residues = "
197 + pdb.chains.elementAt(i).residues.size()
200 // Now lets compare the sequences to get
201 // the start and end points.
202 // Align the sequence to the pdb
203 // TODO: DNa/Pep switch
204 AlignSeq as = new AlignSeq(sequence,
205 pdb.chains.elementAt(i).sequence,
206 pdb.chains.elementAt(i).isNa ? AlignSeq.DNA
208 as.calcScoreMatrix();
210 PrintStream ps = new PrintStream(System.out)
212 public void print(String x)
214 mappingDetails.append(x);
217 public void println()
219 mappingDetails.append("\n");
223 as.printAlignment(ps);
225 if (as.maxscore > max)
230 pdbstart = as.seq2start;
232 seqstart = as.seq1start + sequence.getStart() - 1;
233 seqend = as.seq1end + sequence.getEnd() - 1;
236 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
237 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
240 mainchain = pdb.chains.elementAt(maxchain);
242 mainchain.pdbstart = pdbstart;
243 mainchain.pdbend = pdbend;
244 mainchain.seqstart = seqstart;
245 mainchain.seqend = seqend;
246 mainchain.isVisible = true;
247 // mainchain.makeExactMapping(maxAlignseq, sequence);
248 // mainchain.transferRESNUMFeatures(sequence, null);
250 this.prefsize = new Dimension(getSize().width, getSize().height);
252 // Initialize the matrices to identity
253 for (int i = 0; i < 3; i++)
255 for (int j = 0; j < 3; j++)
259 idmat.addElement(i, j, 0);
260 objmat.addElement(i, j, 0);
264 idmat.addElement(i, j, 1);
265 objmat.addElement(i, j, 1);
270 addMouseMotionListener(this);
271 addMouseListener(this);
273 addKeyListener(new KeyAdapter()
275 public void keyPressed(KeyEvent evt)
289 Vector<Bond> visiblebonds;
293 seqColoursReady = false;
294 // Sort the bonds by z coord
295 visiblebonds = new Vector<Bond>();
297 for (int ii = 0; ii < pdb.chains.size(); ii++)
299 if (pdb.chains.elementAt(ii).isVisible)
301 Vector<Bond> tmp = pdb.chains.elementAt(ii).bonds;
303 for (int i = 0; i < tmp.size(); i++)
305 visiblebonds.addElement(tmp.elementAt(i));
309 seqColoursReady = true;
315 public void findWidth()
317 float[] max = new float[3];
318 float[] min = new float[3];
320 max[0] = (float) -1e30;
321 max[1] = (float) -1e30;
322 max[2] = (float) -1e30;
324 min[0] = (float) 1e30;
325 min[1] = (float) 1e30;
326 min[2] = (float) 1e30;
328 for (int ii = 0; ii < pdb.chains.size(); ii++)
330 if (pdb.chains.elementAt(ii).isVisible)
332 Vector<Bond> bonds = pdb.chains.elementAt(ii).bonds;
334 for (Bond tmp : bonds)
336 if (tmp.start[0] >= max[0])
338 max[0] = tmp.start[0];
341 if (tmp.start[1] >= max[1])
343 max[1] = tmp.start[1];
346 if (tmp.start[2] >= max[2])
348 max[2] = tmp.start[2];
351 if (tmp.start[0] <= min[0])
353 min[0] = tmp.start[0];
356 if (tmp.start[1] <= min[1])
358 min[1] = tmp.start[1];
361 if (tmp.start[2] <= min[2])
363 min[2] = tmp.start[2];
366 if (tmp.end[0] >= max[0])
371 if (tmp.end[1] >= max[1])
376 if (tmp.end[2] >= max[2])
381 if (tmp.end[0] <= min[0])
386 if (tmp.end[1] <= min[1])
391 if (tmp.end[2] <= min[2])
399 width[0] = Math.abs(max[0] - min[0]);
400 width[1] = Math.abs(max[1] - min[1]);
401 width[2] = Math.abs(max[2] - min[2]);
405 if (width[1] > width[0])
410 if (width[2] > width[1])
415 // System.out.println("Maxwidth = " + maxwidth);
418 public float findScale()
424 if (getSize().width != 0)
426 width = getSize().width;
427 height = getSize().height;
431 width = prefsize.width;
432 height = prefsize.height;
444 return (float) (dim / (1.5d * maxwidth));
447 public void findCentre()
455 // Find centre coordinate
456 for (int ii = 0; ii < pdb.chains.size(); ii++)
458 if (pdb.chains.elementAt(ii).isVisible)
460 Vector<Bond> bonds = pdb.chains.elementAt(ii).bonds;
462 bsize += bonds.size();
466 xtot = xtot + b.start[0] + b.end[0];
467 ytot = ytot + b.start[1] + b.end[1];
468 ztot = ztot + b.start[2] + b.end[2];
473 centre[0] = xtot / (2 * (float) bsize);
474 centre[1] = ytot / (2 * (float) bsize);
475 centre[2] = ztot / (2 * (float) bsize);
478 public void paint(Graphics g)
483 g.setColor(Color.white);
484 g.fillRect(0, 0, getSize().width, getSize().height);
485 g.setColor(Color.black);
486 g.setFont(new Font("Verdana", Font.BOLD, 14));
488 MessageManager.getString("label.error_loading_pdb_data"), 50,
489 getSize().height / 2);
493 if (!seqColoursReady)
495 g.setColor(Color.black);
496 g.setFont(new Font("Verdana", Font.BOLD, 14));
497 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
498 getSize().height / 2);
502 // Only create the image at the beginning -
503 // this saves much memory usage
504 if ((img == null) || (prefsize.width != getSize().width)
505 || (prefsize.height != getSize().height))
510 prefsize.width = getSize().width;
511 prefsize.height = getSize().height;
514 img = createImage(prefsize.width, prefsize.height);
515 ig = img.getGraphics();
518 } catch (Exception ex)
520 ex.printStackTrace();
526 drawAll(ig, prefsize.width, prefsize.height);
527 redrawneeded = false;
529 if (appletToolTip != null)
531 ig.setColor(Color.red);
532 ig.drawString(appletToolTip, toolx, tooly);
535 g.drawImage(img, 0, 0, this);
540 public void drawAll(Graphics g, int width, int height)
542 ig.setColor(Color.black);
543 ig.fillRect(0, 0, width, height);
548 public void setColours(ColourSchemeI cs)
556 // This method has been taken out of PDBChain to allow
557 // Applet and Application specific sequence renderers to be used
558 void colourBySequence()
560 SequenceRenderer sr = new SequenceRenderer(ap.av);
562 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
564 boolean showFeatures = false;
565 if (ap.av.isShowSequenceFeatures())
569 fr = new jalview.appletgui.FeatureRenderer(ap.av);
572 fr.transferSettings(ap.getFeatureRenderer());
578 if (bysequence && pdb != null)
580 for (int ii = 0; ii < pdb.chains.size(); ii++)
582 chain = pdb.chains.elementAt(ii);
584 for (int i = 0; i < chain.bonds.size(); i++)
586 Bond tmp = chain.bonds.elementAt(i);
587 tmp.startCol = Color.lightGray;
588 tmp.endCol = Color.lightGray;
589 if (chain != mainchain)
594 for (int s = 0; s < sequence.length; s++)
596 for (int m = 0; m < mapping.length; m++)
598 if (mapping[m].getSequence() == sequence[s])
600 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
603 pos = sequence[s].findIndex(pos);
604 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
607 tmp.startCol = fr.findFeatureColour(tmp.startCol,
611 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
614 pos = sequence[s].findIndex(pos);
615 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
618 tmp.endCol = fr.findFeatureColour(tmp.endCol,
633 public void drawScene(Graphics g)
642 zsort.sort(visiblebonds);
646 for (int i = 0; i < visiblebonds.size(); i++)
648 tmpBond = visiblebonds.elementAt(i);
650 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
651 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
653 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
654 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
656 xmid = (xend + xstart) / 2;
657 ymid = (yend + ystart) / 2;
659 if (depthcue && !bymolecule)
661 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
663 g.setColor(tmpBond.startCol.darker().darker());
664 drawLine(g, xstart, ystart, xmid, ymid);
666 g.setColor(tmpBond.endCol.darker().darker());
667 drawLine(g, xmid, ymid, xend, yend);
669 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
671 g.setColor(tmpBond.startCol.darker());
672 drawLine(g, xstart, ystart, xmid, ymid);
674 g.setColor(tmpBond.endCol.darker());
675 drawLine(g, xmid, ymid, xend, yend);
679 g.setColor(tmpBond.startCol);
680 drawLine(g, xstart, ystart, xmid, ymid);
682 g.setColor(tmpBond.endCol);
683 drawLine(g, xmid, ymid, xend, yend);
687 else if (depthcue && bymolecule)
689 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
691 g.setColor(Color.green.darker().darker());
692 drawLine(g, xstart, ystart, xend, yend);
694 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
696 g.setColor(Color.green.darker());
697 drawLine(g, xstart, ystart, xend, yend);
701 g.setColor(Color.green);
702 drawLine(g, xstart, ystart, xend, yend);
705 else if (!depthcue && !bymolecule)
707 g.setColor(tmpBond.startCol);
708 drawLine(g, xstart, ystart, xmid, ymid);
709 g.setColor(tmpBond.endCol);
710 drawLine(g, xmid, ymid, xend, yend);
714 drawLine(g, xstart, ystart, xend, yend);
717 if (highlightBond1 != null && highlightBond1 == tmpBond)
719 g.setColor(Color.white);
720 drawLine(g, xmid, ymid, xend, yend);
723 if (highlightBond2 != null && highlightBond2 == tmpBond)
725 g.setColor(Color.white);
726 drawLine(g, xstart, ystart, xmid, ymid);
732 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
736 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
738 g.drawLine(x1, y1, x2, y2);
739 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
740 g.drawLine(x1, y1 - 1, x2, y2 - 1);
744 g.setColor(g.getColor().brighter());
745 g.drawLine(x1, y1, x2, y2);
746 g.drawLine(x1 + 1, y1, x2 + 1, y2);
747 g.drawLine(x1 - 1, y1, x2 - 1, y2);
752 g.drawLine(x1, y1, x2, y2);
756 public Dimension minimumsize()
761 public Dimension preferredsize()
766 public void doKeyPressed(KeyEvent evt)
768 if (evt.getKeyCode() == KeyEvent.VK_UP)
770 scale = (float) (scale * 1.1);
774 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
776 scale = (float) (scale * 0.9);
782 public void mousePressed(MouseEvent e)
785 Atom fatom = findAtom(e.getX(), e.getY());
788 fatom.isSelected = !fatom.isSelected;
792 if (foundchain != -1)
794 PDBChain chain = pdb.chains.elementAt(foundchain);
795 if (chain == mainchain)
797 if (fatom.alignmentMapping != -1)
799 if (highlightRes == null)
801 highlightRes = new Vector<String>();
804 final String atomString = Integer
805 .toString(fatom.alignmentMapping);
806 if (highlightRes.contains(atomString))
808 highlightRes.removeElement(atomString);
812 highlightRes.addElement(atomString);
826 public void mouseMoved(MouseEvent e)
829 if (highlightBond1 != null)
831 highlightBond1.at2.isSelected = false;
832 highlightBond2.at1.isSelected = false;
833 highlightBond1 = null;
834 highlightBond2 = null;
837 Atom fatom = findAtom(e.getX(), e.getY());
839 PDBChain chain = null;
840 if (foundchain != -1)
842 chain = pdb.chains.elementAt(foundchain);
843 if (chain == mainchain)
845 mouseOverStructure(fatom.resNumber, chain.id);
854 appletToolTip = chain.id + ":" + fatom.resNumber + " "
861 mouseOverStructure(-1, chain != null ? chain.id : null);
862 appletToolTip = null;
868 public void mouseClicked(MouseEvent e)
872 public void mouseEntered(MouseEvent e)
876 public void mouseExited(MouseEvent e)
880 public void mouseDragged(MouseEvent evt)
887 MCMatrix objmat = new MCMatrix(3, 3);
888 objmat.setIdentity();
890 if ((evt.getModifiers() & Event.META_MASK) != 0)
892 objmat.rotatez(((mx - omx)));
896 objmat.rotatex(((omy - my)));
897 objmat.rotatey(((omx - mx)));
901 for (PDBChain chain : pdb.chains)
903 for (Bond tmpBond : chain.bonds)
905 // Translate the bond so the centre is 0,0,0
906 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
908 // Now apply the rotation matrix
909 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
910 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
912 // Now translate back again
913 tmpBond.translate(centre[0], centre[1], centre[2]);
929 public void mouseReleased(MouseEvent evt)
935 void drawLabels(Graphics g)
938 for (PDBChain chain : pdb.chains)
942 for (Bond tmpBond : chain.bonds)
944 if (tmpBond.at1.isSelected)
946 labelAtom(g, tmpBond, 1);
949 if (tmpBond.at2.isSelected)
951 labelAtom(g, tmpBond, 2);
958 public void labelAtom(Graphics g, Bond b, int n)
964 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
965 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
967 g.setColor(Color.red);
968 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
973 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
974 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
976 g.setColor(Color.red);
977 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
983 public Atom findAtom(int x, int y)
989 for (int ii = 0; ii < pdb.chains.size(); ii++)
991 PDBChain chain = pdb.chains.elementAt(ii);
997 Vector<Bond> bonds = pdb.chains.elementAt(ii).bonds;
999 for (int i = 0; i < bonds.size(); i++)
1001 tmpBond = bonds.elementAt(i);
1003 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
1005 if (Math.abs(truex - x) <= 2)
1007 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1009 if (Math.abs(truey - y) <= 2)
1011 fatom = tmpBond.at1;
1018 // Still here? Maybe its the last bond
1020 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1022 if (Math.abs(truex - x) <= 2)
1024 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1026 if (Math.abs(truey - y) <= 2)
1028 fatom = tmpBond.at2;
1036 if (fatom != null) // )&& chain.ds != null)
1038 chain = pdb.chains.elementAt(foundchain);
1045 public void update(Graphics g)
1050 public void highlightRes(int ii)
1052 if (!seqColoursReady)
1057 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1064 for (index = 0; index < mainchain.bonds.size(); index++)
1066 tmpBond = mainchain.bonds.elementAt(index);
1067 if (tmpBond.at1.alignmentMapping == ii - 1)
1069 if (highlightBond1 != null)
1071 highlightBond1.at2.isSelected = false;
1074 if (highlightBond2 != null)
1076 highlightBond2.at1.isSelected = false;
1079 highlightBond1 = null;
1080 highlightBond2 = null;
1084 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1085 highlightBond1.at2.isSelected = true;
1088 if (index != mainchain.bonds.size())
1090 highlightBond2 = mainchain.bonds.elementAt(index);
1091 highlightBond2.at1.isSelected = true;
1098 redrawneeded = true;
1102 public void setAllchainsVisible(boolean b)
1104 for (int ii = 0; ii < pdb.chains.size(); ii++)
1106 PDBChain chain = pdb.chains.elementAt(ii);
1107 chain.isVisible = b;
1109 mainchain.isVisible = true;
1114 // ////////////////////////////////
1115 // /StructureListener
1116 public String[] getPdbFile()
1119 { pdbentry.getFile() };
1124 public void mouseOverStructure(int pdbResNum, String chain)
1126 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1128 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1131 lastMessage = pdbResNum + chain;
1134 StringBuffer resetLastRes = new StringBuffer();
1136 StringBuffer eval = new StringBuffer();
1139 * Highlight the specified atoms in the structure.
1144 public void highlightAtoms(List<AtomSpec> atoms)
1146 if (!seqColoursReady)
1150 for (AtomSpec atom : atoms)
1152 int atomIndex = atom.getAtomIndex();
1154 if (highlightRes != null
1155 && highlightRes.contains((atomIndex - 1) + ""))
1160 highlightAtom(atomIndex);
1163 redrawneeded = true;
1170 protected void highlightAtom(int atomIndex)
1174 for (index = 0; index < mainchain.bonds.size(); index++)
1176 tmpBond = mainchain.bonds.elementAt(index);
1177 if (tmpBond.at1.atomIndex == atomIndex)
1179 if (highlightBond1 != null)
1181 highlightBond1.at2.isSelected = false;
1184 if (highlightBond2 != null)
1186 highlightBond2.at1.isSelected = false;
1189 highlightBond1 = null;
1190 highlightBond2 = null;
1194 highlightBond1 = mainchain.bonds.elementAt(index - 1);
1195 highlightBond1.at2.isSelected = true;
1198 if (index != mainchain.bonds.size())
1200 highlightBond2 = mainchain.bonds.elementAt(index);
1201 highlightBond2.at1.isSelected = true;
1209 public Color getColour(int atomIndex, int pdbResNum, String chain,
1213 // if (!pdbfile.equals(pdbentry.getFile()))
1216 // return new Color(viewer.getAtomArgb(atomIndex));
1219 public void updateColours(Object source)
1222 redrawneeded = true;
1227 public void releaseReferences(Object svl)
1229 // TODO Auto-generated method stub
1234 public boolean isListeningFor(SequenceI seq)
1236 if (sequence != null)
1238 for (SequenceI s : sequence)