Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Frame.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/Frame.cc
12 /// @brief set of fragments for a certain alignment frame
13 /// @author Oliver Lange (olange@u.washington.edu)
14 /// @date Wed Oct 20 12:08:31 2007
15 ///
16 
17 // Unit Headers
18 #include <core/fragment/Frame.hh>
19 
20 
21 // Package Headers
22 
23 // Project Headers
24 #include <core/pose/Pose.hh>
25 // AUTO-REMOVED #include <core/chemical/ChemicalManager.hh>
29 
31 
32 // ObjexxFCL Headers
33 #include <ObjexxFCL/format.hh>
34 
35 // Utility headers
36 #include <utility/vector1.fwd.hh>
37 #include <basic/Tracer.hh>
38 
40 #include <utility/vector1.hh>
41 
42 
43 namespace core {
44 namespace fragment {
45 
46 /// @details Auto-generated virtual destructor
48 
49 using namespace kinematics;
50 
51 
52 static basic::Tracer tr("core.fragment");
53 //pose::PoseOP Frame::my_static_pose_for_testing_ = NULL;
54 void
56  std::string sequence,
57  chemical::ResidueTypeSet const& residue_set,
58  pose::Pose& pose
59 ) {
60  using namespace chemical;
61  // clear all of the old data in the pose
62  pose.clear();
63 
64  // setup the pose by appending the appropriate residues residues
65  for ( Size seqpos = 1; seqpos <= sequence.length(); ++seqpos ) {
66  char aa = sequence[seqpos-1]; // string indexing is zero-based!
67  // std::cerr<< aa << " aminoacid requested" << std::endl;
68  AA my_aa = aa_from_oneletter_code( aa );
69  ResidueTypeCOPs const & rsd_type_list( residue_set.aa_map( my_aa ) );
70  if ( rsd_type_list.size() == 0 ) {
71  std::cerr << " cannot find aminoacid " << my_aa << " from sequence " << sequence << " in Frame::fragment_from_pose() " << std::endl;
72  }
73  runtime_assert( rsd_type_list.size() );
74  Size best_index = 1;
75  ResidueType const & rsd_type( *(rsd_type_list[ best_index ]) );
77  if ( seqpos == 1 ) {
78  pose.append_residue_by_jump( *new_rsd, 1 );
79  } else {
80  pose.append_residue_by_bond( *new_rsd, true );
81  }
82  } // for seqpos
83 
84  // if ( pose.total_residue() > 1) pose.conformation().insert_chain_ending( pose.total_residue() - 1 ); // probably not necessary
85 
86 } // make_pose_match_sequence_
87 
88 
90  start_( 0 ),
91  end_( 0 ),
92  nr_res_( 0 )
93 {}
94 
96  start_( begin ),
97  end_( end ),
98  nr_res_( nr_res )
99 {}
100 
102  start_( start),
103  end_ ( start + length - 1 ),
104  nr_res_( length )
105 {}
106 
108  start_( start),
109  end_ ( 0 ), //needs to be determined later
110  nr_res_( 0 )
111 {}
112 
114  start_( start ),
115  end_( start + frag1->size() - 1 ),
116  nr_res_( frag1->size() )
117 {
118  add_fragment( frag1 );
119 }
120 
122  : start_( start ),
123  end_( start + length - 1),
124  nr_res_( length )
125 {
126  add_fragment( new FragData( srfd, length ) );
127 }
128 
129 /// @brief clone method, new frame with same alignment position, fragments are not copied!
131  return new Frame( start(), end(), length() );
132 }
133 
134 /// @brief clone method, new frame with same alignment position, fragments are not copied!
136  FrameOP newFrame = clone();// new Frame( start(), end(), length() );
137  *newFrame = *this; //usually that is enough
138  return newFrame;
139 }
140 
141 /// @brief clone method, new frame with same alignment position, one fragments is copied as template ( valid() == false )
143  FrameOP newFrame = clone();// new Frame( start(), end(), length() );
144  if ( nr_frags() ) {
145  newFrame->frag_list_.push_back( frag_list_[ 1 ]->clone() );
146  newFrame->frag_list_[ 1 ]->set_valid( false );
147  }
148  return newFrame;
149 }
150 
151 ///@brief type() is specifying the output name of the Frame in FragmentIO ("FRAME", "JUMPFRAME", etc)
153  return _static_type_name();
154 }
155 
157  return "FRAME";
158 }
159 
160 
161 /// @brief accesors for underlying FragData
162 FragData const & Frame::fragment( core::Size frag_num ) const {
163  return *frag_list_[ frag_num ];
164 }
165 
166 /// @brief accessor for underlying FragData
168  return *frag_list_[ frag_num ];
169 }
170 
171 /// @brief accessor for underlying FragData as owning ptr
173  return frag_list_[ frag_num ];
174 }
175 
176 /// @brief accessor for underlying FragData as owning ptr
178  return frag_list_[ frag_num ];
179 }
180 
181 /// @brief a frame is considered valid if at least one fragment is contained and this fragment is also valid
182 /// (not an empty template fragment)
183 bool Frame::is_valid() const {
184  // why check both 1 and 2?
185  return ( nr_frags() >= 2 && fragment( 2 ).is_valid() )
186  || ( nr_frags() >= 1 && fragment( 1 ).is_valid() );
187 }
188 
189 /// @brief translate intra-frame position into sequence position. (trivial for base-class)
190 core::Size Frame::seqpos( core::Size intra_pos ) const { // BaseClass --> continuous frames
191  return start()+intra_pos-1;
192 }
193 
194 /// @brief a unique id for every fragment in the list.
195 /// his is silly, but would enable later on to avoid cache_clearence on deletion of FragData entries
196 /// in this case, we would require that the ID of a certain fragment never changes, even if the position in FragList changes
198 { return frag_num; }
199 
200 
201 /// @brief true if frame is continuous (always true for base class)
203 { return true; } // base class can only handle continuous frames
204 
205 /// @brief number of fragments attached to this frame
207  return frag_list_.size();
208 }
209 
210 
211 /// @brief whether this fragment contains a certain position
212 bool Frame::contains_seqpos( core::Size seqpos ) const {
213  return (seqpos >= start_ ) && (seqpos <= end_ ); }
214 
215 /// @brief first seqpos of this frame
217  return start_;
218 }
219 
220 
221 /// @brief last sequence position affected by this frame
223  return end_;
224 }
225 
226 /// ///@brief set stop position
227 // core::Size stop( core::Size setting );
228 
229 /// @brief last sequence position affected by this frame
231  return end_;
232 }
233 
234 
235 bool Frame::moves_residue( core::Size pos ) const {
236  return pos <= stop() && pos >= start();
237 }
238 /// @brief number of residues affected by this frame
240  return is_applicable( mm );
241 }
242 
243 /// @brief number of residues in this frame ( for continuous frames it is the same as end()-start() + 1 )
245  return nr_res_;
246 }
247 
248 
249 // can we make this private and accessible via
250 // a friend BaseCacheUnit statement --> are derived classes of a friend still a friend
251 /// @brief return handle to cached data stored under "tag"
252 /// shouldn't be called directly
253 BaseCacheUnit& Frame::cache( std::string tag, BaseCacheUnitOP const& new_cache ) const {
254  CacheMap::const_iterator iter( cache_.find( tag ) );
255  if ( iter == cache_.end() ) {
256  return *( cache_[ tag ] = new_cache->clone() );
257  }
258  return *( cache_[ tag ] );
259 }
260 
261 /// @brief copies all entries in the Frame-Cache for fragment "sid" of Frame "source" to fragment "nid" of "this" frame
262 void Frame::clone_cache_data( Frame const& source, core::Size sid, core::Size nid ) {
263  for ( CacheMap::iterator it=source.cache_.begin(), eit=source.cache_.end(); it!=eit; ++it ) {
264  cache( it->first, it->second ).remap_value( *it->second, sid, nid );
265  }
266 }
267 
268 
269 //@brief prints frame to stream -- multiline object
270 
271 
272 bool Frame::is_mergeable( Frame const& other ) const {
273  if ( !( other.length() == length() && other.start() == start() && other.stop() == stop() )) return false;
274  if ( other.nr_frags()==0 || nr_frags()==0 ) return true;
275  if ( other.fragment( 1 ).is_compatible( fragment( 1 ) )) return true;
276  return false;
277 }
278 
279 
280 /// @brief is a FragData object compatible with the already stored ones ?
281 /// @detail you can only add instances of FragData to the same Frame that are compatible, i.e., that contain the same
282 /// class of FragData, e.g., based on BBTorsionSRFD,
283 /// if you want to have different fragment for other dof's at the same sequence position create a new Frame.
284 /// Users of the fragment-core are aware that multiple Frames for the same sequence position may exist.
285 bool Frame::is_compatible( FragDataCOP new_frag ) const {
286  if ( nr_frags() ) {
287  return new_frag->is_compatible( fragment( 1 ) );
288  } else {
289  if ( length() == 0 ) {
290  nr_res_ = new_frag->size();
291  end_ = start_ + nr_res_ - 1;
292  return true;
293  } else return (new_frag->size() == length() );
294  }
295  /* if ( frag_list_.size() ) {
296  kinematics::MoveMap new_move_map;
297  new_frag->generate_move_map( new_move_map, *this );
298  return true;
299  // return move_map_ == new_move_map;
300  } else {
301  new_frag->generate_move_map( move_map_, *this );
302  return true;
303  }
304  */
305 }
306 
308  start_ = start;
309  end_ = end;
310  nr_res_ = length;
311 }
312 
313 
314 
315 
316 
317 
318 
319 ////////////////////////////////////////// I M P L E M E N T A T I O N S ///////////////////////////////////////////////
320 
322  assert( new_frag );
323  bool success ( is_compatible( new_frag ) );
324  if ( success ) frag_list_.push_back( new_frag );
325  return frag_list_.size();
326 }
327 
328 
329 //
330 // void Frame::delete_fragment( core::Size frag_num )
331 // {
332 // std::cerr << "SPEED WARNING: fragment deleted -- cache invalidated"
333 // << "for now we use frag_num as frag_id, so all cached data is invalidated" << std::endl;
334 // cache_.clear();
335 
336 // //and now delete fragment...
337 // std::cerr << "delete stubbed out" << frag_num << std::endl;
338 // }
339 
341  // if ( nr_frags()==0 ) return true;
342  if ( is_continuous() ) {
343  return fragment( 1 ).is_applicable( mm, start(), end() );
344  } else {
345  return fragment( 1 ).is_applicable( mm, *this );
346  }
347 }
348 
349 core::Size Frame::apply( kinematics::MoveMap const& mm, core::Size frag_num, pose::Pose & pose ) const {
350  if ( is_continuous() ) {
351  return fragment( frag_num ).apply( mm, pose, start(), end() );
352  } else {
353  return fragment( frag_num ).apply( mm, pose, *this );
354  }
355 }
356 
357 core::Size Frame::apply( core::Size frag_num, pose::Pose & pose ) const {
358  if ( is_continuous() ) {
359  return fragment( frag_num ).apply( pose, start(), end() );
360  } else {
361  return fragment( frag_num ).apply( pose, *this );
362  }
363 }
364 
365 
366 
368  return fragment( frag_num ).apply_ss( mm, ss, *this );
369 }
370 
371 
372 void Frame::shift_to( core::Size setting ) {
373  using core::Size;
374  // set end first
375  if ( start_ < setting ) {
376  end_ += ( setting - start_ );
377  } else { // start >= setting
378  end_ -= ( start_ - setting );
379  }
380  // set start second
381  start_ = setting;
382  assert( nr_res_ == ( end_ - start_ + 1 ) ); //OL: changed assert to use == instead of "=", much better now
383 }
384 
385 // void show( std::ostream& out ) {
386 //
387 // }
388 
389 
390 
391 
393  Size frag_num,
394  pose::Pose & pose,
395  chemical::ResidueTypeSetCAP restype_set ) const
396 {
397  // if (!my_static_pose_for_testing_ ) {
398  // std::string const pdbfile ( "protocols/abinitio/2GB3.pdb" );
399  //my_static_pose_for_testing_=new pose::Pose;
400  pose.clear();
401  make_pose_from_sequence_( frag_list_[ frag_num ]->sequence(),
402  *restype_set,
403  pose );
404  // core::import_pose::pose_from_pdb( *my_static_pose_for_testing_, pdbfile );
405  fragment( frag_num ).apply( pose, 1, length() );
406 }
407 
409  for ( FragDataList::const_iterator it=new_frag.begin(), eit=new_frag.end();
410  it!=eit; ++it ) {
411  bool success ( add_fragment( *it ) );
412  if ( !success ) return false;
413  }
414  return true;
415 }
416 
417 void Frame::show_classic( std::ostream &out ) const {
418  using namespace ObjexxFCL::fmt;
419  Size position = start();
420  out << "position: " << RJ( 10, position) << " neighbors: " << RJ( 10, nr_frags() ) << std::endl << std::endl;
421  for ( Size nr = 1; nr <= nr_frags(); nr ++ ) {
422  fragment( nr ).show( out );
423  out << std::endl;
424  }
425 }
426 
427 void Frame::show( std::ostream &out ) const {
428 // using namespace ObjexxFCL::fmt;
429 // out << "FRAME " << " " << RJ( 10, start() ) << RJ( 10, end() ) << std::endl;
430  show_header( out );
431  show_fragments( out );
432 }
433 
434 void Frame::show_header( std::ostream &out ) const {
435  using namespace ObjexxFCL::fmt;
436  out << "FRAME " << " " << RJ( 3, start() ) << " " << RJ( 3, end() ) << std::endl;
437 }
438 
439 void Frame::read( std::istream &in ) {
440  //because of one FragData - multiple frames.. reading of fragments is outsourced to FragmentIO
441  // std::string tag;
442  in >> start_ >> end_;
443  nr_res_ = end_ - start_ + 1;
444 }
445 
446 void Frame::show_fragments( std::ostream& out ) const {
447  for ( Size nr = 1; nr <= nr_frags(); nr ++ ) {
448  runtime_assert( fragment_ptr( nr ) );
449  // std::cerr << "FRAME::show_fragments " << nr << " seqpos:" << start() << std::endl;
450  fragment( nr ).show( out, *this );
451  out << std::endl << std::endl;
452  // std::cerr << "FRAME::show_fragments done" << std::endl;
453  }
454 }
455 
456 bool Frame::merge( Frame const& other ) {
457  if ( !is_mergeable( other) ) return false;
458  //append fragdata
459  //copy cached data
460  Size insert_pos = frag_list_.size()+1;
461  Size other_frag_num = 1;
462  for ( FragDataList::const_iterator it = other.frag_list_.begin(),
463  eit = other.frag_list_.end(); it!=eit; ++it ) {
464  frag_list_.push_back( *it );
465  clone_cache_data( other, other_frag_num++, insert_pos++ );
466  }
467  return true;
468 }
469 
470 ///@brief change frames residue numbers according to map
472  Size s( map[ start() ] );
473  Size e( map[ end() ] );
474  bool success = ( s>0 && e>0 );
475  if ( is_continuous() && success ) {
476  success = ( (e - s + 1 ) == length() );
477  if ( !success ) tr.Trace << "failed to align frame " << start() << " " << end() << " different size after alignment" << std::endl;
478  //check that all residues within frame can be mapped -- or is that an unwanted restriction ?
479  for ( Size pos = start(); success && pos<=end(); pos ++ ) { // this should only happen if we hae insertion and deletion cancelling each other ?
480  success = map[ pos ] > 0;
481  if ( !success ) tr.Trace << "failed to align frame " << start() << " " << end() << " due to pos " << pos << std::endl;
482  }
483  }
484  if ( success ) {
485  start_ = s;
486  end_ = e;
487  }
488  return success;
489 }
490 
492  if ( !is_continuous() ) return NULL;
493  FrameOP sub_frame( clone() ); //creates empty frame with same type
494  sub_frame->start_ += start - 1;
495  sub_frame->nr_res_ = length;
496  sub_frame->end_ = sub_frame->start_ + length - 1;
497  for ( FragDataList::const_iterator it = frag_list_.begin(), eit = frag_list_.end();
498  it!=eit; ++it ) {
499  sub_frame->add_fragment( (*it)->generate_sub_fragment( start, start + length - 1 ) );
500  }
501  runtime_assert( nr_frags() == sub_frame->nr_frags() );
502  return sub_frame;
503 }
504 
505 bool Frame::steal( pose::Pose const& pose) {
506  runtime_assert( nr_frags() );
507  FragDataOP new_frag = frag_list_.front()->clone();
508  bool success ( new_frag->steal( pose, *this ) );
509  if ( success ) {
510  // check if first fragment was just an unitialized template...
511  if ( frag_list_.front()->is_valid() ) {
512  frag_list_.push_back( new_frag );
513  } else { // remove unitialized template and replace it with the new fragment
514  tr.Trace << "remove unitialized template FragData and replace it with stolen one" << std::endl;
515  runtime_assert( nr_frags() == 1 );
516  frag_list_.clear();
517  frag_list_.push_back( new_frag );
518  }
519  }
520  return success;
521 }
522 
523 void Frame::clear() {
524  cache_.clear();
525  if ( nr_frags() ) {
526  FragDataOP frag_template = frag_list_.front()->clone();
527  frag_list_.clear();
528  frag_template->set_valid( false ); //make it a template
529  add_fragment( frag_template );
530  }
531 }
532 
533 }
534 }