Molecular Editing Using Python

Nearly all data in a Chimera session may be accessed using the Python interface. In particular, molecular data is arranged as instances of Atom, Bond, Residue and Molecule classes in the chimera module. Instance attributes may be modified and the changes are automatically reflected in the main graphical window.

The code below illustrates how to show protein backbone while hiding all other atoms and bonds. The graphical window renders atoms (and associated bonds) whose display attribute is set to true. Thus, all that is needed to show or hide atoms (and bonds) is to set the display attribute to true or false, respectively.

Example MolecularEditing.py

Import system modules used in this example.
import re

Import Chimera modules used in this example.
import chimera

Define a regular expression for matching the names of protein backbone atoms (we do not include the carbonyl oxygens because they tend to clutter up the graphics display without adding much information).
MAINCHAIN = re.compile("^(N|CA|C)$", re.I)

Do the actual work of setting the display status of atoms and bonds. The following for statement iterates over molecules. The function call chimera.openModels.list(modelTypes=[chimera.Molecule]) returns a list of all open molecules; non-molecular models such as surfaces and graphics objects will not appear in the list. The loop variable m refers to each model successively.
for m in chimera.openModels.list(modelTypes=[chimera.Molecule]):

The following for statement iterates over atoms. The attribute reference m.atoms returns a list of all atoms in model m, in no particular order. The loop variable a refers to each atom successively.
for a in m.atoms:

Set the display status of atom a. First, we match the atom name, a.name, against the backbone atom name regular expression, MAINCHAIN. The function call MAINCHAIN.match(a.name) returns an re.Match object if the atom name matches the regular expression or None otherwise. The display status of the atom is set to true if there is a match (return value is not None) and false otherwise.
a.display = MAINCHAIN.match(a.name) != None

By default, bonds are displayed if and only if both endpoint atoms are displayed, so therefore we don't have to explicitly set bond display modes; they will automatically "work right".

Code Notes

The code indiscriminately hides atoms whose names do not match protein backbone atom names, so any non-protein molecules will be completely hidden.

Running the Example

You can execute the example code by downloading the linked Python script and opening it with the File→Open menu item or with the open command. Note that the .py extension is required for the open dialog/command to recognize that the file is a Python script.

You could also execute the example code by typing it in, line by line, into the main window of the Python Interactive DeveLopment Environment extension (IDLE, for short). To display the IDLE window, activate the Tools menu and roll over the General Controls submenu to select IDLE. Alternatively, the example code may be saved in a disk file, e.g., ~/Desktop/backbone.py (.py suffix still required) and executed from Chimera's Python command line by typing:

execfile("~/Desktop/backbone.py")

Note that the code in backbone.py could also have been executed via the import statement (e.g. import backbone), but only if the directory containing backbone.py is on your Python path. Also, since modules are only imported once, the code could not have been executed again if desired. Using execfile allows multiple executions.