Spectrumbar

From PyMOLWiki
Revision as of 12:46, 3 September 2009 by Slaw (talk | contribs)
Jump to navigation Jump to search

Description

Often times one may color their structure based upon some predefined color spectrum. However, this alone is not helpful when dealing with publication images. The purpose of this script is to allow the user to draw their own custom spectrum bar to accompany their structural images.

Further work will be done to improve the functionality of this script. Please feel free to contact the author for function requests.

USAGE

load the script using the run command

spectrumbar (RGB_Colors,radius=1.0,name=spectrumbar,head=(0.0,0.0,0.0),tail=(10.0,0.0,0.0),length=10.0)

Please see the script comments for further custom options. Once the script completes, it will generate a new object called "spectrumbar" (which can be changed through the options) which is a cylinder colored according to the user-specified colors.

Image:sb_default.png

EXAMPLES

Coming soon!

Script

load the script using the run command

from pymol.cgo import *
from math import *
from pymol import cmd
from re import *

def spectrumbar (*args, **kwargs):

    """
    Author Sean Law
    Michigan State University
    slaw_(at)_msu_dot_edu

    USAGE

    While in PyMOL

    run spectrumbar.py

    spectrumbar (RGB_Colors,radius=1.0,name=spectrumbar,head=(0.0,0.0,0.0),tail=(10.0,0.0,0.0),length=10.0)

    Parameter     Preset         Type     Description
    RGB_Colors    [1.0,1.0,1.0]  N/A      RGB colors can be specified as a
                                          triplet RGB value or as PyMOL
                                          internal color name (i.e. red)
    radius        1.0            float    Radius of cylindrical spectrum bar
    name          spectrumbar    string   CGO object name for spectrum bar
    head          (0.0,0.0,0.0)  float    Starting coordinate for spectrum bar
    tail          (10.0,0.0,0.0) float    Ending coordinate for spectrum bar
    length        10.0           float    Length of spectrum bar

    Examples:

    spectrumbar red, green, blue
    spectrumbar 1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0

    The above two examples produce the same spectrumbar!

    spectrumbar radius=5.0
    spectrumbar length=20.0

    """

    rgb=[1.0, 1.0, 1.0]
    name="spectrumbar"
    radius=1.0
    x1=0
    y1=0
    z1=0
    x2=10
    y2=0
    z2=0
    num=re.compile('[0-9]')
    abc=re.compile('[a-z]')

    for key in kwargs:
        if (key == "radius"):
            radius = float(kwargs["radius"])
            print radius
        elif (key == "name"):
            name=kwargs["name"]
        elif (key == "head"):
            head=kwargs["head"]
            head=head.strip('" []()')
            x1,y1,z1=map(float,head.split(','))
        elif (key == "tail"):
            tail=kwargs["tail"]
            tail=tail.strip('" []()')
            x2,y2,z2=map(float,tail.split(','))
        elif (key == "length"):
            if (abc.search(kwargs["length"])):
                print "Error: The length must be a value"
                return
            else:
                x2=float(kwargs["length"]);
        elif (key != "_self"):
            print "Ignoring unknown option \""+key+"\""
        else:
            continue

    args=list(args)
    if (len(args)>=1):
        rgb=[]
    while (len(args)>=1):
        if (num.search(args[0]) and abc.search(args[0])):
            print "Error: RGB value \""+args[0]+"\" must either contain letters OR numbers"
            return
        elif (num.search(args[0])):
            rgb.extend(args.pop(0))
        else:
            rgb.extend(cmd.get_color_tuple(args.pop(0)))
    
    if (len(rgb) % 3):
        print "Error: Missing RGB value"
        print "Please double check RGB values"
        return

    dx=x2-x1
    dy=y2-y1
    dz=z2-z1
    t=1.0/(len(rgb)/3.0-1)
    c=len(rgb)/3-1
    s=0
    bar=[]
    
    while (s < c):
        if (len(rgb) >0):
            r=rgb.pop(0)
            g=rgb.pop(0)
            b=rgb.pop(0)
        bar.extend([CYLINDER])
        bar.extend([x1+(s*t)*dx, y1+(s*t)*dy, z1+(s*t)*dz])
        bar.extend([x1+(s+1)*t*dx, y1+(s+1)*t*dy, z1+(s+1)*t*dz])
        bar.extend([radius, float(r), float(g), float(b)])
        if (len(rgb) >= 3):
            bar.extend([float(rgb[0]), float(rgb[1]), float(rgb[2])])
        else:
            bar.extend([float(r), float(g), float(b)])
        s=s+1

    cmd.delete(name)
    cmd.load_cgo(bar,name)
    

    return
cmd.extend("spectrumbar",spectrumbar)