Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SilentStructStore.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 /// @file protocols/wum/SilentStructStore.cc
11 /// @brief
12 /// @author Mike Tyka
13 
14 // MPI headers
15 #ifdef USEMPI
16 #include <mpi.h> //keep this first
17 #endif
18 
20 //#include <protocols/frag_picker/VallChunk.hh>
21 //#include <protocols/frag_picker/VallProvider.hh>
22 
23 // AUTO-REMOVED #include <boost/cstdint.hpp>
24 // AUTO-REMOVED #include <boost/unordered_map.hpp>
25 // AUTO-REMOVED #include <boost/algorithm/string.hpp>
27 
30 #include <core/pose/util.hh>
31 #include <core/kinematics/Jump.hh>
32 #include <core/kinematics/RT.hh>
33 #include <basic/options/option.hh>
35 // AUTO-REMOVED #include <core/io/pdb/pose_io.hh>
37 #include <core/pose/Pose.hh>
39 // AUTO-REMOVED #include <core/conformation/ResidueFactory.hh>
40 // AUTO-REMOVED #include <core/scoring/constraints/util.hh>
41 // AUTO-REMOVED #include <core/scoring/constraints/CoordinateConstraint.hh>
42 // AUTO-REMOVED #include <core/scoring/Energies.hh>
43 // AUTO-REMOVED #include <core/scoring/ScoreFunctionFactory.hh>
44 // AUTO-REMOVED #include <core/scoring/ScoreFunction.hh>
45 #include <basic/Tracer.hh>
46 // AUTO-REMOVED #include <core/scoring/rms_util.hh>
53 
54 // AUTO-REMOVED #include <protocols/moves/Mover.hh>
55 // AUTO-REMOVED #include <utility/excn/Exceptions.hh>
56 #include <utility/exit.hh>
57 // AUTO-REMOVED #include <utility/fixedsizearray1.hh>
58 #include <utility/pointer/owning_ptr.hh>
59 #include <utility/io/izstream.hh>
60 // AUTO-REMOVED #include <core/kinematics/MoveMap.hh>
61 
62 // AUTO-REMOVED #include <core/optimization/AtomTreeMinimizer.hh>
63 // AUTO-REMOVED #include <core/optimization/MinimizerOptions.hh>
64 
65 // C++ headers
66 //#include <cstdlib>
67 
68 #include <iostream>
69 #include <fstream>
70 #include <string>
71 
72 // option key includes
73 #include <basic/options/keys/in.OptionKeys.gen.hh>
74 #include <basic/options/keys/lh.OptionKeys.gen.hh>
75 // AUTO-REMOVED #include <basic/options/keys/out.OptionKeys.gen.hh>
76 
77 //Auto Headers
80 #include <utility/vector1.hh>
81 #include <numeric/random/random.hh>
82 #include <boost/unordered/unordered_map.hpp>
83 
84 
85 using namespace core;
86 using namespace kinematics;
87 //using namespace protocols::frag_picker;
88 using namespace core::io::silent;
89 using namespace core::pose;
90 using namespace core::scoring;
91 using namespace conformation;
92 using namespace protocols::moves;
93 
94 
95 
96 
97 namespace protocols {
98 namespace wum {
99 
100 /// @details Auto-generated virtual destructor
101 SilentStructStore::~SilentStructStore() {}
102 
103 
104 
106  {
107  public:
108  sort_SilentStructOPs(std::string field = "score" ): field_(field) {}
109 
110  bool operator () (const SilentStructOP& left, const SilentStructOP& right)
111  {
112  runtime_assert( left );
113  runtime_assert( right );
114  return left->get_energy( field_ ) < right->get_energy( field_ );
115  }
116  private:
118  };
119 
120 
121  bool find_SilentStructOPs::operator () (const core::io::silent::SilentStructOP& check)
122  {
123  if ( check->get_energy(field_) == value_ ) return true;
124  return false;
125  }
126 
127 
128 
129  static basic::Tracer TR("SilentStructStore");
130 
131  static numeric::random::RandomGenerator RG(1931333); // <- Magic number, do not change it (and dont try and use it anywhere else)
132 
133  void
134  SilentStructStore::clear()
135  {
136  store_.clear();
137  }
138 
139  void
140  SilentStructStore::add( const core::pose::Pose &pose ){
141  using namespace basic::options;
142  using namespace basic::options::OptionKeys;
143  core::io::silent::SilentStructOP ss = option[ lh::bss]() ?
146  ss->fill_struct( pose );
147  add( ss );
148  }
149 
150 
151  void
152  SilentStructStore::add( SilentStructOP new_struct ){
153  store_.push_back( new_struct );
154  }
155 
156  void
157  SilentStructStore::add( const SilentStruct &new_struct ){
158  SilentStructOP pss = new_struct.clone();
159  store_.push_back( pss );
160  }
161 
162  void
163  SilentStructStore::add( core::io::silent::SilentFileData const& sfd ) {
164  using namespace core::io::silent;
165  using namespace core::chemical;
166  for ( SilentFileData::const_iterator it=sfd.begin(), eit=sfd.end(); it!=eit; ++it ) {
167  add( *it );
168  }
169  }
170 
171  void
172  SilentStructStore::add( SilentStructStore &mergestore ) {
173  for( std::vector < SilentStructOP >::const_iterator it = mergestore.store_.begin();
174  it != mergestore.store_.end();
175  ++it ){
176  runtime_assert( *it );
177  store_.push_back( *it );
178  }
179  }
180 
181 
182  // @brief This uses the pose stream to read in everything from -l, -s and -in:file:silent into this store.
183  void
184  SilentStructStore::read_from_cmd_line( ) {
185  using namespace basic::options;
186  using namespace basic::options::OptionKeys;
187 
189  if ( option[ in::file::fullatom ]() ) {
191  } else {
193  }
194 
196  core::Size count = 0;
197  while( input.has_another_pose() && (count < 400 ) ) {
198  core::pose::Pose pose;
199  input.fill_pose( pose, *rsd_set );
200  add( pose );
201  count ++;
202  }
203  TR.Info << "Read " << count << " structures from command line" << std::endl;
204  }
205 
206 
207  // @brief read from silent file
208  void
209  SilentStructStore::read_from_string( const std::string & input ) {
210  std::istringstream iss(input);
211  read_from_stream( iss );
212  }
213 
214  // @brief read from silent file
215  void
216  SilentStructStore::read_from_stream( std::istream & input ) {
217  SilentFileData sfd;
218  utility::vector1< std::string > tags_wanted; // empty vector signals "read all" according to author of silent io
219  sfd.read_stream( input, tags_wanted, false );
221  // utility::vector1< std::string >::iterator citer = comments.begin(); // Unused variable was causing a warning
222  // Now loop over each structure in that silent file
223  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
224  add( *iter);
225  }
226  }
227 
228  void
229  SilentStructStore::read_from_file( const std::string &filename ){
230  utility::io::izstream data( filename.c_str() );
231  if ( !data.good() ) {
232  utility_exit_with_message(
233  "ERROR: Unable to open silent strcuture store file: '" + filename + "'"
234  );
235  }
236  read_from_stream( data );
237  data.close();
238  }
239 
240 
241  void
242  SilentStructStore::get_pose( core::Size index, core::pose::Pose &pose ) const {
243  runtime_assert( index < store_.size() );
244  SilentStructCOP temp_struct = get_struct( index );
245  temp_struct->fill_pose( pose );
246  }
247 
248  // @brief GEt a random structure
249  SilentStructCOP SilentStructStore::get_struct_random() const{
250  if( store_.size() == 0 ) runtime_assert(false);
251  core::Size choice=core::Size( RG.random_range(0,(store_.size()-1)));
252  runtime_assert( choice < store_.size() );
253  return store_[ choice ];
254  }
255 
256  void SilentStructStore::serialize( std::ostream & out ) const {
257  if( store_.size() == 0 ){
258  TR.Warning << "WARNING: Empty silent struct store serialized." << std::endl;
259  } else {
260  (*store_.begin())->print_header( out );
261  SilentFileData sfd;
262  for( std::vector < SilentStructOP >::const_iterator it = store_.begin();
263  it != store_.end();
264  ++it ){
265  runtime_assert( *it );
266  sfd.write_silent_struct( (*(*it)), out );
267  }
268  }
269  }
270 
271  void SilentStructStore::serialize( std::string & out ) const {
272  std::ostringstream ss;
273  serialize( ss );
274  out = ss.str();
275  }
276 
277 
278  void SilentStructStore::serialize_to_file( const std::string &filename ) const {
279  std::ofstream out( filename.c_str() );
280  if ( !out.good() ) {
281  utility_exit_with_message( "ERROR: Unable to open output file : '" + filename + "'" );
282  }
283  serialize( out );
284  out.close();
285  }
286 
287 
288 
289 
290 
291  void SilentStructStore::print( std::ostream & out ) const {
292  SilentFileData sfd;
293  core::Size count=0;
294  out << "----------------------------------------------" << std::endl;
295  for( std::vector < SilentStructOP >::const_iterator it = store_.begin();
296  it != store_.end();
297  ++it ){
298  out << count << " ";
299  runtime_assert( *it );
300  (*it)->print_scores( out );
301  }
302  out << "----------------------------------------------" << std::endl;
303  }
304 
305 
306 
307  core::Size
308  SilentStructStore::mem_footprint() const {
309  core::Size total = 0;
310  for( std::vector < SilentStructOP >::const_iterator it = store_.begin();
311  it != store_.end();
312  ++it ){
313  total += (*it)->mem_footprint();
314  }
315  return total;
316  }
317 
318 
319  void
320  SilentStructStore::sort_by( std::string field )
321  {
322  if( store_.size() == 0 ) return;
323  sort_SilentStructOPs sort_by_field = field;
324  std::sort( store_.begin(), store_.end(), sort_by_field );
325  }
326 
327 
328  void
329  SilentStructStore::all_add_energy( std::string scorename, core::Real value, core::Real weight )
330  {
331  for( std::vector < SilentStructOP >::iterator it = store_.begin();
332  it != store_.end();
333  ++it ){
334  (*it)->add_energy( scorename, value, weight );
335  }
336  }
337 
338  void
339  SilentStructStore::all_sort_silent_scores()
340  {
341  for( std::vector < SilentStructOP >::iterator it = store_.begin();
342  it != store_.end();
343  ++it ){
344  (*it)->sort_silent_scores( );
345  }
346  }
347 
348 
349 
350 // TOOLS
351 
352 
354 encode_alphanum(unsigned long number, int pad_width=0, char pad_char = '0')
355 {
356  std::string code = "";
357  const static char *codes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
358  const static int codes_len = 52;
359 
360  int count_len = 0;
361  while( number > 0 ){
362  unsigned int digit = number % codes_len;
363  code = codes[digit] + code;
364  count_len++;
365  number /= codes_len;
366  }
367 
368  while( count_len < pad_width ){
369  code = pad_char + code;
370  count_len++;
371  }
372 
373  return code;
374 }
375 
376 
377 
378 // without locking this is not really threadsafe. But how to otherwise proved a *globally*
379 // unique number ? A singleton wont help either, just code overhead.
380 
383  static long unique_count=0;
384  int mpi_rank = 0;
385  int mpi_npes = 0;
386  #ifdef USEMPI
387  MPI_Comm_rank( MPI_COMM_WORLD, ( int* )( &mpi_rank ) );
388  MPI_Comm_size( MPI_COMM_WORLD, ( int* )( &mpi_npes ) );
389  #endif
390  int width = int(floor(log(float(mpi_npes))/log(62.0)) + 1.0); // 62 is the base of the coded number below.
391  unique_count++;
392  return encode_alphanum( unique_count ) + encode_alphanum( mpi_rank, width, '0' );
393 }
394 
395 
396 
397 
398 }
399 }
400