1 """@namespace IMP.bayesianem
2 Restraints for handling electron microscopy maps.
5 from __future__
import print_function
20 """Fit Gaussian-decorated particles to an EM map
21 (also represented with a set of Gaussians)
22 \note This class wraps an IMP::bayesianem::GaussianEMRestraint
27 cutoff_dist_model_model=10.0,
28 cutoff_dist_model_data=10.0,
29 target_mass_scale=1.0,
31 target_radii_scale=3.0,
32 model_radii_scale=1.0,
34 spherical_gaussians=
False,
35 close_pair_container=
None,
37 scale_target_to_mass=
False,
39 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 against another one).
82 self.sigmaissampled =
True
83 self.sigmamaxtrans = 0.3
84 self.sigmamin = 0.0001
88 self.densities = densities
89 self.em_root_hier =
None
92 self.m = self.densities[0].get_model()
94 if scale_target_to_mass:
95 def hierarchy_mass(h):
98 target_mass = sum(hierarchy_mass(h)
for h
in densities)
99 print(
'will scale target mass by', target_mass_scale)
102 self._set_dataset(target_fn)
108 radius_scale=target_radii_scale,
109 mass_scale=target_mass_scale)
110 elif target_ps != []:
111 self.target_ps = target_ps
113 print(
'Gaussian EM restraint: must provide target density file '
114 'or properly set up target densities')
119 scale = target_mass / tmass
120 print(
'will set target mass to', target_mass, tmass, scale)
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, self)
130 for h
in self.densities:
132 if model_radii_scale != 1.0:
133 for p
in self.model_ps:
141 if target_is_rigid_body:
147 self.sigmaglobal = IMP.pmi.tools.SetupNuisance(
148 self.m, self.sigmainit, self.sigmamin, self.sigmamax,
149 self.sigmaissampled).get_particle()
152 print(
'target num particles', len(self.target_ps),
154 for p
in self.target_ps]))
155 print(
'model num particles', len(self.model_ps),
157 for p
in self.model_ps]))
159 update_model =
not spherical_gaussians
164 self.sigmaglobal.get_particle().
get_index(),
165 cutoff_dist_model_model,
166 cutoff_dist_model_data,
168 update_model, backbone_slope)
170 self.gaussianEM_restraint.set_density_filename(target_fn)
172 print(
'done EM setup')
174 self.rs.add_restraint(self.gaussianEM_restraint)
177 def _set_dataset(self, target_fn):
178 """Set the dataset to point to the input file"""
180 self.dataset = p.parse_file(target_fn)[
'dataset']
184 aligns the center of mass of the target GMM on the center of mass
189 for p
in self.target_ps:
192 target_com += pos * mass
194 target_com /= target_mass
195 print(
'target com', target_com)
198 for p
in self.model_ps:
201 model_com += pos * mass
203 model_com /= model_mass
204 print(
'model com', model_com)
206 v = target_com - model_com
207 print(
'translating with', -v)
209 for p
in self.target_ps:
214 '''Returns the geometric center of the GMM particles
215 @param target = True - returns target map gmm COM
216 @param target = False - returns model gmm COM'''
233 aligns the center of mass of the target GMM on the origin
236 print(
'target com', target_com)
238 print(
'model com', model_com)
239 v = target_com - model_com
240 print(
'translating with', -v)
242 for p
in self.target_ps:
248 aligns the model on the target density
249 @param input_objects IMP.pmi.representation.Representation
250 or IMP.pmi.topology.State
252 if type(input_object)
is IMP.pmi.representation.Representation:
253 hier = input_object.prot
255 hier = input_object.get_hierarchy()
257 raise Exception(
"Input must be a Representation or "
258 "topology.State object")
260 print(
'target com', target_com)
262 print(
'model com', model_com)
263 v = target_com - model_com
264 print(
'translating with', v)
277 for rb
in list(rigid_bodies):
280 for p
in list(XYZRs):
285 align the model on target GMM
288 print(
'target com', target_com)
290 print(
'model com', model_com)
291 v = target_com - model_com
292 print(
'translating with', v)
298 for p
in self.model_ps:
305 for rb
in list(rigid_bodies):
308 for p
in list(XYZRs):
313 set the weight of the restraint
317 self.rs.set_weight(weight)
319 def set_label(self, label):
322 def add_to_model(self):
325 def get_particles_to_sample(self):
327 if self.sigmaissampled:
328 ps[
"Nuisances_GaussianEMRestraint_sigma_" +
329 self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
331 ps[
"Rigid_Bodies_GaussianEMRestraint_"+self.label] = (
337 def get_rigid_body(self):
339 raise Exception(
"No rigid body created for GMM particles. "
340 "Ensure target_is_rigid_body is set to True")
345 returns a hierarchy whose leaves are the gaussian particles
348 if self.em_root_hier
is None:
351 self.em_root_hier.set_name(
352 "GaussianEMRestraint_density_" + self.label)
353 for p
in self.target_ps:
354 self.em_root_hier.add_child(p)
355 return self.em_root_hier
358 ''' Can add a target GMM to a Hierarchy.
359 For PMI2 a state object may also be passed'''
365 raise Exception(
"Can only add a density to a PMI State object "
366 "or IMP.atom.Hierarchy. You passed a", type(inp))
368 def get_restraint_set(self):
371 def get_output(self):
374 score = self.weight * self.rs.unprotected_evaluate(
None)
375 output[
"_TotalScore"] = str(score)
376 output[
"GaussianEMRestraint_" +
377 self.label] = str(score)
378 output[
"GaussianEMRestraint_sigma_" +
379 self.label] = str(self.sigmaglobal.get_scale())
383 return self.weight * self.rs.unprotected_evaluate(
None)
386 '''Writes target GMM file to MRC'''
388 fileout =
"Gaussian_map_" + self.label +
".mrc"
Support for the mmCIF file format.
def get_center_of_mass
Returns the geometric center of the GMM particles.
Restraints for handling electron microscopy maps.
A member of a rigid body, it has internal (local) coordinates.
def center_model_on_target_density
aligns the model on the target density
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Various classes to hold sets of particles.
static XYZR setup_particle(Model *m, ParticleIndex pi)
def center_target_density_on_origin
aligns the center of mass of the target GMM on the origin
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.
Creates a restraint between two Gaussian Mixture Models, "model" and "density".
Extract metadata from an EM density GMM file.
Fit Gaussian-decorated particles to an EM map (also represented with a set of Gaussians) ...
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.
def center_on_target_density
align the model on target GMM
A decorator for a particle with x,y,z coordinates.
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...
def write_target_gmm_to_mrc
Writes target GMM file to MRC.
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.
def set_weight
set the weight of the restraint
A decorator for a rigid body.
def center_target_density_on_model
aligns the center of mass of the target GMM on the center of mass of the model
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
def get_density_as_hierarchy
returns a hierarchy whose leaves are the gaussian particles of the target GMM
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.