IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
mmcif/restraint.py
1 """@namespace IMP.mmcif.restraint
2  @brief Map IMP restraints to mmCIF categories
3 """
4 
6 import ihm.restraint
7 
8 
9 def _parse_restraint_info(info):
10  """Convert RestraintInfo object to Python dict"""
11  d = {}
12  if info is None:
13  return d
14  info.set_was_used(True)
15  for typ in ('int', 'float', 'string', 'filename', 'floats', 'filenames'):
16  for i in range(getattr(info, 'get_number_of_' + typ)()):
17  key = getattr(info, 'get_%s_key' % typ)(i)
18  value = getattr(info, 'get_%s_value' % typ)(i)
19  d[key] = value
20  return d
21 
22 
23 class _GaussianEMRestraint(ihm.restraint.EM3DRestraint):
24  """Handle an IMP.isd.GaussianEMRestraint"""
25 
26  def __init__(self, imp_restraint, info, modeled_assembly):
27  self._imp_restraint = imp_restraint
28  p = IMP.mmcif.metadata._GMMParser()
29  r = p.parse_file(info['filename'])
30  super(_GaussianEMRestraint, self).__init__(
31  dataset=r['dataset'],
32  assembly=modeled_assembly, # todo: fill in correct assembly
33  number_of_gaussians=r['number_of_gaussians'],
34  fitting_method='Gaussian mixture model')
35 
36  def add_model_fit(self, model):
37  info = _parse_restraint_info(self._imp_restraint.get_dynamic_info())
38  self.fits[model] = ihm.restraint.EM3DRestraintFit(
39  cross_correlation_coefficient=info['cross correlation'])
40 
41 
42 class _RestraintMapper(object):
43  """Map IMP restraints to mmCIF objects"""
44  def __init__(self, system):
45  self._typemap = {"IMP.isd.GaussianEMRestraint": _GaussianEMRestraint}
46  self._system = system
47 
48  def handle(self, r, model, modeled_assembly):
49  """Handle an individual IMP restraint.
50  @return a wrapped version of the restraint if it is handled in
51  mmCIF, otherwise None."""
52  info = _parse_restraint_info(r.get_static_info())
53  if 'type' in info and info['type'] in self._typemap:
54  r = self._typemap[info['type']](r, info, modeled_assembly)
55  r.add_model_fit(model)
56  return r
Classes to extract metadata for various input files.