IMP logo
IMP Reference Guide  2.18.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 from __future__ import division
6 import IMP
7 import IMP.core
8 import IMP.atom
9 import IMP.container
10 import sys
11 
12 IMP.setup_from_argv(sys.argv, "statistical potential")
13 
14 # Create simple test model containing 6 spherical particles
15 m = IMP.Model()
16 ps = IMP.core.create_xyzr_particles(m, 6, 1.0, 5.0)
17 
18 # Assign a numeric type to some of the particles. In practice this might
19 # be a function of the atom and/or residue type.
20 ik = IMP.IntKey("statpot_type")
21 ps[0].add_attribute(ik, 0)
22 ps[1].add_attribute(ik, 1)
23 ps[2].add_attribute(ik, 0)
24 ps[3].add_attribute(ik, 1)
25 
26 
27 # Define a custom PairPredicate that, given a pair of particles, maps the
28 # pair of types to a unique index, assuming that the i-j interaction is the
29 # same as j-i
30 class PairTypePredicate(IMP.PairPredicate):
31  def do_get_inputs(self, m, pis):
32  # We only use the particles themselves, no other inputs
33  return [m.get_particle(i) for i in pis]
34 
35  def get_value_index(self, m, pis):
36  # Return -1 if either particle is untyped
37  if not all(m.get_has_attribute(ik, pi) for pi in pis):
38  print("particle pair %s is untyped" % str(pis))
39  return -1
40  # Particle types 0,0 map to unique index 0; 1,0 or 0,1 to 1; 1,1 to 2
41  ts = sorted(m.get_attribute(ik, pi) for pi in pis)
42  ind = (ts[1] * ts[1]+1) // 2 + ts[0]
43  print("particle pair %s types %s map to %d" % (pis, ts, ind))
44  return ind
45 
46 
47 # Apply our restraint to all pairs of particles within 20.0
49 nbl = IMP.container.ClosePairContainer(lps, 20.0, 0.2)
50 r = IMP.container.PredicatePairsRestraint(PairTypePredicate(), nbl)
51 
52 # Now we can score specific interactions. Here we try to draw pairs
53 # of particles that both have type 1 (unique index 2 returned by
54 # our predicate) together
56 r.set_score(2, ps)
57 
58 # Any other interaction between either typed or untyped particles is ignored
59 r.set_is_complete(False)
60 
61 # Score the system
63 print("Score is", sf.evaluate(False))