1 """@namespace IMP.pmi.restraints.em
2 Restraints for handling electron microscopy maps.
5 from __future__
import print_function
21 """Fit Gaussian-decorated particles to an EM map
22 (also represented with a set of Gaussians)
23 \note This class wraps an isd::GaussianEMRestraint
28 cutoff_dist_model_model=0.0,
29 cutoff_dist_model_data=0.0,
30 target_mass_scale=1.0,
32 target_radii_scale=3.0,
33 model_radii_scale=1.0,
35 spherical_gaussians=
False,
36 close_pair_container=
None,
38 scale_target_to_mass=
False,
40 target_is_rigid_body=
False,
44 @param densities The Gaussian-decorated particles to be restrained
45 @param target_fn GMM file of the target density map
46 (alternatively, pass the ps)
47 @param target_ps List of Gaussians of the target map
48 (alternatively, pass the filename)
49 @param cutoff_dist_model_model Distance in model-model close
51 @param cutoff_dist_model_data Distance in model-data close pair
52 container. Usually can set to zero because we multiply the
54 @param target_mass_scale Scale up the target densities so that
56 Needed if the GMM you generated was not already scaled.
57 To make it the same as model mass, set scale_to_target_mass=True
58 @param target_mass Sets the mass of the target density to the given value. Default is None. This
59 will override target_mass_scale argument
60 @param target_radii_scale Scale the target density radii -
61 only used for the close pair container.
62 If you keep this at 3.0 or so you don't have to use cutoff dist.
63 @param model_radii_scale Scale the model density radii - only used
64 for the close pair container
65 @param slope Linear term added to help bring model into the density
66 @param spherical_gaussians Set to True for a speed bonus when
67 the model densities are spheres. (This means you don't have
68 to do a matrix multiplication if they rotate.)
69 @param close_pair_container Pass a close pair container for
70 the model if you already have one (e.g. for an excluded
71 volume restraint.) May give a speed bonus.
72 @param backbone_slope Only apply slope to backbone particles -
73 only matters for atomic
74 @param scale_target_to_mass Set True if you would need to scale
75 target to EXACTLY the model mass
76 @param weight The restraint weight
77 @param target_is_rigid_body Set True if you want to put the target density particles
78 into a rigid body that need to be sampled (e.g.,when you need to fit one density
79 against another one). Default is False.
80 @param local Only consider density particles that are within the
81 specified model-density cutoff (experimental)
86 self.sigmaissampled =
False
87 self.sigmamaxtrans = 0.3
92 self.densities = densities
93 self.em_root_hier =
None
96 self.m = self.densities[0].get_model()
98 if scale_target_to_mass:
99 def hierarchy_mass(h):
102 target_mass = sum(hierarchy_mass(h)
for h
in densities)
103 print(
'will set target mass to', target_mass)
104 print(
'will scale target mass by', target_mass_scale)
107 self._set_dataset(target_fn, representation)
113 radius_scale=target_radii_scale,
114 mass_scale=target_mass_scale)
115 elif target_ps != []:
116 self.target_ps = target_ps
118 print(
'Gaussian EM restraint: must provide target density file or properly set up target densities')
123 scale=target_mass/tmass
124 for p
in self.target_ps:
129 for p, state
in representation._protocol_output:
130 p.add_em3d_restraint(state, self.target_ps, self.densities,
135 for h
in self.densities:
137 if model_radii_scale != 1.0:
138 for p
in self.model_ps:
146 if target_is_rigid_body:
154 self.sigmaglobal = IMP.pmi.tools.SetupNuisance(self.m, self.sigmainit,
155 self.sigmamin, self.sigmamax,
156 self.sigmaissampled).get_particle()
159 print(
'target num particles', len(self.target_ps), \
161 for p
in self.target_ps]))
162 print(
'model num particles', len(self.model_ps), \
164 for p
in self.model_ps]))
166 update_model=
not spherical_gaussians
172 self.sigmaglobal.get_particle().
get_index(),
173 cutoff_dist_model_model,
174 cutoff_dist_model_data,
176 update_model, backbone_slope, local)
178 print(
'done EM setup')
180 self.rs.add_restraint(self.gaussianEM_restraint)
181 self.set_weight(weight)
183 def _set_dataset(self, target_fn, representation):
184 """Set the dataset to point to the input file"""
186 self.dataset = representation.get_file_dataset(target_fn)
190 details=
"Electron microscopy density map, "
191 "represented as a Gaussian Mixture "
195 m = re.match(
'(.*\.mrc)\..*\.txt$', target_fn)
196 if m
and os.path.exists(m.group(1)):
198 details=
'Original MRC file from which the GMM was derived')
201 def center_target_density_on_model(self):
204 for p
in self.target_ps:
207 target_com += pos * mass
209 target_com /= target_mass
210 print(
'target com', target_com)
213 for p
in self.model_ps:
216 model_com += pos * mass
218 model_com /= model_mass
219 print(
'model com', model_com)
221 v = target_com - model_com
222 print(
'translating with', -v)
224 for p
in self.target_ps:
229 '''Returns the geometric center of the GMM particles
230 @param target = True - returns target map gmm COM
231 @param target = False - returns model gmm COM'''
246 def center_target_density_on_origin(self):
248 print(
'target com', target_com)
250 print(
'model com', model_com)
251 v = target_com - model_com
252 print(
'translating with', -v)
254 for p
in self.target_ps:
258 def center_model_on_target_density(self, input_object):
260 hier = input_object.prot
262 hier = input_object.get_hierarchy()
264 raise Exception(
"Input must be a Representation or topology.State object")
266 print(
'target com', target_com)
268 print(
'model com', model_com)
269 v = target_com - model_com
270 print(
'translating with', v)
283 for rb
in list(rigid_bodies):
286 for p
in list(XYZRs):
289 def center_on_target_density(self):
291 print(
'target com', target_com)
293 print(
'model com', model_com)
294 v = target_com - model_com
295 print(
'translating with', v)
301 for p
in self.model_ps:
308 for rb
in list(rigid_bodies):
311 for p
in list(XYZRs):
317 def set_weight(self,weight):
319 self.rs.set_weight(weight)
321 def set_label(self, label):
324 def add_to_model(self):
327 def get_particles_to_sample(self):
329 if self.sigmaissampled:
330 ps[
"Nuisances_GaussianEMRestraint_sigma_" +
331 self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
333 ps[
"Rigid_Bodies_GaussianEMRestraint"] = (
339 def get_rigid_body(self):
341 raise Exception(
"No rigid body created for GMM particles. Ensure target_is_rigid_body is set to True")
344 def get_density_as_hierarchy(self):
345 if self.em_root_hier
is None:
347 self.em_root_hier.set_name(
"GaussianEMRestraint_density_"+self.label)
348 for p
in self.target_ps:
349 self.em_root_hier.add_child(p)
350 return self.em_root_hier
353 ''' Can add a target GMM to a Hierarchy.
354 For PMI2 a state object may also be passed'''
356 inp.get_hierarchy().add_child(self.get_density_as_hierarchy())
358 inp.add_child(self.get_density_as_hierarchy())
360 raise Exception(
"Can only add a density to a PMI State object or IMP.atom.Hierarchy. You passed a", type(inp))
362 def get_restraint_set(self):
365 def get_output(self):
368 score = self.weight * self.rs.unprotected_evaluate(
None)
369 ccc = self.gaussianEM_restraint.get_cross_correlation_coefficient()
371 output[
"_TotalScore"] = str(score)
372 output[
"GaussianEMRestraint_" +
373 self.label] = str(score)
374 output[
"GaussianEMRestraint_%s_CCC" % self.label] = ccc
375 output[
"GaussianEMRestraint_sigma_" +
376 self.label] = str(self.sigmaglobal.get_scale())
380 return self.weight * self.rs.unprotected_evaluate(
None)
383 '''Writes target GMM file to MRC'''
385 fileout=
"Gaussian_map_" + self.label +
".mrc"
393 """Fit particles to an EM map. This creates a simulate density map and updates them every eval.
394 \note Wraps an em::FitRestraint
405 @param ps The particles to restrain. Currently these must be atomic particles.
406 @param map_fn The EM density map to fit to
407 @param resolution Map resolution
408 @param origin In case you need to tell IMP the correct origin
409 @param voxel_size In case you need to tell IMP the angstroms per pixel
410 @param weight The data weight
411 @param label Extra PMI label
413 print(
'FitRestraint: setup')
415 print(
'\tresolution',resolution)
416 print(
'\tvoxel_size',voxel_size)
417 print(
'\torigin',origin)
418 print(
'\tweight',weight)
421 self.mdl = ps[0].get_model()
427 self.dmap.update_voxel_size(voxel_size)
428 if origin
is not None:
429 if type(origin)==IMP.algebra.Vector3D:
430 self.dmap.set_origin(origin)
431 elif type(origin)==list:
432 self.dmap.set_origin(*origin)
434 print(
'FitRestraint did not recognize format of origin')
438 self.rs.add_restraint(fr)
439 self.set_weight(weight)
441 def set_weight(self,weight):
443 self.rs.set_weight(weight)
445 def set_label(self, label):
448 def add_to_model(self):
451 def get_restraint_set(self):
454 def get_output(self):
457 score = self.weight * self.rs.unprotected_evaluate(
None)
458 output[
"_TotalScore"] = str(score)
459 output[
"EMRestraint_" + self.label] = str(score)
463 return self.weight * self.rs.unprotected_evaluate(
None)
468 class ElectronMicroscopy2D(object):
477 self.m = representation.prot.get_model()
484 resolution=resolution)
487 self.rs.add_restraint(em2d)
489 def set_label(self, label):
492 def add_to_model(self):
495 def get_restraint(self):
498 def set_weight(self,weight):
500 self.rs.set_weigth(self.weight)
502 def get_output(self):
505 score = self.weight*self.rs.unprotected_evaluate(
None)
506 output[
"_TotalScore"] = str(score)
507 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.
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 particles of a Model object.
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.