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