2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.structure.*;
30 import org.jmol.api.*;
31 import org.jmol.adapter.smarter.SmarterJmolAdapter;
33 import org.jmol.popup.*;
34 import jalview.schemes.*;
37 public class AppletJmol extends Frame
38 implements StructureListener, JmolStatusListener,
39 KeyListener, ActionListener, ItemListener
42 Menu fileMenu = new Menu("File");
43 Menu viewMenu = new Menu("View");
44 Menu coloursMenu = new Menu("Colours");
45 Menu chainMenu = new Menu("Show Chain");
46 Menu helpMenu = new Menu("Help");
47 MenuItem mappingMenuItem = new MenuItem("View Mapping");
49 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
50 MenuItem chain = new MenuItem("By Chain");
51 MenuItem charge = new MenuItem("Charge & Cysteine");
52 MenuItem zappo = new MenuItem("Zappo");
53 MenuItem taylor = new MenuItem("Taylor");
54 MenuItem hydro = new MenuItem("Hydrophobicity");
55 MenuItem helix = new MenuItem("Helix Propensity");
56 MenuItem strand = new MenuItem("Strand Propensity");
57 MenuItem turn = new MenuItem("Turn Propensity");
58 MenuItem buried = new MenuItem("Buried Index");
59 MenuItem user = new MenuItem("User Defined Colours");
61 MenuItem jmolHelp = new MenuItem("Jmol Help");
70 StructureSelectionManager ssm;
71 RenderPanel renderPanel;
73 String fileLoadingError;
76 boolean colourBySequence = true;
77 Vector atomsPicked = new Vector();
79 public AppletJmol(PDBEntry pdbentry,
86 this.pdbentry = pdbentry;
88 String alreadyMapped = StructureSelectionManager
89 .getStructureSelectionManager()
90 .alreadyMappedToFile(pdbentry.getId());
92 if (alreadyMapped != null)
94 StructureSelectionManager.getStructureSelectionManager()
95 .setMapping(seq, pdbentry.getFile(), protocol);
96 //PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
97 //FOR NOW, LETS JUST OPEN A NEW WINDOW
100 renderPanel = new RenderPanel();
102 this.add(renderPanel, BorderLayout.CENTER);
103 viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
105 viewer.setAppletContext("jalview",
106 ap.av.applet.getDocumentBase(),
107 ap.av.applet.getCodeBase(),
110 viewer.setJmolStatusListener(this);
112 jmolpopup = JmolPopup.newJmolPopup(viewer);
114 this.addWindowListener(new WindowAdapter()
116 public void windowClosing(WindowEvent evt)
122 MenuBar menuBar = new MenuBar();
123 menuBar.add(fileMenu);
124 fileMenu.add(mappingMenuItem);
125 menuBar.add(viewMenu);
126 mappingMenuItem.addActionListener(this);
127 viewMenu.add(chainMenu);
128 menuBar.add(coloursMenu);
129 menuBar.add(helpMenu);
131 charge.addActionListener(this);
132 hydro.addActionListener(this);
133 chain.addActionListener(this);
134 seqColour.addItemListener(this);
135 zappo.addActionListener(this);
136 taylor.addActionListener(this);
137 helix.addActionListener(this);
138 strand.addActionListener(this);
139 turn.addActionListener(this);
140 buried.addActionListener(this);
141 user.addActionListener(this);
143 jmolHelp.addActionListener(this);
145 coloursMenu.add(seqColour);
146 coloursMenu.add(chain);
147 coloursMenu.add(charge);
148 coloursMenu.add(zappo);
149 coloursMenu.add(taylor);
150 coloursMenu.add(hydro);
151 coloursMenu.add(helix);
152 coloursMenu.add(strand);
153 coloursMenu.add(turn);
154 coloursMenu.add(buried);
155 coloursMenu.add(user);
157 helpMenu.add(jmolHelp);
159 this.setMenuBar(menuBar);
161 if(pdbentry.getFile()!=null)
163 if (protocol.equals(AppletFormatAdapter.PASTE))
164 loadInline(pdbentry.getFile());
166 viewer.openFile(pdbentry.getFile());
169 this.setBounds(400, 400, 400, 400);
171 this.setVisible(true);
174 public void loadInline(String string)
177 viewer.openStringInline(string);
181 void setChainMenuItems(Vector chains)
183 chainMenu.removeAll();
185 MenuItem menuItem = new MenuItem("All");
186 menuItem.addActionListener(this);
188 chainMenu.add(menuItem);
190 CheckboxMenuItem menuItemCB;
191 for (int c = 0; c < chains.size(); c++)
193 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true);
194 menuItemCB.addItemListener(this);
195 chainMenu.add(menuItemCB);
199 boolean allChainsSelected = false;
202 StringBuffer cmd = new StringBuffer();
203 for (int i = 0; i < chainMenu.getItemCount(); i++)
205 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
207 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
209 cmd.append(":" + item.getLabel() + " or ");
213 if (cmd.length() > 0)
214 cmd.setLength(cmd.length() - 4);
216 viewer.evalString("select *;restrict "
217 + cmd + ";cartoon;center " + cmd);
223 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
224 viewer.evalStringQuiet("zap");
225 viewer.setJmolStatusListener(null);
228 //We'll need to find out what other
229 // listeners need to be shut down in Jmol
230 StructureSelectionManager
231 .getStructureSelectionManager()
232 .removeStructureViewerListener(this, pdbentry.getId());
234 this.setVisible(false);
237 public void actionPerformed(ActionEvent evt)
239 if(evt.getSource()==mappingMenuItem)
241 jalview.appletgui.CutAndPasteTransfer cap
242 = new jalview.appletgui.CutAndPasteTransfer(false, null);
243 Frame frame = new Frame();
246 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550,
249 StructureSelectionManager.getStructureSelectionManager().printMapping(
253 else if (evt.getSource() == charge)
255 colourBySequence = false;
256 seqColour.setState(false);
257 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
258 +"select LYS,ARG;color blue;select CYS;color yellow");
261 else if (evt.getSource() == chain)
263 colourBySequence = false;
264 seqColour.setState(false);
265 viewer.evalStringQuiet("select *;color chain");
267 else if (evt.getSource() == zappo)
269 setJalviewColourScheme(new ZappoColourScheme());
271 else if (evt.getSource() == taylor)
273 setJalviewColourScheme(new TaylorColourScheme());
275 else if (evt.getSource() == hydro)
277 setJalviewColourScheme(new HydrophobicColourScheme());
279 else if (evt.getSource() == helix)
281 setJalviewColourScheme(new HelixColourScheme());
283 else if (evt.getSource() == strand)
285 setJalviewColourScheme(new StrandColourScheme());
287 else if (evt.getSource() == turn)
289 setJalviewColourScheme(new TurnColourScheme());
291 else if (evt.getSource() == buried)
293 setJalviewColourScheme(new BuriedColourScheme());
295 else if (evt.getSource() == user)
297 new UserDefinedColours(this);
299 else if(evt.getSource() == jmolHelp)
302 ap.av.applet.getAppletContext().showDocument(
303 new java.net.URL("http://jmol.sourceforge.net/docs/JmolUserGuide/"),
305 }catch(java.net.MalformedURLException ex){}
309 allChainsSelected = true;
310 for (int i = 0; i < chainMenu.getItemCount(); i++)
312 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
313 ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
316 allChainsSelected = false;
320 public void setJalviewColourScheme(ColourSchemeI cs)
322 colourBySequence = false;
323 seqColour.setState(false);
332 Enumeration en = ResidueProperties.aa3Hash.keys();
333 StringBuffer command = new StringBuffer("select *;color white;");
334 while(en.hasMoreElements())
336 res = en.nextElement().toString();
337 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
341 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
343 command.append("select "+res+";color["
345 + col.getGreen() + ","
346 + col.getBlue() + "];");
349 viewer.evalStringQuiet(command.toString());
352 public void itemStateChanged(ItemEvent evt)
354 if (evt.getSource() == seqColour)
356 colourBySequence = seqColour.getState();
357 colourBySequence(ap);
359 else if (!allChainsSelected)
363 public void keyPressed(KeyEvent evt)
365 if (evt.getKeyCode() == KeyEvent.VK_ENTER
366 && scriptWindow.isVisible())
368 viewer.evalString(inputLine.getText());
369 history.append("\n$ "+inputLine.getText());
370 inputLine.setText("");
375 public void keyTyped(KeyEvent evt)
378 public void keyReleased(KeyEvent evt){}
380 //////////////////////////////////
382 public String getPdbFile()
390 public void mouseOverStructure(int atomIndex, String strInfo)
394 int chainSeparator = strInfo.indexOf(":");
396 if(chainSeparator==-1)
397 chainSeparator = strInfo.indexOf(".");
399 pdbResNum = Integer.parseInt(
400 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator));
404 if (strInfo.indexOf(":") > -1)
405 chainId = strInfo.substring
406 (strInfo.indexOf(":")+1, strInfo.indexOf("."));
412 if (lastMessage == null || !lastMessage.equals(strInfo))
413 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
415 lastMessage = strInfo;
418 StringBuffer resetLastRes = new StringBuffer();
419 StringBuffer eval = new StringBuffer();
421 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
423 if (!pdbfile.equals(pdbentry.getFile()))
426 if (resetLastRes.length() > 0)
428 viewer.evalStringQuiet(resetLastRes.toString());
432 eval.append("select " + pdbResNum);
434 resetLastRes.setLength(0);
435 resetLastRes.append("select " + pdbResNum);
437 if (!chain.equals(" "))
439 eval.append(":" + chain);
440 resetLastRes.append(":" + chain);
443 eval.append(";color gold;wireframe 100");
445 Color col = new Color(viewer.getAtomArgb(atomIndex));
447 resetLastRes.append(";color["
449 + col.getGreen() + ","
450 + col.getBlue() + "];wireframe 0");
452 viewer.evalStringQuiet(eval.toString());
456 public void updateColours(Object source)
458 colourBySequence( (AlignmentPanel) source);
461 //End StructureListener
462 ////////////////////////////
465 public void colourBySequence(AlignmentPanel ap)
467 if(!colourBySequence)
471 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
473 if (mapping.length < 1)
476 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
478 boolean showFeatures = false;
479 if (ap.av.showSequenceFeatures)
484 fr = new jalview.appletgui.FeatureRenderer(ap.av);
487 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
490 StringBuffer command = new StringBuffer();
493 for (int s = 0; s < sequence.length; s++)
495 for (int m = 0; m < mapping.length; m++)
497 if (mapping[m].getSequence() == sequence[s])
499 for (int r = 0; r < sequence[s].getLength(); r++)
501 int pos = mapping[m].getPDBResNum(
502 sequence[s].findPosition(r));
504 if (pos < 1 || pos==lastPos)
509 Color col = sr.getResidueBoxColour(sequence[s], r);
512 col = fr.findFeatureColour(col, sequence[s], r);
514 if (command.toString().endsWith(":" + mapping[m].getChain()+
517 + col.getGreen() + ","
518 + col.getBlue() + "]"))
520 command = condenseCommand(command.toString(), pos);
524 command.append(";select " + pos);
526 if (!mapping[m].getChain().equals(" "))
528 command.append(":" + mapping[m].getChain());
531 command.append(";color["
533 + col.getGreen() + ","
534 + col.getBlue() + "]");
542 viewer.evalStringQuiet(command.toString());
545 StringBuffer condenseCommand(String command, int pos)
548 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
550 command = command.substring(sb.length());
554 if (command.indexOf("-") > -1)
556 start = command.substring(0,command.indexOf("-"));
560 start = command.substring(0, command.indexOf(":"));
563 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
568 /////////////////////////////////
571 public String eval(String strEval)
573 // System.out.println(strEval);
574 //"# 'eval' is implemented only for the applet.";
578 public void createImage(String file, String type, int quality)
581 public void setCallbackFunction(String callbackType,
582 String callbackFunction)
585 public void notifyFileLoaded(String fullPathName, String fileName,
586 String modelName, Object clientFile,
591 fileLoadingError = errorMsg;
596 fileLoadingError = null;
598 if (fileName != null)
601 jmolpopup.updateComputedMenus();
602 viewer.evalStringQuiet(
603 "select backbone;restrict;cartoon;wireframe off;spacefill off");
605 ssm = StructureSelectionManager.getStructureSelectionManager();
610 pdb = ssm.setMapping(sequence,
612 AppletFormatAdapter.PASTE);
613 pdbentry.setFile("INLINE"+pdb.id);
617 pdb = ssm.setMapping(sequence,
619 AppletFormatAdapter.URL);
622 pdbentry.setId(pdb.id);
624 ssm.addStructureViewerListener(this);
626 Vector chains = new Vector();
627 for (int i = 0; i < pdb.chains.size(); i++)
629 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
631 setChainMenuItems(chains);
633 colourBySequence(ap);
635 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
638 if (pdbentry.getProperty() != null)
640 if (pdbentry.getProperty().get("method") != null)
642 title.append(" Method: ");
643 title.append(pdbentry.getProperty().get("method"));
645 if (pdbentry.getProperty().get("chains") != null)
647 title.append(" Chain:");
648 title.append(pdbentry.getProperty().get("chains"));
652 this.setTitle(title.toString());
659 public void notifyFrameChanged(int frameNo)
661 boolean isAnimationRunning = (frameNo <= -2);
664 public void notifyScriptStart(String statusMessage, String additionalInfo)
667 public void sendConsoleEcho(String strEcho)
669 if (scriptWindow == null)
672 history.append("\n"+strEcho);
675 public void sendConsoleMessage(String strStatus)
677 if(history!=null && strStatus!=null
678 && !strStatus.equals("Script completed"))
680 history.append("\n"+strStatus);
684 public void notifyScriptTermination(String strStatus, int msWalltime)
687 public void handlePopupMenu(int x, int y)
689 jmolpopup.show(x, y);
692 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
694 notifyAtomPicked(iatom, strMeasure);
697 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
700 public void notifyAtomPicked(int atomIndex, String strInfo)
703 int chainSeparator = strInfo.indexOf(":");
705 if(chainSeparator==-1)
706 chainSeparator = strInfo.indexOf(".");
709 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator);
712 if (strInfo.indexOf(":") > -1)
713 picked+=strInfo.substring(strInfo.indexOf(":")+1,
714 strInfo.indexOf("."));
718 if (!atomsPicked.contains(picked))
720 viewer.evalString("select "+picked+";label %n %r:%c");
721 atomsPicked.addElement(picked);
725 viewer.evalString("select "+picked+";label off");
726 atomsPicked.removeElement(picked);
730 public void notifyAtomHovered(int atomIndex, String strInfo)
732 mouseOverStructure(atomIndex, strInfo);
735 public void sendSyncScript(String script, String appletName)
738 public void showUrl(String url)
741 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
743 }catch(java.net.MalformedURLException ex)
747 public void showConsole(boolean showConsole)
749 if (scriptWindow == null)
751 scriptWindow = new Panel(new BorderLayout());
752 inputLine = new TextField();
753 history = new TextArea(5, 40);
754 scriptWindow.add(history, BorderLayout.CENTER);
755 scriptWindow.add(inputLine, BorderLayout.SOUTH);
756 add(scriptWindow, BorderLayout.SOUTH);
757 scriptWindow.setVisible(false);
758 history.setEditable(false);
759 inputLine.addKeyListener(this);
762 scriptWindow.setVisible(!scriptWindow.isVisible());
766 public float functionXY(String functionName, int x, int y)
771 ///End JmolStatusListener
772 ///////////////////////////////
778 Dimension currentSize = new Dimension();
779 Rectangle rectClip = new Rectangle();
781 public void update(Graphics g) {
784 public void paint(Graphics g)
786 currentSize = this.getSize();
787 rectClip = g.getClipBounds();
791 g.setColor(Color.black);
792 g.fillRect(0, 0, currentSize.width, currentSize.height);
793 g.setColor(Color.white);
794 g.setFont(new Font("Verdana", Font.BOLD, 14));
795 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
799 viewer.renderScreenImage(g, currentSize, rectClip);