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.datamodel.AlignmentI;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.AppletFormatAdapter;
27 import jalview.io.FileParse;
28 import jalview.schemes.BuriedColourScheme;
29 import jalview.schemes.HelixColourScheme;
30 import jalview.schemes.HydrophobicColourScheme;
31 import jalview.schemes.PurinePyrimidineColourScheme;
32 import jalview.schemes.StrandColourScheme;
33 import jalview.schemes.TaylorColourScheme;
34 import jalview.schemes.TurnColourScheme;
35 import jalview.schemes.UserColourScheme;
36 import jalview.schemes.ZappoColourScheme;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.util.MessageManager;
40 import java.awt.BorderLayout;
41 import java.awt.CheckboxMenuItem;
42 import java.awt.Color;
43 import java.awt.Dimension;
45 import java.awt.Frame;
46 import java.awt.Graphics;
48 import java.awt.MenuBar;
49 import java.awt.MenuItem;
50 import java.awt.Panel;
51 import java.awt.TextArea;
52 import java.awt.TextField;
53 import java.awt.event.ActionEvent;
54 import java.awt.event.ActionListener;
55 import java.awt.event.ItemEvent;
56 import java.awt.event.ItemListener;
57 import java.awt.event.KeyEvent;
58 import java.awt.event.KeyListener;
59 import java.awt.event.WindowAdapter;
60 import java.awt.event.WindowEvent;
61 import java.util.ArrayList;
62 import java.util.Hashtable;
63 import java.util.List;
64 import java.util.Vector;
66 public class AppletJmol extends EmbmenuFrame implements
68 KeyListener, ActionListener, ItemListener
71 Menu fileMenu = new Menu(MessageManager.getString("action.file"));
73 Menu viewMenu = new Menu(MessageManager.getString("action.view"));
75 Menu coloursMenu = new Menu(MessageManager.getString("action.colour"));
77 Menu chainMenu = new Menu(MessageManager.getString("action.show_chain"));
79 Menu helpMenu = new Menu(MessageManager.getString("action.help"));
81 MenuItem mappingMenuItem = new MenuItem(
82 MessageManager.getString("label.view_mapping"));
84 CheckboxMenuItem seqColour = new CheckboxMenuItem(
85 MessageManager.getString("action.by_sequence"), true);
87 CheckboxMenuItem jmolColour = new CheckboxMenuItem(
88 MessageManager.getString("action.using_jmol"), false);
90 MenuItem chain = new MenuItem(MessageManager.getString("action.by_chain"));
92 MenuItem charge = new MenuItem(
93 MessageManager.getString("label.charge_cysteine"));
95 MenuItem zappo = new MenuItem(MessageManager.getString("label.zappo"));
97 MenuItem taylor = new MenuItem(MessageManager.getString("label.taylor"));
99 MenuItem hydro = new MenuItem(
100 MessageManager.getString("label.hydrophobicity"));
102 MenuItem helix = new MenuItem(
103 MessageManager.getString("label.helix_propensity"));
105 MenuItem strand = new MenuItem(
106 MessageManager.getString("label.strand_propensity"));
108 MenuItem turn = new MenuItem(
109 MessageManager.getString("label.turn_propensity"));
111 MenuItem buried = new MenuItem(
112 MessageManager.getString("label.buried_index"));
114 MenuItem purinepyrimidine = new MenuItem(
115 MessageManager.getString("label.purine_pyrimidine"));
117 MenuItem user = new MenuItem(
118 MessageManager.getString("label.user_defined_colours"));
120 MenuItem jmolHelp = new MenuItem(
121 MessageManager.getString("label.jmol_help"));
129 RenderPanel renderPanel;
133 List<AlignmentPanel> _aps = new ArrayList<AlignmentPanel>(); // remove? never
136 String fileLoadingError;
138 boolean loadedInline;
140 // boolean colourBySequence = true;
142 FeatureRenderer fr = null;
144 AppletJmolBinding jmb;
147 * datasource protocol for access to PDBEntry
149 String protocol = null;
152 * Load a bunch of pdb entries associated with sequences in the alignment and
153 * display them - aligning them if necessary.
156 * each pdb file (at least one needed)
158 * each set of sequences for each pdb file (must match number of pdb
161 * the target pdb chain corresponding with each sequence associated
162 * with each pdb file (may be null at any level)
166 * associated alignment
168 * how to get pdb data
170 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
171 String[][] boundchains, boolean align, AlignmentPanel ap,
174 throw new Error(MessageManager.getString("error.not_yet_implemented"));
177 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
178 AlignmentPanel ap, String protocol)
181 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(),
183 { pdbentry }, new SequenceI[][]
184 { seq }, new String[][]
185 { chains }, protocol);
186 jmb.setColourBySequence(true);
187 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
189 if (protocol.equals(AppletFormatAdapter.PASTE))
191 pdbentry.setId("PASTED PDB"
192 + (chains == null ? "_" : chains.toString()));
196 pdbentry.setId(pdbentry.getFile());
200 if (jalview.bin.JalviewLite.debug)
203 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
206 String alreadyMapped = StructureSelectionManager
207 .getStructureSelectionManager(ap.av.applet)
208 .alreadyMappedToFile(pdbentry.getId());
209 MCview.PDBfile reader = null;
210 if (alreadyMapped != null)
212 reader = StructureSelectionManager.getStructureSelectionManager(
213 ap.av.applet).setMapping(seq, chains, pdbentry.getFile(),
215 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
216 // FOR NOW, LETS JUST OPEN A NEW WINDOW
218 MenuBar menuBar = new MenuBar();
219 menuBar.add(fileMenu);
220 fileMenu.add(mappingMenuItem);
221 menuBar.add(viewMenu);
222 mappingMenuItem.addActionListener(this);
223 viewMenu.add(chainMenu);
224 menuBar.add(coloursMenu);
225 menuBar.add(helpMenu);
227 charge.addActionListener(this);
228 hydro.addActionListener(this);
229 chain.addActionListener(this);
230 seqColour.addItemListener(this);
231 jmolColour.addItemListener(this);
232 zappo.addActionListener(this);
233 taylor.addActionListener(this);
234 helix.addActionListener(this);
235 strand.addActionListener(this);
236 turn.addActionListener(this);
237 buried.addActionListener(this);
238 purinepyrimidine.addActionListener(this);
239 user.addActionListener(this);
241 jmolHelp.addActionListener(this);
243 coloursMenu.add(seqColour);
244 coloursMenu.add(chain);
245 coloursMenu.add(charge);
246 coloursMenu.add(zappo);
247 coloursMenu.add(taylor);
248 coloursMenu.add(hydro);
249 coloursMenu.add(helix);
250 coloursMenu.add(strand);
251 coloursMenu.add(turn);
252 coloursMenu.add(buried);
253 coloursMenu.add(purinepyrimidine);
254 coloursMenu.add(user);
255 coloursMenu.add(jmolColour);
256 helpMenu.add(jmolHelp);
257 this.setLayout(new BorderLayout());
261 renderPanel = new RenderPanel();
262 embedMenuIfNeeded(renderPanel);
263 this.add(renderPanel, BorderLayout.CENTER);
264 scriptWindow = new Panel();
265 scriptWindow.setVisible(false);
266 // this.add(scriptWindow, BorderLayout.SOUTH);
270 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
271 + "_jmol_", ap.av.applet.getDocumentBase(),
272 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
273 } catch (Exception e)
276 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
277 + ap.av.applet.getDocumentBase()
279 + ap.av.applet.getCodeBase());
284 // jmb.newJmolPopup(true, "Jmol", true);
286 this.addWindowListener(new WindowAdapter()
288 public void windowClosing(WindowEvent evt)
293 if (pdbentry.getProperty() == null)
295 pdbentry.setProperty(new Hashtable());
296 pdbentry.getProperty().put("protocol", protocol);
298 if (pdbentry.getFile() != null)
300 // import structure data from pdbentry.getFile based on given protocol
301 if (protocol.equals(AppletFormatAdapter.PASTE))
303 // TODO: JAL-623 : correctly record file contents for matching up later
304 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
305 loadInline(pdbentry.getFile());
307 else if (protocol.equals(AppletFormatAdapter.FILE)
308 || protocol.equals(AppletFormatAdapter.URL))
310 jmb.viewer.openFile(pdbentry.getFile());
314 // probably CLASSLOADER based datasource..
315 // Try and get a reader on the datasource, and pass that to Jmol
318 java.io.Reader freader = null;
321 if (jalview.bin.JalviewLite.debug)
324 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
326 // re-use the one we opened earlier
327 freader = reader.getReader();
331 if (jalview.bin.JalviewLite.debug)
334 .println("AppletJmol:Creating new PDBfile IO parser.");
336 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
338 // reader = new MCview.PDBfile(fp);
339 // could set ID, etc.
340 // if (!reader.isValid())
342 // throw new Exception("Invalid datasource.
343 // "+reader.getWarningMessage());
346 freader = fp.getReader();
350 throw new Exception(MessageManager.getString("exception.invalid_datasource_couldnt_obtain_reader"));
352 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
354 } catch (Exception e)
357 System.err.println("Couldn't access pdbentry id="
358 + pdbentry.getId() + " and file=" + pdbentry.getFile()
359 + " using protocol=" + protocol);
365 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
368 public void loadInline(String string)
371 jmb.loadInline(string);
374 void setChainMenuItems(Vector<String> chains)
376 chainMenu.removeAll();
378 MenuItem menuItem = new MenuItem(MessageManager.getString("label.all"));
379 menuItem.addActionListener(this);
381 chainMenu.add(menuItem);
383 CheckboxMenuItem menuItemCB;
384 for (String ch : chains)
386 menuItemCB = new CheckboxMenuItem(ch, true);
387 menuItemCB.addItemListener(this);
388 chainMenu.add(menuItemCB);
392 boolean allChainsSelected = false;
396 Vector<String> toshow = new Vector<String>();
397 for (int i = 0; i < chainMenu.getItemCount(); i++)
399 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
401 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
404 toshow.addElement(item.getLabel());
408 jmb.centerViewer(toshow);
415 this.setVisible(false);
418 public void actionPerformed(ActionEvent evt)
420 if (evt.getSource() == mappingMenuItem)
422 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
424 Frame frame = new Frame();
427 StringBuffer sb = new StringBuffer();
430 cap.setText(jmb.printMappings());
431 } catch (OutOfMemoryError ex)
435 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
438 jalview.bin.JalviewLite.addFrame(frame,
439 MessageManager.getString("label.pdb_sequence_mapping"), 550,
442 else if (evt.getSource() == charge)
445 jmb.colourByCharge();
448 else if (evt.getSource() == chain)
453 else if (evt.getSource() == zappo)
456 jmb.setJalviewColourScheme(new ZappoColourScheme());
458 else if (evt.getSource() == taylor)
461 jmb.setJalviewColourScheme(new TaylorColourScheme());
463 else if (evt.getSource() == hydro)
466 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
468 else if (evt.getSource() == helix)
471 jmb.setJalviewColourScheme(new HelixColourScheme());
473 else if (evt.getSource() == strand)
476 jmb.setJalviewColourScheme(new StrandColourScheme());
478 else if (evt.getSource() == turn)
481 jmb.setJalviewColourScheme(new TurnColourScheme());
483 else if (evt.getSource() == buried)
486 jmb.setJalviewColourScheme(new BuriedColourScheme());
488 else if (evt.getSource() == purinepyrimidine)
490 jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
492 else if (evt.getSource() == user)
495 new UserDefinedColours(this);
497 else if (evt.getSource() == jmolHelp)
501 ap.av.applet.getAppletContext().showDocument(
503 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
505 } catch (java.net.MalformedURLException ex)
511 allChainsSelected = true;
512 for (int i = 0; i < chainMenu.getItemCount(); i++)
514 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
516 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
521 allChainsSelected = false;
526 * tick or untick the seqColour menu entry or jmoColour entry depending upon
527 * if it was selected or not.
531 private void setEnabled(MenuItem itm)
533 jmolColour.setState(itm == jmolColour);
534 seqColour.setState(itm == seqColour);
535 jmb.setColourBySequence(itm == seqColour);
538 public void itemStateChanged(ItemEvent evt)
540 if (evt.getSource() == jmolColour)
542 setEnabled(jmolColour);
543 jmb.setColourBySequence(false);
545 else if (evt.getSource() == seqColour)
547 setEnabled(seqColour);
548 jmb.colourBySequence(ap);
550 else if (!allChainsSelected)
556 public void keyPressed(KeyEvent evt)
558 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
560 jmb.eval(inputLine.getText());
561 history.append("\n$ " + inputLine.getText());
562 inputLine.setText("");
567 public void keyTyped(KeyEvent evt)
571 public void keyReleased(KeyEvent evt)
575 public void updateColours(Object source)
577 AlignmentPanel panel = (AlignmentPanel) source;
578 jmb.colourBySequence(panel);
581 public void updateTitleAndMenus()
583 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
588 setChainMenuItems(jmb.chainNames);
589 jmb.colourBySequence(ap);
591 setTitle(jmb.getViewerTitle());
594 public void showUrl(String url)
598 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
600 } catch (java.net.MalformedURLException ex)
605 Panel splitPane = null;
607 public void showConsole(boolean showConsole)
612 splitPane = new Panel();
614 splitPane.setLayout(new java.awt.GridLayout(2, 1));
615 splitPane.add(renderPanel);
616 splitPane.add(scriptWindow);
617 scriptWindow.setVisible(true);
618 this.add(splitPane, BorderLayout.CENTER);
619 splitPane.setVisible(true);
620 splitPane.validate();
624 scriptWindow.setVisible(false);
626 add(renderPanel, BorderLayout.CENTER);
632 public float[][] functionXY(String functionName, int x, int y)
637 // /End JmolStatusListener
638 // /////////////////////////////
640 class RenderPanel extends Panel
642 Dimension currentSize = new Dimension();
644 public void update(Graphics g)
649 public void paint(Graphics g)
651 currentSize = this.getSize();
653 if (jmb.viewer == null)
655 g.setColor(Color.black);
656 g.fillRect(0, 0, currentSize.width, currentSize.height);
657 g.setColor(Color.white);
658 g.setFont(new Font("Verdana", Font.BOLD, 14));
659 g.drawString(MessageManager.getString("label.retrieving_pdb_data"),
660 20, currentSize.height / 2);
664 jmb.viewer.renderScreenImage(g, currentSize.width,
671 * @Override public Color getColour(int atomIndex, int pdbResNum, String
672 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
675 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
677 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
678 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
683 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
684 * jmb.mouseOverStructure(atomIndex, strInfo);
688 public void setJalviewColourScheme(UserColourScheme ucs)
690 jmb.setJalviewColourScheme(ucs);
693 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
695 for (int i = 0; i < _aps.size(); i++)
697 if (_aps.get(i).av.getAlignment() == alignment)
699 return (_aps.get(i));