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(
92 MessageManager.getString("action.by_chain"));
94 MenuItem charge = new MenuItem(
95 MessageManager.getString("label.charge_cysteine"));
97 MenuItem zappo = new MenuItem(
98 MessageManager.getString("label.colourScheme_zappo"));
100 MenuItem taylor = new MenuItem(
101 MessageManager.getString("label.colourScheme_taylor"));
103 MenuItem hydro = new MenuItem(
104 MessageManager.getString("label.colourScheme_hydrophobic"));
106 MenuItem helix = new MenuItem(
107 MessageManager.getString("label.colourScheme_helix_propensity"));
109 MenuItem strand = new MenuItem(
110 MessageManager.getString("label.colourScheme_strand_propensity"));
112 MenuItem turn = new MenuItem(
113 MessageManager.getString("label.colourScheme_turn_propensity"));
115 MenuItem buried = new MenuItem(
116 MessageManager.getString("label.colourScheme_buried_index"));
118 MenuItem purinepyrimidine = new MenuItem(
119 MessageManager.getString("label.colourScheme_purine/pyrimidine"));
121 MenuItem user = new MenuItem(
122 MessageManager.getString("label.user_defined_colours"));
124 MenuItem jmolHelp = new MenuItem(
125 MessageManager.getString("label.jmol_help"));
133 RenderPanel renderPanel;
137 List<AlignmentPanel> _aps = new ArrayList<>(); // remove? never
140 String fileLoadingError;
142 boolean loadedInline;
144 // boolean colourBySequence = true;
146 FeatureRenderer fr = null;
148 AppletJmolBinding jmb;
151 * datasource protocol for access to PDBEntry
153 String protocol = null;
156 * Load a bunch of pdb entries associated with sequences in the alignment and
157 * display them - aligning them if necessary.
160 * each pdb file (at least one needed)
162 * each set of sequences for each pdb file (must match number of pdb
165 * the target pdb chain corresponding with each sequence associated
166 * with each pdb file (may be null at any level)
170 * associated alignment
172 * how to get pdb data
174 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
175 String[][] boundchains, boolean align, AlignmentPanel ap,
178 throw new Error(MessageManager.getString("error.not_yet_implemented"));
181 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
182 AlignmentPanel ap, DataSourceType protocol)
185 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(),
187 { pdbentry }, new SequenceI[][] { seq }, protocol);
188 jmb.setColourBySequence(true);
189 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
191 if (protocol == DataSourceType.PASTE)
194 "PASTED PDB" + (chains == null ? "_" : chains.toString()));
198 pdbentry.setId(pdbentry.getFile());
202 if (JalviewLite.debug)
205 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
208 String alreadyMapped = StructureSelectionManager
209 .getStructureSelectionManager(ap.av.applet)
210 .alreadyMappedToFile(pdbentry.getId());
211 StructureFile reader = null;
212 if (alreadyMapped != null)
214 reader = StructureSelectionManager
215 .getStructureSelectionManager(ap.av.applet)
216 .setMapping(seq, chains, pdbentry.getFile(), protocol, null);
217 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
218 // FOR NOW, LETS JUST OPEN A NEW WINDOW
220 MenuBar menuBar = new MenuBar();
221 menuBar.add(fileMenu);
222 fileMenu.add(mappingMenuItem);
223 menuBar.add(viewMenu);
224 mappingMenuItem.addActionListener(this);
225 viewMenu.add(chainMenu);
226 menuBar.add(coloursMenu);
227 menuBar.add(helpMenu);
229 charge.addActionListener(this);
230 hydro.addActionListener(this);
231 chain.addActionListener(this);
232 seqColour.addItemListener(this);
233 jmolColour.addItemListener(this);
234 zappo.addActionListener(this);
235 taylor.addActionListener(this);
236 helix.addActionListener(this);
237 strand.addActionListener(this);
238 turn.addActionListener(this);
239 buried.addActionListener(this);
240 purinepyrimidine.addActionListener(this);
241 user.addActionListener(this);
243 jmolHelp.addActionListener(this);
245 coloursMenu.add(seqColour);
246 coloursMenu.add(chain);
247 coloursMenu.add(charge);
248 coloursMenu.add(zappo);
249 coloursMenu.add(taylor);
250 coloursMenu.add(hydro);
251 coloursMenu.add(helix);
252 coloursMenu.add(strand);
253 coloursMenu.add(turn);
254 coloursMenu.add(buried);
255 coloursMenu.add(purinepyrimidine);
256 coloursMenu.add(user);
257 coloursMenu.add(jmolColour);
258 helpMenu.add(jmolHelp);
259 this.setLayout(new BorderLayout());
263 renderPanel = new RenderPanel();
264 embedMenuIfNeeded(renderPanel);
265 this.add(renderPanel, BorderLayout.CENTER);
266 scriptWindow = new Panel();
267 scriptWindow.setVisible(false);
268 // this.add(scriptWindow, BorderLayout.SOUTH);
272 jmb.allocateViewer(renderPanel, true,
273 ap.av.applet.getName() + "_jmol_",
274 ap.av.applet.getDocumentBase(), ap.av.applet.getCodeBase(),
275 "-applet", scriptWindow, null);
276 } catch (Exception e)
279 "Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
280 + ap.av.applet.getDocumentBase() + "\nCodebase="
281 + ap.av.applet.getCodeBase());
286 // jmb.newJmolPopup(true, "Jmol", true);
288 this.addWindowListener(new WindowAdapter()
291 public void windowClosing(WindowEvent evt)
296 pdbentry.setProperty("protocol", protocol);
297 if (pdbentry.getFile() != null)
300 // import structure data from pdbentry.getFile based on given protocol
301 if (protocol == DataSourceType.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 == DataSourceType.FILE
308 || protocol == DataSourceType.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 "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 "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(
351 "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<>();
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 "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()
504 .showDocument(new java.net.URL(
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);