IMP  2.0.1
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  initilazing 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 
38  """
39  Class to do Monte Carlo sampling by using as the set of movements
40  relative positions between rigid bodies
41  """
42  def __init__(self,model, rigid_bodies, anchored):
43  log.info("Setting MonteCarloRelativeMoves")
44  self.model = model
45  self.rbs = rigid_bodies
46  self.components = []
47  self.best_models = []
48  self.anchored = anchored
49  self.parent_rbs = []
50  # triplets with the information to build a relative mover using the
51  # results from docking with HEX
52  self.dock_transforms = None
53  self.non_relative_move_prob = 0.1
54 
55  log.debug("Anchored components %s",self.anchored)
56  T = alg.Transformation3D( alg.get_identity_rotation_3d(),
57  alg.Vector3D(0., 0., 0.) )
58  origin = alg.ReferenceFrame3D(T)
59  for rb in self.rbs:
60  rb.set_reference_frame(origin)
61 
62  def set_temperature_pattern(self, temperatures, iterations, cycles):
63  """
64  Set the temperature pattern to use during the Monte Carlo
65  optimizations.
66  @param temperatures List of temperature values
67  @param iterations List with the iterations to do for each of the
68  temperature.
69  @param cycles Number of repetitions of the
70  pattern of temperatures and iterations
71  """
72  self.temperatures = temperatures
73  self.iterations = iterations
74  self.cycles = cycles
75  log.debug("Temperatures %s",self.temperatures)
76  log.debug("Iterations %s",self.iterations)
77  log.debug("Cycles %s",self.cycles)
78 
79  def set_movers(self, max_translation, max_rotation):
80  log.info("Setting movers for subunits")
81  self.movers = []
82  for is_anchored, rb in zip(self.anchored, self.rbs):
83  if(not is_anchored):
84  self.movers.append(core.RigidBodyMover(rb, max_translation,
85  max_rotation))
86 
87  def set_relative_movers(self, max_translation, max_rotation):
88  """
89  Generate the relative models form the transforms. The transforms
90  is a list with triplets [id1, id2, transform_file]
91  @param max_translation Maximum translation distance allowed for
92  the moves
93  @param max_rotation Maximum rotation angle allowed for the moves
94  """
95  log.info("Setting relative movers")
96  self.movers = []
97  relative_movers = []
98  relative_names = []
99  for d in self.dock_transforms:
100  rb_id = representation.get_rb_name(d[1])
101  if rb_id not in relative_names:
102  log.debug("Checking for %s", rb_id)
103  rb_lig = representation.get_rigid_body(self.rbs, rb_id)
104  mv = em2d.RelativePositionMover(rb_lig, max_translation,
105  max_rotation)
106  relative_movers.append(mv)
107  relative_names.append(rb_id)
108  log.debug("added a RelativePositionMover for %s",rb_id)
109  i = relative_names.index(rb_id)
110  relative_movers[i].set_random_move_probability(
111  self.non_relative_move_prob)
112  rb_rec = representation.get_rigid_body(self.rbs,
113  representation.get_rb_name(d[0]))
114  log.debug("Reference added for %s: %s. ref. frame %s ",
115  rb_id, rb_rec.get_name(), rb_rec)
116  Tis = io.read_transforms(d[2])
117  relative_movers[i].add_internal_transformations(rb_rec, Tis)
118  # add regular movers for the rigid bodies that are neither moved
119  # anchored nor moved relative to others
120  regular_movers = []
121  for is_anchored, rb in zip(self.anchored, self.rbs):
122  if(not is_anchored):
123  name =rb.get_name()
124  if(not name in relative_names):
125  log.debug("adding a RigidBodyMover for %s",name)
126  mv = core.RigidBodyMover(rb, max_translation, max_rotation)
127  regular_movers.append(mv)
128  self.movers = regular_movers
129  self.movers += relative_movers
130 
131  def set_moving_parameters(self, max_translations, max_rotations):
132  self.max_translations = max_translations
133  self.max_rotations = max_rotations
134  log.debug("Maximum translations %s",self.max_translations)
135  log.debug("Maximum rotations %s",self.max_rotations)
136 
137 
138  def run_monte_carlo(self):
139  """
140  Run MonteCarlo sampling to generate possible states for DOMINO
141  """
142  t0 = time.time()
143  log.info("Running MonteCarlo")
144  mc = core.MonteCarlo(self.model)
145  mc.set_return_best(True)
146  for i in xrange(self.cycles):
147  log.info("Cycle: %s",i)
148  for iters, T, tr, rot in zip(self.iterations, self.temperatures,
149  self.max_translations, self.max_rotations):
150  for rb in self.rbs:
151  log.debug("%s %s",rb.get_name(), rb.get_reference_frame())
152  self.set_movers(tr, rot)
153  mc.add_movers( self.movers)
154  mc.set_kt(T)
155  mc.optimize(iters)
156  for rb in self.rbs:
157  log.debug("%s %s",rb.get_name(),rb.get_reference_frame())
158 
159  mc.clear_movers()
160  log.info("MonteCarlo run finished. Time %s", time.time() - t0)
161 
162 
163 
165  """
166  Run MonteCarlo sampling to generate possible states for DOMINO
167  """
168  t0 = time.time()
169  log.info("Running MonteCarlo")
170  mc = core.MonteCarlo(self.model)
171  mc.set_return_best(True)
172 
173  for i in xrange(self.cycles):
174  log.info("Cycle: %s",i)
175  for iters, T, tr, rot in zip(self.iterations, self.temperatures,
176  self.max_translations, self.max_rotations):
177  log.debug("BEFORE RELATIVE MOVERS")
178  self.set_relative_movers(tr, rot)
179  for m in self.movers:
180  m.propose()
181  mc.add_movers( self.movers)
182  mc.set_kt(T)
183  log.debug("Optimizing for %s iterations ...", iters)
184  mc.optimize(iters)
185  log.debug("Finished optimizing.")
186  mc.clear_movers()
187  log.info("MonteCarlo run finished. Time %s", time.time() - t0)