IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
container/statistical.py

This example shows how to create a simple statistical potential using the PredicatePairsRestraint.

1 ## \example container/statistical.py
2 # This example shows how to create a simple statistical potential using
3 # the PredicatePairsRestraint.
4 
5 import IMP
6 import IMP.core
7 import IMP.atom
8 import IMP.container
9 import sys
10 
11 IMP.setup_from_argv(sys.argv, "statistical potential")
12 
13 # Create simple test model containing 6 spherical particles
14 m = IMP.Model()
15 ps = 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.
19 ik = IMP.IntKey("statpot_type")
20 ps[0].add_attribute(ik, 0)
21 ps[1].add_attribute(ik, 1)
22 ps[2].add_attribute(ik, 0)
23 ps[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
29 class 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
48 nbl = IMP.container.ClosePairContainer(lps, 20.0, 0.2)
49 r = 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
55 r.set_score(2, ps)
56 
57 # Any other interaction between either typed or untyped particles is ignored
58 r.set_is_complete(False)
59 
60 # Score the system
62 print("Score is", sf.evaluate(False))