Skip to content

Commit

Permalink
Moved inchi_to_svg to cameo.visualization.
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomas1234 committed Jan 13, 2015
1 parent 2852c6a commit f81112a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cameo/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,35 @@ def draw_knockout_result(model, map_name, simulation_method, knockouts, *args, *
except Exception as e:
tm.reset()
raise e

def inchi_to_svg(inchi, file=None):
"""Generate an SVG drawing from an InChI string.
Parameters
----------
inchi : str
An InChI string.
Returns
-------
str
A vector graphics of the compound represented as SVG.
Examples
--------
Draw water
>>> inchi_to_svg('InChI=1S/H2O/h1H2')
'<?xml version="1.0"?>\n<svg version="1.1" id="topsvg"\nxmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"\nxmlns:cml="http://www.xml-cml.org/schema" x="0" y="0" width="200px" height="200px" viewBox="0 0 100 100">\n<title>OBDepict</title>\n<rect x="0" y="0" width="100" height="100" fill="white"/>\n<text text-anchor="middle" font-size="6" fill ="black" font-family="sans-serif"\nx="50" y="98" ></text>\n<g transform="translate(0,0)">\n<svg width="100" height="100" x="0" y="0" viewBox="0 0 80 80"\nfont-family="sans-serif" stroke="rgb(0,0,0)" stroke-width="2" stroke-linecap="round">\n<text x="36" y="48" fill="rgb(255,12,12)" stroke="rgb(255,12,12)" stroke-width="1" font-size="16" >OH</text>\n<text x="60" y="51.68" fill="rgb(255,12,12)" stroke="rgb(255,12,12)" stroke-width="1" font-size="13" >2</text>\n</svg>\n</g>\n</svg>\n\n'
"""
try:
import openbabel
except ImportError, e:
print e
raise ImportError("OpenBabel seems to be not installed.")
convert = openbabel.OBConversion()
convert.SetInFormat("inchi")
convert.SetOutFormat("svg")
mol = openbabel.OBMol()
if not convert.ReadString(mol, inchi):
raise Exception("%s could not be parsed as an inchi string.")
return convert.WriteString(mol)

0 comments on commit f81112a

Please sign in to comment.