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");
71 StructureSelectionManager ssm;
72 RenderPanel renderPanel;
74 String fileLoadingError;
77 boolean colourBySequence = true;
78 Vector atomsPicked = new Vector();
80 public AppletJmol(PDBEntry pdbentry,
89 this.pdbentry = pdbentry;
91 String alreadyMapped = StructureSelectionManager
92 .getStructureSelectionManager()
93 .alreadyMappedToFile(pdbentry.getId());
95 if (alreadyMapped != null)
97 StructureSelectionManager.getStructureSelectionManager()
98 .setMapping(seq, chains, pdbentry.getFile(), protocol);
99 //PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
100 //FOR NOW, LETS JUST OPEN A NEW WINDOW
103 renderPanel = new RenderPanel();
105 this.add(renderPanel, BorderLayout.CENTER);
106 viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
108 viewer.setAppletContext("jalview",
109 ap.av.applet.getDocumentBase(),
110 ap.av.applet.getCodeBase(),
113 viewer.setJmolStatusListener(this);
115 jmolpopup = JmolPopup.newJmolPopup(viewer);
117 this.addWindowListener(new WindowAdapter()
119 public void windowClosing(WindowEvent evt)
125 MenuBar menuBar = new MenuBar();
126 menuBar.add(fileMenu);
127 fileMenu.add(mappingMenuItem);
128 menuBar.add(viewMenu);
129 mappingMenuItem.addActionListener(this);
130 viewMenu.add(chainMenu);
131 menuBar.add(coloursMenu);
132 menuBar.add(helpMenu);
134 charge.addActionListener(this);
135 hydro.addActionListener(this);
136 chain.addActionListener(this);
137 seqColour.addItemListener(this);
138 zappo.addActionListener(this);
139 taylor.addActionListener(this);
140 helix.addActionListener(this);
141 strand.addActionListener(this);
142 turn.addActionListener(this);
143 buried.addActionListener(this);
144 user.addActionListener(this);
146 jmolHelp.addActionListener(this);
148 coloursMenu.add(seqColour);
149 coloursMenu.add(chain);
150 coloursMenu.add(charge);
151 coloursMenu.add(zappo);
152 coloursMenu.add(taylor);
153 coloursMenu.add(hydro);
154 coloursMenu.add(helix);
155 coloursMenu.add(strand);
156 coloursMenu.add(turn);
157 coloursMenu.add(buried);
158 coloursMenu.add(user);
160 helpMenu.add(jmolHelp);
162 this.setMenuBar(menuBar);
164 if(pdbentry.getFile()!=null)
166 if (protocol.equals(AppletFormatAdapter.PASTE))
167 loadInline(pdbentry.getFile());
169 viewer.openFile(pdbentry.getFile());
172 jalview.bin.JalviewLite.addFrame(this, "Jmol", 400,400);
175 public void loadInline(String string)
178 viewer.openStringInline(string);
182 void setChainMenuItems(Vector chains)
184 chainMenu.removeAll();
186 MenuItem menuItem = new MenuItem("All");
187 menuItem.addActionListener(this);
189 chainMenu.add(menuItem);
191 CheckboxMenuItem menuItemCB;
192 for (int c = 0; c < chains.size(); c++)
194 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(), true);
195 menuItemCB.addItemListener(this);
196 chainMenu.add(menuItemCB);
200 boolean allChainsSelected = false;
203 StringBuffer cmd = new StringBuffer();
204 for (int i = 0; i < chainMenu.getItemCount(); i++)
206 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
208 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
210 cmd.append(":" + item.getLabel() + " or ");
214 if (cmd.length() > 0)
215 cmd.setLength(cmd.length() - 4);
217 viewer.evalString("select *;restrict "
218 + cmd + ";cartoon;center " + cmd);
224 viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
225 viewer.evalStringQuiet("zap");
226 viewer.setJmolStatusListener(null);
229 //We'll need to find out what other
230 // listeners need to be shut down in Jmol
231 StructureSelectionManager
232 .getStructureSelectionManager()
233 .removeStructureViewerListener(this, pdbentry.getId());
235 this.setVisible(false);
238 public void actionPerformed(ActionEvent evt)
240 if(evt.getSource()==mappingMenuItem)
242 jalview.appletgui.CutAndPasteTransfer cap
243 = new jalview.appletgui.CutAndPasteTransfer(false, null);
244 Frame frame = new Frame();
247 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 550,
250 StructureSelectionManager.getStructureSelectionManager().printMapping(
254 else if (evt.getSource() == charge)
256 colourBySequence = false;
257 seqColour.setState(false);
258 viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
259 +"select LYS,ARG;color blue;select CYS;color yellow");
262 else if (evt.getSource() == chain)
264 colourBySequence = false;
265 seqColour.setState(false);
266 viewer.evalStringQuiet("select *;color chain");
268 else if (evt.getSource() == zappo)
270 setJalviewColourScheme(new ZappoColourScheme());
272 else if (evt.getSource() == taylor)
274 setJalviewColourScheme(new TaylorColourScheme());
276 else if (evt.getSource() == hydro)
278 setJalviewColourScheme(new HydrophobicColourScheme());
280 else if (evt.getSource() == helix)
282 setJalviewColourScheme(new HelixColourScheme());
284 else if (evt.getSource() == strand)
286 setJalviewColourScheme(new StrandColourScheme());
288 else if (evt.getSource() == turn)
290 setJalviewColourScheme(new TurnColourScheme());
292 else if (evt.getSource() == buried)
294 setJalviewColourScheme(new BuriedColourScheme());
296 else if (evt.getSource() == user)
298 new UserDefinedColours(this);
300 else if(evt.getSource() == jmolHelp)
303 ap.av.applet.getAppletContext().showDocument(
304 new java.net.URL("http://jmol.sourceforge.net/docs/JmolUserGuide/"),
306 }catch(java.net.MalformedURLException ex){}
310 allChainsSelected = true;
311 for (int i = 0; i < chainMenu.getItemCount(); i++)
313 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
314 ( (CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
317 allChainsSelected = false;
321 public void setJalviewColourScheme(ColourSchemeI cs)
323 colourBySequence = false;
324 seqColour.setState(false);
333 Enumeration en = ResidueProperties.aa3Hash.keys();
334 StringBuffer command = new StringBuffer("select *;color white;");
335 while(en.hasMoreElements())
337 res = en.nextElement().toString();
338 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
342 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
344 command.append("select "+res+";color["
346 + col.getGreen() + ","
347 + col.getBlue() + "];");
350 viewer.evalStringQuiet(command.toString());
353 public void itemStateChanged(ItemEvent evt)
355 if (evt.getSource() == seqColour)
357 colourBySequence = seqColour.getState();
358 colourBySequence(ap);
360 else if (!allChainsSelected)
364 public void keyPressed(KeyEvent evt)
366 if (evt.getKeyCode() == KeyEvent.VK_ENTER
367 && scriptWindow.isVisible())
369 viewer.evalString(inputLine.getText());
370 history.append("\n$ "+inputLine.getText());
371 inputLine.setText("");
376 public void keyTyped(KeyEvent evt)
379 public void keyReleased(KeyEvent evt){}
381 //////////////////////////////////
383 public String getPdbFile()
391 public void mouseOverStructure(int atomIndex, String strInfo)
395 int chainSeparator = strInfo.indexOf(":");
397 if(chainSeparator==-1)
398 chainSeparator = strInfo.indexOf(".");
400 pdbResNum = Integer.parseInt(
401 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator));
405 if (strInfo.indexOf(":") > -1)
406 chainId = strInfo.substring
407 (strInfo.indexOf(":")+1, strInfo.indexOf("."));
413 if (lastMessage == null || !lastMessage.equals(strInfo))
414 ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
416 lastMessage = strInfo;
419 StringBuffer resetLastRes = new StringBuffer();
420 StringBuffer eval = new StringBuffer();
422 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
424 if (!pdbfile.equals(pdbentry.getFile()))
427 if (resetLastRes.length() > 0)
429 viewer.evalStringQuiet(resetLastRes.toString());
433 eval.append("select " + pdbResNum);
435 resetLastRes.setLength(0);
436 resetLastRes.append("select " + pdbResNum);
438 if (!chain.equals(" "))
440 eval.append(":" + chain);
441 resetLastRes.append(":" + chain);
444 eval.append(";color gold;wireframe 100");
446 Color col = new Color(viewer.getAtomArgb(atomIndex));
448 resetLastRes.append(";color["
450 + col.getGreen() + ","
451 + col.getBlue() + "];wireframe 0");
453 viewer.evalStringQuiet(eval.toString());
457 public void updateColours(Object source)
459 colourBySequence( (AlignmentPanel) source);
462 //End StructureListener
463 ////////////////////////////
465 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
467 if (!pdbfile.equals(pdbentry.getFile()))
470 return new Color(viewer.getAtomArgb(atomIndex));
475 public void colourBySequence(AlignmentPanel ap)
477 if(!colourBySequence)
481 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
483 if (mapping.length < 1)
486 SequenceRenderer sr = new SequenceRenderer(ap.av);
488 boolean showFeatures = false;
489 if (ap.av.showSequenceFeatures)
494 fr = new jalview.appletgui.FeatureRenderer(ap.av);
497 fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
500 StringBuffer command = new StringBuffer();
503 for (int s = 0; s < sequence.length; s++)
505 for (int m = 0; m < mapping.length; m++)
507 if (mapping[m].getSequence() == sequence[s]
508 && ap.av.alignment.findIndex(sequence[s])>-1)
510 for (int r = 0; r < sequence[s].getLength(); r++)
512 int pos = mapping[m].getPDBResNum(
513 sequence[s].findPosition(r));
515 if (pos < 1 || pos==lastPos)
520 Color col = sr.getResidueBoxColour(sequence[s], r);
523 col = fr.findFeatureColour(col, sequence[s], r);
525 if (command.toString().endsWith(":" + mapping[m].getChain()+
528 + col.getGreen() + ","
529 + col.getBlue() + "]"))
531 command = condenseCommand(command.toString(), pos);
535 command.append(";select " + pos);
537 if (!mapping[m].getChain().equals(" "))
539 command.append(":" + mapping[m].getChain());
542 command.append(";color["
544 + col.getGreen() + ","
545 + col.getBlue() + "]");
553 if (lastCommand != null && !lastCommand.equals(command.toString()))
554 viewer.evalStringQuiet(command.toString());
556 lastCommand = command.toString();
559 StringBuffer condenseCommand(String command, int pos)
562 StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));
564 command = command.substring(sb.length());
568 if (command.indexOf("-") > -1)
570 start = command.substring(0,command.indexOf("-"));
574 start = command.substring(0, command.indexOf(":"));
577 sb.append(start+"-"+pos+command.substring(command.indexOf(":")));
582 /////////////////////////////////
585 public String eval(String strEval)
587 // System.out.println(strEval);
588 //"# 'eval' is implemented only for the applet.";
592 public void createImage(String file, String type, int quality)
595 public void setCallbackFunction(String callbackType,
596 String callbackFunction)
599 public void notifyFileLoaded(String fullPathName, String fileName,
600 String modelName, Object clientFile,
605 fileLoadingError = errorMsg;
610 fileLoadingError = null;
612 if (fileName != null)
615 jmolpopup.updateComputedMenus();
616 viewer.evalStringQuiet(
617 "select backbone;restrict;cartoon;wireframe off;spacefill off");
619 ssm = StructureSelectionManager.getStructureSelectionManager();
624 pdb = ssm.setMapping(sequence,chains,
626 AppletFormatAdapter.PASTE);
627 pdbentry.setFile("INLINE"+pdb.id);
631 pdb = ssm.setMapping(sequence,chains,
633 AppletFormatAdapter.URL);
636 pdbentry.setId(pdb.id);
638 ssm.addStructureViewerListener(this);
640 Vector chains = new Vector();
641 for (int i = 0; i < pdb.chains.size(); i++)
643 chains.addElement( ( (MCview.PDBChain) pdb.chains.elementAt(i)).id);
645 setChainMenuItems(chains);
647 colourBySequence(ap);
649 StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +
652 if (pdbentry.getProperty() != null)
654 if (pdbentry.getProperty().get("method") != null)
656 title.append(" Method: ");
657 title.append(pdbentry.getProperty().get("method"));
659 if (pdbentry.getProperty().get("chains") != null)
661 title.append(" Chain:");
662 title.append(pdbentry.getProperty().get("chains"));
666 this.setTitle(title.toString());
673 public void notifyFrameChanged(int frameNo)
675 boolean isAnimationRunning = (frameNo <= -2);
678 public void notifyScriptStart(String statusMessage, String additionalInfo)
681 public void sendConsoleEcho(String strEcho)
683 if (scriptWindow == null)
686 history.append("\n"+strEcho);
689 public void sendConsoleMessage(String strStatus)
691 if(history!=null && strStatus!=null
692 && !strStatus.equals("Script completed"))
694 history.append("\n"+strStatus);
698 public void notifyScriptTermination(String strStatus, int msWalltime)
701 public void handlePopupMenu(int x, int y)
703 jmolpopup.show(x, y);
706 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
708 notifyAtomPicked(iatom, strMeasure);
711 public void notifyNewDefaultModeMeasurement(int count, String strInfo)
714 public void notifyAtomPicked(int atomIndex, String strInfo)
717 int chainSeparator = strInfo.indexOf(":");
719 if(chainSeparator==-1)
720 chainSeparator = strInfo.indexOf(".");
723 strInfo.substring(strInfo.indexOf("]")+ 1, chainSeparator);
726 if (strInfo.indexOf(":") > -1)
727 picked+=strInfo.substring(strInfo.indexOf(":")+1,
728 strInfo.indexOf("."));
732 if (!atomsPicked.contains(picked))
734 viewer.evalString("select "+picked+";label %n %r:%c");
735 atomsPicked.addElement(picked);
739 viewer.evalString("select "+picked+";label off");
740 atomsPicked.removeElement(picked);
744 public void notifyAtomHovered(int atomIndex, String strInfo)
746 mouseOverStructure(atomIndex, strInfo);
749 public void sendSyncScript(String script, String appletName)
752 public void showUrl(String url)
755 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
757 }catch(java.net.MalformedURLException ex)
761 public void showConsole(boolean showConsole)
763 if (scriptWindow == null)
765 scriptWindow = new Panel(new BorderLayout());
766 inputLine = new TextField();
767 history = new TextArea(5, 40);
768 scriptWindow.add(history, BorderLayout.CENTER);
769 scriptWindow.add(inputLine, BorderLayout.SOUTH);
770 add(scriptWindow, BorderLayout.SOUTH);
771 scriptWindow.setVisible(false);
772 history.setEditable(false);
773 inputLine.addKeyListener(this);
776 scriptWindow.setVisible(!scriptWindow.isVisible());
780 public float functionXY(String functionName, int x, int y)
785 ///End JmolStatusListener
786 ///////////////////////////////
792 Dimension currentSize = new Dimension();
793 Rectangle rectClip = new Rectangle();
795 public void update(Graphics g) {
798 public void paint(Graphics g)
800 currentSize = this.getSize();
801 rectClip = g.getClipBounds();
805 g.setColor(Color.black);
806 g.fillRect(0, 0, currentSize.width, currentSize.height);
807 g.setColor(Color.white);
808 g.setFont(new Font("Verdana", Font.BOLD, 14));
809 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
813 viewer.renderScreenImage(g, currentSize, rectClip);