2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 jalview.schemes.*;
31 public class AppletJmol extends EmbmenuFrame implements
33 KeyListener, ActionListener, ItemListener, SequenceStructureBinding
36 Menu fileMenu = new Menu("File");
38 Menu viewMenu = new Menu("View");
40 Menu coloursMenu = new Menu("Colours");
42 Menu chainMenu = new Menu("Show Chain");
44 Menu helpMenu = new Menu("Help");
46 MenuItem mappingMenuItem = new MenuItem("View Mapping");
48 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
50 CheckboxMenuItem jmolColour = new CheckboxMenuItem("Using Jmol", false);
52 MenuItem chain = new MenuItem("By Chain");
54 MenuItem charge = new MenuItem("Charge & Cysteine");
56 MenuItem zappo = new MenuItem("Zappo");
58 MenuItem taylor = new MenuItem("Taylor");
60 MenuItem hydro = new MenuItem("Hydrophobicity");
62 MenuItem helix = new MenuItem("Helix Propensity");
64 MenuItem strand = new MenuItem("Strand Propensity");
66 MenuItem turn = new MenuItem("Turn Propensity");
68 MenuItem buried = new MenuItem("Buried Index");
70 MenuItem purinepyrimidine = new MenuItem("Purine/Pyrimidine");
72 MenuItem user = new MenuItem("User Defined Colours");
74 MenuItem jmolHelp = new MenuItem("Jmol Help");
82 RenderPanel renderPanel;
86 ArrayList _aps = new ArrayList();
88 String fileLoadingError;
92 // boolean colourBySequence = true;
94 FeatureRenderer fr = null;
96 AppletJmolBinding jmb;
99 * datasource protocol for access to PDBEntry
101 String protocol = null;
104 * Load a bunch of pdb entries associated with sequences in the alignment and
105 * display them - aligning them if necessary.
108 * each pdb file (at least one needed)
110 * each set of sequences for each pdb file (must match number of pdb
113 * the target pdb chain corresponding with each sequence associated
114 * with each pdb file (may be null at any level)
118 * associated alignment
120 * how to get pdb data
122 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
123 String[][] boundchains, boolean align, AlignmentPanel ap,
126 throw new Error("Not yet implemented.");
129 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
130 AlignmentPanel ap, String protocol)
133 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(),
135 { pdbentry }, new SequenceI[][]
136 { seq }, new String[][]
137 { chains }, protocol);
138 jmb.setColourBySequence(true);
139 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
141 if (protocol.equals(AppletFormatAdapter.PASTE))
143 pdbentry.setId("PASTED PDB"
144 + (chains == null ? "_" : chains.toString()));
148 pdbentry.setId(pdbentry.getFile());
152 if (jalview.bin.JalviewLite.debug)
155 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
158 String alreadyMapped = StructureSelectionManager
159 .getStructureSelectionManager(ap.av.applet)
160 .alreadyMappedToFile(pdbentry.getId());
161 MCview.PDBfile reader = null;
162 if (alreadyMapped != null)
164 reader = StructureSelectionManager.getStructureSelectionManager(
165 ap.av.applet).setMapping(seq, chains, pdbentry.getFile(),
167 // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
168 // FOR NOW, LETS JUST OPEN A NEW WINDOW
170 MenuBar menuBar = new MenuBar();
171 menuBar.add(fileMenu);
172 fileMenu.add(mappingMenuItem);
173 menuBar.add(viewMenu);
174 mappingMenuItem.addActionListener(this);
175 viewMenu.add(chainMenu);
176 menuBar.add(coloursMenu);
177 menuBar.add(helpMenu);
179 charge.addActionListener(this);
180 hydro.addActionListener(this);
181 chain.addActionListener(this);
182 seqColour.addItemListener(this);
183 jmolColour.addItemListener(this);
184 zappo.addActionListener(this);
185 taylor.addActionListener(this);
186 helix.addActionListener(this);
187 strand.addActionListener(this);
188 turn.addActionListener(this);
189 buried.addActionListener(this);
190 purinepyrimidine.addActionListener(this);
191 user.addActionListener(this);
193 jmolHelp.addActionListener(this);
195 coloursMenu.add(seqColour);
196 coloursMenu.add(chain);
197 coloursMenu.add(charge);
198 coloursMenu.add(zappo);
199 coloursMenu.add(taylor);
200 coloursMenu.add(hydro);
201 coloursMenu.add(helix);
202 coloursMenu.add(strand);
203 coloursMenu.add(turn);
204 coloursMenu.add(buried);
205 coloursMenu.add(purinepyrimidine);
206 coloursMenu.add(user);
207 coloursMenu.add(jmolColour);
208 helpMenu.add(jmolHelp);
209 this.setLayout(new BorderLayout());
213 renderPanel = new RenderPanel();
214 embedMenuIfNeeded(renderPanel);
215 this.add(renderPanel, BorderLayout.CENTER);
216 scriptWindow = new Panel();
217 scriptWindow.setVisible(false);
218 // this.add(scriptWindow, BorderLayout.SOUTH);
222 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
223 + "_jmol_", ap.av.applet.getDocumentBase(),
224 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
225 } catch (Exception e)
228 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
229 + ap.av.applet.getDocumentBase()
231 + ap.av.applet.getCodeBase());
236 jmb.newJmolPopup(true, "Jmol", true);
238 this.addWindowListener(new WindowAdapter()
240 public void windowClosing(WindowEvent evt)
245 if (pdbentry.getProperty() == null)
247 pdbentry.setProperty(new Hashtable());
248 pdbentry.getProperty().put("protocol", protocol);
250 if (pdbentry.getFile() != null)
252 // import structure data from pdbentry.getFile based on given protocol
253 if (protocol.equals(AppletFormatAdapter.PASTE))
255 // TODO: JAL-623 : correctly record file contents for matching up later
256 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
257 loadInline(pdbentry.getFile());
259 else if (protocol.equals(AppletFormatAdapter.FILE)
260 || protocol.equals(AppletFormatAdapter.URL))
262 jmb.viewer.openFile(pdbentry.getFile());
266 // probably CLASSLOADER based datasource..
267 // Try and get a reader on the datasource, and pass that to Jmol
270 java.io.Reader freader = null;
273 if (jalview.bin.JalviewLite.debug)
276 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
278 // re-use the one we opened earlier
279 freader = reader.getReader();
283 if (jalview.bin.JalviewLite.debug)
286 .println("AppletJmol:Creating new PDBfile IO parser.");
288 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
290 // reader = new MCview.PDBfile(fp);
291 // could set ID, etc.
292 // if (!reader.isValid())
294 // throw new Exception("Invalid datasource.
295 // "+reader.getWarningMessage());
298 freader = fp.getReader();
303 "Invalid datasource. Could not obtain Reader.");
305 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
307 } catch (Exception e)
310 System.err.println("Couldn't access pdbentry id="
311 + pdbentry.getId() + " and file=" + pdbentry.getFile()
312 + " using protocol=" + protocol);
318 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
321 public void loadInline(String string)
324 jmb.loadInline(string);
327 void setChainMenuItems(Vector chains)
329 chainMenu.removeAll();
331 MenuItem menuItem = new MenuItem("All");
332 menuItem.addActionListener(this);
334 chainMenu.add(menuItem);
336 CheckboxMenuItem menuItemCB;
337 for (int c = 0; c < chains.size(); c++)
339 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(),
341 menuItemCB.addItemListener(this);
342 chainMenu.add(menuItemCB);
346 boolean allChainsSelected = false;
350 Vector toshow = new Vector();
352 int mlength, p, mnum;
353 for (int i = 0; i < chainMenu.getItemCount(); i++)
355 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
357 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
360 toshow.addElement(item.getLabel());
364 jmb.centerViewer(toshow);
371 this.setVisible(false);
374 public void actionPerformed(ActionEvent evt)
376 if (evt.getSource() == mappingMenuItem)
378 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
380 Frame frame = new Frame();
383 StringBuffer sb = new StringBuffer();
386 for (int s = 0; s < jmb.pdbentry.length; s++)
388 sb.append(jmb.printMapping(jmb.pdbentry[s].getFile()));
391 cap.setText(sb.toString());
392 } catch (OutOfMemoryError ex)
396 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
399 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping",
402 else if (evt.getSource() == charge)
405 jmb.colourByCharge();
408 else if (evt.getSource() == chain)
413 else if (evt.getSource() == zappo)
416 jmb.setJalviewColourScheme(new ZappoColourScheme());
418 else if (evt.getSource() == taylor)
421 jmb.setJalviewColourScheme(new TaylorColourScheme());
423 else if (evt.getSource() == hydro)
426 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
428 else if (evt.getSource() == helix)
431 jmb.setJalviewColourScheme(new HelixColourScheme());
433 else if (evt.getSource() == strand)
436 jmb.setJalviewColourScheme(new StrandColourScheme());
438 else if (evt.getSource() == turn)
441 jmb.setJalviewColourScheme(new TurnColourScheme());
443 else if (evt.getSource() == buried)
446 jmb.setJalviewColourScheme(new BuriedColourScheme());
448 else if (evt.getSource() == purinepyrimidine)
450 jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
452 else if (evt.getSource() == user)
455 new UserDefinedColours(this);
457 else if (evt.getSource() == jmolHelp)
461 ap.av.applet.getAppletContext().showDocument(
463 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
465 } catch (java.net.MalformedURLException ex)
471 allChainsSelected = true;
472 for (int i = 0; i < chainMenu.getItemCount(); i++)
474 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
475 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
479 allChainsSelected = false;
484 * tick or untick the seqColour menu entry or jmoColour entry depending upon
485 * if it was selected or not.
489 private void setEnabled(MenuItem itm)
491 jmolColour.setState(itm == jmolColour);
492 seqColour.setState(itm == seqColour);
493 jmb.setColourBySequence(itm == seqColour);
496 public void itemStateChanged(ItemEvent evt)
498 if (evt.getSource() == jmolColour)
500 setEnabled(jmolColour);
501 jmb.setColourBySequence(false);
503 else if (evt.getSource() == seqColour)
505 setEnabled(seqColour);
506 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
508 else if (!allChainsSelected)
512 public void keyPressed(KeyEvent evt)
514 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
516 jmb.eval(inputLine.getText());
517 history.append("\n$ " + inputLine.getText());
518 inputLine.setText("");
523 public void keyTyped(KeyEvent evt)
527 public void keyReleased(KeyEvent evt)
531 public void updateColours(Object source)
533 AlignmentPanel ap = (AlignmentPanel) source;
534 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
537 public void updateTitleAndMenus()
539 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
544 setChainMenuItems(jmb.chainNames);
545 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
547 setTitle(jmb.getViewerTitle());
550 public void showUrl(String url)
554 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
556 } catch (java.net.MalformedURLException ex)
561 Panel splitPane = null;
563 public void showConsole(boolean showConsole)
568 splitPane = new Panel();
570 splitPane.setLayout(new java.awt.GridLayout(2, 1));
571 splitPane.add(renderPanel);
572 splitPane.add(scriptWindow);
573 scriptWindow.setVisible(true);
574 this.add(splitPane, BorderLayout.CENTER);
575 splitPane.setVisible(true);
576 splitPane.validate();
580 scriptWindow.setVisible(false);
582 add(renderPanel, BorderLayout.CENTER);
588 public float[][] functionXY(String functionName, int x, int y)
593 // /End JmolStatusListener
594 // /////////////////////////////
596 class RenderPanel extends Panel
598 Dimension currentSize = new Dimension();
600 Rectangle rectClip = new Rectangle();
602 public void update(Graphics g)
607 public void paint(Graphics g)
609 currentSize = this.getSize();
610 rectClip = g.getClipBounds();
612 if (jmb.viewer == null)
614 g.setColor(Color.black);
615 g.fillRect(0, 0, currentSize.width, currentSize.height);
616 g.setColor(Color.white);
617 g.setFont(new Font("Verdana", Font.BOLD, 14));
618 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
622 jmb.viewer.renderScreenImage(g, currentSize, rectClip);
628 * @Override public Color getColour(int atomIndex, int pdbResNum, String
629 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
632 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
634 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
635 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
640 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
641 * jmb.mouseOverStructure(atomIndex, strInfo);
645 public void setJalviewColourScheme(UserColourScheme ucs)
647 jmb.setJalviewColourScheme(ucs);
650 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
652 for (int i = 0; i < _aps.size(); i++)
654 if (((AlignmentPanel) _aps.get(i)).av.getAlignment() == alignment)
656 return ((AlignmentPanel) _aps.get(i));