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