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