IMP logo
core examples

XYZR Decorator

This is a simple example using the XYZRDecorator to set the coordinates and radius of a particle and compute distances between the resulting spheres.

import IMP
import IMP.core
d0= IMP.core.XYZR.setup_particle(p0, IMP.algebra.Sphere3D(IMP.algebra.Vector3D(0,1,2),
1.0))
d1.set_coordinates(IMP.algebra.Vector3D(3,4,5))
d1.set_radius(2.0)
print IMP.core.get_distance(d0, d1)
# use them as XYZ particles
# distance without radii
print IMP.core.get_distance(xd0, xd1)

XYZ Decorator

This is a simple example using the XYZDecorator to set the coordinates of some particles and compute the distance between them.

import IMP
import IMP.core
# add x,y,z coordinates to the particle
# add slots for the coordinates, but leave them uninitialized
# set the coordinate values
d1.set_coordinates(IMP.algebra.Vector3D(3,4,5))
# return the distance between the two points described by the decorated
# particles
print IMP.core.get_distance(d0, d1)

connectivity restraint

This example shows how to use the ConnectivityRestraint to ensure that all the particles end up in a connected conformation following the optimization. One should also check out the IMP::atom::create_connectivity_restraint() helper functions.

import IMP
import IMP.core
import IMP.atom
# Put the parent particles for each molecule
hs=[]
# create the molecules, with 5 particles for each of 10 molecules
for i in range(0,10):
for j in range(0,5):
d.add_child(cd)
xd= IMP.core.XYZR.setup_particle(p, IMP.algebra.Sphere3D(IMP.algebra.Vector3D(3*i,j,0), 1))
hs.append(p)
# score based on the one closest particle from each set of balls
# connect all 10 molecules together
cr.set_particles(hs)
m.add_restraint(cr)
m.evaluate(False)

cover particles

Show how to maintain a sphere per residue which includes all atoms of the residue. The derivatives are propagated from the sphere cover to the atoms so that restraints can be used at multiple levels.

import IMP
import IMP.core
import IMP.atom
import IMP.atom
prot= IMP.atom.read_pdb(IMP.core.get_example_path('example_protein.pdb'), m)
res= IMP.atom.get_by_type(prot, IMP.atom.RESIDUE_TYPE)
for r in res:
# add coordinates and a radius to the residue particle
# make sure that the coordinates and the radius define a sphere
# which contains all of the leaves (atoms) of the residue
IMP.core.Cover.setup_particle(r.get_particle(), pr)
# update the coordinates of the residue particles so that they cover the atoms
m.update()

excluded volume

Setup an excluded volume restraint between a bunch of particles with radius.

(m,c)=IMP.example.create_model_and_particles()
# this container lists all pairs that are close at the time of evaluation
# use the lower bound on the inter-sphere distance to push the spheres apart
m.add_restraint(nbr)
# alternatively, one could just do
m.add_restraint(r)
# get the current score
print m.evaluate(False)

incremental mc

This example shows how to do incremental scoring with Monte Carlo. Incremental scoring can be significantly faster than non-incremental scoring when using moves that only move a few particles at a time.

import IMP.core
bb= IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0,0,0),
IMP.algebra.Vector3D(30, 30, 30))
ps=[]
for i in range(0,20):
ps.append(IMP.Particle(m))
d.set_radius(10)
d.set_coordinates(IMP.algebra.get_random_vector_in(bb))
d.set_coordinates_are_optimized(True)
psl)
mc.set_incremental_scoring_function(isf)
mvs=[IMP.core.BallMover([p], 5) for p in ps]
mc.add_mover(sm)
IMP.set_log_level(IMP.SILENT)
print "initial",isf.evaluate(False)
after=mc.optimize(10000)
print "final", after
name=IMP.base.create_temporary_file_name("incremental_mc", ".pym")
for p in ps:
w.add_geometry(g)
print "pymol", name

ms connectivity restraint

This example shows how to use the MSConnectivityRestraint to ensure that all the particles that are part of complexes end up in a connected conformation following the optimization. It allows multiple copies of particles and takes an experimental tree as an input.

#-- File: ms_connectivity_restraint.py --#
import IMP
import IMP.core
# Setup model
m = IMP.Model()
ps= [IMP.Particle(m) for x in xrange(6)]
ds= []
ds.append(IMP.core.XYZ.setup_particle(ps[0], IMP.algebra.Vector3D(0.0, 0.0, 0.0)))
ds.append(IMP.core.XYZ.setup_particle(ps[1], IMP.algebra.Vector3D(1.0, 1.0, 0.0)))
ds.append(IMP.core.XYZ.setup_particle(ps[2], IMP.algebra.Vector3D(2.0, 0.0, 0.0)))
ds.append(IMP.core.XYZ.setup_particle(ps[3], IMP.algebra.Vector3D(3.0, 0.0, 0.0)))
ds.append(IMP.core.XYZ.setup_particle(ps[4], IMP.algebra.Vector3D(4.0, -1.0, 0.0)))
ds.append(IMP.core.XYZ.setup_particle(ps[5], IMP.algebra.Vector3D(1000, 1000, 1000)))
# Create MS connectivity restraint
# Add particle types to the restraint
# add_type() returns a unique type handle that can be used as an argument to add_composite() later on.
pa = r.add_type([ds[0], ds[1]])
pb = r.add_type([ds[2], ds[3]])
pc = r.add_type([ds[4]])
pd = r.add_type([ds[5]])
# Enter experimental tree data into restraint
# In add_composite(), the first argument is node label and the second argument is the parent.
i1 = r.add_composite([pa, pa, pb, pb, pc])
i2 = r.add_composite([pa, pb, pb, pc], i1)
i3 = r.add_composite([pa, pa, pb, pb], i1)
i4 = r.add_composite([pa, pb], i1)
i5 = r.add_composite([pa, pb, pb], i2)
i6 = r.add_composite([pb, pc], i2)
i7 = r.add_composite([pa, pa, pb], i3)
i8 = r.add_composite([pa, pb], i5)
# Add restraint to the model and evaluate the model score
m.add_restraint(r)
m.evaluate(False)

optimize balls

This example optimizes a set of a balls to form 100 chains packed into a box. It illustrates using Monte Carlo (incremental) and conjugate gradients in conjunction in a non-trivial optimization.

import IMP.core
import IMP.rmf
bb=IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0,0,0),
# in fast do 10,10,10, for the purposes of testing we reduce it
ni=2
nj=2
np=2
radius=.45
k=100
# using a HarmonicDistancePairScore for fixed length links is more
# efficient than using a HarmonicSphereDistnacePairScore and works
# better with the optimizer
IMP.set_log_level(IMP.SILENT)
aps=[]
filters=[]
movers=[]
rss= IMP.RestraintSet(m, 1.0, "bonds")
for i in range(0,ni):
for j in range(0,nj):
chain=[]
for k in range(0,np):
p.set_name("P"+str(i)+" "+str(j)+" "+str(k))
s=IMP.algebra.Sphere3D(IMP.algebra.get_random_vector_in(bb), radius)
movers.append(IMP.core.BallMover([p], radius*2))
movers[-1].set_was_used(True)
if k==0:
d.set_coordinates(base)
else:
d.set_coordinates_are_optimized(True)
chain.append(p)
aps.append(p)
rss.add_restraint(r)
# cheat
filters[-1].set_was_used(True)
k, 1)
nbl.set_model(m)
nbl.set_pair_filters(filters)
#m.add_restraint(nbl)
rss.add_restraint(bbr)
mc.add_mover(sm)
# we are special casing the nbl term
mc.set_restraints([rss])
# use special incremental support for the non-bonded part
isf.add_close_pair_score(sps, 0, aps, filters)
mc.set_incremental_scoring_function(isf)
# first relax the bonds a bit
rs=[]
for p in aps:
0))
cg.set_scoring_function(sf)
cg.optimize(1000)
print "collisions", nbl.evaluate(False), "bonds", rss.evaluate(False), bbr.evaluate(False)
# shrink each of the particles, relax the configuration, repeat
for i in range(1,11):
rs=[]
factor=.1*i
for p in aps:
IMP.core.XYZR(p).get_radius()*factor))
# move each particle 100 times
print factor
for j in range(0,5):
mc.set_kt(100.0/(3*j+1))
print "mc", mc.optimize(ni*nj*np*(j+1)*100), m.evaluate(False), cg.optimize(10)
del rs
print "collisions", nbl.evaluate(False), "bonds", rss.evaluate(False), "bounding box", bbr.evaluate(False)
w= IMP.display.PymolWriter("final.pym")
for p in aps:
w.add_geometry(g)
g.set_name("bb")
w.add_geometry(g)

pair restraint

Restrain the distance between a pair of particles.

(m,c)=IMP.example.create_model_and_particles()
r= IMP.core.PairRestraint(df, (c.get_particle(0), c.get_particle(1)))
m.add_restraint(r)

restrain diameter

An example restraining the diameter of a set of points. That is, the restraint penalizes conformations where there are two point more than a certain distance from one another.

import IMP
import IMP.core
# This example restraints the diameter of a set of particles to be smaller than 10
diameter=10
r=IMP.core.DiameterRestraint(h, lc, diameter)
m.add_restraint(r)
# Set up optimizer
o.set_model(m)
max=0
for p0 in lc.get_particles():
for p1 in lc.get_particles():
if d > max: max=d
print "The maximim distance is "+str(max)
IMP.set_log_level(IMP.SILENT)
o.optimize(100)
max=0
for p0 in lc.get_particles():
for p1 in lc.get_particles():
if d > max: max=d
print "Afterwards, the maximim distance is "+str(max)

rigid bodies

This example shows how to set up rigid bodies, one per residue in a protein. A score state is then used to ensure that the bodies remain rigid during the optimization process.

import IMP
import IMP.core
import IMP.atom
# create a new IMP.atom.Hierarchy for the pdb file
mp1= IMP.atom.read_pdb(IMP.core.get_example_path('example_protein.pdb'), m)
chains= IMP.atom.get_by_type(mp1, IMP.atom.CHAIN_TYPE)
rd= IMP.atom.Hierarchy(chains[0])
# Create a rigid body from the first chain
# note that rbs != chains[0] as the bounding volume for rbs needs to include all of the
# chain, but chains[0] might have a smaller sphere associated with it.
print "all done"

rigid collisions

Collision detection and building of a non-bonded list can be sped up when rigid bodies are used. To do this, use a RigidClosePairsFinder.

import IMP
import IMP.core
import IMP.atom
# This example addes a restraint on nonbonded interactions
# Since it is between two rigid bodies, internal interactions are ignored
# The particles in the rigid bodies
rbp0= IMP.Particle(m)
rbp1= IMP.Particle(m)
rbss0 = IMP.core.RigidBody.setup_particle(rbp0, IMP.core.XYZs(rbps0))
rbss1 = IMP.core.RigidBody.setup_particle(rbp1, IMP.core.XYZs(rbps1))
lsc.add_particles(rbps0)
lsc.add_particles(rbps1)
# Set up the nonbonded list
# Set up excluded volume
evri= m.add_restraint(evr)
# Set up optimizer
o.set_model(m)
done=False
while not done:
try:
o.optimize(1000)
for d in [rbss0, rbss1]:
IMP.algebra.get_random_vector_in(IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0,0,0),
IMP.algebra.Vector3D(10,10,10)))))
else:
done=True

symmetry

Show how to use the code in core to enforce symmetry.

import IMP.core
m.set_log_level(IMP.SILENT)
ps =[]
# create 4 xyz particles
for i in range(0,4):
p = IMP.Particle(m)
ps.append(p)
# set the 0 particle as the reference particle for the others as
# they will get their positions from it
for i,p in enumerate(ps[1:]):
# the other 3 particles are all symmetric copies of the first
# the symmetry operation is rotation around the z axis
tr= IMP.algebra.Transformation3D(IMP.algebra.get_rotation_about_axis(IMP.algebra.get_basis_vector_3d(2),
3.14/2.0*(i+1)),
# set up a constraint for the one particle, if you have more than one with the same symmetry
# transform, you should use an IMP.container.SingletonsConstraint.
m.add_score_state(c)
1)
m.add_restraint(r)
d0= IMP.core.XYZ(ps[0])
# print only optimize the main particle
d0.set_coordinates_are_optimized(True)
opt.optimize(10)
print "score is ", m.evaluate(False)
for p in ps:
print p.get_name(), IMP.core.XYZ(p).get_coordinates()


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