1 """@namespace IMP.pmi.restraints.saxs
2 Restraints for handling small angle x-ray (SAXS) data.
5 from __future__
import print_function
14 class SAXSISDRestraint(object):
21 def __init__(self, representation, profile, resolution=0, weight=1,
22 ff_type=IMP.saxs.HEAVY_ATOMS):
24 self.m = representation.prot.get_model()
28 self.taumaxtrans = 0.05
33 resolution=resolution)
36 self.gamma = IMP.pmi.tools.SetupNuisance(
37 self.m, 1., 0.,
None,
False).get_particle()
40 self.sigma = IMP.pmi.tools.SetupNuisance(self.m, 10.0, 0.,
None,
False
44 self.tau = IMP.pmi.tools.SetupNuisance(self.m, 1., 0.,
None,
False,
48 self.c1 = IMP.pmi.tools.SetupNuisance(self.m, 1.0, 0.95, 1.05,
50 self.c2 = IMP.pmi.tools.SetupNuisance(self.m, 0.0, -2., 4.,
54 self.w = IMP.pmi.tools.SetupWeight(self.m).get_particle()
58 self.cov = [[1
if i == j
else 0
for j
in range(self.prof.size())]
59 for i
in range(self.prof.size())]
61 print(
"create saxs restraint")
62 self.saxs = IMP.isd2.SAXSRestraint(self.prof, self.sigma, self.tau,
63 self.gamma, self.w, self.c1, self.c2)
64 self.saxs.add_scatterer(self.atoms, self.cov, ff_type)
66 self.rs.add_restraint(self.saxs)
67 self.rs.set_weight(weight)
75 self.rs2.add_restraint(j1)
77 self.rs2.add_restraint(j2)
79 self.rs2.add_restraint(j3)
81 def optimize_sigma(self):
82 """set sigma to the value that maximizes its conditional likelihood"""
85 sigma2hat = self.saxs.get_sigmasq_scale_parameter() \
86 / (self.saxs.get_sigmasq_shape_parameter() + 1)
89 def optimize_gamma(self):
90 """set gamma to the value that maximizes its conditional likelihood"""
93 gammahat = exp(self.saxs.get_loggamma_variance_parameter()
94 * self.saxs.get_loggamma_jOg_parameter())
97 def logspace(self, a, b, num=100):
98 """mimick numpy's logspace function"""
100 val = a + float(b - a) / float(num - 1) * i
103 def optimize_tau(self, ltaumin=-2, ltaumax=3, npoints=100):
109 fl = open(
'tauvals.txt',
'w')
110 for tauval
in self.logspace(ltaumin, ltaumax, npoints):
113 values.append((self.m.evaluate(
False), tauval))
116 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
118 ltcenter = log(values[0][1]) / log(10)
119 spacing = (ltaumax - ltaumin) / float(npoints)
121 for tauval
in self.logspace(
122 ltcenter - 2 * spacing, ltcenter + 2 * spacing,
125 values.append((self.m.evaluate(
False), tauval))
126 fl.write(
'%G %G\n' % (values[-1][1], values[-1][0]))
130 def draw_sigma(self):
131 """draw 1/sigma2 from gamma distribution"""
133 self.saxs.draw_sigma()
135 def draw_gamma(self):
136 """draw gamma from lognormal distribution"""
138 self.saxs.draw_gamma()
140 def update_covariance_matrix(self):
145 self.cov = IMP.isd2.compute_relative_covariance(self.atoms, c1, c2,
150 self.saxs.set_cov(0, self.cov)
152 def write_covariance_matrix(self, fname):
153 fl = open(fname,
'w')
154 for line
in self.cov:
159 def get_gamma_value(self):
160 return self.gamma.get_scale()
162 def set_label(self, label):
165 def add_to_model(self):
166 self.m.add_restraint(self.rs)
167 self.m.add_restraint(self.rs2)
169 def set_taumaxtrans(self, taumaxtrans):
170 self.taumaxtrans = taumaxtrans
172 def get_particles_to_sample(self):
178 def get_output(self):
181 score = self.rs.unprotected_evaluate(
None)
182 score2 = self.rs2.unprotected_evaluate(
None)
183 output[
"_TotalScore"] = str(score + score2)
185 output[
"SAXSISDRestraint_Likelihood_" + self.label] = str(score)
186 output[
"SAXSISDRestraint_Prior_" + self.label] = str(score2)
187 output[
"SAXSISDRestraint_Sigma_" +
188 self.label] = str(self.sigma.get_scale())
189 output[
"SAXSISDRestraint_Tau_" +
190 self.label] = str(self.tau.get_scale())
191 output[
"SAXSISDRestraint_Gamma_" +
192 self.label] = str(self.gamma.get_scale())
Add weights for a set of states to a particle.
void write_pdb(const Selection &mhd, base::TextOutput out, unsigned int model=1)
Various classes to hold sets of particles.
Object used to hold a set of restraints.
Low level functionality (logging, error handling, profiling, command line flags etc) that is used by ...
Add scale parameter to particle.
Add nuisance parameter to particle.
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...
Functionality for loading, creating, manipulating and scoring atomic structures.
Support for small angle X-ray scattering (SAXS) data.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...