Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FragmentSampler.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 FragmentSampler.cc
11 /// @brief ab-initio fragment assembly protocol for proteins
12 /// @detailed
13 /// Contains currently: Classic Abinitio
14 ///
15 /// @author Oliver Lange
16 /// @author James Thompson
17 /// @author Mike Tyka
18 
19 // Unit Headers
20 #include <core/types.hh>
21 
22 // Package Headers
24 
25 // Project Headers
27 #include <core/pose/Pose.hh>
33 #include <protocols/jd2/util.hh>
36 
37 // Utility headers
38 #include <utility/exit.hh>
39 #include <utility/excn/Exceptions.hh>
40 #include <utility/vector1.fwd.hh>
41 #include <utility/pointer/ReferenceCount.hh>
42 #include <numeric/numeric.functions.hh>
43 #include <basic/prof.hh>
44 #include <basic/Tracer.hh>
45 #include <basic/options/option.hh>
46 #include <basic/options/keys/abinitio.OptionKeys.gen.hh>
47 #include <basic/options/keys/run.OptionKeys.gen.hh>
48 
49 // C++ headers
50 #include <cstdlib>
51 #include <string>
52 
55 #include <utility/vector1.hh>
56 
57 static basic::Tracer tr("protocols.abinitio");
58 
59 using core::Real;
60 using namespace core;
61 using namespace basic;
62 using namespace basic::options;
63 using namespace basic::options::OptionKeys;
64 
65 /*!
66 @detail call this:
67 FragmentSampler::register_options() before devel::init().
68 Derived classes that overload this function should also call Parent::register_options()
69 */
72  using namespace basic::options;
73  using namespace basic::options::OptionKeys;
74  option.add_relevant( OptionKeys::abinitio::increase_cycles );
75  option.add_relevant( OptionKeys::abinitio::debug );
76  option.add_relevant( OptionKeys::abinitio::skip_stages );
77  option.add_relevant( OptionKeys::abinitio::skip_convergence_check );
78  option.add_relevant( OptionKeys::abinitio::log_frags );
79  option.add_relevant( OptionKeys::abinitio::end_bias );
80  option.add_relevant( OptionKeys::abinitio::symmetry_residue );
81  option.add_relevant( OptionKeys::abinitio::vdw_weight_stage1 );
82  option.add_relevant( OptionKeys::abinitio::override_vdw_all_stages );
83  option.add_relevant( OptionKeys::abinitio::recover_low_in_stages );
84  option.add_relevant( OptionKeys::abinitio::close_chbrk );
85 }
86 
87 namespace protocols {
88 namespace abinitio {
89 
90 //little helper function
92  return find( vec.begin(), vec.end(), query) != vec.end();
93 }
94 
95 /// @detail large (stage1/stage2)
96 /// small(stage2/stage3/stage4)
97 /// smooth_small ( stage3/stage4)
98 FragmentSampler::FragmentSampler( topology_broker::TopologyBrokerOP broker )
99  : topology_broker_( broker ),
100  checkpoints_("FragmentSampler")
101 {
102  BaseClass::type( "FragmentSampler" );
103 
104  set_defaults();
105 }
106 
108 
109 /// @brief FragmentSampler has virtual functions... use this to obtain a new instance
112 {
113  return new FragmentSampler( *this );
114 }
115 
117  core::pose::Pose& pose,
118  StageID stage_id,
119  void (FragmentSampler::*cycles)(core::pose::Pose& )
120 ) {
121 
122  // part X ----------------------------------------
123  tr.Info << "\n===================================================================\n";
124  tr.Info << " " << id2string( stage_id ) << " \n";
125  tr.Info << "--------------------------------------------------------------------\n";
126  tr.Info << "FragmentSampler: " << id2string( stage_id ) << std::endl;
127  PROF_START( id2proftag( stage_id ) );
128  clock_t starttime = clock();
129 
130  if ( !get_checkpoints().recover_checkpoint( pose, get_current_tag(), id2string( stage_id ), false /* fullatom*/, true /*fold tree */ )) {
131  scoring::constraints::ConstraintSetOP orig_constraints( pose.constraint_set()->clone() );
132 
133  //run the fragment cycles
134  (this->*cycles)( pose ); //calls do_stageX_cycles()
135 
136  recover_low( pose, stage_id );
137  if ( tr.Info.visible() ) current_scorefxn().show( tr.Info, pose );
138  tr.Info << std::endl;
139  mc().show_counters();
141  mc().reset_counters();
142 
143  pose.constraint_set( orig_constraints ); // restore constraints - this is critical for checkpointing to work
144  get_checkpoints().checkpoint( pose, get_current_tag(), id2string( stage_id ), true /*fold tree */ );
145  } //recover checkpoint
146  get_checkpoints().debug( get_current_tag(), id2string( stage_id ), current_scorefxn()( pose ) );
147 
148  clock_t endtime = clock();
149  PROF_STOP( id2proftag( stage_id ) );
150  if ( option[ basic::options::OptionKeys::run::profile ] ) prof_show();
151 
153  tr.Info << "Timeperstep: " << (double(endtime) - starttime )/(CLOCKS_PER_SEC ) << std::endl;
154  }
155  jd2::output_intermediate_pose( pose, id2string( stage_id ) );
156 }
157 
159  using namespace moves;
160  using namespace scoring;
161  using namespace scoring::constraints;
162 
163  tr.Info << "Fragment Sampler: " << get_current_tag() << std::endl;
164 
165  runtime_assert( topology_broker_ ); // really this protocol doesn't make much sense without it
166  mc().clear_poses(); // these two statements were only necessary after march 18 2009... something ALF did recently ?
167  mc().reset( pose );
168  // current_scorefxn()( pose );
169  if ( option[ OptionKeys::run::dry_run ]() ) {
170  replace_scorefxn( pose, STAGE_4, 1.0 );
171  return;
172  }
173 
174  total_trials_ = 0;
175  current_scorefxn()(pose);
176  jd2::output_intermediate_pose( pose, "stage0" );
177 
178  try {
180  prepare_stage1( pose );
182  } //skipStage1
183 
185  prepare_stage2( pose );
187  }
188 
190  prepare_stage3( pose );
192  }
193 
195  prepare_stage4( pose );
197  }
198 
199  tr.Info << "\n===================================================================\n";
200  tr.Info << " Finished Abinitio \n";
201  tr.Info << std::endl;
203  } catch ( moves::EXCN_Converged& excn ) {
204  // Size last_stage( STAGE_4 );
205  // while( contains_stageid( skip_stages_, last_stage ) ) --last_stage;
206  mc().recover_low( pose );
208  };
209 
210  replace_scorefxn( pose, STAGE_4, 1.0 ); ///here so we activate all chainbreaks and constraints
211  return;
212 }// FragmentSampler::apply( pose::Pose & pose )
213 
214 
217  return "FragmentSampler";
218 }
219 
220 //@detail read cmd_line options and set default versions for many protocol members: trials/moves, score-functions, Monte-Carlo
222  using namespace basic::options;
223  temperature_ = 2.0;
226  set_default_mc( *score_stage4_ );//if we skip all stages.. this is probably the least surprising "last used scorefxn"
227 }
228 
229 //@detail sets Monto-Carlo object to default
231  scoring::ScoreFunction const & scorefxn
232 ) {
233  set_mc( new moves::MonteCarlo( scorefxn, temperature_ ) );
235 }
236 
237 //@detail sets Monto-Carlo object
239  mc_ = mc_in;
240 }
241 
242 //@detail override cmd-line setting for "increase_cycling"
243 void FragmentSampler::set_cycles( Real increase_cycles ) {
244  stage1_cycles_ = static_cast< int > (2000 * increase_cycles);
245  stage2_cycles_ = static_cast< int > (2000 * increase_cycles);
246  stage3_cycles_ = static_cast< int > (2000 * increase_cycles);
247  stage4_cycles_ = static_cast< int > (4000 * increase_cycles);
248 
249  using namespace basic::options;
250 }
251 
253  topology_broker_ = set;
254 }
255 
257  runtime_assert( topology_broker_ );
258  return *topology_broker_;
259 }
260 
262  using namespace scoring;
263  using namespace basic::options;
264  using namespace basic::options::OptionKeys;
265 
266  tr.Debug << "creating standard scoring functions" << std::endl;
267 
268  if ( option[ OptionKeys::abinitio::stage1_patch ].user() ) {
269  score_stage1_ = ScoreFunctionFactory::create_score_function( "score0", option[ OptionKeys::abinitio::stage1_patch ]() );
270  } else {
272  }
273 
274  if ( option[ OptionKeys::abinitio::stage2_patch ].user() ) {
275  score_stage2_ = ScoreFunctionFactory::create_score_function( "score1", option[ OptionKeys::abinitio::stage2_patch ]() );
276  } else {
278  }
279 
280  if ( option[ OptionKeys::abinitio::stage3a_patch ].user() ) {
281  score_stage3a_ = ScoreFunctionFactory::create_score_function( "score2", option[ OptionKeys::abinitio::stage3a_patch ]() );
282  } else {
284  }
285 
286  if ( option[ OptionKeys::abinitio::stage3b_patch ].user() ) {
287  score_stage3b_ = ScoreFunctionFactory::create_score_function( "score5", option[ OptionKeys::abinitio::stage3b_patch ]() );
288  } else {
290  }
291 
292  if ( option[ OptionKeys::abinitio::stage4_patch ].user() ) {
293  score_stage4_ = ScoreFunctionFactory::create_score_function( "score3", option[ OptionKeys::abinitio::stage4_patch ]() );
294  } else {
296  }
297 
298  if ( option[ OptionKeys::abinitio::override_vdw_all_stages ] ) {
299  set_score_weight( scoring::vdw, option[ OptionKeys::abinitio::vdw_weight_stage1 ], ALL_STAGES );
300  }
301 }
302 
303 
304 /// @brief sets a score weight for all stages of abinitio
306  tr.Debug << "set score weights for ";
307  if ( stage == ALL_STAGES ) tr.Debug << "all stages ";
308  else tr.Debug << "stage " << (stage <= STAGE_3a ? stage : ( stage-1 ) ) << ( stage == STAGE_3b ? "b " : " " );
309  tr.Debug << scoring::name_from_score_type(type) << " " << setting << std::endl;
310  if (score_stage1_ && ( stage == STAGE_1 || stage == ALL_STAGES )) score_stage1_ ->set_weight(type, setting);
311  if (score_stage2_ && ( stage == STAGE_2 || stage == ALL_STAGES )) score_stage2_ ->set_weight(type, setting);
312  if (score_stage3a_ && ( stage == STAGE_3a || stage == ALL_STAGES )) score_stage3a_->set_weight(type, setting);
313  if (score_stage3b_ && ( stage == STAGE_3b || stage == ALL_STAGES )) score_stage3b_->set_weight(type, setting);
314  if (score_stage4_ && ( stage == STAGE_4 || stage == ALL_STAGES )) score_stage4_ ->set_weight(type, setting);
315 }
316 
317 //@brief currently used score function ( depends on stage )
319  return mc().score_function();
320 }
321 
322 //@brief set current scorefunction
324  mc().score_function( scorefxn );
325 }
326 
327 //@brief set individual weight of current scorefunction --- does not change the predefined scores: score_stageX_
329  scoring::ScoreFunctionCOP scorefxn_const ( mc().score_function() );
330  scoring::ScoreFunctionOP scorefxn = scorefxn_const->clone();
331  scorefxn->set_weight( type, setting );
332  mc().score_function( *scorefxn ); //trigger rescore
333 }
334 
336  using namespace basic::options;
337  just_smooth_cycles_ = option[ OptionKeys::abinitio::smooth_cycles_only ]; // defaults to false
338  bQuickTest_ = basic::options::option[ basic::options::OptionKeys::run::test_cycles ]();
339 
340  if ( bQuickTest() ) {
341  set_cycles( 0.001 );
342  } else {
343  set_cycles( option[ OptionKeys::abinitio::increase_cycles ] ); // defaults to factor of 1.0
344  }
345 
346  apply_large_frags_ = true; // apply large frags in phase 2!
347 
348  // in rosetta++ switched on in fold_abinitio if contig_size < 30 in pose_abinitio never
349  short_insert_region_ = false; // apply small fragments in phase 2!
350 
351  skip_stages_.clear();
352  if ( option[ OptionKeys::abinitio::skip_stages ].user() ) {
353  for ( IntegerVectorOption::const_iterator it = option[ OptionKeys::abinitio::skip_stages ]().begin(),
354  eit = option[ OptionKeys::abinitio::skip_stages ]().end(); it!=eit; ++it ) {
355  if ( *it == 1 ) skip_stages_.push_back( STAGE_1 );
356  else if ( *it == 2 ) skip_stages_.push_back( STAGE_2 );
357  else if ( *it == 3 ) skip_stages_.push_back( STAGE_3 );
358  else if ( *it == 4 ) skip_stages_.push_back( STAGE_4 );
359  }
360  }
361 
362  if ( option[ OptionKeys::abinitio::recover_low_in_stages ].user() ) {
363  for ( IntegerVectorOption::const_iterator it = option[ OptionKeys::abinitio::recover_low_in_stages ]().begin(),
364  eit = option[ OptionKeys::abinitio::recover_low_in_stages ]().end(); it!=eit; ++it ) {
365  if ( *it == 1 ) recover_low_stages_.push_back( STAGE_1 );
366  else if ( *it == 2 ) recover_low_stages_.push_back( STAGE_2 );
367  else if ( *it == 3 ) {
368  recover_low_stages_.push_back( STAGE_3a );
369  recover_low_stages_.push_back( STAGE_3b );
370  }
371  else if ( *it == 4 ) recover_low_stages_.push_back( STAGE_4 );
372  }
373  } else {
374  recover_low_stages_.clear();
375  recover_low_stages_.push_back( STAGE_1 );
376  recover_low_stages_.push_back( STAGE_2 );
377  recover_low_stages_.push_back( STAGE_3a );
378  recover_low_stages_.push_back( STAGE_3b );
379  recover_low_stages_.push_back( STAGE_4 );
380  }
381 }
382 
384 FragmentSampler::mover( pose::Pose const& pose, StageID stage_id, core::scoring::ScoreFunction const& scorefxn, core::Real progress ) {
385  topology_broker().apply_filter( pose, stage_id, progress ); //raises exception if filter failed
386  return topology_broker().mover( pose, stage_id, scorefxn, progress );
387 }
388 
391  mc().reset( pose ); // make sure that we keep the final structure
392  jd2::output_intermediate_pose( pose, "stage1_cycles" );
393 }
394 
397 }
398 
399 /*! @detail stage3 cycles:
400  nloop1 : outer iterations
401  nloop2 : inner iterations
402  stage3_cycle : trials per inner iteration
403  every inner iteration we switch between score_stage3a ( default: score2 ) and score_stage3b ( default: score 5 )
404 
405  prepare_loop_in_stage3() is called before the stage3_cycles() of trials are started.
406 
407  first outer loop-iteration is done with TrialMover trial_large()
408  all following iterations with trial_small()
409 
410  start each iteration with the lowest_score_pose. ( mc->recover_low() -- called in prepare_loop_in_stage3() )
411 
412 */
414  using namespace ObjexxFCL;
415 
416  // interlaced score2 / score 5 loops
417  // nloops1 and nloops2 could become member-variables and thus changeable from the outside
418  int nloop1 = 1;
419  int nloop2 = 10; //careful: if you change these the number of structures in the structure store changes.. problem with checkpointing
420  // individual checkpoints for each stage3 iteration would be a remedy. ...
421 
422  if ( short_insert_region_ ) {
423  nloop1 = 2;
424  nloop2 = 5;
425  }
426  Size const total_iterations ( nloop1*nloop2 );
427 
428  int iteration = 1;
429  for ( int lct1 = 1; lct1 <= nloop1; lct1++) {
430  for ( int lct2 = 1; lct2 <= nloop2; lct2++, iteration++ ) {
431  tr.Debug << "Loop: " << lct1 << " " << lct2 << std::endl;
432  StageID current_stage_id = ( numeric::mod( (int)iteration, 2 ) == 0 || iteration > 7 ) ? STAGE_3a : STAGE_3b;
433  prepare_loop_in_stage3( pose, iteration, nloop1*nloop2 );
434  core::Real progress( 1.0* iteration/total_iterations );
435 
436  if ( !get_checkpoints().recover_checkpoint( pose, get_current_tag(), "stage_3_iter"+string_of( lct1)+"_"+string_of(lct2),
437  false /*fullatom */, true /*fold tree */ )) {
438 
439  tr.Debug << " Score stage3 loop iteration " << lct1 << " " << lct2 << std::endl;
440  moves::TrialMoverOP stage3_trials = new moves::TrialMover( mover( pose, current_stage_id, current_scorefxn(), progress ), mc_ptr() );
441  moves::RepeatMover( stage3_trials, stage3_cycles() ).apply( pose );
442 
443  recover_low( pose, current_stage_id );
444  get_checkpoints().checkpoint( pose, get_current_tag(), "stage_3_iter"+string_of( lct1)+"_"+string_of(lct2), true /*fold tree */ );
445  }//recover_checkpoint
446  get_checkpoints().debug( get_current_tag(), "stage_3_iter"+string_of( lct1)+"_"+string_of(lct2), current_scorefxn()( pose ) );
447  } // loop 2
448  } // loop 1
449 }
450 
452  Size nloop_stage4 = 3;
453  for ( Size kk = 1; kk <= nloop_stage4; ++kk ) {
454  prepare_loop_in_stage4( pose, kk, nloop_stage4 );
455 
456  if (!get_checkpoints().recover_checkpoint( pose, get_current_tag(), "stage4_kk_" + ObjexxFCL::string_of(kk), false /* fullatom */, true /* fold_tree */ )) {
457  tr.Debug << "start " << stage4_cycles() << " cycles" << std::endl;
458  moves::TrialMoverOP stage4_trials = new moves::TrialMover( mover( pose, STAGE_4, current_scorefxn(), 1.0*kk/nloop_stage4 ), mc_ptr() );
459  moves::RepeatMover( stage4_trials, stage4_cycles() ).apply( pose );
460  tr.Debug << "finished" << std::endl;
461 
462  recover_low( pose, STAGE_4 );
463  get_checkpoints().checkpoint( pose, get_current_tag(), "stage4_kk_" + ObjexxFCL::string_of(kk), true /*fold tree */ );
464  }
465  get_checkpoints().debug( get_current_tag(), "stage4_kk_" + ObjexxFCL::string_of(kk), current_scorefxn()( pose ) );
466 
467  //don't store last structure since it will be exactly the same as the final structure delivered back via apply
468  } // loop kk
469 }
470 
472  if ( contains_stageid( recover_low_stages_, stage ) ) {
473  if ( tr.Trace.visible() ) current_scorefxn().show( tr.Trace, pose );
474  tr.Trace << "================ RECOVER LOW ==============" << std::endl;
475  mc_->recover_low( pose );
476  if ( tr.Trace.visible() ) current_scorefxn().show( tr.Trace, pose );
477  tr.Trace << std::endl;
478  } else {
479  tr.Trace << "================ RECOVER LOW SKIPPED ===============" << std::endl;
480  }
481 }
482 
483 // anything you want to have done before the stages ?
484 void FragmentSampler::replace_scorefxn( core::pose::Pose& pose, StageID stage, core::Real /*intra_stage_progress */ ) {
485  // must assume that the current pose is the one to be accepted into the next stage! (this change was necessary for
486  // checkpointing to work correctly. --> that means no recover_low at this point! have to call it explicitly in stage3_loops
487 
488  //intra_stage_progress = intra_stage_progress;
489  if (score_stage1_ && ( stage == STAGE_1 )) current_scorefxn( *score_stage1_ );
490  if (score_stage2_ && ( stage == STAGE_2 )) current_scorefxn( *score_stage2_ );
491  if (score_stage3a_ && ( stage == STAGE_3a || stage == STAGE_3 )) current_scorefxn( *score_stage3a_ );
492  if (score_stage3b_ && ( stage == STAGE_3b)) current_scorefxn( *score_stage3b_ );
493  if (score_stage4_ && ( stage == STAGE_4 )) current_scorefxn( *score_stage4_ );
494  mc_->set_autotemp( true, temperature_ );
495  mc_->set_temperature( temperature_ ); // temperature might have changed due to autotemp..
496  mc_->reset( pose );
497 
498  current_scorefxn()(pose);
499 }
500 
501 // prepare stage1 sampling
503  replace_scorefxn( pose, STAGE_1, 0.5 );
504  mc_->set_autotemp( false, temperature_ );
505  /// Now handled automatically. score_stage1_->accumulate_residue_total_energies( pose ); // fix this
506 }
507 
509  replace_scorefxn( pose, STAGE_2, 0.5 );
510 }
511 
513  replace_scorefxn( pose, STAGE_3a, 0 );
514 }
515 
517  replace_scorefxn( pose, STAGE_4, 0 );
518 }
519 
520 void FragmentSampler::prepare_loop_in_stage3( core::pose::Pose &pose/*pose*/, Size iteration, Size total ){
521  if ( numeric::mod( (int)iteration, 2 ) == 0 || iteration > 7 ) {
522  replace_scorefxn( pose, STAGE_3a, 1.0* iteration/total );
523  } else {
524  replace_scorefxn( pose, STAGE_3b, 1.0* iteration/total );
525  }
526 }
527 
529  replace_scorefxn( pose, STAGE_4, 1.0* iteration/total );
530 }
531 
532 std::string const FragmentSampler::id2string_[] = { "all_stages", "stage1", "stage2", "stage3", "stage3", "stage3", "stage4"};
533 
534 basic::ProfTag const FragmentSampler::id2proftag_[] = { basic::STAGE1/*ALL_STAGES*/, basic::STAGE1, basic::STAGE2, basic::STAGE3, basic::STAGE3, basic::STAGE3, basic::STAGE4 };
535 
536 } //abinitio
537 } //protocols