Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RDCScore_Gront.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite && is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file protocols/frag_picker/scores/RDCScore.cc
11 /// @brief
12 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
13 
15 
16 // package headers
20 // AUTO-REMOVED #include <protocols/frag_picker/FragmentPicker.hh>
21 
22 // AUTO-REMOVED #include <utility/vector1.hh>
23 #include <utility/io/izstream.hh>
24 #include <utility/excn/Exceptions.hh>
25 
26 // option key includes
27 // AUTO-REMOVED #include <core/init.hh>
28 // AUTO-REMOVED #include <basic/options/option.hh>
29 // AUTO-REMOVED #include <basic/options/option_macros.hh>
30 // AUTO-REMOVED #include <basic/options/keys/OptionKeys.hh>
31 // AUTO-REMOVED #include <basic/options/keys/constraints.OptionKeys.gen.hh>
32 
34 #include <basic/Tracer.hh>
35 
36 //Auto Headers
37 #include <utility/options/keys/BooleanOptionKey.hh>
38 
39 
40 namespace protocols {
41 namespace frag_picker {
42 namespace scores {
43 
44 #define ROTATE(a,i,j,k,l) g=a[i][j];h=a[k][l];a[i][j]=g-s*(h+g*tau); a[k][l]=h+s*(g-h*tau);
45 
47 
48  rdc_atoms_.empty();
49  rdc_atoms_.push_back("H");
50  rdc_atoms_.push_back("H1");
51  rdc_atoms_.push_back("N");
52  rdc_atoms_.push_back("CA");
53  rdc_atoms_.push_back("CB");
54 
55  return rdc_atoms_;
56 }
57 
58 static basic::Tracer trRDCScore("fragment.picking.scores.RDCScore");
59 
60 /// @param priority - the priority for this scoring method. The lower the priority, the later the score will be evaluated
61 /// Because a fragment may be discarded when a score is too low, the most accurate && meaningful scores should have the highest priority
62 /// @param lowest_acceptable_value - a fragment for which this score is below a certain threshold will be discarded
63 /// @param rdc_file_name - from this file RDC data will be obtained
64 /// @param query_size - the number of residues in the query sequence
65 RDCScore::RDCScore(Size priority, Real lowest_acceptable_value, bool use_lowest, Size query_size) :
66  AtomBasedConstraintsScore(priority, lowest_acceptable_value, use_lowest, query_size,
67  rdc_atoms(), "RDCScore") {
68 }
69 
71 
73 }
74 
75 void RDCScore::clean_up() {
77 }
78 
79 void RDCScore::read_RDC_file(std::string const & filename, Size exp_id = 1) {
80 
81  utility::io::izstream infile(filename.c_str());
82  std::string line;
83 
84  trRDCScore.Info << "Reading RDC file " << filename << std::endl;
85  Size n = 0;
86  while (getline(infile, line)) {
87  std::istringstream line_stream(line);
88  std::string atom1, atom2;
89  Size res1, res2;
90  Real Jdipolar;
91  line_stream >> res1 >> atom1 >> res2 >> atom2 >> Jdipolar;
92 
93  if (atom1 == "HN")
94  atom1 = "H"; //take care of typical NMR community notation
95  if (atom2 == "HN")
96  atom2 = "H";
97  if (res1 == 1 && atom1 == "H")
98  atom1 == "H1"; //or should it be ignored ?
99  if (res2 == 1 && atom2 == "H")
100  atom1 == "H1";
101  if (line_stream.fail()) {
102  trRDCScore.Error << "couldn't read line " << line
103  << " in rdc-file " << filename << "\n";
104  throw(utility::excn::EXCN_BadInput(" invalid line " + line
105  + " in rdc-file " + filename));
106  }
107 
108  Real weight(1.0);
109  line_stream >> weight;
110  if (line_stream.fail()) {
111  trRDCScore.Debug << " set weight for RDC " << res1 << " to 1.0 "
112  << std::endl;
113  weight = 1.0;
114  }
115  rdc_data_.push_back(core::scoring::RDC(res1, atom1, res2, atom2,
116  Jdipolar, weight, exp_id - 1 /*C-style counting*/));
117  n++;
118  trRDCScore.Debug << rdc_data_[n];
119  }
120  trRDCScore.Debug << "\n" << rdc_data_.size()
121  << " data points acquired from " << filename << std::endl;
122 }
123 
124 int RDCScore::m_inv_gen(Real m[5][5], int n, Real minv[5][5]) {
125  Real md[5][5], v[5][5];
126  Real eig[5];
127 
128  Real tol, s;
129  int nzero, i, j, k, nrot;
130  for (i = 0; i < n; i++)
131  for (j = 0; j < n; j++)
132  md[i][j] = m[i][j];
133 
134  tol = 0;
135  for (i = 0; i < n; i++)
136  tol += fabs(md[i][i]);
137  tol = 1e-6 * tol / n;
138 
139  jacobi(md, eig, v, &nrot);
140 
141  nzero = 0;
142  for (i = 0; i < n; i++)
143  if (fabs(eig[i]) < tol) {
144  eig[i] = 0;
145  nzero++;
146  } else
147  eig[i] = 1.0 / eig[i];
148 
149  for (i = 0; i < n; i++)
150  for (j = 0; j < n; j++) {
151  s = 0;
152  for (k = 0; k < n; k++)
153  s += eig[k] * v[i][k] * v[j][k];
154  minv[i][j] = s;
155  }
156 
157  return nzero;
158 }
159 
160 void RDCScore::jacobi(Real a[5][5], Real d[], Real v[5][5], int *nrot) {
161  int j, i;
162  int iq, ip;
163  Real tresh, theta, tau, t, sm, s, h, g, c;
164  Real b[5];
165  Real z[5];
166  int const n(5);
167  for (ip = 0; ip < n; ip++) {
168  for (iq = 0; iq < n; iq++)
169  v[ip][iq] = 0.0;
170  v[ip][ip] = 1.0;
171  }
172  for (ip = 0; ip < n; ip++) {
173  b[ip] = d[ip] = a[ip][ip];
174  z[ip] = 0.0;
175  }
176  *nrot = 0;
177  for (i = 1; i <= 50; i++) {
178  sm = 0.0;
179  for (ip = 0; ip < n - 1; ip++) {
180  for (iq = ip + 1; iq < n; iq++)
181  sm += fabs(a[ip][iq]);
182  }
183  if (sm == 0.0) {
184  return;
185  }
186  if (i < 4) //first 3 iterations
187  tresh = 0.2 * sm / (n * n);
188  else
189  tresh = 0.0;
190  for (ip = 0; ip < n - 1; ip++) {
191  for (iq = ip + 1; iq < n; iq++) {
192  g = 100.0 * fabs(a[ip][iq]);
193  if (i > 4 && fabs(d[ip]) + g == fabs(d[ip]) && fabs(d[iq]) + g
194  == fabs(d[iq]))
195  a[ip][iq] = 0.0;
196  else if (fabs(a[ip][iq]) > tresh) {
197  h = d[iq] - d[ip];
198  if (fabs(h) + g == fabs(h))
199  t = (a[ip][iq]) / h;
200  else {
201  theta = 0.5 * h / (a[ip][iq]);
202  t = 1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
203  if (theta < 0.0)
204  t = -t;
205  }
206  c = 1.0 / sqrt(1 + t * t);
207  s = t * c;
208  tau = s / (1.0 + c);
209  h = t * a[ip][iq];
210  z[ip] -= h;
211  z[iq] += h;
212  d[ip] -= h;
213  d[iq] += h;
214  a[ip][iq] = 0.0;
215  for (j = 0; j < ip; j++) {
216  ROTATE (a,j,ip,j,iq)
217  }
218  for (j = ip + 1; j < iq; j++) {
219  ROTATE(a,ip,j,j,iq)
220  }
221  for (j = iq + 1; j < n; j++) {
222  ROTATE(a,ip,j,iq,j)
223  }
224  for (j = 0; j < n; j++) {
225  ROTATE(v,j,ip,j,iq)
226  }
227  ++(*nrot);
228  }
229  }
230  }
231  for (ip = 0; ip < n; ip++) {
232  b[ip] += z[ip];
233  d[ip] = b[ip];
234  z[ip] = 0.0;
235  }
236  }
237  //probably different type of Exception is better suited
238  throw(utility::excn::EXCN_BadInput(
239  " too many iterations in Jacobi when compute RDC tensor"));
240 }
241 
243 
244  Real total_score = 0;
245  Size frag_len = f->get_length();
246  empty_map->set_score_component(total_score, id_ + frag_len - frag_len); // just to prevent a compiler from complaining bout unused frag_len
247 
248  return true;
249 }
250 
251 /*void RDCScore::evaluate_score(core::pose::PoseOP pose, utility::vector1<
252  core::scoring::RDC> & data) {
253 
254  bool const
255  correct_NH(
256  basic::options::option[basic::options::OptionKeys::rdc::correct_NH_length]);
257  bool const
258  bReduced(
259  basic::options::option[basic::options::OptionKeys::rdc::reduced_couplings]);
260 
261  const Size nrow = data.size();
262  Real D_[1000][5]; //rvec5 x nrows
263  Real rhs_[5]; //rvec5 x nrows
264  Real S_[3][3]; // 3 x 3 x nex
265  Real T_[5][5]; // 5 x 5 x nex
266 
267  trace.Debug << "Evaluating RDC score based on " << data.size()
268  << " data points" << std::endl;
269 
270  utility::vector1<core::scoring::RDC>::const_iterator it;
271  Size d;
272  d = 0;
273  for (it = data.begin(); it != data.end(); ++it) {
274 
275  numeric::xyzVector<Real> r(
276  pose->residue(it->res1()).atom(it->atom1()).xyz()
277  - pose->residue(it->res2()).atom(it->atom2()).xyz());
278 
279  core::Real r2 = r.norm_squared();
280  if (it->type() == RDC::RDC_TYPE_NH && correct_NH)
281  r2 = 1.04 * 1.04;
282 
283  core::Real invr = 1.0 / sqrt(r2);
284  core::Real pfac = it->Dconst() * invr * invr;
285  pfac *= invr * invr * invr;
286  D_[d][0] = 3* pfac * (2* r [0] * r[0] + r[1] * r[1] - r2);
287  D_[d][1] = 3* pfac * (2* r [0] * r[1]);
288  D_[d][2] = 3* pfac * (2* r [0] * r[2]);
289  D_[d][3] = 3* pfac * (2* r [1] * r[1] + r[0] * r[0] - r2);
290  D_[d][4] = 3* pfac * (2* r [1] * r[2]);
291  d++;
292  } //~ End of atoms
293 
294  // Calculate the order tensor S for each experiment via optimization
295  for (Size i = 0; i < 5; i++) {
296  rhs_[i] = 0;
297  for (Size j = 0; j <= i; j++) {
298  T_[i][j] = T_[j][i] = 0;
299  }
300  }
301 
302  for (core::Size d = 0; d < data.size(); d++) {
303  core::Real weight = data[d + 1].weight(); //force constant
304  core::Real obs = data[d + 1].Jdipolar();
305  // Calculate the vector rhs && half the matrix T for the 5 equations
306  for (Size i = 0; i < 5; i++) {
307  rhs_[i] += D_[d][i] * obs * weight;
308  for (Size j = 0; j <= i; j++)
309  T_[i][j] += D_[d][i] * D_[d][j] * weight;
310  }
311  }
312 
313  // Now we have all the data we can calculate S
314  // Correct corrfac && copy one half of T to the other half
315  for (Size i = 0; i < 5; i++) {
316  for (Size j = 0; j < i; j++) {
317  T_[j][i] = T_[i][j];
318  }
319  }
320  try {
321  m_inv_gen(T_, 5, T_);
322  } catch (utility::excn::EXCN_BadInput &excn) {
323  if (trace.Debug) {
324  pose->dump_pdb("failed_jacobi.pdb");
325  }
326  throw excn;
327  }
328  // Calculate the orientation tensor S for this experiment
329  S_[0][0] = 0;
330  S_[0][1] = 0;
331  S_[0][2] = 0;
332  S_[1][1] = 0;
333  S_[1][2] = 0;
334  for (Size i = 0; i < 5; i++) {
335  S_[0][0] += 1.5 * T_[0][i] * rhs_[i];
336  S_[0][1] += 1.5 * T_[1][i] * rhs_[i];
337  S_[0][2] += 1.5 * T_[2][i] * rhs_[i];
338  S_[1][1] += 1.5 * T_[3][i] * rhs_[i];
339  S_[1][2] += 1.5 * T_[4][i] * rhs_[i];
340  }
341  S_[1][0] = S_[0][1];
342  S_[2][0] = S_[0][2];
343  S_[2][1] = S_[1][2];
344  S_[2][2] = -S_[0][0] - S_[1][1];
345  Real Smax = sqrt(sqr(S_[0][0]) + sqr(S_[0][1]) + sqr(S_[0][2]) + sqr(
346  S_[1][1]) + sqr(S_[1][2]));
347 
348  trace.Debug << "Smax( " << 1 << " ): " << Smax << std::endl;
349 
350  Real wsv2 = 0;
351  Real sw = 0;
352  Real two_thr = 2.0 / 3.0;
353  Real vtot = 0;
354  Real Q = 0;
355  Real Qnorm = 0;
356  core::Real const Rohl2Hess(2.5);
357 
358  Size irow(0);
359  for (utility::vector1<core::scoring::RDC>::iterator it = data.begin(); it
360  != data.end(); ++it) {
361  ++irow;
362  Size const d(irow - 1);
363 
364  //for( Size d = 0; d<nrow; d++ ) {
365  Size ex = it->expid(); //exp_id const 1 ... fix later
366 
367  Real computed_coupling = it->Jdipolar_computed_ = two_thr * (S_[0][0]
368  * D_[d][0] + S_[0][1] * D_[d][1] + S_[0][2] * D_[d][2]
369  + S_[1][1] * D_[d][3] + S_[1][2] * D_[d][4]);
370 
371  Size const power(3); //this will be 0 for CSA see above
372  RDC & rdc = *it;
373  numeric::xyzVector<Real> r(
374  pose->residue(rdc.res1()).atom(rdc.atom1()).xyz()
375  - pose->residue(rdc.res2()).atom(rdc.atom2()).xyz());
376  core::Real r2 = r.norm_squared();
377  core::Real invr = 1.0 / sqrt(r2);
378  core::Real invr2 = sqr(invr);
379  core::Real pfac = rdc.Dconst() * invr2 * invr2 * invr * Rohl2Hess
380  / (Smax * Smax) / data.size();
381 
382  core::Real const pfac_NH = 6.088 / 1.05 * Rohl2Hess / (Smax * Smax)
383  / data.size();
384  Real obs = it->Jdipolar();
385  Real dev = computed_coupling - obs;
386  if (bReduced) {
387  trace.Trace << "reducing coupling for " << rdc << " dev: " << dev
388  << " pfac: " << pfac << " pfac_NH " << pfac_NH;
389  dev *= pfac_NH / pfac;
390  obs *= pfac_NH / pfac;
391  trace.Trace << " new dev: " << dev << std::endl;
392  }
393 
394  Real Sr[3];
395  Real rgmx[3];
396  rgmx[0] = r[0];
397  rgmx[1] = r[1];
398  rgmx[2] = r[2];
399  mvmul(S_, rgmx, Sr);
400 
401  if (bReduced)
402  pfac = pfac_NH;
403  for (Size i = 0; i < 3; i++) {
404  rdc.fij_[i] = -pfac * dev * (4* Sr [i] - 2* (2 + power ) * invr2 *
405  iprod (Sr ,rgmx) * rgmx[ i]);
406  }
407 
408  Real weight = it->weight();
409  vtot += 0.5*sqr( dev )/( Smax * Smax );
410  wsv2 += weight*sqr(dev);
411  sw += weight;
412  Q += sqr( dev );
413  Qnorm += sqr( obs );
414  }
415  Real R_ = sqrt(Q / Qnorm / 2);
416  Real rmsd_ = sqrt(wsv2 / sw);
417 
418  trace.Debug<< Rohl2Hess*vtot/data.size()<<std::endl;
419  }*/
420 
421 }
422 } // frag_picker
423 } // protocols