PyMOL: colouring proteins by property

We all love pretty, colourful pictures of proteins. There is quite a variety of programs to produce publication-quality images of proteins, some of the most popular being VMD, PyMOL and Chimera. Each has advantages and disadvantages — for example, VMD is particularly good to deal with molecular dynamics simulations (perhaps that’s why it is called “Visual Molecular Dynamics”?), and Chimera is able to produce breathtaking graphics with very little user input. In my work, however, I tend to peruse PyMOL: a Python interface is incredibly helpful to produce quick analyses.

In this post, I am going to tell you about a quick and dirty hack to colour your protein by a property (maybe the distance to a template, solvent accesibility or the flexibility of a position according to some MD simulation; you name it): by changing the B-factor of the protein with a very simple function:

from pymol import cmd

def hack_bfactor(mol_id, property_dict):
    """Modify the B-factor of a residue to some
    other property for the purposes of plotting."""
    for residue_id, property_value in property_dict.items():
        cmd.select('current_residue', f'{mol_id} AND resi {residue_id}')
        cmd.alter('current_residue', f'b={property_value}')
    cmd.rebuild()

Once you have altered the B-factor values, it is very simple to produce pictures using the spectrum command. Here is an example: human myoglobin, with every residue coloured with the number of binary contacts it takes part on (i.e. how many beta-carbons are there less than 8 angstroms apart from a residue’s own beta-carbon).

Happy plotting!

Author