IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
statistical.py
1## \example container/statistical.py
2# This example shows how to create a simple statistical potential using
3# the PredicatePairsRestraint.
4
5import IMP
6import IMP.core
7import IMP.atom
8import IMP.container
9import sys
10
11IMP.setup_from_argv(sys.argv, "statistical potential")
12
13# Create simple test model containing 6 spherical particles
14m = IMP.Model()
15ps = IMP.core.create_xyzr_particles(m, 6, 1.0, 5.0)
16
17# Assign a numeric type to some of the particles. In practice this might
18# be a function of the atom and/or residue type.
19ik = IMP.IntKey("statpot_type")
20ps[0].add_attribute(ik, 0)
21ps[1].add_attribute(ik, 1)
22ps[2].add_attribute(ik, 0)
23ps[3].add_attribute(ik, 1)
24
25
26# Define a custom PairPredicate that, given a pair of particles, maps the
27# pair of types to a unique index, assuming that the i-j interaction is the
28# same as j-i
29class PairTypePredicate(IMP.PairPredicate):
30 def do_get_inputs(self, m, pis):
31 # We only use the particles themselves, no other inputs
32 return [m.get_particle(i) for i in pis]
33
34 def get_value_index(self, m, pis):
35 # Return -1 if either particle is untyped
36 if not all(m.get_has_attribute(ik, pi) for pi in pis):
37 print("particle pair %s is untyped" % str(pis))
38 return -1
39 # Particle types 0,0 map to unique index 0; 1,0 or 0,1 to 1; 1,1 to 2
40 ts = sorted(m.get_attribute(ik, pi) for pi in pis)
41 ind = (ts[1] * ts[1]+1) // 2 + ts[0]
42 print("particle pair %s types %s map to %d" % (pis, ts, ind))
43 return ind
44
45
46# Apply our restraint to all pairs of particles within 20.0
48nbl = IMP.container.ClosePairContainer(lps, 20.0, 0.2)
49r = IMP.container.PredicatePairsRestraint(PairTypePredicate(), nbl)
50
51# Now we can score specific interactions. Here we try to draw pairs
52# of particles that both have type 1 (unique index 2 returned by
53# our predicate) together
55r.set_score(2, ps)
56
57# Any other interaction between either typed or untyped particles is ignored
58r.set_is_complete(False)
59
60# Score the system
62print("Score is", sf.evaluate(False))
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Abstract predicate function.
Return all close unordered pairs of particles taken from the SingletonContainer.
Applies a PairScore to each Pair in a list based on a predicate.
Score distance between two particle centers using a harmonic function.
Create a scoring function on a list of restraints.
Functionality for loading, creating, manipulating and scoring atomic structures.
Various classes to hold sets of particles.
Basic functionality that is expected to be used by a wide variety of IMP users.
XYZRs create_xyzr_particles(Model *m, unsigned int num, Float radius, Float box_side=10)
Create a set of particles with random coordinates.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.