2 from __future__
import print_function
7 import IMP.isd_emxl.gmm_tools
12 class EMRestraint(object):
22 """ create a FitRestraint. can provide rigid bodies instead of individual particles """
24 print(
'FitRestraint: setup')
25 print(
'\tmap_fn',map_fn)
26 print(
'\tresolution',resolution)
27 print(
'\tvoxel_size',voxel_size)
28 print(
'\torigin',origin)
29 print(
'\tweight',weight)
32 self.mdl = root.get_model()
35 dh = self.dmap.get_header()
36 dh.set_resolution(resolution)
38 self.dmap.update_voxel_size(voxel_size)
39 if type(origin)==IMP.algebra.Vector3D:
40 self.dmap.set_origin(origin)
41 elif type(origin)==list:
42 self.dmap.set_origin(*origin)
44 print(
'FitRestraint did not recognize format of origin')
52 self.rs.add_restraint(fr)
53 self.set_weight(weight)
55 def set_weight(self,weight):
57 self.rs.set_weight(weight)
59 def set_label(self, label):
62 def add_to_model(self):
63 self.mdl.add_restraint(self.rs)
65 def get_restraint_set(self):
71 score = self.weight * self.rs.unprotected_evaluate(
None)
72 output[
"_TotalScore"] = str(score)
73 output[
"EMRestraint_" + self.label] = str(score)
77 return self.weight * self.rs.unprotected_evaluate(
None)
79 class GaussianEMRestraint(object):
84 cutoff_dist_model_model=0.0,
85 cutoff_dist_model_data=0.0,
86 overlap_threshold=0.0,
87 target_mass_scale=1.0,
88 target_radii_scale=1.0,
89 model_radii_scale=1.0,
91 pointwise_restraint=
False,
103 sigmaissampled =
False
114 if model_ps
is not None:
115 self.model_ps+=model_ps
116 if len(self.model_ps)==0:
117 raise Exception(
"GaussianEM: must provide hier or model_ps")
118 self.m = self.model_ps[0].get_model()
119 print(
'will scale target mass by', target_mass_scale)
122 IMP.isd_emxl.gmm_tools.decorate_gmm_from_text(
126 radius_scale=target_radii_scale,
127 mass_scale=target_mass_scale)
129 print(
'Gaussian EM restraint: must provide target density file')
133 if orig_map_fn
is not None:
135 self.dmap.update_voxel_size(orig_map_apix)
136 dh = self.dmap.get_header()
137 dh.set_resolution(orig_map_res)
142 if orig_map_origin
is not None:
143 self.dmap.set_origin(-orig_map_origin[0]/orig_map_apix,
144 -orig_map_origin[1]/orig_map_apix,
145 -orig_map_origin[2]/orig_map_apix)
147 frscore = self.fr.unprotected_evaluate(
None)
148 print(
'init CC eval!',1.0-frscore)
152 if model_radii_scale != 1.0:
153 for p
in self.model_ps:
163 sigmaglobal = tools.SetupNuisance(self.m, sigmainit,
165 sigmaissampled).get_particle()
168 print(
'target num particles', len(target_ps), \
170 for p
in target_ps]))
171 print(
'model num particles', len(self.model_ps), \
173 for p
in self.model_ps]))
175 if not pointwise_restraint:
176 self.gaussianEM_restraint = \
178 IMP.get_indexes(self.model_ps),
179 IMP.get_indexes(target_ps),
181 cutoff_dist_model_model,
182 cutoff_dist_model_data,
186 print(
'USING POINTWISE RESTRAINT')
187 print(
'mm_container',mm_container)
188 self.gaussianEM_restraint = \
189 IMP.isd_emxl.PointwiseGaussianEMRestraint(self.m,
190 IMP.get_indexes(self.model_ps),
191 IMP.get_indexes(target_ps),
193 cutoff_dist_model_model,
194 cutoff_dist_model_data,
199 print(
'done EM setup')
201 self.rs.add_restraint(self.gaussianEM_restraint)
203 def set_weight(self,weight):
205 self.rs.set_weight(weight)
207 def set_label(self, label):
210 def add_to_model(self):
211 self.m.add_restraint(self.rs)
213 def get_restraint_set(self):
216 def get_output(self):
219 score = self.weight * self.rs.unprotected_evaluate(
None)
220 output[
"_TotalScore"] = str(score)
221 output[
"GaussianEMRestraint_" +
222 self.label] = str(score)
224 frscore = self.fr.unprotected_evaluate(
None)
225 output[
"CrossCorrelation"] = str(1.0-frscore)
231 return self.weight * self.rs.unprotected_evaluate(
None)
233 class GaussianPointRestraint(object):
243 target_mass_scale=1.0,
246 backbone_slope=
False,
252 self.cutoff_dist = cutoff_dist
257 if lmda_init<lmda_min:
262 self.m = self.model_ps[0].get_model()
265 if sigma_init
is None:
267 sigma_init = (0.42466090014400953*map_res)**2 + float(radius**2)/2
269 for p
in self.model_ps:
278 if voxel_size
is not None:
279 self.dmap.update_voxel_size(voxel_size)
280 dh = self.dmap.get_header()
281 dh.set_resolution(map_res)
282 if origin
is not None:
283 self.dmap.set_origin(-origin[0]/voxel_size,
284 -origin[1]/voxel_size,
285 -origin[2]/voxel_size)
290 frscore = self.fr.unprotected_evaluate(
None)
291 print(
'init CC eval!',1.0-frscore)
295 self.sigma = tools.SetupNuisance(self.m, sigma_init,
296 sigma_min, sigma_max,
297 isoptimized=
True).get_particle()
298 self.lmda = tools.SetupNuisance(self.m, lmda_init,
300 isoptimized=
True).get_particle()
301 self.gaussian_restraint = IMP.isd_emxl.GaussianPointRestraint(self.m,
302 IMP.get_indexes(self.model_ps),
303 self.sigma.get_particle_index(),
304 self.lmda.get_particle_index(),
309 self.rs.add_restraint(self.gaussian_restraint)
312 print(
'done EM setup')
314 def set_weight(self,weight):
316 self.rs.set_weight(weight)
318 def set_label(self, label):
321 def add_to_model(self):
322 self.m.add_restraint(self.rs)
324 def get_restraint_set(self):
327 def get_output(self):
330 score = self.weight * self.rs.unprotected_evaluate(
None)
331 output[
"_TotalScore"] = str(score)
332 output[
"GaussianPointRestraint_" +
333 self.label] = str(score)
335 frscore = self.fr.unprotected_evaluate(
None)
336 output[
"CrossCorrelation"] = str(1.0-frscore)
337 output[
"GaussianPointRestraint_lambda"]=self.lmda.get_scale()
338 output[
"GaussianPointRestraint_sigma"]=self.sigma.get_scale()
344 return self.weight * self.rs.unprotected_evaluate(
None)
346 def get_mc_sample_objects(self,max_step):
347 """ HACK! Make a SampleObjects class that can be used with PMI::samplers"""
348 ps=[[self.sigma,self.lmda],max_step]
349 return [sampling_tools.SampleObjects(
'Nuisances',ps)]
static FloatKey get_mass_key()
Ints get_index(const kernel::ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
double get_mass(ResidueType c)
Get the mass from the residue type.
void add_radii(Hierarchy d, const ForceFieldParameters *ffp=get_all_atom_CHARMM_parameters(), FloatKey radius_key=FloatKey("radius"))
Object used to hold a set of restraints.
static bool get_is_setup(const IMP::kernel::ParticleAdaptor &p)
A Gaussian distribution in 3D.
static XYZR setup_particle(kernel::Model *m, ParticleIndex pi)
Creates a restraint between two Gaussian Mixture Models, "model" and "density".
Basic utilities for handling cryo-electron microscopy 3D density maps.
A decorator for a particle with x,y,z coordinates.
DensityMap * read_map(std::string filename)
Read a density map from a file and return it.
Calculate score based on fit to EM map.
Rotation3D get_identity_rotation_3d()
Return a rotation that does not do anything.
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
Select hierarchy particles identified by the biological name.
static Gaussian setup_particle(kernel::Model *m, ParticleIndex pi)
A decorator for a particle with x,y,z coordinates and a radius.