1 """@namespace IMP.pmi.restraints.em
2 Restraints for handling electron microscopy maps.
5 from __future__
import print_function
18 """Fit Gaussian-decorated particles to an EM map
19 (also represented with a set of Gaussians)
20 \note This class wraps an isd::GaussianEMRestraint
25 cutoff_dist_model_model=0.0,
26 cutoff_dist_model_data=0.0,
27 target_mass_scale=1.0,
28 target_radii_scale=3.0,
29 model_radii_scale=1.0,
31 spherical_gaussians=
False,
32 close_pair_container=
None,
34 scale_target_to_mass=
False,
36 target_is_rigid_body=
False):
38 @param densities The Gaussian-decorated particles to be restrained
39 @param target_fn GMM file of the target density map
40 (alternatively, pass the ps)
41 @param target_ps List of Gaussians of the target map
42 (alternatively, pass the filename)
43 @param cutoff_dist_model_model Distance in model-model close
45 @param cutoff_dist_model_data Distance in model-data close pair
46 container. Usually can set to zero because we multiply the
48 @param target_mass_scale Scale up the target densities so that
50 Needed if the GMM you generated was not already scaled.
51 To make it the same as model mass, set scale_to_target_mass=True
52 @param target_radii_scale Scale the target density radii -
53 only used for the close pair container.
54 If you keep this at 3.0 or so you don't have to use cutoff dist.
55 @param model_radii_scale Scale the model density radii - only used
56 for the close pair container
57 @param slope Linear term added to help bring model into the density
58 @param spherical_gaussians Set to True for a speed bonus when
59 the model densities are spheres. (This means you don't have
60 to do a matrix multiplication if they rotate.)
61 @param close_pair_container Pass a close pair container for
62 the model if you already have one (e.g. for an excluded
63 volume restraint.) May give a speed bonus.
64 @param backbone_slope Only apply slope to backbone particles -
65 only matters for atomic
66 @param scale_target_to_mass Set True if you would need to scale
67 target to EXACTLY the model mass
68 @param weight The restraint weight
69 @param target_is_rigid_body Set True if you want to put the target density particles
70 into a rigid body that need to be sampled (e.g.,when you need to fit one density
71 against another one). Default is False.
76 self.sigmaissampled =
False
77 self.sigmamaxtrans = 0.3
82 self.densities = densities
83 self.em_root_hier =
None
86 self.m = self.densities[0].get_model()
87 if scale_target_to_mass:
88 def hierarchy_mass(h):
91 target_mass_scale = sum(hierarchy_mass(h)
for h
in densities)
92 print(
'will scale target mass by', target_mass_scale)
100 radius_scale=target_radii_scale,
101 mass_scale=target_mass_scale)
102 elif target_ps != []:
103 self.target_ps = target_ps
105 print(
'Gaussian EM restraint: must provide target density file or properly set up target densities')
110 for h
in self.densities:
112 if model_radii_scale != 1.0:
113 for p
in self.model_ps:
121 if target_is_rigid_body:
129 self.sigmaglobal = IMP.pmi.tools.SetupNuisance(self.m, self.sigmainit,
130 self.sigmamin, self.sigmamax,
131 self.sigmaissampled).get_particle()
134 print(
'target num particles', len(self.target_ps), \
136 for p
in self.target_ps]))
137 print(
'model num particles', len(self.model_ps), \
139 for p
in self.model_ps]))
141 update_model=
not spherical_gaussians
147 self.sigmaglobal.get_particle().
get_index(),
148 cutoff_dist_model_model,
149 cutoff_dist_model_data,
151 update_model, backbone_slope)
153 print(
'done EM setup')
155 self.rs.add_restraint(self.gaussianEM_restraint)
156 self.set_weight(weight)
158 def center_target_density_on_model(self):
161 for p
in self.target_ps:
164 target_com += pos * mass
166 target_com /= target_mass
167 print(
'target com', target_com)
170 for p
in self.model_ps:
173 model_com += pos * mass
175 model_com /= model_mass
176 print(
'model com', model_com)
178 v = target_com - model_com
179 print(
'translating with', -v)
181 for p
in self.target_ps:
186 '''Returns the geometric center of the GMM particles
187 @param target = True - returns target map gmm COM
188 @param target = False - returns model gmm COM'''
203 def center_target_density_on_origin(self):
205 print(
'target com', target_com)
207 print(
'model com', model_com)
208 v = target_com - model_com
209 print(
'translating with', -v)
211 for p
in self.target_ps:
215 def center_model_on_target_density(self, input_object):
217 hier = input_object.prot
219 hier = input_object.get_hierarchy()
221 raise Exception(
"Input must be a Representation or topology.State object")
223 print(
'target com', target_com)
225 print(
'model com', model_com)
226 v = target_com - model_com
227 print(
'translating with', v)
240 for rb
in list(rigid_bodies):
243 for p
in list(XYZRs):
247 def set_weight(self,weight):
249 self.rs.set_weight(weight)
251 def set_label(self, label):
254 def add_to_model(self):
257 def get_particles_to_sample(self):
259 if self.sigmaissampled:
260 ps[
"Nuisances_GaussianEMRestraint_sigma_" +
261 self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
263 ps[
"Rigid_Bodies_GaussianEMRestraint"] = (
269 def get_rigid_body(self):
271 raise Exception(
"No rigid body created for GMM particles. Ensure target_is_rigid_body is set to True")
274 def get_density_as_hierarchy(self):
275 if self.em_root_hier
is None:
277 self.em_root_hier.set_name(
"GaussianEMRestraint_density_"+self.label)
278 for p
in self.target_ps:
279 self.em_root_hier.add_child(p)
280 return self.em_root_hier
283 ''' Can add a target GMM to a Hierarchy.
284 For PMI2 a state object may also be passed'''
286 inp.get_hierarchy().add_child(self.get_density_as_hierarchy())
288 inp.add_child(self.get_density_as_hierarchy())
290 raise Exception(
"Can only add a density to a PMI State object or IMP.atom.Hierarchy. You passed a", type(inp))
292 def get_restraint_set(self):
295 def get_output(self):
298 score = self.weight * self.rs.unprotected_evaluate(
None)
299 output[
"_TotalScore"] = str(score)
300 output[
"GaussianEMRestraint_" +
301 self.label] = str(score)
302 output[
"GaussianEMRestraint_sigma_" +
303 self.label] = str(self.sigmaglobal.get_scale())
307 return self.weight * self.rs.unprotected_evaluate(
None)
310 '''Writes target GMM file to MRC'''
312 fileout=
"Gaussian_map_" + self.label +
".mrc"
320 """Fit particles to an EM map. This creates a simulate density map and updates them every eval.
321 \note Wraps an em::FitRestraint
332 @param ps The particles to restrain. Currently these must be atomic particles.
333 @param map_fn The EM density map to fit to
334 @param resolution Map resolution
335 @param origin In case you need to tell IMP the correct origin
336 @param voxel_size In case you need to tell IMP the angstroms per pixel
337 @param weight The data weight
338 @param label Extra PMI label
340 print(
'FitRestraint: setup')
342 print(
'\tresolution',resolution)
343 print(
'\tvoxel_size',voxel_size)
344 print(
'\torigin',origin)
345 print(
'\tweight',weight)
348 self.mdl = ps[0].get_model()
354 self.dmap.update_voxel_size(voxel_size)
355 if origin
is not None:
356 if type(origin)==IMP.algebra.Vector3D:
357 self.dmap.set_origin(origin)
358 elif type(origin)==list:
359 self.dmap.set_origin(*origin)
361 print(
'FitRestraint did not recognize format of origin')
365 self.rs.add_restraint(fr)
366 self.set_weight(weight)
368 def set_weight(self,weight):
370 self.rs.set_weight(weight)
372 def set_label(self, label):
375 def add_to_model(self):
378 def get_restraint_set(self):
381 def get_output(self):
384 score = self.weight * self.rs.unprotected_evaluate(
None)
385 output[
"_TotalScore"] = str(score)
386 output[
"EMRestraint_" + self.label] = str(score)
390 return self.weight * self.rs.unprotected_evaluate(
None)
395 class ElectronMicroscopy2D(object):
404 self.m = representation.prot.get_model()
411 resolution=resolution)
414 self.rs.add_restraint(em2d)
416 def set_label(self, label):
419 def add_to_model(self):
422 def get_restraint(self):
425 def set_weight(self,weight):
427 self.rs.set_weigth(self.weight)
429 def get_output(self):
432 score = self.weight*self.rs.unprotected_evaluate(
None)
433 output[
"_TotalScore"] = str(score)
434 output[
"ElectronMicroscopy2D_" + self.label] = str(score)
Fit Gaussian-decorated particles to an EM map (also represented with a set of Gaussians) ...
A member of a rigid body, it has internal (local) coordinates.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
def write_target_gmm_to_mrc
Writes target GMM file to MRC.
Various classes to hold sets of particles.
static XYZR setup_particle(Model *m, ParticleIndex pi)
double get_mass(ResidueType c)
Get the mass from the residue type.
def add_target_density_to_hierarchy
Can add a target GMM to a Hierarchy.
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Object used to hold a set of restraints.
ParticleIndexPairs get_indexes(const ParticlePairsTemp &ps)
The standard decorator for manipulating molecular structures.
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
void transform(XYZ a, const algebra::Transformation3D &tr)
Apply a transformation to the particle.
Creates a restraint between two Gaussian Mixture Models, "model" and "density".
A decorator for a particle with x,y,z coordinates.
def get_center_of_mass
Returns the geometric center of the GMM particles.
static Copy setup_particle(Model *m, ParticleIndex pi, Int number)
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...
Calculate score based on fit to EM map.
The general base class for IMP exceptions.
IMP::core::RigidBody create_rigid_body(Hierarchy h)
Class to handle individual model particles.
Stores a list of Molecules all with the same State index.
A decorator for a rigid body.
Set up the representation of all proteins and nucleic acid macromolecules.
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
Fit particles to an EM map.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...
A decorator for a particle with x,y,z coordinates and a radius.