IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
The Integrative Modeling Platform
sampling.py
1 """@namespace IMP.EMageFit.sampling
2  Utility functions to handle sampling.
3 """
4 
5 import IMP.algebra as alg
6 import IMP.EMageFit.solutions_io as solutions_io
8 import itertools
9 import logging
10 import os
11 
12 log = logging.getLogger("sampling")
13 
14 #
15 """
16 
17  Functions required for sampling in a 2D/3D grid
18 
19 """
20 #
21 
22 
23 def create_sampling_grid_3d(diameter, n_axis_points):
24  """
25  Creates a grid of positions (Vector3Ds), centered at 0.
26  @param diameter The shape of the grid is a sphere with this diameter.
27  @param n_axis_points Number of points used along an axis for the grid.
28  The axis X Y and Z will contain n_axis_points, equispaced.
29  The other regions of space will contain only the points allowed by
30  the size of the sphere.
31  """
32  radius = diameter / 2.0
33  step = diameter / n_axis_points
34  points = [-radius + step * n for n in range(n_axis_points + 1)]
35  # filter points closer than 1A
36  points = [x for x in points if abs(x) > 1]
37  log.info("Points along the axis %s", points)
38  positions = []
39  for x, y, z in itertools.product(points, points, points):
40  d = (x ** 2. + y ** 2. + z ** 2.) ** .5
41  # allow 1% margin. Avoids approximation problems
42  if d < (1.01 * radius):
43  positions.append(alg.Vector3D(x, y, z))
44  return positions
45 
46 
47 def create_sampling_grid_2d(diameter, n_axis_points):
48  """
49  Creates a grid of positions (Vector3Ds), centered at 0.
50  The shape of the grid is a circle with diameter given by the parameter.
51  n_axis_points is the number of points along an axis: The axis X Y
52  will contain n_axis_points, equispaced. The other regions of space will
53  contain only the points allowed by the size of the circle.
54  """
55  if diameter < 0:
56  raise ValueError("create_sampling_grid_2d: Negative diameter.")
57  if n_axis_points < 1:
58  raise ValueError("create_sampling_grid_2d: Less than one axis point.")
59  radius = diameter / 2.0
60  step = diameter / n_axis_points
61  points = [-radius + step * n for n in range(n_axis_points + 1)]
62  log.info("Points along the axis %s", points)
63  positions = []
64  for x, y in itertools.product(points, points):
65  d = (x ** 2. + y ** 2.) ** .5
66  # allow 1% margin. Avoids approximation problems
67  if d < (1.01 * radius):
68  positions.append(alg.Vector3D(x, y, 0.0))
69  return positions
70 
71 
72 def get_orientations_nearby(rotation, n, f):
73  """
74  Rotations nearby a given one. They are got interpolating with
75  the rotations of the uniform coverage. The parameter f
76  (0 <= f <= 1) must be close to 0 to get orientations
77  that are close to the given orientation
78  - Values from 0.1 (tight cluster) to 0.4 (loose)
79  seem to be ok
80  n - number of rotations requested
81  """
82  log.debug("Computing nearby rotations around %s", rotation)
83  unif = alg.get_uniform_cover_rotations_3d(n)
84  id_rot = alg.get_identity_rotation_3d()
85  oris = []
86  for rot in unif:
87  r = alg.get_interpolated(rot, id_rot, f)
88  r = alg.compose(r, rotation)
89  oris.append(r)
90  return oris
91 
92 
93 class SamplingSchema:
94 
95  def __init__(self, n_components, fixed, anchored):
96  """
97  Build a set of orientations and positions for sampling with DOMINO
98  @param n_components Number of components in the assembly
99  @param fixed List of True/False values. If fixed == True
100  for a component its position is not changed.
101  @param anchor A list of True/False values. If anchor = True,
102  the component is set at (0,0,0) without rotating it.
103  """
104  if n_components < 1:
105  raise ValueError(
106  "SamplingSchema: Requesting less than 1 components.")
107 
108  self.anchored = anchored
109  self.fixed = fixed
110  self.n_components = n_components
111 
112  def get_sampling_transformations(self, i):
113  return self.transformations[i]
114 
115  def read_from_database(self, fn_database, fields=["reference_frames"],
116  max_number=False, orderby=False):
117  """
118  Read orientations and positions from a database file.
119  self.anchored and self.fixed overwrite
120  the positions and orientations read from the database
121  """
122  if not os.path.exists(fn_database):
123  raise IOError("read_from_database: Database file not found. "
124  "Are you perhaps trying to run the DOMINO "
125  "optimization without having the database yet?")
126 
128  db.connect(fn_database)
129  data = db.get_solutions(fields, max_number, orderby)
130  db.close()
131  self.transformations = [[] for T in range(self.n_components)]
132  # Each record contains a reference frame for each of the
133  # components. But here the states considered make sense as columns.
134  # Each rigidbody is considered to have all the states in a column.
135  for d in data:
136  texts = d[0].split("/")
137  for i, t in zip(range(self.n_components), texts):
138  T = io.TextToTransformation3D(t).get_transformation()
139  self.transformations[i].append(T)
140 
141  # Set the anchored components
142  for i in range(self.n_components):
143  if self.anchored[i]:
144  origin = alg.Transformation3D(alg.get_identity_rotation_3d(),
145  alg.Vector3D(0.0, 0.0, 0.))
146  self.transformations[i] = [origin]
147 
148  # If fixed, only the first state is kept
149  for i in range(self.n_components):
150  if self.fixed[i]:
151  if len(self.transformations[i]) == 0:
152  raise ValueError("There are positions to keep fixed")
153  self.transformations[i] = [self.transformations[i][0]]
Utility functions to handle IO.
Definition: io.py:1
def get_orientations_nearby
Rotations nearby a given one.
Definition: sampling.py:72
Utility functions to store and retrieve solution information.
Definition: solutions_io.py:1
Class for managing the results of the experiments.
def create_sampling_grid_3d
Creates a grid of positions (Vector3Ds), centered at 0.
Definition: sampling.py:23
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
def create_sampling_grid_2d
Creates a grid of positions (Vector3Ds), centered at 0.
Definition: sampling.py:47