Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DipolarCoupling.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 and 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 core/scoring/DipolarCoupling.cc
11 /// @brief Uses NMR DC for scoring (Bertram R et al J of Magn Reson 147, 9-16)
12 /// @author Lei Shi
13 
14 //Unit headers
16 
17 // Package headers
18 
19 // Project headers
22 
23 // AUTO-REMOVED #include <basic/options/after_opts.hh>
24 #include <basic/options/option.hh>
25 
26 #include <basic/datacache/BasicDataCache.hh>
28 #include <core/pose/Pose.hh>
29 #include <core/pose/util.hh>
30 
31 #include <basic/Tracer.hh>
32 
33 
34 // ObjexxFCL headers
35 #include <ObjexxFCL/format.hh>
36 
37 //C++ headers
38 #include <iostream>
39 #include <fstream>
40 #include <string>
41 
42 /// Utility headers
43 #include <utility/excn/Exceptions.hh>
44 #include <utility/io/izstream.hh>
45 #include <utility/io/ozstream.hh>
46 // AUTO-REMOVED #include <basic/options/option_macros.hh>
47 
48 // option key includes
49 #include <basic/options/keys/in.OptionKeys.gen.hh>
50 #include <basic/options/keys/dc.OptionKeys.gen.hh>
51 
52 #include <numeric/random/random.hh>
53 // Numeric headers
54 #include <numeric/xyz.functions.hh>
55 #include <numeric/xyzVector.hh>
56 #include <numeric/xyzMatrix.hh>
57 
58 //membrane
62 
63 static basic::Tracer tr("core.scoring.DipolarCoupling");
64 
65 namespace core {
66 namespace scoring {
67 
68 //////////////////////////////////////////////////////
69 //@brief reads in DC data file
70 //////////////////////////////////////////////////////
73 }
74 
77  return static_cast<DipolarCoupling const *> (pose.data().get_const_ptr(
79  };
80  return NULL;
81 }
82 
85  return static_cast<DipolarCoupling*> (pose.data().get_ptr(
87  };
88  return NULL;
89 }
90 
91 void DipolarCoupling::show(std::ostream& out) const {
92  Size ct=0;
93  for (DC_lines::const_iterator it=All_DC_lines_.begin();it!=All_DC_lines_.end();it++){
94  out << "DC "<<++ct << " ";
95  out << (*it) << std::endl;
96  }
97 }
98 
99 void DC::show(std::ostream& out) const {
100  using namespace ObjexxFCL::fmt;
101  out << RJ(4, res1_) << RJ(5, DCval_);
102 }
103 
104 std::ostream& operator<<(std::ostream& out, DC const& dc) {
105  dc.show(out);
106  return out;
107 }
108 
109 std::ostream& operator<<(std::ostream& out, DipolarCoupling const& dc) {
110  dc.show(out);
111  return out;
112 }
113 
115  basic::datacache::CacheableData(other) {
117 }
118 
119 //explicit asnumeric::signment operator to initialize buffers
122  basic::datacache::CacheableData::operator=(other);
124  return *this;
125 }
126 
128 
129  std::string line;
130  utility::io::izstream infile(filename.c_str());
131 
132  if ( !infile.good() ) {
133  throw( utility::excn::EXCN_FileNotFound( filename ) );
134  }
135 
136  tr.Info << "Reading DC file " << filename << std::endl;
137  while (getline(infile, line)) {
138  std::istringstream line_stream(line);
139  std::string atom1,atom2;
140  Size res1,res2;
141  Real weight;
142  Real DCval,DCerr;
143  line_stream >> res1 >> atom1 >> res2 >> atom2 >> DCval >> DCerr;
144  weight=1.0;
145 
146  if ( line_stream.fail() ) {
147  tr.Error << "couldn't read line " << line << " in dc-file " << filename << std::endl;
148  throw( utility::excn::EXCN_BadInput(" invalid line "+line+" in dc-file "+filename));
149  } else {
150  All_DC_lines_.push_back(DC(res1,atom1,res2,atom2,DCval,DCerr,weight));
151  }
152  } //end of readline
153 }//end of read file
154 
156  using namespace basic::options;
157  using namespace basic::options::OptionKeys;
158  if ( !option[ OptionKeys::in::file::dc ].user() ) {
159  tr.Warning << "no DC file specified" << std::endl;
160  return;
161  } else {
163  read_DC_file( filename);
164  }
165 }
166 
167 
169  if (atom == "HN" || atom == "H" || atom == "HA")
170  return "H";
171  if (atom == "C" || atom == "CA")
172  return "C";
173  if (atom == "N")
174  return "N";
175  throw(utility::excn::EXCN_BadInput("unknown atom for DC: " + atom));
176  return ""; //to make compile happy.
177 }
178 //////////////////////////////////////////////////////
179 //@brief returns type of DC data N-H, HA-CA etc
180 //////////////////////////////////////////////////////
182  std::string elem1(element_string_dc(atom1));
183  std::string elem2(element_string_dc(atom2));
184 
185  DC_TYPE DC_type;
186  if ((elem1 == "N" && elem2 == "H") || (elem1 == "H" && elem2 == "N"))
187  DC_type = DC_TYPE_NH;
188  else if ((elem1 == "C" && elem2 == "H") || (elem1 == "H" && elem2 == "C"))
189  DC_type = DC_TYPE_CH;
190  else if ((elem1 == "C" && elem2 == "N") || (elem1 == "N" && elem2 == "C"))
191  DC_type = DC_TYPE_NC;
192  else if ((elem1 == "C" && elem2 == "C"))
193  DC_type = DC_TYPE_CC;
194  else
195  throw(utility::excn::EXCN_BadInput(
196  "unknown combination of atoms for DC " + atom1 + " " + atom2));
197  return DC_type;
198 }
199 
200 //compute dcscore
202  Real total_dev=0.0;
203  numeric::xyzVector<Real> memnorm;
204  //Should we use the membrane normal?
205  if ( basic::options::option[ basic::options::OptionKeys::dc::useZ].user() ) {
206  memnorm=numeric::xyzVector<Real> (0.0,0.0,1.0);
207  memnorm.normalize();
208  } else {
209  //add the membrane code here!!
211  memnorm.normalize();
212  }
213  tr.Trace << "memnorm.x(): " << memnorm.x() << " memnorm.y() " << memnorm.y() << " memnorm.z() " << memnorm.z() << std::endl;
214 
215  for (utility::vector1<core::scoring::DC>::iterator it = All_DC_lines_.begin(); it != All_DC_lines_.end(); ++it) {
216 // tr.Trace << "it->res1(): " << it->res1() << " it->atom1() " << it->atom1() << std::endl;
217 // tr.Trace << "it->res2(): " << it->res2() << " it->atom2() " << it->atom2() << std::endl;
218  numeric::xyzVector<Real> v1( pose.residue(it->res2()).atom(it->atom2()).xyz() - pose.residue(it->res1()).atom(it->atom1()).xyz());
219  numeric::xyzVector<Real> u1=v1.normalized();
220 
221  Real costheta=dot_product(u1,memnorm);
222 
223  //compute the observed DC
224  Real compDC=it->DCval_computed_=it->Dconst()*(3*costheta*costheta-1);
225 
226  //compute the difference and then energy
227  Real devDC;
228  Real dcos_dxA=0.0;
229  Real dcos_dyA=0.0;
230  Real dcos_dzA=0.0;
231  Real dcos_dxB=0.0;
232  Real dcos_dyB=0.0;
233  Real dcos_dzB=0.0;
234 
235  Real dv_xA=0.0;
236  Real dv_yA=0.0;
237  Real dv_zA=0.0;
238  Real dv_xB=0.0;
239  Real dv_yB=0.0;
240  Real dv_zB=0.0;
241 
242  DC& dc = *it;
243 
244  if ( it->DCval()<=it->Dconst() ) {
245  //devDC = fabs(fabs(compDC) - it->DCval()) < it->DCerr() ? 0.0 : fabs(fabs(compDC - it->DCval())-it->DCerr());
246  //derivatives
247  if (fabs(fabs(compDC) - it->DCval()) <= it->DCerr()) {
248  devDC=0.0;
249  dc.f1ij_[0] = 0;
250  dc.f1ij_[1] = 0;
251  dc.f1ij_[2] = 0;
252  dc.f2ij_[0] = 0;
253  dc.f2ij_[1] = 0;
254  dc.f2ij_[2] = 0;
255  } else if ( fabs(compDC) - it->DCval()>it->DCerr() ) {
256  devDC=fabs(compDC) - it->DCval() - it->DCerr();
257  dcos_dxA= ( -memnorm.x()/v1.length()+dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
258  dcos_dyA= ( -memnorm.y()/v1.length()+dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
259  dcos_dzA= ( -memnorm.z()/v1.length()+dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
260  dcos_dxB= ( memnorm.x()/v1.length()-dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
261  dcos_dyB= ( memnorm.y()/v1.length()-dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
262  dcos_dzB= ( memnorm.z()/v1.length()-dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
263 
264  dv_xA=6*it->Dconst()*costheta*dcos_dxA;
265  dv_yA=6*it->Dconst()*costheta*dcos_dyA;
266  dv_zA=6*it->Dconst()*costheta*dcos_dzA;
267  dv_xB=6*it->Dconst()*costheta*dcos_dxB;
268  dv_yB=6*it->Dconst()*costheta*dcos_dyB;
269  dv_zB=6*it->Dconst()*costheta*dcos_dzB;
270 
271  dc.f1ij_[0] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_xA;
272  dc.f1ij_[1] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_yA;
273  dc.f1ij_[2] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_zA;
274  dc.f2ij_[0] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_xB;
275  dc.f2ij_[1] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_yB;
276  dc.f2ij_[2] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_zB;
277  } else {
278  devDC=fabs(compDC) - it->DCval() + it->DCerr();
279  dcos_dxA= ( -memnorm.x()/v1.length()+dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
280  dcos_dyA= ( -memnorm.y()/v1.length()+dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
281  dcos_dzA= ( -memnorm.z()/v1.length()+dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
282  dcos_dxB= ( memnorm.x()/v1.length()-dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
283  dcos_dyB= ( memnorm.y()/v1.length()-dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
284  dcos_dzB= ( memnorm.z()/v1.length()-dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
285 
286  dv_xA=6*it->Dconst()*costheta*dcos_dxA;
287  dv_yA=6*it->Dconst()*costheta*dcos_dyA;
288  dv_zA=6*it->Dconst()*costheta*dcos_dzA;
289  dv_xB=6*it->Dconst()*costheta*dcos_dxB;
290  dv_yB=6*it->Dconst()*costheta*dcos_dyB;
291  dv_zB=6*it->Dconst()*costheta*dcos_dzB;
292 
293  dc.f1ij_[0] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_xA;
294  dc.f1ij_[1] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_yA;
295  dc.f1ij_[2] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_zA;
296  dc.f2ij_[0] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_xB;
297  dc.f2ij_[1] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_yB;
298  dc.f2ij_[2] = it->weight()*numeric::sign(3*costheta*costheta-1)*2*devDC*dv_zB;
299  }
300 
301  }
302  else if ( it->DCval()>it->Dconst() && it->DCval()<=2*it->Dconst() ) {
303  if (fabs(compDC- it->DCval()) <= it->DCerr()) {
304  devDC=0.0;
305  dc.f1ij_[0] = 0;
306  dc.f1ij_[1] = 0;
307  dc.f1ij_[2] = 0;
308  dc.f2ij_[0] = 0;
309  dc.f2ij_[1] = 0;
310  dc.f2ij_[2] = 0;
311  } else if ( compDC- it->DCval()>it->DCerr() ) {
312  devDC=compDC- it->DCval() - it->DCerr();
313  dcos_dxA= ( -memnorm.x()/v1.length()+dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
314  dcos_dyA= ( -memnorm.y()/v1.length()+dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
315  dcos_dzA= ( -memnorm.z()/v1.length()+dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
316  dcos_dxB= ( memnorm.x()/v1.length()-dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
317  dcos_dyB= ( memnorm.y()/v1.length()-dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
318  dcos_dzB= ( memnorm.z()/v1.length()-dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
319 
320  dv_xA=6*it->Dconst()*costheta*dcos_dxA;
321  dv_yA=6*it->Dconst()*costheta*dcos_dyA;
322  dv_zA=6*it->Dconst()*costheta*dcos_dzA;
323  dv_xB=6*it->Dconst()*costheta*dcos_dxB;
324  dv_yB=6*it->Dconst()*costheta*dcos_dyB;
325  dv_zB=6*it->Dconst()*costheta*dcos_dzB;
326 
327  dc.f1ij_[0] = it->weight()*2*devDC*dv_xA;
328  dc.f1ij_[1] = it->weight()*2*devDC*dv_yA;
329  dc.f1ij_[2] = it->weight()*2*devDC*dv_zA;
330  dc.f2ij_[0] = it->weight()*2*devDC*dv_xB;
331  dc.f2ij_[1] = it->weight()*2*devDC*dv_yB;
332  dc.f2ij_[2] = it->weight()*2*devDC*dv_zB;
333  } else {
334  devDC=compDC- it->DCval() + it->DCerr();
335  dcos_dxA= ( -memnorm.x()/v1.length()+dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
336  dcos_dyA= ( -memnorm.y()/v1.length()+dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
337  dcos_dzA= ( -memnorm.z()/v1.length()+dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
338  dcos_dxB= ( memnorm.x()/v1.length()-dot_product(v1,memnorm)*v1.x()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
339  dcos_dyB= ( memnorm.y()/v1.length()-dot_product(v1,memnorm)*v1.y()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
340  dcos_dzB= ( memnorm.z()/v1.length()-dot_product(v1,memnorm)*v1.z()/(v1.length()*v1.length()*v1.length()) ) / memnorm.length();
341 
342  dv_xA=6*it->Dconst()*costheta*dcos_dxA;
343  dv_yA=6*it->Dconst()*costheta*dcos_dyA;
344  dv_zA=6*it->Dconst()*costheta*dcos_dzA;
345  dv_xB=6*it->Dconst()*costheta*dcos_dxB;
346  dv_yB=6*it->Dconst()*costheta*dcos_dyB;
347  dv_zB=6*it->Dconst()*costheta*dcos_dzB;
348 
349  dc.f1ij_[0] = it->weight()*2*devDC*dv_xA;
350  dc.f1ij_[1] = it->weight()*2*devDC*dv_yA;
351  dc.f1ij_[2] = it->weight()*2*devDC*dv_zA;
352  dc.f2ij_[0] = it->weight()*2*devDC*dv_xB;
353  dc.f2ij_[1] = it->weight()*2*devDC*dv_yB;
354  dc.f2ij_[2] = it->weight()*2*devDC*dv_zB;
355  }
356  }
357  else
358  throw(utility::excn::EXCN_BadInput( "DC value not in resonable range"));
359 
360  //Add weight to energy
361  total_dev+=it->weight()*devDC*devDC;
362 
363  if ( tr.Trace.visible() ) {
364  tr.Trace << "resi: " << it->res1() << " compDC: " << compDC << " expDC: " << it->DCval() << " expErr " << it->DCerr() << " devDC: " << devDC << " total_dev "<< total_dev << std::endl;
365  }
366 
367  }
368 
369  //return energy
370  return total_dev;
371  //return total_dev/All_DC_lines_.size();
372 
373 }//end of compute_dcscore
374 
375 } //namespace Scoring
376 } //namespace core