Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MembraneAbinitio.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 MembraneAbinitio.cc
11 /// @brief ab-initio fragment assembly protocol for membrane proteins
12 /// @detailed
13 /// Contains currently: Membrane Abinitio
14 ///
15 ///
16 /// @author Bjorn Wallner (copied some time ago from ClassicAbinitio.cc of Oliver Lange )
17 
18 
19 // Unit Headers
21 // AUTO-REMOVED #include <protocols/simple_moves/SymmetricFragmentMover.hh>
23 
24 // Package Headers
25 
26 // Project Headers
27 #include <core/pose/Pose.hh>
30 #include <core/types.hh>
37 
38 // AUTO-REMOVED #include <core/scoring/rms_util.hh>
39 // AUTO-REMOVED #include <core/pack/task/PackerTask.fwd.hh>
42 #include <basic/datacache/BasicDataCache.hh>
43 
44 // Option headers
45 #include <basic/options/keys/abinitio.OptionKeys.gen.hh>
46 #include <basic/options/keys/out.OptionKeys.gen.hh>
47 // AUTO-REMOVED #include <basic/options/keys/membrane.OptionKeys.gen.hh>
48 
49 #include <protocols/moves/Mover.hh>
54 
55 #ifdef GL_GRAPHICS
57 #endif
58 
59 // AUTO-REMOVED #include <protocols/checkpoint/Checkpoint.hh>
60 // AUTO-REMOVED #include <protocols/jumping/MembraneJump.hh>
61 
62 // ObjexxFCL Headers
63 #include <ObjexxFCL/string.functions.hh>
64 #include <ObjexxFCL/FArray1D.hh>
65 #include <ObjexxFCL/FArray2D.hh>
66 
67 // Utility headers
68 #include <utility/vector1.fwd.hh>
69 #include <utility/pointer/ReferenceCount.hh>
70 // AUTO-REMOVED #include <utility/file/file_sys_util.hh>
71 // AUTO-REMOVED #include <utility/io/izstream.hh>
72 // AUTO-REMOVED #include <utility/io/ozstream.hh>
73 #include <numeric/numeric.functions.hh>
74 // AUTO-REMOVED #include <numeric/random/random.hh>
75 #include <basic/prof.hh>
76 #include <basic/Tracer.hh>
77 #include <basic/options/option.hh>
78 #include <basic/options/keys/OptionKeys.hh>
79 #include <basic/options/option_macros.hh>
80 
81 //// C++ headers
82 #include <cstdlib>
83 #include <string>
84 #ifdef WIN32
85 #include <ctime>
86 #endif
87 
88 //Auto Headers
89 #include <core/fragment/FragSet.hh>
91 #include <utility/vector0.hh>
92 #include <utility/vector1.hh>
93 #include <numeric/random/random.fwd.hh>
94 
95 //Auto using namespaces
96 namespace ObjexxFCL { } using namespace ObjexxFCL; // AUTO USING NS
97 namespace ObjexxFCL { namespace fmt { } } using namespace ObjexxFCL::fmt; // AUTO USING NS
98 //Auto using namespaces end
99 
100 
101 
102 
103 static basic::Tracer tr("protocols.membrane.abinitio",basic::t_info);
104 
105 
106 using core::Real;
107 using namespace core;
108 using namespace basic;
109 using namespace basic::options;
110 using namespace basic::options::OptionKeys;
111 
112 /*!
113 @detail call this:
114 MembraneAbinitio::register_options() before devel::init().
115  Derived classes that overload this function should also call Parent::register_options()
116 */
117 
118 OPT_1GRP_KEY( Boolean, abinitio, debug_structures_2 )
119 //OPT_1GRP_KEY( Boolean, abinitio, membrane_print )
120 OPT_1GRP_KEY( Boolean, abinitio, test_2 )
121 OPT_1GRP_KEY( File, abinitio, log_frags_2 )
122 OPT_1GRP_KEY( Boolean, abinitio, only_stage1_2 )
123 OPT_1GRP_KEY( Real, abinitio, end_bias_2 )
124 OPT_1GRP_KEY( Integer, templates, change_movemap_2 )
125 OPT_1GRP_KEY( Integer, abinitio, symmetry_residue_2 )
126 
127 void protocols::abinitio::MembraneAbinitio::register_options() {
128  using namespace basic::options;
129  using namespace basic::options::OptionKeys;
130  option.add_relevant( OptionKeys::abinitio::increase_cycles );
131  option.add_relevant( OptionKeys::abinitio::smooth_cycles_only );
132  option.add_relevant( OptionKeys::abinitio::debug );
133  option.add_relevant( OptionKeys::abinitio::skip_convergence_check );
134  NEW_OPT( abinitio::debug_structures_2, "write structures to debug-out files", false );
135  //NEW_OPT( abinitio::membrane_print, "be noisy and very informative...", false );
136  NEW_OPT( abinitio::test_2, "if set the protocol will run quickly to test your setup (files, cmd-line, etc. )", false );
137  NEW_OPT( abinitio::log_frags_2, "fragment insertions (each trial) will be logged to file", "" );
138  NEW_OPT( abinitio::only_stage1_2, "useful for benchmarks sets cycle of all higher stages to 0", false );
139  NEW_OPT( abinitio::end_bias_2, "set the endbias for Fragment moves", 30.0 );
140  NEW_OPT( templates::change_movemap_2, "stage in which movemap is switched to allow all bb-residues to move, valid stages: 3..4 (HACK)", 3);
141  NEW_OPT( abinitio::symmetry_residue_2,"hacky symmetry mode for dimers, fragments are inserted at i and i + SR - 1", -1 );
142 }
143 
144 
145 namespace protocols {
146 namespace abinitio {
147 
148 /// @detail large (stage1/stage2)
149 /// small(stage2/stage3/stage4)
150 /// smooth_small ( stage3/stage4)
151 MembraneAbinitio::MembraneAbinitio(
152  simple_moves::FragmentMoverOP brute_move_small,
153  simple_moves::FragmentMoverOP brute_move_small_top25,
154  simple_moves::FragmentMoverOP brute_move_large,
155  simple_moves::FragmentMoverOP smooth_move_small,
156  int /*dummy otherwise the two constructors are ambiguous */
157 ) :
158  brute_move_small_( brute_move_small ),
159  brute_move_small_top25_( brute_move_small_top25 ),
160  brute_move_large_( brute_move_large ),
161  smooth_move_small_( smooth_move_small ),
162  checkpoint_( "MembraneAbinitio" )
163  // output_tag_( "debug ")
164 {
165  BaseClass::type( "MembraneAbintio" );
166 
167  // std::cerr << "MembraneAbinitio::constructor has stubbed out...(fatal) see code file";
168  // assert( 0 ); //---> needs more implementation to use this constructor: e.g. read out movemap from FragmentMover...
169  movemap_ = brute_move_large->movemap();
170  // set_defaults( pose ); in constructor virtual functions are not called
171 }
172 
174  core::fragment::FragSetCOP fragset_small,
175  core::fragment::FragSetCOP fragset_small_top25,
176  core::fragment::FragSetCOP fragset_large,
178 ) :
179  movemap_( movemap ),
180  checkpoint_( "MembraneAbinitio" )
181 {
182  BaseClass::type( "MembraneAbinitio" );
183  using namespace basic::options;
184  simple_moves::ClassicFragmentMoverOP bms, bms25, bml, sms;
185 /* if ( option[ OptionKeys::abinitio::log_frags_2 ].user() ) {
186  if ( !option[ OptionKeys::abinitio::debug ] ) utility_exit_with_message( "apply option abinitio::log_frags always together with abinitio::debug!!!");
187  bms = new LoggedFragmentMover( fragset_small, movemap );
188  bml = new LoggedFragmentMover( fragset_large, movemap );
189  sms = new SmoothFragmentMover( fragset_small, movemap, new GunnCost );
190  } else if ( option[ OptionKeys::abinitio::symmetry_residue_2 ].user() ) {
191  Size const sr ( option[ OptionKeys::abinitio::symmetry_residue_2 ] );
192  bms = new SymmetricFragmentMover( fragset_small, movemap, sr );
193  bml = new SymmetricFragmentMover( fragset_large, movemap, sr );
194  sms = new SmoothSymmetricFragmentMover( fragset_small, movemap, new GunnCost, sr );
195  } else { */
196 
197  using namespace protocols::simple_moves;
198  bms25 = new ClassicFragmentMover( fragset_small_top25, movemap );
199  bms = new ClassicFragmentMover( fragset_small, movemap );
200  bml = new ClassicFragmentMover( fragset_large, movemap );
201  sms = new SmoothFragmentMover ( fragset_small, movemap, new GunnCost );
202 // }
203 /*
204  bms->set_end_bias( option[ OptionKeys::abinitio::end_bias_2 ] ); //default is 30.0
205  bml->set_end_bias( option[ OptionKeys::abinitio::end_bias_2 ] );
206  bms->set_end_bias( option[ OptionKeys::abinitio::end_bias_2 ] );
207 */
208 
209  brute_move_small_ = bms;
210  brute_move_small_top25_ = bms25;
211  brute_move_large_ = bml;
212  smooth_move_small_ = sms;
213 }
214 
215 /// @brief setup moves, mc-object, scores
216 /// @details can't call this from constructor; virtual functions don't operate until construction has completed.
217 
218 void
220  // Parent::init( pose );
221  set_defaults( pose );
222  // bInitialized_ = true;
223 }
224 
225 /// @brief MembraneAbinitio has virtual functions... use this to obtain a new instance
226 //MembraneAbinitioOP
229 {
230  return new MembraneAbinitio( *this );
231 }
232 
234  using namespace moves;
235  using namespace scoring;
236  //bool success =
237  Parent::apply( pose );
238  total_trials_ = 0;
239 
240 #ifdef GL_GRAPHICS
241  protocols::viewer::add_conformation_viewer( pose.conformation(), "start_pose" ); //add viewer
242 #endif
243 
244  //std::exit(1);
245  if ( !only_stage4_ ) {
246  if (!recover_checkpoint( pose, "stage_1")) {
247 
248  PROF_START( basic::STAGE1 );
249  clock_t starttime = clock();
250  // part 1 ----------------------------------------
251  tr.Info << "\n===================================================================\n";
252  tr.Info << " Stage 1 \n";
253  tr.Info << " Folding with score0 for max of " << stage1_cycles() << std::endl;
254 
255  prepare_stage1( pose );
256 
258  prof_show();
259  output_debug_structure( pose, "stage0" );
260  }
261  //std::cout << "MOVABLE JUMP " << movemap_->get_jump(1);
262  //std::cout << "\n";
263  /*
264  for(Size i=1;i<=pose.total_residue();++i) {
265  pose.set_phi(i,-60);
266  pose.set_psi(i,-40);
267  if(movemap_->get_bb(i)){
268  std::cout << 1;
269  } else {
270  std::cout << 0;
271  }
272  }
273  std::cout <<"\n";
274  */
275  //pose.dump_pdb("score0pre.pdb");
276  //std::exit(1);
277  do_stage1_cycles( pose );
278  //pose.dump_pdb("score0.pdb");
279  //std::exit(1);
280  // mc().recover_low( pose ); seems to be a bad choice after score0
281 
282  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose );
283  mc().show_counters();
285  mc().reset_counters();
286 
287  clock_t endtime = clock();
288  PROF_STOP( basic::STAGE1 );
289 
291  tr << "Timeperstep: " << (double(endtime) - starttime )/(CLOCKS_PER_SEC ) << std::endl;
292  prof_show();
293  output_debug_structure( pose, "stage1" );
294  }
295 
296  checkpoint( pose, "stage_1");
297  }
298 
299 // if ( option[ OptionKeys::abinitio::only_stage1_2 ] ) return success;
300 
301 
302 
303  //
304  //
305 
306 
307  // part 2 ----------------------------------------
308 
309 
312  Size total_tmhelix(topology.tmhelix());
313  Size tmh_inserted(0);
314 
315 
316  //ADD
317  add_spanning_region(pose);
318  while(tmh_inserted<total_tmhelix)
319  {
320  tr.Info << "\n===================================================================\n";
321  tr.Info << " Stage 2 \n";
322  tr.Info << " Starting sequencial insertion of " << total_tmhelix << ", membrane helices " << tmh_inserted << " inserted so far\n";
323  tr.Info << " Folding with score_membrane for " << stage2_cycles() << std::endl;
324  print_debug(pose);
325  {
326  PROF_START( basic::STAGE2 );
327  clock_t starttime = clock();
328 
329 
330 
331  prepare_stage2( pose );
332 
333  do_stage2_cycles( pose );
334  mc().recover_low( pose );
335 
336  if ( tr.visible() ) current_scorefxn().show( tr, pose );
337  mc().show_counters();
339  mc().reset_counters();
340 
341  clock_t endtime = clock();
342  PROF_STOP( basic::STAGE2 );
343 
345  // pose.dump_pdb("stage2.pdb");
346  output_debug_structure( pose, "stage2" );
347  tr << "Timeperstep: " << (double(endtime) - starttime )/(CLOCKS_PER_SEC ) << std::endl;
348  prof_show();
349  }
350 
351  // checkpoint( pose, "stage_2" );
352  }
353 
354  {
355  //BW ADD ALLOW FRAGMENT INSERTION IN ALL INSERTED REGIONS
356  move_all_inserted(pose);
357 
358  //
359  // moved checkpointing into do_stage3_cycles because of structure store
360  // part 3 ----------------------------------------
361  tr.Info << "\n===================================================================\n";
362  tr.Info << " Stage 3 \n";
363  tr.Info << " Folding all inserted regions with score_membrane for " << stage3_cycles() <<std::endl;
364 // tr.Info << std::endl << movemap_;
365  print_debug(pose);
366  PROF_START( basic::STAGE3 );
367  clock_t starttime = clock();
368 
369  prepare_stage3( pose );
370  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose );
371 
372  do_stage3_cycles( pose );
373  mc().recover_low( pose );
374 
375  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose );
376  mc().show_counters();
378  mc().reset_counters();
379 
380  clock_t endtime = clock();
381  PROF_STOP( basic::STAGE3);
382 
384  output_debug_structure( pose, "stage3" );
385  tr << "Timeperstep: " << (double(endtime) - starttime )/( CLOCKS_PER_SEC) << std::endl;
386  prof_show();
387  }
388 
389  //add the extra score25 (stage3b)
390  {
391  // moved checkpointing into do_stage3_cycles because of structure store
392  // part 3 ----------------------------------------
393  tr.Info << "\n===================================================================\n";
394  tr.Info << " Stage 3b \n";
395  tr.Info << " Folding all inserted regions with score_membrane for " << stage3_cycles() <<std::endl;
396  // tr.Info << std::endl << movemap_;
397  print_debug(pose);
398  PROF_START( basic::STAGE3 );
399  clock_t starttime = clock();
400 
401  prepare_stage3( pose );
402  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose );
403 
404  do_stage3b_cycles( pose );
405  mc().recover_low( pose );
406 
407  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose );
408  mc().show_counters();
410  mc().reset_counters();
411 
412  clock_t endtime = clock();
413  PROF_STOP( basic::STAGE3);
414 
416  output_debug_structure( pose, "stage3" );
417  tr << "Timeperstep: " << (double(endtime) - starttime )/( CLOCKS_PER_SEC) << std::endl;
418  prof_show();
419  }
420  }
421 
422  }
423  //ADD A NEW REGION TO BE INSERTED
424  //tr.Info << "Before: tmh_inserted total_tmhelix " << tmh_inserted << " " << topology.tmh_inserted() << " " << total_tmhelix << std::endl;
425  tmh_inserted=get_tmh_inserted(pose);
426  //tr.Info << "Before: tmh_inserted total_tmhelix " << tmh_inserted << " " << topology.tmh_inserted() << " " << total_tmhelix << std::endl;
427  add_spanning_region(pose);
428  //tr.Info << "After: tmh_inserted total_tmhelix " << tmh_inserted << " " << topology.tmh_inserted() << " " << total_tmhelix << std::endl;
429  }
430  } // if (! only_stage4 )
431  move_all_inserted(pose);
432 
433 
434  // part 4 ------------------------------------------
435  tr.Info << "\n===================================================================\n";
436  tr.Info << " Stage 4 \n";
437  tr.Info << " Folding ALL REGIONS with score_membrane for " << stage4_cycles() <<std::endl;
438  print_debug(pose);
439  PROF_START( basic::STAGE4 );
440  clock_t starttime = clock();
441 
442  prepare_stage4( pose );
443 
444  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose);
445 
446  do_stage4_cycles( pose );
447  mc().recover_low( pose );
448 
449  if ( tr.Info.visible() ) current_scorefxn().show( tr, pose);
450  mc().show_counters();
452  mc().reset_counters();
453 
454  clock_t endtime = clock();
455  PROF_STOP( basic::STAGE4 );
456 
458  output_debug_structure( pose, "stage4" );
459  tr << "Timeperstep: " << (double(endtime) - starttime )/( CLOCKS_PER_SEC ) << std::endl;
460  prof_show();
461  }
462 
463  tr.Info << "\n===================================================================\n";
464  tr.Info << " Finished Abinitio \n";
465  tr.Info << " Total trials " << total_trials_ << "\n";
466  tr.Info << std::endl;
467 
468 // score_stage3a_->show( tr, pose );
469 // score_stage3b_->show( tr, pose );
470 // tr.flush();
472  // structure_store().clear();
473  return; // true;
474 } // MembraneAbinitio::apply( pose::Pose & pose )
475 
476 
477 //@brief return FramgentMover for smooth_small fragment insertions (i.e., stage4 moves)
480  return smooth_move_small_;
481 }
482 
485  return "MembraneAbinitio";
486 }
487 
488 //@brief return FragmentMover for small fragment insertions ( i.e., stage3/4 moves )
491  return brute_move_small_;
492 }
493 
497 }
498 
499 //@brief return FragmentMover for large fragment insertions (i.e., stage1/2 moves )
502  return brute_move_large_;
503 }
504 
505 //@brief change the movemap ( is propagated to mover-objects )
506 //@detail overload if your extension stores additional moves as member variables
507 void
509 {
510  movemap_ = mm;
515 }
516 
517 //@brief set new instances of FragmentMovers
518 void
523 )
524 {
528  update_moves();
529 }
530 
531 //@brief returns current movemap
534  return movemap_;
535 }
536 
537 //@detail read cmd_line options and set default versions for many protocol members: trials/moves, score-functions, Monte-Carlo
539  temperature_ = 2.0;
542  set_default_mc( pose, *score_stage1_ );
543  update_moves();
544 }
545 
546 //@detail called to notify about changes in Movers: new movemap or Moverclass
548  /* set apply_large_frags_ and
549  short_insert_region_
550  */
551  /* what about move-map ? It can be set manually for all Fragment_Moves .. */
552  // set_move_map();
553  set_trials();
554 }
555 
556 //@detail create instances of TrialMover for our FragmentMover objects
558  // setup loop1
559  assert( brute_move_large_ );
561  // trial_large_->keep_stats_type( false );
562  //trial_large_->keep_stats_type( moves::all_stats );
563 
564  assert( brute_move_small_ );
566  //trial_small_->set_keep_stats( false );
567  // trial_small_->keep_stats_type( false );
568  //trial_small_->keep_stats_type( moves::all_stats );
569 
570  assert( brute_move_small_top25_ );
572  //trial_small_top25_ ->set_keep_stats( false );
573  //trial_small_top25_ ->keep_stats_type( false );
574  //trial_small_top25_ ->keep_stats_type( moves::all_stats );
575 
576  assert( smooth_move_small_ );
578  // smooth_trial_small_->set_keep_stats( false );
579  //smooth_trial_small_->keep_stats_type( false );
580  //smooth_trial_small_ ->keep_stats_type( moves::all_stats );
581 }
582 
583 //@detail sets Monto-Carlo object to default
585  set_mc( new moves::MonteCarlo( pose, scorefxn, temperature_ ) );
586 }
587 
588 //@detail sets Monto-Carlo object
590  mc_ = mc_in;
591  if ( trial_large_ ) trial_large_->set_mc( mc_ );
592  if ( trial_small_ ) trial_small_->set_mc( mc_ );
593  if ( trial_small_top25_ ) trial_small_top25_ ->set_mc( mc_ );
594  if ( smooth_trial_small_ ) smooth_trial_small_->set_mc( mc_ );
595 }
596 
597 //@detail override cmd-line setting for "increase_cycling"
598 void MembraneAbinitio::set_cycles( Real increase_cycles ) {
599  stage1_cycles_ = static_cast< int > (10000 * increase_cycles); //it will bail out if all fragments are replaced and since we are dealing with proteins longer than 50 residues we need some more cycles.
600  stage2_cycles_ = static_cast< int > (2000 * increase_cycles);
601  stage3_cycles_ = static_cast< int > (2000 * increase_cycles);
602  stage4_cycles_ = static_cast< int > (4000 * increase_cycles);
603 
604  using namespace basic::options;
605 // if ( option[ OptionKeys::abinitio::only_stage1_2 ]() ) {
606 // stage2_cycles_ = 0;
607 // stage3_cycles_ = 0;
608 // stage4_cycles_ = 0;
609 // }
610 }
611 
613  using namespace scoring;
614  tr.Debug << "creating membrane scoring functions" << std::endl;
617  //score_stage2_ = ScoreFunctionFactory::create_score_function( "score2" );
621 
622 }
623 
624 /// @brief sets a score weight for all stages of abinitio
626  tr.Debug << "set score weights for ";
627  if ( stage == ALL_STAGES ) tr.Debug << "all stages ";
628  else tr.Debug << "stage " << (stage <= STAGE_3a ? stage : ( stage-1 ) ) << ( stage == STAGE_3b ? "b " : " " );
629  tr.Debug << scoring::name_from_score_type(type) << " " << setting << std::endl;
630  if (score_stage1_ && ( stage == STAGE_1 || stage == ALL_STAGES )) score_stage1_ ->set_weight(type, setting);
631  if (score_stage2_ && ( stage == STAGE_2 || stage == ALL_STAGES )) score_stage2_ ->set_weight(type, setting);
632  if (score_stage3a_ && ( stage == STAGE_3a || stage == ALL_STAGES )) score_stage3a_->set_weight(type, setting);
633  if (score_stage3b_ && ( stage == STAGE_3b || stage == ALL_STAGES )) score_stage3b_->set_weight(type, setting);
634  if (score_stage4_ && ( stage == STAGE_4 || stage == ALL_STAGES )) score_stage4_ ->set_weight(type, setting);
635 }
636 
637  //@brief currently used score function ( depends on stage )
639  return mc().score_function();
640 }
641 
642 //@brief set current scorefunction
644  mc().score_function( scorefxn );
645 }
646 
647 //@brief set individual weight of current scorefunction --- does not change the predifined scores: score_stageX_
649  scoring::ScoreFunction scorefxn ( mc().score_function() );
650  scorefxn.set_weight( type, setting );
651  mc().score_function( scorefxn ); //trigger rescore
652 }
653 
655  using namespace basic::options;
656  just_smooth_cycles_ = option[ OptionKeys::abinitio::smooth_cycles_only ]; // defaults to false
657 // bQuickTest_ = option[ OptionKeys::abinitio::test_2 ];
658  bQuickTest_ = false;
659  if ( bQuickTest() ) {
660  set_cycles( 0.001 );
661  } else {
662  set_cycles( option[ OptionKeys::abinitio::increase_cycles ] ); // defaults to factor of 1.0
663  }
664 
666 
667  //bw fix for uninitialized value
668  apply_large_frags_ = true; // apply large frags in phase 2!
669 
670  // in rosetta++ switched on in fold_abinitio if contig_size < 30 in pose_abinitio never
671  //bw fix for uninitialized value
672  short_insert_region_ = false; // apply small fragments in phase 2!
673 }
674 
675 
676 
678 
679  //using namespace common_regions;
680  //using namespace misc;
681  //using namespace membrane;
682  //using namespace protein_maps;
683 
684  //bool exist;
685  //bool none_selected = true;
686  Size new_membrane_region=0;
687  bool done=false;
688  Size TMHs=0;
689  //Size membrane_jump_counter=1;
690  Size const num_cutpoints(pose.fold_tree().num_cutpoint());
691  Size const num_jumps(pose.num_jump());
693  Size const total_tmhelix=topology.tmhelix();
694  Size const total_residue=pose.total_residue();
695 
696 
697 
698 
699  tr.Info << "Adding region tmh:" << total_tmhelix << " tmh_inserted: " << topology.tmh_inserted() << " nres: " << total_residue << " num_jumps: " << num_jumps << std::endl;
700 
701  //FArray2D_int const & jump_point( pose.fold_tree().get_jump_point() );
702 
703  utility::vector1< Size > const & cuts( pose.fold_tree().cutpoints() ); //( num_cutpoints ));
704 
705  //bw just define this vector to interface with the r++ code might change later...
706  FArray2D_int jump_point(num_cutpoints,2);
707 
708 
709  FArray1D_bool new_region(total_residue,false);
710  FArray1D_bool inserted_regions(total_residue,false);
711 
712  //Assigns a vector with the helices numbered.
713  FArray1D<Size> res_TMH_mapping(pose.total_residue()); //should perhaps be global..
714  FArray2D_bool nb_tmh_list(total_tmhelix,total_tmhelix,false);
715 
716  //check all helices that are connect by a jump.
717  FArray2D_bool tmh_jump(total_tmhelix,total_tmhelix,false);
718  for ( Size j = 1; j <= pose.total_residue(); ++j ) {
719  new_region(j) = false;
720  inserted_regions(j) = false;
721 
722  //bw change definition of membrane region to include jumps to non-tmh.
723  if(j<=topology.span_end(1)) //membrane_helix(1,2))
724  {
725  res_TMH_mapping(j)=1;
726  } else if(j>topology.span_end(total_tmhelix)) {
727  res_TMH_mapping(j)=total_tmhelix;
728  }
729  else
730  {
731  for ( Size reg = 2; reg <= total_tmhelix; ++reg ) {
732  if(j>topology.span_end(reg-1) && j<=topology.span_end(reg)) //membrane_helix( reg-1, 2 ) && j<=membrane_helix(reg,2))
733  {
734  res_TMH_mapping(j)=reg;
735  }
736  }
737  }
738 // tr.Info << "RES TMH " << j << " " << res_TMH_mapping(j) << std::endl;
739  }
740 
741  //mark tmh as neighbours if they are consecutive and no cut is between them
742  for ( Size reg = 1; reg < total_tmhelix; ++reg ) {
743  // check cuts.
744  bool no_cut=true;
745  for(Size i=topology.span_end(reg);i<topology.span_begin(reg+1);++i) { // (int i=membrane_helix(reg,2);i<membrane_helix(reg+1,2)??? no cut in tmh so it should be the same as check to begin;++i){
746  for(Size j=1;j<=num_cutpoints;++j){
747  if(cuts[j]==i)
748  no_cut=false;
749  }
750  }
751  if(no_cut)
752  {
753  nb_tmh_list(reg,reg+1)=true;
754  nb_tmh_list(reg+1,reg)=true;
755  //std::cout << "NO " << reg << "," << reg+1 << std::endl;
756  }
757  }
758 
759 
760  //check all helices that are connect by a jump.
761  //diagonal elements are true if the helix is involved in a jump.
762  for(Size i=1;i<=num_jumps;++i)
763  {
766  tmh_jump(res_TMH_mapping(d),res_TMH_mapping(u))=true;
767  tmh_jump(res_TMH_mapping(d),res_TMH_mapping(d))=true;
768  tmh_jump(res_TMH_mapping(u),res_TMH_mapping(d))=true;
769  tmh_jump(res_TMH_mapping(u),res_TMH_mapping(u))=true;
770  nb_tmh_list(res_TMH_mapping(d),res_TMH_mapping(u))=true;
771  nb_tmh_list(res_TMH_mapping(u),res_TMH_mapping(d))=true;
772 
773  }
774  // check if all helices involved in a jump have been inserted.
775  // produce a vector with insertable regions.
776  // a region is insertable if it is next to something that is already inserted.
777  // either by sequence or by a jump. Or if nothing is inserted everything is insertable.
778 
779  // Choose to insert a jump region first. The next jump to be inserted will be choosen randomly.
780 
781  // When the fold tree gets complicted adjecent helices might not be in contact.
782 
783  FArray1D<Size> insertable_region(total_tmhelix);
784  Size k=0;
785  if(topology.tmh_inserted()==0)
786  {
787  for ( Size i = 1; i <= total_tmhelix; ++i ) {
788  if(tmh_jump(i,i) && !topology.allow_tmh_scoring(i)) //!TMH_done(i))
789  {
790  ++k;
791  insertable_region(k)=i;
792  tr.Info << "INSERTABLE JUMP REGION: " << i << std::endl;
793  }
794  }
795  }
796  if(k==0) // if no jumps are available
797  {
798  //non jump helices
799  if(topology.tmh_inserted()==0) //first pass?
800  {
801  for ( Size j = 1; j <= total_tmhelix; ++j ) {
802  ++k;
803  insertable_region(k)=j;
804  tr.Info << "INSERTABLE REGION FIRST PASS: " << j << std::endl;
805  }
806  } else {
807  for ( Size i = 1; i <= total_tmhelix; ++i ) {
808  if(topology.allow_tmh_scoring(i))
809  {
810  for ( Size j = 1; j <= total_tmhelix; ++j ) {
811  if(!topology.allow_tmh_scoring(j) && nb_tmh_list(i,j)) // neigbor in fold tree
812  {
813  ++k;
814  insertable_region(k)=j;
815  tr.Info << "INSERTABLE REGION: " << j << std::endl;
816  }
817  }
818  }
819  }
820  }
821  }
822  if(k>0) // any more regions to insert?
823  {
824  Size index=1 + static_cast< int >( numeric::random::uniform()*k);
825  FArray1D_bool new_membrane_regions(total_tmhelix,false); //need an array to handle the case when more than helix is inserted at the time.
826  new_membrane_region=insertable_region(index);
827  new_membrane_regions(new_membrane_region)=true;
828  if(tmh_jump(new_membrane_region,new_membrane_region)) // a "jump helix"
829  {
830  //insert all helices that are connected through jumps
831  // could in principle be a whole network of jumps..
832  for(Size i=1;i<=total_tmhelix;++i) {
833  new_membrane_regions(i)=tmh_jump(new_membrane_region,i);
834  if(new_membrane_regions(i)) {
835  for(Size j=1;j<=total_tmhelix;++j) {
836  new_membrane_regions(j)=tmh_jump(i,j);
837  }
838  }
839  }
840  for(Size i=1;i<=total_tmhelix;++i) {
841  if(new_membrane_regions(i))
842  {
843  topology.set_allow_tmh_scoring(i,true);
844  tr.Info << "NEW JUMP REGION: index " << i << std::endl;
845  }
846  }
847  }
848  else
849  {
850 
851  if(topology.tmh_inserted()==0) { // FIRST PASS
852  Size new_membrane_region2;
853  if(new_membrane_region==1){
854  new_membrane_region2=new_membrane_region+1;
855  } else if(new_membrane_region==total_tmhelix) {
856  new_membrane_region2=new_membrane_region-1;
857  } else if( numeric::random::uniform() < 0.5) {
858  new_membrane_region2=new_membrane_region+1;
859  }
860  else {
861  new_membrane_region2=new_membrane_region-1;
862  }
863  tr.Info << "NEW REGION FIRST PASS: region1 region2 " << new_membrane_region << " " << new_membrane_region2 << std::endl;
864  new_membrane_regions(new_membrane_region2)=true;
865  topology.set_allow_tmh_scoring(new_membrane_region2,true);
866 
867  }
868 
869 
870  tr.Info << "NEW REGION: index " << new_membrane_region << " " << index << " " << k << std::endl;
871 // TMH_done(new_membrane_region)=true;
872  topology.set_allow_tmh_scoring(new_membrane_region,true);
873 // allow_tmh_scoring( new_membrane_region ) = true;
874  // define maximum range middle_helix_num1 should be the first and middle_helix_num2 should be the last.
875  // membrane_score_quick then uses the the allow_tmh_scoring vector to know which helix to include in the
876  // membrane normal calculation. Assume they are correctly set before hand. We know that they span the min-max range
877  // and we only have to check if the newly added region will change the min-max boundery.
878  // if(new_membrane_region>middle_helix_num2)
879  // middle_helix_num2=new_membrane_region;
880  // if(new_membrane_region<middle_helix_num1)
881  // middle_helix_num1=new_membrane_region;
882  }
883 
884  //Mark up which residues that are allowed to scored based on the TMH_done.
885  Size start,end;
886  TMHs=0;
887  for ( Size i = 1; i <= total_tmhelix; ++i ) {
888  if(topology.allow_tmh_scoring(i)) { //TMH_done(i)){
889  if(i==1){
890  start=1;
891  } else {
892  start=topology.span_end(i-1); //membrane_helix( i-1, 2 ); //from old code
893  }
894  if(i==total_tmhelix){
895  end=pose.total_residue();
896  } else {
897  end=topology.span_begin(i+1); //membrane_helix( i+1, 1 ); //from old code
898  }
899  for(Size j=1;j<=num_cutpoints;++j) {
900  if(cuts[j] >= start && cuts[j] <= end) {
901  if(cuts[j]-start >= end-cuts[j]) {
902  end=cuts[j]; //the residue is cut off so do not include it in scoring.
903  } else {
904  start=cuts[j]+1;
905  }
906  }
907  }
908  for(Size j=start;j<=end;++j) {
909  inserted_regions(j)=true;
910  if(new_membrane_regions(i)) {
911  new_region(j)=true;
912  }
913  }
914  ++TMHs;
915  }
916  }
917  done=false;
918  }
919  else
920  {
921  done=true;
922  for ( Size j = 1; j <= total_residue; ++j ) {
923  inserted_regions(j) = true;
924  }
925  TMHs=total_tmhelix;
926  }
927  topology.set_tmh_inserted(TMHs);
929  if(done)
930  {
931  new_mm->set_bb( true );
932  for ( Size i = 1; i <= total_residue; ++i ) {
933  topology.set_allow_scoring(i,true);
934  }
935  } else {
936  for ( Size i = 1; i <= total_residue; ++i ) {
937  if(inserted_regions(i)) {
938  topology.set_allow_scoring(i,true);
939  } else {
940  topology.set_allow_scoring(i,false);
941  }
942  if(new_region(i) ){
943  new_mm->set_bb(i,true);
944  } else {
945  new_mm->set_bb(i,false);
946  }
947  }
948  }
949  set_movemap(new_mm);
950 
951  /*
952  middle_helix_num1=1;
953  middle_helix_num2=0;
954  for ( int j = 1; j <= total_tmhelix; ++j ) {
955  allow_tmh_scoring(j)=false;
956  if(TMH_done(j)) {
957  allow_tmh_scoring(j)=true;
958  middle_helix_num2=j;
959  } else if(middle_helix_num2==0) {
960  middle_helix_num1=j;
961  }
962  }
963  //bw This are important for scoring still...
964 
965 
966 
967  mres_start=1;
968  mres_end=1;
969  for ( int j = 1; j <= pose.total_residue(); ++j ) {
970  if(inserted_regions(j))
971  {
972  mres_end=j;
973  }
974  else if(mres_end==1)
975  {
976  mres_start=j;
977  }
978  }
979 
980  // exist = reset_insert_map();
981  //for ( int j = 1; j <= total_residue; ++j ) {
982  // pose.set_allow_bb_move(j,allow_insert(j));
983  //}
984 
985  */
986  tr.Info << "max_tm new_reg TMHs done tmh_inserted " << std::endl;
987 
988  tr.Info << I( 7, total_tmhelix ) << ' ' << I( 7, new_membrane_region ) << ' ' << I(4, TMHs) << ' ' << done << ' ' << topology.tmh_inserted() << std::endl;
989  /*
990  << ' ' << I( 7, middle_helix_num1 )
991  << ' ' << I( 7, middle_helix_num2 ) << ' ' << I( 7, mres_start ) << ' ' << I( 8, mres_end ) << ' '
992  << I( 4, TMHs ) << ' ' << L( 4, done ) << " " << A( 6, TMHpred_method ) << std::endl;
993  */
994  //for(int a=1;a<total_residue;++a)
995  // {
996  // if(allow_insert(a))
997  // {
998  // std::cout << "1";
999  // }
1000  // else
1001  // {
1002  // std::cout << "0";
1003  // }
1004  // }
1005  // std::cout << std::endl;
1006  //std::cout << insert_map(total_insert) << " " << total_insert << std::endl;
1007 
1008 
1009 }
1010 
1014  for(Size i=1;i<=pose.total_residue();++i){
1015  if(topology.allow_scoring(i)){
1016  new_mm->set_bb(i,true);
1017  } else {
1018  new_mm->set_bb(i,false);
1019  }
1020  }
1021  set_movemap(new_mm);
1022 }
1023 
1025  {
1026 // return;
1027 // if(!option[basic::options::OptionKeys::abinitio::membrane_print])
1028 // return;
1030  Size total_tmhelix=topology.tmhelix();
1031  FArray1D_int res_TMH_mapping(pose.total_residue()); //should perhaps be global..
1032  for ( Size j = 1; j <= pose.total_residue(); ++j ) {
1033  Size tmh=0;
1034  for ( Size reg = 1; reg <= total_tmhelix; ++reg ) {
1035  if(j>=topology.span_begin(reg) && j<=topology.span_end(reg)) //membrane_helix( reg-1, 2 ) && j<=membrane_helix(reg,2))
1036  {
1037  tmh=reg;
1038 
1039  }
1040  }
1041 
1042  res_TMH_mapping(j)=tmh;
1043  // tr.Info << "RES TMH " << j << " " << res_TMH_mapping(j) << std::endl;
1044  }
1045 
1046 // pose.dump_pdb("debug.pdb");
1047  std::cout << "TMH : ";
1048 
1049 
1050  for(Size i=1;i<=pose.total_residue();++i){
1051  std::cout << res_TMH_mapping(i);
1052  }
1053  std::cout <<"\n";
1054  std::cout << "SCORING : ";
1055  for(Size i=1;i<=pose.total_residue();++i){
1056  if(topology.allow_scoring(i)) {
1057  std::cout << 1;
1058  } else {
1059  std::cout << 0;
1060  }
1061  }
1062  std::cout <<"\n";
1063  std::cout << "MOVEMAP : ";
1064  for(Size i=1;i<=pose.total_residue();++i){
1065  if(movemap_->get_bb(i)) {
1066  std::cout << 1;
1067  } else {
1068  std::cout << 0;
1069  }
1070  }
1071  std::cout <<"\n";
1072  mc_->show_scores();
1073  current_scorefxn().show( std::cout, pose );
1074 
1075  }
1076 
1077 /// @brief (helper) functor class which keeps track of initial phi/psi values.
1078 /// @detail
1079 /// calls of operator ( pose ) compare the initial phi/psi values
1080 ////to the current values of the given pose. Returns false once all phi/psi values
1081 /// have been modified.
1083 public:
1085  insert_pos_( pose.total_residue(), false )
1086  {
1087  set_initial_pose( pose );
1088  compute_insert_pos( insert_map );
1089  }
1090 private:
1091 
1092  void compute_insert_pos( core::fragment::InsertMap const& insert_map ) {
1093  for ( core::fragment::InsertMap::const_iterator it = insert_map.begin(),
1094  eit = insert_map.end(); it != eit; ++it ) {
1095  Size const pos ( *it );
1096  if ( pos > insert_pos_.size() ) break;
1097  insert_pos_[ pos ] = true;
1098  }
1099  }
1100 
1101  void set_initial_pose( const core::pose::Pose & pose ) {
1102  for ( unsigned int i = 1; i <= pose.total_residue(); ++i ) {
1103  initial_phis.push_back( pose.phi(i) );
1104  initial_psis.push_back( pose.psi(i) );
1105  }
1106 
1107  original_sequence_ = pose.sequence();
1108  }
1109 
1110 public:
1111  virtual bool operator() ( const core::pose::Pose & pose ) {
1112  assert( original_sequence_ == pose.sequence() ); // imperfect attempt to check that Pose hasn't changed ...
1113  for ( unsigned int i = 1; i <= pose.total_residue(); ++i ) {
1114  if ( initial_phis[i] == pose.phi(i) && insert_pos_[ i ] ) {
1115  return false;
1116  }
1117  if ( initial_psis[i] == pose.psi(i) && insert_pos_[ i ] ) {
1118  return false;
1119  }
1120  }
1121  return true;
1122  }
1123 
1124  private:
1125  utility::vector1< core::Real > initial_phis;
1126  utility::vector1< core::Real > initial_psis;
1127 
1128  bool initialized_;
1129 
1130  std::string original_sequence_;
1131  utility::vector1< bool > insert_pos_;
1132 };
1133 
1134  /*
1135 /// @brief (helper) functor class which keeps track of old pose for the
1136 /// convergence check in stage3 cycles
1137 /// @detail
1138 /// calls of operator ( pose ) compare the
1139 class MonteCarloExceptionConverge;
1140 typedef utility::pointer::owning_ptr< MonteCarloExceptionConverge > MonteCarloExceptionConvergeOP;
1141 
1142 class MonteCarloExceptionConverge : public moves::PoseCondition {
1143 public:
1144  MonteCarloExceptionConverge() : bInit_( false ), ct_( 0 ) {};
1145  void reset() { ct_ = 0; bInit_ = false; };
1146  void set_trials( moves::TrialMoverOP trin ) {
1147  trials_ = trin;
1148  assert( trials_->keep_stats() );
1149  last_move_ = 0;
1150  };
1151  virtual bool operator() ( const core::pose::Pose & pose );
1152 private:
1153  pose::Pose very_old_pose_;
1154  bool bInit_;
1155  Size ct_;
1156  moves::TrialMoverOP trials_;
1157  Size last_move_;
1158 };
1159 
1160 // keep going --> return true
1161 bool MonteCarloExceptionConverge::operator() ( const core::pose::Pose & pose ) {
1162  if ( !bInit_ ) {
1163  bInit_ = true;
1164  very_old_pose_ = pose;
1165  return true;
1166  }
1167  assert( trials_ );
1168  tr.Trace << "TrialCounter in MonteCarloExceptionConverge: " << trials_->num_accepts() << std::endl;
1169  if ( numeric::mod(trials_->num_accepts(),100) != 0 ) return true;
1170  if ( (Size) trials_->num_accepts() <= last_move_ ) return true;
1171  last_move_ = trials_->num_accepts();
1172  // change this later to this: (after we compared with rosetta++ and are happy)
1173  // if ( numeric::mod(++ct_, 1000) != 0 ) return false; //assumes an approx acceptance rate of 0.1
1174 
1175  // still here? do the check:
1176 
1177  core::Real converge_rms = core::scoring::CA_rmsd( very_old_pose_, pose );
1178  very_old_pose_ = pose;
1179  if ( converge_rms >= 3.0 ) {
1180  return true;
1181  };
1182  // if we get here thing is converged stop the While-Loop
1183  tr.Info << " stop cycles in stage3 due to convergence " << std::endl;
1184  return false;
1185 }
1186 
1187 */
1189  AllResiduesChanged done( pose, brute_move_large()->insert_map() );
1190  Size j;
1191  for ( j = 1; j <= stage1_cycles(); ++j ) {
1192  // std::cout << j << " " << pose.energies() << "\n";
1193 
1194  trial_large()->apply( pose );// apply a large fragment insertion, accept with MC boltzmann probability
1195  // bool tmp=done(pose);
1196 // std::cout << tmp << "\n";
1197  //pose::Pose tmp_pose;
1198 
1199  //pose.dump_pdb(string_of(j)+".pdb");
1200  if(j % 1000==0)
1201  {
1202  tr.Info << "Step (score0) " << j << std::endl;
1203  }
1204  if ( done(pose) ) {
1205  tr.Info << "Replaced extended chain after " << j << " cycles" << std::endl;
1206  mc().reset( pose ); // make sure that we keep the final structure
1207  return j;
1208  }
1209  }
1210 
1211  tr.Info << "Warning: extended chain may still remain after " << stage1_cycles() << " cycles!" << std::endl;
1212  mc().reset( pose ); // make sure that we keep the final structure
1213  return j;
1214 }
1215 
1217 
1218  //setup cycle
1219 
1220  //if ( apply_large_frags_ ) cycle->add_mover( trial_large_->mover() );
1221  //if ( short_insert_region_ ) cycle->add_mover( trial_small_->mover() );
1222 
1223  /*
1224  Size j;
1225  Size cycles=stage2_cycles();
1226  //Size tmh_inserted=get_tmh_inserted(pose);
1227 // if(get_tmh_inserted(pose)==1) {
1228 // cycles*=;
1229 // }
1230 
1231  std::cout << "Start stage2\n";
1232  for ( j = 1; j <= cycles; ++j ) {
1233  trial_large()->apply( pose );
1234 
1235  //if(option[basic::options::OptionKeys::abinitio::membrane_print] && j%100 == 0) {
1236 
1237 
1238  // std::cout << "large j " << j <<"\n";
1239  // print_debug(pose);
1240  //}
1241 
1242  //if( tmh_inserted > 1)
1243  // pose.dump_pdb("stage_2_TMHs"+ObjexxFCL::string_of( tmh_inserted )+"-"+ObjexxFCL::string_of( j )); //+"_"+ObjexxFCL::string_of(lct2)
1244  }
1245  std::cout << "Start 3mer\n";
1246  for ( j = 1; j <= cycles; ++j ) {
1247  trial_small_top25()->apply( pose );
1248 
1249  if(option[basic::options::OptionKeys::abinitio::membrane_print] && j%100 == 0) {
1250  std::cout << "small1 j " << j <<"\n";
1251  print_debug(pose);
1252  }
1253 
1254 
1255 }
1256  std::cout << "Start 3mer\n";
1257  for ( j = 1; j <= cycles; ++j ) {
1258 
1259  trial_small_top25()->apply( pose );
1260 
1261  if(option[basic::options::OptionKeys::abinitio::membrane_print] && j%100 == 0) {
1262  std::cout << "small2 j " << j <<"\n";
1263  print_debug(pose);
1264  }
1265 
1266  }
1267  clock_t stop = clock();
1268  double t = (double) (stop-start)/CLOCKS_PER_SEC;
1269  std::cout << "End stage2: time " << t << "\n";
1270  */
1271  // clock_t start = clock();
1273  cycle->add_mover( trial_large_->mover() );
1274  cycle->add_mover( trial_small_top25_->mover() );
1275  cycle->add_mover( trial_small_top25_->mover() );
1276 
1277  Size nr_cycles = stage2_cycles(); // ( short_insert_region ? 2 : 1 );
1278  moves::TrialMoverOP trials = new moves::TrialMover( cycle, mc_ptr() );
1279  moves::RepeatMover( trials, nr_cycles ).apply(pose);
1280  // clock_t stop = clock();
1281  // double t = (double) (stop-start)/CLOCKS_PER_SEC;
1282  // std::cout << "End stage2: time " << t << "\n";
1283 // std::exit(1);
1284  // moves::RepeatMover( stage2_mover( pose, trials ), nr_cycles ).apply(pose);
1285 //*/
1286  //is there a better way to find out how many steps ? for instance how many calls to scoring?
1287  return 3*nr_cycles; //stage2_cycles(); //nr_cycles; // as best guess
1288 }
1289 
1290 
1291 
1292 
1293 /*! @detail stage3 cycles:
1294  nloop1 : outer iterations
1295  nloop2 : inner iterations
1296  stage3_cycle : trials per inner iteration
1297  every inner iteration we switch between score_stage3a ( default: score2 ) and score_stage3b ( default: score 5 )
1298 
1299  prepare_loop_in_stage3() is called before the stage3_cycles() of trials are started.
1300 
1301  first outer loop-iteration is done with TrialMover trial_large()
1302  all following iterations with trial_small()
1303 
1304  start each iteration with the lowest_score_pose. ( mc->recover_low() -- called in prepare_loop_in_stage3() )
1305 
1307  // interlaced score2 / score 5 loops
1308 
1309  // nloops1 and nloops2 could become member-variables and thus changeable from the outside
1310  int nloop1 = 1;
1311  int nloop2 = 9; //10; //careful: if you change these the number of structures in the structure store changes.. problem with checkpointing
1312  // individual checkpoints for each stage3 iteration would be a remedy. ...
1313  /*
1314  if ( short_insert_region_ ) {
1315  nloop1 = 2;
1316  nloop2 = 5;
1317  }
1318 
1319  MonteCarloExceptionConvergeOP convergence_checker ( NULL );
1320  if ( !option[ basic::options::OptionKeys::abinitio::skip_convergence_check ] ) {
1321  convergence_checker = new MonteCarloExceptionConverge;
1322  }
1323  */
1324  Size cycles=stage3_cycles();
1325 // if(get_tmh_inserted(pose)==1) {
1326 // cycles*=0.01;
1327 // }
1328  moves::TrialMoverOP trials = trial_large();
1329  int iteration = 1;
1330  for ( int lct1 = 1; lct1 <= nloop1; lct1++) {
1331  // if ( lct1 > 1 ) trials = trial_small(); //only with short_insert_region!
1332  for ( int lct2 = 1; lct2 <= nloop2; lct2++, iteration++ ) {
1333  tr.Debug << "Loop: " << lct1 << " " << lct2 << std::endl;
1334  if(lct2>3) trials = trial_small_top25();
1335  if ( !recover_checkpoint( pose, "stage_3_iter"+ObjexxFCL::string_of( lct1)+"_"+ObjexxFCL::string_of(lct2) )) {
1336  prepare_loop_in_stage3( pose, iteration, nloop1*nloop2 );// bw only sets temperature currently...
1337 
1338  // interlace score2/score5
1339  if ( numeric::mod( lct2, 2 ) == 0 || lct2 > 7 ) { // chainbreak ramping...
1340  Real chainbreak_weight=lct1*lct2*0.25;
1341  mc_->recover_low( pose );
1342  tr.Debug << "select score_stage3a..." << std::endl;
1343  ( *score_stage3a_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1344  mc_->score_function( *score_stage3a_ );
1345  // mc_->reset( pose );
1346  } else {
1347  Real chainbreak_weight=lct1*lct2*0.05;
1348  mc_->recover_low( pose );
1349  tr.Debug << "select score_stage3b..." << std::endl;
1350  ( *score_stage3b_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1351  mc_->score_function( *score_stage3b_ );
1352  // mc_->reset( pose );
1353  }
1354 
1355  tr.Debug << " Score stage3 loop iteration " << lct1 << " " << lct2 << std::endl;
1356  /* if ( convergence_checker ) {
1357  moves::TrialMoverOP stage3_trials = stage3_mover( pose, lct1, lct2, trials );
1358  convergence_checker->set_trials( stage3_trials ); //can be removed late
1359  moves::WhileMover( stage3_trials, stage3_cycles(), convergence_checker ).apply( pose );
1360  } else { //no convergence check -> no WhileMover */
1361  moves::RepeatMover( stage3_mover( pose, lct1, lct2, trials ), stage3_cycles() ).apply( pose );
1362  //moves::RepeatMover( trials, stage3_cycles() ).apply( pose );
1363 
1364 
1365 // for(Size j=1;j<=cycles;++j) {
1366 // trials->apply(pose);
1367  /*
1368  if(option[basic::options::OptionKeys::abinitio::membrane_print] && j%100 == 0) {
1369  std::cout << "3 j " << j << " " << lct2 << "\n";
1370  print_debug(pose);
1371  }
1372  */
1373 // }
1374 
1375  // }
1376  checkpoint( pose, "stage_3" );
1377  // structure_store().push_back( mc_->lowest_score_pose() );
1378  }//recover_checkpoint
1379  }; // loop 2
1380  }; // loop 1
1381  return nloop1*nloop2*cycles; //stage3_cycles();
1382  }
1383 
1384 
1385 
1387  // interlaced score2 / score 5 loops
1388 
1389  // nloops1 and nloops2 could become member-variables and thus changeable from the outside
1390  int nloop1 = 1;
1391  int nloop2 = 2; //10; //careful: if you change these the number of structures in the structure store changes.. problem with checkpointing
1392  // individual checkpoints for each stage3 iteration would be a remedy. ...
1393 /*
1394  if ( short_insert_region_ ) {
1395  nloop1 = 2;
1396  nloop2 = 5;
1397  }
1398 
1399  MonteCarloExceptionConvergeOP convergence_checker ( NULL );
1400  if ( !option[ basic::options::OptionKeys::abinitio::skip_convergence_check ] ) {
1401  convergence_checker = new MonteCarloExceptionConverge;
1402  }
1403  */Size cycles=stage3_cycles();
1404  //if(get_tmh_inserted(pose)==1) {
1405  // cycles*=0.01;
1406  //}
1408  int iteration = 1;
1409  for ( int lct1 = 1; lct1 <= nloop1; lct1++) {
1410  // if ( lct1 > 1 ) trials = trial_small(); //only with short_insert_region!
1411  for ( int lct2 = 1; lct2 <= nloop2; lct2++, iteration++ ) {
1412  tr.Debug << "Loop: " << lct1 << " " << lct2 << std::endl;
1413 // if(lct2>3) trial_small();
1414  if ( !recover_checkpoint( pose, "stage_3_iter"+ObjexxFCL::string_of( lct1)+"_"+ObjexxFCL::string_of(lct2) )) {
1415  prepare_loop_in_stage3( pose, iteration, nloop1*nloop2 );// bw only sets temperature currently...
1416 
1417  // interlace score2/score5
1418  if ( numeric::mod( lct2, 2 ) == 0 || lct2 > 7 ) { // chainbreak ramping...
1419  Real chainbreak_weight=lct1*lct2*0.25;
1420  mc_->recover_low( pose );
1421  tr.Debug << "select score_stage3a..." << std::endl;
1422  ( *score_stage3a_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1423  mc_->score_function( *score_stage3a_ );
1424  // mc_->reset( pose );
1425  } else {
1426  Real chainbreak_weight=lct1*lct2*0.05;
1427  mc_->recover_low( pose );
1428  tr.Debug << "select score_stage3b..." << std::endl;
1429  ( *score_stage3b_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1430  mc_->score_function( *score_stage3b_ );
1431  // mc_->reset( pose );
1432  }
1433 
1434  tr.Debug << " Score stage3 loop iteration " << lct1 << " " << lct2 << std::endl;
1435  /* if ( convergence_checker ) {
1436  moves::TrialMoverOP stage3_trials = stage3_mover( pose, lct1, lct2, trials );
1437  convergence_checker->set_trials( stage3_trials ); //can be removed late
1438  moves::WhileMover( stage3_trials, stage3_cycles(), convergence_checker ).apply( pose );
1439  } else { //no convergence check -> no WhileMover */
1440  moves::RepeatMover( stage3_mover( pose, lct1, lct2, trials ), stage3_cycles() ).apply( pose );
1441  //moves::RepeatMover( trials, stage3_cycles() ).apply( pose );
1442 
1443  //for(Size j=1;j<=cycles;++j) {
1444  // trials->apply(pose);
1445  /*
1446  if(option[basic::options::OptionKeys::abinitio::membrane_print] && j%100 == 0) {
1447 
1448  std::cout << "3b j " << j << " " << lct2 << "\n";
1449  print_debug(pose);
1450  }*/
1451 
1452  //}
1453  // }
1454  checkpoint( pose, "stage_3" );
1455  // structure_store().push_back( mc_->lowest_score_pose() );
1456  }//recover_checkpoint
1457  }; // loop 2
1458  }; // loop 1
1459  return nloop1*nloop2*cycles; //stage3_cycles();
1460 }
1461 
1462 
1463 
1466  return trials;
1467 }
1468 
1469 
1472  return trials;
1473 }
1474 
1477  return trials;
1478 }
1479 
1480  // interlaced score2 / score 5 loops
1481 /*! @detail stage4 cycles:
1482  nloop_stage4: iterations
1483  stage4_cycle : trials per iteration
1484 
1485  first iteration: use trial_small()
1486  following iterations: use trial_smooth()
1487  only trial_smooth() if just_smooth_cycles==true
1488 
1489  prepare_loop_in_stage4() is called each time before the stage4_cycles_ of trials are started.
1490 
1491  start each iteration with the lowest_score_pose. ( mc->recover_low() in prepare_loop_in_stage4() )
1492 
1493 */
1495  Size nloop_stage4 = 3;
1496 
1497  for ( Size kk = 1; kk <= nloop_stage4; ++kk ) {
1498  if (!recover_checkpoint( pose, "stage4_kk_" + ObjexxFCL::string_of(kk))) {
1499  // if ( kk == 2 ) choose_frag_set_top_N_frags(number3merfrags);
1500  moves::TrialMoverOP trials;
1501  if ( kk == 1 && !just_smooth_cycles_ ) {
1502  trials = trial_small_top25();
1503  } else if(kk==2) {
1504  trials = trial_small();
1505  } else {
1506  tr.Debug << "switch to smooth moves" << std::endl;
1507  trials = trial_smooth();
1508  }
1509  Real chainbreak_weight=kk*0.5+2.5;
1510  ( *score_stage4_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1511  mc_->score_function( *score_stage4_ );
1512  mc_->reset( pose );
1513  tr.Debug << "prepare ..." << std::endl ;
1514  prepare_loop_in_stage4( pose, kk, nloop_stage4 );
1515  tr.Debug << "start " << stage4_cycles() << " cycles" << std::endl;
1516  moves::RepeatMover( stage4_mover( pose, kk, trials ), stage4_cycles() ).apply(pose);
1517  //don't store last structure since it will be exactly the same as the final structure delivered back via apply
1518  // structure_store().push_back( mc_->lowest_score_pose() );
1519  tr.Debug << "finished" << std::endl;
1520  checkpoint( pose, "stage4_kk_" + ObjexxFCL::string_of(kk));
1521  }
1522  } // loop kk
1523  return stage4_cycles() * nloop_stage4;
1524 }
1525 
1526 // anything you want to have done before the stages ?
1527 
1528 // prepare stage1 sampling
1530  // don't do the following, because it destroys a user-defined mc.
1531  // set_default_mc( pose, *score_stage1_ ); //get a new mc-Object in case pose is not the same as before
1532 // Real chainbreak_weight=1.0;
1533 // ( *score_stage1_).set_weight(scoring::linear_chainbreak,chainbreak_weight);
1534 // ( *score_stage1_).set_weight(scoring::chainbreak,chainbreak_weight);
1535 
1536  mc_->score_function( *score_stage1_ );
1537  mc_->set_autotemp( false, temperature_ );
1538  mc_->set_temperature( temperature_ );
1539  mc_->reset( pose );
1540  (*score_stage1_)( pose );
1541  //score_stage1_->accumulate_residue_total_energies( pose ); // fix this
1543  new_mm->set_bb( true );
1544  set_movemap(new_mm);
1545 }
1546 
1548  mc_->score_function( *score_stage2_ );
1549  mc_->set_autotemp( true, temperature_ );
1550  mc_->set_temperature( temperature_ ); // temperature might have changed due to autotemp..
1551  mc_->reset( pose );
1552 
1553  (*score_stage2_)(pose);
1554  ///score_stage2_->accumulate_residue_total_energies( pose );
1555 }
1556 
1557 
1559  mc_->score_function( *score_stage3a_ );
1560  mc_->set_autotemp( true, temperature_ );
1561  mc_->set_temperature( temperature_ ); // temperature might have changed due to autotemp..
1562  mc_->reset( pose );
1563  //score for this stage is changed in the do_stage3_cycles explicitly
1564  /*if ( option[ templates::change_movemap_2 ].user() && option[ templates::change_movemap_2 ] == 3 ) {
1565  kinematics::MoveMapOP new_mm = new kinematics::MoveMap( *movemap() );
1566  new_mm->set_bb( true );
1567  set_movemap( new_mm ); // --> store it in movemap_ --> original will be reinstated at end of apply()
1568  }
1569  */
1570 }
1571 
1572 
1574  mc_->set_autotemp( true, temperature_ );
1575  mc_->set_temperature( temperature_ ); // temperature might have changed due to autotemp..
1576  mc_->score_function( *score_stage4_ );
1577  mc_->reset( pose );
1578 
1579  (*score_stage4_)( pose );
1580  ///score_stage4_->accumulate_residue_total_energies( pose ); // fix this
1581 /*
1582  if ( option[ templates::change_movemap_2 ].user() && option[ templates::change_movemap_2 ] == 4 ) {
1583  kinematics::MoveMapOP new_mm = new kinematics::MoveMap( *movemap() );
1584  new_mm->set_bb( true );
1585  set_movemap( new_mm ); // --> store it in movemap_ --> original will be reinstated at end of apply()
1586  }
1587 */
1588 }
1589 
1591  // mc_->recover_low( pose );
1592  mc_->set_temperature( temperature_ );
1593 }
1594 
1596  // mc_->recover_low( pose );
1597  mc_->set_temperature( temperature_ );
1598 }
1599 
1601  return checkpoint_.recover_checkpoint( pose, get_current_tag(), id );
1602 }
1603 // if (!protocols::checkpoint::Timer::is_on()) return false; // required for checkpoint
1604 
1605 // // must be same as in checkpoint function
1606 // std::string checkpoint_id( type() + "_" + get_current_tag() + "_" + id );
1607 
1608 // if (utility::file::file_exists( checkpoint_id + ".pdb" )) {
1609 // utility::io::izstream izs(checkpoint_id + ".rng.state.gz");
1610 // numeric::random::RandomGenerator::restoreAllStates(izs);
1611 // izs.close();
1612 // core::import_pose::centroid_pose_from_pdb( pose, checkpoint_id + ".pdb" );
1613 // if (utility::file::file_exists( checkpoint_id + ".mc_last.pdb" )) {
1614 // pose::Pose recovered_mc_last;
1615 // core::import_pose::centroid_pose_from_pdb( recovered_mc_last, checkpoint_id + ".mc_last.pdb" );
1616 // mc_->set_last_accepted_pose( recovered_mc_last );
1617 // }
1618 // if (utility::file::file_exists( checkpoint_id + ".mc_low.pdb" )) {
1619 // pose::Pose recovered_mc_low;
1620 // core::import_pose::centroid_pose_from_pdb( recovered_mc_low, checkpoint_id + ".mc_low.pdb" );
1621 // mc_->set_lowest_score_pose( recovered_mc_low );
1622 // }
1623 // checkpoint_ids_.push_back( checkpoint_id );
1624 // // tr << "Recovered checkpoint: " << checkpoint_id << std::endl;
1625 // return true;
1626 // }
1627 
1628 // return false;
1629 //}
1630 
1632  checkpoint_.checkpoint( pose, get_current_tag(), id );
1633 }
1634 // if (!protocols::checkpoint::Timer::time_to_checkpoint()) return; // required for checkpoint
1635 
1636 // if (get_current_tag() == "NoTag") return;
1637 
1638 // std::string checkpoint_id( type() + "_" + get_current_tag() + "_" + id );
1639 
1640 // checkpoint_ids_.push_back( checkpoint_id );
1641 
1642 // utility::io::ozstream ozs(checkpoint_id + ".rng.state.gz");
1643 // numeric::random::RandomGenerator::saveAllStates(ozs);
1644 // ozs.close();
1645 
1646 // io::pdb::dump_pdb( mc_->last_accepted_pose(), checkpoint_id + ".mc_last.pdb" );
1647 // io::pdb::dump_pdb( mc_->lowest_score_pose(), checkpoint_id + ".mc_low.pdb" );
1648 // io::pdb::dump_pdb( pose, checkpoint_id + ".pdb" );
1649 // // tr << "Created checkpoint: " << checkpoint_id << std::endl;
1650 
1651 // protocols::checkpoint::Timer::reset(); // required for checkpoint
1652 // }
1653 
1654 /// @brief for debugging, one wants to have access to the native pose.
1655 //void MembraneAbinitio::set_native_pose( core::pose::Pose const & /*pose*/ ) {
1656  // native_pose_ = pose; // this is bad since one doesn't know if set or not. make it a Pointer!
1657 //}
1658 
1661 }
1662 // for ( int i = 0; i < int( checkpoint_ids_.size() ); i++ ) {
1663 // // tr << "deleting checkpoint files with id: " << checkpoint_ids_[i] << std::endl;
1664 // utility::file::file_delete( checkpoint_ids_[i] + ".mc_last.pdb" );
1665 // utility::file::file_delete( checkpoint_ids_[i] + ".mc_low.pdb" );
1666 // utility::file::file_delete( checkpoint_ids_[i] + ".pdb" );
1667 // utility::file::file_delete( checkpoint_ids_[i] + ".rng.state.gz" );
1668 // }
1669 // checkpoint_ids_.clear();
1670 // } // MembraneAbinitio::clear_checkpoints()
1671 
1673  using namespace core::io::silent;
1674  if ( option[ basic::options::OptionKeys::out::file::silent ].user() ) {
1675  SilentFileData sfd;
1676  std::string silent_file = option[ basic::options::OptionKeys::out::file::silent ]() + "_" + prefix;
1677 
1678  mc().score_function()( pose );
1679 
1680  ProteinSilentStruct pss;
1681  pss.fill_struct( pose, get_current_tag() );
1682 
1683  evaluate_pose( pose, get_current_tag(), pss );
1684 
1685  sfd.write_silent_struct( pss, silent_file, !option[ OptionKeys::abinitio::debug_structures_2 ]() /* bWriteScoresOnly */ );
1686  } // if option[ out::file::silent ].user()
1687 
1688  if ( option[ basic::options::OptionKeys::abinitio::explicit_pdb_debug ]() ) {
1689  pose.dump_pdb( prefix + get_current_tag() + ".pdb" );
1690  }
1691 /*
1692  if ( option[ basic::options::OptionKeys::abinitio::log_frags_2 ].user() ) {
1693  std::string filename = prefix + "_" + get_current_tag() + "_" + std::string( option[ basic::options::OptionKeys::abinitio::log_frags_2 ]() );
1694  utility::io::ozstream output( filename );
1695  LoggedFragmentMover& log_frag = dynamic_cast< LoggedFragmentMover& > (*brute_move_large_);
1696  log_frag.show( output );
1697  log_frag.clear();
1698  } // if option[ abinitio::log_frags ].user()
1699 */
1700 
1701 } // MembraneAbinitio::output_debug_structure( core::pose::Pose & pose, std::string prefix )
1702 
1703  Size
1705  {
1707  }
1708  /// @details Pose must already contain a cenlist object or this method will fail.
1711  {
1712  //using core::pose::datacache::CacheableDataType::MEMBRANE_TOPOLOGY;
1713 
1714  return *( static_cast< core::scoring::MembraneTopology const * >( pose.data().get_const_ptr( core::pose::datacache::CacheableDataType::MEMBRANE_TOPOLOGY )() ));
1715  }
1716 
1717  /// @details Either returns a non-const reference to the cenlist object already stored
1718  /// in the pose, or creates a new cenist object, places it in the pose, and returns
1719  /// a non-const reference to it.
1722  {
1723  //using core::pose::datacache::CacheableDataType::MEMBRANE_TOPOLOGY;
1724 
1727  }
1728  // else
1730  pose.data().set( core::pose::datacache::CacheableDataType::MEMBRANE_TOPOLOGY, membrane_topology );
1731  return *membrane_topology;
1732  }
1733  /// @details Pose must already contain a cenlist object or this method will fail.
1736  {
1737  //using core::pose::datacache::CacheableDataType::MEMBRANE_EMBED;
1738 
1739  return *( static_cast< core::scoring::MembraneEmbed const * >( pose.data().get_const_ptr( core::pose::datacache::CacheableDataType::MEMBRANE_EMBED )() ));
1740  }
1741 
1742  /// @details Either returns a non-const reference to the cenlist object already stored
1743  /// in the pose, or creates a new cenist object, places it in the pose, and returns
1744  /// a non-const reference to it.
1747  {
1748  //using core::pose::datacache::CacheableDataType::MEMBRANE_EMBED;
1749 
1751  return *( static_cast< core::scoring::MembraneEmbed * >( pose.data().get_ptr( core::pose::datacache::CacheableDataType::MEMBRANE_EMBED )() ));
1752  }
1753  // else
1755  pose.data().set( core::pose::datacache::CacheableDataType::MEMBRANE_EMBED, membrane_embed );
1756  return *membrane_embed;
1757  }
1758 
1759 
1760 } //abinitio
1761 } //protocols