Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SilentFileData.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 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file core/io/silent/SilentFileData.cc
12 ///
13 /// @brief silent input file reader for mini.
14 /// @author James Thompson
15 
16 // C++ Headers
17 #include <cstdlib>
18 #include <fstream>
19 #include <iostream>
20 #include <utility>
21 #include <vector>
22 #include <list>
23 #include <string>
24 #include <map>
25 #include <set>
26 #include <sstream>
27 
28 // mini headers
29 #include <ObjexxFCL/char.functions.hh>
30 #include <ObjexxFCL/string.functions.hh>
31 
36 
39 
40 #include <basic/Tracer.hh>
41 #include <utility/io/izstream.hh>
42 #include <utility/io/ozstream.hh>
43 #include <utility/excn/Exceptions.hh>
44 
45 #include <basic/options/option.hh>
46 #include <basic/options/keys/in.OptionKeys.gen.hh>
47 #include <basic/options/keys/out.OptionKeys.gen.hh>
48 #include <basic/prof.hh>
49 
50 #include <numeric/random/random.hh>
51 #include <numeric/random/reservoir_sample.hh>
52 
53 #include <utility/vector1.hh>
54 
55 
56 namespace core {
57 namespace io {
58 namespace silent {
59 
60 static basic::Tracer tr("core.io.silent");
61 static numeric::random::RandomGenerator RG(21458);
62 
65 }
66 
70  read_tags_fast( filename, tags_in_file );
71  return tags_in_file;
72 } // read_tags_fast
73 
76 
77  for ( const_iterator it=structure_map_.begin(),
78  it_end = structure_map_.end(); it != it_end; ++it ){
79  tag_list.push_back( it->decoy_tag() );
80  }
81  return tag_list;
82 } // tags
83 
84 
86  std::string const & filename,
87  utility::vector1< std::string > & tags_in_file
88 ) const {
89 
90  utility::io::izstream data( filename.c_str() );
91  if ( !data.good() ) {
92  utility_exit_with_message(
93  "ERROR: Unable to open silent_input file: '" + filename + "'"
94  );
95  }
96 
97  std::string line,tag;
98  getline( data, line ); // sequence line
99  getline( data, line ); // score line
100 
101  while( getline(data,line) ) {
102  if ( line.substr(0,7) == "SCORE: " && line.substr(8,20).find( "score" ) == std::string::npos ) {
103  std::istringstream l( line );
104 
105  std::string current_word;
106  while ( !l.fail() ) {
107  l >> current_word;
108  }
109  tags_in_file.push_back( current_word );
110  }
111  } // while( getline(data,line) )
112  return true;
113 }
114 
115 bool
117  std::string const & expression,
118  std::string const & mode,
119  utility::vector1< std::string >& tags_in_file//,
120  // utility::vector1< SilentStructOP >& decoys_in_file,
121  // bool ignore_decoys
122 ) const {
123 
124  //open file
125  utility::io::izstream data( filename().c_str() );
126  if ( !data.good() ) {
127 // utility_exit_with_message(
128 // "ERROR: Unable to open silent_input file: '" + filename() + "'"
129 // );
130  tr.Info << "file: " << filename() << " not found" << std::endl;
131  return false;
132  }
133 
134  //figure out search mode
135  bool return_first=false;
136  bool return_last=false;
137  if ( mode == "first" ) {
138  return_first=true;
139  } else if ( mode == "last" ) {
140  return_last=true;
141  } else if ( mode != "all" ) {
142  utility_exit_with_message( "illegal mode selection in SilentFileData::matched_tag(). available modes are first, last, and all " );
143  }
144 
145  //search the tags
146  std::string line;
147  std::string current_tag;
148  std::string final_tag;
149  //bool ignore_this_decoy = true;
151  while( getline(data,line) ) {
152  //find a line with SCORE: but without "score"
153  if ( line.substr(0,7) == "SCORE: " && line.substr(8,20).find( "score" ) == std::string::npos ) {
154  tr.Info << "reading the lines in file:\n" << line << std::endl;
155  //find last word in line -> current_tag
156  std::istringstream l( line );
157  while ( !l.fail() ) {
158  l >> current_tag;
159  }
160  tr.Info << "current_tag:" << current_tag << std::endl;
161  //does it match ?
162  if ( current_tag.find(expression)!=std::string::npos ) {
163  // ignore_this_decoy = ignore_decoys;
164  if ( !return_last ) {
165  tags_in_file.push_back( current_tag );
166  } else final_tag = current_tag;
167  if ( return_first ) return true; //if we want decoys this will be wrong
168  }
169  } // SCORE: header of decoy
170  // if ( !ignore_this_decoy ) {
171  // utility_exit_with_message( "SilentFileData::matched_tag() function stubbed out -- cannot return decoys yet" );
172  // }
173  } // while( getline(data,line) )
174 // if ( return_last ) {
175 // if ( current_tag.find(expression)!=std::string::npos ) {
176 // tags_in_file.push_back( current_tag );
177 // return true;
178 // }
179 // }
180  if ( return_last ) {
181  if ( final_tag.find( expression ) != std::string::npos ) {
182  tags_in_file.push_back( final_tag );
183  return true;
184  }
185  }
186  return tags_in_file.size();
187 }
188 
189 
190 
191 //////////////////////////////////////////////////////
193  SilentStructOP & new_struct
194 ) {
195 
196  std::string old_tag = new_struct->decoy_tag();
197  std::string new_tag = old_tag;
198 
199  using ObjexxFCL::string_of;
200 
201  int n_tries = 0;
202  while ( has_tag( new_tag ) ) {
203  // assign a new tag. Stupid algorithm for the moment, there's nothing to
204  // keep the length of tags from exploding into long strings. If these are
205  // getting to be too long, use the renumber_all_decoys() method to assign
206  // more sensible tags.
207  ++n_tries;
208  new_tag = old_tag + "_" + string_of( n_tries );
209  }
210 
211  if ( n_tries > 0 ) {
212  tr.Warning << "renamed tag " << old_tag << " to " << new_tag;
213  tr.Warning << " (SilentStruct with " << old_tag;
214  tr.Warning << " already exists!)" << std::endl;
215  }
216  new_struct->decoy_tag( new_tag );
217 
218  add_structure( new_struct );
219 }
220 
221 //////////////////////////////////////////////////////
223  SilentStructOP const & new_struct
224 ) {
225 
226  std::string const & new_tag( new_struct->decoy_tag() );
227 
228  if ( !has_tag( new_tag ) ) {
229  structure_map_[ new_tag ] = new_struct;
230  tr.Debug << "added structure with tag " << new_tag << std::endl;
231  tr.Debug << "now have " << structure_map_.size() << " structures."
232  << std::endl;
233  tr.flush();
234  } else {
235  SilentStructOP new_struct_clone = new_struct->clone();
236  add_structure_replace_tag_if_necessary( new_struct_clone );
237  }
238 
239 } // add_structure
240 
242  SilentStruct const & new_struct
243 ) {
244  add_structure( new_struct.clone() );
245 } // add_structure
246 
248  utility::vector1< SilentStructOP > silent_structs;
249  silent_structs.reserve( structure_map_.size() );
250 
251  Structure_Map::iterator map_iter;
252  for ( map_iter = structure_map_.begin(); map_iter != structure_map_.end(); ++map_iter ) {
253  silent_structs.push_back( map_iter->second );
254  }
256 
257  using ObjexxFCL::string_of;
258  int count = 1;
260  for ( ss_iter = silent_structs.begin(); ss_iter != silent_structs.end(); ++ss_iter ) {
261  std::string new_tag = (*ss_iter)->decoy_tag().substr(0,2) + string_of( count );
262  (*ss_iter)->decoy_tag( new_tag );
263  structure_map_[ new_tag ] = *ss_iter;
264  ++count;
265  }
266 } // renumber_all_decoys
267 
269  SilentStruct & s,
270  std::string const & filename,
271  bool bWriteScoreOnly
272 ) const {
273  bool success = false;
274 
275  using namespace basic::options;
276  using namespace basic::options::OptionKeys;
277 
278  utility::io::ozstream output;
279 
280  std::stringstream header;
281  if ( bWriteScoreOnly ) s.print_score_header( header );
282  else s.print_header( header ); //this is SEQUENCE line + print_score_header
283 
284 #ifndef USEMPI
285  //this seems to segfault in mpi mode...
286  //moreover it is a rather specialized feature which I'd rather see in a more highlevel code OL 6/2/09
287  // which is also not consistently implemented, since it is missing in the write_all method.
288  if ( store_argv_in_file() ) {
289  std::string cmd_executed = option.get_argv();
290  write_comment( header, cmd_executed );
291  }
292 #endif
293 
294  // std::string header_string( header.str() );
295  // std::cout << header_string << std::endl;
296 
297  //atomic version of opening procedure... writes header only if file is new -- appends if file already exists.
298  output.open_append_if_existed( filename, header );
299 
300  // to prevent jobs from proceeding blithely when a directory doesn't exist.
301  if ( !output.good() ){
302  utility_exit_with_message( "Could not make "+filename );
303  }
304 
305  write_silent_struct( s, output, bWriteScoreOnly );
306 
307  output.close();
308 
309  success = true;
310  return success;
311 }
312 
314  SilentStruct & s,
315  std::ostream & out,
316  bool bWriteScoreOnly) const {
317  s.print_header(out);
318  return write_silent_struct(s,out,bWriteScoreOnly);
319 }
320 
322  SilentStruct & s,
323  std::ostream & out,
324  bool bWriteScoreOnly
325 ) const {
326  bool success = false;
327 
328  EnergyNamesOP enames;
329  // set minimal set of EnergyNames if we know about them ...
331  enames = EnergyNamesOP(
332  static_cast< EnergyNames * > ( get_shared_silent_data( energynames )() )
333  );
334  } else {
335  enames = new EnergyNames();
336  enames->energy_names( s.energy_names().energy_names() );
338  }
339 
340  if ( strict_column_mode() ) {
341  s.set_valid_energies( enames->energy_names() );
342  } else {
343  // check to make sure the scores in this silent-struct are the same as the
344  // last one that I printed. If not, re-print the header! Also, if the
345  // user says to print a SCORE header for each decoy, print that header.
346  using namespace basic::options;
347  using namespace basic::options::OptionKeys;
348  if ( s.energy_names().energy_names() != enames->energy_names() ||
349  option[ out::file::silent_print_all_score_headers ]()
350  ) {
351  s.print_header( out );
352  enames = new EnergyNames();
353  enames->energy_names( s.energy_names().energy_names() );
355  }
356  }
357 
358  s.print_scores( out );
359  if ( !bWriteScoreOnly ) {
360  s.print_conformation( out );
361  }
362 
363  // the following code was written to satisfy the American No Child
364  // Left Behind act, so that the SilentFileData::write_silent_struct
365  // method will always be successful.
366  success = true;
367  return success;
368 }
369 
371  std::ostream & out,
372  std::string const & line
373 ) const {
374  out << "# " << line << '\n';
375 }
376 
377 int SilentFileData::nres() const {
378  return static_cast< int > ( begin()->nres() );
379 }
380 
382  runtime_assert( has_tag( tag ) );
383  return structure_map_[ tag ];
384 }
385 
387  std::string const & filename, bool bWriteScoreOnly
388 ) const {
389  if ( begin() == end() ) return;
390 
391  utility::io::ozstream output;
392 
393  if ( begin() != end() ) {
394  std::stringstream header;
395  begin()->print_header( header );
396  output.open_append_if_existed( filename, header );
397  }
398 
400  it_end = end(); iter != it_end; ++iter ) {
401  write_silent_struct( **iter, output, bWriteScoreOnly );
402  // iter->print_scores ( output );
403  // if ( !bWriteScoreOnly ) {
404  // iter->print_conformation( output );
405  // }
406  } // for ( iter )
407 } // write_all
408 
409 
410 bool
412  std::string const & filename
413 ) {
414 
415  using namespace basic::options;
416  using namespace basic::options::OptionKeys;
417 
418  bool success = _read_file(
419  filename,
420  !option[ OptionKeys::in::file::silent_read_through_errors ]() /*throw_exception_on_bad_structs*/
421  );
422  if ( !success && !option[ OptionKeys::in::file::silent_read_through_errors ]() ) {
423  throw utility::excn::EXCN_BadInput("no success reading silent file "+filename);
424  }
425  return success;
426 } // read_file
427 
428 
429 bool
431  std::string const & filename,
432  bool throw_exception_on_bad_structs /*default false*/
433 ) {
434 
435  using std::string;
436  using utility::vector1;
437  using namespace basic::options;
438  using namespace basic::options::OptionKeys;
439 
440  //empty tag-vector to signal read ALL
442 
443  utility::vector1< std::string > all_tags = read_tags_fast( filename );
444  if ( option[ in::file::silent_select_random ].user() ) {
445  core::Size const n_wanted( option[ in::file::silent_select_random ]() );
446  runtime_assert( n_wanted > 0.0 );
447  runtime_assert( n_wanted <= all_tags.size() );
448 
449  tags_wanted = numeric::random::reservoir_sample< string >(
450  all_tags, n_wanted, RG
451  );
452  tr.Debug << "selected " << tags_wanted.size() << " tags from " <<
453  all_tags.size() << std::endl;
454  }
455 
456  if ( option[ in::file::silent_select_range_start ]() >= 0 ) {
457 
458  // get start index and limit to size of silent file (all_tags.size() ) by wrapping around (rather then by throwing an error)
459  int range_start = ( option[ in::file::silent_select_range_start ]() *
460  option[ in::file::silent_select_range_mul ] )
461  % all_tags.size();
462  int range_end = range_start + option[ in::file::silent_select_range_len ]();
463 
464  // setting in::file::silent_select_range_len to -1 means run to the end.
465  if( option[ in::file::silent_select_range_len ]() < 0 ) range_end = all_tags.size();
466 
467  // do a range check just in case.
468  range_end = std::min( (int)range_end, (int)all_tags.size() );
469 
470  tr << "Reading range: " << range_start << " <= i < " << range_end << std::endl;
471  for( core::Size position = range_start; position < range_end; position++){
472  tags_wanted.push_back( all_tags[position+1] ); // the +1 converts to 1 based counting ov the utility::vector1
473  }
474 
475  }
476 
477  if( all_tags.size() == 0 ) return true;
478 
479  bool success = _read_file( filename, tags_wanted, throw_exception_on_bad_structs );
480  return success;
481 } // read_file
482 
483 
484 bool
486  std::string const & filename,
488 ) {
489 using namespace basic::options;
490  using namespace basic::options::OptionKeys;
491 
492  bool success = _read_file(
493  filename,
494  tags,
495  !option[ OptionKeys::in::file::silent_read_through_errors ]() /*throw_exception_on_bad_structs*/
496  );
497  if ( !success && !option[ OptionKeys::in::file::silent_read_through_errors ]() ) {
498  throw utility::excn::EXCN_BadInput("no success reading silent file "+filename);
499  }
500  return success;
501 }
502 
503 
504 bool
506  std::string const & filename,
508  bool throw_exception_on_bad_structs /*default false*/
509 ) {
510  bool success = false; // default value for the early return statements
511  utility::io::izstream data( filename.c_str() );
512  if ( !data.good() ) {
513  if ( throw_exception_on_bad_structs ) {
514  throw utility::excn::EXCN_FileNotFound( filename );
515  } else {
516  utility_exit_with_message(
517  "ERROR:: Unable to open silent_input file: '" +
518  filename + "'"
519  );
520  }
521  }
522  success = read_stream(data,tags,throw_exception_on_bad_structs,filename);
523  return success;
524 }
525 
526 bool
528  std::istream & data,
530  bool throw_exception_on_bad_structs, /*default false*/
531  std::string filename /** for error reporting **/
532 ){
533 
534  // put the tags on a set to avoid repetition and fast search
535  std::set<std::string> tagset(tags.begin(), tags.end());
536  std::string sequence_line, score_line, line;
537  bool success = false;
538  // read SEQUENCE line:
539  std::getline( data, sequence_line );
540  if ( sequence_line.substr(0,9) != "SEQUENCE:" ) {
541  tr.Error << "bad format in first line of silent file " << filename << " (function read_stream):"
542  << std::endl;
543  tr.Error << sequence_line << std::endl;
544  return success;
545  }
546 
547  check_if_rna_from_sequence_line( sequence_line );
548 
549  // read header SCORE line
550  getline( data, score_line );
551  if ( score_line.substr(0,7) != "SCORE: " ) {
552  tr.Error << "bad format in second line of silent file " << filename << " (function read_stream):" << std::endl;
553  tr.Error << score_line << std::endl;
554  return success;
555  }
556 
557  // read potential REMARK line
558  getline( data, line );
559 
560  //////////////////////////////
561  // Checking for remarks...
562  // NOTE THAT THIS IS COMPLETELY TERRIBLE -- THESE TAGS SHOULD BE UNIFIED
563  // WITH THE PRINT_HEADER() REMARKS THAT ARE IN THE INDIVIDUAL SILENT STRUCTS, ETC.
564  // Probably should all be handled by the SilentStructFactory, but I don't have
565  // time to refactor this now -- rhiju, Dec. 2009.
566 
567  // fixed a bug when reading silent-files with lines like this:
568  // SCORE: score template description
569  // SCORE: -2.00 1ERNA.pdb S_000000001
570 
571 
573  SilentStructOP tmp_struct = create_SilentStructOP();
574 
575  utility::vector1< std::string > mylines; // used for initialization of particular Silent-Structs
576  mylines.push_back( sequence_line );
577  mylines.push_back( score_line );
578 
579  if ( tagset.size() ) tr.Info << "Reading " << tagset.size()
580  << " structures from " << filename
581  << std::endl;
582  else tr.Info << "Reading all structures from " << filename << std::endl;
583  bool all_tags = tagset.size() == 0; //if no tags selected we read all decoys
584 
585  // start looping over the structures
586  bool line_ok( true );
587  while ( line_ok ) {
588  if ( line.substr(0,1) == "#" ) {
589  tr.Info << "Read a comment line from " << filename << std::endl;
590  tr.Info << line << std::endl;
591  comment_lines_.push_back( line );
592  line_ok = getline(data,line);
593  continue;
594  }
595  //is the next decoy coming up ? then stop collecting lines and initialize the silent-struct from the read lines
596  if ( line.substr(0,10) == "SEQUENCE: " && mylines.size() == 2 && mylines[ 1 ].substr(0,10) == "SEQUENCE: " ) {
597  //we have just read the two SEQUENCE: and SCORE: header lines and a new header starts... ignore this
598  mylines.clear();
599  }
600  if ( ( line.substr(0,7) == "SCORE: " || line.substr(0,10) == "SEQUENCE: " ) && mylines.size() > 3 ) {
601  bool init_good = tmp_struct->init_from_lines( mylines, *this );
602  if ( !init_good && throw_exception_on_bad_structs ) {
603  throw utility::excn::EXCN_BadInput(
604  "failure to read decoy "+tmp_struct->decoy_tag()+
605  " from silent-file " + filename
606  );
607  }
608 
609  if ( init_good ) {
610  //tr.Debug << "candidate structure " << tmp_struct->decoy_tag()
611  // << std::endl;
612  PROF_START( basic::SILENT_READ_TAG_TEST );
613 
614  // Look for the tag in the tagset, if it is present set
615  // add the structure and remove the tag from the tagset.
616  // if afer removal of the tag tagset becames empty break
617  // because that means that all the structures that we want
618  // have been read.
619  bool good_tag = false;
620  if ( !all_tags ) {
621  std::set<std::string>::iterator tag_it = tagset.find(tmp_struct->decoy_tag());
622  if ( tag_it != tagset.end() ) {
623  good_tag = true;
624  tagset.erase(tag_it); //expensive restructering of set -- how about saving a bunch of bools to do this book-keeping.
625  }
626  }
627  bool add_struct( init_good && ( all_tags || good_tag ));
628  PROF_STOP( basic::SILENT_READ_TAG_TEST );
629 
630  if (record_source_) tmp_struct->add_comment("SOURCE",filename);
631 
632  if ( add_struct ) {
633  add_structure( tmp_struct );
634  // check if there are any tags left in the tagset, if not break
635  // since there is no need to read the rest of the file.
636  }
637 
638  tmp_struct = create_SilentStructOP();
639  }
640 
641  mylines.clear();
642  mylines.reserve( tmp_struct->sequence().size() + 10 ); //+10 since there are at least the SCORE lines extra, maybe some REMARK lines
643  }
644 
645  if ( read_silent_struct_type_from_remark( line ) ) tmp_struct = create_SilentStructOP();
646 
647  mylines.push_back( line );
648 
649  line_ok = getline(data,line);
650  /// in no case interrupt loop here, because then mylines will have incomplete silent-struct causing
651  /// seqfault in the "init_from_lines" for last decoy
652  /* CAUSES SEGFAULT OL 12/10/11
653  if(!all_tags && tagset.empty())
654  break; */
655  } // while( getline(data,line) )
656 
657  // don't forget to initialize last structure!
658  bool init_good = tmp_struct->init_from_lines( mylines, *this );
659  if ( !init_good && throw_exception_on_bad_structs ) throw utility::excn::EXCN_BadInput( "failure to read decoy "+tmp_struct->decoy_tag()+" from silent-file " +filename);
660 
661  bool good_tag = false;
662  std::set<std::string>::iterator tag_it = tagset.find(tmp_struct->decoy_tag());
663  if ( tag_it != tagset.end() ) {
664  good_tag = true;
665  }
666 
667  bool add_struct( init_good && ( all_tags || good_tag ));
668 
669  if ( add_struct ) {
670  add_structure( tmp_struct );
671  }
672 
673  tr.Info << "Finished reading " << structure_map_.size() << " structures from "
674  << filename << std::endl;
675  tr.flush();
676 
677  success = true;
678 
679  return success;
680 }
681 
682 
683 ///@detail The first remarks line in a silent file block
684 ///described the type of silent file that is comming. For example,
685 ///
686 bool
688  std::string const& line,
689  bool header
690 ) {
691 
692  using namespace basic::options;
693  using namespace basic::options::OptionKeys;
694 
695  bool changed( false );
696 
697  if ( line.substr(0,6) != "SCORE:" ) {
698  if (( line.find( "BINARY_SILENTFILE" ) != std::string::npos ) || ( line.find ("BINARY SILENTFILE" ) != std::string::npos )) {
699  if ( line.find( "RNA" ) != std::string::npos || option[ in::file::residue_type_set ]() == "rna" ) {
700  silent_struct_type_ = "binary_rna";
701  } else {
702  silent_struct_type_ = "binary";
703  }
704  changed = true;
705  } else if ( header && ( line.find( "RNA" ) != std::string::npos || option[ in::file::residue_type_set ]() == "rna") &&
706  silent_struct_type_ != "binary_rna" && silent_struct_type_ != "rna" ) {
707  silent_struct_type_ = "rna";
708  changed = true;
709  } else if (( line.find( "PROTEIN_SILENTFILE" ) != std::string::npos ) || ( line.find( "PROTEIN SILENTFILE" ) != std::string::npos ) ) {
710  if ( line.find("SINGLE") != std::string::npos ) {
711  silent_struct_type_ ="protein_float";
712  } else {
713  silent_struct_type_ = "protein";
714  }
715  changed = true;
716  }
717  }
718  if ( changed ) tr.Trace << "found new silent_struct_type_ " << silent_struct_type_ << " from line " << line << std::endl;
719 
720  return changed;
721 }
722 
723 
724 /// @brief This is somewhat redundant if the silent file has a "REMARK RNA" line, but some older silent files didn't do that.
726  std::istringstream l( line );
727  std::string dummy, sequence;
728  l >> dummy;
729  l >> sequence;
730  bool is_rna( true );
731  for ( Size n = 0; n < sequence.size(); n++ ) {
732  if ( sequence[n] != 'a' && sequence[n] != 'g' && sequence[n] != 'c' && sequence[n] != 'u' ) {
733  is_rna = false; break;
734  }
735  }
736 
737  if ( is_rna ) silent_struct_type_ = "rna";
738  // could also be binary_rna type, but historically we always made sure to include a "REMARK BINARY_RNA" line in those silent files, and
739  // this will be picked up later.
740 
741  return false;
742 }
743 
744 /// @brief creates a SilentStructOP using command-line options. Just a wrapper
745 /// around SilentStructFactory::get_instance()->get_silent_struct_in().
747 
748  //static SilentStructFactory ssf;
749  SilentStructOP new_ss_op;
750 
751  //if( silent_struct_type_ == "" ) new_ss_op = ssf.get_silent_struct_in();
752  //else new_ss_op = ssf.get_silent_struct( silent_struct_type_ );
755 
756  return new_ss_op;
757 }
758 
759 /// @brief Returns the SharedSilentDataOP associated with the given type.
762 ) const {
763  return shared_silent_data_[ ssdt ];
764 }
765 
768  SharedSilentDataOP ssd_op
769 ) const {
770  shared_silent_data_[ ssdt ] = ssd_op;
771 }
772 
774  return ( shared_silent_data_.find( ssdt ) != shared_silent_data_.end() );
775 }
776 
777 void
779  Real const score_fraction
780 ) {
781  // make sure fraction
782  runtime_assert( score_fraction <= 0 );
783  runtime_assert( score_fraction >= -1 );
784 
785  // go through all of the scores, and calculate the threshold for accepting a
786  // decoy by score.
788  for ( iterator iter = begin(), it_end = end(); iter != it_end; ++iter )
789  scores.push_back( iter->get_energy( "score" ) );
790 
791  //Real local_score_fraction( score_fraction * -1 );
792  std::sort( scores.begin(), scores.end() );
793  std::reverse( scores.begin(), scores.end() );
794  Size const idx( static_cast< Size > ( -1 * score_fraction * scores.size() ) );
795  Real const boundary( *( scores.begin() + idx ) );
796  tr.Debug << "reverse_score_filter: " << std::endl;
797  tr.Debug << "filtering for decoys with score worse than " << boundary << std::endl;
798 
799  Structure_Map new_structure_map_;
800  for ( iterator iter = begin(), it_end = end(); iter != it_end; ++iter ) {
801  if ( !iter->has_energy( "score" ) ) {
802  std::string msg(
803  "Error: can't find score in SilentStuct!"
804  );
805  msg += "\nSilentStruct object has scores:\n";
806 
807  // little bit of indirection here to get the SilentStruct scores.
808  std::ostringstream mystream;
809  iter->print_score_header( mystream );
810  mystream << std::endl;
811  iter->print_scores( mystream );
812  mystream << std::endl;
813  msg += mystream.str();
814 
815  utility_exit_with_message( msg );
816  }
817 
818  if ( iter->get_energy( "score" ) > boundary ) {
819  new_structure_map_[ iter->decoy_tag() ] = *iter;
820  }
821  }
822 
823  structure_map_ = new_structure_map_;
824 } // score_filter
825 
826 void
828  Real const score_fraction
829 ) {
830  if ( score_fraction < 0 ) {
831  return reverse_score_filter( score_fraction );
832  }
833  // make sure fraction is in bounds
834  runtime_assert( score_fraction >= 0 );
835  runtime_assert( score_fraction <= 1 );
836 
837  // go through all of the scores, and calculate the threshold for accepting a
838  // decoy by score.
840  for ( iterator iter = begin(), it_end = end(); iter != it_end; ++iter )
841  scores.push_back( iter->get_energy( "score" ) );
842 
843  std::sort( scores.begin(), scores.end() );
844  Size const idx( static_cast< Size > (score_fraction * scores.size() ) );
845  Real const boundary( *( scores.begin() + idx ) );
846  tr.Debug << "score_filter: " << std::endl;
847  tr.Debug << "filtering for decoys with score worse than " << boundary << std::endl;
848 
849  Structure_Map new_structure_map_;
850  for ( iterator iter = begin(), it_end = end(); iter != it_end; ++iter ) {
851  if ( !iter->has_energy( "score" ) ) {
852  std::string msg(
853  "Error: can't find score in SilentStuct!"
854  );
855  msg += "\nSilentStruct object has scores:\n";
856 
857  // little bit of indirection here to get the SilentStruct scores.
858  std::ostringstream mystream;
859  iter->print_score_header( mystream );
860  mystream << std::endl;
861  iter->print_scores( mystream );
862  mystream << std::endl;
863  msg += mystream.str();
864 
865  utility_exit_with_message( msg );
866  }
867 
868  if ( iter->get_energy( "score" ) < boundary ) {
869  new_structure_map_[ iter->decoy_tag() ] = *iter;
870  }
871  }
872 
873  structure_map_ = new_structure_map_;
874 } // score_filter
875 
876 ////////////////////////////////////////////////////////////////////////////////////////
877 // This is a little funky -- the "tags" by which the silent structs are
878 // keyed in the SilentFileData need to be renamed. Weird because the silent
879 // struct's own "decoy_tag" will no longer match their key within the silent file data.
880 ////////////////////////////////////////////////////////////////////////////////////////
881 void
883 {
884 
885  using namespace core::io::silent;
886 
887  // go through all of the scores, and then order.
888  typedef std::list< std::pair< Real, SilentStructOP > > ScoreTagList;
889  ScoreTagList score_tag_list;
890  for ( iterator iter = begin(), it_end = end(); iter != it_end; ++iter ) {
891  Real const & silent_score = (*iter)->get_energy( "score" );
892  score_tag_list.push_back( std::make_pair( silent_score, *iter /*SilentStructOP*/ ) );
893  }
894 
895  score_tag_list.sort();
896 
897  Structure_Map new_structure_map_;
898  Size count( 0 );
899  for ( ScoreTagList::const_iterator iter = score_tag_list.begin();
900  iter != score_tag_list.end(); ++iter ) {
901  SilentStructOP const & silent_struct_op( iter->second );
902 
903  ///////////////////////////////////////////////
904  // Note that we have to supply a new tag when
905  // we reorder the silent structs by energy...
906  // maps are ordered by key in C++.
907  ///////////////////////////////////////////////
908  std::string const new_tag = "S_" +
909  ObjexxFCL::lead_zero_string_of( count++,6 )+
910  silent_struct_op->decoy_tag();
911 
912  new_structure_map_[ new_tag ] = silent_struct_op;
913  }
914 
915  structure_map_ = new_structure_map_;
916 
917 } // score_filter
918 
919 
920 } // namespace silent
921 } // namespace io
922 } // namespace core