IMP logo
display examples

basic geometry

This example shows how to write basic geometric primitives to a file with color and name.

# or IMP.display.ChimeraWriter
# if using chimera, make sure there is a %1% in the name to support multiple frames
name=IMP.create_temporary_file_name("example", ".py")
print "File name is", name
bb= IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0,0,0),
IMP.algebra.Vector3D(100, 100, 100))
# we could skip the outer loop if we only have one frame
for f in range(0,10):
w.set_frame(f)
g.set_name("bb")
w.add_geometry(g)
for i in range(0,10):
g= IMP.display.SphereGeometry(IMP.algebra.Sphere3D(p, 10))
# give each a distinctive color
g.set_name(str(i))
# add it to the file
w.add_geometry(g)

display log

A simple example of how to use one of the IMP.display.LogOptimizerStates.

import IMP
import IMP.core
# write it to a series of files, if the file name did not contain %1%, then
# it would concatenate the outputs into a single file instead. Concatenating the
# output can be quite useful with Pymol as that makes it less likely to crash.
# the logging occurs ever two frames
log.set_period(2)
g.set_name("my particles")
g.set_color(IMP.display.Color(1,0,0))
log.add_geometry(g)
m.add_restraint(r)
mv= IMP.core.BallMover(ps, 10)
o.add_mover(mv)
o.add_optimizer_state(log)
o.optimize(1000)

displaying ensembles

The script shows a couple experiments with trying to visualize an ensembe of structures. The ensemble is fairly tight on the assembly scale, but there is significant variation between the location and orientation of the individual proteins (which were modeled as rigid bodies). To save space, the models have had their sidechain atoms removed.

import IMP.atom
# turn off internal checks to speed things up
IMP.set_check_level(IMP.USAGE)
def read(m, beyond_file):
print "reading"
hs=[]
for i in range(0,beyond_file):
# create a simplified version for each chain to speed up computations
name= IMP.display.get_example_path("ensemble/aligned-"+str(i)+".pdb")
hs.append(hr)
for c in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
hr.add_child(simp)
if i==0:
print " ",i
return hs
def add_markers(h, c, w):
"""Add markers to a the passed conformation. The marker locations are chosen
pretty thoughtlessly and don't really illustrate the technique well."""
def add_marker(s, name):
g= IMP.core.XYZRGeometry(s.get_selected_particles()[0])
g.set_name(name)
g.set_color(c)
w.add_geometry(g)
s= IMP.atom.Selection(h, chain='B', residue_index=317)
add_marker(s, "m0")
s= IMP.atom.Selection(h, chain='G', residue_index=212)
add_marker(s, "m1")
s= IMP.atom.Selection(h, chain='I', residue_index=237)
add_marker(s, "m2")
s= IMP.atom.Selection(h, chain='F', residue_index=101)
add_marker(s, "m3")
def get_nice_name(h):
nm= h.get_name()
return nm[nm.find('-')+1:nm.rfind('.')]
def add_axis(h, c, w, chain_colors):
"""Add a coordinate axis to show the relative orientation of the protein"""
for hc in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
rb= IMP.core.RigidMember(hc).get_rigid_body()
g= IMP.display.ReferenceFrameGeometry(rb.get_reference_frame())
g.set_name(get_nice_name(h)+"_orient")
if c:
g.set_color(c)
else:
g.set_color(chain_colors[IMP.atom.Chain(hc).get_id()])
w.add_geometry(g)
def add_skeleton(h, c, r, w, chain_colors):
"""Show the connectivity skeleton of the conformation to give an idea of
how things are layed out"""
for hc0 in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
for hc1 in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
if hc1 <= hc0:
continue
d= ps.evaluate((hc0, hc1), None)
if d < 1:
d0= IMP.core.XYZ(hc0)
d1= IMP.core.XYZ(hc1)
mp= .5*(d0.get_coordinates()+d1.get_coordinates())
g= IMP.display.CylinderGeometry(Cylinder(Segment(d0.get_coordinates(), mp), r))
if c:
g.set_color(c)
else:
g.set_color(chain_colors[IMP.atom.Chain(d0).get_id()])
g.set_name(get_nice_name(h)+"_skel")
w.add_geometry(g)
g= IMP.display.CylinderGeometry(Cylinder(Segment(d1.get_coordinates(), mp), r))
if c:
g.set_color(c)
else:
g.set_color(chain_colors[IMP.atom.Chain(d1).get_id()])
g.set_name(get_nice_name(h)+"_skel")
w.add_geometry(g)
IMP.set_log_level(IMP.TERSE)
# change to 46 to display all of them
hs= read(m, 3)
# used to test of two molecules are touching one another
ps.set_log_level(IMP.SILENT)
print "creating rigid bodies"
base_chains={}
for hc in IMP.atom.get_by_type(hs[0], IMP.atom.CHAIN_TYPE):
base_chains[c.get_id()]=c
for i, h in enumerate(hs):
for hc in IMP.atom.get_by_type(h, IMP.atom.CHAIN_TYPE):
if h==hs[0]:
else:
# make sure the rigid bodies have equivalent defining reference frames
# if we just used IMP.atom.create_rigid_body, globular proteins are likely
# to have different axis computed when starting in different orientations
crb=IMP.atom.create_compatible_rigid_body(hc, base_chains[c.get_id()])
print " ",i
chains= IMP.atom.get_by_type(hs[0], IMP.atom.CHAIN_TYPE)
chains.sort(lambda x,y: cmp(IMP.core.XYZ(x).get_x()+IMP.core.XYZ(x).get_y(),
IMP.core.XYZ(y).get_x()+ IMP.core.XYZ(y).get_y()))
chain_colors={}
for i,c in enumerate(chains):
id= IMP.atom.Chain(c).get_id()
#f= i/float(len(chains))
#IMP.display.get_jet_color(f)
chain_colors[id]=color
w= IMP.display.PymolWriter("markers.pym")
add_markers(hs[0], IMP.display.Color(1,1,1), w)
hso= hs[1:]
# sort them spatially so the colors are nicely arranged and allow one to visually connect
# the position of one end with that of the other
hso.sort(lambda h0, h1: cmp(IMP.core.XYZ(IMP.atom.Selection(h0, chain='I',
residue_index=237).get_selected_particles()[0]).get_z(),
residue_index=237).get_selected_particles()[0]).get_z()))
print "adding markers",
for i,h in enumerate(hso):
add_markers(h, c, w)
print " ",i
print "adding axis",
add_axis(hs[0], IMP.display.Color(1,1,1), w, chain_colors)
for i,h in enumerate(hs[1:]):
add_axis(h, None, w, chain_colors)
print i,
w= IMP.display.PymolWriter("skeletons.pym")
add_skeleton(hs[0], IMP.display.Color(1,1,1), 5, w, chain_colors)
print "adding skeleton",
for i,h in enumerate(hs[1:]):
add_skeleton(h, None, 1, w, chain_colors)
print " ",i

show particles as spheres

Write two colored set of random IMP::core::XYZR particles to a Chimera input file. One could write to a pymol file simply by replacing the IMP::display::Writer.

import IMP
import IMP.core
# create two lists of random particles for display
m = IMP.Model()
n = 10
radius = 1.0
bounding_box_size = 10.0
xyzrs0 = IMP.core.create_xyzr_particles(m, 10, radius, bounding_box_size)
xyzrs1 = IMP.core.create_xyzr_particles(m, 10, radius, bounding_box_size)
xyzrs0_container = IMP.container.ListSingletonContainer(xyzrs0)
xyzrs1_container = IMP.container.ListSingletonContainer(xyzrs1)
# create a writer that generates Chimera python scripts for visualizing the particle lists
# write first list of particles
g0= IMP.core.XYZRsGeometry(xyzrs0_container)
g0.set_name("my particles")
g0.set_color(IMP.display.Color(1,0,0))
w.add_geometry(g0)
# write second list of particles
g1= IMP.core.XYZRsGeometry(xyzrs1_container)
g1.set_name("my other particles")
g1.set_color(IMP.display.Color(0,1,0))
w.add_geometry(g1)
# make sure that the file is flushed
del w


Generated on Tue May 22 2012 23:33:21 for IMP by doxygen 1.8.1