PyMol API: Simple Example

PyMol is a USER-SPONSORED molecular visualization system on an OPEN-SOURCE foundation. And, it’s a darn good one as well. It can be accessed by an intuitive GUI (regular mode) or via Python Scripts (Pymol API). It can also run its own scripts (real time savers). My issue was how to run it from a python script and I roamed the web for about an hour until I got all the answers I needed. Thus, here’s the short version :)

First, we need to have it installed on the system, obviously. I recommend *dearly* a self-compilation as it is as simple as it can be. Then, comes the installation of any additional scripts/tools such as those listed here and here. Finally, programming the python script. I’ll just leave an example that is well commented so it should be self-explanatory.

The first lines may be kind of “WTF is that for?” but they are handy. They are to provide a couple of “flags” to pymol so it doesn’t open any graphical window (-c of command-line) nor outputs all the text it usually does (-q of quiet). Then we have to import pymol, just as we import any other module, and then the method finish_launching() is essential, otherwise it will break again and again with some threading error.

My script essentially reads 2 pdb files, loads then, aligns them using cealign, and produces a file where the mobile structure is in the position of the static one. Well, it is useful for me :) You can always refer to PyMolWiki for a comprehensive aid for all the commands you can possibily need. They have not only the “in-pymol” way to do it, but the API access as well. Most of them are ran by something like pymol.cmd.command(args).

In the end, don’t forget to close with pymol.cmd.quit()

Oh, yes, I know I could have done from pymol import cmd, but it gave me some errors.. temperamental laptop.

#!/usr/bin/python
#
#   Usage: superimposition.py STATIC MOBILE
#   Utrecht @ 2009
#   Joao Rodrigues
#

import __main__
__main__.pymol_argv = [ 'pymol', '-qc'] # Quiet and no GUI

import sys, time, os
import pymol

pymol.finish_launching()

##
# Read User Input
staticStructurePath = os.path.abspath(sys.argv[1])
staticStructureName = staticStructurePath.split('/')[-1].split('.')[0]
mobileStructurePath = os.path.abspath(sys.argv[2])
mobileStructureName = mobileStructurePath.split('/')[-1].split('.')[0]

# Load Structures

pymol.cmd.load(mobileStructurePath, mobileStructureName)
pymol.cmd.load(staticStructurePath, staticStructureName)

# CEAlign STATIC, MOBILE
# CEAlign produces same alignment as the complex SUPER below
pymol.cmd.do("run /home/joao/Software/cealign-0.9/cealign.py") # Import Module
pymol.cmd.do("cealign %s, %s" %(staticStructureName, mobileStructureName))
time.sleep(1) # Dunno why, but if I don't wait, structures do not align properly..
# Save Superimposition
# save(file, selection, state (0 default), format)
pymol.cmd.save("%s_%s.pdb" %(mobileStructureName, staticStructureName), mobileStructureName, 0, 'pdb')

## SUPER - old
#pymol.cmd.super((staticStructureName and (resn ZN around 5 and (resn CYS or resn HIS))), (mobileStructureName and (resn ZN around 5 and (resn CYS or resn HIS))))
#pymol.cmd.save("%s_%s_SUPER.pdb" %(mobileStructureName, staticStructureName), mobileStructureName, 0, 'pdb')

# Get out!
pymol.cmd.quit()

6 thoughts on “PyMol API: Simple Example

  1. I have a doubt. I have to execute the command turn x,10. The input 10 comes from a python script. so I tried giving a=10 turn x,a.But it is giving me errors.Can this be done.

  2. Hello Joao,

    Thanks for the reply. I had checked that thread before, and just tried to follow the help again with no success.
    I was afraid running PyMOL using python scripts was just not possible, seeing the answer from Delano on this same thread.

    I am happy to see you succeeded at it. The PyMOL documentation mentions it is possible too, so there has to be a way for me to make it work.

    I am also trying to make it work on Windows with the exact same results, I must be doing something very wrong. I hope I can solve this.

    Again some questions : you compiled your PyMOL on linux : which version of PyMOL did you install? Did you have to install several dependencies yourself, or did you use a package manager (apt-get, yum…)? What is your Python version ?

    Thank you for sharing in this thread and helping out.

    Thomas.

  3. Hello,

    I have been trying to do the same thing and have not succeeded : launch PyMOL through a python script, and perform tasks using the PyMOL API.

    I have a few questions about what you did :
    -How do you launch your script : from the prompt (python yourScript.py) or from within PyMOL (launch PyMOL, run yourScript.py) ?
    -In other words, did you use the python installed on your computer or did you use the python provided with PyMOL ?
    -Did you have to change any of these environment variables : PYMOL_PATH, PYTHONHOME, PYTHONPATH ?

    I have been trying to launch PyMOL from a python script for several days, and I have looked for answers on the web AND in the PyMOL files (pymol/__init__.py, pymol/cmd.py …)

    The error I get is :
    ImportError: No module named _cmd

    Which I think is because the script should use PyMOL’s python, and I doesn’t find _cmd.
    Is _cmd a C compiled module ? (I got this from pymol/cmd.py commentaries)
    I cannot locate _cmd on my computer.

    I have tried playing with the environment variables cited above, and have also tried to move the content of pymol/modules in my computer’s python/…/site-packages.
    I have tried different versions of PyMOL with no success.

    I am using:
    OSX 10.5.8
    Python 2.5.4
    MacPyMOL 1.2r3 (educational release)

    I hope what I explained makes sense.
    Thanks in advance to anyone with some insight !

    Thomas.

  4. Thanks! I searched a while for this. Python was giving this error:

    AttributeError: ‘module’ object has no attribute ‘glutThread’

    The pymol.finish_launching() command was what I was missing.

Leave a comment