Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BBGaussianMover.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 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file protocols/simple_moves/BBGaussianMover.fwd.hh
12 /// @brief Gaussian Perturbation to backbone
13 /// @author Yuan Liu (wendao@u.washington.edu)
14 
16 //core
18 // AUTO-REMOVED #include <core/chemical/AtomTypeSet.hh>
19 // AUTO-REMOVED #include <core/chemical/ChemicalManager.hh>
21 #include <core/pose/Pose.hh>
22 #include <basic/basic.hh>
23 
25 
26 #include <basic/options/option.hh>
27 // AUTO-REMOVED #include <basic/options/option_macros.hh>
28 #include <basic/options/keys/bbg.OptionKeys.gen.hh>
29 
30 //util
31 #include <utility/exit.hh>
32 #include <basic/Tracer.hh>
33 #include <numeric/random/random.hh>
34 // AUTO-REMOVED #include <numeric/xyz.functions.hh>
35 // AUTO-REMOVED #include <numeric/xyzMatrix.hh>
36 // AUTO-REMOVED #include <numeric/constants.hh>
37 // AUTO-REMOVED #include <numeric/internal/RowVectors.hh>
38 
39 #include <iostream>
40 // AUTO-REMOVED #include <fstream>
41 #include <sstream>
42 
43 #include <utility/vector1.hh>
44 
45 
46 using namespace std;
47 using namespace core;
48 using namespace core::pose;
49 using namespace utility;
50 using namespace numeric;
51 using namespace basic::options;
52 using namespace basic::options::OptionKeys;
53 
54 static basic::Tracer TR("protocols.simple_moves.BBGaussianMover");
55 static numeric::random::RandomGenerator RG(6233); //Magic Number
56 
57 namespace protocols {
58 namespace simple_moves {
59 
60 
61 BBGaussianMover::BBGaussianMover( Size n_end_atom, Size n_dof_angle, Size n_pert_res)
62  :Mover(),
63  n_end_atom_(n_end_atom),
64  n_dof_angle_(n_dof_angle),
65  n_pert_res_(n_pert_res),
66  matrix_G(n_dof_angle_,utility::vector1<Real>(n_dof_angle_)),
67  matrix_A(n_dof_angle_,utility::vector1<Real>(n_dof_angle_)),
68  matrix_dRdPhi(n_end_atom_,utility::vector1<Vector>(n_dof_angle_)),
69  last_proposal_density_ratio_(1.0)
70 {
71  protocols::moves::Mover::type("BBGaussianMover");
73  movemap->set_bb(true);
75  // setup_list(pose);
76 }
77 
79 
81 {
83 }
84 
86 {
87  using namespace id;
88  using namespace basic::options;
89  using namespace basic::options::OptionKeys;
90 
91  /* old version, can not handle that segment shorter than n_pert
92  available_res_list_.erase(available_res_list_.begin(),available_res_list_.end());
93  Size seg_len = 0;
94  //for (Size n=n_pert_res_,end=pose.n_residue(); n<=end; n++)
95  for (Size n=1,end=pose.n_residue(); n<=end; n++)
96  {
97  conformation::Residue const & rsd( pose.residue( n ) );
98  if ( rsd.is_protein() && movemap_->get( TorsionID( n, BB, phi_torsion ) ) &&
99  movemap_->get( TorsionID( n, BB, psi_torsion ) ) )
100  {
101  seg_len++;
102  if(seg_len>=n_pert_res_)available_res_list_.push_back(n);
103  }
104  else
105  {
106  seg_len = 0;
107  }
108  }*/
109 
110  //!!! can not handle cutpoint
112  int seg_len = 0;
113  for (int n=1,end=static_cast< int >(pose.n_residue()); n<=end; n++)
114  {
115  conformation::Residue const & rsd( pose.residue( n ) );
116  if ( rsd.is_protein()
117  && movemap_->get( TorsionID( n, BB, phi_torsion ) )
118  && movemap_->get( TorsionID( n, BB, psi_torsion ) )
119  && n<end
120  && (!option[bbg::ignore_improper_res] || pose.residue(n).name1()!='P') )
121  {
122  //TR << "Yes: " << n << std::endl;
123  seg_len++;
124  }
125  else
126  {
127  //TR << "No: " << n << std::endl;
128  if (seg_len>0)
129  {
130  //put them in
131  int first(n-seg_len);
132  int last;
133  if( n==end &&
134  pose.residue( n ).is_protein()
135  && movemap_->get( TorsionID( n, BB, phi_torsion ) )
136  && movemap_->get( TorsionID( n, BB, psi_torsion ) )
137  && (!option[bbg::ignore_improper_res] || pose.residue(n).name1()!='P') )
138  {
139  last = end;
140  }
141  else
142  {
143  last = n-1;
144  }
145  //TR << "first=" << first << " last=" << last << std::endl;
146 
147  int r_num(first);
148  int l_num(first + 1 - static_cast< int >(n_pert_res_));
149  //TR << "l=" << l_num << " r=" << r_num << std::endl;
150  for (;l_num<=last;r_num++,l_num++)
151  {
152  //TR << "l=" << l_num << " r=" << r_num << std::endl;
153  //in the middle of the scaffold, no end fix case
154  int part1, part2;
155 
156  if (option[bbg::fix_short_segment] && l_num<first) continue;
157  else part1 = l_num<first?first:l_num;
158  if (option[bbg::fix_short_segment] && r_num>last) continue;
159  else part2 = r_num>last?last:r_num;
160 
161  //get one
162  std::pair<Size, Size> seg(part1,part2);
163  available_seg_list_.push_back(seg);
164  }
165  }
166  seg_len = 0;
167  }
168  }
169 }
170 
172 {
173  return movemap_;
174 }
175 
177 {
178  movemap_=new_movemap;
179  available_seg_list_.clear();
180 }
181 
183 {
184  Size i,j,k;
185  Real sum;
187 
188  for (i=1;i<=n;i++)
189  {
190  for (j=i;j<=n;j++)
191  {
192  for (sum=a[i][j],k=i-1;k>=1;k--) sum -= a[i][k]*a[j][k];
193  if (i == j)
194  {
195  runtime_assert(sum>0.0);
196  p[i]=sqrt(sum);
197  }
198  else
199  {
200  a[j][i]=sum/p[i];
201  }
202  }
203  }
204 
205  for(i=from; i<=to; i++)
206  {
207  p[i]*=scale;
208  for(j=from; j<i; j++)
209  {
210  a[i][j] *= scale;
211  }
212  }
213 
214  for (i=n;i>0;i--)
215  {
216  for (sum=delta[i],k=i+1;k<=n;k++) sum-=a[k][i]*dphi[k];
217  dphi[i]=sum/p[i];
218  }
219 
220  Real detL = 1.0;
221  for (i=1;i<=n;i++) detL*=p[i];
222  return detL;
223 }
224 
226 {
227  Size i,j,k;
228  Real sum;
230 
231  for (i=1;i<=n;i++)
232  {
233  for (j=i;j<=n;j++)
234  {
235  for (sum=a[i][j],k=i-1;k>=1;k--) sum -= a[i][k]*a[j][k];
236  if (i == j)
237  {
238  runtime_assert(sum>0.0);
239  p[i]=sqrt(sum);
240  }
241  else
242  {
243  a[j][i]=sum/p[i];
244  }
245  }
246  }
247 
248  for(i=from; i<=to; i++)
249  {
250  p[i]*=scale;
251  for(j=from; j<i; j++)
252  {
253  a[i][j] *= scale;
254  }
255  }
256 
257  for (i=1;i<=n;i++)
258  {
259  delta[i]=p[i]*dphi[i];
260  for (j=i+1;j<=n;j++) delta[i]+=a[j][i]*dphi[j];
261  }
262 
263  Real detL = 1.0;
264  for (i=1;i<=n;i++) detL*=p[i];
265  return detL;
266 }
267 
268 /// @brief randomly rotate the dih angle in this range for avoiding the fixed ends
270 {
271  //copy from Backbone mover, make_move()
272  static Real big_angle_= 12.0;//loop parameter
273 
274  for (;i<=to;i++)
275  {
276  Real old_phi_ = pose.phi(i);
277  Real new_phi_ = basic::periodic_range( old_phi_ + RG.gaussian() * big_angle_, 360.0 );
278  //Real new_phi_ = basic::periodic_range( old_phi_ + RG.uniform() * big_angle_, 360.0 );
279 
280  Real old_psi_ = pose.psi(i);
281  Real new_psi_ = basic::periodic_range( old_psi_ + RG.gaussian() * big_angle_, 360.0 );
282  //Real new_psi_ = basic::periodic_range( old_psi_ + RG.uniform() * big_angle_, 360.0 );
283 
284  pose.set_phi( i, new_phi_ );
285  pose.set_psi( i, new_psi_ );
286  }
287 }
288 
289 ////////////////////////////////////////////
290 // BBG8T3AMover
291 ////////////////////////////////////////////
292 /*
293 void BBG8T3AMover::register_options() {
294 
295  using namespace basic::options;
296  using namespace basic::options::OptionKeys;
297 
298  OPT( bbg::factorA );
299  OPT( bbg::factorB );
300 }
301 */
302 void
304  factorA_ = fA;
305 }
306 
307 void
309  factorB_ = fB;
310 }
311 
313  :BBGaussianMover(3,8,4),
314  dphi(utility::vector1<Real>(n_dof_angle_))
315 {
316  protocols::moves::Mover::type("BBG8T3AMover");
317  //build end atom list
318  end_atom_list_.push_back("CA");
319  end_atom_list_.push_back("C");
320  end_atom_list_.push_back("O");
321 
322  //init the A/C factor
323  factorA_ = option[ bbg::factorA ];
324  factorB_ = option[ bbg::factorB ];
325 }
326 
328 
331  return "BBG8T3AMover";
332 }
333 
335 {
336  conformation::Residue const & rsd4( pose.residue( resnum_ ) );
337  conformation::Residue const & rsd3( pose.residue( resnum_-1 ) );
338  conformation::Residue const & rsd2( pose.residue( resnum_-2 ) );
339  conformation::Residue const & rsd1( pose.residue( resnum_-3 ) );
340 
341  for (Size i=1;i<=end_atom_list_.size();i++)
342  {
343  //for each end atom
344  Vector end_xyz = rsd4.atom(end_atom_list_[i]).xyz();
345  //TR << "Phi: 8" << endl;
346  matrix_dRdPhi[i][8] = get_dRdPhi(rsd4.atom("CA").xyz(),
347  rsd4.atom("C").xyz(),
348  end_xyz);
349  //TR << "Phi: 7" << endl;
350  matrix_dRdPhi[i][7] = get_dRdPhi(rsd4.atom("N").xyz(),
351  rsd4.atom("CA").xyz(),
352  end_xyz);
353  //TR << "Phi: 6" << endl;
354  matrix_dRdPhi[i][6] = get_dRdPhi(rsd3.atom("CA").xyz(),
355  rsd3.atom("C").xyz(),
356  end_xyz);
357  //TR << "Phi: 5" << endl;
358  matrix_dRdPhi[i][5] = get_dRdPhi(rsd3.atom("N").xyz(),
359  rsd3.atom("CA").xyz(),
360  end_xyz);
361  //TR << "Phi: 4" << endl;
362  matrix_dRdPhi[i][4] = get_dRdPhi(rsd2.atom("CA").xyz(),
363  rsd2.atom("C").xyz(),
364  end_xyz);
365  //TR << "Phi: 3" << endl;
366  matrix_dRdPhi[i][3] = get_dRdPhi(rsd2.atom("N").xyz(),
367  rsd2.atom("CA").xyz(),
368  end_xyz);
369  //TR << "Phi: 2" << endl;
370  matrix_dRdPhi[i][2] = get_dRdPhi(rsd1.atom("CA").xyz(),
371  rsd1.atom("C").xyz(),
372  end_xyz);
373  //TR << "Phi: 1" << endl;
374  matrix_dRdPhi[i][1] = get_dRdPhi(rsd1.atom("N").xyz(),
375  rsd1.atom("CA").xyz(),
376  end_xyz);
377  }
378 }
379 
381 {
382  for (Size i=1; i<=n_dof_angle_; i++)
383  {
384  for (Size j=i; j<=n_dof_angle_; j++)
385  {
386  matrix_G[i][j] = 0.0;
387  for (Size n=1; n<=end_atom_list_.size();n++)
388  {
389  matrix_G[i][j] += matrix_dRdPhi[n][i].dot(matrix_dRdPhi[n][j]);
390  }
391  //if (matrix_G[i][j]>-ZERO && matrix_G[i][j]<ZERO) matrix_G[i][j] = 0.0;
392  if (i<j) matrix_G[j][i]=matrix_G[i][j];
393 
394  }
395  }
396  /*
397  TR << " --Gij-- " << endl;
398  for(Size i=1; i<=n_dof_angle_; i++)
399  {
400  for(Size j=1; j<=n_dof_angle_; j++)
401  {
402  TR<< matrix_G[i][j] << " ";
403  }
404  TR << endl;
405  }
406  */
407 }
408 
410 {
411  for (Size i=1; i<=n_dof_angle_; i++)
412  {
413  for (Size j=i; j<=n_dof_angle_; j++)
414  {
415  matrix_A[i][j] = factorB_ * matrix_G[i][j];
416  if (i==j) matrix_A[i][j] += 1.0;
417  matrix_A[i][j] *= factorA_ / 2.0;
418  if (i<j) matrix_A[j][i] = matrix_A[i][j];
419  }
420  }
421 }
422 
424 {
425  //gerate a Gaussian dx vector
427  for (Size i=1; i<=n_dof_angle_; i++) delta[i]=RG.gaussian();
428  /*
429  for (Size i=1; i<=n_dof_angle_; i+=2)
430  {
431  Real r1=sqrt(-log(RG.uniform()));
432  Real r2=RG.uniform();
433  delta[i]=r1*cos(numeric::constants::r::pi_2*r2);
434  delta[i+1]=r1*sin(numeric::constants::r::pi_2*r2);
435  }*/
436  //calculate d^2 = delta^2
437  Real d2=0.0;
438  for (Size i=1; i<=n_dof_angle_; i++) d2+=delta[i]*delta[i];
439 
440  //cholesky, get L^t, L^-1
441  Real detL = cholesky_fw(matrix_A, n_dof_angle_, delta, dphi);
442  /*
443  //dphi=L^-t * delta, L^-t = (L^-1)^t ???
444  for (Size i=1; i<=n_dof_angle_; i++)
445  {
446  dphi[i] = 0.0;
447  for (Size j=i; j<=n_dof_angle_; j++)
448  {
449  dphi[i]+=matrix_Li[j][i]*delta[j];
450  }
451  }
452 
453 
454  for(Size i=1; i<=n_dof_angle_; i++)TR << "dx: "<<delta[i] << endl;
455 
456  //check delta
457 
458  for (Size i=1; i<=n_dof_angle_; i++)
459  {
460  delta[i] = 0.0;
461  for (Size j=i; j<=n_dof_angle_; j++)
462  {
463  delta[i]+=matrix_Lt[i][j]*dphi[j];
464  }
465  }
466  for(Size i=1; i<=n_dof_angle_; i++)TR << "dx: "<<delta[i] << endl;
467 
468  TR << "delta --> d^2:" << d2 << endl;
469  d2 = 0.0;
470  for(Size i=1; i<=n_dof_angle_; i++)
471  {
472  for(Size j=1; j<=n_dof_angle_; j++)
473  {
474  d2+=dphi[j]*matrix_A[i][j]*dphi[i];
475  }
476  }
477  TR << "dphi --> d^2:" << d2 << endl;
478  */
479  //for(Size i=1; i<=n_dof_angle_; i++)TR << "dx: "<<delta[i] << endl;
480  //for(Size i=1; i<=n_dof_angle_; i++)TR << "dphi: "<<dphi[i] << endl;
481 
482  //W_old *= exp(-d^2)
483  Real W_old = detL*exp(-d2/2.0);
484 
485  /*
486  TR << " --Aij-- " << endl;
487  for(Size i=1; i<=n_dof_angle_; i++)
488  {
489  for(Size j=1; j<=n_dof_angle_; j++)
490  {
491  TR<< matrix_A[i][j] << " ";
492  }
493  TR << endl;
494  }
495  TR << " --matrix_Lt-- " << endl;
496  for(Size i=1; i<=n_dof_angle_; i++)
497  {
498  for(Size j=1; j<=n_dof_angle_; j++)
499  {
500  TR<< matrix_Lt[i][j] << " ";
501  }
502  TR << endl;
503  }
504  TR << " --matrix_Li-- " << endl;
505  for(Size i=1; i<=n_dof_angle_; i++)
506  {
507  for(Size j=1; j<=n_dof_angle_; j++)
508  {
509  TR<< matrix_Li[i][j] << " ";
510  }
511  TR << endl;
512  }
513  TR << " --U-- " << endl;
514  for(Size i=1; i<=n_dof_angle_; i++)
515  {
516  for(Size j=1; j<=n_dof_angle_; j++)
517  {
518  TR<< matrix_U[i][j] << " ";
519  }
520  TR << endl;
521  }
522  */
523 
524  //set the new phi,psi (above all called phi, actually 4 phi, 4 psi)
525  pose.set_psi(resnum_, basic::periodic_range( pose.psi(resnum_)+dphi[8], 360.0 ) );
526  pose.set_phi(resnum_, basic::periodic_range( pose.phi(resnum_)+dphi[7], 360.0 ) );
527  pose.set_psi(resnum_-1, basic::periodic_range( pose.psi(resnum_-1)+dphi[6], 360.0 ) ) ;
528  pose.set_phi(resnum_-1, basic::periodic_range( pose.phi(resnum_-1)+dphi[5], 360.0 ) );
529  pose.set_psi(resnum_-2, basic::periodic_range( pose.psi(resnum_-2)+dphi[4], 360.0 ) );
530  pose.set_phi(resnum_-2, basic::periodic_range( pose.phi(resnum_-2)+dphi[3], 360.0 ) );
531  pose.set_psi(resnum_-3, basic::periodic_range( pose.psi(resnum_-3)+dphi[2], 360.0 ) );
532  pose.set_phi(resnum_-3, basic::periodic_range( pose.phi(resnum_-3)+dphi[1], 360.0 ) );
533  /*
534  TR << "detL:" << detL << endl;
535  TR << "d^2:" << d2 << endl;
536  TR << "W_old:" << W_old << endl;*/
537 
538  return W_old;
539 }
540 
542 {
544  //get L
545  Real detL = cholesky_bw(matrix_A, n_dof_angle_, dphi, delta);
546  //delta = L^t * dphi
547  /*
548  for (Size i=1; i<=n_dof_angle_; i++)
549  {
550  delta[i] = 0.0;
551  for (Size j=i; j<=n_dof_angle_; j++)
552  {
553  delta[i]+=matrix_Lt[i][j]*(-dphi[j]);
554  }
555  }
556  */
557  //calculate d^2 = delta^2
558  Real d2=0.0;
559  for (Size i=1; i<=n_dof_angle_; i++)d2+=delta[i]*delta[i];
560 
561  //for(Size i=1; i<=n_dof_angle_; i++)TR << "dx: "<<delta[i] << endl;
562  //for(Size i=1; i<=n_dof_angle_; i++)TR << "dphi: "<<dphi[i] << endl;
563 
564  //dphi=L^-t * delta, L^-t = (L^-1)^t
565  //for(Size i=1; i<=n_dof_angle_; i++)
566  //{
567  // dphi[i] = 0.0;
568  // for(Size j=i; j<=n_dof_angle_; j++)
569  // {
570  // dphi[i]+=matrix_Li[j][i]*delta[j];
571  // }
572  //}
573 
574  //for(Size i=1; i<=n_dof_angle_; i++)TR << "dphi: "<<dphi[i] << endl;
575 
576  /*
577  TR << " --Aij-- " << endl;
578  for(Size i=1; i<=n_dof_angle_; i++)
579  {
580  for(Size j=1; j<=n_dof_angle_; j++)
581  {
582  TR<< matrix_A[i][j] << " ";
583  }
584  TR << endl;
585  }
586  TR << " --matrix_Lt-- " << endl;
587  for(Size i=1; i<=n_dof_angle_; i++)
588  {
589  for(Size j=1; j<=n_dof_angle_; j++)
590  {
591  TR<< matrix_Lt[i][j] << " ";
592  }
593  TR << endl;
594  }
595  TR << " --matrix_Li-- " << endl;
596  for(Size i=1; i<=n_dof_angle_; i++)
597  {
598  for(Size j=1; j<=n_dof_angle_; j++)
599  {
600  TR<< matrix_Li[i][j] << " ";
601  }
602  TR << endl;
603  }
604  TR << " --U-- " << endl;
605  for(Size i=1; i<=n_dof_angle_; i++)
606  {
607  for(Size j=1; j<=n_dof_angle_; j++)
608  {
609  TR<< matrix_U[i][j] << " ";
610  }
611  TR << endl;
612  }
613 
614  TR << "delta --> d^2:" << d2 << endl;
615  d2 = 0.0;
616  for(Size i=1; i<=n_dof_angle_; i++)
617  {
618  for(Size j=1; j<=n_dof_angle_; j++)
619  {
620  d2+=dphi[j]*matrix_A[i][j]*dphi[i];
621  }
622  }
623  TR << "dphi --> d^2:" << d2 << endl;
624  */
625  //W_new *= exp(-d'^2)
626  //Real W_new = detL*exp(-d2/2.0);
627  Real W_new = detL*exp(-d2/2.0);
628 
629  /*
630  TR << "detL:" << detL << endl;
631  TR << "d^2:" << d2 << endl;
632  TR << "W_new:" << W_new << endl;*/
633 
634  return W_new;
635 }
636 
638 {
639  //setup_list(pose);
640  if(available_seg_list_.size()==0)setup_list(pose);
641  if(available_seg_list_.size()==0)return;
642  //randomly select a residue in the list
643  int ndx=static_cast< int >( RG.uniform()*available_seg_list_.size()+1 );
644  Size left = available_seg_list_[ ndx ].first;
645  resnum_ = available_seg_list_[ ndx ].second;
646 
647  if (resnum_-left+1 < n_pert_res_)
648  {
649  //do random
650  pivot_range_randomly(pose, left, resnum_);
651  return;
652  }
653 
654  get_VdRdPhi(pose);
655  get_G();
656  get_A();
657  Real W_old = get_L_move(pose);
658 
659  //TR << endl;
660  //TR << "------new-------" << endl;
661  get_VdRdPhi(pose);
662  get_G();
663  get_A();
664  Real W_new = get_L_prime();
665 
666  last_proposal_density_ratio_ = W_new / W_old;
667 }
668 
669 }//namespace simple_moves
670 }//namespace protocols
671