This example shows how to write basic geometric primitives to a file with color and name.
import IMP.display
# 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
w= IMP.display.PymolWriter(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_color(IMP.display.get_display_color(i))
g.set_name(str(i))
# add it to the file
w.add_geometry(g)
|
A simple example of how to use one of the IMP.display.LogOptimizerStates.
import IMP
import IMP.display
import IMP.core
import IMP.container
m= IMP.Model()
ps=IMP.core.create_xyzr_particles(m, 20, 5)
# 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= IMP.core.XYZRsGeometry(c)
g.set_name("my particles")
g.set_color(IMP.display.Color(1,0,0))
log.add_geometry(g)
m.add_restraint(r)
o= IMP.core.MonteCarlo(m)
mv= IMP.core.BallMover(ps, 10)
o.add_mover(mv)
o.add_optimizer_state(log)
o.optimize(1000)
|
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.display
import IMP.atom
Segment= IMP.algebra.Segment3D
Cylinder= IMP.algebra.Cylinder3D
# 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")
h= IMP.atom.read_pdb(name, m, IMP.atom.CAlphaPDBSelector())
hs.append(hr)
simp= IMP.atom.create_simplified_along_backbone(IMP.atom.Chain(c), 4)
hr.add_child(simp)
if i==0:
base=IMP.atom.get_leaves(hr)
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"""
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"""
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)
m= IMP.Model()
# change to 46 to display all of them
hs= read(m, 3)
# used to test of two molecules are touching one another
ps= IMP.core.KClosePairsPairScore(IMP.core.SphereDistancePairScore(IMP.core.HarmonicUpperBound(10,1)),
ps.set_log_level(IMP.SILENT)
print "creating rigid bodies"
base_chains={}
c= IMP.atom.Chain(hc)
base_chains[c.get_id()]=c
for i, h in enumerate(hs):
c=IMP.atom.Chain(hc)
if h==hs[0]:
crb=IMP.atom.create_rigid_body(hc)
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)
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))
color= IMP.display.get_display_color(i)
#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
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
w= IMP.display.PymolWriter("axis.pym")
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
|
|
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.algebra
import IMP.container
import IMP.core
import IMP.display
# 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
w = IMP.display.ChimeraWriter("out.py")
# 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
|