1 """@namespace IMP.pmi.restraints.saxs
2 Restraints for handling small angle x-ray (SAXS) data.
5 from __future__
import print_function
19 """Basic SAXS restraint."""
21 def __init__(self, input_objects, saxs_datafile, weight=1.0,
22 ff_type=IMP.saxs.HEAVY_ATOMS, label=
None):
23 """Builds the restraint.
24 @param input_objects A list of hierarchies or PMI objects that the
25 SAXS restraint will be applied to. This hierarchy MUST be
26 atomic. You can pass a list of CA atom particles to evaluate
28 @param saxs_datafile the SAXS .dat file.
29 @param weight Restraint score coefficient
30 @param ff_type the form factor to use, of the following types:
31 - IMP.saxs.HEAVY_ATOMS: use form factors with implicit
33 - IMP.saxs.ALL_ATOMS: use individual form factors for all
34 atoms. Does not build missing hydrogens.
35 - IMP.saxs.CA_ATOMS: use residue based form factors
37 @param label Label for the restraint in outputs
41 m = list(hiers)[0].get_model()
42 super(SAXSRestraint, self).
__init__(m, label=label, weight=weight)
45 if ff_type == IMP.saxs.CA_ATOMS:
47 hiers, atom_type=IMP.atom.AT_CA).get_selected_particles()
48 elif ff_type == IMP.saxs.HEAVY_ATOMS:
50 hiers, resolution=0).get_selected_particles()
51 elif ff_type == IMP.saxs.ALL_ATOMS:
53 hiers, resolution=0).get_selected_particles()
55 raise Exception(
"SAXSRestraint: Must provide an IMP.saxs atom "
56 "type: CA_ATOMS, HEAVY_ATOMS or ALL_ATOMS")
57 if len(self.particles) == 0:
58 raise Exception(
"SAXSRestraint: There are no selected particles")
62 self.rs.add_restraint(self.restraint)
67 """Basic SAXS restraint using ISD."""
73 print(
"Module isd2 not installed. Cannot use SAXSISDRestraint")
75 def __init__(self, representation, profile, resolution=0, weight=1,
76 ff_type=IMP.saxs.HEAVY_ATOMS, label=
None):
78 m = representation.prot.get_model()
79 super(SAXSISDRestraint, self).
__init__(m, label=label, weight=weight)
81 self.taumaxtrans = 0.05
86 resolution=resolution)
89 self.gamma = IMP.pmi.tools.SetupNuisance(
90 self.m, 1., 0.,
None,
False).get_particle()
93 self.sigma = IMP.pmi.tools.SetupNuisance(self.m, 10.0, 0.,
None,
False
97 self.tau = IMP.pmi.tools.SetupNuisance(self.m, 1., 0.,
None,
False,
101 self.c1 = IMP.pmi.tools.SetupNuisance(self.m, 1.0, 0.95, 1.05,
103 self.c2 = IMP.pmi.tools.SetupNuisance(self.m, 0.0, -2., 4.,
107 self.w = IMP.pmi.tools.SetupWeight(self.m).get_particle()
111 self.cov = [[1
if i == j
else 0
for j
in range(self.prof.size())]
112 for i
in range(self.prof.size())]
114 print(
"create saxs restraint")
115 self.saxs = IMP.isd2.SAXSRestraint(self.prof, self.sigma, self.tau,
116 self.gamma, self.w, self.c1,
118 self.saxs.add_scatterer(self.atoms, self.cov, ff_type)
120 self.rs.add_restraint(self.saxs)
125 self.rs2 = self._create_restraint_set(
'Prior')
128 self.rs2.add_restraint(j1)
130 self.rs2.add_restraint(j2)
132 self.rs2.add_restraint(j3)
135 """Set sigma to the value that maximizes its conditional likelihood"""
137 sigma2hat = self.saxs.get_sigmasq_scale_parameter() \
138 / (self.saxs.get_sigmasq_shape_parameter() + 1)
142 """Set gamma to the value that maximizes its conditional likelihood"""
144 gammahat = math.exp(self.saxs.get_loggamma_variance_parameter() *
145 self.saxs.get_loggamma_jOg_parameter())
148 def optimize_tau(self, ltaumin=-2, ltaumax=3, npoints=100):
152 fl = open(
'tauvals.txt',
'w')
153 for tauval
in self._logspace(ltaumin, ltaumax, npoints):
156 values.append((self.m.evaluate(
False), tauval))
159 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
161 ltcenter = math.log(values[0][1]) / math.log(10)
162 spacing = (ltaumax - ltaumin) / float(npoints)
164 for tauval
in self._logspace(
165 ltcenter - 2 * spacing, ltcenter + 2 * spacing,
168 values.append((self.m.evaluate(
False), tauval))
169 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
174 """Get value of gamma."""
175 return self.gamma.get_scale()
177 def set_taumaxtrans(self, taumaxtrans):
178 self.taumaxtrans = taumaxtrans
181 """Draw 1/sigma2 from gamma distribution."""
183 self.saxs.draw_sigma()
186 """Draw gamma from lognormal distribution."""
188 self.saxs.draw_gamma()
190 def update_covariance_matrix(self):
195 self.cov = IMP.isd2.compute_relative_covariance(self.atoms, c1, c2,
200 self.saxs.set_cov(0, self.cov)
202 def write_covariance_matrix(self, fname):
203 fl = open(fname,
'w')
204 for line
in self.cov:
210 output = super(SAXSISDRestraint, self).
get_output()
211 suffix = self._get_label_suffix()
212 output[
"SAXSISDRestraint_Sigma" +
213 suffix] = str(self.sigma.get_scale())
214 output[
"SAXSISDRestraint_Tau" + suffix] = str(self.tau.get_scale())
215 output[
"SAXSISDRestraint_Gamma" +
216 suffix] = str(self.gamma.get_scale())
220 def _logspace(a, b, num=100):
221 """Mimick numpy's logspace function"""
223 val = a + float(b - a) / float(num - 1) * i
Add weights for a set of states 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.
def __init__
Builds the restraint.
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.
def get_output
Get outputs to write to stat files.
Support for small angle X-ray scattering (SAXS) data.
Base class for PMI restraints, which wrap IMP.Restraint(s).
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...