IMP  2.4.0
The Integrative Modeling Platform
write_a_restraint.py
1 ## \example kernel/write_a_restraint.py
2 # While we do not recommend doing serious work using restraints written in Python, it is often useful when prototyping or testing code. Copy this example and modify as needed.
3 #
4 
5 from __future__ import print_function
6 import IMP
7 
8 # a restraint which checks if particles are sorted in
9 # increasing order on k.
10 
11 
12 class MyRestraint(IMP.kernel.Restraint):
13  # take the list of particles and the key to use
14 
15  def __init__(self, m, ps, k):
16  IMP.kernel.Restraint.__init__(self, m, "MyRestraint %1%")
17  self.ps = ps
18  self.k = k
19 
20  def unprotected_evaluate(self, da):
21  score = 0
22  for i in range(1, len(self.ps)):
23  p0 = self.ps[i - 1]
24  p1 = self.ps[i]
25  if p0.get_value(k) > p1.get_value(k):
26  diff = (p0.get_value(k) - p1.get_value(k))
27  score = score + diff
28  p0.add_to_derivative(k, -1, da)
29  p1.add_to_derivative(k, 1, da)
30  else:
31  if IMP.get_log_level() >= IMP.base.TERSE:
32  print(p0.get_name(), "and", p1.get_name(), " are ok")
33  return score
34 
35  def do_get_inputs(self):
36  return self.ps
37 
38 # some code to create and evaluate it
39 k = IMP.FloatKey("a key")
40 m = IMP.kernel.Model()
41 ps = []
42 for i in range(0, 10):
43  p = IMP.kernel.Particle(m)
44  p.add_attribute(k, i)
45  ps.append(p)
46 r = MyRestraint(m, ps, k)
47 # IMP.base.set_log_level(IMP.base.TERSE)
48 print(r.evaluate(True))
A restraint is a term in an IMP ScoringFunction.
Class to handle individual model particles.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73