2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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 org.jmol.api.*;
31 import org.jmol.popup.*;
32 import org.jmol.viewer.JmolConstants;
34 import jalview.schemes.*;
36 public class AppletJmol extends EmbmenuFrame implements
38 KeyListener, ActionListener, ItemListener, SequenceStructureBinding
41 Menu fileMenu = new Menu("File");
43 Menu viewMenu = new Menu("View");
45 Menu coloursMenu = new Menu("Colours");
47 Menu chainMenu = new Menu("Show Chain");
49 Menu helpMenu = new Menu("Help");
51 MenuItem mappingMenuItem = new MenuItem("View Mapping");
53 CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
55 CheckboxMenuItem jmolColour = new CheckboxMenuItem("Using Jmol", false);
57 MenuItem chain = new MenuItem("By Chain");
59 MenuItem charge = new MenuItem("Charge & Cysteine");
61 MenuItem zappo = new MenuItem("Zappo");
63 MenuItem taylor = new MenuItem("Taylor");
65 MenuItem hydro = new MenuItem("Hydrophobicity");
67 MenuItem helix = new MenuItem("Helix Propensity");
69 MenuItem strand = new MenuItem("Strand Propensity");
71 MenuItem turn = new MenuItem("Turn Propensity");
73 MenuItem buried = new MenuItem("Buried Index");
75 MenuItem user = new MenuItem("User Defined Colours");
77 MenuItem jmolHelp = new MenuItem("Jmol Help");
85 RenderPanel renderPanel;
88 ArrayList _aps = new ArrayList();
90 String fileLoadingError;
94 // boolean colourBySequence = true;
96 FeatureRenderer fr = null;
98 AppletJmolBinding jmb;
101 * datasource protocol for access to PDBEntry
103 String protocol = null;
106 * Load a bunch of pdb entries associated with sequences in the alignment and
107 * display them - aligning them if necessary.
110 * each pdb file (at least one needed)
112 * each set of sequences for each pdb file (must match number of pdb
115 * the target pdb chain corresponding with each sequence associated
116 * with each pdb file (may be null at any level)
120 * associated alignment
122 * how to get pdb data
124 public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
125 String[][] boundchains, boolean align, AlignmentPanel ap,
128 throw new Error("Not yet implemented.");
131 public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
132 AlignmentPanel ap, String protocol)
135 jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(), new PDBEntry[]
136 { pdbentry }, new SequenceI[][]
137 { seq }, new String[][]
138 { chains }, protocol);
139 jmb.setColourBySequence(true);
140 if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
142 if (protocol.equals(AppletFormatAdapter.PASTE))
144 pdbentry.setId("PASTED PDB"
145 + (chains == null ? "_" : chains.toString()));
149 pdbentry.setId(pdbentry.getFile());
153 if (jalview.bin.JalviewLite.debug)
156 .println("AppletJmol: PDB ID is '" + pdbentry.getId() + "'");
159 String alreadyMapped = StructureSelectionManager
160 .getStructureSelectionManager(ap.av.applet).alreadyMappedToFile(
162 MCview.PDBfile reader = null;
163 if (alreadyMapped != null)
165 reader = StructureSelectionManager.getStructureSelectionManager(ap.av.applet)
166 .setMapping(seq, chains, pdbentry.getFile(), protocol);
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 user.addActionListener(this);
192 jmolHelp.addActionListener(this);
194 coloursMenu.add(seqColour);
195 coloursMenu.add(chain);
196 coloursMenu.add(charge);
197 coloursMenu.add(zappo);
198 coloursMenu.add(taylor);
199 coloursMenu.add(hydro);
200 coloursMenu.add(helix);
201 coloursMenu.add(strand);
202 coloursMenu.add(turn);
203 coloursMenu.add(buried);
204 coloursMenu.add(user);
205 coloursMenu.add(jmolColour);
206 helpMenu.add(jmolHelp);
207 this.setLayout(new BorderLayout());
211 renderPanel = new RenderPanel();
212 embedMenuIfNeeded(renderPanel);
213 this.add(renderPanel, BorderLayout.CENTER);
214 scriptWindow = new Panel();
215 scriptWindow.setVisible(false);
216 // this.add(scriptWindow, BorderLayout.SOUTH);
220 jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
221 + "_jmol_", ap.av.applet.getDocumentBase(),
222 ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
223 } catch (Exception e)
226 .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
227 + ap.av.applet.getDocumentBase()
229 + ap.av.applet.getCodeBase());
234 jmb.newJmolPopup(true, "Jmol", true);
236 this.addWindowListener(new WindowAdapter()
238 public void windowClosing(WindowEvent evt)
243 if (pdbentry.getProperty() == null)
245 pdbentry.setProperty(new Hashtable());
246 pdbentry.getProperty().put("protocol", protocol);
248 if (pdbentry.getFile() != null)
250 // import structure data from pdbentry.getFile based on given protocol
251 if (protocol.equals(AppletFormatAdapter.PASTE))
253 // TODO: JAL-623 : correctly record file contents for matching up later
254 // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
255 loadInline(pdbentry.getFile());
257 else if (protocol.equals(AppletFormatAdapter.FILE)
258 || protocol.equals(AppletFormatAdapter.URL))
260 jmb.viewer.openFile(pdbentry.getFile());
264 // probably CLASSLOADER based datasource..
265 // Try and get a reader on the datasource, and pass that to Jmol
268 java.io.Reader freader = null;
271 if (jalview.bin.JalviewLite.debug)
274 .println("AppletJmol:Trying to reuse existing PDBfile IO parser.");
276 // re-use the one we opened earlier
277 freader = reader.getReader();
281 if (jalview.bin.JalviewLite.debug)
284 .println("AppletJmol:Creating new PDBfile IO parser.");
286 FileParse fp = new FileParse(pdbentry.getFile(), protocol);
288 // reader = new MCview.PDBfile(fp);
289 // could set ID, etc.
290 // if (!reader.isValid())
292 // throw new Exception("Invalid datasource.
293 // "+reader.getWarningMessage());
296 freader = fp.getReader();
301 "Invalid datasource. Could not obtain Reader.");
303 jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
305 } catch (Exception e)
308 System.err.println("Couldn't access pdbentry id="
309 + pdbentry.getId() + " and file=" + pdbentry.getFile()
310 + " using protocol=" + protocol);
316 jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
319 public void loadInline(String string)
322 jmb.loadInline(string);
325 void setChainMenuItems(Vector chains)
327 chainMenu.removeAll();
329 MenuItem menuItem = new MenuItem("All");
330 menuItem.addActionListener(this);
332 chainMenu.add(menuItem);
334 CheckboxMenuItem menuItemCB;
335 for (int c = 0; c < chains.size(); c++)
337 menuItemCB = new CheckboxMenuItem(chains.elementAt(c).toString(),
339 menuItemCB.addItemListener(this);
340 chainMenu.add(menuItemCB);
344 boolean allChainsSelected = false;
348 Vector toshow = new Vector();
350 int mlength, p, mnum;
351 for (int i = 0; i < chainMenu.getItemCount(); i++)
353 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
355 CheckboxMenuItem item = (CheckboxMenuItem) chainMenu.getItem(i);
358 toshow.addElement(item.getLabel());
362 jmb.centerViewer(toshow);
369 this.setVisible(false);
372 public void actionPerformed(ActionEvent evt)
374 if (evt.getSource() == mappingMenuItem)
376 jalview.appletgui.CutAndPasteTransfer cap = new jalview.appletgui.CutAndPasteTransfer(
378 Frame frame = new Frame();
381 StringBuffer sb = new StringBuffer();
384 for (int s = 0; s < jmb.pdbentry.length; s++)
386 sb.append(jmb.printMapping(
387 jmb.pdbentry[s].getFile()));
390 cap.setText(sb.toString());
391 } catch (OutOfMemoryError ex)
395 .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
398 jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping",
401 else if (evt.getSource() == charge)
404 jmb.colourByCharge();
407 else if (evt.getSource() == chain)
412 else if (evt.getSource() == zappo)
415 jmb.setJalviewColourScheme(new ZappoColourScheme());
417 else if (evt.getSource() == taylor)
420 jmb.setJalviewColourScheme(new TaylorColourScheme());
422 else if (evt.getSource() == hydro)
425 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
427 else if (evt.getSource() == helix)
430 jmb.setJalviewColourScheme(new HelixColourScheme());
432 else if (evt.getSource() == strand)
435 jmb.setJalviewColourScheme(new StrandColourScheme());
437 else if (evt.getSource() == turn)
440 jmb.setJalviewColourScheme(new TurnColourScheme());
442 else if (evt.getSource() == buried)
445 jmb.setJalviewColourScheme(new BuriedColourScheme());
447 else if (evt.getSource() == user)
450 new UserDefinedColours(this);
452 else if (evt.getSource() == jmolHelp)
456 ap.av.applet.getAppletContext().showDocument(
458 "http://jmol.sourceforge.net/docs/JmolUserGuide/"),
460 } catch (java.net.MalformedURLException ex)
466 allChainsSelected = true;
467 for (int i = 0; i < chainMenu.getItemCount(); i++)
469 if (chainMenu.getItem(i) instanceof CheckboxMenuItem)
470 ((CheckboxMenuItem) chainMenu.getItem(i)).setState(true);
474 allChainsSelected = false;
479 * tick or untick the seqColour menu entry or jmoColour entry depending upon if it was selected
484 private void setEnabled(MenuItem itm)
486 jmolColour.setState(itm == jmolColour);
487 seqColour.setState(itm == seqColour);
488 jmb.setColourBySequence(itm == seqColour);
491 public void itemStateChanged(ItemEvent evt)
493 if (evt.getSource() == jmolColour)
495 setEnabled(jmolColour);
496 jmb.setColourBySequence(false);
498 if (evt.getSource() == seqColour)
500 setEnabled(seqColour);
501 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
503 else if (!allChainsSelected)
507 public void keyPressed(KeyEvent evt)
509 if (evt.getKeyCode() == KeyEvent.VK_ENTER && scriptWindow.isVisible())
511 jmb.eval(inputLine.getText());
512 history.append("\n$ " + inputLine.getText());
513 inputLine.setText("");
518 public void keyTyped(KeyEvent evt)
522 public void keyReleased(KeyEvent evt)
526 public void updateColours(Object source)
528 AlignmentPanel ap = (AlignmentPanel) source;
529 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
532 public void updateTitleAndMenus()
534 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
539 setChainMenuItems(jmb.chainNames);
540 jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
542 setTitle(jmb.getViewerTitle());
545 public void showUrl(String url)
549 ap.av.applet.getAppletContext().showDocument(new java.net.URL(url),
551 } catch (java.net.MalformedURLException ex)
556 Panel splitPane = null;
558 public void showConsole(boolean showConsole)
563 splitPane = new Panel();
565 splitPane.setLayout(new java.awt.GridLayout(2, 1));
566 splitPane.add(renderPanel);
567 splitPane.add(scriptWindow);
568 scriptWindow.setVisible(true);
569 this.add(splitPane, BorderLayout.CENTER);
570 splitPane.setVisible(true);
571 splitPane.validate();
575 scriptWindow.setVisible(false);
577 add(renderPanel, BorderLayout.CENTER);
583 public float[][] functionXY(String functionName, int x, int y)
588 // /End JmolStatusListener
589 // /////////////////////////////
591 class RenderPanel extends Panel
593 Dimension currentSize = new Dimension();
595 Rectangle rectClip = new Rectangle();
597 public void update(Graphics g)
602 public void paint(Graphics g)
604 currentSize = this.getSize();
605 rectClip = g.getClipBounds();
607 if (jmb.viewer == null)
609 g.setColor(Color.black);
610 g.fillRect(0, 0, currentSize.width, currentSize.height);
611 g.setColor(Color.white);
612 g.setFont(new Font("Verdana", Font.BOLD, 14));
613 g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
617 jmb.viewer.renderScreenImage(g, currentSize, rectClip);
623 * @Override public Color getColour(int atomIndex, int pdbResNum, String
624 * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
627 * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
629 * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
630 * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
635 * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
636 * jmb.mouseOverStructure(atomIndex, strInfo);
640 public void setJalviewColourScheme(UserColourScheme ucs)
642 jmb.setJalviewColourScheme(ucs);
645 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
647 for (int i=0;i<_aps.size();i++)
649 if (((AlignmentPanel)_aps.get(i)).av.getAlignment()==alignment)
651 return ((AlignmentPanel)_aps.get(i));