1 """@namespace IMP.pmi.restraints.em
2 Restraints for handling electron microscopy maps.
5 from __future__
import print_function
19 """Fit Gaussian-decorated particles to an EM map
20 (also represented with a set of Gaussians)
21 @note This class wraps an isd::GaussianEMRestraint
26 cutoff_dist_model_model=0.0,
27 cutoff_dist_model_data=0.0,
28 target_mass_scale=1.0,
30 target_radii_scale=3.0,
31 model_radii_scale=1.0,
33 spherical_gaussians=
False,
34 close_pair_container=
None,
36 scale_target_to_mass=
False,
38 target_is_rigid_body=
False,
41 @param densities The Gaussian-decorated particles to be restrained
42 @param target_fn GMM file of the target density map
43 (alternatively, pass the ps)
44 @param target_ps List of Gaussians of the target map
45 (alternatively, pass the filename)
46 @param cutoff_dist_model_model Distance in model-model close
48 @param cutoff_dist_model_data Distance in model-data close pair
49 container. Usually can set to zero because we multiply the
51 @param target_mass_scale Scale up the target densities so that
53 Needed if the GMM you generated was not already scaled.
54 To make it the same as model mass, set scale_to_target_mass=True
55 @param target_mass Sets the mass of the target density to the given
56 value. Default is None. This will override target_mass_scale
58 @param target_radii_scale Scale the target density radii -
59 only used for the close pair container.
60 If you keep this at 3.0 or so you don't have to use cutoff dist.
61 @param model_radii_scale Scale the model density radii - only used
62 for the close pair container
63 @param slope Linear term added to help bring model into the density
64 @param spherical_gaussians Set to True for a speed bonus when
65 the model densities are spheres. (This means you don't have
66 to do a matrix multiplication if they rotate.)
67 @param close_pair_container Pass a close pair container for
68 the model if you already have one (e.g. for an excluded
69 volume restraint.) May give a speed bonus.
70 @param backbone_slope Only apply slope to backbone particles -
71 only matters for atomic
72 @param scale_target_to_mass Set True if you would need to scale
73 target to EXACTLY the model mass
74 @param weight The restraint weight
75 @param target_is_rigid_body Set True if you want to put the target
76 density particles into a rigid body that need to be sampled
77 (e.g.,when you need to fit one density
78 against another one). Default is False.
79 @param local Only consider density particles that are within the
80 specified model-density cutoff (experimental)
85 self.sigmaissampled =
False
86 self.sigmamaxtrans = 0.3
91 self.densities = densities
92 self.em_root_hier =
None
95 self.m = self.densities[0].get_model()
97 if scale_target_to_mass:
98 def hierarchy_mass(h):
101 target_mass = sum(hierarchy_mass(h)
for h
in densities)
102 print(
'will set target mass to', target_mass)
103 print(
'will scale target mass by', target_mass_scale)
106 self._set_dataset(target_fn)
109 target_fn, self.target_ps, self.m,
110 radius_scale=target_radii_scale,
111 mass_scale=target_mass_scale)
112 elif target_ps != []:
113 self.target_ps = target_ps
115 print(
'Gaussian EM restraint: must provide target density file '
116 'or properly set up target densities')
121 scale = target_mass/tmass
122 for p
in self.target_ps:
126 for p, state
in IMP.pmi.tools._all_protocol_outputs(densities[0]):
127 p.add_em3d_restraint(state, self.target_ps, self.densities,
132 for h
in self.densities:
134 if model_radii_scale != 1.0:
135 for p
in self.model_ps:
143 if target_is_rigid_body:
149 self.sigmaglobal = IMP.pmi.tools.SetupNuisance(
150 self.m, self.sigmainit, self.sigmamin, self.sigmamax,
151 self.sigmaissampled).get_particle()
154 print(
'target num particles', len(self.target_ps),
156 for p
in self.target_ps))
157 print(
'model num particles', len(self.model_ps),
159 for p
in self.model_ps))
161 update_model =
not spherical_gaussians
166 self.sigmaglobal.get_particle().
get_index(),
167 cutoff_dist_model_model,
168 cutoff_dist_model_data,
170 update_model, backbone_slope, local)
172 self.gaussianEM_restraint.set_density_filename(target_fn)
174 print(
'done EM setup')
176 self.rs.add_restraint(self.gaussianEM_restraint)
177 self.set_weight(weight)
179 def _set_dataset(self, target_fn):
180 """Set the dataset to point to the input file"""
182 self.dataset = p.parse_file(target_fn)[
'dataset']
184 def center_target_density_on_model(self):
187 for p
in self.target_ps:
190 target_com += pos * mass
192 target_com /= target_mass
193 print(
'target com', target_com)
196 for p
in self.model_ps:
199 model_com += pos * mass
201 model_com /= model_mass
202 print(
'model com', model_com)
204 v = target_com - model_com
205 print(
'translating with', -v)
207 for p
in self.target_ps:
212 '''Returns the geometric center of the GMM particles
213 @param target = True - returns target map gmm COM
214 @param target = False - returns model gmm COM'''
229 def center_target_density_on_origin(self):
231 print(
'target com', target_com)
233 print(
'model com', model_com)
234 v = target_com - model_com
235 print(
'translating with', -v)
237 for p
in self.target_ps:
241 def center_model_on_target_density(self, input_object):
242 hier = input_object.get_hierarchy()
244 print(
'target com', target_com)
246 print(
'model com', model_com)
247 v = target_com - model_com
248 print(
'translating with', v)
261 for rb
in list(rigid_bodies):
264 for p
in list(XYZRs):
267 def center_on_target_density(self):
269 print(
'target com', target_com)
271 print(
'model com', model_com)
272 v = target_com - model_com
273 print(
'translating with', v)
279 for p
in self.model_ps:
286 for rb
in list(rigid_bodies):
289 for p
in list(XYZRs):
292 def set_weight(self, weight):
294 self.rs.set_weight(weight)
296 def set_label(self, label):
299 def add_to_model(self):
302 def get_particles_to_sample(self):
304 if self.sigmaissampled:
305 ps[
"Nuisances_GaussianEMRestraint_sigma_" +
306 self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
308 ps[
"Rigid_Bodies_GaussianEMRestraint"] = (
314 def get_rigid_body(self):
316 raise Exception(
"No rigid body created for GMM particles. Ensure "
317 "target_is_rigid_body is set to True")
320 def get_density_as_hierarchy(self):
321 if self.em_root_hier
is None:
324 self.em_root_hier.set_name(
"GaussianEMRestraint_density_"
326 for p
in self.target_ps:
327 self.em_root_hier.add_child(p)
328 return self.em_root_hier
331 ''' Can add a target GMM to a Hierarchy.
332 For PMI2 a state object may also be passed'''
334 inp.get_hierarchy().add_child(self.get_density_as_hierarchy())
336 inp.add_child(self.get_density_as_hierarchy())
339 "Can only add a density to a PMI State object or "
340 "IMP.atom.Hierarchy. You passed a", type(inp))
342 def get_restraint(self):
345 def get_restraint_set(self):
348 def get_output(self):
350 score = self.weight * self.rs.unprotected_evaluate(
None)
351 ccc = self.gaussianEM_restraint.get_cross_correlation_coefficient()
353 output[
"_TotalScore"] = str(score)
354 output[
"GaussianEMRestraint_" +
355 self.label] = str(score)
356 output[
"GaussianEMRestraint_%s_CCC" % self.label] = ccc
357 output[
"GaussianEMRestraint_sigma_" +
358 self.label] = str(self.sigmaglobal.get_scale())
362 """Get the unweighted likelihood of the restraint"""
363 likelihood = self.gaussianEM_restraint.get_probability()
367 return self.weight * self.rs.unprotected_evaluate(
None)
370 '''Writes target GMM file to MRC'''
372 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.