5 import IMP.em2d.imp_general.io
as io
11 log = logging.getLogger(
"sampling")
16 Functions required for sampling in a 2D/3D grid
22 def create_sampling_grid_3d(diameter, n_axis_points):
24 Creates a grid of positions (Vector3Ds), centered at 0.
25 @param diameter The shape of the grid is a sphere with this diameter.
26 @param n_axis_points Number of points used alogn an axis for the grid.
27 The axis X Y and Z will contain n_axis_points, equispaced.
28 The other regions of space will contain only the points allowed by
29 the size of the spere.
32 step = diameter/n_axis_points
33 points = [-radius + step*n
for n
in range(n_axis_points +1 )]
34 points = filter(
lambda x: abs(x) > 1, points)
35 log.info(
"Points along the axis %s", points)
37 for x, y, z
in itertools.product(points, points, points):
38 d = (x**2. + y**2. + z**2.)**.5
39 if(d < (1.01 *radius)):
40 positions.append( alg.Vector3D(x,y,z))
44 def create_sampling_grid_2d(diameter, n_axis_points):
46 Creates a grid of positions (Vector3Ds), centered at 0.
47 The shape of the grid is a circle with diameter given by the parameter.
48 n_axis_points is the number of points along an axis: The axis X Y
49 will contain n_axis_points, equispaced. The other regions of space will
50 contain only the points allowed by the size of the circle.
53 raise ValueError(
"create_sampling_grid_2d: Negative diameter.")
54 if(n_axis_points < 1 ):
55 raise ValueError(
"create_sampling_grid_2d: Less than one axis point.")
57 step = diameter/n_axis_points
58 points = [-radius + step*n
for n
in range(n_axis_points +1 )]
59 log.info(
"Points along the axis %s", points)
61 for x, y
in itertools.product(points, points):
62 d = (x**2. + y**2.)**.5
63 if(d < (1.01 *radius)):
64 positions.append( alg.Vector3D(x,y, 0.0))
68 def get_orientations_nearby(rotation, n, f):
70 Rotations nearby a given one. They are got intepolating with
71 the rotations of the uniform coverage. The parameter f
72 (0 <= f <= 1) must be close to 0 to get orientations
73 that are close to the given orientation
74 - Values from 0.1 (tight cluster) to 0.4 (loose)
76 n - number of rotations requested
78 log.debug(
"Computing nearby rotations around %s", rotation)
79 unif = alg.get_uniform_cover_rotations_3d(n)
80 id_rot = alg.get_identity_rotation_3d()
83 r = alg.get_interpolated(rot, id_rot, f)
84 r = alg.compose(r, rotation )
92 def __init__(self, n_components, fixed, anchored):
94 Build a set of orientations and positions for sampling with DOMINO
95 @param n_components Number of components in the assembly
96 @param fixed List of True/False values. If fixed == True
97 for a component its position is not changed.
98 @param anchor A list of True/False values. If anchor = True,
99 the component is set at (0,0,0) without rotating it.
101 if(n_components < 1 ):
102 raise ValueError(
"SamplingSchema: Requesting less than 1 components.")
104 self.anchored = anchored
106 self.n_components = n_components
108 def get_sampling_transformations(self, i):
109 return self.transformations[i]
111 def read_from_database(self, fn_database, fields=["reference_frames"],
112 max_number=
False, orderby=
False ):
114 Read orientations and positions from a database file.
115 self.anchored and self.fixed overwrite
116 the positions and orientations read from the database
118 if not os.path.exists(fn_database):
119 raise IOError(
"read_from_database: Database file not found. " \
120 "Are you perhaps trying to run the DOMINO optimization without " \
121 "having the database yet?")
124 db = solutions_io.ResultsDB()
125 db.connect(fn_database)
126 data = db.get_solutions(fields, max_number, orderby)
128 self.transformations = [ []
for T
in range(self.n_components)]
133 texts = d[0].split(
"/")
134 for i, t
in zip(range(self.n_components), texts):
135 T = io.TextToTransformation3D(t).get_transformation()
136 self.transformations[i].append(T)
141 for i
in range(self.n_components):
143 origin = alg.Transformation3D(alg.get_identity_rotation_3d(),
144 alg.Vector3D(0.0, 0.0, 0.))
145 self.transformations[i] = [origin]
148 for i
in range(self.n_components):
150 if len(self.transformations[i]) == 0:
151 raise ValueError(
"There are positions to keep fixed")
152 self.transformations[i] = [self.transformations[i][0]]