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.structure.AtomSpec;
30 import jalview.structure.StructureListener;
31 import jalview.structure.StructureMapping;
32 import jalview.structure.StructureSelectionManager;
33 import jalview.util.MessageManager;
35 import java.awt.Color;
36 import java.awt.Dimension;
37 import java.awt.Event;
39 import java.awt.Graphics;
40 import java.awt.Image;
41 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
42 import java.awt.Panel;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.io.PrintStream;
49 import java.util.List;
50 import java.util.Vector;
52 public class AppletPDBCanvas extends Panel implements MouseListener,
53 MouseMotionListener, StructureListener
56 MCMatrix idmat = new MCMatrix(3, 3);
58 MCMatrix objmat = new MCMatrix(3, 3);
60 boolean redrawneeded = true;
82 float[] centre = new float[3];
84 float[] width = new float[3];
94 boolean bysequence = true;
96 boolean depthcue = true;
100 boolean bymolecule = false;
102 boolean zbuffer = true;
118 Font font = new Font("Helvetica", Font.PLAIN, 10);
120 public SequenceI[] sequence;
122 final StringBuffer mappingDetails = new StringBuffer();
124 String appletToolTip = null;
132 boolean pdbAction = false;
134 Bond highlightBond1, highlightBond2;
136 boolean errorLoading = false;
138 boolean seqColoursReady = false;
144 StructureSelectionManager ssm;
146 public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
147 String[] chains, AlignmentPanel ap, String protocol)
151 this.pdbentry = pdbentry;
154 ssm = StructureSelectionManager
155 .getStructureSelectionManager(ap.av.applet);
159 pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
161 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
163 pdbentry.setFile("INLINE" + pdb.id);
166 } catch (Exception ex)
168 ex.printStackTrace();
172 pdbentry.setId(pdb.id);
174 ssm.addStructureViewerListener(this);
185 // JUST DEAL WITH ONE SEQUENCE FOR NOW
186 SequenceI sequence = seq[0];
188 for (int i = 0; i < pdb.chains.size(); i++)
191 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
192 + pdb.chains.elementAt(i).sequence
193 .getSequenceAsString());
194 mappingDetails.append("\nNo of residues = "
195 + pdb.chains.elementAt(i).residues.size()
198 // Now lets compare the sequences to get
199 // the start and end points.
200 // Align the sequence to the pdb
201 // TODO: DNa/Pep switch
202 AlignSeq as = new AlignSeq(sequence,
203 pdb.chains.elementAt(i).sequence,
204 pdb.chains.elementAt(i).isNa ? AlignSeq.DNA
206 as.calcScoreMatrix();
208 PrintStream ps = new PrintStream(System.out)
210 public void print(String x)
212 mappingDetails.append(x);
215 public void println()
217 mappingDetails.append("\n");
221 as.printAlignment(ps);
223 if (as.maxscore > max)
228 pdbstart = as.seq2start;
230 seqstart = as.seq1start + sequence.getStart() - 1;
231 seqend = as.seq1end + sequence.getEnd() - 1;
234 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
235 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
238 mainchain = pdb.chains.elementAt(maxchain);
240 mainchain.pdbstart = pdbstart;
241 mainchain.pdbend = pdbend;
242 mainchain.seqstart = seqstart;
243 mainchain.seqend = seqend;
244 mainchain.isVisible = true;
245 // mainchain.makeExactMapping(maxAlignseq, sequence);
246 // mainchain.transferRESNUMFeatures(sequence, null);
248 this.prefsize = new Dimension(getSize().width, getSize().height);
250 // Initialize the matrices to identity
251 for (int i = 0; i < 3; i++)
253 for (int j = 0; j < 3; j++)
257 idmat.addElement(i, j, 0);
258 objmat.addElement(i, j, 0);
262 idmat.addElement(i, j, 1);
263 objmat.addElement(i, j, 1);
268 addMouseMotionListener(this);
269 addMouseListener(this);
271 addKeyListener(new KeyAdapter()
273 public void keyPressed(KeyEvent evt)
291 seqColoursReady = false;
292 // Sort the bonds by z coord
293 visiblebonds = new Vector();
295 for (int ii = 0; ii < pdb.chains.size(); ii++)
297 if (pdb.chains.elementAt(ii).isVisible)
299 Vector tmp = pdb.chains.elementAt(ii).bonds;
301 for (int i = 0; i < tmp.size(); i++)
303 visiblebonds.addElement(tmp.elementAt(i));
307 seqColoursReady = true;
313 public void findWidth()
315 float[] max = new float[3];
316 float[] min = new float[3];
318 max[0] = (float) -1e30;
319 max[1] = (float) -1e30;
320 max[2] = (float) -1e30;
322 min[0] = (float) 1e30;
323 min[1] = (float) 1e30;
324 min[2] = (float) 1e30;
326 for (int ii = 0; ii < pdb.chains.size(); ii++)
328 if (pdb.chains.elementAt(ii).isVisible)
330 Vector bonds = pdb.chains.elementAt(ii).bonds;
332 for (int i = 0; i < bonds.size(); i++)
334 Bond tmp = (Bond) bonds.elementAt(i);
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 bonds = pdb.chains.elementAt(ii).bonds;
462 bsize += bonds.size();
464 for (int i = 0; i < bonds.size(); i++)
466 xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
467 + ((Bond) bonds.elementAt(i)).end[0];
469 ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
470 + ((Bond) bonds.elementAt(i)).end[1];
472 ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
473 + ((Bond) bonds.elementAt(i)).end[2];
478 centre[0] = xtot / (2 * (float) bsize);
479 centre[1] = ytot / (2 * (float) bsize);
480 centre[2] = ztot / (2 * (float) bsize);
483 public void paint(Graphics g)
488 g.setColor(Color.white);
489 g.fillRect(0, 0, getSize().width, getSize().height);
490 g.setColor(Color.black);
491 g.setFont(new Font("Verdana", Font.BOLD, 14));
493 MessageManager.getString("label.error_loading_pdb_data"), 50,
494 getSize().height / 2);
498 if (!seqColoursReady)
500 g.setColor(Color.black);
501 g.setFont(new Font("Verdana", Font.BOLD, 14));
502 g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
503 getSize().height / 2);
507 // Only create the image at the beginning -
508 // this saves much memory usage
509 if ((img == null) || (prefsize.width != getSize().width)
510 || (prefsize.height != getSize().height))
515 prefsize.width = getSize().width;
516 prefsize.height = getSize().height;
519 img = createImage(prefsize.width, prefsize.height);
520 ig = img.getGraphics();
523 } catch (Exception ex)
525 ex.printStackTrace();
531 drawAll(ig, prefsize.width, prefsize.height);
532 redrawneeded = false;
534 if (appletToolTip != null)
536 ig.setColor(Color.red);
537 ig.drawString(appletToolTip, toolx, tooly);
540 g.drawImage(img, 0, 0, this);
545 public void drawAll(Graphics g, int width, int height)
547 ig.setColor(Color.black);
548 ig.fillRect(0, 0, width, height);
553 public void setColours(jalview.schemes.ColourSchemeI cs)
561 // This method has been taken out of PDBChain to allow
562 // Applet and Application specific sequence renderers to be used
563 void colourBySequence()
565 SequenceRenderer sr = new SequenceRenderer(ap.av);
567 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
569 boolean showFeatures = false;
570 if (ap.av.isShowSequenceFeatures())
574 fr = new jalview.appletgui.FeatureRenderer(ap.av);
577 fr.transferSettings(ap.getFeatureRenderer());
583 if (bysequence && pdb != null)
585 for (int ii = 0; ii < pdb.chains.size(); ii++)
587 chain = pdb.chains.elementAt(ii);
589 for (int i = 0; i < chain.bonds.size(); i++)
591 Bond tmp = (Bond) chain.bonds.elementAt(i);
592 tmp.startCol = Color.lightGray;
593 tmp.endCol = Color.lightGray;
594 if (chain != mainchain)
599 for (int s = 0; s < sequence.length; s++)
601 for (int m = 0; m < mapping.length; m++)
603 if (mapping[m].getSequence() == sequence[s])
605 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
608 pos = sequence[s].findIndex(pos);
609 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
612 tmp.startCol = fr.findFeatureColour(tmp.startCol,
616 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
619 pos = sequence[s].findIndex(pos);
620 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
623 tmp.endCol = fr.findFeatureColour(tmp.endCol,
638 public void drawScene(Graphics g)
647 zsort.Zsort(visiblebonds);
651 for (int i = 0; i < visiblebonds.size(); i++)
653 tmpBond = (Bond) visiblebonds.elementAt(i);
655 xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
656 ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
658 xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
659 yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
661 xmid = (xend + xstart) / 2;
662 ymid = (yend + ystart) / 2;
664 if (depthcue && !bymolecule)
666 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
668 g.setColor(tmpBond.startCol.darker().darker());
669 drawLine(g, xstart, ystart, xmid, ymid);
671 g.setColor(tmpBond.endCol.darker().darker());
672 drawLine(g, xmid, ymid, xend, yend);
674 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
676 g.setColor(tmpBond.startCol.darker());
677 drawLine(g, xstart, ystart, xmid, ymid);
679 g.setColor(tmpBond.endCol.darker());
680 drawLine(g, xmid, ymid, xend, yend);
684 g.setColor(tmpBond.startCol);
685 drawLine(g, xstart, ystart, xmid, ymid);
687 g.setColor(tmpBond.endCol);
688 drawLine(g, xmid, ymid, xend, yend);
692 else if (depthcue && bymolecule)
694 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
696 g.setColor(Color.green.darker().darker());
697 drawLine(g, xstart, ystart, xend, yend);
699 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
701 g.setColor(Color.green.darker());
702 drawLine(g, xstart, ystart, xend, yend);
706 g.setColor(Color.green);
707 drawLine(g, xstart, ystart, xend, yend);
710 else if (!depthcue && !bymolecule)
712 g.setColor(tmpBond.startCol);
713 drawLine(g, xstart, ystart, xmid, ymid);
714 g.setColor(tmpBond.endCol);
715 drawLine(g, xmid, ymid, xend, yend);
719 drawLine(g, xstart, ystart, xend, yend);
722 if (highlightBond1 != null && highlightBond1 == tmpBond)
724 g.setColor(Color.white);
725 drawLine(g, xmid, ymid, xend, yend);
728 if (highlightBond2 != null && highlightBond2 == tmpBond)
730 g.setColor(Color.white);
731 drawLine(g, xstart, ystart, xmid, ymid);
737 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
741 if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
743 g.drawLine(x1, y1, x2, y2);
744 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
745 g.drawLine(x1, y1 - 1, x2, y2 - 1);
749 g.setColor(g.getColor().brighter());
750 g.drawLine(x1, y1, x2, y2);
751 g.drawLine(x1 + 1, y1, x2 + 1, y2);
752 g.drawLine(x1 - 1, y1, x2 - 1, y2);
757 g.drawLine(x1, y1, x2, y2);
761 public Dimension minimumsize()
766 public Dimension preferredsize()
771 public void doKeyPressed(KeyEvent evt)
773 if (evt.getKeyCode() == KeyEvent.VK_UP)
775 scale = (float) (scale * 1.1);
779 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
781 scale = (float) (scale * 0.9);
787 public void mousePressed(MouseEvent e)
790 Atom fatom = findAtom(e.getX(), e.getY());
793 fatom.isSelected = !fatom.isSelected;
797 if (foundchain != -1)
799 PDBChain chain = pdb.chains.elementAt(foundchain);
800 if (chain == mainchain)
802 if (fatom.alignmentMapping != -1)
804 if (highlightRes == null)
806 highlightRes = new Vector();
809 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
811 highlightRes.removeElement(fatom.alignmentMapping + "");
815 highlightRes.addElement(fatom.alignmentMapping + "");
829 public void mouseMoved(MouseEvent e)
832 if (highlightBond1 != null)
834 highlightBond1.at2.isSelected = false;
835 highlightBond2.at1.isSelected = false;
836 highlightBond1 = null;
837 highlightBond2 = null;
840 Atom fatom = findAtom(e.getX(), e.getY());
842 PDBChain chain = null;
843 if (foundchain != -1)
845 chain = pdb.chains.elementAt(foundchain);
846 if (chain == mainchain)
848 mouseOverStructure(fatom.resNumber, chain.id);
857 appletToolTip = chain.id + ":" + fatom.resNumber + " "
864 mouseOverStructure(-1, chain != null ? chain.id : null);
865 appletToolTip = null;
871 public void mouseClicked(MouseEvent e)
875 public void mouseEntered(MouseEvent e)
879 public void mouseExited(MouseEvent e)
883 public void mouseDragged(MouseEvent evt)
890 MCMatrix objmat = new MCMatrix(3, 3);
891 objmat.setIdentity();
893 if ((evt.getModifiers() & Event.META_MASK) != 0)
895 objmat.rotatez(((mx - omx)));
899 objmat.rotatex(((omy - my)));
900 objmat.rotatey(((omx - mx)));
904 for (int ii = 0; ii < pdb.chains.size(); ii++)
906 Vector bonds = pdb.chains.elementAt(ii).bonds;
908 for (int i = 0; i < bonds.size(); i++)
910 Bond tmpBond = (Bond) bonds.elementAt(i);
912 // Translate the bond so the centre is 0,0,0
913 tmpBond.translate(-centre[0], -centre[1], -centre[2]);
915 // Now apply the rotation matrix
916 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
917 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
919 // Now translate back again
920 tmpBond.translate(centre[0], centre[1], centre[2]);
936 public void mouseReleased(MouseEvent evt)
942 void drawLabels(Graphics g)
945 for (int ii = 0; ii < pdb.chains.size(); ii++)
947 PDBChain chain = pdb.chains.elementAt(ii);
951 Vector bonds = pdb.chains.elementAt(ii).bonds;
953 for (int i = 0; i < bonds.size(); i++)
955 Bond tmpBond = (Bond) bonds.elementAt(i);
957 if (tmpBond.at1.isSelected)
959 labelAtom(g, tmpBond, 1);
962 if (tmpBond.at2.isSelected)
965 labelAtom(g, tmpBond, 2);
972 public void labelAtom(Graphics g, Bond b, int n)
978 int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
979 int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
981 g.setColor(Color.red);
982 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
987 int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
988 int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
990 g.setColor(Color.red);
991 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
997 public Atom findAtom(int x, int y)
1003 for (int ii = 0; ii < pdb.chains.size(); ii++)
1005 PDBChain chain = pdb.chains.elementAt(ii);
1007 Bond tmpBond = null;
1009 if (chain.isVisible)
1011 Vector bonds = pdb.chains.elementAt(ii).bonds;
1013 for (int i = 0; i < bonds.size(); i++)
1015 tmpBond = (Bond) bonds.elementAt(i);
1017 truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
1019 if (Math.abs(truex - x) <= 2)
1021 int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
1023 if (Math.abs(truey - y) <= 2)
1025 fatom = tmpBond.at1;
1032 // Still here? Maybe its the last bond
1034 truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
1036 if (Math.abs(truex - x) <= 2)
1038 int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
1040 if (Math.abs(truey - y) <= 2)
1042 fatom = tmpBond.at2;
1050 if (fatom != null) // )&& chain.ds != null)
1052 chain = pdb.chains.elementAt(foundchain);
1059 public void update(Graphics g)
1064 public void highlightRes(int ii)
1066 if (!seqColoursReady)
1071 if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1078 for (index = 0; index < mainchain.bonds.size(); index++)
1080 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1081 if (tmpBond.at1.alignmentMapping == ii - 1)
1083 if (highlightBond1 != null)
1085 highlightBond1.at2.isSelected = false;
1088 if (highlightBond2 != null)
1090 highlightBond2.at1.isSelected = false;
1093 highlightBond1 = null;
1094 highlightBond2 = null;
1098 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1099 highlightBond1.at2.isSelected = true;
1102 if (index != mainchain.bonds.size())
1104 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1105 highlightBond2.at1.isSelected = true;
1112 redrawneeded = true;
1116 public void setAllchainsVisible(boolean b)
1118 for (int ii = 0; ii < pdb.chains.size(); ii++)
1120 PDBChain chain = pdb.chains.elementAt(ii);
1121 chain.isVisible = b;
1123 mainchain.isVisible = true;
1128 // ////////////////////////////////
1129 // /StructureListener
1130 public String[] getPdbFile()
1133 { pdbentry.getFile() };
1138 public void mouseOverStructure(int pdbResNum, String chain)
1140 if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1142 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1145 lastMessage = pdbResNum + chain;
1148 StringBuffer resetLastRes = new StringBuffer();
1150 StringBuffer eval = new StringBuffer();
1153 * Highlight the specified atoms in the structure.
1158 public void highlightAtoms(List<AtomSpec> atoms)
1160 if (!seqColoursReady)
1164 for (AtomSpec atom : atoms)
1166 int atomIndex = atom.getAtomIndex();
1168 if (highlightRes != null
1169 && highlightRes.contains((atomIndex - 1) + ""))
1174 highlightAtom(atomIndex);
1177 redrawneeded = true;
1184 protected void highlightAtom(int atomIndex)
1188 for (index = 0; index < mainchain.bonds.size(); index++)
1190 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1191 if (tmpBond.at1.atomIndex == atomIndex)
1193 if (highlightBond1 != null)
1195 highlightBond1.at2.isSelected = false;
1198 if (highlightBond2 != null)
1200 highlightBond2.at1.isSelected = false;
1203 highlightBond1 = null;
1204 highlightBond2 = null;
1208 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1209 highlightBond1.at2.isSelected = true;
1212 if (index != mainchain.bonds.size())
1214 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1215 highlightBond2.at1.isSelected = true;
1223 public Color getColour(int atomIndex, int pdbResNum, String chain,
1227 // if (!pdbfile.equals(pdbentry.getFile()))
1230 // return new Color(viewer.getAtomArgb(atomIndex));
1233 public void updateColours(Object source)
1236 redrawneeded = true;
1241 public void releaseReferences(Object svl)
1243 // TODO Auto-generated method stub