2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
22 import java.awt.event.*;
24 import jalview.api.SequenceStructureBinding;
25 import jalview.datamodel.*;
26 import jalview.structure.*;
29 import org.jmol.api.*;
31 import org.jmol.popup.*;
32 import org.jmol.viewer.JmolConstants;
34 import jalview.schemes.*;
36 public class AppletJmol extends EmbmenuFrame implements
38 KeyListener, ActionListener, ItemListener, SequenceStructureBinding
41 Menu fileMenu = new Menu("File");
43 Menu viewMenu = new Menu("View");
45 Menu coloursMenu = new Menu("Colours");
47 Menu chainMenu = new Menu("Show Chain");
49 Menu helpMenu = new Menu("Help");
51 MenuItem mappingMenuItem = new MenuItem("View Mapping");
53 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
55 CheckboxMenuItem jmolColour = new CheckboxMenuItem("Using Jmol", false);
57 MenuItem chain = new MenuItem("By Chain");
59 MenuItem charge = new MenuItem("Charge & Cysteine");
61 MenuItem zappo = new MenuItem("Zappo");
63 MenuItem taylor = new MenuItem("Taylor");
65 MenuItem hydro = new MenuItem("Hydrophobicity");
67 MenuItem helix = new MenuItem("Helix Propensity");
69 MenuItem strand = new MenuItem("Strand Propensity");
71 MenuItem turn = new MenuItem("Turn Propensity");
73 MenuItem buried = new MenuItem("Buried Index");
75 MenuItem purinepyrimidine = new MenuItem("Purine/Pyrimidine");
77 MenuItem user = new MenuItem("User Defined Colours");
79 MenuItem jmolHelp = new MenuItem("Jmol Help");
87 RenderPanel renderPanel;
90 ArrayList _aps = new ArrayList();
92 String fileLoadingError;
96 // boolean colourBySequence = true;
98 FeatureRenderer fr = null;
100 AppletJmolBinding jmb;
103 * datasource protocol for access to PDBEntry
105 String protocol = null;
108 * Load a bunch of pdb entries associated with sequences in the alignment and
109 * display them - aligning them if necessary.
112 * each pdb file (at least one needed)
114 * each set of sequences for each pdb file (must match number of pdb
117 * the target pdb chain corresponding with each sequence associated
118 * with each pdb file (may be null at any level)
122 * associated alignment
124 * how to get pdb data
126 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
127 String[][] boundchains, boolean align, AlignmentPanel ap,
130 throw new Error("Not yet implemented.");
133 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
134 AlignmentPanel ap, String protocol)
137 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(), new PDBEntry[]
138 { pdbentry }, new SequenceI[][]
139 { seq }, new String[][]
140 { chains }, protocol);
141 jmb.setColourBySequence(true);
142 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
144 if (protocol.equals(AppletFormatAdapter.PASTE))
146 pdbentry.setId("PASTED PDB"
147 + (chains == null ? "_" : chains.toString()));
151 pdbentry.setId(pdbentry.getFile());
155 if (jalview.bin.JalviewLite.debug)
158 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
161 String alreadyMapped = StructureSelectionManager
162 .getStructureSelectionManager(ap.av.applet).alreadyMappedToFile(
164 MCview.PDBfile reader = null;
165 if (alreadyMapped != null)
167 reader = StructureSelectionManager.getStructureSelectionManager(ap.av.applet)
168 .setMapping(seq, chains, pdbentry.getFile(), protocol);
169 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
170 // FOR NOW, LETS JUST OPEN A NEW WINDOW
172 MenuBar menuBar = new MenuBar();
173 menuBar.add(fileMenu);
174 fileMenu.add(mappingMenuItem);
175 menuBar.add(viewMenu);
176 mappingMenuItem.addActionListener(this);
177 viewMenu.add(chainMenu);
178 menuBar.add(coloursMenu);
179 menuBar.add(helpMenu);
181 charge.addActionListener(this);
182 hydro.addActionListener(this);
183 chain.addActionListener(this);
184 seqColour.addItemListener(this);
185 jmolColour.addItemListener(this);
186 zappo.addActionListener(this);
187 taylor.addActionListener(this);
188 helix.addActionListener(this);
189 strand.addActionListener(this);
190 turn.addActionListener(this);
191 buried.addActionListener(this);
192 purinepyrimidine.addActionListener(this);
193 user.addActionListener(this);
195 jmolHelp.addActionListener(this);
197 coloursMenu.add(seqColour);
198 coloursMenu.add(chain);
199 coloursMenu.add(charge);
200 coloursMenu.add(zappo);
201 coloursMenu.add(taylor);
202 coloursMenu.add(hydro);
203 coloursMenu.add(helix);
204 coloursMenu.add(strand);
205 coloursMenu.add(turn);
206 coloursMenu.add(buried);
207 coloursMenu.add(purinepyrimidine);
208 coloursMenu.add(user);
209 coloursMenu.add(jmolColour);
210 helpMenu.add(jmolHelp);
211 this.setLayout(new BorderLayout());
215 renderPanel = new RenderPanel();
216 embedMenuIfNeeded(renderPanel);
217 this.add(renderPanel, BorderLayout.CENTER);
218 scriptWindow = new Panel();
219 scriptWindow.setVisible(false);
220 // this.add(scriptWindow, BorderLayout.SOUTH);
224 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
225 + "_jmol_", ap.av.applet.getDocumentBase(),
226 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
227 } catch (Exception e)
230 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
231 + ap.av.applet.getDocumentBase()
233 + ap.av.applet.getCodeBase());
238 jmb.newJmolPopup(true, "Jmol", true);
240 this.addWindowListener(new WindowAdapter()
242 public void windowClosing(WindowEvent evt)
247 if (pdbentry.getProperty() == null)
249 pdbentry.setProperty(new Hashtable());
250 pdbentry.getProperty().put("protocol", protocol);
252 if (pdbentry.getFile() != null)
254 // import structure data from pdbentry.getFile based on given protocol
255 if (protocol.equals(AppletFormatAdapter.PASTE))
257 // TODO: JAL-623 : correctly record file contents for matching up later
258 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
259 loadInline(pdbentry.getFile());
261 else if (protocol.equals(AppletFormatAdapter.FILE)
262 || protocol.equals(AppletFormatAdapter.URL))
264 jmb.viewer.openFile(pdbentry.getFile());
268 // probably CLASSLOADER based datasource..
269 // Try and get a reader on the datasource, and pass that to Jmol
272 java.io.Reader freader = null;
275 if (jalview.bin.JalviewLite.debug)
278 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
280 // re-use the one we opened earlier
281 freader = reader.getReader();
285 if (jalview.bin.JalviewLite.debug)
288 .println("AppletJmol:Creating new PDBfile IO parser.");
290 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
292 // reader = new MCview.PDBfile(fp);
293 // could set ID, etc.
294 // if (!reader.isValid())
296 // throw new Exception("Invalid datasource.
297 // "+reader.getWarningMessage());
300 freader = fp.getReader();
305 "Invalid datasource. Could not obtain Reader.");
307 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
309 } catch (Exception e)
312 System.err.println("Couldn't access pdbentry id="
313 + pdbentry.getId() + " and file=" + pdbentry.getFile()
314 + " using protocol=" + protocol);
320 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
323 public void loadInline(String string)
326 jmb.loadInline(string);
329 void setChainMenuItems(Vector chains)
331 chainMenu.removeAll();
333 MenuItem menuItem = new MenuItem("All");
334 menuItem.addActionListener(this);
336 chainMenu.add(menuItem);
338 CheckboxMenuItem menuItemCB;
339 for (int c = 0; c < chains.size(); c++)
341 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(),
343 menuItemCB.addItemListener(this);
344 chainMenu.add(menuItemCB);
348 boolean allChainsSelected = false;
352 Vector toshow = new Vector();
354 int mlength, p, mnum;
355 for (int i = 0; i < chainMenu.getItemCount(); i++)
357 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
359 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
362 toshow.addElement(item.getLabel());
366 jmb.centerViewer(toshow);
373 this.setVisible(false);
376 public void actionPerformed(ActionEvent evt)
378 if (evt.getSource() == mappingMenuItem)
380 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
382 Frame frame = new Frame();
385 StringBuffer sb = new StringBuffer();
388 for (int s = 0; s < jmb.pdbentry.length; s++)
390 sb.append(jmb.printMapping(
391 jmb.pdbentry[s].getFile()));
394 cap.setText(sb.toString());
395 } catch (OutOfMemoryError ex)
399 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
402 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping",
405 else if (evt.getSource() == charge)
408 jmb.colourByCharge();
411 else if (evt.getSource() == chain)
416 else if (evt.getSource() == zappo)
419 jmb.setJalviewColourScheme(new ZappoColourScheme());
421 else if (evt.getSource() == taylor)
424 jmb.setJalviewColourScheme(new TaylorColourScheme());
426 else if (evt.getSource() == hydro)
429 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
431 else if (evt.getSource() == helix)
434 jmb.setJalviewColourScheme(new HelixColourScheme());
436 else if (evt.getSource() == strand)
439 jmb.setJalviewColourScheme(new StrandColourScheme());
441 else if (evt.getSource() == turn)
444 jmb.setJalviewColourScheme(new TurnColourScheme());
446 else if (evt.getSource() == buried)
449 jmb.setJalviewColourScheme(new BuriedColourScheme());
451 else if(evt.getSource() == purinepyrimidine)
453 jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
455 else if (evt.getSource() == user)
458 new UserDefinedColours(this);
460 else if (evt.getSource() == jmolHelp)
464 ap.av.applet.getAppletContext().showDocument(
466 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
468 } catch (java.net.MalformedURLException ex)
474 allChainsSelected = true;
475 for (int i = 0; i < chainMenu.getItemCount(); i++)
477 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
478 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
482 allChainsSelected = false;
487 * tick or untick the seqColour menu entry or jmoColour entry depending upon if it was selected
492 private void setEnabled(MenuItem itm)
494 jmolColour.setState(itm == jmolColour);
495 seqColour.setState(itm == seqColour);
496 jmb.setColourBySequence(itm == seqColour);
499 public void itemStateChanged(ItemEvent evt)
501 if (evt.getSource() == jmolColour)
503 setEnabled(jmolColour);
504 jmb.setColourBySequence(false);
506 if (evt.getSource() == seqColour)
508 setEnabled(seqColour);
509 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
511 else if (!allChainsSelected)
515 public void keyPressed(KeyEvent evt)
517 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
519 jmb.eval(inputLine.getText());
520 history.append("\n$ " + inputLine.getText());
521 inputLine.setText("");
526 public void keyTyped(KeyEvent evt)
530 public void keyReleased(KeyEvent evt)
534 public void updateColours(Object source)
536 AlignmentPanel ap = (AlignmentPanel) source;
537 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
540 public void updateTitleAndMenus()
542 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
547 setChainMenuItems(jmb.chainNames);
548 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
550 setTitle(jmb.getViewerTitle());
553 public void showUrl(String url)
557 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
559 } catch (java.net.MalformedURLException ex)
564 Panel splitPane = null;
566 public void showConsole(boolean showConsole)
571 splitPane = new Panel();
573 splitPane.setLayout(new java.awt.GridLayout(2, 1));
574 splitPane.add(renderPanel);
575 splitPane.add(scriptWindow);
576 scriptWindow.setVisible(true);
577 this.add(splitPane, BorderLayout.CENTER);
578 splitPane.setVisible(true);
579 splitPane.validate();
583 scriptWindow.setVisible(false);
585 add(renderPanel, BorderLayout.CENTER);
591 public float[][] functionXY(String functionName, int x, int y)
596 // /End JmolStatusListener
597 // /////////////////////////////
599 class RenderPanel extends Panel
601 Dimension currentSize = new Dimension();
603 Rectangle rectClip = new Rectangle();
605 public void update(Graphics g)
610 public void paint(Graphics g)
612 currentSize = this.getSize();
613 rectClip = g.getClipBounds();
615 if (jmb.viewer == null)
617 g.setColor(Color.black);
618 g.fillRect(0, 0, currentSize.width, currentSize.height);
619 g.setColor(Color.white);
620 g.setFont(new Font("Verdana", Font.BOLD, 14));
621 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
625 jmb.viewer.renderScreenImage(g, currentSize, rectClip);
631 * @Override public Color getColour(int atomIndex, int pdbResNum, String
632 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
635 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
637 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
638 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
643 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
644 * jmb.mouseOverStructure(atomIndex, strInfo);
648 public void setJalviewColourScheme(UserColourScheme ucs)
650 jmb.setJalviewColourScheme(ucs);
653 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
655 for (int i=0;i<_aps.size();i++)
657 if (((AlignmentPanel)_aps.get(i)).av.getAlignment()==alignment)
659 return ((AlignmentPanel)_aps.get(i));