Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConstantLengthFragSet.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/ConstantLengthFragSet.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 
16 // Unit Headers
18 
19 // Package Headers
20 #ifdef WIN32
21 #include <core/fragment/FragID.hh>
22 #endif
23 
26 #include <core/fragment/Frame.hh>
29 
30 // Project Headers
31 #include <core/types.hh>
32 
33 // ObjexxFCL Headers
34 #include <ObjexxFCL/string.functions.hh>
35 
36 // Utility headers
37 #include <utility/vector1.fwd.hh>
38 #include <utility/io/izstream.hh>
39 #include <utility/pointer/owning_ptr.hh>
40 #include <basic/Tracer.hh>
41 
42 // C/C++ headers
43 #include <algorithm>
44 #include <iostream>
45 #include <ostream>
46 #include <sstream>
47 #include <string>
48 
50 #include <utility/vector1.hh>
51 
52 namespace ObjexxFCL { namespace fmt { } } using namespace ObjexxFCL::fmt;
53 
54 namespace core {
55 namespace fragment {
56 
57 using namespace kinematics;
58 using namespace ObjexxFCL;
59 
60 static basic::Tracer tr("core.fragments.ConstantLengthFragSet");
61 
62 ConstantLengthFragSet::~ConstantLengthFragSet() {}
63 
64 ConstantLengthFragSet::ConstantLengthFragSet( Size frag_length, std::string filename ) {
65  set_max_frag_length( frag_length );
66  read_fragment_file(filename);
67  }
68 
69 // if fragments are of differing length assertion in add will fail
70 ConstantLengthFragSet::ConstantLengthFragSet( FragSet const& fragments ) {
71  set_max_frag_length( fragments.max_frag_length() );
72  Parent::add( fragments );
73 }
74 
75 ///@brief there is only one Frame per position, end / max_overlap are ignored
76 Size ConstantLengthFragSet::region(
77  MoveMap const& mm,
80  core::Size , //min_overlap not used
81  core::Size , //min_length not used
82  FrameList &frames
83 ) const {
84  Size count( 0 );
85  for ( Size pos=start; pos<=frames_.size() && pos<=end; pos++ ) {
86  if ( frames_[pos] ) {
87  if ( frames_[pos]->is_applicable( mm ) && frames_[pos]->is_valid() ) {
88  frames.push_back( frames_[ pos ] );
89  count++;
90  }
91  }
92  }
93  return count;
94 }
95 
96 // This method will only be called if the frame has been incompatible with
97 // existing frames. In case of ConstantLengthFragSet aka single-kind-of Frame
98 // FragSet this means position at aframe->start() is empty
99 void ConstantLengthFragSet::add_( FrameOP aframe ) {
100  if ( max_frag_length() ) runtime_assert( aframe->length() == max_frag_length() );
101  Size seqpos( aframe->start() );
102  if ( frames_.size() < seqpos ) {
103  frames_.resize( seqpos, NULL );
104  }
105  runtime_assert( frames_[ seqpos ] == 0 ); //I consider this as error... don't add incompatible frames to a ConstantLengthFragSet
106  frames_[ seqpos ] = aframe;
107 }
108 
109 void ConstantLengthFragSet::read_fragment_file( std::string filename, Size top25, Size ncopies, bool bAnnotation ) {
110  using std::cerr;
111  using std::endl;
112 
113  utility::io::izstream data( filename );
114  if ( !data.good() ) {
115  cerr << "Open failed for file: " << data.filename() << endl;
116  utility::exit( EXIT_FAILURE, __FILE__, __LINE__);
117  }
118 
119  read_fragment_stream(data,top25,ncopies,bAnnotation);
120 }
121 
122 void ConstantLengthFragSet::read_fragment_stream( utility::io::izstream & data, Size top25, Size ncopies, bool bAnnotation ) {
123  using namespace ObjexxFCL;
124  using namespace ObjexxFCL::fmt;
125  using std::endl;
126  using std::istringstream;
127  using std::string;
128 
129  Real score = 0.0;
130  string line;
131 
132  Size insertion_pos = 1;
133  FragDataOP current_fragment( NULL );
134  FrameOP frame;
135 
136  Size n_frags( 0 );
137  while ( getline( data, line ) ) {
138  // skip blank lines
139  if ( line == "" || line == " " ) {
140  // add current_fragment to the appropriate frame
141  if ( current_fragment && frame ) {
142  if ( !top25 || frame->nr_frags() < top25*ncopies ) {
143  current_fragment->set_valid(); //it actually contains data
144  if ( !frame->add_fragment( current_fragment ) ) {
145  tr.Fatal << "Incompatible Fragment in file: " << data.filename() << endl;
146  utility::exit( EXIT_FAILURE, __FILE__, __LINE__);
147  } else {
148  for ( Size i = 2; i <= ncopies; i++ ) frame->add_fragment( current_fragment );
149  }
150  }
151  }
152 
153  // create a new current_fragment
154  current_fragment = NULL;
155  continue;
156  }
157 
158  // skip lines beginning with # characters, representing comments
159  // save score if it exists
160  if ( line.substr(0,1) == "#" ) {
161  istringstream in( line );
162  string tag;
163  in >> tag;
164  if (!in.eof()) {
165  in >> tag;
166  if (tag == "score" && !in.eof()) {
167  in >> score;
168  }
169  }
170  continue;
171  }
172 
173  // skip lines that are too short?? --- such a stupid thing
174  if ( line.length() < 22 ) {
175  tr.Warning << "line too short: Skipping line '" << line << "'" << endl;
176  continue;
177  }
178 
179  // skip position and neighbor lines for now
180  if ( line.find("position") != string::npos ) {
181  istringstream in( line );
182  string tag;
183  in >> tag;
184  in >> insertion_pos;
185 
186  // create a new frame
187  if ( frame && frame->is_valid() ) {
188  add( frame );
189  n_frags = std::max( n_frags, frame->nr_frags() );
190  }
191  frame = new Frame( insertion_pos );
192  continue;
193  }
194 
195  // actual reading happens here
196  string pdbid = line.substr(1, 4);
197  char chain = char_of(line.substr(6, 1));
198  int aa_index = int_of(line.substr(8, 5));
199  char aa = char_of(line.substr(14, 1));
200  char ss = char_of(line.substr(16, 1));
201  Real phi = float_of(line.substr(18, 9));
202  Real psi = float_of(line.substr(27, 9));
203  Real omega = float_of(line.substr(36, 9));
204 
205  BBTorsionSRFDOP res = new BBTorsionSRFD(3, ss, aa); // 3 protein torsions
206 
207  // set torsions
208  res->set_torsion(1, phi);
209  res->set_torsion(2, psi);
210  res->set_torsion(3, omega);
211 
212  // optionally read in and set cartesian coordinates for the residue's CA
213  if (line.length() > 45) {
214  Real x = float_of(line.substr(45, 9));
215  Real y = float_of(line.substr(54, 9));
216  Real z = float_of(line.substr(64, 9));
217  res->set_coordinates(x, y, z);
218  //tr.Info << "Read cartesian coordinates (" << x << "," << y << "," << z << ")" << endl;
219  }
220 
221  // set predicted secondary structure
222  res->set_secstruct(ss);
223 
224  if ( !current_fragment ) {
225  if ( bAnnotation ) {
226  current_fragment = new AnnotatedFragData( pdbid, aa_index, chain );
227  }
228  else {
229  current_fragment = new FragData;
230  }
231  }
232  current_fragment->add_residue(res);
233  current_fragment->set_score(score);
234  } // while ( getline( data, line ) )
235 
236  if ( frame && frame->is_valid() ) {
237  add( frame );
238  n_frags = std::max( n_frags, frame->nr_frags() );
239  }
240 
241  tr.Info << "finished reading top " << n_frags << " "
242  << max_frag_length() << "mer fragments from file " << data.filename()
243  << endl;
244 }
245 
246 FrameIterator ConstantLengthFragSet::begin() const {
247  return FrameIterator( new ConstantLengthFragSetIterator_( frames_.begin(), frames_.end() ) );
248 }
249 
251  return FrameIterator( new ConstantLengthFragSetIterator_( frames_.end(), frames_.end() ) );
252 }
253 
254 } //fragment
255 } //core