IMP logo
IMP Reference Guide  2.20.1
The Integrative Modeling Platform
core/linear_and_harmonic_scores.py

An example for setting a linear or harmonic score between two particles using either a point distance (between particle centers) or a sphere distance (between particle surfaces). Note: this example relies on matplotlib for plotting the scores, but it can be easily modified to just print the scores.

Author: Barak Raveh, 2022/12/13

1 ## \example core/linear_and_harmonic_scores.py
2 # An example for setting a linear or harmonic score between two particles
3 # using either a point distance (between particle centers) or a sphere
4 # distance (between particle surfaces).
5 #
6 # Note: this example relies on matplotlib for plotting the scores,
7 # but it can be easily modified to just print the scores.
8 #
9 # Author: Barak Raveh, 2022/12/13
10 
11 import IMP
12 import IMP.algebra
13 import IMP.core
14 try:
15  import matplotlib.pyplot
16 except ImportError:
17  matplotlib = None
18 import numpy
19 import sys
20 
21 
22 IMP.setup_from_argv(sys.argv, "linear or harmonic score example")
23 
24 
25 DEFAULT_RADIUS = 2.0 # radius of particles
26 LINEAR_OFFSET = 4.0 # distance at which the function is zero (note this is not the Y-axis intercept!)
27 LINEAR_SLOPE = 3.0 # slope of linear score (= force in kcal/mol/A)
28 HARMONIC_MEAN = 3 # distance at which the function is minimal/maximal
29 HARMONIC_K = 2.0 # quadratic coefficient
30 
31 def create_particle(m, radius = DEFAULT_RADIUS):
32  p = IMP.Particle(m)
33  s = IMP.algebra.Sphere3D([0,0,0], radius)
35  return p
36 
37 def create_linear_point_pair_score():
38  ''' slope*x + intercept for point distance '''
39  linear_functor = IMP.core.Linear(LINEAR_OFFSET, LINEAR_SLOPE)
40  return IMP.core.DistancePairScore(linear_functor)
41 
42 def create_harmonic_point_pair_score():
43  ''' 0.5*k*(x-mean)^2 for point distance '''
44  harmonic_functor = IMP.core.Harmonic(HARMONIC_MEAN, HARMONIC_K)
45  return IMP.core.DistancePairScore(harmonic_functor)
46 
47 def create_linear_sphere_pair_score():
48  ''' slope*x + intercept for sphere distance '''
49  linear_functor = IMP.core.Linear(LINEAR_OFFSET, LINEAR_SLOPE)
50  return IMP.core.SphereDistancePairScore(linear_functor)
51 
52 def create_harmonic_sphere_pair_score():
53  ''' 0.5*k*(x-mean)^2 for sphere distance '''
54  harmonic_functor = IMP.core.Harmonic(HARMONIC_MEAN, HARMONIC_K)
55  return IMP.core.SphereDistancePairScore(harmonic_functor)
56 
57 def create_model(pair_score):
58  m = IMP.Model()
59  particles = [create_particle(m) for x in range(2)]
60  restraint = IMP.core.PairRestraint(m, pair_score, particles)
61  xyzrs = [IMP.core.XYZR(p) for p in particles]
62  return m, xyzrs, restraint
63 
64 def plot_score(pair_score, caption,
65  xmin = -15.0, xmax = 15.0, xstep = 0.01):
66  '''
67  Plots a pair_score between two particles, one particle
68  being at [0,0,0] and the other particle being at [x,0,0]
69  for x in the closed interval [xmin:xstep:xmax]
70  '''
71  m, xyzrs, restraint = create_model(pair_score)
72  xyzrs[0].set_coordinates([0,0,0])
73  X = numpy.arange(xmin, xmax+0.1*xstep, xstep)
74  Y = 0.0*X
75  for i,x in enumerate(X):
76  xyzrs[0].set_coordinates([x,0,0])
77  Y[i] = restraint.get_score()
78  if not matplotlib:
79  print("Not showing plot; matplotlib is not installed "
80  "or could not be imported")
81  elif IMP.get_is_quick_test():
82  print("Not showing plot, as we are running test cases")
83  else:
84  matplotlib.pyplot.plot(X,Y,'-')
85  matplotlib.pyplot.title(caption)
86  matplotlib.pyplot.xlabel(r"$X_2$ [$\AA$]")
87  matplotlib.pyplot.ylabel("Energy [$kcal \cdot mol^{-1}$]")
88  matplotlib.pyplot.gca().spines['bottom'].set_position(('data', 0))
89  matplotlib.pyplot.show()
90 
91 if __name__ == "__main__":
92  # NOTE: sphere distance is not distance!
93  linear_str = "{:.1f}*(dist-{:.1f})".format(LINEAR_SLOPE, LINEAR_OFFSET)
94  harmonic_str = "{:.1f}*(dist-{:.1f})^2".format(HARMONIC_K, HARMONIC_MEAN)
95  plot_score(create_linear_point_pair_score(),
96  caption="Linear point distance\n{}"
97  .format(linear_str))
98  plot_score(create_linear_sphere_pair_score(),
99  caption="Linear sphere distance (R={:.1f} A)\n{}"
100  .format(DEFAULT_RADIUS, linear_str))
101  plot_score(create_harmonic_point_pair_score(),
102  caption="Harmonic point distance\n{}"
103  .format(harmonic_str))
104  plot_score(create_harmonic_sphere_pair_score(),
105  caption="Harmonic sphere distance (R={:.1f} A)\n{}"
106  .format(DEFAULT_RADIUS, harmonic_str))