My software notes

March 14, 2007

color by b-factor values

Filed under: pymol/ molmol — kpwu @ 6:51 pm

Usually, people have to draw the protein structure according to folding, titration or relaxation experiments; especially for NMR people. PyMOL supports this function by using less “used” b-factor column of the PDB file. Robert Campbell has a python script on his PyMOL homepage that you can use.

However, I am showing another way to color the structure without using any scripts. I found it from PyMOLWIKI (

The steps are copied to this blog and the example I made is also showing here. I use 1XQ8.pdb to show it’s hydrophobicity (by Kyte-Doolittle’s method).

The updated B-factors can be saved by saving a new molecule. 
The hydrophobicities were saved for CA b-factors, here is the example:
cat 1XQ8.pdb |grep CA
ATOM      2  CA  MET A   1     250.631  34.877   0.809  1.00  0.67           C
ATOM     21  CA  ASP A   2     246.860  35.394   1.143  1.00  0.02           C
ATOM     33  CA  VAL A   3     246.085  36.822  -2.313  1.00  0.61           C
ATOM     49  CA  PHE A   4     243.374  38.718  -0.399  1.00 -0.29           C
ATOM     69  CA  MET A   5     245.745  40.728   1.827  1.00 -0.99           C
ATOM     86  CA  LYS A   6     247.572  41.993  -1.282  1.00 -1.24           C
ATOM    108  CA  GLY A   7     244.331  43.152  -2.944  1.00 -0.34           C
ATOM    115  CA  LEU A   8     243.551  44.864   0.384  1.00  0.17           C
ATOM    134  CA  SER A   9     246.922  46.620   0.797  1.00 -0.06           C
ATOM    145  CA  LYS A  10     246.614  47.601  -2.883  1.00  0.23           C
ATOM    167  CA  ALA A  11     243.302  49.441  -2.385  1.00  0.87           C
ATOM    177  CA  LYS A  12     244.578  51.340   0.678  1.00  0.28           C
# load the protein

# open the file of new values (just 1 column of numbers, one for each alpha carbon)
inFile = open("newBFactors", 'r')

# create the global, stored array
stored.newB = []

# read the new B factors from file
for line in inFile.readlines(): stored.newB.append( float(line) )

# close the input file

# clear out the old B Factors
alter protA, b=0.0

# update the B Factors with new properties
alter protA and n. CA, b=stored.newB.pop(0)

# color the protein based on the new B Factors of the alpha carbons
cmd.spectrum("b", "protA and n. CA")


  1. I like the simplicity of this method, but I would still do the above as a script rather than having to type out those lines each time. You also have to be sure that the file with the new B-factor data is stored in the same order and with the same number of elements as you have CA atoms. Also your data file has just a single column of numbers with no other identifying information. I often work with the same sort of data, but need to apply it to different (homologous) structures. In that case, I find it useful if the file contains residue names and numbers to that I can be sure I know which structure it applies to. If you ever wish to apply data to just a portion of the structure (I’m not sure off hand why that might happen!), then my script can be helpful. You then have to create a file that contains the residue names and numbers (and optionally chain ID) as well as the value to apply to the B (or Q columns). The file can also contain comments (ines beginning with ‘#’). My script also has more colouring options than does the built-in spectrum command. You can partition the colours like a histogram with evenly spaced values of B, or you can partition them as a ramp with even numbers of atoms in each colour. You can alter the saturation and brightness (value) of the colours and I think there are more gradient colouring options.

    Just my opinions, of course! Cheers, Rob

    Comment by Robert Campbell — May 6, 2008 @ 5:26 am | Reply

  2. Hi Dr. Campbell,
    Many thanks for the clear explanation of your home-made python scripts for b-factor coloring. The more scripts we can use for inserting/changing b-factor values, the easier way we can use PyMol to represent the data. Thank you for sharing the script.


    Comment by kpwu — May 6, 2008 @ 12:31 pm | Reply

  3. Hi fellows ..

    I have values for CG and CD1, I want to give gradient color red to CG abd CD1, but want to keep the protein in gray90. please advice any script for me..

    Comment by anwar — November 3, 2014 @ 8:51 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: