Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
looprelax_protocols.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 relax_protocols
11 /// @brief protocols that are specific to LoopRebuild
12 /// @detailed
13 /// @author Mike Tyka
14 /// @author James Thompson
15 /// @author Srivatsan Raman
16 
20 
33 #include <core/id/AtomID.hh>
35 #include <utility/file/FileName.hh>
36 #include <core/types.hh>
37 #include <core/io/pdb/pose_io.hh>
38 #include <core/pose/Pose.hh>
39 #include <basic/options/option.hh>
45 #include <protocols/loops/Loop.hh>
46 #include <protocols/loops/Loops.hh>
51 #include <core/fragment/FragSet.hh>
52 #include <basic/Tracer.hh>
53 #include <core/scoring/Energies.hh>
54 #include <numeric/model_quality/rms.hh>
55 
56 // External library headers
57 #include <utility/exit.hh>
58 #include <numeric/random/random.hh>
59 
60 //C++ headers
61 #include <vector>
62 #include <string>
63 #include <sstream>
64 #if defined(WIN32) || defined(__CYGWIN__)
65  #include <ctime>
66 #endif
67 
68 // option key includes
69 
70 #include <basic/options/keys/loops.OptionKeys.gen.hh>
71 #include <basic/options/keys/constraints.OptionKeys.gen.hh>
72 
73 
74 #include <core/pose/util.hh>
75 #include <utility/vector0.hh>
76 #include <utility/vector1.hh>
77 #include <ObjexxFCL/format.hh>
78 
79 //Auto Headers
81 
82 
83 using basic::T;
84 using basic::Error;
85 using basic::Warning;
86 
87 //using namespace ObjexxFCL;
88 
89 namespace protocols {
90 
91 static basic::Tracer TR("protocols.looprelax_protocols");
92 
93 using namespace core;
94 using io::pdb::dump_pdb;
95 
96 static numeric::random::RandomGenerator RG(431111); // <- Magic number, do not change it!
97 
98 
99 
100 using namespace protocols::loops;
101 
104  protocols::loops::Loops Loops_in
105  ) : Mover(),
106  scorefxn_( scorefxn ),
107  Loops_in_( Loops_in )
108  {
110  Mover::type("LoopRebuild");
112  }
113 
115 
116 /// @brief Clone this object
119  return new LoopRebuild(*this);
120 }
121 
122 
123 /// @details Apply the loop rebuilding protocol to a pose.
124 /// The loops to be rebuilt and fragments are defined upon object construction.
126  using namespace basic::options;
127  using namespace basic::options::OptionKeys;
128 
129  //core::Real score_filter_cutoff;
130  bool passed_score_filter( false );
131  int const n_score_filter_fail_tol( 2 );
132  int n_score_filter_fail( 0 );
133  int n_loop_fail_tol( 2 );
134  int loop_build_round( 1 ); //build full loops this many times
135  success = false;
136 
137  //setting mc object
138  if( !mc_created )
139  set_default_mc( pose );
140 
141  //initialize fragments
142  // std::map< Size, protocols::frags::TorsionFragmentLibraryOP > frag_libs;
143  // initialize_fragments( frag_libs );
144 
145  // found_loops = read_loop_file( ori_loops_begin, ori_loops_end );
146  TR.Debug << "Loop file size : " << Loops_in_.size() << std::endl;
147  //read_coord_cst(); //include this function later !
148 
149  Loops_in_.verify_against( pose );
151  //score_filter_cutoff = get_score_filter_cutoff(); // set but never used ~Labonte
152 
153  TR.Debug << "Loop file size (after filtering) : " << Loops_in_.size() << std::endl;
154  //save the initial pose in case failed scorefilter
155  pose::Pose init_pose_obj;
156  init_pose_obj = pose;
157 
158  bool loop_done( false );
159  while( !passed_score_filter && n_score_filter_fail < n_score_filter_fail_tol ){
160  n_score_filter_fail++;
161 
162  //reset pose
163  pose = init_pose_obj;
164 
165 
166  int loop_nfail( 0 );
167  loop_done = false;
168  while( !loop_done && loop_nfail <= n_loop_fail_tol ) {
169  loop_nfail++;
170  for( int i = 0; i < loop_build_round; ++i ) {
171  loop_done = build_random_loops( pose );
172  }
173  }
174 
175  if (!loop_done) TR.Error << "build_random_loops() returned failure." << std::endl;
176 
177  passed_score_filter = true; //change this to a call to score_filter function, once it is written
178  }
179 
180  success = (loop_done && passed_score_filter);
181  if (!success) TR.Error << "LoopRebuild::apply() returned false" << std::endl;
182 }
183 
186  return "LoopRebuild";
187 }
188 
189 
190 ///////////////////////////////////////////////////////////////////////
192  using namespace basic::options;
193  using namespace basic::options::OptionKeys;
194 
195  static core::Real rmsd_tol = { 10000.0 };
196  static bool init = { false };
197 
198  if ( !init ) {
199  rmsd_tol = option[ OptionKeys::loops::rmsd_tol ]();
200  if( rmsd_tol <= 0 ) rmsd_tol = 10000.0;
201  init = true;
202  }
203  return rmsd_tol;
204 }
205 
206 
207 ////////////////////////////////////////////////////////////////////////
209  using namespace basic::options;
210  using namespace basic::options::OptionKeys;
211 
212  static core::Real chain_break_tol = { 0.2 };
213  static bool init = { false };
214 
215  if ( !init ) {
216 
217  chain_break_tol = option[ OptionKeys::loops::chain_break_tol ]();
218  init = true;
219  }
220  return chain_break_tol;
221 }
222 
223 
224 ///////////////////////////////////////////////////////////////////////
225 /// @details Rebuild all the loops in the pose, one at a time, choosing each in random order.
227  using namespace basic::options;
228  using namespace basic::options::OptionKeys;
229  int const nres( pose.total_residue() );
230 
231  std::vector< int > free_res; // stores residue numbers in real loops
232  for( Loops::const_iterator it=Loops_in_.begin(), it_end=Loops_in_.end(); it != it_end; ++it ) {
233  TR.Debug << "Loop res " << it->start() << " " << it->stop() << std::endl;
234  for ( int k = (int)it->start(); k <= (int)it->stop(); ++k )
235  free_res.push_back(k);
236  }
237 
238  std::vector<int> inter_res; // residues in-between loop_file defined loops
239 
240  // now go through all the loop regions
241  core::pose::Pose stage_pose; // stores a pose at certain stage
242  std::vector< int > folded_loops; // loops folded
243  int num_desired_loops ( get_desired_loops_exist()? std::min( desired_loops(), Loops_in_.size() ) : Loops_in_.size() );
244  int cutpoint(0);
245 
246  if ( loop_model() ) num_desired_loops = Loops_in_.size(); // build all obligate loops
247 
248 
249  for ( int loop_counter = 0; loop_counter < num_desired_loops; ++loop_counter ){
250 
251  // save the original pose at this stage
252  stage_pose = pose;
253 
254  // randomly select one loop from the loop regions
255  inter_res.clear();
256  int selected_loop, def_loop_begin, def_loop_end, combine_number;
257  bool loops_combined( false );
258  bool extend_this_loop = option[ OptionKeys::loops::extended ].user();
259  bool use_selected_loop =
260  select_one_loop( nres, selected_loop, folded_loops,
261  inter_res, def_loop_begin, def_loop_end, cutpoint, extend_this_loop,
262  loops_combined, combine_number, loop_counter );
263 
264  if ( !use_selected_loop ) continue; // skip this loop
265 
266  // params record the behaviors of individual loop modeling process
267  int nfail( 0 );
268  int n_chain_break_fail( 1 );
269  bool is_chain_break( true );
270  bool rmsd_acceptable( false );
271  int barcst_extend_begin( 0 );
272  int barcst_extend_end( 0 );
273  int total_combine( 0 );
274  if ( loops_combined ) total_combine = combine_number;
275  int backward_combine( 0 );
276 
277  // select further random stems which will be constrainted by barcode cst
278  if ( get_random_loop_flag() ){
279  barcst_extend_begin = static_cast <int> ( numeric::random::uniform() * 2 );
280  barcst_extend_end = static_cast <int> ( numeric::random::uniform() * 2 );
281  }
282 
283  // store starting fold tree
284  kinematics::FoldTree f_orig=pose.fold_tree();
285 
286  int final_loop_begin=0;
287  int final_loop_end=0;
288  int time_start = time(NULL);
289  float time_per_build = 0.0;
290  int nclosurefail = 0;
291  int nrmsfail = 0;
292 
293  while( is_chain_break || !rmsd_acceptable ) {
294  // epic fail
295  if( nfail++ > get_allowed_failure_before_stop() ) {
296  // restore simple fold tree
297  TR.Error << "Failed to rebuild loop " << nfail << " times! Aborting." << std::endl;
298  remove_cutpoint_variants( pose );
299  pose.fold_tree( f_orig );
300 
301  if( get_abort_on_failed_loop() ) return false;
302  else break;
303  }
304 
305  // refresh pose
306  pose = stage_pose;
307 
308  // combine next loop if this loop can't be closed after 20 iterations
310  if ( n_chain_break_fail % get_allowed_failure_before_extend() == 0 ){
311  if ( core::Size(selected_loop + total_combine + 1) < Loops_in_.size() ){
312  def_loop_end = Loops_in_[ selected_loop + total_combine + 1 ].stop();
313  extend_this_loop |= Loops_in_[ selected_loop + total_combine + 1 ].is_extended();
314  if( !loop_model() ) folded_loops.push_back( selected_loop + total_combine + 1);
315  total_combine++; // one more combined loop
316  } else if ( selected_loop - backward_combine > 1 ){
317  def_loop_begin = Loops_in_[ selected_loop - backward_combine - 1 ].start();
318  extend_this_loop |= Loops_in_[ selected_loop - backward_combine - 1 ].is_extended();
319  if( !loop_model() ) folded_loops.push_back( selected_loop - backward_combine - 1 );
320  backward_combine++;
321  }
322  else
323  break;// return false; // give up if no loop to combine
324 
325  if( !loop_model() )
326  loop_counter++;
327 
328  barcst_extend_begin = static_cast <int> ( numeric::random::uniform() * 2 );
329  barcst_extend_end = static_cast <int> ( numeric::random::uniform() * 2 );
330  }
331  }
332 
333  // further extend loop regions, and add barcode constraints on these
334  // extended regions
335  int loop_begin = def_loop_begin - 1;
336  int loop_end = def_loop_end;
337  if( loop_begin < 1 ) loop_begin = 1;
338  if( loop_end > nres ) loop_end = nres;
339 
340  if( get_random_loop_flag() ){
341  barcode_extend_stems( pose, barcst_extend_begin, barcst_extend_end,
342  loop_begin, loop_end, def_loop_begin, def_loop_end,
343  nres, selected_loop, total_combine,
344  backward_combine );
345  }
346 
347  if( option[ OptionKeys::loops::extended_beta ].user() ){
348  Real expfactor = exp( - option[ OptionKeys::loops::extended_beta ]() * fabs( (double) loop_begin - loop_end ) );
349  bool stochastic_extend_this_loop = ( numeric::random::uniform() < expfactor );
350  extend_this_loop = extend_this_loop || stochastic_extend_this_loop;
351  }
352 
353  build_loop_with_ccd_closure( pose, loop_begin, loop_end, cutpoint, extend_this_loop );
354 
355  // extend more barcode regions
356  extend_barcode_regions_if_chain_break( pose, loop_begin, loop_end,
357  n_chain_break_fail, is_chain_break,
358  barcst_extend_begin, barcst_extend_end );
359  rmsd_acceptable = acceptable_rmsd_change( stage_pose, pose );
360 
361  if( ! rmsd_acceptable ){ nclosurefail++; TR.Info << "WARNING: Loop Built, rms not acceptable - trying again" << std::endl; }
362  if( is_chain_break ){ nrmsfail++; TR.Info << "WARNING: Chain_break_remains - tryign again" << std::endl; }
363 
364  final_loop_begin = loop_begin;
365  final_loop_end = loop_end;
366  }//accepted this loop
367 
368  int time_end = time(NULL);
369  time_per_build = float(time_end - time_start) / float(nfail);
370 
371  using namespace ObjexxFCL::fmt;
372  TR << "Loopstat: "
373  << " " << I(3,def_loop_begin)
374  << " " << I(3,def_loop_end )
375  << " " << I(3,final_loop_begin )
376  << " " << I(3,final_loop_end )
377  << " " << I(3,final_loop_end - final_loop_begin )
378  << " time " << F(5,1,time_per_build )
379  << " " << I(3,nfail)
380  << " " << I(3,nclosurefail )
381  << " " << I(3,nrmsfail)
382  << " " << (extend_this_loop ? std::string(" ext ") : std::string(" noext " )) << std::endl;
383 
384 
385  remove_cutpoint_variants( pose ); // remove cutpoint variants
386  pose.fold_tree( f_orig ); // restore simple fold tree
387 
388  } // all loops folded
389 
390  return true;
391 }
392 
393 
394 ////////////////////////////////////////////////////////////////////////////////
396  int const & loop_begin,
397  int const & loop_end,
399  ) {
400  using namespace core::id;
401 
402  mm.set_bb( false );
403  mm.set_jump( false );
404 
405  for ( int i = loop_begin; i <= loop_end; ++i ) {
406  mm.set_bb( i, true );
407  mm.set( TorsionID(i,BB,3), false ); //omega is fixed
408  mm.set_chi( i, true );
409  }
410 }
411 
412 
413 
414 
415 
416 
417 
418 //////////////////////////////////////////////////////////////////////////////////
419 /// @details Rebuild a loop via fragment insertion + ccd closure + minimization
421  core::pose::Pose & pose,
422  int const & loop_begin,
423  int const & loop_end,
424  int & cutpoint,
425  bool const & extend_this_loop
426 ) {
427  using namespace kinematics;
428  using namespace scoring;
429  using namespace optimization;
430  using namespace protocols::simple_moves;
431  using namespace basic::options;
432  using namespace basic::options::OptionKeys;
433  using namespace numeric::random;
434 
435  int const nres = pose.total_residue();
436 
437 
438  kinematics::MoveMap mm_one_loop;
439  set_looprlx_allow_move_map( loop_begin, loop_end, mm_one_loop );
440  if ( cutpoint== 0 ){
441  Loop myloop( loop_begin+1, loop_end, 0 );
442  myloop.choose_cutpoint( pose );
443  cutpoint = myloop.cut();
444  }
445  int cut_orig = cutpoint;
446  TR.Info << "Loop and cutpoint: " << loop_begin
447  << " " << cutpoint
448  << " " << loop_end
449  << (extend_this_loop ? " extended " : " nonextended" ) << std::endl;
450 
451  /////
452  // ensure (cutpoint+1) is not a proline (this is a horrible hack -fpd)
453  int ntries = 0;
454  while (cutpoint != nres &&
455  pose.residue(cutpoint+1).aa() == core::chemical::aa_pro &&
456  ntries < 100) {
457  Loop myloop( loop_begin, loop_end );
458  myloop.choose_cutpoint( pose );
459  cutpoint = myloop.cut();
460  ntries++;
461  }
462  // if not sussessfully chosen cutpoint, just chose whatever is not a
463  // profile in the loop !
464  ntries = 0;
465  while (cutpoint != nres &&
466  pose.residue(cutpoint+1).aa() == core::chemical::aa_pro &&
467  ntries < 100) {
468  cutpoint = loop_begin + int( float(loop_end - loop_begin) * uniform() );
469  ntries++;
470  }
471  if (cutpoint != nres && pose.residue(cutpoint+1).aa() == core::chemical::aa_pro ) {
472  TR << " Unable to rebuild loop [" << loop_begin << ", " << loop_end << "] " << std::endl;
473  return;
474  }
475  if (cutpoint != cut_orig)
476  TR << " Changing cutpoint to " << cutpoint << "" << std::endl;
477 
478 
479  int const loop_size( loop_end - loop_begin + 1 );
480  core::Real const cycle_ratio( std::max( 0.2 , get_looprlx_cycle_ratio() ) ); // 0.5->0.2, fpd
481  int cycles2;
482  int cycles3;
483 
484  if( get_ccd_closure_exist() ) {
485  cycles2 = loop_model() ? 3:2 ;
486  int base_cycles( std::max( 15, static_cast<int>( 5*loop_size*cycle_ratio )));
487  cycles3 = loop_model() ? 2*base_cycles:base_cycles;
488  } else {
489  cycles2 = 10;
490  cycles3 = std::max( 30, static_cast<int>( 10*loop_size*cycle_ratio));
491  }
492 
493  TR << "Number of cycles: cycles2 and cycles3 " << cycles2 << " " << cycles3 << std::endl;
494 
495 
496  /// ----- Set up repacker -------------------------------------------------------------
497  pack::task::PackerTaskOP base_packer_task( pack::task::TaskFactory::create_packer_task( pose ));
498  pack::task::PackerTaskOP this_packer_task( base_packer_task->clone() );
499  utility::vector1< bool > allow_repack( nres, false );
500  for ( int i = loop_begin; i <= loop_end; ++i )
501  allow_repack[ i ] = true;
502  this_packer_task->restrict_to_residues( allow_repack );
503 
504  set_single_loop_fold_tree( pose, Loop(loop_begin, loop_end, cutpoint ) );
505 
506  bool chainbreak_present = ( loop_begin != 1 && loop_end != nres );
507 
508  // special case ... vrt res at last position
509  chainbreak_present &= (loop_end != nres-1 || pose.residue( nres ).aa() != core::chemical::aa_vrt );
510 
511  // set cutpoint variant residue for chainbreak score if chanbreak is present
512  if( chainbreak_present ){
515  }
516 
517  (*scorefxn_)(pose);
518  /// Now handled automatically. scorefxn_->accumulate_residue_total_energies( pose );
519  core::pose::Pose start_pose = pose;
520 
521 
522  // either extend or at least idealize the loop (just in case).
523  if( extended_loop() || extend_this_loop) set_extended_torsions( pose, Loop( loop_begin, loop_end ) );
524  else idealize_loop( pose, Loop( loop_begin, loop_end ) );
525 
526 
527 
528 
529 
530  /// prepare fragment movers
531  MoveMapOP movemap = new MoveMap();
532  movemap->set_bb_true_range(loop_begin, loop_end);
533 
534  std::vector< FragmentMoverOP > fragmover;
535  for ( std::vector< core::fragment::FragSetOP >::const_iterator
536  it = frag_libs_.begin(), it_end = frag_libs_.end();
537  it != it_end; it++ ) {
538  ClassicFragmentMoverOP cfm = new ClassicFragmentMover( *it, movemap );
539  cfm->set_check_ss( false );
540  cfm->enable_end_bias_check( false );
541  fragmover.push_back( cfm );
542  }
543 
544  if( !option[OptionKeys::loops::no_randomize_loop] ){
545  // insert random fragment as many times as the loop is long (not quite the exact same as the old code)
546  for ( int i = loop_begin; i <= loop_end; ++i ) {
547  for ( std::vector< FragmentMoverOP >::const_iterator
548  it = fragmover.begin(),it_end = fragmover.end(); it != it_end; it++ ) {
549  (*it)->apply( pose );
550  }
551  }
552  }
553 
554  // setup the move objects
555  simple_moves::SmallMoverOP small_mover( new simple_moves::SmallMover( movemap, 1.0, 1 ) );
556  small_mover->angle_max( 'H', 2.0 );
557  small_mover->angle_max( 'E', 2.0 );
558  small_mover->angle_max( 'L', 3.0 );
559 
560  // setup the move objects
561  simple_moves::ShearMoverOP shear_mover( new simple_moves::ShearMover( movemap, 1.0, 1 ) );
562  shear_mover->angle_max( 'H', 2.0 );
563  shear_mover->angle_max( 'E', 2.0 );
564  shear_mover->angle_max( 'L', 3.0 );
565 
566  // create a Random Mover, fill it with individual moves
567  moves::RandomMoverOP refine_mover = new moves::RandomMover();
568  refine_mover->add_mover( small_mover );
569  refine_mover->add_mover( shear_mover );
570 
571  // Set up Minimizer object
572  AtomTreeMinimizer mzr;
573  core::Real const dummy_tol( 0.001 );
574  bool const use_nblist( true ), deriv_check( false );
575  MinimizerOptions options( "linmin", dummy_tol, use_nblist, deriv_check );
576 
577 
578  // Set up MonteCarlo Object
579  core::Real const init_temp = 2.0;
580  core::Real temperature = init_temp;
581  mc_->reset( pose );
582  mc_->set_temperature( temperature );
583 
584 
585 
586  // Run abinitio if the loop is longer than 15 residues!
587 // if( (loop_end - loop_begin) > 15 ){
588 // using namespace core::fragment;
589 //
590 // protocols::abinitio::AbrelaxApplication abrelax_app;
591 // do {
592 // protocols::abinitio::ClassicAbinitio abinitio( frag_libs_[0], frag_libs_[1],movemap );
593 // abinitio.apply( pose );
594 // } while( !abrelax_app.check_filters( pose ) );
595 //
596 // pose.dump_pdb("abinitio_after.pdb");
597 // return;
598 // }
599 
600  //int counter=0;
601 
602 
603 
604  // --- Figure out constraints (ConstraintSet) ------
606 
607 
608  // --- Figure out constraints (ConstraintSet) ------
609  if( option[OptionKeys::loops::coord_cst ]() > 0.0 ){
611  for( int ir=loop_begin; ir <= loop_end; ir++ ){
612  Real middlefactor = 1.0 - (float(std::min( std::abs(ir - loop_begin), std::abs(loop_end - ir) ))/ float(std::abs(loop_end-loop_begin))) * 2 * 0.8;
613 
614  core::scoring::constraints::FuncOP CA_cst = new core::scoring::constraints::HarmonicFunc( 0, option[OptionKeys::loops::coord_cst ]() * middlefactor );
616  core::id::AtomID( pose.residue_type(ir).atom_index("CA") , ir ),
617  core::id::AtomID( pose.residue_type( 1).atom_index("CA") , 1 ),
618  start_pose.residue(ir).xyz( "CA" ),
619  CA_cst
620  );
621  new_csts->add_constraint( newcst );
622  }
623  pose.constraint_set( new_csts );
624  }
625 
626  float final_constraint_weight = option[ basic::options::OptionKeys::constraints::cst_weight ]();
627 
628  //bool has_residue_pair_constraints = pose.constraint_set()->has_residue_pair_constraints();
629  //bool has_intra_residue_constraints = pose.constraint_set()->has_intra_residue_constraints();
630  //bool has_non_residue_pair_constraints = pose.constraint_set()->has_non_residue_pair_constraints();
631  bool has_constraints = pose.constraint_set()->has_constraints();
632 
633  core::scoring::constraints::ConstraintSetCOP full_cst = pose.constraint_set(); // pose.constraint_set()
634 
635  if( has_constraints ){
636  // do we want to disable all constraints that are not part of the loop for speed
637  // reasons ? (Constraints are really quite slow!)
638 
639  // ConstraintSet
640  //filter out constraints that have at least one "anchor" in the loop
641  scorefxn_->show( TR , pose );
642  core::scoring::constraints::ConstraintSetOP loop_cst = new core::scoring::constraints::ConstraintSet( *full_cst, loop_begin, loop_end );
643  pose.constraint_set( loop_cst );
644  scorefxn_->show( TR , pose );
645  }
646 
647 
648  // OK! Let's go !
649  int starttime = time(NULL);
650  int frag_count = 0;
651  scorefxn_->show_line_headers( TR );
652 
653  if ( get_ccd_closure_exist() ) {
654  TR << "***** CCD CLOSURE *****" << std::endl;
655  core::Real const final_temp( 1.0 );
656  core::Real const gamma = std::pow( (final_temp/init_temp), (1.0/(cycles2*cycles3)) );
657 
658  for( int c2 = 1; c2 <= cycles2; ++c2 ) {
659  mc_->recover_low( pose );
660  (*scorefxn_)(pose);
661 
662  // ramp up constraints
663  if( has_constraints ) {
664  if( c2 != cycles2 ) {
665  scorefxn_->set_weight( core::scoring::atom_pair_constraint, final_constraint_weight*float(c2)/float( cycles2 ) );
666  } else {
667  scorefxn_->set_weight( core::scoring::atom_pair_constraint, final_constraint_weight * 0.2 );
668  }
669  }
670 
671  scorefxn_->show_line( TR , pose );
672  TR << std::endl;
673  mc_->score_function( *scorefxn_ );
674 
675  for( int c3 = 1; c3 <= cycles3; ++c3 ){
676  temperature *= gamma;
677  mc_->set_temperature( temperature );
678  for ( std::vector< FragmentMoverOP >::const_iterator
679  it = fragmover.begin(),it_end = fragmover.end(); it != it_end; it++ ) {
680  (*it)->apply( pose );
681  if( chainbreak_present ) fast_ccd_close_loops( pose, loop_begin, loop_end, cutpoint, mm_one_loop );
682  mzr.run( pose, mm_one_loop, *scorefxn_, options );
683  mc_->boltzmann( pose, "ccd_closure" );
684  frag_count++;
685  }
686  }
687  }
688 
689  // now ensure the loop is properly closed!
690  if( false && chainbreak_present ){
691  mc_->recover_low( pose );
692  TR << "--" << std::endl;
693  (*scorefxn_)(pose);
694  scorefxn_->show_line( TR , pose );
695  TR << std::endl;
696  fast_ccd_close_loops( pose, std::max(cutpoint-3,loop_begin), std::min(cutpoint+3,loop_end), cutpoint, mm_one_loop );
697  (*scorefxn_)(pose);
698  scorefxn_->show_line( TR , pose );
699  TR << std::endl;
700  mc_->reset( pose );
701  }
702 
703  } else {
704  TR << "***** DOING CCD MOVES *****" << std::endl;
705  float const final_chain_break_weight = 5.0;
706  float const delta_weight( final_chain_break_weight/cycles2 );
707  scorefxn_->set_weight( chainbreak, 0.0 ); //not evaluating quadratic chainbreak
708  mc_->score_function( *scorefxn_ );
709 
710  //core::Real const final_temp( 1.0 );
711  //core::Real const gamma = std::pow( (final_temp/init_temp), (1.0/(cycles2*cycles3)) );
712 
713  for ( int c2 = 1; c2 <= cycles2; ++c2 ) {
714  mc_->recover_low( pose );
715 
716  // ramp up constraints
717  if( has_constraints ) {
718  if( c2 != cycles2 ) {
719  scorefxn_->set_weight( core::scoring::atom_pair_constraint, final_constraint_weight*float(c2)/float( cycles2 ) );
720  } else {
721  scorefxn_->set_weight( core::scoring::atom_pair_constraint, final_constraint_weight * 0.2 );
722  }
723  }
724  if ( chainbreak_present ) {
725  scorefxn_->set_weight( linear_chainbreak, c2*delta_weight );
726  }
727  mc_->score_function( *scorefxn_ );
728 
729  // score and print an info line
730  (*scorefxn_)(pose);
731  scorefxn_->show_line( TR , pose );
732  TR << std::endl;
733 
734  for ( int c3 = 1; c3 <= cycles3; ++c3 ) {
735  //temperature *= gamma;
736  //mc_->set_temperature( temperature );
737  if(( !chainbreak_present || uniform()*cycles2 > c2 ))
738  {
739  //do fragment moves here
740 
741  if( !option[OptionKeys::loops::refine_only ]() ){
742  for ( std::vector< FragmentMoverOP >::const_iterator
743  it = fragmover.begin(),it_end = fragmover.end(); it != it_end; it++ ) {
744 
745 
746  if( ((*it)->fragments()->max_frag_length() == 1 ) && (uniform() < option[OptionKeys::loops::skip_1mers ]() ) ) continue;
747  if( ((*it)->fragments()->max_frag_length() == 3 ) && (uniform() < option[OptionKeys::loops::skip_3mers ]() ) ) continue;
748  if( ((*it)->fragments()->max_frag_length() == 9 ) && (uniform() < option[OptionKeys::loops::skip_9mers ]() ) ) continue;
749 
750  (*it)->apply( pose );
751  frag_count++;
752  }
753  }else{
754  refine_mover->apply( pose );
755  }
756 
757  } else {
758  //do ccd_moves here
759  if( ! option[OptionKeys::loops::skip_ccd_moves ]() ){
760  loop_closure::ccd::ccd_moves(5, pose, mm_one_loop, loop_begin, loop_end, cutpoint );
761  }
762  }
763  mc_->boltzmann( pose, "ccd_moves" );
764  } // cycles3
765  mzr.run( pose, mm_one_loop, *scorefxn_, options );
766  } //cycles2
767 
768  // now ensure the loop is properly closed!
769  if( false && chainbreak_present ){
770  mc_->recover_low( pose );
771  TR << "--" << std::endl;
772  (*scorefxn_)(pose);
773  scorefxn_->show_line( TR , pose );
774  TR << std::endl;
775  fast_ccd_close_loops( pose, std::max(cutpoint-3,loop_begin), std::min(cutpoint+3,loop_end), cutpoint, mm_one_loop );
776  (*scorefxn_)(pose);
777  scorefxn_->show_line( TR , pose );
778  TR << std::endl;
779  mc_->reset( pose );
780  }
781  } // if-else get_ccd_closure_exist
782 
783  int looptime = time(NULL) - starttime;
784  TR << "FragCount: " << frag_count << std::endl;
785  TR << "Looptime " << looptime << std::endl;
786 
787  //v pose = mc.lowest_score_pose();
788  pose = mc_->lowest_score_pose();
789  scorefxn_->show( TR , pose );
790  TR << "-------------------------" << std::endl;
791  mc_->show_counters();
792 
793  pose.constraint_set( pose_cst );
794 }
795 
796 
797 //////////////////////////////////////////////////////////////////////////////////
798 /// @details CCD close the loop [loop_begin,loop_end]. Wraps protocols::loops::fast_ccd_loop_closure,
799 /// setting reasonable weights for the protocol.
801  core::pose::Pose & pose,
802  int const & loop_begin,
803  int const & loop_end,
804  int const & cutpoint,
806  ) {
807  // param for ccd_closure
808  int const ccd_cycles = { 100 }; // num of cycles of ccd_moves
809  Real const ccd_tol = { 0.01 }; // criterion for a closed loop
810  bool const rama_check = { false };
811  Real const max_rama_score_increase = { 2.0 }; // dummy number when rama_check is false
812  Real const max_total_delta_helix = { 10.0 }; // max overall angle changes for a helical residue
813  Real const max_total_delta_strand = { 50.0 }; // ... for a residue in strand
814  Real const max_total_delta_loop = { 75.0 }; // ... for a residue in loop
815 
816  // output for ccd_closure
817  Real forward_deviation, backward_deviation; // actually loop closure msd, both dirs
818  Real torsion_delta, rama_delta; // actually torsion and rama score changes, averaged by loop_size
819 
820  //Real const bond_angle1( pose.residue( cutpoint ).upper_connect().icoor().theta() );// CA-C=N bond angle
821  //Real const bond_angle2( pose.residue( cutpoint+1 ).lower_connect().icoor().theta() ); // C=N-CA bond angle
822  //Real const bond_length( pose.residue( cutpoint+1 ).lower_connect().icoor().d() ); // C=N distance
823 
824  loop_closure::ccd::fast_ccd_loop_closure( pose, mm, loop_begin, loop_end, cutpoint, ccd_cycles,
825  ccd_tol, rama_check, max_rama_score_increase, max_total_delta_helix,
826  max_total_delta_strand, max_total_delta_loop, forward_deviation,
827  backward_deviation, torsion_delta, rama_delta );
828 
829  // fix secondary structure??
830  for (int i=loop_begin; i<=loop_end; ++i) {
831  char ss_i = pose.conformation().secstruct( i );
832  if ( ss_i != 'L' && ss_i != 'H' && ss_i != 'E')
833  pose.set_secstruct( i , 'L' );
834  }
835 }
836 
837 
838 
839 //////////////////////////////////////////////////////////////////////////////////
841  int nres,
842  int & selected_loop,
843  std::vector< int > & folded_loops,
844  std::vector< int > & inter_res,
845  int & loop_begin,
846  int & loop_end,
847  int & cutpoint,
848  bool & extend_this_loop,
849  bool & are_loops_combined,
850  int & combine_interval,
851  int & loop_counter
852  ) {
853  int const num_loops = (int)(Loops_in_.size() );
854 
855  // make how many loop combination(s) per structure on average
856  core::Real const loop_combine_rate( get_loop_combine_rate() );
857  core::Real const num_of_loops_to_combine( numeric::random::uniform()*(num_loops - 1)*loop_combine_rate );
858 
859  TR << "LoopsToCombine: " << num_of_loops_to_combine << " " << num_loops << std::endl;
860 
861  selected_loop = 0;
862  do{
863  // do loops in order if random_loop is not set !
864  if ( !get_random_loop_flag() ){
865  selected_loop += 1;
866  }else{
867  selected_loop = (int)( numeric::random::uniform()*num_loops + 1);
868  }
869  }while( std::find( folded_loops.begin(), folded_loops.end(), selected_loop ) !=
870  folded_loops.end() );
871  folded_loops.push_back(selected_loop);
872 
873  loop_begin = Loops_in_[selected_loop].start();
874  loop_end = Loops_in_[selected_loop].stop();
875  cutpoint = Loops_in_[selected_loop].cut();
876  extend_this_loop |= Loops_in_[selected_loop].is_extended();
877 
878  if (loop_begin <= 0) loop_begin = 1;
879  if (loop_end >= nres) loop_end = nres;
880 
881  if ( !get_random_loop_flag() ) return true;
882 
883  // LOOPSETS APPLIED OUTSIDE THE PROTOCOL
884  //if ( numeric::random::uniform() < get_loop_skip_rate() )
885  // return false;
886 
887  combine_interval = 1;
888  // combine consecutive loop regions
889  if ( selected_loop < num_loops - combine_interval && num_of_loops_to_combine > 0 ) {
890  int const longer_loop_size ( Loops_in_[ selected_loop + combine_interval ].stop() -
891  Loops_in_[ selected_loop ].start());
892  int loop_limit;
893  int const terminal_loop(12);
894  int const internal_loop(25);
895  if( (int)Loops_in_[ selected_loop + combine_interval ].stop() >= nres ||
896  (int)Loops_in_[ selected_loop ].start() <= 1 )
897  loop_limit = terminal_loop;
898  else loop_limit = internal_loop;
899  // don't merge internal loops longer than 25,
900  // or terminal loops longer than 12
901 
902 
903 
904  if ( numeric::random::uniform() < num_of_loops_to_combine/(num_loops-1+1e-10 ) ) {
905 
906  loop_begin = Loops_in_[selected_loop].start();
907  loop_end = Loops_in_[selected_loop].stop() + combine_interval;
908  TR.Info << "Combining loops: " << loop_begin << " " << loop_end << std::endl;
909  // if loop exceeds maximum length - truncate it randomly on either side
910  if( longer_loop_size > loop_limit ){
911  if( numeric::random::uniform() < 0.5 ){
912  loop_begin = loop_end - (loop_limit - 1);
913  }else{
914  loop_end = loop_begin + (loop_limit - 1);
915  }
916  }
917 
918  for ( int ll = 0; ll < combine_interval; ++ll ){
919  for ( int k = (int)Loops_in_[ selected_loop +ll ].stop();
920  k <= (int)Loops_in_[ selected_loop +ll + 1 ].start(); ++k )
921  inter_res.push_back(k);
922  if( !loop_model() ) {
923  folded_loops.push_back(selected_loop+ll+1);
924  loop_counter++;
925  }
926  }
927  are_loops_combined = true;
928  }
929  }
930 
931 
932  // choose random taking off points
933  //int const old_loop_begin( loop_begin );
934  //int const old_loop_end( loop_end );
935  int rand_extension = 0; //fpd 1->0
936  int rand_limit = 4;
937  if ( loop_model() || extended_loop() || extend_this_loop ){
938  rand_extension = 0;
939  rand_limit = 3;
940  }
941 
942  do {
943  if ( loop_begin > 1 )
944  loop_begin = loop_begin - static_cast <int> ( numeric::random::uniform() * rand_limit )
945  + rand_extension;
946  if ( loop_end != nres )
947  loop_end = loop_end + static_cast <int> ( numeric::random::uniform() * rand_limit )
948  - rand_extension;
949  // TR << "Fraying" << loop_begin << " " << loop_end << std::endl;
950  } while ( numeric::random::uniform() < 0.3 );
951 
952  if ( loop_end - loop_begin < 0 ){
953  loop_end++;
954  loop_begin--;
955  }
956  loop_begin = std::max( 1, loop_begin );
957  loop_end = std::min( nres, loop_end );
958 
959  return true;
960 }
961 
962 
963 
964 //////////////////////////////////////////////////////////////////////////////////
966  using namespace basic::options;
967  using namespace basic::options::OptionKeys;
968 
969  static core::Real score_filter_cutoff = { 1.0 };
970  static bool init = { false };
971 
972  if ( !init ) {
973  score_filter_cutoff = option[OptionKeys::loops::score_filter_cutoff ]();
974  init = true;
975  }
976  return score_filter_cutoff;
977 }
978 
979 //////////////////////////////////////////////////////////////////////////////////
981 {
982  using namespace basic::options;
983  using namespace basic::options::OptionKeys;
984 
985  static bool loop_model = { false };
986  static bool init = { false };
987 
988  if ( !init ) {
989  loop_model = option[ OptionKeys::loops::loop_model ];
990  init = true;
991  }
992  return loop_model;
993 }
994 
995 
996 
997 //////////////////////////////////////////////////////////////////////////////////
999  using namespace basic::options;
1000  using namespace basic::options::OptionKeys;
1001 
1002  static bool ccd_closure_exist = { false };
1003  static bool init = { false };
1004 
1005  if ( !init ) {
1006  ccd_closure_exist = option[ OptionKeys::loops::ccd_closure];
1007  init = true;
1008  }
1009  return ccd_closure_exist;
1010 }
1011 
1012 
1013 //////////////////////////////////////////////////////////////////////////////////
1015  using namespace basic::options;
1016  using namespace basic::options::OptionKeys;
1017 
1018  static bool desired_loops_exist = { false };
1019  static bool init = { false };
1020 
1021  if ( !init ) {
1022  desired_loops_exist = option[ OptionKeys::loops::loops_subset];
1023  init = true;
1024  }
1025  return desired_loops_exist;
1026 }
1027 
1028 
1029 //////////////////////////////////////////////////////////////////////////////////
1031  using namespace basic::options;
1032  using namespace basic::options::OptionKeys;
1033 
1034  static bool init = false;
1035  static int desired_loop = 1;
1036 
1037  if ( !init ) {
1038  desired_loop = option[ OptionKeys::loops::num_desired_loops]();
1039  init = true;
1040  }
1041  return desired_loop;
1042 }
1043 
1044 
1045 //////////////////////////////////////////////////////////////////////////////////
1047  using namespace basic::options;
1048  using namespace basic::options::OptionKeys;
1049 
1050  static bool init = { false };
1051  static core::Real loop_combine_rate = 0.0;
1052 
1053  if ( !init ) {
1054  loop_combine_rate = option[ OptionKeys::loops::loop_combine_rate ]();
1055  init = true;
1056  }
1057  return loop_combine_rate;
1058 }
1059 
1060 
1061 //////////////////////////////////////////////////////////////////////////////////
1063  using namespace basic::options;
1064  using namespace basic::options::OptionKeys;
1065 
1066  static bool combine_loop = { true };
1067  static bool init = { false };
1068 
1069  if ( !init ) {
1070  combine_loop = option[ OptionKeys::loops::no_combine_if_fail];
1071  init = true;
1072  }
1073  return combine_loop;
1074 }
1075 
1076 
1077 ///////////////////////////////////////////////////////////////////////////////
1079  core::pose::Pose & pose,
1080  int & barcst_extend_begin,
1081  int & barcst_extend_end,
1082  int & loop_begin,
1083  int & loop_end,
1084  int const & old_loop_begin,
1085  int const & old_loop_end,
1086  int const & nres,
1087  int const & selected_loop,
1088  int const & total_combine,
1089  int const & backward_combine
1090 ) {
1091  int const num_loop( static_cast<int>( Loops_in_.size() ) );
1092  // further extend loop regions, and add barcode constraints on these
1093  // extended regions
1094  int barcst_begin ( old_loop_begin );
1095  int barcst_end ( old_loop_end );
1096 
1097  barcst_extend_begin = std::min( 5, barcst_extend_begin );// extend 5 res at most
1098  barcst_extend_end = std::min( 5, barcst_extend_end );// extend 5 res at most
1099  // loop_update_active_cst_list ( barcst_begin, barcst_end, free_res,
1100  // barcst_extend_begin, barcst_extend_end );
1101  barcst_begin -= barcst_extend_begin;
1102  barcst_end += barcst_extend_end;
1103  while ( barcst_begin < 1 ) barcst_begin++;
1104  while ( barcst_end > nres ) barcst_end--;
1105 
1106  // make sure not to extend the stems to loop regions
1107  if ( selected_loop - backward_combine > 1 )
1108  while ( barcst_begin <= (int) Loops_in_[ selected_loop - backward_combine -1 ].stop() + 1 )
1109  barcst_begin++;
1110  if ( selected_loop + total_combine != num_loop )
1111  while ( barcst_end >= (int) Loops_in_[ selected_loop + total_combine +1 ].start() - 1 )
1112  barcst_end--;
1113 
1114  // shorten loops when do looprlx ( loop_modeling)
1115  if( !loop_model() && shorten_long_terminal_loop() ){
1116  if( barcst_end == nres && barcst_end - barcst_begin > 10 )
1117  barcst_begin = barcst_end - 10;
1118  if( barcst_begin == 1 && barcst_end - barcst_begin > 10 )
1119  barcst_end = barcst_begin + 10;
1120  }
1121 
1122  // make sure loop length is greater than 3
1123  if ( barcst_end - barcst_begin < 2 && barcst_begin != 1 && barcst_end != nres ){
1124  barcst_begin -= 1;
1125  barcst_end += 1;
1126  }
1127  //vats hack REMOVE THIS
1128  loop_begin = barcst_begin;
1129  loop_end = barcst_end;
1130  // loop_begin = loops_begin.at( 0 );
1131  // loop_end = loops_end.at( 0 );
1132 
1133  // show the loop sequence for sanity-checking
1134  TR.Debug << "barcode_extend_stems(): loop_begin = " << loop_begin <<
1135  " loop_end =" << loop_end <<
1136  " loop_sequence =";
1137  for ( int i=loop_begin; i<= loop_end; ++i )
1138  TR.Debug << " " << pose.residue( i ).name3();
1139  TR.Debug << std::endl;
1140 }
1141 
1142 
1143 //////////////////////////////////////////////////////////////////////////////////
1145  using namespace basic::options;
1146  using namespace basic::options::OptionKeys;
1147 
1148  static bool init = { false };
1149  static bool short_loop = { false };
1150 
1151  if ( !init ) {
1152  short_loop = option[ OptionKeys::loops::shorten_long_terminal_loop];
1153  init = true;
1154  }
1155  return short_loop;
1156 }
1157 
1158 
1159 
1160 //////////////////////////////////////////////////////////////////////////////////
1162  core::pose::Pose & pose,
1163  int const & loop_begin,
1164  int const & loop_end,
1165  int & n_chain_break_fail,
1166  bool & is_chain_break,
1167  int & barcst_extend_begin, // output
1168  int & barcst_extend_end
1169 ) {
1170  static int n_small_chain_break_fail = { 1 };
1171  static bool barcst_flip_flop = { true };
1172  static bool barcst_small_flip_flop = { true };
1173  is_chain_break = false;
1174 
1175  // params control the behaviors of all loop modeling processes
1176  core::Real const chain_break_tol( get_chain_break_tolerance() );
1177  core::Real chain_break_score=0;
1178 
1179  int const nres = pose.total_residue();
1180 
1181  (*scorefxn_)( pose );
1182  if ( loop_begin != 1 && loop_end != nres ){
1183  chain_break_score = std::max( (float)pose.energies().total_energies()[ scoring::chainbreak ],
1184  (float)pose.energies().total_energies()[ scoring::linear_chainbreak ] );
1185  // chain_break_score = pose.get_0D_score( pose_ns::CHAINBREAK );
1186  }
1187 
1188  TR.Debug << "loop_begin and loop_end " << loop_begin << " " << loop_end << " " << chain_break_score << std::endl;
1189  scorefxn_->show( TR , pose );
1190  TR.Debug << "*****\n*****\n*****\n";
1191  TR.Debug << pose.fold_tree() << std::endl;;
1192  TR.Debug << "*****\n*****\n*****\n";
1193 
1194  // extend more barcode regionsi
1195  TR.Info << "Chainbreak: " << chain_break_score << " Max: " << chain_break_tol << std::endl;
1196  if ( chain_break_score > chain_break_tol ){
1197  is_chain_break = true;
1198  n_chain_break_fail++;
1199 
1200  if( chain_break_score <= 10* chain_break_tol )
1201  n_small_chain_break_fail++;
1202 
1203  if ( get_random_loop_flag() ){
1204  if ( chain_break_score > 10* chain_break_tol ) {
1205  if( barcst_flip_flop ){
1206  // if (loop_begin != 1 && loop_end != nres &&
1207  // pose.secstruct( loop_begin - 1) != 'H' &&
1208  // pose.secstruct( loop_begin - 1) != 'E' )
1209  barcst_extend_begin++;
1210  barcst_flip_flop = false;
1211  } else {
1212  barcst_extend_end++;
1213  barcst_flip_flop = true;
1214  }
1215  }
1216  else if ( n_small_chain_break_fail % 3 == 0 ) {
1217  if( barcst_small_flip_flop ){
1218  barcst_extend_begin++;
1219  barcst_small_flip_flop = false;
1220  } else {
1221  barcst_extend_end++;
1222  barcst_small_flip_flop = true;
1223  }
1224  }
1225  }
1226  }
1227 }
1228 
1229 
1230 //////////////////////////////////////////////////////////////////////////////////
1232  core::pose::Pose & pose1,
1233  core::pose::Pose & pose2
1234  ) {
1235  runtime_assert( pose1.total_residue() == pose2.total_residue() );
1236  int natoms( pose1.total_residue() );
1237  using ObjexxFCL::FArray2D;
1238  FArray2D< core::Real > p1a( 3, natoms );
1239  FArray2D< core::Real > p2a( 3, natoms );
1240  //I am not sure, but I think '3' is CA. Check with someone if this is correct
1241  int CA_pos( 3 );
1242  core::Real rmsd_tol ( get_rmsd_tolerance() );
1243 
1244  int atom_count( 0 );
1245  for( int i = 1; i <= int( pose1.total_residue() ); ++i ) {
1246  const numeric::xyzVector< Real > & vec1( pose1.residue( i ).xyz( CA_pos ) );
1247  const numeric::xyzVector< Real > & vec2( pose2.residue( i ).xyz( CA_pos ) );
1248  atom_count++;
1249  for( int k = 0; k < 3; ++k ){
1250  p1a( k+1, atom_count ) = vec1[ k ];
1251  p2a( k+1, atom_count ) = vec2[ k ];
1252  }
1253  }
1254  core::Real rmsd( numeric::model_quality::rms_wrapper( natoms, p1a, p2a ) );
1255  TR.Debug << "RMSd change " << rmsd << std::endl;
1256  if ( rmsd <= rmsd_tol ) return true;
1257 
1258  return false;
1259 }
1260 
1261 
1262 ///////////////////////////////////////////////////////////////////////
1264  using namespace basic::options;
1265  using namespace basic::options::OptionKeys;
1266 
1267  static bool init = { false };
1268  static core::Real cycle_ratio = 1.0;
1269 
1270  if ( !init ) {
1271  cycle_ratio = option[ OptionKeys::loops::looprlx_cycle_ratio ]();
1272  init = true;
1273  }
1274  return cycle_ratio;
1275 }
1276 
1277 
1278 //////////////////////////////////////////////////////////////////////////////////
1280  if ( use_default_extend_loops ) {
1281  // from now on, use stored value
1282  extend_loops = basic::options::option[ basic::options::OptionKeys::loops::extended ];
1283  use_default_extend_loops = false;
1284  }
1285 
1286  return extend_loops;
1287 }
1288 
1289 
1290 //////////////////////////////////////////////////////////////////////////////////
1292  extend_loops = val;
1293  use_default_extend_loops = false;
1294 }
1295 
1296 
1297 
1298 
1299 
1300 
1301 
1302 
1303 //////////////////////////////////////////////////////////
1304 ///@brief Helper function tokenizes a str
1305 /////////////////////////////////////////////////////////
1306 // don't use use string_util.hh:split or string_split instead
1307 //
1310  const std::string &delimiters=" ") {
1311  tokens.clear();
1312  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
1313  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
1314 
1315  while (std::string::npos != pos || std::string::npos != lastPos) {
1316  tokens.push_back(str.substr(lastPos, pos - lastPos));
1317  lastPos = str.find_first_not_of(delimiters, pos);
1318  pos = str.find_first_of(delimiters, lastPos);
1319  }
1320 }
1321 
1322 /*
1323 //////////////////////////////////////////////////////////////////////////////////
1324 void LoopRebuild::read_loop_file(
1325  utility::vector1< LRLoop > &loops,
1326  std::string filename
1327 ) {
1328  loops.clear();
1329  std::ifstream infile( filename.c_str() );
1330 
1331  if (!infile.good()) {
1332  utility_exit_with_message( "[ERROR] Error opening RBSeg file '" + filename + "'" );
1333  }
1334  std::string line;
1335  utility::vector1< std::string > tokens;
1336 
1337  while( getline(infile,line) ) {
1338  Tokenize( line, tokens ) ;
1339 
1340  if( tokens.size() > 0 ) {
1341  if ( tokens[1] == "LOOP" ) {
1342  if ( tokens.size() < 3 ) {
1343  utility_exit_with_message( "[ERROR] Error parsing line '" + line + "'" );
1344  }
1345  core::Size start_res = (core::Size) atoi(tokens[2].c_str());
1346  core::Size end_res = (core::Size) atoi(tokens[3].c_str());
1347  core::Size cutpt = 0; // default - let LoopRebuild choose cutpoint
1348  core::Real skip_rate = 0.0; // default - never skip
1349  std::string extend_loop_str;
1350  bool extend_loop = false;
1351 
1352  if (tokens.size() > 3)
1353  cutpt = (core::Size) atoi(tokens[4].c_str());
1354  if (tokens.size() > 4)
1355  skip_rate = atof(tokens[5].c_str());
1356  if (tokens.size() > 5)
1357  extend_loop_str = tokens[6];
1358 
1359  if (extend_loop_str.length() > 0)
1360  extend_loop = true;
1361 
1362  if( start_res >= end_res ){
1363  utility_exit_with_message( "[ERROR] Line '" + string_of(line) + "' has invalid loop definition (start residue >= end residue) - ERROR" );
1364  }else{
1365  loops.push_back( LRLoop(start_res, end_res, cutpt, skip_rate, extend_loop) );
1366  }
1367  } else if ( tokens[1][0] != '#' ) {
1368  if (tokens.size() >= 2) {
1369  TR << "[WARNING] Reading r++ style loopfile" << std::endl;
1370  core::Size start_res = (core::Size) atoi(tokens[1].c_str());
1371  core::Size end_res = (core::Size) atoi(tokens[2].c_str());
1372  core::Size cutpt = 0; // default - let LRLoopRebuild choose cutpoint
1373  core::Real skip_rate = 0.0; // default - never skip
1374  bool extend_loop = false;
1375 
1376  if (tokens.size() > 2)
1377  cutpt = (core::Size) atoi(tokens[3].c_str());
1378  if (tokens.size() > 3)
1379  skip_rate = atof(tokens[4].c_str());
1380 
1381  if( start_res >= end_res ){
1382  utility_exit_with_message( "[ERROR] Line '" + string_of(line) + "' has invalid loop definition (start residue >= end residue) - ERROR" );
1383  }else{
1384  loops.push_back( LRLoop(start_res, end_res, cutpt, skip_rate, extend_loop) );
1385  }
1386  } else {
1387  TR << "[WARNING] Skipping line '" << line << "'" << std::endl;
1388  }
1389  }
1390  }
1391  }
1392 
1393 
1394  // sort by start residue
1395  std::sort( loops.begin(), loops.end(), Loop_lt() );
1396 
1397  // debug ... print in the data structure
1398  for (core::Size i=1; i<=loops.size(); ++i) {
1399  TR << "[debug] LOOP " << loops[i].start() << " " << loops[i].stop()
1400  << " " << loops[i].cut() << " " << loops[i].skip_rate() << std::endl;
1401  }
1402 }
1403 
1404 */
1405 
1406 
1407 
1408 /// ///////////////////////////////////////////////////////////////////////
1409 /// LRLoop combine_loops( const LRLoop &loop1 , const LRLoop &loop2 ){
1410 /// return LRLoop(
1411 /// std::min( loop1.start() , loop2.start() ),
1412 /// std::max( loop1.start() , loop2.start() ),
1413 /// (loop1.cut() + loop2.cut()) / 2,
1414 /// 1.0 - (1.0-loop1.skip_rate()) * (1.0-loop2.skip_rate()) ,
1415 /// loop1.is_extended() || loop2.is_extended()
1416 /// );
1417 /// }
1418 
1419 
1420 ///////////////////////////////////////////////////////////////////////
1421 /// @brief returns mc object
1423 {
1424 
1425  set_default_mc( pose );
1426  mc_created = true;
1427  return mc_;
1428 
1429 }
1430 
1432  use_default_extend_loops = true;
1433  mc_created = false;
1434  random_loop_flag_ = basic::options::option[ basic::options::OptionKeys::loops::random_loop ];
1437  abort_on_failed_loop_ = true;
1438 }
1439 
1440 
1441 ///////////////////////////////////////////////////////////////////////
1442 ///@brief sets mc object
1444 {
1445  m_Temperature_ = 2.0;
1446  mc_ = new moves::MonteCarlo( pose, *scorefxn_, m_Temperature_ );
1447 }
1448 ///////////////////////////////////////////////////////////////////////
1449 ///@brief Full atom loop refinement
1451  core::pose::Pose & pose
1452 )
1453 {
1454 
1455  TR << "***** Starting full-atom loop refinement protocol ****" << std::endl;
1456 
1457  protocols::loops::LoopsOP LoopsToRefine = new protocols::loops::Loops();
1458 
1459  for( Loops::const_iterator it=Loops_in_.begin(), it_end=Loops_in_.end(); it != it_end; ++it ) {
1460  Loop refine_loop( *it );
1461  refine_loop.choose_cutpoint( pose );
1462  LoopsToRefine->add_loop( refine_loop );
1463  }
1464 
1466  protocols::loops::fold_tree_from_loops( pose, *LoopsToRefine, f );
1467  pose.fold_tree( f );
1468 
1469  //protocols::loops::refine_loops_with_ccd( pose, pose, LoopsToRefine );
1470  loop_mover::refine::LoopMover_Refine_CCD refine_ccd( LoopsToRefine );
1471  refine_ccd.set_native_pose( new pose::Pose ( pose ) );
1472  refine_ccd.apply( pose );
1473 
1474 }
1475 
1478  return "LoopRefine";
1479 }
1480 
1481 
1482 ///////////////////////////////////////////////////////////////////////
1483 
1484 } // namespace protocols