Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EvaluatedArchive.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 // This file is part of the Rosetta software suite and is made available under license.
5 // The Rosetta software is developed by the contributing members of the Rosetta Commons consortium.
6 // (C) 199x-2009 Rosetta Commons participating institutions and developers.
7 // For more information, see http://www.rosettacommons.org/.
8 
9 /// @file protocols/jd2/MPIFileBufJobDistributor.cc
10 /// @brief implementation of MPIFileBufJobDistributor
11 /// @author Oliver Lange olange@u.washington.edu
12 
13 // Unit headers
17 
19 
21 //#include <core/scoring/constraints/ConstraintIO.hh>
22 //#include <core/scoring/constraints/util.hh>
23 #include <core/scoring/Energies.hh>
24 
25 #include <core/pose/Pose.hh>
26 #include <basic/datacache/BasicDataCache.hh>
27 
30 
31 #include <utility/exit.hh>
32 #include <utility/excn/Exceptions.hh>
33 #include <numeric/util.hh>
34 
35 #include <basic/Tracer.hh>
36 #include <basic/prof.hh>
37 
38 //for DebugArchive
39 // AUTO-REMOVED #include <utility/io/ozstream.hh>
40 
41 #include <basic/options/option.hh>
42 #include <basic/options/option_macros.hh>
43 
44 //for setup_default_evaluators
45 #include <basic/options/keys/constraints.OptionKeys.gen.hh>
46 // AUTO-REMOVED #include <basic/options/keys/jumps.OptionKeys.gen.hh>
47 #include <basic/options/keys/in.OptionKeys.gen.hh>
48 
49 //#include <core/scoring/ResidualDipolarCoupling.hh>
50 //#include <core/scoring/constraints/ConstraintSet.hh>
51 #include <utility/vector1.hh>
52 
53 
54 static basic::Tracer tr("protocols.jd2.Archive");
55 
56 //OPT_1GRP_KEY( Real, iterative, chainbreak_evaluator_exponent )
57 //OPT_1GRP_KEY( Boolean, iterative, simulate_bg4_cbtreatment )
58 OPT_1GRP_KEY( Boolean, iterative, evaluate_only_on_slaves )
59 OPT_1GRP_KEY( Real, iterative, penalize_initial_decoys )
60 std::string const SPECIAL_INITIAL_DECOY_PENALTY( "special_initial_decoy_penalty" );
61 
62 bool protocols::jd2::archive::EvaluatedArchive::options_registered_( false );
63 
64 //Mike: when you want to remove these Macros... leave them at least here as comment - since they provide documentation
65 void protocols::jd2::archive::EvaluatedArchive::register_options() {
67  if ( !options_registered_ ) {
68  // NEW_OPT( iterative::chainbreak_evaluator_exponent, "exponent for nrjump_weighted_chainbreaks", 1.5 );
69  // NEW_OPT( iterative::simulate_bg4_cbtreatment, "this gives special cb weights", false );
70  NEW_OPT( iterative::evaluate_only_on_slaves,"do not re-evaluate decoys when they are read into archvie (e.g. on BlueGene)", false );
71  NEW_OPT( iterative::penalize_initial_decoys, "decoys read from input_pool have X extra score", 1000.0 );
72  options_registered_ = true;
73  }
74 }
75 
76 #define OBSOLETE(key) \
77  if ( basic::options::option[ key ].user() ) { \
78  tr.Warning << "WARNING: Option "<< #key<< " is deprecated!" << std::endl; \
79  }
80 
81 namespace protocols {
82 namespace jd2 {
83 namespace archive {
84 // using namespace basic::options;
85 // using namespace basic::options::OptionKeys;
86 using namespace core;
87 using namespace core::io::silent;
88 
90 
91 
93  : scorefxn_( NULL ),
94  scores_are_clean_( true ),
95  b_evaluate_incoming_decoys_( !basic::options::option[ basic::options::OptionKeys::iterative::evaluate_only_on_slaves ]() ) ///yields bottleneck on BG
96 {
97  runtime_assert( options_registered_ );
99  OBSOLETE(basic::options::OptionKeys::iterative::evaluate_only_on_slaves)
100 }
101 
103  : ArchiveBase( ptr ),
104  scorefxn_( NULL ),
105  scores_are_clean_( true ),
106  b_evaluate_incoming_decoys_( !basic::options::option[ basic::options::OptionKeys::iterative::evaluate_only_on_slaves ]() ) ///yields bottleneck on BG
107 {
108  runtime_assert( options_registered_ );
110 }
111 
113  start_eval_time_ = time(NULL);
114  tr.Trace << "start evaluation" << std::endl;
115 }
116 
118  using namespace basic::options;
119  tr.Info << "add structure" << std::endl;
120  core::io::silent::SilentStructOP evaluated_decoy = evaluate_silent_struct( from_batch );
121  bool added( add_evaluated_structure( evaluated_decoy, batch ) );
122  return added;
123 }
124 
125 ///@detail before we can apply score-fxn we have to add extra data: RDC, NOES, (not supported yet: PCS, ... )
126 void EvaluatedArchive::score( pose::Pose & pose ) const {
127  scorefxn()( pose );
128 }
129 
131  //get score
132  Real new_decoy_score ( select_score( evaluated_decoy ) );
133  if ( numeric::isnan( new_decoy_score ) ) return false;
134 
135  //find position in sorted list to insert
136  SilentStructs::iterator iss = decoys().begin();
137  while ( iss != decoys().end() && new_decoy_score >= select_score( *iss ) ) ++iss;
138 
139  //if we are at the end this decoy has a worse score than all others
140  if ( iss != decoys().end() || decoys().size() < nstruct() ) {
141  int now = time(NULL);
142  int eval_time = now-start_eval_time_;
143  evaluated_decoy->add_energy( "eval_time", 1.0*eval_time, 1.0 );
144  tr.Trace << "add evaluated structure " << evaluated_decoy->decoy_tag() << " after " << eval_time << " seconds of evaluation."<< std::endl;
145  add_structure_at_position( iss, evaluated_decoy );
146  return true;
147  }
148  // here if score was not good enough to be added
149  return false;
150 }
151 
152 //overloaded to add some tracer output
154  tr.Info << "structures are scored with the following weights: " << std::endl;
155  WeightMap const& variations( score_variations() );
156  for ( WeightMap::const_iterator it=select_weights_.begin(); it != select_weights_.end(); ++it ) {
157  std::string const& name( it->first );
158  core::Real const& weight( it->second );
159  tr.Info << name << " " << weight << " " << weight / variations.find( name )->second << std::endl;
160  }
161  if ( evaluate_local() ) {
162  tr.Info << "The score will be computed according to " << std::endl;
163  scorefxn().show_pretty( tr.Info );
164  tr.Info << std::endl;
165  }
166  Parent::read_structures( sfd, batch );
167  tr.Info << "finished reading structures for batch: " << batch.batch() << std::endl;
168 
169  basic::prof_show();
170 }
171 
172 
173 
174 
175 ///@details evaluate decoy... if non-local evaluation just copy silent-struct
179 
180  //non-local evalution ? just return input
181  if ( !evaluate_local() ) {
182  tr.Trace << "don't evaluate local: trust energies saved in " << iss->decoy_tag() << std::endl;
183  return iss;
184  }
185 
186  tr.Trace << "evaluate local for " << iss->decoy_tag() << std::endl;
187  core::io::silent::SilentStructOP pss = iss->clone();
188  using namespace core::io::silent;
189  utility::vector1< SilentEnergy > old_energies( iss->energies() );
190  pss->clear_energies(); //need to get rid of e.g., _archive_select_score_ // is actually also done implicitly in evaluate_pose, when it calls 'energies_from_pose'
191  //note: clear_energies should not kill the comments (TAG_IN_FILE, SOURCE_FILE ) should still be present...
192  //need to keep prefa_centroid_score...
193 
194  // runtime_assert( pss->has_energy( "chem_shifts" ) );
195  //make pose for scoring and evaluation purposes
196  PROF_START( basic::ARCHIVE_FILL_POSE );
197  pose::Pose pose;
198  pss->fill_pose( pose ); //has to reread RDC file for each pose!
199  pose.data().clear();
200  pose.energies().clear();
201  PROF_STOP( basic::ARCHIVE_FILL_POSE );
202 
203  //now evaluate the pose
204  pss = evaluate_pose( pss, pose );
205 
206 
207  for ( utility::vector1< SilentEnergy >::const_iterator it = old_energies.begin(); it != old_energies.end(); ++it ) {
208  if ( it->name() != "_archive_select_score_" && !pss->has_energy( it->name() )) {
209  pss->add_energy( it->name(), it->value(), it->weight() );
210  }
211  }
212  //runtime_assert( pss->has_energy( "chem_shifts" ) );
213  int total_time = 0;
214  if ( pss->has_energy( "total_eval_time" ) ) {
215  total_time = pss->get_energy( "total_eval_time" );
216  }
217  total_time += time(NULL)-start_eval_time_;
218  pss->add_energy( "total_eval_time", total_time, 1.0 );
219  return pss;
220 
221 }
222 
223 ///@detail evaluate a pose ... store info in iss (and return the same for convenience )
226  tr.Trace << "evaluate decoy " << iss->decoy_tag() << std::endl;
227 
228  //apply scorefxn
229  PROF_START( basic::ARCHIVE_SCORE_POSE );
230  score( pose );
231  iss->energies_from_pose( pose );
232  PROF_STOP( basic::ARCHIVE_SCORE_POSE );
233 
234  //apply evaluators
235  PROF_START( basic::ARCHIVE_EVALUATORS );
236  for ( EvaluatorMap::const_iterator it=evaluators_.begin(), eit=evaluators_.end();
237  it!=eit; ++it ) {
238  // tr.Trace << "evaluate with " << it->first << std::endl;
239  // basic::DynamicProfileThis here( "Evaluate "+it->first );
240  it->second->apply( pose, iss->decoy_tag(), *iss );
241  }
242  PROF_STOP( basic::ARCHIVE_EVALUATORS );
243 
244  return iss;
245 }
246 
247 ///@detail compute the score of an evaluated decoy
249 
250  ///are cached scores clean ?
252 
253  ///is there a cached score for this decoy ?
254  if ( evaluated_decoy->has_energy( "_archive_select_score_" ) ) {
255  return evaluated_decoy->get_energy( "_archive_select_score_" );
256  }
257 
258  ///no cached score: compute score and cache it
259  Real sum( 0.0 );
260  WeightMap const& variations( score_variations() );
261  ///sum all enegy terms to get 'archive_select_score' ---- iterate over evaluators
262  for ( WeightMap::const_iterator it=select_weights_.begin(); it != select_weights_.end(); ++it ) {
263  std::string const& name( it->first );
264  Real const& weight( it->second );
265  if ( weight == 0.0 ) continue;
266 
267  if ( name == SPECIAL_INITIAL_DECOY_PENALTY ) { ///special evaluator name--- compute score-contribution for this one
268  //crude hack to test things out ...
269  if ( evaluated_decoy->get_comment( "source_file" ).find( "batch_000000/" ) != std::string::npos ) {
270  sum += weight;
271  }
272  } else { //normal score-contribution --- column should be present
273  if ( !evaluated_decoy->has_energy( name ) ) {
274  throw EXCN_Archive( "energy name "+name+" not found in returned decoys -- run with rescoring in archive to avoid this or fix your batches" );
275  } // add weighted column-value to final score
276  if ( weight > 0.01 ) {
277  sum += weight * evaluated_decoy->get_energy( name ) / variations.find( name )->second;
278  }
279  }
280  }
281 
282  //tracer output with full result
283  tr.Trace << "evaluated select_score for " << evaluated_decoy->decoy_tag()
284  << " that was tagged as " << evaluated_decoy->get_comment( "tag_in_file")
285  << " : " << sum << " with " << select_weights_.size() << " evaluators" << std::endl;
286 
287  //add energy to cache
288  evaluated_decoy->add_energy( "_archive_select_score_", sum );
289  return sum;
290 }
291 
293  Parent::save_to_file( suffix );
294 }
295 
296 ///@detail restore archive and sort
298  bool b_have_restored = Parent::restore_from_file();
299  sort();
300  scores_are_clean_ = false;
301  return b_have_restored;
302 }
303 
304 // ---------------------- sort archive -------------------
305 
307 public:
308  SortPredicate( EvaluatedArchive& arc ) : arc_( arc ) {};
309  bool operator() (SilentStructOP const& pss1, SilentStructOP const& pss2 ) {
310  return arc_.select_score( pss1 ) < arc_.select_score( pss2 );
311  }
313 };
314 
316  decoys().sort( SortPredicate( *this ) );
317 }
318 
319 // --------------------------- end sort ------------------------------
320 
321 
322 ///@detail rescore and sort archive
324  tr.Debug << "rescore " << name() << " decoys " << std::endl;
325  tr.Info << "structures are rescored with the following weights: " << std::endl;
326  WeightMap const& variations( score_variations() );
327  for ( WeightMap::const_iterator it=select_weights_.begin(); it != select_weights_.end(); ++it ) {
328  std::string const& name( it->first );
329  core::Real const& weight( it->second );
330  tr.Info << name << " " << weight << " " << weight / variations.find( name )->second << std::endl;
331  }
332  //rescore all decoys
333  for ( SilentStructs::iterator iss = decoys().begin(); iss != decoys().end(); ++iss ) {
334  *iss = evaluate_silent_struct( *iss ); //_archive_select_score_ will be removed here
335  }
336  scores_are_clean_ = true; //do this first, since SortPredicate asks for the select_score
337  sort(); //here the summing is done via select_score() and a new _archive_select_score_ is computed
338 
339  tr.Debug << "...done rescoring and sorting " << std::endl;
340 // if ( tr.Trace.visible() ) {
341 // for ( SilentStructs::const_iterator it = decoys().begin(); it != decoys().end(); ++it ) {
342 // tr.Trace << select_score( *it ) << " " << (*it)->decoy_tag() << std::endl;
343 // }
344 // }
345 }
346 
347 
348 /* =================== maintenance of evaluators and weights ====================== */
349 
351  tr.Info << "added evaluator " << eval->name( 1 ) << " with weight " << weight << " to EvaluatedArchive " << name() << std::endl;
352  for ( Size i=1; i<= eval->size(); i++ ) {
353  std::string const& column( eval->name( i ) );
354  set_weight( column, weight );
355  evaluators_[ column ] = eval;
356  }
357  scores_are_clean_ = false;
358 }
359 
361  tr.Info << "remve evaluator " << name << std::endl;
362  std::string const& column( name );
363 
364  EvaluatorMap::iterator iter = evaluators_.find( column );
365  if ( iter != evaluators_.end() ) evaluators_.erase( iter );
366 
367  WeightMap::iterator iter2 = select_weights_.find( column );
368  if ( iter2 != select_weights_.end() ) {
369  select_weights_.erase( iter2 );
370  if ( iter2->second > 0.001 ) scores_are_clean_ = false;
371  }
372 }
373 
374 void EvaluatedArchive::set_weight( std::string const& column, core::Real weight ) {
375  tr.Info << "set_weight " << column << " to " << weight << " in " << name() << std::endl;
376  // runtime_assert( has_evaluator( column ) ); or part of score!
377  WeightMap::const_iterator iter = select_weights_.find( column );
378  if ( iter != select_weights_.end() && std::abs( iter->second - weight ) < 0.001 ) return;
379  tr.Info << "set_weight " << column << " to " << weight << " in " << name() << std::endl;
380  select_weights_[ column ] = weight;
381  if ( weight > 0.01 ) {
382  dummy_score_variations_[ column ] = 1.0; // to make sure we have a value ( e.g., in case we do no local evaluation )
383  scores_are_clean_ = false; //need to re-determine the score-variations
385  }
386 }
387 
388 
389 void EvaluatedArchive::set_weights( WeightMap const& setting ) {
390  select_weights_.clear();
391  for ( WeightMap::const_iterator it = setting.begin(); it != setting.end(); ++it ) {
392  set_weight( it->first, it->second );
393  }
394 }
395 
398 }
399 
401  evaluators_.clear();
402  for ( EvaluatorMap::const_iterator it = evaluators.begin(); it != evaluators.end(); ++it ) {
403  WeightMap::const_iterator itfind=weights.find( it->first );
404  add_evaluation( it->second, itfind->second );
405  }
406 }
407 
409  // runtime_assert( has_evaluator( column ) ); or part of score!
410  WeightMap::const_iterator iter = select_weights_.find( column );
411  if ( iter != select_weights_.end() ) return iter->second;
412  else return 0.0;
413 }
414 
416  return 1.0;
417 }
418 
420  EvaluatorMap::const_iterator iter = evaluators_.find( column );
421  return iter != evaluators_.end();
422 }
423 
426  scores_are_clean_ = false;
427 }
428 
431  runtime_assert( scorefxn_ );
432  return *scorefxn_;
433 }
434 
437  runtime_assert( scorefxn_ );
438  return scorefxn_;
439 }
440 
442  using namespace basic::options;
443  using namespace basic::options::OptionKeys;
444  //using namespace scoring::constraints;
445 // if ( evaluate_local() && option[ constraints::cst_file ].user() ) {
446 // std::string filename( option[ constraints::cst_file ]()[ 1 ] );
447 // evaluation::PoseEvaluatorOP ev_cst ( new evaluation::ConstraintEvaluator( "cmdline", filename ) );
448 // add_evaluation( ev_cst, option[ constraints::cst_weight ] );
449 // //The ConstraintEvaluator creates two columns: cmdline_cst and cmdline_viol...
450 // set_weight( "cmdline_viol", 0.0 );
451 // }
452  set_weight( "score", 1.0 );
453  if ( option[ OptionKeys::iterative::penalize_initial_decoys ]() > 0.0 ) {
454  set_weight( SPECIAL_INITIAL_DECOY_PENALTY , option[ OptionKeys::iterative::penalize_initial_decoys ]() );
455  }
456 
457  // evaluation::MetaPoseEvaluator cmdline_evals;
458  // evaluation::EvaluatorFactory::get_instance()->add_all_evaluators(cmdline_evals);
459  // for ( evaluation::MetaPoseEvaluator::EvaluatorList::const_iterator it = cmdline_evals.evaluators().begin();
460  // it != cmdline_evals.evaluators().end(); ++it ) {
461  // add_evaluation( *it );
462  // }
463 }
464 
465 /* =================== end maintenance of evaluators and weights ====================== */
466 
467 
468 }//archive
469 }//jd2
470 }//protocols