Difference between revisions of "Color"

From PyMOLWiki
Jump to: navigation, search
(Expanded some examples included the new B Factor coloring mini-script.)
(arguments)
 
(30 intermediate revisions by 12 users not shown)
Line 1: Line 1:
===DESCRIPTION===
+
'''color''' sets the color of an object or an atom selection to a predefined, named color, an RGB hex color, or a color [[ramp_new|ramp]].
'''color''' changes the color of an object or an atom selection.
+
For an overview of predefined colors, see [[Color Values]].
 +
For a script that enumerates all the colors see, [[List_Colors]].
 +
If you want to define your own colors, see [[Set_Color]].
  
===USAGE===
+
== Usage ==
color color-name
 
color color-name, object-name
 
color color-name, (selection)
 
  
===PYMOL API===
 
<source lang="python">cmd.color( string color, string selection )</source>
 
 
==Using RGB for Color==
 
If you prefer RGB to color any object
 
 
<source lang="python">
 
<source lang="python">
set_color newcolor, [r,g,b]
+
color color-name
color newcolor
+
color color-name, object-name
 +
color color-name, (selection)
 
</source>
 
</source>
List of [[Color Values]]
 
  
==EXAMPLES==
+
== Arguments ==
 +
 
 +
* color-name = str or int: [[Color Values|named color]], index of a named color, <code>0xRRGGBB</code> string, <code>0x40RRGGBB</code> integer, name of a [[ramp_new|ramp]] object, or special value <code>atomic</code>
 +
* object-name = str: Name of an object or name pattern (with Asterisk (<code>*</code>)) which matches multiple objects. Changes the [[set_object_color|object color]], and if it is a molecular object, also the color of all atoms. {default: all}
 +
* selection = str: [[Selection Algebra|atom selection]], this is any expression which is not a valid object pattern. E.g. an expression which starts with a parentheses. This will only color atoms, not objects.
 +
 
 +
== Examples ==
 +
 
 
===Color all carbons yellow===
 
===Color all carbons yellow===
 
<source lang="python">color yellow, (name C*)</source>
 
<source lang="python">color yellow, (name C*)</source>
  
===RGB Example===
+
=== Color by element, except carbons ===
 +
 
 +
color atomic, (not elem C)
 +
 
 +
=== Use a custom RGB color ===
 +
 
 +
color 0x006600
 +
 
 +
===Color by Spectrum Example===
 +
Color by spectrum is in the GUI menu but did you realize that the spectrum is not limited to a simple rainbow?
 
<source lang="python">
 
<source lang="python">
set_color khaki, [195,176,145]
+
spectrum count, palette, object_name
color khaki
 
 
</source>
 
</source>
 +
 +
For available palettes and more details see: [[spectrum]]
  
 
===B-Factors===
 
===B-Factors===
The command to color your molecule by B-Factors (B Factors) is:
+
The command to color a molecule by B-Factors (B Factors) is:
 
<source lang="python">
 
<source lang="python">
cmd.spectrum("b", selection="SEL");
+
spectrum b, selection=SEL
 
</source>
 
</source>
where '''SEL''' is your selection, for example, "protA and n. CA", for protein A's alpha carbons.
+
where '''SEL''' is a valid selection, for example, "protA and n. CA", for protein A's alpha carbons.
 +
 
 +
For more details see: [[spectrum]]
  
 
====Reassigning B-Factors and Coloring====
 
====Reassigning B-Factors and Coloring====
Line 63: Line 76:
 
</source>
 
</source>
  
 +
If you want to save the file with the new B Factor values for each alpha carbon,
 +
<source lang="python">
 +
cmd.save("protA_newBFactors.pdb", "protA")
 +
</source>
 +
or similar is all you need.
 +
 +
A script (data2bfactor.py) that loads data into the B-factor (b) or occupancy (q) columns from an external file can be found in Robert Campbell's PyMOL script repository (http://pldserver1.biochem.queensu.ca/~rlc/work/pymol/)
 +
 +
====Reassigning B-Factors and Coloring - from file ====
 +
<source lang="python">
 +
reinitialize
 +
prot="1XYZ"
 +
cmd.fetch(prot,async=0)
 +
 +
# Set b value to zero
 +
cmd.alter(prot,b=0.0)
 +
cmd.show_as("cartoon",prot)
 +
 +
python
 +
inFile = open("phi_values.txt", 'r')
 +
val_list = []
 +
for line in inFile.readlines()[1:]:
 +
    split = line.split()
 +
    resn = split[0][0]
 +
    resi = split[0][1:-1]
 +
    phi_ppm2 = float(split[1])
 +
    phi_ppm2_err = float(split[3])
 +
    R2_0 = float(split[4])
 +
    R2_0_err = float(split[6])
 +
    print "Resn=%s Resi=%s, phi_ppm2=%2.2f, phi_ppm2_err=%2.2f, R2_0=%2.2f, R2_0_err=%2.2f"%(resn,resi,phi_ppm2,phi_ppm2_err,R2_0,R2_0_err)
 +
 +
    val_list.append(phi_ppm2)
 +
    cmd.alter("%s and resi %s and n. CA"%(prot,resi), "b=%s"%phi_ppm2)
 +
 +
python end
 +
minval = min(val_list)
 +
print minval
 +
maxval = max(val_list)
 +
print maxval
 +
cmd.spectrum("b", "blue_white_red", "%s and n. CA"%prot, minimum=0, maximum=maxval)
 +
cmd.ramp_new("ramp_obj", prot, range=[0, minval, maxval], color="[blue, white, red ]")
 +
cmd.save("%s_newBFactors.pdb"%prot, "%s"%prot)
 +
</source>
 +
 +
=== Expanding to Surface ===
 +
See [[Expand_To_Surface]].
 +
 +
If you have run the above code and would like the colors to be shown in the [[Surface]] representation, too, then you need to do the following:
 +
<source lang="python">
 +
# Assumes alpha carbons colored from above.
 +
create ca_obj, your-object-name and name ca
 +
ramp_new ramp_obj, ca_obj, [0, 10], [-1, -1, 0]
 +
set surface_color, ramp_obj, your-object-name
 +
</source>
 +
 +
Thanks to Warren, for this one.
 +
 +
=== Getting Atom Colors ===
 +
<source lang="python">
 +
stored.list = []
 +
iterate all, stored.list.append(color) # use cmd.get_color_tuple(color) to convert color index to RGB values
 +
print stored.list
 +
</source>
 +
 +
Or, you can [[label]] each atom by it's color code:
 +
<source lang="python">
 +
label all, color
 +
</source>
 +
 +
=== What colors does PyMOL currently have? ===
 +
basic colors can be manually accessed and edited from the PyMOL menu under '''Setting''' --> '''Colors...'''<br>
 +
At the Pymol prompt, you can use the command [[Get Color Indices]] to get a list of Pymols named colors.<br>
 +
[[Get_colors]] is a script that allows accessing colors as well.<br>
 +
 +
=== Color States Individually ===
 +
<source lang="python">
 +
fetch 1nmr
 +
set all_states
 +
 +
# the object has 20 states, so we can set separate line colors
 +
# for each state as follows:
 +
for a in range(1,21): cmd.set("line_color","auto","1nmr",a)
 +
</source>
 +
Or, we can do it differently,
 +
<source lang="python">
 +
# start over,
 +
fetch 1nmr
 +
 +
# break apart the object by state
 +
split_states 1nmr
 +
 +
# delete the original
 +
dele 1nmr
 +
 +
# and color by object (carbons only)
 +
util.color_objs("elem c")
 +
 +
# (all atoms)
 +
util.color_objs("all")
 +
</source>
 +
 +
== See Also ==
  
 +
* [[Advanced Coloring]]
 +
* [[Get Color Indices]]
 +
* [[spectrum]]
 +
* [[Ramp_New]]
  
[[Category:Objects_and_Selections]]
+
[[Category:Objects_and_Selections|Color]]
[[Category:Commands|color]]
+
[[Category:Commands|Color]]
 +
[[Category:States|Coloring states individually]]
 +
[[Category:Coloring|Color]]

Latest revision as of 11:21, 24 October 2019

color sets the color of an object or an atom selection to a predefined, named color, an RGB hex color, or a color ramp. For an overview of predefined colors, see Color Values. For a script that enumerates all the colors see, List_Colors. If you want to define your own colors, see Set_Color.

Usage

color color-name
color color-name, object-name
color color-name, (selection)

Arguments

  • color-name = str or int: named color, index of a named color, 0xRRGGBB string, 0x40RRGGBB integer, name of a ramp object, or special value atomic
  • object-name = str: Name of an object or name pattern (with Asterisk (*)) which matches multiple objects. Changes the object color, and if it is a molecular object, also the color of all atoms. {default: all}
  • selection = str: atom selection, this is any expression which is not a valid object pattern. E.g. an expression which starts with a parentheses. This will only color atoms, not objects.

Examples

Color all carbons yellow

color yellow, (name C*)

Color by element, except carbons

color atomic, (not elem C)

Use a custom RGB color

color 0x006600

Color by Spectrum Example

Color by spectrum is in the GUI menu but did you realize that the spectrum is not limited to a simple rainbow?

spectrum count, palette, object_name

For available palettes and more details see: spectrum

B-Factors

The command to color a molecule by B-Factors (B Factors) is:

spectrum b, selection=SEL

where SEL is a valid selection, for example, "protA and n. CA", for protein A's alpha carbons.

For more details see: spectrum

Reassigning B-Factors and Coloring

It is commonplace to replace the B-Factor column of a protein with some other biochemical property at that residue, observed from some calculation or experiment. PyMOL can easily reassign the B-Factors and color them, too. The following example will load a protein, set ALL it's B Factors to "0", read in a list of properties for each alpha carbon in the proteins, assign those new values as the B-Factor values and color by the new values. This example is possible because commands PyMOL does not recognize are passed to the Python interpreter --- a very powerful tool.

# load the protein
cmd.load("protA.pdb")

# 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
inFile.close()

# 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")

If you want to save the file with the new B Factor values for each alpha carbon,

cmd.save("protA_newBFactors.pdb", "protA")

or similar is all you need.

A script (data2bfactor.py) that loads data into the B-factor (b) or occupancy (q) columns from an external file can be found in Robert Campbell's PyMOL script repository (http://pldserver1.biochem.queensu.ca/~rlc/work/pymol/)

Reassigning B-Factors and Coloring - from file

reinitialize
prot="1XYZ"
cmd.fetch(prot,async=0)

# Set b value to zero
cmd.alter(prot,b=0.0)
cmd.show_as("cartoon",prot)

python
inFile = open("phi_values.txt", 'r')
val_list = []
for line in inFile.readlines()[1:]:
    split = line.split()
    resn = split[0][0] 
    resi = split[0][1:-1]
    phi_ppm2 = float(split[1])
    phi_ppm2_err = float(split[3])
    R2_0 = float(split[4])
    R2_0_err = float(split[6])
    print "Resn=%s Resi=%s, phi_ppm2=%2.2f, phi_ppm2_err=%2.2f, R2_0=%2.2f, R2_0_err=%2.2f"%(resn,resi,phi_ppm2,phi_ppm2_err,R2_0,R2_0_err)

    val_list.append(phi_ppm2)
    cmd.alter("%s and resi %s and n. CA"%(prot,resi), "b=%s"%phi_ppm2)

python end
minval = min(val_list)
print minval
maxval = max(val_list)
print maxval
cmd.spectrum("b", "blue_white_red", "%s and n. CA"%prot, minimum=0, maximum=maxval)
cmd.ramp_new("ramp_obj", prot, range=[0, minval, maxval], color="[blue, white, red ]")
cmd.save("%s_newBFactors.pdb"%prot, "%s"%prot)

Expanding to Surface

See Expand_To_Surface.

If you have run the above code and would like the colors to be shown in the Surface representation, too, then you need to do the following:

# Assumes alpha carbons colored from above.
create ca_obj, your-object-name and name ca 
ramp_new ramp_obj, ca_obj, [0, 10], [-1, -1, 0]
set surface_color, ramp_obj, your-object-name

Thanks to Warren, for this one.

Getting Atom Colors

stored.list = []
iterate all, stored.list.append(color) # use cmd.get_color_tuple(color) to convert color index to RGB values
print stored.list

Or, you can label each atom by it's color code:

label all, color

What colors does PyMOL currently have?

basic colors can be manually accessed and edited from the PyMOL menu under Setting --> Colors...
At the Pymol prompt, you can use the command Get Color Indices to get a list of Pymols named colors.
Get_colors is a script that allows accessing colors as well.

Color States Individually

fetch 1nmr
set all_states

# the object has 20 states, so we can set separate line colors
# for each state as follows:
for a in range(1,21): cmd.set("line_color","auto","1nmr",a)

Or, we can do it differently,

# start over,
fetch 1nmr

# break apart the object by state
split_states 1nmr

# delete the original
dele 1nmr

# and color by object (carbons only)
util.color_objs("elem c")

# (all atoms)
util.color_objs("all")

See Also