Message Boards Message Boards

The MoleculeViewer package

Posted 7 years ago

I have just released a package entitled "MoleculeViewer", whose functionality is exactly what it says on the tin.

This package was inspired in part by previous efforts by @BoB LeSuer and @Bianca Eifert. I took the best parts of their packages, along with some of the good parts of the built-in molecule renderer, and added a few of my own tweaks. One noticeable tweak would be the depiction of multiple bonds (just like what is done in some physical models), as in the following image:



The package has a number of other nifty features and auxiliary functions, like highlighting:

MoleculeViewer["caffeine", Highlighted -> {"O", "N" -> Orange}]


and legends:

MoleculeViewer[RunOpenBabel[GetChemSpider["calicheamicin", "InChI"]], PlotLegends -> True]


Before using the package, you will need to install Open Babel for some of its conversion functionality. Additionally, to use the ChemSpider search functionality, you will need to register to obtain an API key.

Download the paclet from GitHub and install in the usual manner. Alternatively, using the technique featured here, evaluate

PacletInstall["MoleculeViewer", "Site" -> ""]

Documentation and a gallery are given as separate notebooks.

19 Replies
Posted 5 years ago

Version 3.0 of MoleculeViewer has just been released. Altho version 12 now has MoleculePlot3D[], the method for depicting multiple bonds in MoleculeViewer[] is markedly different. (The package also remains useful for older versions without built-in Molecule[] objects.) Using one of the examples in the docs for MoleculePlot3D[]:


ball-and-stick model displayed with MoleculeViewer

MoleculeViewer["Nc1nccc(n1)c1c(C)onc1c1ccccc1", PlotStyle -> "Tubes"]

stick model displayed with MoleculeViewer

Apart from the new multiple bond depiction style and the facility to change the depiction style for molecules (ball and stick, spacefilling, etc.), other features that have been added include

  • data access functions for PDB and ZINC
  • OCR for molecules using Imago OCR
  • support for chemical structure drawing with Accelrys JDraw and JChemPaint
  • ChEMBL Beaker support functions
  • additional molecule manipulation functions

and many more.

m1 = ChEMBLSMILESTo3D["Nc1nccc(n1)c1c(C)onc1c1ccccc1", Method -> "ETKDG"];
bond = Reverse[Extract[Pick[##, "Single"] & @@ ArrayPad[m1, -1], 4]];
Animate[MoleculeViewer[BondRotate[m1, bond, ?],
                       PlotRange -> 600, ViewPoint -> {-0.832, 0.453, 3.265}],
        {?, 0, 2 ?, ?/12}]

bond rotation of a random molecule

Download the paclet from GitHub.

Posted 6 years ago

Version 2.0 of MoleculeViewer has been released. The package now uses the new API system for ChemSpider, so users of previous versions will need to register for a new API key on the RSC page.

Additionally, the PubChem API is now also used to obtain structural information. For example:



Support for files and URLs has also been implemented:



as well as chemical Entity[] objects:


Download the paclet from GitHub.

Posted 7 years ago

MoleculeViewer has been updated to version 1.1. Some updates include

  • user is now explicitly prompted for a ChemSpider API key
  • implemented detailed option settings for RunOpenBabel
  • some minor tweaks and bug fixes

Please obtain the updated paclet from the GitHub releases page.

Anonymous User
Anonymous User
Posted 7 years ago

As a passer by to this article I have to ask: why is a chemical "stick modeler" needed - albeit free?

Why I ask: Mathematica has chemical modeling built-in and also downloadable data (ie, molecules), including new extension to show electron affinity of bonds in a cloud/bond manner (and i think even keeping bond angles right). And I imagine Mathematica store has one or a few extenders of that. (i know only a little about Organic and Inorganic chemistry IUPAC naming and other things - i'm no chemistry professor though)

POSTED BY: Anonymous User
Posted 7 years ago

Why I ask: Mathematica has chemical modeling built-in and also downloadable data (ie, molecules)

  • Right, this augments the built-in functionality. I've already noted the bit about depicting multiple bonds (which the default renderer does not do) in the very first post. Additionally, I do use ChemicalData[] as one possible source of info, but it does not have everything. (Try looking up the first and last examples in ChemicalData[] yourself and report back.)

And I imagine Mathematica store has one or a few extenders of that.

  • If indeed there are, they did not show up in my searching efforts. Feel free to post links to anything you've seen. ;)

P. S. I am a chemist, FWIW.


enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming! The post was also added to our curated Mathematica Add-Ons group.

POSTED BY: Moderation Team

@Murray Eisenberg, try opening Mathematica from the command line to have access to your usual $PATH. For me, I enter in the terminal

/Applications/ &

and in the front end that pops up, you should see a different return for Environment["PATH"].

@J. M. - very cool visualizations!

POSTED BY: Jason Biggs

But why is Mathematica not picking up the path from /etc/paths (really /private/etc/paths) and /etc/paths.d (really, /private/etc/paths.d) files?

As I indicated in a previous reply, one of the paths in /etc/paths is not being picked up by Mathematica as shown in the output from evaluating Environment["PATH"].

POSTED BY: Murray Eisenberg
Posted 7 years ago

The path thing seems to be an issue that is worth a separate question. It is indeed mystifying how path settings are inherited on the Mac.

In the meantime, for the upcoming point release (should be out by the coming weekend), I've now implemented a user dialog prompting a first-time user to enter his or her ChemSpider API key, which is then stored for subsequent sessions. This is hopefully less annoying than finding out from an error message that you had not entered the key yet.


Next issue: how to tell RunOpenBabel the path on which to find the openbabel binary obabel. The program is in my macOS system path (at /opt/local/bin/obabel).

I ask because I tried in your notebook molviewer.nb to execute the code:

MoleculeViewer[RunOpenBabel[GetChemSpider["ixabepilone", "InChI"]], 
 PlotLegends -> True]

But that gave an error:

RunProcess: Program babel not found. Check Environment'"PATH"]
POSTED BY: Murray Eisenberg
Posted 7 years ago

Interesting, I thought the installer should have taken care of appending to the path environment variable. (It does on *nix and Windows, so Mac feedback is valuable!)

There is a procedure given here (alternatively, here), which as I see now is quite similar to what one does on *nix for editing PATH: go through the command line, and edit the PATH variable yourself with a text editor. (I am told one might sometimes need to reboot the computer after making such a change. In Ubuntu, I didn't have to.)

After all this, evaluate


in Mathematica to verify if the changes to PATH were saved, before loading the package.


Although the location of babel is on my system path — which is set in ~/.profile — still Mathematica is not recognizing that:


and hence your RunOpenBabel function fails with the same error I listed. This is even after a system reboot.

The link you provide for changing the value of PATH will probably lead to chaos on macOS, because if the file .bash_profile exists, then .profile will not even be read by the shell, and .profile is where MacPorts and other apps place additions to PATH (along with various command aliases).

What's even stranger is the following. In a new Mathematica session, before loading MoleculeViewer, I evaluate the commands

    SetEnvironment["PATH" -> StringJoin[Environment["PATH"], ":opt/local/bin"]]

and get output:


The last location there, /opt/local/bin is definitely where the obabel executable resides, as I can check with the Mac's system command:

mdfind -name obabel

And indeed, in a Terminal session I can evaluate, e.g., either of:

obabel -H
/opt/local/bin/obabel -H

and get the expected output.

POSTED BY: Murray Eisenberg

Yes, definitely worth at least documenting what you just said about entering the ChemSpider API ID!

When others use a package, it's a wholly different thing when you, the package creator, uses it for himself (as I know all two well from my designing packages for students over many years). I urge some documentation in the standard Wolfram Documentation Center format for an add-on, so that it will be available when the use clicks the package name in the Documentation Center's Addons link.

And certain it would help now if you added to each of gallery.nb and movieviewer.nb a line

 $ChemSpiderAPIToken = "" (* enter your ChemSpider API ID here *)

with an explanatory text cell on how to retrieve it that ID.

Thank you for the package and your help in how to use it!

POSTED BY: Murray Eisenberg
Posted 7 years ago

I agree with your assessment; I have been using it for a while (and its predecessor functions for a while more), so I do have blind spots with respect to usability. (Feedback like yours is a good thing!)

I believe I did something like that for $ChemSpiderAPIToken in one of the initialization cells for molviewer.nb, but did not add one for gallery.nb. I'll commit that, along with fixes for some bug reports that have been sent in, in my next point (i.e., 1.1) release.


I installed the MoleculeViewer paclet in the usual way, as well as openbabel (Macports version). And I registered with ChemSpider (which was a confusing procedure, as it involves first getting a Royal Society of Chemistry login and then linking tht with ChemSpider).

But how do I actually get that ChemSpider API key? And how do I tell MoleculeViewer about it?

I ask because when I tried to reevaluate a cell in the notebook gallery.nb, I got error message:

GetChemSpider: ChemSpider API key in $ChemSpiderAPIToken not detected or invalid
POSTED BY: Murray Eisenberg
Posted 7 years ago

When you have a ChemSpider (actually, RSC) account, go to your ChemSpider profile page. The API key is listed under the "Security Token" field. Copy that, and assign the string to $ChemSpiderAPIToken, which should look like this:

$ChemSpiderAPIToken = "ef739634-61c8-3c6c-6ff5-6d3a8a12f5d1"

(not my actual API key!)

You might wish to edit the notebook or the package itself so that $ChemSpiderAPIToken is permanently assigned to your API key.

I'll need to think of an easier way to let people enter their API keys.


In order to use MoleculeViewer using Mathematica on macOS: which installation procedure will work with it for Open Babel?

  • Macports?
  • Conda (conda install open babel)?
  • compile the source code (which is always a hassle).?
POSTED BY: Murray Eisenberg
Posted 7 years ago

I don't have a Mac, but it seems Conda or HomeBrew are recommended here. Alternatively, there is a link to an installer here. Let me know if you can get it to work!


I'll try the MacPorts version of MoleculeViewer, as it seems to be the current version, as contrasted with the installer to which you supplied a link.

Homebrew is not a viable option on a Mac where one is already using MacPorts: using both can cause considerable confusion and clashes between different versions of libraries.

POSTED BY: Murray Eisenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract