Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ArchiveBase.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
16 
18 // AUTO-REMOVED #include <core/io/silent/SilentStructFactory.hh>
19 
20 // AUTO-REMOVED #include <core/conformation/Conformation.hh>
21 #include <core/pose/Pose.hh>
22 
25 // AUTO-REMOVED #include <core/io/raw_data/DisulfideFile.hh>
26 // AUTO-REMOVED #include <core/scoring/ResidualDipolarCoupling.hh>
27 
28 
29 
30 // AUTO-REMOVED #include <basic/prof.hh>
31 #include <basic/Tracer.hh>
32 #include <basic/MemTracer.hh>
33 
35 
36 // Utility headers
37 #include <utility/exit.hh>
38 #include <utility/excn/Exceptions.hh>
39 #include <utility/file/file_sys_util.hh>
40 
41 #include <utility/io/ozstream.hh>
42 #include <utility/io/izstream.hh>
43 #include <ObjexxFCL/format.hh>
44 
45 #include <basic/options/option.hh>
46 #include <basic/options/keys/abinitio.OptionKeys.gen.hh>
47 #include <basic/options/keys/run.OptionKeys.gen.hh>
48 
49 //C++
50 // AUTO-REMOVED #include <iterator>
51 
52 static basic::Tracer tr("protocols.jd2.Archive");
53 using basic::mem_tr;
54 // Utility headers
55 #include <basic/options/option_macros.hh>
56 #include <basic/prof.hh>
57 
58 #include <utility/vector1.hh>
59 #include <boost/algorithm/string/erase.hpp>
60 
61 //Auto Headers
63 #include <core/kinematics/Jump.hh>
64 
65 
66 //
67 OPT_1GRP_KEY( Integer, iterative, pool_size )
68 
69 bool protocols::jd2::archive::ArchiveBase::options_registered_( false );
70 
71 using namespace basic::options;
72 using namespace basic::options::OptionKeys;
73 //Mike: when you want to remove these Macros... leave them at least here as comment - since they provide documentation
74 void protocols::jd2::archive::ArchiveBase::register_options() {
75  if ( !options_registered_ ) {
76  options_registered_ = true;
77  NEW_OPT( iterative::pool_size, "number of structures maintained in pool", 500 );
78  }
79 }
80 
81 
82 namespace protocols {
83 namespace jd2 {
84 namespace archive {
85 
86 /// @details Auto-generated virtual destructor
88 // using namespace basic::options;
89 // using namespace basic::options::OptionKeys;
90 using namespace core;
91 
92 std::string const ArchiveBase::TAG_IN_FILE( "tag_in_file" );
93 std::string const ArchiveBase::SOURCE_FILE( "source_file" );
94 
96  AbstractArchiveBase( ptr ),
97  max_nstruct_( option[ iterative::pool_size ]() ),
98  accepts_since_last_batch_( 0 ),
99  total_accepts_( 0 ),
100  proposed_since_last_batch_( 0 ),
101  total_proposed_( 0 )
102 {
103 
104  runtime_assert( options_registered_ );
105 
106  //for debugging --- make archive smaller
107  if ( ( option[ run::test_cycles ]() || option[ run::dry_run ]() ) && !option[ run::memory_test_cycles ]() ) {
108  max_nstruct_ = 20;
109  }
110 
112 
115 }
116 
118 
119 ///@brief count the structure for the acceptance statistics
120 /// only count if not from expired batch
121 void ArchiveBase::count_structure( Batch const& batch, bool accepted ) {
122  if ( still_interested( batch ) ) {
123  accepts_since_last_batch_ += accepted ? 1 : 0;
125 
126  //count with floating-average
129  if ( acceptance_history_.front() ) floating_acceptance_ratio_ -= inv_A;
130  acceptance_history_.pop_front();
131  } else { //assume before history we had full-acceptance ( in line with initializing floating_acc_rat = 1 ).
133  }
134  if ( accepted ) floating_acceptance_ratio_ += inv_A;
135  acceptance_history_.push_back( accepted );
136  }
137 }
138 
139 ///@brief count the structure for the acceptance statistics
140 /// only count if not from expired batch
142  if ( accepts_since_last_batch() > n_removed ) accepts_since_last_batch()-=n_removed;
143  else accepts_since_last_batch() = 0;
144 
145  //count with floating-average
147 
148  while ( n_removed-- > 0 && acceptance_history_.size() ) {
149  if ( acceptance_history_.front() ) floating_acceptance_ratio_ -= inv_A;
150  acceptance_history_.pop_front();
151  acceptance_history_.push_back( false );
152  }
153  nstruct_ = decoys().size();
154 }
155 
157  tr.Trace << "ArchiveBase::init_from_decoy_set" << std::endl;
158  //make bogus batch that contains init-file
159  Batch init_batch( 0 );
160  using namespace core::io::silent;
161  using namespace core::chemical;
162 
163  //transform to centroid
164  SilentFileData centroid_sfd;
165  for ( SilentFileData::const_iterator it=sfd.begin(), eit=sfd.end(); it!=eit; ++it ) {
166  std::string tag = it->decoy_tag();
167  pose::Pose pose;
168  (*it)->fill_pose( pose, *ChemicalManager::get_instance()->residue_type_set( chemical::CENTROID ) );
169  SilentStructOP pss=(*it)->clone();
170  pss->fill_struct( pose, tag );
171  centroid_sfd.add_structure( pss );
172  }
173  read_structures( centroid_sfd, init_batch );
174 }
175 
176 
178  using namespace core;
179  using namespace io::silent;
180 
181  tr.Debug << "read structures returned for " << batch.batch() << std::endl;
182  Size ct( batch.decoys_returned() );
183  Size accepted_ct( 0 );
184  for ( SilentFileData::iterator it=sfd.begin(), eit=sfd.end(); it!=eit; ++it ) {
185  std::string tag = it->decoy_tag();
186  it->set_decoy_tag( batch.batch()+"_"+ObjexxFCL::lead_zero_string_of( ++ct, 6 ) );
187 
188  (*it)->add_comment( TAG_IN_FILE, tag );
189  (*it)->add_comment( SOURCE_FILE, batch.silent_out() );
190 
191  bool accept = add_structure( *it, batch );
192  count_structure( batch, accept );
193  accepted_ct += accept ? 1 : 0;
194  }
195 
196  mem_tr << " ArchiveBase::read_structures now: " << decoys().size() << " decoys "<< std::endl;
197 
198  tr.Debug << "...done reading --- " << accepted_ct << " structures were accepted into the archive" << std::endl;
199  tr.Trace << "now there are " << decoys().size() << " decoys in archive " << std::endl;
200 }
201 
203  add_structure_at_position( decoys_.begin(), new_decoy );
204  return true;
205 }
206 
207 void ArchiveBase::add_structure_at_position( SilentStructs::iterator iss, core::io::silent::SilentStructOP new_decoy ) {
208  decoys_.insert( iss, new_decoy );
209  if ( decoys_.size() > max_nstruct_ ) { //take all decoys until full
210  decoys_.pop_back();
211  }
212 }
213 
215  std::string const dirname( name() + suffix );
216  std::string const filename ( dirname + "/decoys.out" );
217  std::string const backup_filename ( dirname + "/decoys.out.backup" );
218  std::string const tmp_filename ( dirname + "/tmp_decoys.out" );
219  using namespace core::io::silent;
220 
221  //don't write empty file
222  if ( !decoys().size() ) return;
223 
224  utility::file::create_directory( dirname );
225 
226  SilentFileData sfd;
227 
228  //handle output myself... so it keeps the order of decoys.
229  utility::io::ozstream output( tmp_filename );
230  if ( decoys_.begin() != decoys_.end() ) (*decoys_.begin())->print_header( output );
231 
232  for ( SilentStructs::const_iterator it = decoys_.begin(); it != decoys_.end(); ++it ) {
233  // sfd.add_structure( *it ); //only add OP to sfd
234  // sfd.write_silent_struct( *it, tmp_filename );
235  sfd.write_silent_struct( **it, output );
236  }
237  // sfd.write_all( tmp_filename );
238 
239  //rename to final
240  //delete old file
241  if ( utility::file::file_exists( filename ) ) {
242  rename( filename.c_str(), backup_filename.c_str() );
243  }
244  rename( tmp_filename.c_str(), filename.c_str() );
245 
246  utility::io::ozstream status( dirname+"/STATUS" );
247  basic::show_time( tr, "save "+name()+" status");
248  save_status( status );
249 }
250 
252  std::string const& dirname( name() );
253  std::string const filename ( dirname + "/decoys.out" );
254  bool b_have_restored( false );
255  using namespace core::io::silent;
256  mem_tr << "ArchiveBase::restore_from_file..." << std::endl;
257  decoys_.clear();
258  if ( utility::file::file_exists( filename ) ) {
259  SilentFileData sfd;
260  if ( !sfd.read_file( filename ) ) throw ( utility::excn::EXCN_BadInput( "problem reading silent file"+filename ) );
261  for ( SilentFileData::iterator it=sfd.begin(), eit=sfd.end(); it!=eit; ++it ) {
262  decoys_.push_back( *it );
263  }
264  }
265  mem_tr << "ArchiveBase::restored " << decoys_.size() << " decoys" << std::endl;
266  utility::io::izstream status( dirname+"/STATUS" );
267  if ( status.good() ) {
268  restore_status( status );
269  b_have_restored = true;
270  } else {
271  tr.Info << name() << ": no archive status found... " << std::endl;
272  }
273 
274  mem_tr << "ArchiveBase::restored_from_file" << std::endl;
275  return b_have_restored;
276 }
277 
278 void ArchiveBase::restore_status( std::istream& is ) {
279  std::string line;
280  getline( is, line );
282  std::string tag;
283  is >> tag;
284  runtime_assert( tag == "acceptance_history:" );
286  is >> tag;
287  while ( tag != "END_AH" ) {
288  runtime_assert( tag == "AH:" );
289  getline( is, line ); //read rest of line
290  std::istringstream line_stream( line );
291  typedef std::istream_iterator< AcceptHistoryQueue::value_type > istream_iterator;
292  std::copy( istream_iterator( line_stream ), istream_iterator(), std::back_inserter( acceptance_history_ ) );
293  is >> tag;
294  }
295  mem_tr << "ArchiveBase::restored_status" << std::endl;
296 }
297 
298 void ArchiveBase::save_status( std::ostream& os ) const {
299  using namespace ObjexxFCL::fmt;
300  os << "total_accepts accepts_during_stage total_proposed proposed_during_stage\n"
302  << RJ( 15, total_proposed_+proposed_since_last_batch_ ) << RJ( 30, proposed_since_last_batch_) << std::endl;
303  os << "acceptance_history: " << floating_acceptance_ratio_ << "\nAH: ";
304  Size const cols( 50 );
305  Size ct( cols );
306  for ( AcceptHistoryQueue::const_iterator it = acceptance_history_.begin(); it != acceptance_history_.end(); ++it, --ct ) {
307  os << *it << " ";
308  if ( ct <= 1 ) { os << "\nAH: "; ct = cols; }
309  }
310  os << "\nEND_AH" << std::endl;
311 }
312 
314  using namespace core::io::silent;
315  ++ct_batches_;
316  tr.Debug << "generate batch number " << ct_batches_ << std::endl;
317  if ( ct_batches_ <= 3 || decoys().size()==0 ) {
318 
319  //always call start_new_batch to generate a new batch
320  Batch& batch( manager().start_new_batch() );
321 
322  utility::io::ozstream flags( batch.flag_file() );
323  if ( ct_batches_ == 1 && make_mistake_ ) flags << "-abinitio::sskip_stages 1 2" << std::endl;
324  flags << "-abinitio::skip_convergence_check" << std::endl;
325  flags.close();
326 
327  //always finish your batch-generation with "finalize_batch"
328  try {
329  manager().finalize_batch( batch );
330  } catch ( EXCN_Archive& excn ) {
331  --ct_batches_;
332  make_mistake_ = false; //don't make this mistake again
333  }
334 
335  } else {
336  SilentStructOPs start_decoys;
337  std::copy( decoys().begin(), decoys().end(), std::back_inserter( start_decoys ) );
338 
339  Batch& batch( manager().start_new_batch( start_decoys ) );
340 
341  utility::io::ozstream broker( batch.broker_file() );
342  broker << "USE_INPUT_POSE\n\nCLAIMER StartStructClaimer\nEND_CLAIMER\n" << std::endl;
343  broker.close();
344 
345  manager().finalize_batch( batch );
346  }
347 }
348 
350  runtime_assert( !pose.is_fullatom() );
351  runtime_assert( cen_score_ );
352  (*cen_score_)( pose );
353 }
354 
356  ArchiveBase( ptr ),
357  ct_batches_( 0 ),
358  cen_score_( NULL ),
359  make_mistake_( true )
360 {
361  using namespace basic::options;
362  using namespace basic::options::OptionKeys;
363  using namespace core::scoring;
364  if ( !cen_score_ ) cen_score_ = ScoreFunctionFactory::create_score_function( "score3", option[ OptionKeys::abinitio::stage4_patch ]() );
365 }
366 
367 void DebugArchive::save_status( std::ostream& out ) const {
369  out << "nr_batches_generated: "<< ct_batches_ << std::endl;
370 }
371 
372 void DebugArchive::restore_status( std::istream& in ) {
374  if ( in.good() ) {
375  std::string tag;
376  in >> tag >> ct_batches_;
377  }
378 }
379 
381  if ( decoys().size() < 100 ) {
382  decoys().push_back( decoy ); //of course this can't remain as simple as this.
383  } else {
384  decoys().front() = decoy;
385  }
386  return true;
387 }
388 
389 }//archive
390 }//jd2
391 }//protocols