Skip to main content

Simple visualization of molecules in python

Project description

mogli is a python module for easily rendering molecules.

The fastest way to get started is using in the command line:

% python -m mogli <file_name>

This will open a new window in which the molecule will be rendered. You can then rotate the molecule using your mouse.

In python, you can achieve something similar with three lines of code:

>>> import mogli
>>> molecules ='examples/')
DNA rendered in a GR window

DNA rendered in a GR window

Atomic bonds

You might notice that the atomic bonds between the atoms don’t look right. You can hide the bonds by adding the show_bonds=False to your show() call, but of course there’s a a better way to fix the missing bonds here.

For the example above, adding bond_param=1.15 to show() will do the trick. mogli currently offers two ways of calculating atomic bonds. The first method compares the distance between every two atoms with the sum of their valence radii. If they are further apart, no bond is formed. To allow a bit of adjustment, this radii sum is multiplied with a factor that can be set using the bond_param parameter to show() and draw(). By default, 1.0 is used.

The second method uses a constant maximum distance instead. If you use this method by passing bond_method='constant_delta' to show() or draw(), you can set the constant distance with the bond_param parameter.

Exporting to files

Instead of viewing molecules interactively, you can export the molecule as well, for example as Portable Network Graphics into a .png file, or as HTML5 page with interactive WebGL code as .html file. To do so, simply call the export() function, like so:

>>> import mogli
>>> molecules ='examples/')
>>> mogli.export(molecules[0], 'dna.html', width=1920, height=1080,
                 bonds_param=1.15, camera=((40, 0, 0),
                                           (0, 0, 0),
                                           (0, 1, 0)))

This example code also shows setting the camera in code by passing the camera parameter. It expects three sequences of three floating point numbers: the camera position, the center of focus and a direction that should point upward. Exporting to HTML5 with WebGL is a pretty near feature, give it a try!

Drawing with GR

In case you use the GR framework, you can use mogli to draw molecules into your GR graphics. To do so, just call draw(). You can use the parameters xmin, xmax, ymin, ymax, width and height just like you would when using gr.drawimage().

>>> import gr
>>> import mogli
>>> molecules ='examples/')
>>> mogli.draw(molecules[0])
>>> gr.updatews()
DNA rendered in a GR window

DNA rendered in a GR window


mogli depends on GR3, which is included in the GR framework (PyPI, GitHub), on glfw3 (GitHub) and python bindings for glfw3 (PyPI, GitHub).

Additionally, Pybel should be installed; mogli will work without it, but only for xyz files.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mogli-0.2.0.tar.gz (12.1 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page