Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseRole.cc
Go to the documentation of this file.
1 //
2 // (c) Copyright Rosetta Commons Member Institutions.
3 // (c) This file is part of the Rosetta software suite and is made available under license.
4 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
5 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
6 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
7 
8 /// @file protocols/elscripts/BaseRole.cc
9 /// @brief BaseRole, which handles a lot of common functions between all the elscripts roles
10 /// including a lot of lua stuff
11 /// @author Ken Jung
12 
13 
14 #include <fstream>
15 #include <streambuf>
16 
17 #ifdef USELUA
20 
21 #ifdef USEBOOSTSERIALIZE
22 // really, why would you run this without boost serialize...
23 #include <boost/archive/binary_oarchive.hpp>
24 #endif
25 
26 #include <basic/options/option.hh>
27 #include <basic/options/keys/els.OptionKeys.gen.hh>
28 
29 #include <utility/lua/LuaIterator.hh>
30 #include "luabind/class_info.hpp"
31 
32 
33 #include <utility/Factory.hh>
36 #include <protocols/moves/Mover.hh>
40 
42 #include <core/pose/util.hh>
44 
45 // starting includes for initialize_scorefxns()
51 //ending
52 
53 // stupid fucking calculatorfactory
59 // end stupid fucking calculatorfactory
60 
61 #include <basic/Tracer.hh>
62 
63 // elscripts master
64 namespace protocols {
65 namespace elscripts {
66 
67 static basic::Tracer TR("protocols.elscripts.BaseRole");
68 
69 void lregister_BaseRole( lua_State * lstate ) {
70  luabind::module(lstate, "protocols")
71  [
72  luabind::namespace_("elscripts")
73  [
74  luabind::class_<BaseRole>("BaseRole")
75  .def("reparse_def", &BaseRole::reparse_def)
76  ]
77  ];
78 }
79 void BaseRole::lua_init(){
80  using namespace basic::options;
81  //load lua
82  lstate_ = luaL_newstate();
83  luabind::open(lstate_);
84  luabind::bind_class_info(lstate_);
85  luaL_openlibs(lstate_);
86 
87  // start everything into a elscripts namespace
88  std::string action = "DELIM(\n"
89  "els = {}\n"
90  "setmetatable(els, {__index = _G })\n"
91  "do\n"
92  " local _ENV = els\n"
93  " dtasks = {}\n"
94  " dscorefxns = {}\n"
95  " dmovers = {}\n"
96  " dfilters = {}\n"
97  " dinputters = {}\n"
98  " dinputterstream = {}\n"
99  " doutputters = {}\n"
100  " dworkunits = {}\n"
101  "end\n"
102  ")DELIM";
103  int err = luaL_dostring ( lstate_, action.c_str() );
104  if( err == 1) {
105  TR << "Creating els namespace failed. Error is:" << std::endl;
106  TR << lua_tostring(lstate_, -1) << std::endl;
107  std::exit(9);
108  }
109 
110  // register some classes with lua
111  core::io::serialization::lregister_PipeMap(lstate_);
112  core::io::serialization::lregister_Pipe(lstate_);
113  core::pose::lregister_Pose(lstate_);
114  core::pose::lregister_util(lstate_);
115  protocols::moves::lregister_Mover(lstate_);
116  protocols::filters::lregister_Filter(lstate_);
117  core::scoring::lregister_ScoreFunction(lstate_);
118  protocols::moves::lregister_SerializableState(lstate_);
119  protocols::outputter::lregister_Outputter(lstate_);
120  protocols::inputter::lregister_Inputter(lstate_);
121  core::pack::task::operation::lregister_TaskOperation(lstate_);
122 
123  // load elscripts vars
124  if( option[OptionKeys::els::vars].user() ) {
125  err = luaL_dostring ( lstate_, option[OptionKeys::els::vars]().c_str() );
126  if( err == 1) {
127  TR << "Lua interpreting of elscripts::vars failed. Error is:" << std::endl;
128  TR << lua_tostring(lstate_, -1) << std::endl;
129  }
130  }
131 
132  // load lua script
133  if( ! option[OptionKeys::els::script].user() ){
134  TR << "Must specify a elscript to run" << std::endl;
135  std::exit(9);
136  } else {
137  std::string scriptname = option[OptionKeys::els::script]().name();
138  // err = luaL_doString(lstate_, scriptname.c_str());
139  // We need to load the script inside a closure, to share ENV among all the user-defined fxns
140  // I'm sure there's a beautiful way to do this in pure lua with upvalues, but I'm ???
141  // Instead, we'll do it the ugly way and prepend lines to the actual script
142  // Because I can't figure out how to combine closures, I'll predefine loop_every()
143  // With this setup, I also can't, after the fact, fix missing run() or proceed(). Oh well
144  std::ifstream script( scriptname );
145  std::string script_contents((std::istreambuf_iterator<char>(script)),
146  std::istreambuf_iterator<char>());
147  std::string closure_script = "DELIM(\n"
148  "do\n"
149  " local _ENV = _ENV\n"
150  " function els_setenv(t) _ENV = t end\n"
151  " function loop_every()\n"
152  " master:make_wu_until_limit(\"default\", 1)\n"
153  " end\n"
154  " )DELIM"
155  // I guess manually load in modules here
156  + modules::MonteCarlo
157  + script_contents
158  + "(\n"
159  "end\n"
160  ")";
161  err = luaL_dostring ( lstate_, closure_script.c_str() );
162  if( err == 1) {
163  TR << "Loading lua script '" << scriptname << "' failed. Error is:" << std::endl;
164  TR << lua_tostring(lstate_, -1) << std::endl;
165  std::exit(9);
166  }
167 
168  // run definitions() in lua script
169  err = luaL_dostring ( lstate_, "definitions()" );
170  if( err == 1) {
171  TR << "Calling lua function definitions() failed. Error is:" << std::endl;
172  TR << lua_tostring(lstate_, -1) << std::endl;
173  std::exit(9);
174  }
175 
176  }
177 
178 }
179 void BaseRole::instantiate_workunits() {
180  // run workunits() in lua script
181  // this "instantiates" the wus on the lua sid
182  // must be called after everything else has already been instantiated
183  // so that nothing is pointing at nil
184  int err = luaL_dostring ( lstate_, "define_workunits()" );
185  if( err == 1) {
186  TR << "Calling lua function define_workunits() failed. Error is:" << std::endl;
187  TR << lua_tostring(lstate_, -1) << std::endl;
188  std::exit(9);
189  }
190 }
191 
192 void BaseRole::instantiate_scorefxns() {
193  using namespace core::scoring;
194  using namespace core::scoring::symmetry;
195  using namespace utility::lua;
196 
197  TR << "----------Instantiating Score Functions----------" << std::endl;
198  // i might move this somewhere else, it is quite involved
199  // ported straight from mini/src/protocols/jd2/parser/ScoreFunctionLoader.cc
200 
201  luabind::globals(lstate_)["els"]["scorefxns"] = luabind::newtable( lstate_ );
202  scorefxns_.raw( luabind::globals(lstate_)["els"]["scorefxns"] );
203 
204  LuaObject dscorefxns(luabind::globals(lstate_)["els"]["dscorefxns"]);
205  for (LuaIterator i=dscorefxns.begin(), end; i != end; ++i) {
206  TR << "Instantiating score function named " << i.skey() << std::endl;
207  // scorefxn
208  ScoreFunctionOP scorefxn;
209  if( (*i)["weights"] && (*i)["patch"] ) {
211  (*i)["weights"].to<std::string>(),
212  (*i)["patch"].to<std::string>()
213  );
214  } else if ( (*i)["weights"] ) {
215  scorefxn = ScoreFunctionFactory::create_score_function( (*i)["weights"].to<std::string>() );
216  } else {
217  scorefxn = new ScoreFunction;
218  scorefxn->reset();
219  }
220 
221  // reweight
222  if( (*i)["reweight"]) {
223  for (LuaIterator j=(*i)["reweight"].begin(), end; j != end; ++j) {
224  ScoreType const type = score_type_from_name( j.skey() );
225  scorefxn->set_weight( type, (*j).to<core::Real>() );
226  }
227  }
228 
229  // energy method options
230  if( (*i)["emoptions"] ) {
231 
232  core::scoring::methods::EnergyMethodOptions emoptions( scorefxn->energy_method_options() );
233  core::scoring::hbonds::HBondOptionsOP hboptions( emoptions.hbond_options() );
234 
235  for (LuaIterator j=(*i)["emoptions"].begin(), end; j != end; ++j) {
236  if( j.skey() == "softrep_etable" && (*j).to<bool>() ) {
237  emoptions.etable_type( core::scoring::FA_STANDARD_SOFT );
238  } else if( j.skey() == "exclude_protein_protein_hack_elec" ) {
239  emoptions.exclude_protein_protein_hack_elec( (*j).to<bool>() );
240  } else if( j.skey() == "exclude_DNA_DNA" ) {
241  emoptions.exclude_DNA_DNA( (*j).to<bool>() );
242  } else if( j.skey() == "use_hb_env_dep_DNA" ) {
243  hboptions->use_hb_env_dep_DNA( (*j).to<bool>() );
244  } else if( j.skey() == "use_hb_env_dep" ) {
245  hboptions->use_hb_env_dep( (*j).to<bool>() );
246  } else if( j.skey() == "smooth_hb_env_dep" ) {
247  hboptions->smooth_hb_env_dep( (*j).to<bool>() );
248  } else if( j.skey() == "decompose_bb_hb_into_pair_energies" ) {
249  hboptions->decompose_bb_hb_into_pair_energies( (*j).to<bool>() );
250  }
251 
252  scorefxn->set_energy_method_options( emoptions );
253  }
254  }
255 
256  // hotspot hashing
257  // why is this more special than a normal reweight?
258  if( (*i)["hs_hash"] ) {
259  scorefxn->set_weight( backbone_stub_constraint, (*i)["hs_hash"].to<core::Real>() );
260  }
261 
262  //symmetry
263  bool const scorefxn_symm = (*i)["symmetric"] ? (*i)["symmetric"].to<bool>() : 0 ;
264  if (scorefxn_symm) {
265  scorefxn = ScoreFunctionOP( new SymmetricScoreFunction( scorefxn ) );
266  }
267 
268  // owning ptrs suck
269  ScoreFunctionSP tmpsp( scorefxn.get() );
270  scorefxn.relinquish_ownership();
271  luabind::globals(lstate_)["els"]["scorefxns"][ i.skey() ] = tmpsp;
272  }
273  // add default score12 if score12 isn't yet taken
274  if( luabind::type( scorefxns_["score12"].raw() ) == 0 ) {
276  ScoreFunctionSP tmpsp( scorefxn.get() );
277  scorefxn.relinquish_ownership();
278  luabind::globals(lstate_)["els"]["scorefxns"]["score12"] = tmpsp;
279  }
280  TR << "----------Finished Instantiating Score Functions----------" << std::endl;
281 }
282 
283 void BaseRole::instantiate_tasks() {
284  using namespace core::pack::task::operation;
285  using namespace utility::lua;
286 
287  TR << "----------Instantiating TaskOperations----------" << std::endl;
288 
289  luabind::globals(lstate_)["els"]["tasks"] = luabind::newtable( lstate_ );
290  tasks_.raw( luabind::globals(lstate_)["els"]["tasks"] );
291 
292  LuaObject dtasks( luabind::globals(lstate_)["els"]["dtasks"]);
293  //Factory<MMover> * mmoverfactory = Factory<MMover>::get_instance();
294  for (LuaIterator i=dtasks.begin(), end; i != end; ++i) {
295  TR << "Instantiating task operation " << (*i)["class"].to<std::string>() << " named " << i.skey() << std::endl;
296  TaskOperationOP task( TaskOperationFactory::get_instance()->newTaskOperation( (*i)["class"].to<std::string>(), NULL) );
297  //MMoverSP mmover ( mmoverfactory->from_string( (*i)["class"].to<std::string>() ) );
298  task->parse_def( (*i) );
299  TaskOperationSP tmpsp( task.get() );
300  task.relinquish_ownership();
301  tasks_.raw()[ i.skey() ] = tmpsp;
302  }
303  TR << "----------Finished Instantiating TaskOperations----------" << std::endl;
304 }
305 
306 void BaseRole::instantiate_movers() {
307  using namespace utility;
308  using namespace utility::lua;
309  using namespace protocols::moves;
310 
311  TR << "----------Instantiating Movers----------" << std::endl;
312 
313  luabind::globals(lstate_)["els"]["movers"] = luabind::newtable( lstate_ );
314  movers_.raw( luabind::globals(lstate_)["els"]["movers"] );
315 
316  LuaObject dmovers( luabind::globals(lstate_)["els"]["dmovers"]);
317  Factory<Mover> * moverfactory = Factory<Mover>::get_instance();
318  for (LuaIterator i=dmovers.begin(), end; i != end; ++i) {
319  TR << "Instantiating mover " << (*i)["class"].to<std::string>() << " named " << i.skey() << std::endl;
320  if( moverfactory->has_string( (*i)["class"].to<std::string>() ) ) {
321  MoverSP mover = moverfactory->from_string( (*i)["class"].to<std::string>() );
322  mover->parse_def( (*i), scorefxns_, tasks_, mover_cache_ );
323  luabind::globals(lstate_)["els"]["movers"][ i.skey() ] = mover;
324  } else {
325  MoverOP mover( MoverFactory::get_instance()->newMover( (*i)["class"].to<std::string>() ) );
326  mover->parse_def( (*i), scorefxns_, tasks_, mover_cache_ );
327  MoverSP tmpsp( mover.get() );
328  mover.relinquish_ownership();
329  luabind::globals(lstate_)["els"]["movers"][ i.skey() ] = tmpsp;
330  }
331  }
332  TR << "----------Finished Instantiating Movers----------" << std::endl;
333 }
334 
335 void BaseRole::instantiate_filters() {
336  using namespace utility::lua;
337  using namespace protocols::filters;
338 
339  TR << "----------Instantiating Filters----------" << std::endl;
340 
341  luabind::globals(lstate_)["els"]["filters"] = luabind::newtable( lstate_ );
342  filters_.raw( luabind::globals(lstate_)["els"]["filters"] );
343 
344  LuaObject dfilters( luabind::globals(lstate_)["els"]["dfilters"]);
345  for (LuaIterator i=dfilters.begin(), end; i != end; ++i) {
346  TR << "Instantiating filter " << (*i)["class"].to<std::string>() << " named " << i.skey() << std::endl;
347  FilterOP filter( FilterFactory::get_instance()->newFilter( (*i)["class"].to<std::string>() ) );
348  filter->parse_def( (*i), scorefxns_, tasks_);
349  FilterSP tmpsp( filter.get() );
350  filter.relinquish_ownership();
351  luabind::globals(lstate_)["els"]["filters"][ i.skey() ] = tmpsp;
352  }
353  TR << "----------Finished Instantiating Filters----------" << std::endl;
354 }
355 void BaseRole::instantiate_output() {
356  using namespace utility::lua;
357  using namespace protocols::outputter;
358 
359  TR << "----------Instantiating Outputters----------" << std::endl;
360 
361  luabind::globals(lstate_)["els"]["outputters"] = luabind::newtable( lstate_ );
362  outputters_.raw( luabind::globals(lstate_)["els"]["outputters"] );
363 
364  LuaObject doutputters( luabind::globals(lstate_)["els"]["doutputters"] );
365  utility::Factory<Outputter> * outputterfactory = utility::Factory<Outputter>::get_instance();
366  for (LuaIterator i=doutputters.begin(), end; i != end; ++i) {
367  TR << "Instantiating outputter " << (*i)["class"].to<std::string>() << " named " << i.skey() << std::endl;
368  OutputterSP outputter ( outputterfactory->from_string( (*i)["class"].to<std::string>() ) );
369  outputter->lregister( lstate_ );
370  outputter->parse_def( (*i), tasks_ );
371  luabind::globals(lstate_)["els"]["outputters"][ i.skey() ] = outputter;
372  }
373  TR << "----------Finished Instantiating Outputters----------" << std::endl;
374 }
375 void BaseRole::instantiate_inputters() {
376  using namespace utility::lua;
377  using namespace protocols::inputter;
378 
379  TR << "----------Instantiating Inputters----------" << std::endl;
380 
381  luabind::globals(lstate_)["els"]["inputters"] = luabind::newtable( lstate_ );
382  inputters_.raw( luabind::globals(lstate_)["els"]["inputters"] );
383 
384  LuaObject dinputters( luabind::globals(lstate_)["els"]["dinputters"] );
385  utility::Factory<Inputter> * inputterfactory = utility::Factory<Inputter>::get_instance();
386  for (LuaIterator i=dinputters.begin(), end; i != end; ++i) {
387  TR << "Instantiating inputter " << (*i)["class"].to<std::string>() << " named " << i.skey() << std::endl;
388  InputterSP inputter ( inputterfactory->from_string( (*i)["class"].to<std::string>() ) );
389  inputter->lregister( lstate_ );
390  inputter->parse_def( (*i), tasks_, inputters_ );
391  luabind::globals(lstate_)["els"]["inputters"][ i.skey() ] = inputter;
392  }
393 
394  TR << "----------Finished Instantiating Inputters----------" << std::endl;
395 }
396 
397 void BaseRole::instantiate_inputterstream() {
398  using namespace utility::lua;
399  using namespace protocols::inputter;
400  TR << "----------Adding Inputters to InputterStream----------" << std::endl;
401  LuaObject dinputterstream( luabind::globals(lstate_)["els"]["dinputterstream"]);
402  for (LuaIterator i=dinputterstream.begin(), end; i != end; ++i) {
403  inputterstream_->add_inputter( inputters_[ (*i).to<std::string>() ].to<InputterSP>() );
404  }
405  if( inputterstream_->size() == 0 ) {
406  LuaObject dinputters( luabind::globals(lstate_)["els"]["dinputters"] );
407  // inputterstream wasn't defined, so add all inputters to it
408  for (LuaIterator i=dinputters.begin(), end; i != end; ++i) {
409  inputterstream_->add_inputter( inputters_[ i.skey() ].to<InputterSP>() );
410  }
411  }
412  TR << "----------Finished adding Inputters to InputterStream----------" << std::endl;
413 }
414 
415 void BaseRole::update_mover_cache_mem() {
416  if( mover_cache_length_ != mover_cache_->size() ) {
417  // cache_mem_ is out of date
418  // actually could be out of data even if size is same, but w.e, someone else can write a wrapper map class that keeps track of real memory usage while maintaining performance
419 #ifdef USEBOOSTSERIALIZE
420  std::stringstream s;
421  boost::archive::binary_oarchive oa(s);
422  oa << mover_cache_;
423  mover_cache_mem_ = s.str().length();
424  mover_cache_length_ = mover_cache_->size();
425 #else
426  TR << "Memory usage tracked only if compiled against serialize" << std::endl;
427 #endif
428  }
429 }
430 void BaseRole::register_calculators() {
431  // this is so stupid, why is this not like all the other factories and register at init
432  // and furthermore wtf is a calculator and how is it different from a filter
433  // should totally be reimplemented
434  core::Size const chain1( 1 ), chain2( 2 );
435  using namespace core::pose::metrics;
436 
437  if( !CalculatorFactory::Instance().check_calculator_exists( "sasa_interface" ) ){
439  CalculatorFactory::Instance().register_calculator( "sasa_interface", int_sasa_calculator );
440  }
441 
442  if( !CalculatorFactory::Instance().check_calculator_exists( "sasa" ) ){
444  CalculatorFactory::Instance().register_calculator( "sasa", sasa_calculator );
445  }
446  if( !CalculatorFactory::Instance().check_calculator_exists( "ligneigh" ) ){
448  CalculatorFactory::Instance().register_calculator( "ligneigh", lig_neighbor_calc );
449  }
450  if( !CalculatorFactory::Instance().check_calculator_exists( "liginterfE" ) ){
452  CalculatorFactory::Instance().register_calculator( "liginterfE", lig_interf_E_calc );
453  }
454 }
455 
456 void BaseRole::reparse_def( std::string const & type, std::string const & name ) {
457  using namespace utility::lua;
458  if( type == "tasks" ) {
459  using namespace core::pack::task::operation;
460  LuaObject dtasks( luabind::globals(lstate_)["els"]["dtasks"]);
461  for (LuaIterator i=dtasks.begin(), end; i != end; ++i) {
462  if( i.skey() == name ) {
463  tasks_[ i.skey() ].to<TaskOperationSP>()->parse_def( (*i) );
464  break;
465  }
466  }
467  }
468  if( type == "movers" ) {
469  using namespace protocols::moves;
470  LuaObject dmovers( luabind::globals(lstate_)["els"]["dmovers"]);
471  for (LuaIterator i=dmovers.begin(), end; i != end; ++i) {
472  if( i.skey() == name ) {
473  movers_[ i.skey() ].to<MoverSP>()->parse_def( (*i), scorefxns_, tasks_, mover_cache_ );
474  break;
475  }
476  }
477  }
478 }
479 
480 } //elscripts
481 } //protocols
482 #endif