Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SilentFileData.hh
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.hh
12 ///
13 /// @brief silent input file reader for mini
14 /// @author James Thompson
15 
16 #ifndef INCLUDED_core_io_silent_SilentFileData_hh
17 #define INCLUDED_core_io_silent_SilentFileData_hh
18 
19 // mini headers
20 #include <core/types.hh>
21 
26 
27 #include <utility/pointer/ReferenceCount.hh>
28 
29 // C++ Headers
30 #include <string>
31 #include <map>
32 
33 // AUTO-REMOVED #include <utility/options/keys/BooleanOptionKey.hh>
34 // AUTO-REMOVED #include <iostream>
35 
36 #include <utility/vector1.hh>
37 
38 
39 namespace core {
40 namespace io {
41 namespace silent {
42 
43 /// @brief Abstract base class for classes that read and write different types of
44 /// silent-files. Silent-files can contain SilentStruct objects which are expected,
45 /// to be uniquely identified by some sort of string-based tag inside the file.
47 
48 private:
49  // mapping from tags to structure data pointers
52 
53  mutable std::map< SharedSilentDataType, SharedSilentDataOP > shared_silent_data_;
54  std::string filename_; // filename of the last file that we wrote a structure to
59 
60 public:
61  ///////////////////////////////////////////////////////////////////////////
62  // constructor
63 
65  filename_(),
66  store_argv_in_file_( false ),
67  strict_column_mode_( false ),
68  record_source_( false ),
69  silent_struct_type_("") // by default its option controlled.
70  {}
71 
73  filename_( filename ),
74  store_argv_in_file_( false ),
75  strict_column_mode_( false ),
76  record_source_( false ),
77  silent_struct_type_("") // by default its option controlled.
78  {}
79 
81  const std::string &filename,
82  bool store_argv_in_file,
83  bool strict_column_mode,
84  const std::string & silent_struct_type
85  ) :
86  filename_( filename ),
87  store_argv_in_file_( store_argv_in_file ),
88  strict_column_mode_( strict_column_mode ),
89  record_source_( false ),
90  silent_struct_type_( silent_struct_type )
91  {}
92 
93  /// @brief Read in the SilentStruct objects contained in the given filename.
94  /// this version will throw an exception if things go wrong (boolean return value is thus always true)
95  bool read_file(
96  std::string const & filename
97  );
98 
99  /// @brief Read in the SilentStruct objects contained in the given filename.
100  /// this version returns with a boolean to tell you about success
101  bool _read_file(
102  std::string const & filename,
103  bool throw_exception_on_bad_structs = false
104  );
105 
106 
107  /// @brief Read in the SilentStruct objects contained in the given filename.
108  /// Ignore any SilentStruct with a tag not in the tags vector.
109  /// throw an exception if things go wrong (returned boolean always true)
110  bool read_file(
111  std::string const & filename,
113  );
114 
115  bool read_stream(
116  std::istream & data,
118  bool throw_exception_on_bad_structs, /*default false*/
119  std::string filename="read_from_stream" /** for error reporting **/
120  );
121 
122  /// @brief Read in the SilentStruct objects contained in the given filename.
123  /// Ignore any SilentStruct with a tag not in the tags vector.
124  /// returns with a boolean to tell about success
125  bool _read_file(
126  std::string const & filename,
128  bool throw_exception_on_bad_structs = false
129  );
130 
131 
132  /// @brief Returns true if this silent-file plans on storing
133  /// option.get_argv() as a comment in the silent-file, false
134  /// otherwise.
135  bool store_argv_in_file() const {
136  return store_argv_in_file_;
137  }
138 
139  /// @brief Sets a boolean variable governing if this silent-file
140  /// plans on storing option.get_argv() as a comment in the
141  /// silent-file.
142  void store_argv_in_file( bool new_setting ) {
143  store_argv_in_file_ = new_setting;
144  }
145 
146 
147  /// @brief Write the given silent-struct to the given outfile.
148  bool write_silent_struct(
149  SilentStruct & s,
150  std::string const & filename,
151  bool bWriteScoreOnly = false
152  ) const;
153 
154  //
156  SilentStruct & s,
157  std::ostream & out,
158  bool bWriteScoreOnly = false
159  ) const;
160 
161  /// @brief Write the given silent-struct to the given ostream.
162  bool write_silent_struct(
163  SilentStruct & s,
164  std::ostream & out,
165  bool bWriteScoreOnly = false
166  ) const;
167 
168  /// @brief Write a comment to the given output stream.
169  void write_comment(
170  std::ostream & out,
171  std::string const & line
172  ) const;
173 
174  /// @brief Returns the number of structures contained in this container.
175  Size size() const { return structure_map_.size(); }
176 
177  /// @brief Returns the number of residues in the first structure in
178  /// this object. Not guaranteed to be fixed for all structures in
179  /// this container.
180  int nres() const;
181 
182  /// @brief Sets the filename that this SilentFileData object will
183  /// write to.
186  }
187 
189 
190  /// @brief Gets the filename that this SilentFileData object will
191  /// write to.
192  std::string const& filename() const {
193  return filename_;
194  }
195  /// @brief Return all tags in this container.
197 
198  /// @brief quickly read a list of tags from a silent-input file. Only checks
199  /// lines beginning with SCORE: strings.
201  std::string const & filename
202  ) const;
203 
204  /// @brief quickly read a list of tags from a silent-input file. Only checks
205  /// lines beginning with SCORE: strings.
206  bool read_tags_fast(
208  ) const;
209 
210  ///@brief return mode=first,last,all matched tags -- currently matching 'expression*' to tags in file, boost::regexp possible later
211  bool matched_tags(
212  std::string const& expression,
213  std::string const& mode,
214  utility::vector1< std::string >& tags_in_file
215  // utility::vector1< SilentStructOP >& decoys_in_file,
216  // bool ignore_decoys = false
217  ) const;
218 
219  /// @brief Returns a boolean indicating whether or not the strict_column_mode
220  /// is turned on when printing scores.
221  /// @detailed If strict_column_mode() is true, then the first SilentStruct
222  /// printed to this SilentFileData object sets the EnergyNames that will be
223  /// printed for all other SilentStruct objects. Extra EnergyNames in
224  /// subsequent SilentStruct objects are ignored. If new objects are missing
225  /// energies that should be printed in strict_column_mode, missing energy
226  /// values are set to zero. In !strict_column_mode(), when each SilentStruct
227  /// is printed, a new SCORE: header is printed if energies differ from last
228  /// printed SilentStruct.
229  bool strict_column_mode() const {
230  return strict_column_mode_;
231  }
232 
233  /// @brief Sets value for strict column mode. See strict_column_mode() for
234  /// more information.
235  void strict_column_mode( bool new_mode ) {
236  strict_column_mode_ = new_mode;
237  }
238 
239  ////////////////////////////////////////////////////
240  void set_record_source( bool const & new_mode ) {
241  record_source_ = new_mode;
242  }
243 
244  /// @brief Returns true if we have a SilentStruct matching the given tag,
245  /// false otherwise.
246  bool has_tag( std::string const & tag ) const {
247  return ( structure_map_.count( tag ) > 0 );
248  }
249 
250  /// @brief Returns a vector1 of the comment lines read in from
251  /// a silent-file. Comment lines are any lines in the silent-file
252  /// beginning with a # character.
254  return comment_lines_;
255  }
256 
257  /// @brief Adds a comment-line that will be printed in write_all method.
258  /// Comment lines are simply lines in the silent-file that begin with the #
259  /// character, and are printed in the write_all method.
260  void comment_line( std::string const & line ) {
261  comment_lines_.push_back( line );
262  }
263 
264  /// @brief Removes the worst ( 1 - score_fraction ) percent of the decoys by
265  /// score. The value of score_fraction should be between 0 and 1.0.
266  void
267  score_filter(
268  Real const score_fraction
269  );
270 
271  void
273  Real const score_fraction
274  );
275 
276  /// @brief Orders silent structs by energy.
277  void
278  order_by_energy();
279 
280  // This was basically the old add_structure...
282 
283  /// @brief Adds a SilentStructOP to the structure_map_. If the SilentStruct's
284  /// tag already exists in the structure_map_, a new tag is assigned. Careful
285  /// with this method, as it stores an owning pointer. If you change the
286  /// SilentStruct later, it will change your already stored structures.
287  void add_structure( SilentStructOP const & new_struct );
288 
289  /// @brief push_back to provide compatibility with other std containers.
290  void push_back( SilentStructOP const & new_struct ) {
291  add_structure( new_struct );
292  }
293 
294  /// @brief Saves a copy of the silent struct. This method is:
295  /// - SAFE! in the sense that it actually copies the SilentStruct object, not
296  /// just the pointer to the object.
297  /// - SLOW! in the sense that copying the object takes a small amount of time.
298  void add_structure( SilentStruct const & new_struct );
299 
300  /// @brief Return a SilentStruct referred to by the given tag. Assumes that
301  /// we have checked the tag!!
302  SilentStruct const &
304  const std::string & tag
305  ) const {
306  return *( structure_map_.find(tag)->second );
307  }
308 
309  /// @brief Remove all of the SilentStruct objects from this object.
311  structure_map_.clear();
312  }
313 
314  /// @brief Clears all of the data associated with this object.
315  void clear() {
317  comment_lines_.clear();
318 
319  shared_silent_data_.clear();
320  filename_.clear();
321  store_argv_in_file_ = false;
322  strict_column_mode_ = false;
323  }
324 
325  /// @brief Destructor.
326  virtual ~SilentFileData();
327 
328  /// @brief write all SilentStruct objects in the structure_map_ to the given
329  /// filename.
330  void write_all(
331  std::string const & filename, bool bWriteScoreOnly = false
332  ) const;
333 
334  /// @brief renumber all of the decoys in this SilentFileData object. This
335  /// obliterates decoy tag associated with every SilentStruct object, and tries
336  /// to sensibly number the decoys with similar and increasingly numbered decoy
337  /// tags.
338  void renumber_all_decoys();
339 
341 
342  /// @brief SharedSilentData methods
346  ) const;
347  bool has_shared_silent_data( SharedSilentDataType ssdt ) const;
348 
349 private:
350  //some utility function for the reading process --- returns bool if new type
351  bool read_silent_struct_type_from_remark( std::string const& line, bool header=false /*make true if this is one of the first 3 lines*/ );
352 
353  bool check_if_rna_from_sequence_line( std::string const& sequence_line );
354 
355 public:
356  /// @brief Iterator class for SilentFileData container.
357  class iterator {
358 
359  friend class const_iterator;
360 
361  public:
365  typedef std::ptrdiff_t difference_type;
366  typedef std::forward_iterator_tag iterator_category;
367  /// @brief empty constructor
368  iterator() {}
369 
370  /// @brief Constructor, given an iterator into the Structure_Map.
371  iterator( Structure_Map::iterator s_iter ) {
372  it_ = s_iter;
373  }
374 
376 
377  iterator& operator=( const iterator& src ) {
378  it_ = src.it_;
379  return (*this);
380  }
381 
382  bool operator==( const iterator& other ) const {
383  return ( it_ == other.it_ );
384  }
385 
386  bool operator!=( const iterator& other ) const {
387  return ( it_ != other.it_ );
388  }
389 
391  it_++;
392  return (*this);
393  }
394 
396  it_--;
397  return (*this);
398  }
399 
401  return it_->second();
402  }
403 
405  return it_->second;
406  }
407 
408  protected:
409  Structure_Map::iterator it_; // keep track of my place in a Structure_Map
410  }; // class iterator
411 
412  /// @brief const_iterator class for SilentFileData container.
417  typedef std::ptrdiff_t difference_type;
418  typedef std::bidirectional_iterator_tag iterator_category;
419 
420  public:
421  /// @brief empty constructor
423 
424  /// @brief Constructor, given an iterator into the Structure_Map.
425  const_iterator( Structure_Map::const_iterator s_iter ) {
426  it_ = s_iter;
427  }
428 
430 
432  it_ = src.it_;
433  return (*this);
434  }
435 
436  bool operator==( const const_iterator& other ) {
437  return ( it_ == other.it_ );
438  }
439 
440  bool operator!=( const const_iterator& other ) {
441  return ( it_ != other.it_ );
442  }
443 
445  it_++;
446  return (*this);
447  }
448 
450  it_--;
451  return (*this);
452  }
453 
455  return it_->second();
456  }
457 
459  return it_->second;
460  }
461 
462 
463 
464  private:
465  Structure_Map::const_iterator it_; // keeps track of my place in a Structure_Map
466  }; // class iterator
467 
468  // void open_for_writing( utility::io::ozstream&, std::string const& filename, std::stringstream& ) const; //open silent-file and write header if first
469 
470  /// @brief Returns an iterator to the start of the members of this container.
471  iterator begin() { return ( iterator( structure_map_.begin() ) ); }
472 
473  /// @brief Returns an iterator to the start of the members of this container.
474  const_iterator begin() const { return ( const_iterator( structure_map_.begin() ) ); }
475 
476  /// @brief Returns an iterator to the end of the members of this container.
477  iterator end() { return ( iterator( structure_map_.end() ) ); }
478 
479  /// @brief Returns an iterator to the end of the members of this container.
480  const_iterator end() const { return ( const_iterator( structure_map_.end() ) ); }
481 
482  //const_iterator begin_const() const { return ( const_iterator( structure_map_.begin() ) ); }
483  // const_iterator end_const() const { return ( const_iterator( structure_map_.end() ) ); }
484 
485 }; // class SilentFileData
486 
487 } // namespace silent
488 } // namespace io
489 } // namespace core
490 
491 #endif