Dear PyMOLers,

Nat Echols <n...@mail.csb.yale.edu> [2002-05-23 10:25] wrote:
> > 
> > Can PyMOL do coloring by B-factors?  I didn't see anything 
> > about this in
> > the manual.  If not, I'll go ahead and write a new function, unless
> > someone else is working on this.
> > 
* DeLano, Warren <war...@sunesis.com> [2002-05-23 13:45] replied:
> This can only be done over discrete ranges, as follows:
> 
> color br0,b>5
> color br1,b>10
> color br2,b>15
> color br3,b>20
> color br4,b>25
> color br5,b>30
> color br6,b>35
> color br7,b>40
> color br8,b>45
> color br9,b>50

Or you could do it with the following python script (which I called
color_b.py). Read this file into pymol using "run color_b.py" and then
you can color your selection by doing:

  color_hist_b("prot and not resn HOH")
  color_ramp_b("all")

or

  color_hist_b (prot and not resn HOH)
  color_ramp_b all

The color_hist_b function colours the model in bins of equal ranges of
B-values (so outliers would tend to have a colour all to themselves), 
while color_ramp_b colours the model in bins of equal numbers
of atoms in each colour, so the model is coloured in a more even
gradation of all the colours.

If you prefer the "rainbow" type of colour ramp (blue, cyan, green,
yellow, orange, red) then one could easily modify this script to use
those colours instead of br0 through br9 and by changing the number of
bins from 10 to 6.  That's the way I originally wrote this, until I
reread what Warren had written above.

##############################################################################
from pymol.cgo import *
from pymol import cmd

def color_hist_b(model):
    """color model (which could be a selection) in bins of equal range of
    B-factor values
    """
    m = cmd.get_model(model)
    b_list = []

    if len(m.atom) == 0:
        print "No atoms selected"

    else:
        for i in range(len(m.atom)):
            b_list.append(m.atom[i].b)

        max_b = max(b_list)
        min_b = min(b_list)
        bin_width = (max_b - min_b)/10.0

        # subtract 1 from the lowest B in order to ensure that the single
        # atom with the lowest B value doesn't get omitted

        sel0 = model + " and b > " + str(min_b - 1.0)
        sel1 = model + " and b > " + str(min_b + bin_width)
        sel2 = model + " and b > " + str(min_b + 2*bin_width)
        sel3 = model + " and b > " + str(min_b + 3*bin_width)
        sel4 = model + " and b > " + str(min_b + 4*bin_width)
        sel5 = model + " and b > " + str(min_b + 5*bin_width)
        sel6 = model + " and b > " + str(min_b + 6*bin_width)
        sel7 = model + " and b > " + str(min_b + 7*bin_width)
        sel8 = model + " and b > " + str(min_b + 8*bin_width)
        sel9 = model + " and b > " + str(min_b + 9*bin_width)

        cmd.color("br0",sel0)
        cmd.color("br1",sel1)
        cmd.color("br2",sel2)
        cmd.color("br3",sel3)
        cmd.color("br4",sel4)
        cmd.color("br5",sel5)
        cmd.color("br6",sel6)
        cmd.color("br7",sel7)
        cmd.color("br8",sel8)
        cmd.color("br9",sel9)

def color_ramp_b(model):
    """color model (which could be a selection) in bins of equal numbers of
    B-factor values
    """
    m = cmd.get_model(model)
    b_list = []

    if len(m.atom) == 0:
        print "No atoms selected"

    else:
        for i in range(len(m.atom)):
            b_list.append(m.atom[i].b)

        b_list.sort()
        bin_num = int(len(b_list))/10

        # subtract 1 from the lowest B in order to ensure that the single
        # atom with the lowest B value doesn't get omitted
        sel0 = model + " and b > " + str(b_list[0] - 1.0)
        sel1 = model + " and b > " + str(b_list[bin_num])
        sel2 = model + " and b > " + str(b_list[2*bin_num])
        sel3 = model + " and b > " + str(b_list[3*bin_num])
        sel4 = model + " and b > " + str(b_list[4*bin_num])
        sel5 = model + " and b > " + str(b_list[5*bin_num])
        sel6 = model + " and b > " + str(b_list[5*bin_num])
        sel7 = model + " and b > " + str(b_list[7*bin_num])
        sel8 = model + " and b > " + str(b_list[8*bin_num])
        sel9 = model + " and b > " + str(b_list[9*bin_num])

        cmd.color("br0",sel0)
        cmd.color("br1",sel1)
        cmd.color("br2",sel2)
        cmd.color("br3",sel3)
        cmd.color("br4",sel4)
        cmd.color("br5",sel5)
        cmd.color("br6",sel6)
        cmd.color("br7",sel7)
        cmd.color("br8",sel8)
        cmd.color("br9",sel9)

# allow calling without parentheses: color_hist_b <selection>
cmd.extend("color_hist_b",color_hist_b)
cmd.extend("color_ramp_b",color_ramp_b)

##############################################################################

Please let me know if you find any bugs!

Cheers,
Robert
-- 
Robert L. Campbell, Ph.D.               http://biophysics.med.jhmi.edu/rlc
r...@k2.med.jhmi.edu                                    phone: 410-614-6313
Research Specialist/X-ray Facility Manager
HHMI/Dept. of Biophysics & Biophysical Chem., The Johns Hopkins University
    PGP Fingerprint: 9B49 3D3F A489 05DC B35C  8E33 F238 A8F5 F635 C0E2

Attachment: pgpqJ1VW8DkeI.pgp
Description: PGP signature

Reply via email to