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