Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoopHashDiversifier.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 
11 /// @file protocols/loophash/movers/LoopHashDiversifier.cc
12 /// @brief Simple mover that uses loophash to replace randomly chosen fragments in a given pose.
13 /// Heavy inspiration taken by LoophashMoverWrapper.
14 /// @author Tim Jacobs
15 
16 // Unit headers
19 
20 // Project headers
21 #include <core/pose/Pose.hh>
23 #include <core/pose/selection.hh>
24 #include <utility/tag/Tag.hh>
25 #include <basic/Tracer.hh>
30 
43 #include <utility/string_util.hh>
44 
45 
46 #include <numeric/random/random.hh>
47 #include <numeric/random/random_permutation.hh>
48 
49 #include <utility/sort_predicates.hh>
50 
52 #include <core/pose/util.hh>
53 #include <utility/vector0.hh>
54 #include <utility/vector1.hh>
55 
56 //Auto Headers
57 #include <core/id/AtomID.hh>
58 
59 namespace protocols {
60 namespace loophash {
61 
62 using core::pose::Pose;
63 using namespace utility;
64 using namespace protocols::moves;
65 using core::Real;
66 using core::Size;
67 using std::string;
70 
71 static basic::Tracer TR( "protocols.loophash.LoopHashDiversifier" );
72 
73 ///****Creator Methods****///
76 {
78 }
79 
82  return new LoopHashDiversifier;
83 }
84 
87 {
88  return "LoopHashDiversifier";
89 }
90 ///****End Creator Methods****///
91 
93 
95  protocols::moves::Mover( LoopHashDiversifierCreator::mover_name() ),
96  library_( NULL ),
97  min_inter_ss_bbrms_( 0.0 ),
98  max_inter_ss_bbrms_( 100000.0 ),
99  min_intra_ss_bbrms_( 0.0 ),
100  max_intra_ss_bbrms_( 100000.0 ),
101  min_rms_( 0.0 ),
102  max_rms_( 100.0 ),
103  start_res_( 2 ),
104  stop_res_( 0 ),
105  window_size_(4),
106  max_radius_(4),
107  max_struct_(10),
108  num_iterations_(100),
109  num_try_div_(100),
110  ideal_( false ),
111  filter_by_phipsi_( false ),
112  cenfilter_( NULL ),
113  ranking_cenfilter_( NULL ),
114  scorefxn_cen_cst_(NULL),
115  scorefxn_rama_cst_(NULL)
116 {
117  loop_sizes_.clear();
118  loop_sizes_.push_back(window_size_);
119 
120  library_ = new LoopHashLibrary( loop_sizes() , 1 , 0 );
121  library_->load_mergeddb();
122  library_->mem_foot_print();
123 }
124 
126  LoopHashLibraryOP library,
127  core::Real min_inter_ss_bbrms,
128  core::Real max_inter_ss_bbrms,
129  core::Real min_intra_ss_bbrms,
130  core::Real max_intra_ss_bbrms,
131  core::Real min_rms,
132  core::Real max_rms,
133  core::Size start_res,
134  core::Size stop_res,
135  core::Size window_size,
136  core::Size max_radius,
137  core::Size max_struct,
138  core::Size num_iterations,
139  core::Size num_try_div,
140  bool ideal,
141  bool filter_by_phipsi,
143  protocols::filters::FilterOP ranking_cenfilter,
144  core::scoring::ScoreFunctionOP scorefxn_cen_cst,
145  core::scoring::ScoreFunctionOP scorefxn_rama_cst
146 ) :
147  protocols::moves::Mover( LoopHashDiversifierCreator::mover_name() ),
148  library_( library ),
149  min_inter_ss_bbrms_( min_inter_ss_bbrms ),
150  max_inter_ss_bbrms_( max_inter_ss_bbrms ),
151  min_intra_ss_bbrms_( min_intra_ss_bbrms ),
152  max_intra_ss_bbrms_( max_intra_ss_bbrms ),
153  min_rms_( min_rms ),
154  max_rms_( max_rms ),
155  start_res_( start_res ),
156  stop_res_( stop_res ),
157  window_size_(window_size),
158  max_radius_(max_radius),
159  max_struct_(max_struct),
160  num_iterations_(num_iterations),
161  ideal_( ideal ),
162  filter_by_phipsi_( filter_by_phipsi ),
163  cenfilter_( cenfilter ),
164  ranking_cenfilter_( ranking_cenfilter ),
165  scorefxn_cen_cst_(scorefxn_cen_cst),
166  scorefxn_rama_cst_(scorefxn_rama_cst)
167 {
168  loop_sizes_.clear();
169  loop_sizes_.push_back(window_size_);
170 
171  library_ = new LoopHashLibrary( loop_sizes() , 1 , 0 );
172  library_->load_mergeddb();
173  library_->mem_foot_print();
174 }
175 
176 
177 void
179 {
180  using namespace core::io::silent;
181  runtime_assert( library_ );
182  Pose const saved_pose( pose );
183 
185 
187 
188  LocalInserter_SimpleMinOP simple_inserter( new LocalInserter_SimpleMin() );
189  if(scorefxn_cen_cst_){simple_inserter->scorefxn_cen_cst(*scorefxn_cen_cst_);}
190  if(scorefxn_rama_cst_){simple_inserter->scorefxn_rama_cst(*scorefxn_rama_cst_);}
191 
192  LoopHashSampler lsampler( library_, simple_inserter );
193 
194  //Configure iteration-indepenent loophash options
195 
196  //Max rms of decoys
197  lsampler.set_min_rms( min_rms() );
198  lsampler.set_max_rms( max_rms() );
199 
200  //Run Loophash
201  lsampler.set_nonideal( !ideal_ );
202  lsampler.set_max_radius( max_radius_ );
203  lsampler.set_max_struct( max_struct_ );
205 
206  for(core::Size cur_iter=1; cur_iter<=num_iterations_; ++cur_iter)
207  {
208  core::Size cur_num_try_div = 1 ;
209  bool div_success = false;
210  while (!div_success && cur_num_try_div <= num_try_div_) // it tries upto num_try_div_ times to diversify
211  {
212  //Choose a random window-size of residues to run loophash on
213  core::Size lh_start = numeric::random::random_range(start_res_, stop_res_-window_size_+1);
214  core::Size lh_stop = lh_start+window_size_-1;
215 
216  lsampler.set_start_res( lh_start );
217  lsampler.set_stop_res ( lh_start );
218 
219  //Determine min and max torsion RMSD based on secondary structure
220  char sec_struct = pose.conformation().secstruct(lh_start);
221  bool inter_ss=false;
222  for(core::Size res=lh_start+1; res<=lh_stop; ++res)
223  {
224  if(pose.conformation().secstruct(res) != sec_struct)
225  {
226  inter_ss=true;
227  }
228  }
229 
230  if(inter_ss)
231  {
232  lsampler.set_min_bbrms( min_inter_ss_bbrms() );
233  lsampler.set_max_bbrms( max_inter_ss_bbrms() );
234  }
235  else
236  {
237  lsampler.set_min_bbrms( min_intra_ss_bbrms() );
238  lsampler.set_max_bbrms( max_intra_ss_bbrms() );
239  }
240 
241 
242  std::vector< SilentStructOP > lib_structs;
243  Size starttime = time( NULL );
244  lsampler.build_structures( pose, lib_structs );
245  Size endtime = time( NULL );
246  Size nstructs = lib_structs.size();
247  TR << "Found " << nstructs << " alternative states in time: " << endtime - starttime << std::endl;
248 
249  //Shuffle the loophash structures
250  // numeric::random::random_permutation(lib_structs.begin(), lib_structs.end(), numeric::random::RG);
251 
252  std::vector< std::pair< Real, SilentStructOP > > cen_scored_structs;
253  for(std::vector< SilentStructOP >::const_iterator struct_it = lib_structs.begin();
254  struct_it != lib_structs.end(); ++struct_it)
255  {
256  Pose rpose;
257  (*struct_it)->fill_pose( rpose );
258 
259  // apply selection criteria
260  bool passed_i = cenfilter_->apply( rpose );
261  if (passed_i) {
262  core::Real score_i = ranking_cenfilter()->report_sm( rpose );
263 
267  new_struct->fill_struct( rpose );
268  cen_scored_structs.push_back( std::pair<Real, SilentStructOP >(-score_i,new_struct) );
269  }
270  }
271 
272  // sort by centroid criteria
273  std::sort( cen_scored_structs.begin(), cen_scored_structs.end(), utility::SortFirst<Real, SilentStructOP>() );
274  all_structs_ = cen_scored_structs;
275  TR << "After centroid filter: " << all_structs_.size() << " of " << cen_scored_structs.size() << " structures" << std::endl;
276 
277  if( all_structs_.size() == 0 )
278  {
279  TR<<"No structures survived centroid filter. Consider relaxing filters"<<std::endl;
280  cur_num_try_div++;
281  //set_last_move_status( protocols::moves::FAIL_RETRY );
282  //return;
283  }
284  else
285  {
286  div_success = true;
287  }
288  } //while (!div_success && cur_num_try_div <= num_try_div_)
289 
290  if (!div_success)
291  {
292  TR<<"diversification failed after " << num_try_div_ << " trials"<<std::endl;
294  return;
295  }
296 
297  //Success!
299 
300  // make best from list the next starting structure
301  std::pair< Real, SilentStructOP > currbest = all_structs_.back();
302  all_structs_.pop_back();
303  TR << "Best score after round " << cur_iter << ": " << -currbest.first << std::endl;
304 
305  currbest.second->fill_pose( pose );
306 
307  }
308 
309  //Change to FA
311 }
312 
313 //core::pose::PoseOP
314 //LoopHashDiversifier::get_additional_output() {
315 // if ( all_structs_.size() == 0)
316 // return NULL;
317 //
318 // // pop best
319 // core::pose::PoseOP pose( new core::pose::Pose() );
320 // std::pair< core::Real, core::io::silent::SilentStructOP > currbest = all_structs_.back();
321 // all_structs_.pop_back();
322 // TR << "Returning score = " << -currbest.first << std::endl;
323 // currbest.second->fill_pose( *pose );
324 //
325 // return pose;
326 //}
327 
331 }
332 
333 void
335  DataMap & data,
336  protocols::filters::Filters_map const &filters,
337  Movers_map const &movers,
338  Pose const & pose
339 ){
340  num_iterations_ = tag->getOption< Real >( "num_iterations", 100 );
341  num_try_div_ = tag->getOption< Real >( "num_try_div", 100 );
342 
343  min_inter_ss_bbrms_ = tag->getOption< Real >( "min_inter_ss_bbrms", 0 );
344  max_inter_ss_bbrms_ = tag->getOption< Real >( "max_inter_ss_bbrms", 100000 );
345 
346  min_intra_ss_bbrms_ = tag->getOption< Real >( "min_intra_ss_bbrms", 0 );
347  max_intra_ss_bbrms_ = tag->getOption< Real >( "max_intra_ss_bbrms", 100000 );
348 
349  min_rms_ = tag->getOption< Real >( "min_rms", 0.0 );
350  max_rms_ = tag->getOption< Real >( "max_rms", 100.0 );
351 
352  max_radius_ = tag->getOption< Size >( "max_radius", 4 );
353 
354  max_struct_ = tag->getOption< Size >( "max_struct", 10 );
355 
356  ideal_ = tag->getOption< bool >( "ideal", false ); // by default, assume structure is nonideal
357  filter_by_phipsi_ = tag->getOption< bool >( "filter_by_phipsi", false );
358 
359  start_res_ = 2;
360  stop_res_ = pose.total_residue();
361  if ( tag->hasOption( "start_res_num" ) || tag->hasOption( "start_pdb_num"))
362  start_res_ = core::pose::get_resnum( tag, pose, "start_" );
363  if ( tag->hasOption( "stop_res_num" ) || tag->hasOption( "stop_pdb_num"))
364  stop_res_ = core::pose::get_resnum( tag, pose, "stop_" );
365 
366  window_size_ = tag->getOption< Size >( "window_size", 4 );
367 
368  if( tag->hasOption("scorefxn_cen_cst") )
369  {
370  std::string scorefxn_name = tag->getOption<string>( "scorefxn_cen_cst" );
371  scorefxn_cen_cst_ = data.get< ScoreFunction* >( "scorefxns", scorefxn_name );
372  }
373  if( tag->hasOption("scorefxn_rama_cst") )
374  {
375  std::string scorefxn_name = tag->getOption<string>( "scorefxn_rama_cst" );
376  scorefxn_rama_cst_ = data.get< ScoreFunction* >( "scorefxns", scorefxn_name );
377  }
378 
379  //Currently use only window_size fragment sizes
381 
382  // path to DB -- if not specified then command-line flag is used
383  library_ = new LoopHashLibrary( loop_sizes() , 1 , 0 );
384  if ( tag->hasOption( "db_path" )) {
385  std::string db_path = tag->getOption< string >( "db_path" );
386  library_->set_db_path( db_path );
387  }
388  library_->load_mergeddb();
389  library_->mem_foot_print();
390 
391  // centroid filter
392  string const centroid_filter_name( tag->getOption< string >( "centroid_filter", "true_filter" ) );
393  Filters_map::const_iterator find_cenfilter( filters.find( centroid_filter_name ) );
394  if( find_cenfilter == filters.end() )
395  utility_exit_with_message( "Filter " + centroid_filter_name + " not found in LoopHashDiversifier" );
396  cenfilter( find_cenfilter->second );
397  ranking_cenfilter( protocols::rosetta_scripts::parse_filter( tag->getOption< std::string >( "ranking_cenfilter", centroid_filter_name ), filters ) );
398 }
399 
400 
401 
402 //Min RMS
403 Real
405 
406 void
408  min_rms_ = min_rms;
409 }
410 
411 //Max RMS
412 Real
414 
415 void
417  max_rms_ = max_rms;
418 }
419 
420 //Min inter-ss bbrms
421 Real
423 
424 void
427 }
428 
429 //Max inter-ss bbrms
430 Real
432 
433 void
436 }
437 
438 
439 //Min intra-ss bbrms
440 Real
442 
443 void
446 }
447 
448 //Max intra-ss bbrms
449 Real
451 
452 void
455 }
456 
457 
458 //Number of iterations
462 }
463 
464 //Number of trys in each iteration
468 }
469 
470 void
473 }
474 
475 
478  return loop_sizes_;
479 }
480 
481 void
483  loop_sizes_.push_back( loop_size );
484 }
485 
486 } //loophash
487 } //protocols
488