Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HedgeArchive.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 IterativeAbrelax
10 /// @brief iterative protocol starting with abinitio and getting progressively more concerned with full-atom relaxed structures
11 /// @detailed
12 /// @author Oliver Lange
13 
14 // Unit Headers
16 
18 
19 // Package Headers
22 
23 // Utility Headers
24 #include <basic/Tracer.hh>
25 #include <basic/MemTracer.hh>
26 #include <numeric/random/random.hh>
27 #include <numeric/random/random_permutation.hh>
28 
29 #include <ObjexxFCL/string.functions.hh>
30 #include <utility/file/file_sys_util.hh>
31 
32 
33 static basic::Tracer tr("protocols.iterative.HedgeArchive");
34 using basic::mem_tr;
35 
36 static numeric::random::RandomGenerator RG(12983472); // <- Magic number, do not change
37 
38 using core::Real;
39 
40 
41 namespace protocols {
42 namespace abinitio {
43 
44 
45 
47  : score_cut_per_batch_( 0.1 ),
48  add_fuzzy_( 0.1 ) //0 for strictly score based, 1 for totally random
49 {
50  set_name( name );
51  set_max_nstruct( 1e6 ); //more than this and we have clearly too much filesystem load.
52  set_evaluate_local( false ); //never re-evaluate decoys in this Archive
53 }
54 
56  tr.Debug << "batch " << batch_id << " has finished... incorporating into hedge archive " << std::endl;
57  SilentStructs& sorted_decoys( incoming_structures_[ batch_id ] );
58  sorted_decoys.sort();
59  Size ind_max( static_cast< Size > ( sorted_decoys.size()*score_cut_per_batch_ ) );
60  for ( SilentStructs::const_iterator sit = sorted_decoys.begin(); sit != sorted_decoys.end() && ind_max>0; ++sit ) {
61  if ( RG.uniform() < (1-add_fuzzy_) ) {
62  set_max_nstruct( 1e6 );
63  tr.Debug << "add decoy from batch " << batch_id << std::endl;
64  add_structure_at_position( decoys().end(), sit->second );
65  --ind_max;
66  }
67  }
68  incoming_structures_.erase( batch_id );
69  remove_decoys( batch_id );
70  old_batches_.insert( batch_id );
71 }
72 
74  core::io::silent::SilentStructOP incoming_decoy = evaluated_decoy->clone(); //clone because that makes it more consistent after restart...
75  if ( old_batches_.find( batch.id() ) != old_batches_.end() ) return false;
76  incoming_structures_[ batch.id() ].push_back( std::make_pair( select_score( incoming_decoy ), incoming_decoy ) );
77  tr.Debug << "incoming from batch: " << batch.id() << " " << incoming_structures_[ batch.id() ].size() << " " << batch.decoys_returned() << " " << batch.has_finished() << std::endl;
78  if ( batch.has_finished() && incoming_structures_[ batch.id() ].size() >= batch.decoys_returned() ) { //90% of structures have arrived
79  incorporate_batch( batch.id() );
80  }
81  return true;
82 }
83 
85  return std::string("pending_")+ObjexxFCL::lead_zero_string_of( batch_id , 4 );
86 }
87 
88 void HedgeArchive::save_decoys( SilentStructs const& decoys, core::Size batch_id ) const {
89  std::string const& dirname( name() );
90  std::string const ffilename ( dirname + "/" + filename( batch_id ) );
91  std::string const backup_filename ( ffilename+".backup" );
92  std::string const tmp_filename ( ffilename+".tmp" );
93  //handle output myself... so it keeps the order of decoys.
94 
96  // utility::io::ozstream output( tmp_filename );
97  // if ( decoys.begin() != decoys.end() ) (*decoys.begin())->print_header( output );
98  for ( SilentStructs::const_iterator it = decoys.begin(); it != decoys.end(); ++it ) {
99  sfd.add_structure( *(it->second) ); //only add OP to sfd
100  }
101  sfd.write_all( tmp_filename );
102 
103  //rename to final
104  if ( utility::file::file_exists( ffilename ) ) {
105  rename( ffilename.c_str(), backup_filename.c_str() );
106  }
107  rename( tmp_filename.c_str(), ffilename.c_str() );
108 }
109 
110 void HedgeArchive::remove_decoys( core::Size batch_id ) const {
111  std::string const& dirname( name() );
112  std::string const ffilename ( dirname + "/" + filename( batch_id ) );
113  std::string const backup_filename ( ffilename+".backup" );
114  std::string const tmp_filename ( ffilename+".tmp" );
115  utility::file::file_delete( ffilename );
116  utility::file::file_delete( backup_filename );
117  utility::file::file_delete( tmp_filename );
118 }
119 
120 
123  for ( core::Size i=1; i<=decoys().size(); ++i ) {
124  indices[i]=i<=batch.nstruct();
125  }
126  numeric::random::random_permutation(indices, RG);
127  numeric::random::random_permutation(indices, RG);
128  core::Size i=1;
129  start_decoys.reserve( batch.nstruct() );
130  for ( Parent::SilentStructs::const_iterator it = decoys().begin(); it != decoys().end(); ++it, ++i ) {
131  if ( indices[i] ) {
132  start_decoys.push_back( *it );
133  }
134  }
135 }
136 
137 void HedgeArchive::save_status( std::ostream& os ) const {
138  Parent::save_status( os );
139  os << "OPEN_BATCHES" << std::endl;
140  for ( BatchStructuresMap::const_iterator it=incoming_structures_.begin(); it!=incoming_structures_.end(); ++it ) {
141  if ( !it->second.size() ) continue;
142  os << it->first << std::endl;
143  save_decoys( it->second, it->first );
144  }
145  os << "END_BATCHES" << std::endl;
146 }
147 
148 void HedgeArchive::restore_status( std::istream& is ) {
150  std::string tag;
151  is >> tag;
152  runtime_assert( tag == "OPEN_BATCHES" );
153  while ( is >> tag ) {
154  if ( tag == "END_BATCHES" ) break;
155  Size batch_id = ObjexxFCL::int_of( tag );
156  std::string const& dirname( name() );
157  std::string const ffilename ( dirname + "/" + filename( batch_id ) );
158  std::string const backup_filename ( ffilename+".backup" );
159  std::string const tmp_filename ( ffilename+".tmp" );
160  if ( utility::file::file_exists( ffilename ) ) {
161  using namespace core::io::silent;
162  SilentFileData sfd;
163  if ( !sfd.read_file( ffilename ) ) throw ( utility::excn::EXCN_BadInput( "problem reading silent file"+ffilename ) );
164  for ( SilentFileData::iterator it=sfd.begin(), eit=sfd.end(); it!=eit; ++it ) {
165  incoming_structures_[ batch_id ].push_back( std::make_pair( select_score( *it ), *it ) );
166  }
167  }
168  }
169 }
170 
171 
172 } //abinitio
173 } //protocols