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 ////////////////////////////
464 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
466 if (!pdbfile.equals(pdbentry.getFile()))
469 return new Color(viewer.getAtomArgb(atomIndex));
473 public void colourBySequence(AlignmentPanel ap)
475 if(!colourBySequence)
479 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
481 if (mapping.length < 1)
484 SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
486 boolean showFeatures = false;
487 if (ap.av.showSequenceFeatures)
492 fr = new jalview.appletgui.FeatureRenderer(ap.av);
495 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
498 StringBuffer command = new StringBuffer();
501 for (int s = 0; s < sequence.length; s++)
503 for (int m = 0; m < mapping.length; m++)
505 if (mapping[m].getSequence() == sequence[s]
506 && ap.av.alignment.findIndex(sequence[s])>-1)
508 for (int r = 0; r < sequence[s].getLength(); r++)
510 int pos = mapping[m].getPDBResNum(
511 sequence[s].findPosition(r));
513 if (pos < 1 || pos==lastPos)
518 Color col = sr.getResidueBoxColour(sequence[s], r);
521 col = fr.findFeatureColour(col, sequence[s], r);
523 if (command.toString().endsWith(":" + mapping[m].getChain()+
526 + col.getGreen() + ","
527 + col.getBlue() + "]"))
529 command = condenseCommand(command.toString(), pos);
533 command.append(";select " + pos);
535 if (!mapping[m].getChain().equals(" "))
537 command.append(":" + mapping[m].getChain());
540 command.append(";color["
542 + col.getGreen() + ","
543 + col.getBlue() + "]");
551 viewer.evalStringQuiet(command.toString());
554 StringBuffer condenseCommand(String command, int pos)
557 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
559 command = command.substring(sb.length());
563 if (command.indexOf("-") > -1)
565 start = command.substring(0,command.indexOf("-"));
569 start = command.substring(0, command.indexOf(":"));
572 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
577 /////////////////////////////////
580 public String eval(String strEval)
582 // System.out.println(strEval);
583 //"# 'eval' is implemented only for the applet.";
587 public void createImage(String file, String type, int quality)
590 public void setCallbackFunction(String callbackType,
591 String callbackFunction)
594 public void notifyFileLoaded(String fullPathName, String fileName,
595 String modelName, Object clientFile,
600 fileLoadingError = errorMsg;
605 fileLoadingError = null;
607 if (fileName != null)
610 jmolpopup.updateComputedMenus();
611 viewer.evalStringQuiet(
612 "select backbone;restrict;cartoon;wireframe off;spacefill off");
614 ssm = StructureSelectionManager.getStructureSelectionManager();
619 pdb = ssm.setMapping(sequence,
621 AppletFormatAdapter.PASTE);
622 pdbentry.setFile("INLINE"+pdb.id);
626 pdb = ssm.setMapping(sequence,
628 AppletFormatAdapter.URL);
631 pdbentry.setId(pdb.id);
633 ssm.addStructureViewerListener(this);
635 Vector chains = new Vector();
636 for (int i = 0; i < pdb.chains.size(); i++)
638 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
640 setChainMenuItems(chains);
642 colourBySequence(ap);
644 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
647 if (pdbentry.getProperty() != null)
649 if (pdbentry.getProperty().get("method") != null)
651 title.append(" Method: ");
652 title.append(pdbentry.getProperty().get("method"));
654 if (pdbentry.getProperty().get("chains") != null)
656 title.append(" Chain:");
657 title.append(pdbentry.getProperty().get("chains"));
661 this.setTitle(title.toString());
668 public void notifyFrameChanged(int frameNo)
670 boolean isAnimationRunning = (frameNo <= -2);
673 public void notifyScriptStart(String statusMessage, String additionalInfo)
676 public void sendConsoleEcho(String strEcho)
678 if (scriptWindow == null)
681 history.append("\n"+strEcho);
684 public void sendConsoleMessage(String strStatus)
686 if(history!=null && strStatus!=null
687 && !strStatus.equals("Script completed"))
689 history.append("\n"+strStatus);
693 public void notifyScriptTermination(String strStatus, int msWalltime)
696 public void handlePopupMenu(int x, int y)
698 jmolpopup.show(x, y);
701 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
703 notifyAtomPicked(iatom, strMeasure);
706 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
709 public void notifyAtomPicked(int atomIndex, String strInfo)
712 int chainSeparator = strInfo.indexOf(":");
714 if(chainSeparator==-1)
715 chainSeparator = strInfo.indexOf(".");
718 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator);
721 if (strInfo.indexOf(":") > -1)
722 picked+=strInfo.substring(strInfo.indexOf(":")+1,
723 strInfo.indexOf("."));
727 if (!atomsPicked.contains(picked))
729 viewer.evalString("select "+picked+";label %n %r:%c");
730 atomsPicked.addElement(picked);
734 viewer.evalString("select "+picked+";label off");
735 atomsPicked.removeElement(picked);
739 public void notifyAtomHovered(int atomIndex, String strInfo)
741 mouseOverStructure(atomIndex, strInfo);
744 public void sendSyncScript(String script, String appletName)
747 public void showUrl(String url)
750 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
752 }catch(java.net.MalformedURLException ex)
756 public void showConsole(boolean showConsole)
758 if (scriptWindow == null)
760 scriptWindow = new Panel(new BorderLayout());
761 inputLine = new TextField();
762 history = new TextArea(5, 40);
763 scriptWindow.add(history, BorderLayout.CENTER);
764 scriptWindow.add(inputLine, BorderLayout.SOUTH);
765 add(scriptWindow, BorderLayout.SOUTH);
766 scriptWindow.setVisible(false);
767 history.setEditable(false);
768 inputLine.addKeyListener(this);
771 scriptWindow.setVisible(!scriptWindow.isVisible());
775 public float functionXY(String functionName, int x, int y)
780 ///End JmolStatusListener
781 ///////////////////////////////
787 Dimension currentSize = new Dimension();
788 Rectangle rectClip = new Rectangle();
790 public void update(Graphics g) {
793 public void paint(Graphics g)
795 currentSize = this.getSize();
796 rectClip = g.getClipBounds();
800 g.setColor(Color.black);
801 g.fillRect(0, 0, currentSize.width, currentSize.height);
802 g.setColor(Color.white);
803 g.setFont(new Font("Verdana", Font.BOLD, 14));
804 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
808 viewer.renderScreenImage(g, currentSize, rectClip);