Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FragmentIO.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/fragments/FragSet.cc
12 /// @brief set of fragments for a certain alignment frame
13 /// @author Oliver Lange (olange@u.washington.edu)
14 /// @author James Thompson (tex@u.washington.edu)
15 /// @author Christopher Miles (cmiles@uw.edu)
16 
17 // Unit Headers
19 #ifdef WIN32
20 #include <core/fragment/FragID.hh>
21 #endif
22 
23 // Factory Headers
28 
29 // Packet Headers
30 #include <core/fragment/Frame.hh>
34 
35 // Project Headers
36 #include <core/types.hh>
37 
38 // Utility headers
39 #include <utility/vector1.fwd.hh>
40 #include <utility/io/izstream.hh>
41 #include <utility/io/ozstream.hh>
42 #include <utility/pointer/owning_ptr.hh>
43 #include <basic/Tracer.hh>
44 #include <ostream>
45 
46 // Auto Headers
49 
50 #include <utility/vector1.hh>
51 
52 
53 namespace core {
54 namespace fragment {
55 
56 static basic::Tracer tr("core.io.fragments");
57 
58 FragFactory FragmentIO::frag_factory_;
60 
61 void
63  frag_types_[ type_name ] = new_frag;
64 }
65 
66 void
67 FragFactory::add_frame_type( std::string const& type_name, FrameOP new_frame ) {
68  frame_types_[ type_name ] = new_frame;
69 }
70 
72  // initialization of fragtions which this factory knows how to instantiate
77 
80 }
81 
82 FrameOP FragFactory::frame( std::string const& type ) const {
83  FrameTypes::const_iterator const iter ( frame_types_.find( type ) );
84  if ( iter != frame_types_.end() ) {
85  return iter->second->clone();
86  }
87  return NULL;
88 }
89 
91 FragFactory::frag_type( std::string const& frag_name ) const {
92  SRFD_Types::const_iterator const iter ( frag_types_.find( frag_name ) );
93  if ( iter != frag_types_.end() ) {
94  return iter->second->clone();
95  }
96  return NULL;
97 }
98 
100  return frag_factory_;
101 }
102 
103 void FragmentIO::read_next_frames( std::istream& data, std::string& next_line, FrameList &next_frames ) {
104  //read lines defining the FRAMES for a given block of Fragments
105  do {
106  if ( !next_line.size() ) continue; //skip empty lines
107  std::string tag;
108  std::istringstream line_stream( next_line );
109  line_stream >> tag;
110  FrameOP new_frame = frag_factory_.frame( tag );
111  if ( new_frame ) {
112  new_frame->read( line_stream );
113  tr.Trace << "read frame " << *new_frame << std::endl;
114  next_frames.push_back( new_frame );
115  } else break; // not a Frame tag: line means something else .. finished reading frames for this block
116  if ( line_stream.fail() ) data.setstate( std::ios_base::failbit );
117  } while ( getline( data, next_line ) );
118 }
119 
120 void FragmentIO::read_frag_data( std::istream& data, std::string& next_line, FrameList &new_frames ) {
121  if ( !new_frames.size() ) return;
122  Size const length( new_frames[ 1 ]->length() );
123  tr.Trace << "frags in frame at beginning: " << new_frames[ 1 ]->nr_frags() << std::endl;
124 
125  FragDataOP current_fragment = NULL;//new FragData;
126  do {
127  if ( !next_line.size() ) continue; //skip empty lines
128  if ( next_line == "ENDFRAME" ) { //can probably vanish soon
129  getline( data, next_line );
130  break;
131  }
132  if ( next_line == "FRAME" ) { // shouldn't work, since next_line will be FRAME 12 20
133  break;
134  }
135  std::string tag;
136  Size pos;
137  Size pdb_pos;
138  std::string pdb_id;
139 
140  std::istringstream line_stream( next_line + " " );
141  line_stream >> pos >> pdb_pos >> pdb_id >> tag;
142  if ( line_stream.fail() ) break; //e.g., we have a line that starts with a tag
143 
144  // bIgnoreFragments if we have already top_ -- but don't forget to eat all lines until next "FRAME"
145  if ( top_ && (*new_frames.begin())->nr_frags() >= top_ * ncopies_ ) continue;
146 
147  if ( !current_fragment ) {
148  if ( pdb_pos && bAnnotate_ ) current_fragment = new AnnotatedFragData( pdb_id, pdb_pos );
149  else current_fragment = new FragData;
150  }
151 
153  if ( !new_srfd ) {
154  tr.Error << "ERROR: tag " << tag << " not known by frag_factory" << std::endl;
155  break;
156  }
157  line_stream >> *new_srfd;
158  if ( line_stream.fail() ) {
159  tr.Error << "ERROR: reading of " << tag << " failed in line: " << next_line << std::endl;
160  data.setstate( std::ios_base::failbit );
161  break;
162  }
163  current_fragment->add_residue( new_srfd );
164  if ( current_fragment->size() == length ) {
165  current_fragment->set_valid();
166  tr.Trace << "read FragData" << std::endl << *current_fragment << std::endl;
167  for ( FrameList::iterator frame = new_frames.begin(), eframe = new_frames.end();
168  frame != eframe; ++frame ) {
169 
170  //jump out if top_ fragments have been read
171  if ( !(*frame)->add_fragment( current_fragment ) ) {
172  tr.Error << "failed to add " << *current_fragment << " to frame " << (**frame) << std::endl;
173  data.setstate( std::ios_base::failbit );
174  } else {
175  for ( Size i = 2; i<=ncopies_; i++ ) {
176  (*frame)->add_fragment( current_fragment );
177  }
178  }
179  } //frames
180  current_fragment = NULL;
181  }
182  } while ( getline( data, next_line ) );
183  if ( data.fail() && data.eof() && !data.bad() ) { //stupid getline sets fail bit instead of eof if last line is read.
184  tr.Debug << "End of fragment file during fragment reading... set eofbit" << std::endl;
185  data.clear( std::ios_base::eofbit );
186  }
187  if ( current_fragment ) {
188  tr.Error << "ERROR: read incomplete fragment at line" << next_line << std::endl;
189  data.setstate( std::ios_base::failbit );
190  }
191 }
192 
193 void FragmentIO::read_data( std::istream& data, FrameList& all_frames ) {
194  tr.Info << " read fragments options: top = " << top_ << " copies: " << ncopies_
195  << " annotate: " << ( bAnnotate_ ? "yes" : "no" ) << std::endl;
196 
197  std::string next_line;
198  while ( data.good() ) {
199  FrameList new_frames;
200 
201  read_next_frames( data, next_line, new_frames );
202  if ( new_frames.size() == 0 ) tr.Debug << "found no new FRAME entry ... finished. " << std::endl;
203  if ( data.eof() ) tr.Debug << " reached EOF during reading of FRAME entries " << std::endl;
204  if ( !data || new_frames.size() == 0 ) break;
205 
206  read_frag_data( data, next_line, new_frames );
207  if ( data.fail() ) break;
208 
209  for ( FrameList::iterator frame = new_frames.begin(), eframe = new_frames.end();
210  frame != eframe; ++frame ) {
211  if ( (*frame)->nr_frags() ) {
212  all_frames.push_back( *frame );
213  }
214  }
215  }
216  if ( data.fail() || data.bad() ) {
217  tr.Error << "reading failed at line " << next_line << std::endl;
218  }
219 }
220 
222  //detect format:
223  std::string line;
224 
225  if ( frag_cache_.find( filename ) == frag_cache_.end() ) {
226  //okay we are about to read a new fragment file...
227  // at this moment we loose trust in fragments that are only referenced by the frag_cache..
229 
230  // format check...
231  utility::io::izstream data( filename );
232  if ( !data.good() )
233  utility_exit_with_message("ERROR: FragmentIO: could not open file " + filename );
234 
235  tr.Info << "reading fragments from file: " << filename << " ... " << std::endl;
236  getline( data, line );
237  std::istringstream str( line );
238  std::string tag;
239  str >> tag;
240  if ( tag == "position:" ) {
241  tr.Info << "rosetta++ fileformat detected! Calling legacy reader... "
242  << std::endl;
243 
245  frags->read_fragment_file( filename, top_, ncopies_, bAnnotate_ );
246  frag_cache_[ filename ] = frags;
247  return frags;
248  }
249 
250  FrameList frames;
251  read_data( filename, frames );
252 
253  //find out if ConstantLengthFragSet is sufficient...
254  // for now always use OrderedFragSet
255  FragSetOP frags = new OrderedFragSet();
256  frags->add( frames );
257  frag_cache_[ filename ] = frags;
258  }
259  return frag_cache_[ filename ];
260 }
261 
263  for ( FragFileCache::iterator it = frag_cache_.begin(); it != frag_cache_.end(); ) {
264  if ( it->second->ref_count() == 1 ) {
265  tr.Info << "GARBAGE COLLECTION: remove " << it->first << " from frag_cache " << std::endl;
266  frag_cache_.erase( it++ ); //doesn't return iterator ... help with post-fix iterator
267  } else ++it;
268  }
269 }
270 
272  utility::io::izstream data( filename );
273  if ( !data.good() ) utility_exit_with_message("ERROR: FragmentIO: could not open file " + filename );
274  tr.Info << "reading fragment frames from file: " << filename << " ... " << std::endl;
275 
276  read_data( data, frames);
277  if ( data.fail() ) {
278  utility_exit_with_message( "failed to read fragments from file " + filename );
279  }
280 }
281 
282 void FragmentIO::write_data( std::string const& file, FragSet const& frags ) {
283  utility::io::ozstream out( file );
284  for ( FrameIterator it=frags.begin(), eit=frags.end(); it!=eit; ++it ) {
285  (*it)->show( out );
286  }
287 }
288 
289 } //fragment
290 } //core