Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoopHashMoverWrapper.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/LoopHashMoverWrapper.cc
12 /// @brief
13 /// @author Sarel Fleishman (sarelf@u.washington.edu)
14 
15 // Unit headers
18 
19 // Project headers
20 #include <core/pose/Pose.hh>
22 #include <core/pose/selection.hh>
23 #include <utility/tag/Tag.hh>
24 #include <boost/foreach.hpp>
25 #define foreach BOOST_FOREACH
26 #include <basic/Tracer.hh>
31 
32 // AUTO-REMOVED #include <core/chemical/ResidueTypeSet.hh>
33 // AUTO-REMOVED #include <core/chemical/ChemicalManager.hh>
34 // AUTO-REMOVED #include <core/scoring/rms_util.hh>
35 // AUTO-REMOVED #include <core/io/silent/ProteinSilentStruct.hh>
37 // AUTO-REMOVED #include <core/io/silent/SilentFileData.hh>
49 #include <utility/string_util.hh>
50 
51 
52 #include <numeric/random/random.hh>
53 #include <numeric/random/random_permutation.hh>
54 
55 #include <utility/sort_predicates.hh>
56 
58 #include <core/pose/util.hh>
59 #include <utility/vector0.hh>
60 #include <utility/vector1.hh>
61 
62 //Auto Headers
63 #include <core/id/AtomID.hh>
64 
65 namespace protocols {
66 namespace loophash {
67 
68 using core::pose::Pose;
69 using namespace utility;
70 using namespace protocols::moves;
71 using core::Real;
72 using core::Size;
73 using std::string;
76 
77 static basic::Tracer TR( "protocols.loophash.LoopHashMoverWrapper" );
78 
81 {
83 }
84 
87  return new LoopHashMoverWrapper;
88 }
89 
92 {
93  return "LoopHash";
94 }
95 
96 
98  protocols::moves::Mover( LoopHashMoverWrapperCreator::mover_name() ),
99  library_( NULL ),
100  fastrelax_( NULL ),
101  min_bbrms_( 0 ),
102  max_bbrms_( 0 ),
103  min_rms_( 0 ),
104  max_rms_( 0 ),
105  start_res_( 2 ),
106  stop_res_( 0 ),
107  max_nstruct_( 1000000 ),
108  cenfilter_( NULL ),
109  ranking_cenfilter_( NULL ),
110  fafilter_( NULL ),
111  ranking_fafilter_( NULL ),
112  nprefilter_( 0 ),
113  prefilter_scorefxn_( NULL ),
114  ideal_( false ),
115  sample_weight_const_( 1 )
116 {
117  loop_sizes_.clear();
118 }
119 
121 
122 void
124 {
125  using namespace core::io::silent;
126  runtime_assert( library_ );
127  Pose const saved_pose( pose );
128 
130 
131  // symmetric->asymmetric
133  core::pose::Pose pose_asu;
135  pose = pose_asu;
136  }
137 
139 
140  LocalInserter_SimpleMinOP simple_inserter( new LocalInserter_SimpleMin() );
141  LoopHashSampler lsampler( library_, simple_inserter );
142 
143  lsampler.set_start_res( start_res_ );
144  lsampler.set_stop_res ( stop_res_ );
145  lsampler.set_min_bbrms( min_bbrms() );
146  lsampler.set_max_bbrms( max_bbrms() );
147  lsampler.set_min_rms( min_rms() );
148  lsampler.set_max_rms( max_rms() );
149  lsampler.set_max_nstruct( max_nstruct() );
150  lsampler.set_nonideal( !ideal_ );
152  lsampler.set_max_radius( max_radius_ );//all hardcoded for testing
153  lsampler.set_max_struct( max_struct_ );
156 
157  std::vector< SilentStructOP > lib_structs;
158  Size starttime = time( NULL );
159  std::string sample_weight( "" );
160  for( Size resi = 1; resi <= pose.total_residue(); ++resi )
161  sample_weight += utility::to_string( sample_weight_const_ ) + " ";
162  core::pose::add_comment( pose, "sample_weight", sample_weight );
163  lsampler.build_structures( pose, lib_structs );
164  Size endtime = time( NULL );
165  Size nstructs = lib_structs.size();
166  TR << "Found " << nstructs << " alternative states in time: " << endtime - starttime << std::endl;
167  //std::random__shuffle( lib_structs.begin(), lib_structs.end() );
168  numeric::random::random_permutation(lib_structs.begin(), lib_structs.end(), numeric::random::RG);
169 
170  std::vector< std::pair< Real, SilentStructOP > > cen_scored_structs;
171  foreach( SilentStructOP structure, lib_structs ){
172  Pose rpose;
173  structure->fill_pose( rpose );
174 
175  // asymmetric->symmetric (if input was symmetric)
176  if (core::pose::symmetry::is_symmetric( saved_pose )) {
177  core::pose::Pose pose_asu = rpose;
178  rpose = saved_pose;
179 
180  // xyz copy
183  for (Size i=1; i<=pose_asu.total_residue(); ++i) {
184  if (pose_asu.residue_type(i).aa() == core::chemical::aa_vrt) continue;
185  for (Size j=1; j<=pose_asu.residue_type(i).natoms(); ++j) {
186  core::id::AtomID atm_ij(j,i);
187  atm_ids.push_back( atm_ij );
188  atm_xyzs.push_back( pose_asu.xyz( atm_ij ) );
189  }
190  }
191  rpose.batch_set_xyz( atm_ids, atm_xyzs );
192  }
193 
194  // apply selection criteria
195  bool passed_i = cenfilter_->apply( rpose );
196  if (passed_i) {
197  core::Real score_i = ranking_cenfilter()->report_sm( rpose );
199 
203  new_struct->fill_struct( rpose );
204  cen_scored_structs.push_back( std::pair<Real, SilentStructOP >(-score_i,new_struct) );
205  }
206  } // foreach structure
207 
208  // sort by centroid criteria
209  std::sort( cen_scored_structs.begin(), cen_scored_structs.end(), utility::SortFirst<Real, SilentStructOP>() );
210  if (ncentroid_ >= cen_scored_structs.size()) {
211  all_structs_ = cen_scored_structs;
212  } else {
213  std::vector< std::pair< Real, SilentStructOP > >::const_iterator first = cen_scored_structs.end() - ncentroid_;
214  std::vector< std::pair< Real, SilentStructOP > >::const_iterator last = cen_scored_structs.end();
215  all_structs_ = std::vector< std::pair< Real, SilentStructOP > >(first, last);
216  }
217  TR << "After centroid filter: " << all_structs_.size() << " of " << cen_scored_structs.size() << " structures" << std::endl;
218 
219  // if a relax mover is specified, apply to each structure passing the filter
220  if (fastrelax_) {
221  std::vector< std::pair< Real, SilentStructOP > > fa_scored_structs;
222  std::vector< std::pair< Real, SilentStructOP > >::const_iterator it = all_structs_.begin();
223 
224  while (it != all_structs_.end()) {
225  // prepare local batch
226  std::vector < SilentStructOP > relax_structs; // the local batch
227 
228  for (int i=0;i<(int)batch_size_; ++i) {
229  SilentStructOP new_struct;
230  new_struct = (*it).second->clone();
231 
232  relax_structs.push_back( new_struct );
233  if (++it == all_structs_.end()) break; // early exit
234  }
235 
236  TR << "BATCHSIZE: " << relax_structs.size() << std::endl;
237  fastrelax_->batch_apply( relax_structs );
238 
239  // Now save the resulting decoys
240  foreach( SilentStructOP structure, relax_structs ){
241  // inflate ...
242  Pose rpose;
243  structure->fill_pose( rpose );
244 
245  // ... check against filter ...
246  bool passed_i = fafilter_->apply( rpose );
247 
248  // ... and insert
249  if (passed_i) {
250  core::Real score_i = ranking_fafilter_->report_sm( rpose );
254  new_struct->fill_struct( rpose );
255  fa_scored_structs.push_back( std::pair<Real, SilentStructOP >(-score_i,new_struct) );
256  }
257  }
258  }
259 
260  // sort by fa criteria
261  std::sort( fa_scored_structs.begin(), fa_scored_structs.end(), utility::SortFirst<Real, SilentStructOP>() );
262  if (nfullatom_ >= fa_scored_structs.size()) {
263  all_structs_ = fa_scored_structs;
264  } else {
265  std::vector< std::pair< Real, SilentStructOP > >::const_iterator first = fa_scored_structs.end() - nfullatom_;
266  std::vector< std::pair< Real, SilentStructOP > >::const_iterator last = fa_scored_structs.end();
267  all_structs_ = std::vector< std::pair< Real, SilentStructOP > >(first, last);
268  }
269  TR << "After fullatom filter: " << all_structs_.size() << " of " << cen_scored_structs.size() << " structures" << std::endl;
270  }
271 
272  if( all_structs_.size() == 0 ){
273  TR<<"No structures survived fullatom filter. Consider relaxing filters"<<std::endl;
275  return;
276  }
277  // pop best
278  std::pair< Real, SilentStructOP > currbest = all_structs_.back();
279  all_structs_.pop_back();
280  TR << "Returning score = " << -currbest.first << std::endl;
281 
282  currbest.second->fill_pose( pose );
283 }
284 
287  if ( all_structs_.size() == 0)
288  return NULL;
289 
290  // pop best
291  core::pose::PoseOP pose( new core::pose::Pose() );
292  std::pair< core::Real, core::io::silent::SilentStructOP > currbest = all_structs_.back();
293  all_structs_.pop_back();
294  TR << "Returning score = " << -currbest.first << std::endl;
295  currbest.second->fill_pose( *pose );
296 
297  return pose;
298 }
299 
300 
304 }
305 
306 void
308  DataMap & data,
309  protocols::filters::Filters_map const &filters,
310  Movers_map const &movers,
311  Pose const & pose )
312 {
313  min_bbrms_ = tag->getOption< Real >( "min_bbrms", 0 );
314  max_bbrms_ = tag->getOption< Real >( "max_bbrms", 100000 );
315  min_rms_ = tag->getOption< Real >( "min_rms", 0.0 );
316  max_rms_ = tag->getOption< Real >( "max_rms", 4.0 );
317  max_nstruct_ = tag->getOption< Size >( "max_nstruct", 1000000 );
318  ideal_ = tag->getOption< bool >( "ideal", false ); // by default, assume structure is nonideal
319  max_radius_ = tag->getOption< Size >( "max_radius", 4 );
320  max_struct_ = tag->getOption< Size >( "max_struct", 10 );
321  max_struct_per_radius_ = tag->getOption< Size >( "max_struct_per_radius", 10 );
322  filter_by_phipsi_ = tag->getOption< bool >( "filter_by_phipsi", 1 );
323  sample_weight_const_ = tag->getOption< Real >( "sample_weight_const", 1.0 );
324 
325  start_res_ = 2;
326  stop_res_ = 0;
327  if ( tag->hasOption( "start_res_num" ) || tag->hasOption( "start_pdb_num"))
328  start_res_ = core::pose::get_resnum( tag, pose, "start_" );
329  if ( tag->hasOption( "stop_res_num" ) || tag->hasOption( "stop_pdb_num"))
330  stop_res_ = core::pose::get_resnum( tag, pose, "stop_" );
331 
332  string const loop_sizes_str( tag->getOption< string >( "loop_sizes" ) );
333  vector1< string > const loop_sizes_split( utility::string_split( loop_sizes_str, ',' ) );
334  foreach( string const loop_size, loop_sizes_split )
335  add_loop_size( (Size)std::atoi(loop_size.c_str()) ) ;
336 
337  // path to DB -- if not specified then command-line flag is used
338  library_ = new LoopHashLibrary( loop_sizes() , 1 , 0 );
339  if ( tag->hasOption( "db_path" )) {
340  std::string db_path = tag->getOption< string >( "db_path" );
341  library_->set_db_path( db_path );
342  }
343  library_->load_mergeddb();
344  library_->mem_foot_print();
345 
346  // FILTERING STEP 1 --- filter with chainbreak
347  nprefilter_ = tag->getOption< Size >( "nprefilter", 0 );
348  if ( tag->hasOption( "prefilter_scorefxn" )) {
349  string const prefilter_scorefxn_name( tag->getOption< string >( "prefilter_scorefxn" ) );
350  prefilter_scorefxn_ = ScoreFunctionOP( data.get< ScoreFunction * >( "scorefxns", prefilter_scorefxn_name ) );
351  }
352 
353  // FILTERING STEP 2 --- filter after idealization (+symmetrization)
354  // number of structures to accept
355  // 0 = accept everything passing the filter
356  ncentroid_ = tag->getOption< Size >( "ncentroid", 0 );
358 
359  // centroid filter
360  string const centroid_filter_name( tag->getOption< string >( "centroid_filter", "true_filter" ) );
361  Filters_map::const_iterator find_cenfilter( filters.find( centroid_filter_name ) );
362  if( find_cenfilter == filters.end() )
363  utility_exit_with_message( "Filter " + centroid_filter_name + " not found in LoopHashMoverWrapper" );
364  cenfilter( find_cenfilter->second );
365  ranking_cenfilter( protocols::rosetta_scripts::parse_filter( tag->getOption< std::string >( "ranking_cenfilter", centroid_filter_name ), filters ) );
366 
367  // batch relax mover
368  if (tag->hasOption( "relax_mover" )) {
369  string const relax_mover_name( tag->getOption< string >( "relax_mover" ) );
370  Movers_map::const_iterator find_mover( movers.find( relax_mover_name ) );
371  bool const mover_found( find_mover != movers.end() );
372  if( mover_found )
373  relax_mover( dynamic_cast< protocols::relax::FastRelax* > (find_mover->second()) );
374  else
375  utility_exit_with_message( "Mover " + relax_mover_name + " not found in LoopHashMoverWrapper" );
376 
377  // nonideal
378  fastrelax_->set_force_nonideal( !ideal_ );
379 
380  // batch size
381  batch_size_ = tag->getOption< Size >( "batch_size", 32 );
382 
383  // maximum number of structures to accept
384  // may be less depending on # of centroid structures actually generated, ncentroid_, and batch_size_;
385  nfullatom_ = tag->getOption< Size >( "nfullatom", ncentroid_ );
386 
387  // fullatom filter <<<< used to select best 'nfullatom' from all decoys
388  string const fullatom_filter_name( tag->getOption< string >( "fullatom_filter", "true_filter" ) );
389  Filters_map::const_iterator find_fafilter( filters.find( fullatom_filter_name ) );
390  if( find_fafilter == filters.end() )
391  utility_exit_with_message( "Filter " + fullatom_filter_name + " not found in LoopHashMoverWrapper" );
392  fafilter( find_fafilter->second );
393  ranking_fafilter( protocols::rosetta_scripts::parse_filter( tag->getOption< std::string> ( "ranking_fafilter", fullatom_filter_name ), filters ) );
394  } else {
395  if (tag->hasOption( "batch_size" ) ) TR << "Ignoring option batch_size" << std::endl;
396  if (tag->hasOption( "nfullatom" ) ) TR << "Ignoring option nfullatom" << std::endl;
397  if (tag->hasOption( "fullatom_filter" ) ) TR << "Ignoring option fullatom_filter" << std::endl;
398  }
399 }
400 
401 Real
403  return min_bbrms_;
404 }
405 
406 Real
408  return max_bbrms_;
409 }
410 
411 Real
413  return min_rms_;
414 }
415 
416 Real
418  return max_rms_;
419 }
420 
421 void
424 }
425 
426 void
429 }
430 
431 void
433  min_rms_ = min_rms;
434 }
435 
436 void
438  max_rms_ = max_rms;
439 }
440 
441 Size
443  return max_nstruct_;
444 }
445 
446 void
449 }
450 
451 void
454 }
455 
456 void
459 }
460 
461 void
464 }
465 
466 void
469 }
470 
473  return loop_sizes_;
474 }
475 
476 void
478  loop_sizes_.push_back( loop_size );
479 }
480 
481 } //loophash
482 } //protocols
483