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
import IMP.algebra
m= IMP.Model()
p0= IMP.Particle(m)
d0= IMP.core.XYZR.setup_particle(p0, IMP.algebra.Sphere3D(IMP.algebra.Vector3D(0,1,2),
1.0))
p1= IMP.Particle(m)
d1= IMP.core.XYZR.setup_particle(p1)
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
xd0= IMP.core.XYZ.decorate_particle(p0)
xd1= IMP.core.XYZ.decorate_particle(p1)
# distance without radii
print IMP.core.get_distance(xd0, xd1)
|
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
import IMP.algebra
m= IMP.Model()
p0= IMP.Particle(m)
# add x,y,z coordinates to the particle
d0= IMP.core.XYZ.setup_particle(p0, IMP.algebra.Vector3D(0,1,2))
p1= IMP.Particle(m)
# add slots for the coordinates, but leave them uninitialized
d1= IMP.core.XYZ.setup_particle(p1)
# 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)
|
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.algebra
import IMP.atom
m= IMP.Model()
# 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):
p=IMP.Particle(m)
for j in range(0,5):
p=IMP.Particle(m)
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
lrps = IMP.core.KClosePairsPairScore(ps, cps, 1)
# connect all 10 molecules together
cr = IMP.core.ConnectivityRestraint(lrps)
cr.set_particles(hs)
m.add_restraint(cr)
m.evaluate(False)
|
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
m= IMP.Model()
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()
|
Setup an excluded volume restraint between a bunch of particles with radius.
import IMP.example
(m,c)=IMP.example.create_model_and_particles()
# this container lists all pairs that are close at the time of evaluation
nbl= IMP.container.ClosePairContainer(c, 0,2)
h= IMP.core.HarmonicLowerBound(0,1)
# use the lower bound on the inter-sphere distance to push the spheres apart
nbr= IMP.container.PairsRestraint(sd, nbl)
m.add_restraint(nbr)
# alternatively, one could just do
m.add_restraint(r)
# get the current score
print m.evaluate(False)
|
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.
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
import IMP.algebra
# Setup model
m = IMP.Model()
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
ub = IMP.core.HarmonicUpperBound(1.0, 0.1)
ss= IMP.core.DistancePairScore(ub)
# 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)
|
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.display
import IMP.container
import IMP.rmf
bb=IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0,0,0),
IMP.algebra.Vector3D(10,10,10));
# 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
lps= IMP.core.HarmonicDistancePairScore(1.5*radius, k)
sps= IMP.core.SoftSpherePairScore(k)
m= IMP.Model()
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):
base=IMP.algebra.Vector3D(i,j,0)
chain=[]
for k in range(0,np):
p= IMP.Particle(m)
p.set_name("P"+str(i)+" "+str(j)+" "+str(k))
s=IMP.algebra.Sphere3D(IMP.algebra.get_random_vector_in(bb), radius)
d= IMP.core.XYZR.setup_particle(p,s)
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)
cpc= IMP.container.ExclusiveConsecutivePairContainer(chain)
r= IMP.container.PairsRestraint(lps, cpc)
rss.add_restraint(r)
# cheat
filters.append(IMP.container.InContainerPairFilter(cpc))
filters[-1].set_was_used(True)
laps=IMP.container.ListSingletonContainer(aps)
nbl= IMP.core.ExcludedVolumeRestraint(laps,
k, 1)
nbl.set_model(m)
nbl.set_pair_filters(filters)
#m.add_restraint(nbl)
ibss= IMP.core.BoundingBox3DSingletonScore(IMP.core.HarmonicUpperBound(0,k), bb)
bbr= IMP.container.SingletonsRestraint(ibss, laps)
rss.add_restraint(bbr)
cg= IMP.core.ConjugateGradients(m)
mc=IMP.core.MonteCarlo(m)
sm= IMP.core.SerialMover(movers)
mc.add_mover(sm)
# we are special casing the nbl term
mc.set_restraints([rss])
isf= IMP.core.IncrementalScoringFunction(aps, [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)
sf= IMP.core.RestraintsScoringFunction([rss, nbl])
# first relax the bonds a bit
rs=[]
for p in aps:
rs.append(IMP.ScopedSetFloatAttribute(p, IMP.core.XYZR.get_radius_key(),
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:
rs.append(IMP.ScopedSetFloatAttribute(p, IMP.core.XYZR.get_radius_key(),
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:
g= IMP.core.XYZRGeometry(p)
w.add_geometry(g)
g.set_name("bb")
w.add_geometry(g)
|
|
Restrain the distance between a pair of particles.
import IMP.example
(m,c)=IMP.example.create_model_and_particles()
uf= IMP.core.Harmonic(0,1)
df= IMP.core.DistancePairScore(uf)
r= IMP.core.PairRestraint(df, (c.get_particle(0), c.get_particle(1)))
m.add_restraint(r)
|
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
import IMP.container
# This example restraints the diameter of a set of particles to be smaller than 10
diameter=10
m= IMP.Model()
lc= IMP.container.ListSingletonContainer(IMP.core.create_xyzr_particles(m, 50, 1.0))
h=IMP.core.HarmonicUpperBound(0,1)
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():
IMP.core.XYZ(p1))
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():
IMP.core.XYZ(p1))
if d > max: max=d
print "Afterwards, the maximim distance is "+str(max)
|
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
import IMP.container
m= IMP.Model()
# 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.
rbs=IMP.atom.create_rigid_body(chains[0])
print "all done"
|
Collision detection and building of a non-bonded list can be sped up when rigid bodies are used. To do this, use a RigidClosePairsFinder.
Show how to use the code in core to enforce symmetry.
import IMP.core
import IMP.container
m= IMP.Model()
m.set_log_level(IMP.SILENT)
ps =[]
# create 4 xyz particles
for i in range(0,4):
p = IMP.Particle(m)
ps.append(p)
d= IMP.core.XYZ.setup_particle(p, IMP.algebra.Vector3D(.5,0,0))
# 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
IMP.core.Reference.setup_particle(p, ps[0])
# 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)),
IMP.algebra.Vector3D(0,0,0))
sm= IMP.core.TransformationSymmetry(tr)
# 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.
c= IMP.core.SingletonConstraint(sm, None, p)
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= IMP.core.ConjugateGradients(m)
opt.optimize(10)
print "score is ", m.evaluate(False)
for p in ps:
print p.get_name(), IMP.core.XYZ(p).get_coordinates()
|
|