1 """@namespace IMP.pmi.restraints.saxs
2 Restraints for handling small angle x-ray (SAXS) data.
5 from __future__
import print_function
17 class SAXSRestraint(IMP.pmi.restraints.RestraintBase):
18 _include_in_rmf =
True
20 """Basic SAXS restraint."""
22 def __init__(self, input_objects, saxs_datafile, weight=1.0,
23 ff_type=IMP.saxs.HEAVY_ATOMS, label=
None, maxq=
"standard"):
24 """Builds the restraint.
25 @param input_objects A list of hierarchies or PMI objects that the
26 SAXS restraint will be applied to. This hierarchy MUST be
27 atomic. You can pass a list of CA atom particles to evaluate
29 @param saxs_datafile the SAXS .dat file.
30 @param weight Restraint score coefficient
31 @param ff_type the form factor to use, of the following types:
32 - IMP.saxs.HEAVY_ATOMS: use form factors with implicit
34 - IMP.saxs.ALL_ATOMS: use individual form factors for all
35 atoms. Does not build missing hydrogens.
36 - IMP.saxs.CA_ATOMS: use residue based form factors
38 @param label Label for the restraint in outputs
40 @param maxq - maximum q value that the restraint will be evaluated at
41 Default vaules for ff_type = ALL_ATOMS : 0.5. HEAVY_ATOMS : 0.4,
42 CA_ATOMS and RESIDUES = 0.15. These values were eyeballed
43 by comparing ALL_ATOM calculated SAXS profiles to those calculated
44 with the reduced representations, so could be improved.
49 model = list(hiers)[0].get_model()
50 super(SAXSRestraint, self).__init__(model, label=label, weight=weight)
53 if maxq ==
"standard":
54 if ff_type == IMP.saxs.CA_ATOMS
or ff_type == IMP.saxs.RESIDUES:
56 elif ff_type == IMP.saxs.HEAVY_ATOMS:
60 elif type(maxq) == float:
61 if maxq < 0.01
or maxq > 4.0:
62 raise Exception(
"SAXSRestraint: maxq must be set between 0.01 and 4.0")
63 if (ff_type == IMP.saxs.CA_ATOMS
or ff_type == IMP.saxs.RESIDUES)
and maxq > 0.15:
64 warnings.warn(
"SAXSRestraint: for residue-resolved form "
65 "factors, a maxq > 0.15 is not recommended!",
68 raise Exception(
"SAXSRestraint: maxq must be set to a number between 0.01 and 4.0")
73 if ff_type == IMP.saxs.RESIDUES:
75 hiers, resolution=1).get_selected_particles()
77 elif ff_type == IMP.saxs.CA_ATOMS:
79 hiers, atom_type=IMP.atom.AT_CA).get_selected_particles()
81 elif ff_type == IMP.saxs.HEAVY_ATOMS
or ff_type == IMP.saxs.ALL_ATOMS:
83 hiers, resolution=0).get_selected_particles()
86 raise Exception(
"SAXSRestraint: Must provide an IMP.saxs atom type: RESIDUES, CA_ATOMS, HEAVY_ATOMS or ALL_ATOMS")
88 if len(self.particles) == 0:
89 raise Exception(
"SAXSRestraint: There are no selected particles")
93 self.rs.add_restraint(self.restraint)
98 """Basic SAXS restraint using ISD."""
104 print(
"Module isd2 not installed. Cannot use SAXSISDRestraint")
106 def __init__(self, representation, profile, resolution=0, weight=1,
107 ff_type=IMP.saxs.HEAVY_ATOMS, label=
None):
109 model = representation.prot.get_model()
110 super(SAXSISDRestraint, self).__init__(model, label=label,
113 self.taumaxtrans = 0.05
116 self.atoms = IMP.pmi.tools.select(
118 resolution=resolution)
121 self.gamma = IMP.pmi.tools.SetupNuisance(
122 self.model, 1., 0.,
None,
False).get_particle()
125 self.sigma = IMP.pmi.tools.SetupNuisance(self.model, 10.0, 0.,
None,
False
129 self.tau = IMP.pmi.tools.SetupNuisance(self.model, 1., 0.,
None,
False,
133 self.c1 = IMP.pmi.tools.SetupNuisance(self.model, 1.0, 0.95, 1.05,
135 self.c2 = IMP.pmi.tools.SetupNuisance(self.model, 0.0, -2., 4.,
139 self.w = IMP.pmi.tools.SetupWeight(self.model).get_particle()
143 self.cov = [[1
if i == j
else 0
for j
in range(self.prof.size())]
144 for i
in range(self.prof.size())]
146 print(
"create saxs restraint")
147 self.saxs = IMP.isd2.SAXSRestraint(self.prof, self.sigma, self.tau,
148 self.gamma, self.w, self.c1,
150 self.saxs.add_scatterer(self.atoms, self.cov, ff_type)
152 self.rs.add_restraint(self.saxs)
157 self.rs2 = self._create_restraint_set(
'Prior')
160 self.rs2.add_restraint(j1)
162 self.rs2.add_restraint(j2)
164 self.rs2.add_restraint(j3)
167 pw.set_weights(pw.get_unit_simplex().get_barycenter())
168 pw.set_weights_are_optimized(
True)
171 """Set sigma to the value that maximizes its conditional likelihood"""
173 sigma2hat = self.saxs.get_sigmasq_scale_parameter() \
174 / (self.saxs.get_sigmasq_shape_parameter() + 1)
178 """Set gamma to the value that maximizes its conditional likelihood"""
180 gammahat = math.exp(self.saxs.get_loggamma_variance_parameter() *
181 self.saxs.get_loggamma_jOg_parameter())
184 def optimize_tau(self, ltaumin=-2, ltaumax=3, npoints=100):
188 fl = open(
'tauvals.txt',
'w')
189 for tauval
in self._logspace(ltaumin, ltaumax, npoints):
192 values.append((self.model.evaluate(
False), tauval))
195 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
197 ltcenter = math.log(values[0][1]) / math.log(10)
198 spacing = (ltaumax - ltaumin) / float(npoints)
200 for tauval
in self._logspace(
201 ltcenter - 2 * spacing, ltcenter + 2 * spacing,
204 values.append((self.model.evaluate(
False), tauval))
205 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
210 """Get value of gamma."""
211 return self.gamma.get_scale()
213 def set_taumaxtrans(self, taumaxtrans):
214 self.taumaxtrans = taumaxtrans
217 """Draw 1/sigma2 from gamma distribution."""
219 self.saxs.draw_sigma()
222 """Draw gamma from lognormal distribution."""
224 self.saxs.draw_gamma()
226 def update_covariance_matrix(self):
231 self.cov = IMP.isd2.compute_relative_covariance(self.atoms, c1, c2,
236 self.saxs.set_cov(0, self.cov)
238 def write_covariance_matrix(self, fname):
239 fl = open(fname,
'w')
240 for line
in self.cov:
245 def get_output(self):
246 output = super(SAXSISDRestraint, self).get_output()
247 suffix = self._get_label_suffix()
248 output[
"SAXSISDRestraint_Sigma" +
249 suffix] = str(self.sigma.get_scale())
250 output[
"SAXSISDRestraint_Tau" + suffix] = str(self.tau.get_scale())
251 output[
"SAXSISDRestraint_Gamma" +
252 suffix] = str(self.gamma.get_scale())
256 def _logspace(a, b, num=100):
257 """Mimick numpy's logspace function"""
259 val = a + float(b - a) / float(num - 1) * i
Add weights to a particle.
Various classes to hold sets of particles.
def optimize_gamma
Set gamma to the value that maximizes its conditional likelihood.
Calculate score based on fit to SAXS profile.
void write_pdb(const Selection &mhd, TextOutput out, unsigned int model=1)
Add scale parameter to particle.
Classes to handle different kinds of restraints.
def optimize_sigma
Set sigma to the value that maximizes its conditional likelihood.
def draw_gamma
Draw gamma from lognormal distribution.
Add nuisance parameter to particle.
Basic SAXS restraint using ISD.
def get_gamma_value
Get value of gamma.
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...
The general base class for IMP exceptions.
def draw_sigma
Draw 1/sigma2 from gamma distribution.
Functionality for loading, creating, manipulating and scoring atomic structures.
Select hierarchy particles identified by the biological name.
Support for small angle X-ray scattering (SAXS) data.
Warning for probably incorrect input parameters.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...