2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
21 package jalview.appletgui;
25 import java.awt.event.*;
27 import jalview.api.SequenceStructureBinding;
28 import jalview.datamodel.*;
29 import jalview.structure.*;
32 import jalview.schemes.*;
33 import jalview.util.MessageManager;
35 public class AppletJmol extends EmbmenuFrame implements
37 KeyListener, ActionListener, ItemListener
40 Menu fileMenu = new Menu(MessageManager.getString("action.file"));
42 Menu viewMenu = new Menu(MessageManager.getString("action.view"));
44 Menu coloursMenu = new Menu(MessageManager.getString("action.colour"));
46 Menu chainMenu = new Menu(MessageManager.getString("action.show_chain"));
48 Menu helpMenu = new Menu(MessageManager.getString("action.help"));
50 MenuItem mappingMenuItem = new MenuItem(
51 MessageManager.getString("label.view_mapping"));
53 CheckboxMenuItem seqColour = new CheckboxMenuItem(
54 MessageManager.getString("action.by_sequence"), true);
56 CheckboxMenuItem jmolColour = new CheckboxMenuItem(
57 MessageManager.getString("action.using_jmol"), false);
59 MenuItem chain = new MenuItem(MessageManager.getString("action.by_chain"));
61 MenuItem charge = new MenuItem(
62 MessageManager.getString("label.charge_cysteine"));
64 MenuItem zappo = new MenuItem(MessageManager.getString("label.zappo"));
66 MenuItem taylor = new MenuItem(MessageManager.getString("label.taylor"));
68 MenuItem hydro = new MenuItem(
69 MessageManager.getString("label.hydrophobicity"));
71 MenuItem helix = new MenuItem(
72 MessageManager.getString("label.helix_propensity"));
74 MenuItem strand = new MenuItem(
75 MessageManager.getString("label.strand_propensity"));
77 MenuItem turn = new MenuItem(
78 MessageManager.getString("label.turn_propensity"));
80 MenuItem buried = new MenuItem(
81 MessageManager.getString("label.buried_index"));
83 MenuItem purinepyrimidine = new MenuItem(
84 MessageManager.getString("label.purine_pyrimidine"));
86 MenuItem user = new MenuItem(
87 MessageManager.getString("label.user_defined_colours"));
89 MenuItem jmolHelp = new MenuItem(
90 MessageManager.getString("label.jmol_help"));
98 RenderPanel renderPanel;
102 ArrayList _aps = new ArrayList();
104 String fileLoadingError;
106 boolean loadedInline;
108 // boolean colourBySequence = true;
110 FeatureRenderer fr = null;
112 AppletJmolBinding jmb;
115 * datasource protocol for access to PDBEntry
117 String protocol = null;
120 * Load a bunch of pdb entries associated with sequences in the alignment and
121 * display them - aligning them if necessary.
124 * each pdb file (at least one needed)
126 * each set of sequences for each pdb file (must match number of pdb
129 * the target pdb chain corresponding with each sequence associated
130 * with each pdb file (may be null at any level)
134 * associated alignment
136 * how to get pdb data
138 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
139 String[][] boundchains, boolean align, AlignmentPanel ap,
142 throw new Error(MessageManager.getString("error.not_yet_implemented"));
145 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
146 AlignmentPanel ap, String protocol)
149 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(),
151 { pdbentry }, new SequenceI[][]
152 { seq }, new String[][]
153 { chains }, protocol);
154 jmb.setColourBySequence(true);
155 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
157 if (protocol.equals(AppletFormatAdapter.PASTE))
159 pdbentry.setId("PASTED PDB"
160 + (chains == null ? "_" : chains.toString()));
164 pdbentry.setId(pdbentry.getFile());
168 if (jalview.bin.JalviewLite.debug)
171 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
174 String alreadyMapped = StructureSelectionManager
175 .getStructureSelectionManager(ap.av.applet)
176 .alreadyMappedToFile(pdbentry.getId());
177 MCview.PDBfile reader = null;
178 if (alreadyMapped != null)
180 reader = StructureSelectionManager.getStructureSelectionManager(
181 ap.av.applet).setMapping(seq, chains, pdbentry.getFile(),
183 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
184 // FOR NOW, LETS JUST OPEN A NEW WINDOW
186 MenuBar menuBar = new MenuBar();
187 menuBar.add(fileMenu);
188 fileMenu.add(mappingMenuItem);
189 menuBar.add(viewMenu);
190 mappingMenuItem.addActionListener(this);
191 viewMenu.add(chainMenu);
192 menuBar.add(coloursMenu);
193 menuBar.add(helpMenu);
195 charge.addActionListener(this);
196 hydro.addActionListener(this);
197 chain.addActionListener(this);
198 seqColour.addItemListener(this);
199 jmolColour.addItemListener(this);
200 zappo.addActionListener(this);
201 taylor.addActionListener(this);
202 helix.addActionListener(this);
203 strand.addActionListener(this);
204 turn.addActionListener(this);
205 buried.addActionListener(this);
206 purinepyrimidine.addActionListener(this);
207 user.addActionListener(this);
209 jmolHelp.addActionListener(this);
211 coloursMenu.add(seqColour);
212 coloursMenu.add(chain);
213 coloursMenu.add(charge);
214 coloursMenu.add(zappo);
215 coloursMenu.add(taylor);
216 coloursMenu.add(hydro);
217 coloursMenu.add(helix);
218 coloursMenu.add(strand);
219 coloursMenu.add(turn);
220 coloursMenu.add(buried);
221 coloursMenu.add(purinepyrimidine);
222 coloursMenu.add(user);
223 coloursMenu.add(jmolColour);
224 helpMenu.add(jmolHelp);
225 this.setLayout(new BorderLayout());
229 renderPanel = new RenderPanel();
230 embedMenuIfNeeded(renderPanel);
231 this.add(renderPanel, BorderLayout.CENTER);
232 scriptWindow = new Panel();
233 scriptWindow.setVisible(false);
234 // this.add(scriptWindow, BorderLayout.SOUTH);
238 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
239 + "_jmol_", ap.av.applet.getDocumentBase(),
240 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
241 } catch (Exception e)
244 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
245 + ap.av.applet.getDocumentBase()
247 + ap.av.applet.getCodeBase());
252 jmb.newJmolPopup(true, "Jmol", true);
254 this.addWindowListener(new WindowAdapter()
256 public void windowClosing(WindowEvent evt)
261 if (pdbentry.getProperty() == null)
263 pdbentry.setProperty(new Hashtable());
264 pdbentry.getProperty().put("protocol", protocol);
266 if (pdbentry.getFile() != null)
268 // import structure data from pdbentry.getFile based on given protocol
269 if (protocol.equals(AppletFormatAdapter.PASTE))
271 // TODO: JAL-623 : correctly record file contents for matching up later
272 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
273 loadInline(pdbentry.getFile());
275 else if (protocol.equals(AppletFormatAdapter.FILE)
276 || protocol.equals(AppletFormatAdapter.URL))
278 jmb.viewer.openFile(pdbentry.getFile());
282 // probably CLASSLOADER based datasource..
283 // Try and get a reader on the datasource, and pass that to Jmol
286 java.io.Reader freader = null;
289 if (jalview.bin.JalviewLite.debug)
292 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
294 // re-use the one we opened earlier
295 freader = reader.getReader();
299 if (jalview.bin.JalviewLite.debug)
302 .println("AppletJmol:Creating new PDBfile IO parser.");
304 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
306 // reader = new MCview.PDBfile(fp);
307 // could set ID, etc.
308 // if (!reader.isValid())
310 // throw new Exception("Invalid datasource.
311 // "+reader.getWarningMessage());
314 freader = fp.getReader();
318 throw new Exception(MessageManager.getString("exception.invalid_datasource_couldnt_obtain_reader"));
320 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
322 } catch (Exception e)
325 System.err.println("Couldn't access pdbentry id="
326 + pdbentry.getId() + " and file=" + pdbentry.getFile()
327 + " using protocol=" + protocol);
333 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
336 public void loadInline(String string)
339 jmb.loadInline(string);
342 void setChainMenuItems(Vector chains)
344 chainMenu.removeAll();
346 MenuItem menuItem = new MenuItem(MessageManager.getString("label.all"));
347 menuItem.addActionListener(this);
349 chainMenu.add(menuItem);
351 CheckboxMenuItem menuItemCB;
352 for (int c = 0; c < chains.size(); c++)
354 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(),
356 menuItemCB.addItemListener(this);
357 chainMenu.add(menuItemCB);
361 boolean allChainsSelected = false;
365 Vector toshow = new Vector();
367 int mlength, p, mnum;
368 for (int i = 0; i < chainMenu.getItemCount(); i++)
370 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
372 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
375 toshow.addElement(item.getLabel());
379 jmb.centerViewer(toshow);
386 this.setVisible(false);
389 public void actionPerformed(ActionEvent evt)
391 if (evt.getSource() == mappingMenuItem)
393 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
395 Frame frame = new Frame();
398 StringBuffer sb = new StringBuffer();
401 for (int s = 0; s < jmb.pdbentry.length; s++)
403 sb.append(jmb.printMapping(jmb.pdbentry[s].getFile()));
406 cap.setText(sb.toString());
407 } catch (OutOfMemoryError ex)
411 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
414 jalview.bin.JalviewLite.addFrame(frame,
415 MessageManager.getString("label.pdb_sequence_mapping"), 550,
418 else if (evt.getSource() == charge)
421 jmb.colourByCharge();
424 else if (evt.getSource() == chain)
429 else if (evt.getSource() == zappo)
432 jmb.setJalviewColourScheme(new ZappoColourScheme());
434 else if (evt.getSource() == taylor)
437 jmb.setJalviewColourScheme(new TaylorColourScheme());
439 else if (evt.getSource() == hydro)
442 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
444 else if (evt.getSource() == helix)
447 jmb.setJalviewColourScheme(new HelixColourScheme());
449 else if (evt.getSource() == strand)
452 jmb.setJalviewColourScheme(new StrandColourScheme());
454 else if (evt.getSource() == turn)
457 jmb.setJalviewColourScheme(new TurnColourScheme());
459 else if (evt.getSource() == buried)
462 jmb.setJalviewColourScheme(new BuriedColourScheme());
464 else if (evt.getSource() == purinepyrimidine)
466 jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
468 else if (evt.getSource() == user)
471 new UserDefinedColours(this);
473 else if (evt.getSource() == jmolHelp)
477 ap.av.applet.getAppletContext().showDocument(
479 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
481 } catch (java.net.MalformedURLException ex)
487 allChainsSelected = true;
488 for (int i = 0; i < chainMenu.getItemCount(); i++)
490 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
491 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
495 allChainsSelected = false;
500 * tick or untick the seqColour menu entry or jmoColour entry depending upon
501 * if it was selected or not.
505 private void setEnabled(MenuItem itm)
507 jmolColour.setState(itm == jmolColour);
508 seqColour.setState(itm == seqColour);
509 jmb.setColourBySequence(itm == seqColour);
512 public void itemStateChanged(ItemEvent evt)
514 if (evt.getSource() == jmolColour)
516 setEnabled(jmolColour);
517 jmb.setColourBySequence(false);
519 else if (evt.getSource() == seqColour)
521 setEnabled(seqColour);
522 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
524 else if (!allChainsSelected)
528 public void keyPressed(KeyEvent evt)
530 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
532 jmb.eval(inputLine.getText());
533 history.append("\n$ " + inputLine.getText());
534 inputLine.setText("");
539 public void keyTyped(KeyEvent evt)
543 public void keyReleased(KeyEvent evt)
547 public void updateColours(Object source)
549 AlignmentPanel ap = (AlignmentPanel) source;
550 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
553 public void updateTitleAndMenus()
555 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
560 setChainMenuItems(jmb.chainNames);
561 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
563 setTitle(jmb.getViewerTitle());
566 public void showUrl(String url)
570 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
572 } catch (java.net.MalformedURLException ex)
577 Panel splitPane = null;
579 public void showConsole(boolean showConsole)
584 splitPane = new Panel();
586 splitPane.setLayout(new java.awt.GridLayout(2, 1));
587 splitPane.add(renderPanel);
588 splitPane.add(scriptWindow);
589 scriptWindow.setVisible(true);
590 this.add(splitPane, BorderLayout.CENTER);
591 splitPane.setVisible(true);
592 splitPane.validate();
596 scriptWindow.setVisible(false);
598 add(renderPanel, BorderLayout.CENTER);
604 public float[][] functionXY(String functionName, int x, int y)
609 // /End JmolStatusListener
610 // /////////////////////////////
612 class RenderPanel extends Panel
614 Dimension currentSize = new Dimension();
616 Rectangle rectClip = new Rectangle();
618 public void update(Graphics g)
623 public void paint(Graphics g)
625 currentSize = this.getSize();
626 rectClip = g.getClipBounds();
628 if (jmb.viewer == null)
630 g.setColor(Color.black);
631 g.fillRect(0, 0, currentSize.width, currentSize.height);
632 g.setColor(Color.white);
633 g.setFont(new Font("Verdana", Font.BOLD, 14));
634 g.drawString(MessageManager.getString("label.retrieving_pdb_data"),
635 20, currentSize.height / 2);
639 jmb.viewer.renderScreenImage(g, currentSize, rectClip);
645 * @Override public Color getColour(int atomIndex, int pdbResNum, String
646 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
649 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
651 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
652 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
657 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
658 * jmb.mouseOverStructure(atomIndex, strInfo);
662 public void setJalviewColourScheme(UserColourScheme ucs)
664 jmb.setJalviewColourScheme(ucs);
667 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
669 for (int i = 0; i < _aps.size(); i++)
671 if (((AlignmentPanel) _aps.get(i)).av.getAlignment() == alignment)
673 return ((AlignmentPanel) _aps.get(i));