2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
23 import jalview.bin.JalviewLite;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.DataSourceType;
28 import jalview.io.FileParse;
29 import jalview.io.StructureFile;
30 import jalview.schemes.BuriedColourScheme;
31 import jalview.schemes.HelixColourScheme;
32 import jalview.schemes.HydrophobicColourScheme;
33 import jalview.schemes.PurinePyrimidineColourScheme;
34 import jalview.schemes.StrandColourScheme;
35 import jalview.schemes.TaylorColourScheme;
36 import jalview.schemes.TurnColourScheme;
37 import jalview.schemes.UserColourScheme;
38 import jalview.schemes.ZappoColourScheme;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.util.MessageManager;
42 import java.awt.BorderLayout;
43 import java.awt.CheckboxMenuItem;
44 import java.awt.Color;
45 import java.awt.Dimension;
47 import java.awt.Frame;
48 import java.awt.Graphics;
50 import java.awt.MenuBar;
51 import java.awt.MenuItem;
52 import java.awt.Panel;
53 import java.awt.TextArea;
54 import java.awt.TextField;
55 import java.awt.event.ActionEvent;
56 import java.awt.event.ActionListener;
57 import java.awt.event.ItemEvent;
58 import java.awt.event.ItemListener;
59 import java.awt.event.KeyEvent;
60 import java.awt.event.KeyListener;
61 import java.awt.event.WindowAdapter;
62 import java.awt.event.WindowEvent;
63 import java.util.ArrayList;
64 import java.util.List;
65 import java.util.Vector;
67 public class AppletJmol extends EmbmenuFrame implements
69 KeyListener, ActionListener, ItemListener
72 Menu fileMenu = new Menu(MessageManager.getString("action.file"));
74 Menu viewMenu = new Menu(MessageManager.getString("action.view"));
76 Menu coloursMenu = new Menu(MessageManager.getString("action.colour"));
78 Menu chainMenu = new Menu(MessageManager.getString("action.show_chain"));
80 Menu helpMenu = new Menu(MessageManager.getString("action.help"));
82 MenuItem mappingMenuItem = new MenuItem(
83 MessageManager.getString("label.view_mapping"));
85 CheckboxMenuItem seqColour = new CheckboxMenuItem(
86 MessageManager.getString("action.by_sequence"), true);
88 CheckboxMenuItem jmolColour = new CheckboxMenuItem(
89 MessageManager.getString("action.using_jmol"), false);
91 MenuItem chain = new MenuItem(MessageManager.getString("action.by_chain"));
93 MenuItem charge = new MenuItem(
94 MessageManager.getString("label.charge_cysteine"));
96 MenuItem zappo = new MenuItem(
97 MessageManager.getString("label.colourScheme_zappo"));
99 MenuItem taylor = new MenuItem(
100 MessageManager.getString("label.colourScheme_taylor"));
102 MenuItem hydro = new MenuItem(
103 MessageManager.getString("label.colourScheme_hydrophobic"));
105 MenuItem helix = new MenuItem(
106 MessageManager.getString("label.colourScheme_helix_propensity"));
108 MenuItem strand = new MenuItem(
109 MessageManager.getString("label.colourScheme_strand_propensity"));
111 MenuItem turn = new MenuItem(
112 MessageManager.getString("label.colourScheme_turn_propensity"));
114 MenuItem buried = new MenuItem(
115 MessageManager.getString("label.colourScheme_buried_index"));
117 MenuItem purinepyrimidine = new MenuItem(
118 MessageManager.getString("label.colourScheme_purine/pyrimidine"));
120 MenuItem user = new MenuItem(
121 MessageManager.getString("label.user_defined_colours"));
123 MenuItem jmolHelp = new MenuItem(
124 MessageManager.getString("label.jmol_help"));
132 RenderPanel renderPanel;
136 List<AlignmentPanel> _aps = new ArrayList<AlignmentPanel>(); // remove? never
139 String fileLoadingError;
141 boolean loadedInline;
143 // boolean colourBySequence = true;
145 FeatureRenderer fr = null;
147 AppletJmolBinding jmb;
150 * datasource protocol for access to PDBEntry
152 String protocol = null;
155 * Load a bunch of pdb entries associated with sequences in the alignment and
156 * display them - aligning them if necessary.
159 * each pdb file (at least one needed)
161 * each set of sequences for each pdb file (must match number of pdb
164 * the target pdb chain corresponding with each sequence associated
165 * with each pdb file (may be null at any level)
169 * associated alignment
171 * how to get pdb data
173 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
174 String[][] boundchains, boolean align, AlignmentPanel ap,
177 throw new Error(MessageManager.getString("error.not_yet_implemented"));
180 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
181 AlignmentPanel ap, DataSourceType protocol)
184 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(),
185 new PDBEntry[] { pdbentry }, new SequenceI[][] { seq },
187 jmb.setColourBySequence(true);
188 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
190 if (protocol == DataSourceType.PASTE)
192 pdbentry.setId("PASTED PDB"
193 + (chains == null ? "_" : chains.toString()));
197 pdbentry.setId(pdbentry.getFile());
201 if (JalviewLite.debug)
204 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
207 String alreadyMapped = StructureSelectionManager
208 .getStructureSelectionManager(ap.av.applet)
209 .alreadyMappedToFile(pdbentry.getId());
210 StructureFile reader = null;
211 if (alreadyMapped != null)
213 reader = StructureSelectionManager.getStructureSelectionManager(
214 ap.av.applet).setMapping(seq, chains, pdbentry.getFile(),
216 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
217 // FOR NOW, LETS JUST OPEN A NEW WINDOW
219 MenuBar menuBar = new MenuBar();
220 menuBar.add(fileMenu);
221 fileMenu.add(mappingMenuItem);
222 menuBar.add(viewMenu);
223 mappingMenuItem.addActionListener(this);
224 viewMenu.add(chainMenu);
225 menuBar.add(coloursMenu);
226 menuBar.add(helpMenu);
228 charge.addActionListener(this);
229 hydro.addActionListener(this);
230 chain.addActionListener(this);
231 seqColour.addItemListener(this);
232 jmolColour.addItemListener(this);
233 zappo.addActionListener(this);
234 taylor.addActionListener(this);
235 helix.addActionListener(this);
236 strand.addActionListener(this);
237 turn.addActionListener(this);
238 buried.addActionListener(this);
239 purinepyrimidine.addActionListener(this);
240 user.addActionListener(this);
242 jmolHelp.addActionListener(this);
244 coloursMenu.add(seqColour);
245 coloursMenu.add(chain);
246 coloursMenu.add(charge);
247 coloursMenu.add(zappo);
248 coloursMenu.add(taylor);
249 coloursMenu.add(hydro);
250 coloursMenu.add(helix);
251 coloursMenu.add(strand);
252 coloursMenu.add(turn);
253 coloursMenu.add(buried);
254 coloursMenu.add(purinepyrimidine);
255 coloursMenu.add(user);
256 coloursMenu.add(jmolColour);
257 helpMenu.add(jmolHelp);
258 this.setLayout(new BorderLayout());
262 renderPanel = new RenderPanel();
263 embedMenuIfNeeded(renderPanel);
264 this.add(renderPanel, BorderLayout.CENTER);
265 scriptWindow = new Panel();
266 scriptWindow.setVisible(false);
267 // this.add(scriptWindow, BorderLayout.SOUTH);
271 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
272 + "_jmol_", ap.av.applet.getDocumentBase(),
273 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
274 } catch (Exception e)
277 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
278 + ap.av.applet.getDocumentBase()
280 + ap.av.applet.getCodeBase());
285 // jmb.newJmolPopup(true, "Jmol", true);
287 this.addWindowListener(new WindowAdapter()
290 public void windowClosing(WindowEvent evt)
295 pdbentry.setProperty("protocol", protocol);
296 if (pdbentry.getFile() != null)
299 // import structure data from pdbentry.getFile based on given protocol
300 if (protocol == DataSourceType.PASTE)
302 // TODO: JAL-623 : correctly record file contents for matching up later
303 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
304 loadInline(pdbentry.getFile());
306 else if (protocol == DataSourceType.FILE
307 || protocol == DataSourceType.URL)
309 jmb.viewer.openFile(pdbentry.getFile());
313 // probably CLASSLOADER based datasource..
314 // Try and get a reader on the datasource, and pass that to Jmol
317 java.io.Reader freader = null;
320 if (jalview.bin.JalviewLite.debug)
323 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
325 // re-use the one we opened earlier
326 freader = reader.getReader();
330 if (jalview.bin.JalviewLite.debug)
333 .println("AppletJmol:Creating new PDBfile IO parser.");
335 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
337 // reader = new MCview.PDBfile(fp);
338 // could set ID, etc.
339 // if (!reader.isValid())
341 // throw new Exception("Invalid datasource.
342 // "+reader.getWarningMessage());
345 freader = fp.getReader();
351 .getString("exception.invalid_datasource_couldnt_obtain_reader"));
353 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
355 } catch (Exception e)
358 System.err.println("Couldn't access pdbentry id="
359 + pdbentry.getId() + " and file=" + pdbentry.getFile()
360 + " using protocol=" + protocol);
366 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
369 public void loadInline(String string)
372 jmb.loadInline(string);
375 void setChainMenuItems(List<String> chains)
377 chainMenu.removeAll();
379 MenuItem menuItem = new MenuItem(MessageManager.getString("label.all"));
380 menuItem.addActionListener(this);
382 chainMenu.add(menuItem);
384 CheckboxMenuItem menuItemCB;
385 for (String ch : chains)
387 menuItemCB = new CheckboxMenuItem(ch, true);
388 menuItemCB.addItemListener(this);
389 chainMenu.add(menuItemCB);
393 boolean allChainsSelected = false;
397 Vector<String> toshow = new Vector<String>();
398 for (int i = 0; i < chainMenu.getItemCount(); i++)
400 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
402 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
405 toshow.addElement(item.getLabel());
409 jmb.centerViewer(toshow);
416 this.setVisible(false);
420 public void actionPerformed(ActionEvent evt)
422 if (evt.getSource() == mappingMenuItem)
424 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
426 Frame frame = new Frame();
429 StringBuffer sb = new StringBuffer();
432 cap.setText(jmb.printMappings());
433 } catch (OutOfMemoryError ex)
437 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
440 jalview.bin.JalviewLite.addFrame(frame,
441 MessageManager.getString("label.pdb_sequence_mapping"), 550,
444 else if (evt.getSource() == charge)
447 jmb.colourByCharge();
450 else if (evt.getSource() == chain)
455 else if (evt.getSource() == zappo)
458 jmb.setJalviewColourScheme(new ZappoColourScheme());
460 else if (evt.getSource() == taylor)
463 jmb.setJalviewColourScheme(new TaylorColourScheme());
465 else if (evt.getSource() == hydro)
468 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
470 else if (evt.getSource() == helix)
473 jmb.setJalviewColourScheme(new HelixColourScheme());
475 else if (evt.getSource() == strand)
478 jmb.setJalviewColourScheme(new StrandColourScheme());
480 else if (evt.getSource() == turn)
483 jmb.setJalviewColourScheme(new TurnColourScheme());
485 else if (evt.getSource() == buried)
488 jmb.setJalviewColourScheme(new BuriedColourScheme());
490 else if (evt.getSource() == purinepyrimidine)
492 jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
494 else if (evt.getSource() == user)
497 new UserDefinedColours(this);
499 else if (evt.getSource() == jmolHelp)
503 ap.av.applet.getAppletContext().showDocument(
505 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
507 } catch (java.net.MalformedURLException ex)
513 allChainsSelected = true;
514 for (int i = 0; i < chainMenu.getItemCount(); i++)
516 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
518 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
523 allChainsSelected = false;
528 * tick or untick the seqColour menu entry or jmoColour entry depending upon
529 * if it was selected or not.
533 private void setEnabled(MenuItem itm)
535 jmolColour.setState(itm == jmolColour);
536 seqColour.setState(itm == seqColour);
537 jmb.setColourBySequence(itm == seqColour);
541 public void itemStateChanged(ItemEvent evt)
543 if (evt.getSource() == jmolColour)
545 setEnabled(jmolColour);
546 jmb.setColourBySequence(false);
548 else if (evt.getSource() == seqColour)
550 setEnabled(seqColour);
551 jmb.colourBySequence(ap);
553 else if (!allChainsSelected)
560 public void keyPressed(KeyEvent evt)
562 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
564 jmb.eval(inputLine.getText());
565 addToHistory("$ " + inputLine.getText());
566 inputLine.setText("");
572 public void keyTyped(KeyEvent evt)
577 public void keyReleased(KeyEvent evt)
581 public void updateColours(Object source)
583 AlignmentPanel panel = (AlignmentPanel) source;
584 jmb.colourBySequence(panel);
587 public void updateTitleAndMenus()
589 if (jmb.hasFileLoadingError())
594 setChainMenuItems(jmb.getChainNames());
595 jmb.colourBySequence(ap);
597 setTitle(jmb.getViewerTitle());
600 public void showUrl(String url)
604 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
606 } catch (java.net.MalformedURLException ex)
611 Panel splitPane = null;
613 public void showConsole(boolean showConsole)
618 splitPane = new Panel();
620 splitPane.setLayout(new java.awt.GridLayout(2, 1));
621 splitPane.add(renderPanel);
622 splitPane.add(scriptWindow);
623 scriptWindow.setVisible(true);
624 this.add(splitPane, BorderLayout.CENTER);
625 splitPane.setVisible(true);
626 splitPane.validate();
630 scriptWindow.setVisible(false);
632 add(renderPanel, BorderLayout.CENTER);
638 public float[][] functionXY(String functionName, int x, int y)
643 // /End JmolStatusListener
644 // /////////////////////////////
646 class RenderPanel extends Panel
648 Dimension currentSize = new Dimension();
651 public void update(Graphics g)
657 public void paint(Graphics g)
659 currentSize = this.getSize();
661 if (jmb.viewer == null)
663 g.setColor(Color.black);
664 g.fillRect(0, 0, currentSize.width, currentSize.height);
665 g.setColor(Color.white);
666 g.setFont(new Font("Verdana", Font.BOLD, 14));
667 g.drawString(MessageManager.getString("label.retrieving_pdb_data"),
668 20, currentSize.height / 2);
672 jmb.viewer.renderScreenImage(g, currentSize.width,
679 * @Override public Color getColour(int atomIndex, int pdbResNum, String
680 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
683 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
685 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
686 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
691 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
692 * jmb.mouseOverStructure(atomIndex, strInfo);
696 public void setJalviewColourScheme(UserColourScheme ucs)
698 jmb.setJalviewColourScheme(ucs);
701 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
703 for (int i = 0; i < _aps.size(); i++)
705 if (_aps.get(i).av.getAlignment() == alignment)
707 return (_aps.get(i));
714 * Append the given text to the history object
718 public void addToHistory(String text)
720 // actually currently never initialised
723 history.append("\n" + text);