X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fjmol%2FJalviewJmolBinding.java;h=dc18369aa2678804f1061b59b12552d9b3cc661b;hb=ecc31307701d785d41f503484fa5a823580ea79f;hp=061271af8395ba3575029afa04c5ecdae5511af1;hpb=f236e4e9e66583ca15dce3c3ef0cc43b02bf092e;p=jalview.git diff --git a/src/jalview/ext/jmol/JalviewJmolBinding.java b/src/jalview/ext/jmol/JalviewJmolBinding.java index 061271a..dc18369 100644 --- a/src/jalview/ext/jmol/JalviewJmolBinding.java +++ b/src/jalview/ext/jmol/JalviewJmolBinding.java @@ -31,6 +31,8 @@ import java.util.Map; import java.util.StringTokenizer; import java.util.Vector; +import javax.swing.SwingUtilities; + import org.jmol.adapter.smarter.SmarterJmolAdapter; import org.jmol.api.JmolAppConsoleInterface; import org.jmol.api.JmolSelectionListener; @@ -39,9 +41,14 @@ import org.jmol.api.JmolViewer; import org.jmol.c.CBK; import org.jmol.viewer.Viewer; +import jalview.api.AlignmentViewPanel; import jalview.api.FeatureRenderer; +import jalview.api.FeatureSettingsModelI; +import jalview.api.SequenceRenderer; +import jalview.bin.Console; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; +import jalview.gui.AppJmol; import jalview.gui.IProgressIndicator; import jalview.gui.StructureViewer.ViewerType; import jalview.io.DataSourceType; @@ -51,6 +58,8 @@ import jalview.structure.StructureCommand; import jalview.structure.StructureCommandI; import jalview.structure.StructureSelectionManager; import jalview.structures.models.AAStructureBindingModel; +import jalview.ws.dbsources.Pdb; +import javajs.util.BS; public abstract class JalviewJmolBinding extends AAStructureBindingModel implements JmolStatusListener, JmolSelectionListener, @@ -82,8 +91,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel setStructureCommands(new JmolCommands()); /* * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(), - * "jalviewJmol", ap.av.applet .getDocumentBase(), - * ap.av.applet.getCodeBase(), "", this); + * "jalviewJmol", ap.av.applet .getDocumentBase(), ap.av.applet.getCodeBase(), + * "", this); * * jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true); */ @@ -111,14 +120,26 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel return getViewerTitle("Jmol", true); } - public void closeViewer() + private String jmolScript(String script) { - // remove listeners for all structures in viewer - getSsm().removeStructureViewerListener(this, this.getStructureFiles()); - jmolViewer.dispose(); - lastCommand = null; - jmolViewer = null; - releaseUIResources(); + return jmolScript(script, false); + } + + private String jmolScript(String script, boolean useScriptWait) + { + Console.debug(">>Jmol>> " + script); + String s; + if (useScriptWait) + { + s = jmolViewer.scriptWait(script); + } + else + { + s = jmolViewer.evalStringQuiet(script); // scriptWait(script); BH + } + Console.debug("< 0 + * defensive check for null as getModelFileName can return null even when model + * count ms.mc is > 0 */ filePath = jmolViewer.ms.getModelFileName(i); if (filePath != null && !mset.contains(filePath)) @@ -210,7 +231,10 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel mset.add(filePath); } } - modelFileNames = mset.toArray(new String[mset.size()]); + if (!mset.isEmpty()) + { + modelFileNames = mset.toArray(new String[mset.size()]); + } } return modelFileNames; @@ -245,74 +269,96 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel { if (resetLastRes.length() > 0) { - jmolViewer.evalStringQuiet(resetLastRes.toString()); + jmolScript(resetLastRes.toString()); resetLastRes.setLength(0); } + StringBuilder highlightCommands=null; for (AtomSpec atom : atoms) { - highlightAtom(atom.getAtomIndex(), atom.getPdbResNum(), + StringBuilder thisAtom = highlightAtom(atom.getAtomIndex(), atom.getPdbResNum(), atom.getChain(), atom.getPdbFile()); + if (thisAtom!=null) { + if (highlightCommands==null) + { + highlightCommands=thisAtom; + } else { + highlightCommands.append(thisAtom); + } + } + } + if (highlightCommands!=null) + { + jmolHistory(false); + jmolScript(highlightCommands.toString()); + jmolHistory(true); } + // Highlight distances between atoms with a 'measure' command - not yet + // working + // if (atoms.size() >= 2) + // { + // StringBuilder sb = new StringBuilder(); + // for (int a = 0; a < atoms.size(); a++) + // { + // AtomSpec speca = atoms.get(a); + // String a_model = getModelIdForFile(speca.getPdbFile()); + // for (int b = a + 1; b < atoms.size(); b++) + // { + // AtomSpec specb = atoms.get(b); + // String b_model = getModelIdForFile(speca.getPdbFile()); + // sb.append("measure ALL (" + speca.getAtomIndex() + " and */" + // + a_model + ") (" + specb.getAtomIndex() + " and */" + // + b_model + ");"); + // } + // } + // jmolHistory(false, useScriptWait); + // jmolScript(sb.toString(), useScriptWait); + // jmolHistory(true, useScriptWait); + // } + } + } // jmol/ssm only - public void highlightAtom(int atomIndex, int pdbResNum, String chain, + private StringBuilder highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile) { - if (modelFileNames == null) + String modelId = getModelIdForFile(pdbfile); + if (modelId.isEmpty()) { - return; - } - - // look up file model number for this pdbfile - int mdlNum = 0; - // may need to adjust for URLencoding here - we don't worry about that yet. - while (mdlNum < modelFileNames.length - && !pdbfile.equals(modelFileNames[mdlNum])) - { - mdlNum++; - } - if (mdlNum == modelFileNames.length) - { - return; + return null; } - jmolHistory(false); - + StringBuilder selection = new StringBuilder(32); StringBuilder cmd = new StringBuilder(64); - cmd.append("select ").append(String.valueOf(pdbResNum)); // +modelNum - - resetLastRes.append("select ").append(String.valueOf(pdbResNum)); // +modelNum - - cmd.append(":"); - resetLastRes.append(":"); + selection.append("select ").append(String.valueOf(pdbResNum)); + selection.append(":"); if (!chain.equals(" ")) { - cmd.append(chain); - resetLastRes.append(chain); - } - { - cmd.append(" /").append(String.valueOf(mdlNum + 1)); - resetLastRes.append("/").append(String.valueOf(mdlNum + 1)); + selection.append(chain); } - cmd.append(";wireframe 100;" + cmd.toString() + " and not hetero;"); + selection.append(" /").append(modelId); - resetLastRes.append(";wireframe 0;" + resetLastRes.toString() - + " and not hetero; spacefill 0;"); + cmd.append(selection).append(";wireframe 100;").append(selection) + .append(" and not hetero;").append("spacefill 200;select none"); - cmd.append("spacefill 200;select none"); - - jmolViewer.evalStringQuiet(cmd.toString()); - jmolHistory(true); + resetLastRes.append(selection).append(";wireframe 0;").append(selection) + .append(" and not hetero; spacefill 0;"); + return cmd; } private boolean debug = true; private void jmolHistory(boolean enable) { - jmolViewer.evalStringQuiet("History " + ((debug || enable) ? "on" : "off")); + jmolHistory(enable, false); + } + + private void jmolHistory(boolean enable, boolean useScriptWait) + { + jmolScript("History " + ((debug || enable) ? "on" : "off"), + useScriptWait); } public void loadInline(String string) @@ -412,8 +458,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel } /* - * highlight position on alignment(s); if some text is returned, - * show this as a second line on the structure hover tooltip + * highlight position on alignment(s); if some text is returned, show this as a + * second line on the structure hover tooltip */ String label = getSsm().mouseOverStructure(pdbResNum, chainId, pdbfilename); @@ -441,7 +487,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel lastMessage = strInfo; if (data != null) { - System.err.println("Ignoring additional hover info: " + data + jalview.bin.Console.errPrintln("Ignoring additional hover info: " + data + " (other info: '" + strInfo + "' pos " + atomIndex + ")"); } mouseOverStructure(atomIndex, strInfo); @@ -449,8 +495,8 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel /* * { if (history != null && strStatus != null && - * !strStatus.equals("Script completed")) { history.append("\n" + strStatus); - * } } + * !strStatus.equals("Script completed")) { history.append("\n" + strStatus); } + * } */ public void notifyAtomPicked(int atomIndex, String strInfo, @@ -458,11 +504,11 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel { /** * this implements the toggle label behaviour copied from the original - * structure viewer, MCView + * structure viewer, mc_view */ if (strData != null) { - System.err.println("Ignoring additional pick data string " + strData); + jalview.bin.Console.errPrintln("Ignoring additional pick data string " + strData); } int chainSeparator = strInfo.indexOf(":"); int p = 0; @@ -489,7 +535,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel if (!atomsPicked.contains(picked)) { - jmolViewer.evalStringQuiet("select " + picked + ";label %n %r:%c"); + jmolScript("select " + picked + ";label %n %r:%c"); atomsPicked.addElement(picked); } else @@ -511,6 +557,28 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel @Override public void notifyCallback(CBK type, Object[] data) { + /* + * ensure processed in AWT thread to avoid risk of deadlocks + */ + SwingUtilities.invokeLater(new Runnable() + { + + @Override + public void run() + { + processCallback(type, data); + } + }); + } + + /** + * Processes one callback notification from Jmol + * + * @param type + * @param data + */ + protected void processCallback(CBK type, Object[] data) + { try { switch (type) @@ -542,7 +610,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel (data == null) ? ((String) null) : (String) data[1]); break; case ERROR: - // System.err.println("Ignoring error callback."); + // jalview.bin.Console.errPrintln("Ignoring error callback."); break; case SYNC: case RESIZE: @@ -552,13 +620,13 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel case CLICK: default: - System.err.println( + jalview.bin.Console.errPrintln( "Unhandled callback " + type + " " + data[1].toString()); break; } } catch (Exception e) { - System.err.println("Squashed Jmol callback handler error:"); + jalview.bin.Console.errPrintln("Squashed Jmol callback handler error:"); e.printStackTrace(); } } @@ -612,6 +680,11 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel modelFileNames = null; boolean notifyLoaded = false; String[] modelfilenames = getStructureFiles(); + if (modelfilenames == null) + { + // Jmol is still loading files! + return; + } // first check if we've lost any structures if (oldmodels != null && oldmodels.length > 0) { @@ -675,8 +748,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel // see JAL-623 - need method of matching pasted data up { pdb = getSsm().setMapping(getSequence()[pe], getChains()[pe], - pdbfile, DataSourceType.PASTE, - getIProgressIndicator()); + pdbfile, DataSourceType.PASTE, getIProgressIndicator()); getPdbEntry(modelnum).setFile("INLINE" + pdb.getId()); matches = true; foundEntry = true; @@ -744,7 +816,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel // } if (!isLoadingFromArchive()) { - jmolViewer.evalStringQuiet( + jmolScript( "model *; select backbone;restrict;cartoon;wireframe off;spacefill off"); } // register ourselves as a listener and notify the gui that it needs to @@ -755,7 +827,9 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel FeatureRenderer fr = getFeatureRenderer(null); if (fr != null) { - fr.featuresAdded(); + FeatureSettingsModelI colours = new Pdb().getFeatureColourScheme(); + ((AppJmol) getViewer()).getAlignmentPanel().av + .applyFeaturesStyle(colours); } refreshGUI(); loadNotifiesHandled++; @@ -784,8 +858,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel public abstract void sendConsoleEcho(String strEcho); /* * { showConsole(true); * - * history.append("\n" + - * strEcho); } + * history.append("\n" + strEcho); } */ // /End JmolStatusListener @@ -802,14 +875,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel public void setCallbackFunction(String callbackType, String callbackFunction) { - System.err.println("Ignoring set-callback request to associate " + jalview.bin.Console.errPrintln("Ignoring set-callback request to associate " + callbackType + " with function " + callbackFunction); } public void showHelp() { - showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp"); + showUrl("http://wiki.jmol.org" + // BH 2018 "http://jmol.sourceforge.net/docs/JmolUserGuide/" + , "jmolHelp"); } /** @@ -826,7 +901,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel */ public abstract void showConsole(boolean show); + public static Viewer getJmolData(JmolParser jmolParser) + { + return (Viewer) JmolViewer.allocateViewer(null, null, null, null, null, + "-x -o -n", jmolParser); + } + /** + * + * + * * @param renderPanel * @param jmolfileio * - when true will initialise jmol's file IO system (should be false @@ -857,13 +941,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel * @param consolePanel * - panel to contain Jmol console * @param buttonsToShow - * - buttons to show on the console, in ordr + * - buttons to show on the console, in order */ public void allocateViewer(Container renderPanel, boolean jmolfileio, String htmlName, URL documentBase, URL codeBase, String commandOptions, final Container consolePanel, String buttonsToShow) { + + jalview.bin.Console.errPrintln("Allocating Jmol Viewer: " + commandOptions); + if (commandOptions == null) { commandOptions = ""; @@ -875,7 +962,15 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel jmolViewer.setJmolStatusListener(this); // extends JmolCallbackListener - console = createJmolConsole(consolePanel, buttonsToShow); + try + { + console = createJmolConsole(consolePanel, buttonsToShow); + } catch (Throwable e) + { + jalview.bin.Console.errPrintln("Could not create Jmol application console. " + + e.getMessage()); + e.printStackTrace(); + } if (consolePanel != null) { consolePanel.addComponentListener(this); @@ -887,6 +982,9 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel protected abstract JmolAppConsoleInterface createJmolConsole( Container consolePanel, String buttonsToShow); + // BH 2018 -- Jmol console is not working due to problems with styled + // documents. + protected org.jmol.api.JmolAppConsoleInterface console = null; @Override @@ -990,4 +1088,23 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel { return ".spt"; } + + @Override + public void selectionChanged(BS arg0) + { + // TODO Auto-generated method stub + + } + + @Override + public SequenceRenderer getSequenceRenderer(AlignmentViewPanel avp) + { + return new jalview.gui.SequenceRenderer(avp.getAlignViewport()); + } + + @Override + public String getHelpURL() + { + return "http://wiki.jmol.org"; // BH 2018 + } }