Using Python in PyMOL

Decades later, we owe Warren DeLano and his commitment to open source a great debt. Warren wrote PyMOL, an amazingly powerful and popular molecular visualization tool, but it has many hidden talents.

Perhaps its greatest strength is the use of the open source language, Python, as its control language.

Before we go any further, you might need to install PyMOL first. The best way to do this is to install the open source version of PyMOL—not the “Education” version from Schrödinger. The primary reason is the ability to install and use plugins is absent in the “incentive” version that Schrödinger distributes. Check out the wonderful PyMOL Wiki for instructions on how to install PyMOL in Linux, macOS, and Windows.

It’s not unusual to have dozens of molecular objects in a PyMOL session. To do something to all or a subset of them, we need a Python list of all the objects we want to work on. To do this, we can use the ‘cmd.get_object_list()’ method:

from pymol import cmd
allobjs = cmd.get_object_list('all')

While much functionality in PyMOL is accessible using the GUI, we can also access these and many more commands from the ‘pymol’ Python package, in particular, ‘pymol.cmd’.

Once we have a list of all the objects, we can do things like print the names of the objects, disable (effectively turn them off), or align them onto a reference object.

Example 1: Print the names of all the selected objects

from pymol import cmd

seleobjs = cmd.get_object_list('(sele)')
for o in seleobjs: \
    print o

Example 2: Turn off all the selected objects

from pymol import cmd

seleobjs = cmd.get_object_list('(sele)')
for o in seleobjs: \
    cmd.disable(o)

Example 3: Align multiple selected molecules to a reference molecule

Let’s say we have an object called ‘TARGET’ that we want to align all the selected objects onto. We can use the align method (see the “align” page on PyMOL Wiki for more information).

from pymol import cmd

seleobjs = cmd.get_object_list('(sele)')
for o in seleobjs:
    cmd.align(o, 'TARGET')

There are better algorithms for aligning proteins, though. PyMOL comes with ‘super‘ and ‘cealign‘. It is also possible to install and use TMalign in PyMOL, too; the corresponding TMscore will be reported in the PyMOL message window.

Author