IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
em.py
1 """@namespace IMP.pmi.restraints.em
2 Restraints for handling electron microscopy maps.
3 """
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.core
8 import IMP.algebra
9 import IMP.atom
10 import IMP.container
11 import IMP.isd
12 import IMP.pmi.tools
13 
14 class GaussianEMRestraint(object):
15 
16  def __init__(self, densities,
17  target_fn='',
18  target_ps=[],
19  cutoff_dist_model_model=0.0,
20  cutoff_dist_model_data=0.0,
21  overlap_threshold=0.0,
22  target_mass_scale=1.0,
23  target_radii_scale=3.0,
24  model_radii_scale=1.0,
25  slope=0.0,
26  spherical_gaussians=False,
27  pointwise_restraint=False,
28  local_mm=False,
29  close_pair_container=None,
30  backbone_slope=False,
31  scale_target_to_mass=False,
32  weight=1.0):
33  global sys, tools
34  import sys
35  import IMP.pmi.tools as tools
36  import IMP.isd.gmm_tools
37  from math import sqrt
38 
39 
40  # some parameters
41  self.label = "None"
42  self.sigmaissampled = False
43  self.sigmamaxtrans = 0.3
44  self.sigmamin = 1.0
45  self.sigmamax = 100.0
46  self.sigmainit = 2.0
47  self.label = "None"
48  self.densities = densities
49 
50  # setup target GMM
51  self.m = self.densities[0].get_model()
52  if scale_target_to_mass:
53  target_mass_scale = sum((IMP.atom.Mass(p).get_mass() for h in densities for p in IMP.atom.get_leaves(h)))
54  print('will scale target mass by', target_mass_scale)
55  if target_fn != '':
56  self.target_ps = []
58  target_fn,
59  self.target_ps,
60  self.m,
61  radius_scale=target_radii_scale,
62  mass_scale=target_mass_scale)
63  elif target_ps != []:
64  self.target_ps = target_ps
65  else:
66  print('Gaussian EM restraint: must provide target density file or properly set up target densities')
67  return
68 
69  # setup model GMM
70  self.model_ps = []
71  for h in self.densities:
72  self.model_ps += IMP.atom.get_leaves(h)
73  if model_radii_scale != 1.0:
74  for p in self.model_ps:
75  rmax = sqrt(max(IMP.core.Gaussian(p).get_variances())) * \
76  model_radii_scale
79  else:
80  IMP.core.XYZR(p).set_radius(rmax)
81 
82  # sigma particle
83  self.sigmaglobal = tools.SetupNuisance(self.m, self.sigmainit,
84  self.sigmamin, self.sigmamax,
85  self.sigmaissampled).get_particle()
86 
87  # create restraint
88  print('target num particles', len(self.target_ps), \
89  'total weight', sum([IMP.atom.Mass(p).get_mass()
90  for p in self.target_ps]))
91  print('model num particles', len(self.model_ps), \
92  'total weight', sum([IMP.atom.Mass(p).get_mass()
93  for p in self.model_ps]))
94 
95  update_model=not spherical_gaussians
96  log_score=False
97  if not pointwise_restraint:
98  self.gaussianEM_restraint = \
100  IMP.get_indexes(self.model_ps),
101  IMP.get_indexes(self.target_ps),
102  self.sigmaglobal.get_particle().get_index(),
103  cutoff_dist_model_model,
104  cutoff_dist_model_data,
105  slope,
106  update_model, backbone_slope)
107  else:
108  print('USING POINTWISE RESTRAINT - requires isd_emxl')
109  import IMP.isd_emxl
110  print('update model?',update_model)
111  self.gaussianEM_restraint = \
112  IMP.isd_emxl.PointwiseGaussianEMRestraint(self.m,
113  IMP.get_indexes(self.model_ps),
114  IMP.get_indexes(self.target_ps),
115  self.sigmaglobal.get_particle().get_index(),
116  cutoff_dist_model_model,
117  cutoff_dist_model_data,
118  slope,
119  update_model,
120  log_score,
121  close_pair_container)
122 
123  print('done EM setup')
124  self.rs = IMP.RestraintSet(self.m, 'GaussianEMRestraint')
125  self.rs.add_restraint(self.gaussianEM_restraint)
126  self.set_weight(weight)
127 
128  def center_target_density_on_model(self):
129  target_com = IMP.algebra.Vector3D(0, 0, 0)
130  target_mass = 0.0
131  for p in self.target_ps:
132  mass = IMP.atom.Mass(p).get_mass()
133  pos = IMP.core.XYZ(p).get_coordinates()
134  target_com += pos * mass
135  target_mass += mass
136  target_com /= target_mass
137  print('target com', target_com)
138  model_com = IMP.algebra.Vector3D(0, 0, 0)
139  model_mass = 0.0
140  for p in self.model_ps:
141  mass = IMP.atom.Mass(p).get_mass()
142  pos = IMP.core.XYZ(p).get_coordinates()
143  model_com += pos * mass
144  model_mass += mass
145  model_com /= model_mass
146  print('model com', model_com)
147 
148  v = target_com - model_com
149  print('translating with', -v)
151  for p in self.target_ps:
152  IMP.core.transform(IMP.core.RigidBody(p), transformation)
153  # IMP.pmi.tools.translate_hierarchies(self.densities,v)
154 
155  def center_model_on_target_density(self, representation):
156  target_com = IMP.algebra.Vector3D(0, 0, 0)
157  target_mass = 0.0
158  for p in self.target_ps:
159  mass = IMP.atom.Mass(p).get_mass()
160  pos = IMP.core.XYZ(p).get_coordinates()
161  target_com += pos * mass
162  target_mass += mass
163  target_com /= target_mass
164  print('target com', target_com)
165  model_com = IMP.algebra.Vector3D(0, 0, 0)
166  model_mass = 0.0
167  for p in self.model_ps:
168  mass = IMP.atom.Mass(p).get_mass()
169  pos = IMP.core.XYZ(p).get_coordinates()
170  model_com += pos * mass
171  model_mass += mass
172  model_com /= model_mass
173  print('model com', model_com)
174 
175  v = target_com - model_com
176  print('translating with', v)
178 
179  rigid_bodies = set()
180  XYZRs = set()
181 
182  for p in IMP.atom.get_leaves(representation.prot):
184  rb = IMP.core.RigidMember(p).get_rigid_body()
185  rigid_bodies.add(rb)
187  rb = IMP.core.RigidMember(p).get_rigid_body()
188  rigid_bodies.add(rb)
190  XYZRs.add(p)
191 
192  for rb in list(rigid_bodies):
193  IMP.core.transform(rb, transformation)
194 
195  for p in list(XYZRs):
196  IMP.core.transform(IMP.core.XYZ(p), transformation)
197 
198 
199  def set_weight(self,weight):
200  self.weight = weight
201  self.rs.set_weight(weight)
202 
203  def set_label(self, label):
204  self.label = label
205 
206  def add_to_model(self):
207  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
208 
209  def get_particles_to_sample(self):
210  ps = {}
211  if self.sigmaissampled:
212  ps["Nuisances_GaussianEMRestraint_sigma_" +
213  self.label] = ([self.sigmaglobal], self.sigmamaxtrans)
214  return ps
215 
216  def get_hierarchy(self):
217  return self.prot
218 
219  def get_density_as_hierarchy(self):
220  f=IMP.atom.Fragment().setup_particle(IMP.Particle(self.m))
221  f.set_name("GaussianEMRestraint_density_"+self.label)
222  for p in self.target_ps:
223  f.add_child(p)
224  return f
225 
226  def get_restraint_set(self):
227  return self.rs
228 
229  def get_output(self):
230  self.m.update()
231  output = {}
232  score = self.weight * self.rs.unprotected_evaluate(None)
233  output["_TotalScore"] = str(score)
234  output["GaussianEMRestraint_" +
235  self.label] = str(score)
236  output["GaussianEMRestraint_sigma_" +
237  self.label] = str(self.sigmaglobal.get_scale())
238  return output
239 
240  def evaluate(self):
241  return self.weight * self.rs.unprotected_evaluate(None)
242 
243 
244 #-------------------------------------------
245 
246 class ElectronMicroscopy2D(object):
247 
248  def __init__(
249  self,
250  representation,
251  images,
252  resolution=None):
253 
254  self.weight=1.0
255  self.m = representation.prot.get_model()
256  self.rs = IMP.RestraintSet(self.m, 'em2d')
257  self.label = "None"
258 
259  # IMP.atom.get_by_type
260  particles = IMP.pmi.tools.select(
261  representation,
262  resolution=resolution)
263 
264  em2d = None
265  self.rs.add_restraint(em2d)
266 
267  def set_label(self, label):
268  self.label = label
269 
270  def add_to_model(self):
271  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
272 
273  def get_restraint(self):
274  return self.rs
275 
276  def set_weight(self,weight):
277  self.weight=weight
278  self.rs.set_weigth(self.weight)
279 
280  def get_output(self):
281  self.m.update()
282  output = {}
283  score = self.weight*self.rs.unprotected_evaluate(None)
284  output["_TotalScore"] = str(score)
285  output["ElectronMicroscopy2D_" + self.label] = str(score)
286  return output
Tools for handling Gaussian Mixture Models.
Definition: gmm_tools.py:1
Add mass to a particle.
Definition: Mass.h:23
Simple 3D transformation class.
A decorator to associate a particle with a part of a protein/DNA/RNA.
Definition: Fragment.h:20
Various classes to hold sets of particles.
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition: XYZR.h:48
double get_mass(ResidueType c)
Get the mass from the residue type.
Miscellaneous utilities.
Definition: tools.py:1
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: rigid_bodies.h:469
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: XYZR.h:47
Object used to hold a set of restraints.
Definition: RestraintSet.h:36
ParticleIndexPairs get_indexes(const ParticlePairsTemp &ps)
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.
Creates a restraint between two Gaussian Mixture Models, "model" and "density".
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: tools.py:22
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
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...
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Class to handle individual model particles.
Definition: Particle.h:37
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: tools.py:662
A decorator for a rigid body.
Definition: rigid_bodies.h:75
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
def decorate_gmm_from_text
read the output from write_gmm_to_text, decorate as Gaussian and Mass
Definition: gmm_tools.py:22
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: rigid_bodies.h:490
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.
Definition: XYZR.h:27