Looping Through Data Files and Running Chimera Commands on Them

...and not much else


This primer covers the scenario where you have a set of data files in a folder and want to perform a series of Chimera commands on each one in turn, writing out some kind of results file (e.g. image; structure measurements; modified data file) for each. Though this primer provides all the Python code needed to carry out the scenario tasks, it would still be beneficial (though not strictly necessary) for you to read the Python Tutorial (at least the first few sections) in order to understand the code better and to be able to modify it if needed in ways this primer doesn't cover.

Broad Outline

You will take the code below, modified for your needs, and place it in a file ending with a '.py' suffix. The '.py' suffix will indicate to Chimera that the file should be interpreted as Python code. You could then run the Python code by opening the file with Chimera's File→Open dialog or with the open command. If you want to suppress the Chimera interface from appearing during your script processing, you can start Chimera using the --nogui option (e.g. chimera --nogui processData.py). Note that if your script creates images then you must start the Chimera interface unless you've downloaded the "headless" version of Chimera (see the download page).

An important fact to know is that any Chimera command can be executed in Python using the runCommand() call. For instance, to color all models red and surface them:

from chimera import runCommand
runCommand("color red")

This makes it simple to perform actions in your Python script as long as you know the equivalent Chimera command.

Scripting Approach

The general scheme used in the script will be to enter the folder containing your data files, gather the names of the files, and then loop through them one by one, performing a series of commands on each. In the example below the data files are PDB files (suffix: .pdb), each of which has a ligand and a receptor. The script focuses on the ligand, attempts to ensure that the receptor isn't obscuring the ligand, surfaces the receptor, and saves an image.

The Script

There are a lot of comments in the script describing the code. Python comments are introduced by the # character (as long as it's not inside a quoted string of course).

import os
from chimera import runCommand as rc # use 'rc' as shorthand for runCommand
from chimera import replyobj # for emitting status messages

# change to folder with data files

# gather the names of .pdb files in the folder
file_names = [fn for fn in os.listdir(".") if fn.endswith(".pdb")]

# loop through the files, opening, processing, and closing each in turn
for fn in file_names:
	replyobj.status("Processing " + fn) # show what file we're working on
	rc("open " + fn)
	rc("align ligand ~ligand") # put ligand in front of remainder of molecule
	rc("focus ligand") # center/zoom ligand
	rc("surf") # surface receptor
	rc("preset apply publication 1") # make everything look nice
	rc("surftransp 15") # make the surface a little bit see-through
	# save image to a file that ends in .png rather than .pdb
	png_name = fn[:-3] + "png"
	rc("copy file " + png_name + " supersample 3")
	rc("close all")
# uncommenting the line below will cause Chimera to exit when the script is done
#rc("stop now")
# note that indentation is significant in Python; the fact that
# the above command is exdented means that it is executed after
# the loop completes, whereas the indented commands that 
# preceded it are executed as part of the loop.

Actual Script File

Here is a link to an actual file containing the script so that you can download it and use it as a starting point for your own script — and save yourself some typing.


Frequently execution of your script will put information in the Reply Log that you want to save. If you are running with the "--nogui" option, then log output will simpy go to the shell that you run Chimera from, which you can then redirect to a file. If you are running with the graphical user interface, then adding these lines to your script will save the contents of the Reply Log into a file named "reply-log.txt" in your home folder:

from chimera.tkgui import saveReplyLog