IMP logo
IMP Reference Guide  2.19.0
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 
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))
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
XYZRs create_xyzr_particles(Model *m, unsigned int num, Float radius, Float box_side=10)
Create a set of particles with random coordinates.
Applies a PairScore to each Pair in a list based on a predicate.
Create a scoring function on a list of restraints.
Return all close unordered pairs of particles taken from the SingletonContainer.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Store a list of ParticleIndexes.
Basic functionality that is expected to be used by a wide variety of IMP users.
Abstract predicate function.
Definition: PairPredicate.h:31
Functionality for loading, creating, manipulating and scoring atomic structures.