14
$\begingroup$

For a web-based project, I am looking for a piece of software to create a 2D vector graphics structural formula from a textual structure encoding scheme such as SMILES or InChI, running on the server or client, and callable via some kind of API.

I’m imagining a client- or server-side library that you could just use like this:

svg_output = lib.render_from_smiles(smiles_input);

Ideally, it would have the following properties:

  • Take a SMILES or InChI representation of the compound as input
  • Compute the structure graph and 2D positional data (positions of atoms, types of bonds, etc.)
  • Render the structure in some kind of vector graphics representation (SVG would be perfect, but any other open format would do)
  • Or, alternatively, just output the computed drawing parameters, to be passed on to a separate renderer (which would need to be written, but that is the less challenging part)
  • Written in a language that is easy to use and deploy in web environments (PHP, JavaScript, Python, Ruby, Go, etc.)
  • If a renderer is included, allow me to control the rendering style (stroke width/colours, text attributes etc.)
  • Open source or with license terms that are affordable to a non-profit project :)

The application just needs to render individual structures. No reaction mechanisms or anything fancy. Small organic molecules only.

I know there are various standalone software packages (ChemDraw, MarvinSketch etc.) that do this kind of thing, but I did not find anything that can be used as a library like this.

Any suggestions or ideas? Thank you!

$\endgroup$
  • 1
    $\begingroup$ If you can't find anything you like, I would like to know about it; I'm a coder and a chemist, I can definitely set this up if it isn't out there. $\endgroup$ – sqykly Oct 31 '16 at 15:02
  • 1
    $\begingroup$ @sqykly - I think you're far better contributing to RDKit or Open Babel. Setting this up requires lots of twisty passages, all alike. $\endgroup$ – Geoff Hutchison Nov 1 '16 at 3:54
12
$\begingroup$

You might want to have a look at Open Babel.

  • It is licensed under GNU GPLv2
  • It has bindings for Python
  • It has bindings for PHP
  • It has bindings for Ruby
  • It can read (and write) SMILES
  • It can read (and write) InChI
  • It can write SVG
  • It can write a list of painter commands
$\endgroup$
  • 3
    $\begingroup$ smiles shortcut: obabel -:"O=C(O)c1ccccc1OC(=O)C" -osvg -O asp.svg src Of course OB is not without bugs and may fail for 3D type structures, but that shouldn't be a problem for this simple conversion $\endgroup$ – Martin - マーチン Oct 31 '16 at 11:47
  • $\begingroup$ @Martin Yes, the shortcut is a neat feature in the shell. OTOH, even ghosts are cool in the shell ;) $\endgroup$ – Klaus-Dieter Warzecha Oct 31 '16 at 11:50
  • $\begingroup$ @Martin-マーチン- "may fail for 3D type structures"? Any particular examples? $\endgroup$ – Geoff Hutchison Nov 1 '16 at 3:50
8
$\begingroup$

You should also look at RDKit, which is at its core is C++ code for manipulating molecular structures, but which also has Python and Java bindings. Most people use it via its Python bindings.

Here's some example code.

# import rdkit components
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
IPythonConsole.ipython_useSVG=True

# displaying an SVG structure (in a Jupyter notebook)
mol = Chem.MolFromSmiles('O=C(O)c1ccccc1OC(=O)C')
mol

Inside of a Jupyter notebook, that results in a nice SVG-formated image. enter image description here. You can get a file of this image using the standard way to get SVG out of Jupyter notebooks, which I can't remember right now.

# 2D positional data for atoms
for atom in mol.GetAtoms():
    print (atom.GetIdx(), atom.GetSymbol(), atom.GetExplicitValence(), atom.GetIsAromatic())

# 2D positional data for bonds
for bond in mol.GetBonds():
    print (bond.GetIdx(), bond.GetBeginAtomIdx(), bond.GetEndAtomIdx(), bond.GetBondType())

The code above gives you just a sense of the flavor of how things are done, and just a few examples of properties available for bonds and atoms. It certainly isn't an exhaustive list of bond/atom properties. The output is:

(0, 'O', 2, False)
(1, 'C', 4, False)
(2, 'O', 1, False)
(3, 'C', 4, True)
(4, 'C', 3, True)
(5, 'C', 3, True)
(6, 'C', 3, True)
(7, 'C', 3, True)
(8, 'C', 4, True)
(9, 'O', 2, False)
(10, 'C', 4, False)
(11, 'O', 2, False)
(12, 'C', 1, False)
(0, 0, 1, rdkit.Chem.rdchem.BondType.DOUBLE)
(1, 1, 2, rdkit.Chem.rdchem.BondType.SINGLE)
(2, 1, 3, rdkit.Chem.rdchem.BondType.SINGLE)
(3, 3, 4, rdkit.Chem.rdchem.BondType.AROMATIC)
(4, 4, 5, rdkit.Chem.rdchem.BondType.AROMATIC)
(5, 5, 6, rdkit.Chem.rdchem.BondType.AROMATIC)
(6, 6, 7, rdkit.Chem.rdchem.BondType.AROMATIC)
(7, 7, 8, rdkit.Chem.rdchem.BondType.AROMATIC)
(8, 8, 9, rdkit.Chem.rdchem.BondType.SINGLE)
(9, 9, 10, rdkit.Chem.rdchem.BondType.SINGLE)
(10, 10, 11, rdkit.Chem.rdchem.BondType.DOUBLE)
(11, 10, 12, rdkit.Chem.rdchem.BondType.SINGLE)
(12, 8, 3, rdkit.Chem.rdchem.BondType.AROMATIC)

Converting between/from/to InChI and SMILES is easy. Here's an example of InChI output.

    # output InChI
    Chem.MolToInchi(mol)

'InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)'

You can learn a bit more about how to do common tasks in RDKit via this GitHub repo of example Jupyter Notebooks.

$\endgroup$
2
$\begingroup$

openchemlib is a library that can convert a smiles into a SVG. It can be used on the the server (Java) or on the client side (JavaScript).

https://github.com/Actelion/openchemlib

https://github.com/cheminfo/openchemlib-js

$\endgroup$
  • $\begingroup$ Again, a great solution that I will have a look at. Highly appreciated! $\endgroup$ – Niels Heidenreich Dec 4 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.