IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
The Integrative Modeling Platform
monte_carlo.py
1 """@namespace IMP.EMageFit.monte_carlo
2  Classes for Monte Carlo sampling using rigid body relative moves.
3 """
4 
5 import IMP
6 import IMP.core as core
7 import IMP.em2d as em2d
8 import IMP.algebra as alg
9 import IMP.EMageFit.imp_general.representation as representation
10 import IMP.EMageFit.imp_general.io as io
11 import time
12 import logging
13 log = logging.getLogger("MonteCarloParticleSates")
14 
15 
16 #
17 
18 # Generate possible configurations for the rigid bodies of the
19 # elements of a complex applying MonteCarlo
20 
21 #
22 
23 
24 def set_random_seed(index):
25  """
26  initializing the random number generator manually is useful for the
27  jobs in the cluster.
28  @param index Seed for the random number generator
29  """
30  if index != -1:
31  log.debug("Seed for the random number generator: %s", index)
32  IMP.random_number_generator.seed(index)
33  else:
34  IMP.random_number_generator.seed(int(time.time()))
35 
36 
38 
39  """
40  Class to do Monte Carlo sampling by using as the set of movements
41  relative positions between rigid bodies
42  """
43 
44  def __init__(self, model, rigid_bodies, anchored):
45  log.info("Setting MonteCarloRelativeMoves")
46  self.model = model
47  self.rbs = rigid_bodies
48  self.components = []
49  self.best_models = []
50  self.anchored = anchored
51  self.parent_rbs = []
52  # triplets with the information to build a relative mover using the
53  # results from docking with HEX
54  self.dock_transforms = None
55  self.non_relative_move_prob = 0.1
56 
57  log.debug("Anchored components %s", self.anchored)
58  T = alg.Transformation3D(alg.get_identity_rotation_3d(),
59  alg.Vector3D(0., 0., 0.))
60  origin = alg.ReferenceFrame3D(T)
61  for rb in self.rbs:
62  rb.set_reference_frame(origin)
63 
64  def set_temperature_pattern(self, temperatures, iterations, cycles):
65  """
66  Set the temperature pattern to use during the Monte Carlo
67  optimizations.
68  @param temperatures List of temperature values
69  @param iterations List with the iterations to do for each of the
70  temperatures.
71  @param cycles Number of repetitions of the
72  pattern of temperatures and iterations
73  """
74  self.temperatures = temperatures
75  self.iterations = iterations
76  self.cycles = cycles
77  log.debug("Temperatures %s", self.temperatures)
78  log.debug("Iterations %s", self.iterations)
79  log.debug("Cycles %s", self.cycles)
80 
81  def set_movers(self, max_translation, max_rotation):
82  log.info("Setting movers for subunits")
83  self.movers = []
84  for is_anchored, rb in zip(self.anchored, self.rbs):
85  if not is_anchored:
86  self.movers.append(core.RigidBodyMover(rb, max_translation,
87  max_rotation))
88 
89  def set_relative_movers(self, max_translation, max_rotation):
90  """
91  Generate the relative models form the transforms. The transforms
92  is a list with triplets [id1, id2, transform_file]
93  @param max_translation Maximum translation distance allowed for
94  the moves
95  @param max_rotation Maximum rotation angle allowed for the moves
96  """
97  log.info("Setting relative movers")
98  self.movers = []
99  relative_movers = []
100  relative_names = []
101  for d in self.dock_transforms:
102  rb_id = representation.get_rb_name(d[1])
103  if rb_id not in relative_names:
104  log.debug("Checking for %s", rb_id)
105  rb_lig = representation.get_rigid_body(self.rbs, rb_id)
106  rb_lig.set_coordinates_are_optimized(True)
107  mv = em2d.RelativePositionMover(rb_lig, max_translation,
108  max_rotation)
109  relative_movers.append(mv)
110  relative_names.append(rb_id)
111  log.debug("added a RelativePositionMover for %s", rb_id)
112  i = relative_names.index(rb_id)
113  relative_movers[i].set_random_move_probability(
114  self.non_relative_move_prob)
115  rb_rec = representation.get_rigid_body(
116  self.rbs, representation.get_rb_name(d[0]))
117  rb_rec.set_coordinates_are_optimized(True)
118  log.debug("Reference added for %s: %s. ref. frame %s ",
119  rb_id, rb_rec.get_name(), rb_rec)
120  Tis = io.read_transforms(d[2])
121  relative_movers[i].add_internal_transformations(rb_rec, Tis)
122  # add regular movers for the rigid bodies that are neither moved
123  # anchored nor moved relative to others
124  regular_movers = []
125  for is_anchored, rb in zip(self.anchored, self.rbs):
126  if not is_anchored:
127  name = rb.get_name()
128  if name not in relative_names:
129  rb.set_coordinates_are_optimized(True)
130  log.debug("adding a RigidBodyMover for %s", name)
131  mv = core.RigidBodyMover(rb, max_translation, max_rotation)
132  regular_movers.append(mv)
133  self.movers = regular_movers
134  self.movers += relative_movers
135 
136  def set_moving_parameters(self, max_translations, max_rotations):
137  self.max_translations = max_translations
138  self.max_rotations = max_rotations
139  log.debug("Maximum translations %s", self.max_translations)
140  log.debug("Maximum rotations %s", self.max_rotations)
141 
142  def set_scoring_function(self, scoring_function):
143  """Set the scoring function to be used by MonteCarlo."""
144  self._scoring_function = scoring_function
145 
146  def run_monte_carlo(self):
147  """
148  Run MonteCarlo sampling to generate possible states for DOMINO
149  """
150  t0 = time.time()
151  log.info("Running MonteCarlo")
152  mc = core.MonteCarlo(self.model)
153  mc.set_scoring_function(self._scoring_function)
154  mc.set_return_best(True)
155  for i in range(self.cycles):
156  log.info("Cycle: %s", i)
157  for iters, T, tr, rot in zip(self.iterations, self.temperatures,
158  self.max_translations,
159  self.max_rotations):
160  for rb in self.rbs:
161  log.debug("%s %s", rb.get_name(), rb.get_reference_frame())
162  self.set_movers(tr, rot)
163  mc.add_movers(self.movers)
164  mc.set_kt(T)
165  mc.optimize(iters)
166  for rb in self.rbs:
167  log.debug("%s %s", rb.get_name(), rb.get_reference_frame())
168 
169  mc.clear_movers()
170  log.info("MonteCarlo run finished. Time %s", time.time() - t0)
171 
173  """
174  Run MonteCarlo sampling to generate possible states for DOMINO
175  """
176  t0 = time.time()
177  log.info("Running MonteCarlo")
178  mc = core.MonteCarlo(self.model)
179  mc.set_scoring_function(self._scoring_function)
180  mc.set_return_best(True)
181 
182  for i in range(self.cycles):
183  log.info("Cycle: %s", i)
184  for iters, T, tr, rot in zip(self.iterations, self.temperatures,
185  self.max_translations,
186  self.max_rotations):
187  log.debug("BEFORE RELATIVE MOVERS")
188  self.set_relative_movers(tr, rot)
189  for m in self.movers:
190  m.propose()
191  m.accept()
192  mc.add_movers(self.movers)
193  mc.set_kt(T)
194  log.debug("Optimizing for %s iterations ...", iters)
195  mc.optimize(iters)
196  log.debug("Finished optimizing.")
197  mc.clear_movers()
198  log.info("MonteCarlo run finished. Time %s", time.time() - t0)
A Monte Carlo optimizer.
Definition: MonteCarlo.h:44
Restraints using electron microscopy 2D images (class averages).
Utility functions to handle representation.
def set_temperature_pattern
Set the temperature pattern to use during the Monte Carlo optimizations.
Definition: monte_carlo.py:64
Modify the transformation of a rigid body.
Class to do Monte Carlo sampling by using as the set of movements relative positions between rigid bo...
Definition: monte_carlo.py:37
Utility functions to handle IO.
Definition: io.py:1
def run_monte_carlo
Run MonteCarlo sampling to generate possible states for DOMINO.
Definition: monte_carlo.py:146
def set_relative_movers
Generate the relative models form the transforms.
Definition: monte_carlo.py:89
def set_scoring_function
Set the scoring function to be used by MonteCarlo.
Definition: monte_carlo.py:142
def run_monte_carlo_with_relative_movers
Run MonteCarlo sampling to generate possible states for DOMINO.
Definition: monte_carlo.py:172
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
def set_random_seed
initializing the random number generator manually is useful for the jobs in the cluster.
Definition: monte_carlo.py:24