1 """@namespace IMP.pmi.restraints.em
2 Restraints for handling electron microscopy maps.
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,
29 target_radii_scale=3.0,
30 model_radii_scale=1.0,
32 spherical_gaussians=
False,
33 close_pair_container=
None,
35 scale_target_to_mass=
False,
37 target_is_rigid_body=
False,
40 @param densities The Gaussian-decorated particles to be restrained
41 @param target_fn GMM file of the target density map
42 (alternatively, pass the ps)
43 @param target_ps List of Gaussians of the target map
44 (alternatively, pass the filename)
45 @param cutoff_dist_model_model Distance in model-model close
47 @param cutoff_dist_model_data Distance in model-data close pair
48 container. Usually can set to zero because we multiply the
50 @param target_mass_scale Scale up the target densities so that
52 Needed if the GMM you generated was not already scaled.
53 To make it the same as model mass, set scale_to_target_mass=True
54 @param target_mass Sets the mass of the target density to the given
55 value. Default is None. This will override target_mass_scale
57 @param target_radii_scale Scale the target density radii -
58 only used for the close pair container.
59 If you keep this at 3.0 or so you don't have to use cutoff dist.
60 @param model_radii_scale Scale the model density radii - only used
61 for the close pair container
62 @param slope Linear term added to help bring model into the density
63 @param spherical_gaussians Set to True for a speed bonus when
64 the model densities are spheres. (This means you don't have
65 to do a matrix multiplication if they rotate.)
66 @param close_pair_container Pass a close pair container for
67 the model if you already have one (e.g. for an excluded
68 volume restraint.) May give a speed bonus.
69 @param backbone_slope Only apply slope to backbone particles -
70 only matters for atomic
71 @param scale_target_to_mass Set True if you would need to scale
72 target to EXACTLY the model mass
73 @param weight The restraint weight
74 @param target_is_rigid_body Set True if you want to put the target
75 density particles into a rigid body that need to be sampled
76 (e.g.,when you need to fit one density
77 against another one). Default is False.
78 @param local Only consider density particles that are within the
79 specified model-density cutoff (experimental)
84 self.sigmaissampled =
False
85 self.sigmamaxtrans = 0.3
90 self.densities = densities
91 self.em_root_hier =
None
94 self.m = self.densities[0].get_model()
96 if scale_target_to_mass:
97 def hierarchy_mass(h):
100 target_mass = sum(hierarchy_mass(h)
for h
in densities)
101 print(
'will set target mass to', target_mass)
102 print(
'will scale target mass by', target_mass_scale)
105 self._set_dataset(target_fn)
108 target_fn, self.target_ps, self.m,
109 radius_scale=target_radii_scale,
110 mass_scale=target_mass_scale)
111 elif target_ps != []:
112 self.target_ps = target_ps
114 print(
'Gaussian EM restraint: must provide target density file '
115 'or properly set up target densities')
120 scale = target_mass/tmass
121 for p
in self.target_ps:
125 for p, state
in IMP.pmi.tools._all_protocol_outputs(densities[0]):
126 p.add_em3d_restraint(state, self.target_ps, self.densities,
131 for h
in self.densities:
133 if model_radii_scale != 1.0:
134 for p
in self.model_ps:
142 if target_is_rigid_body:
148 self.sigmaglobal = IMP.pmi.tools.SetupNuisance(
149 self.m, self.sigmainit, self.sigmamin, self.sigmamax,
150 self.sigmaissampled).get_particle()
153 print(
'target num particles', len(self.target_ps),
155 for p
in self.target_ps))
156 print(
'model num particles', len(self.model_ps),
158 for p
in self.model_ps))
160 update_model =
not spherical_gaussians
165 self.sigmaglobal.get_particle().
get_index(),
166 cutoff_dist_model_model,
167 cutoff_dist_model_data,
169 update_model, backbone_slope, local)
171 self.gaussianEM_restraint.set_density_filename(target_fn)
173 print(
'done EM setup')
175 self.rs.add_restraint(self.gaussianEM_restraint)
176 self.set_weight(weight)
178 def _set_dataset(self, target_fn):
179 """Set the dataset to point to the input file"""
181 self.dataset = p.parse_file(target_fn)[
'dataset']
183 def center_target_density_on_model(self):
186 for p
in self.target_ps:
189 target_com += pos * mass
191 target_com /= target_mass
192 print(
'target com', target_com)
195 for p
in self.model_ps:
198 model_com += pos * mass
200 model_com /= model_mass
201 print(
'model com', model_com)
203 v = target_com - model_com
204 print(
'translating with', -v)
206 for p
in self.target_ps:
211 '''Returns the geometric center of the GMM particles
212 @param target = True - returns target map gmm COM
213 @param target = False - returns model gmm COM'''
228 def center_target_density_on_origin(self):
230 print(
'target com', target_com)
232 print(
'model com', model_com)
233 v = target_com - model_com
234 print(
'translating with', -v)
236 for p
in self.target_ps:
240 def center_model_on_target_density(self, input_object):
241 hier = input_object.get_hierarchy()
243 print(
'target com', target_com)
245 print(
'model com', model_com)
246 v = target_com - model_com
247 print(
'translating with', v)
260 for rb
in list(rigid_bodies):
263 for p
in list(XYZRs):
266 def center_on_target_density(self):
268 print(
'target com', target_com)
270 print(
'model com', model_com)
271 v = target_com - model_com
272 print(
'translating with', v)
278 for p
in self.model_ps:
285 for rb
in list(rigid_bodies):
288 for p
in list(XYZRs):
291 def set_weight(self, weight):
293 self.rs.set_weight(weight)
295 def set_label(self, label):
298 def add_to_model(self):
301 def get_particles_to_sample(self):
303 if self.sigmaissampled:
304 ps[
"Nuisances_GaussianEMRestraint_sigma_" +
305 self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
307 ps[
"Rigid_Bodies_GaussianEMRestraint"] = (
313 def get_rigid_body(self):
315 raise Exception(
"No rigid body created for GMM particles. Ensure "
316 "target_is_rigid_body is set to True")
319 def get_density_as_hierarchy(self):
320 if self.em_root_hier
is None:
323 self.em_root_hier.set_name(
"GaussianEMRestraint_density_"
325 for p
in self.target_ps:
326 self.em_root_hier.add_child(p)
327 return self.em_root_hier
330 ''' Can add a target GMM to a Hierarchy.
331 For PMI2 a state object may also be passed'''
333 inp.get_hierarchy().add_child(self.get_density_as_hierarchy())
335 inp.add_child(self.get_density_as_hierarchy())
338 "Can only add a density to a PMI State object or "
339 "IMP.atom.Hierarchy. You passed a", type(inp))
341 def get_restraint(self):
344 def get_restraint_set(self):
347 def get_output(self):
349 score = self.weight * self.rs.unprotected_evaluate(
None)
350 ccc = self.gaussianEM_restraint.get_cross_correlation_coefficient()
352 output[
"_TotalScore"] = str(score)
353 output[
"GaussianEMRestraint_" +
354 self.label] = str(score)
355 output[
"GaussianEMRestraint_%s_CCC" % self.label] = ccc
356 output[
"GaussianEMRestraint_sigma_" +
357 self.label] = str(self.sigmaglobal.get_scale())
361 """Get the unweighted likelihood of the restraint"""
362 likelihood = self.gaussianEM_restraint.get_probability()
366 return self.weight * self.rs.unprotected_evaluate(
None)
369 '''Writes target GMM file to MRC'''
371 fileout =
"Gaussian_map_" + self.label +
".mrc"
Support for the mmCIF file format.
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.
Extract metadata from an EM density GMM file.
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)
Get the indexes from a list of particle pairs.
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.
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)
Create a decorator for the numberth copy.
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...
The general base class for IMP exceptions.
IMP::core::RigidBody create_rigid_body(Hierarchy h)
Class to handle individual particles of a Model object.
Stores a list of Molecules all with the same State index.
A decorator for a rigid body.
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
def get_likelihood
Get the unweighted likelihood of the restraint.
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.