Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Template.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 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @author Oliver Lange
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 // Unit Headers
15 
16 // Project Headers
17 #include <core/types.hh>
19 #include <core/pose/Pose.hh>
21 #include <core/id/AtomID.hh>
22 #include <basic/options/option.hh>
23 #include <basic/options/keys/templates.OptionKeys.gen.hh>
24 #include <core/fragment/FragSet.hh>
25 
26 #ifdef WIN32
27 #include <core/fragment/FragID.hh>
28 #endif
29 
35 #include <core/sequence/util.hh>
37 
38 // ObjexxFCL Headers
39 #include <ObjexxFCL/format.hh>
40 
41 // Utility headers
42 #include <utility/vector1.hh>
43 #include <basic/Tracer.hh>
44 
45 // C++ headers
46 #include <cstdlib>
47 #include <string>
48 #include <vector>
49 
51 #include <core/fragment/Frame.hh>
55 #include <fstream>
56 
57 static THREAD_LOCAL basic::Tracer tr( "protocols.abinitio.Templates" );
58 using namespace core;
59 using namespace basic;
60 using namespace basic::options;
61 using namespace ObjexxFCL::format;
62 
64  using namespace basic::options;
65  using namespace basic::options::OptionKeys;
66  option.add_relevant( templates::min_frag_size );
67  option.add_relevant( templates::max_shrink );
68  option.add_relevant( templates::shrink_step );
69  option.add_relevant( templates::shrink_pos_step );
70  option.add_relevant( templates::min_padding );
71  option.add_relevant( templates::min_align_pos );
72  option.add_relevant( templates::max_align_pos );
73 }
74 
75 namespace protocols {
76 namespace abinitio {
77 
78 /// @details Auto-generated virtual destructor
79 Template::~Template() {}
80 
81 using namespace core;
82 using namespace fragment;
83 using namespace scoring::constraints;
84 
85 void
86 dump_movemap( kinematics::MoveMap const& mm, Size nres, std::ostream& out ) {
87  for ( Size i = 1; i<=nres; i++ ) {
88  if ( (i-1)%10 == 0 ) { out << i; continue; }
89  //large numbers take several characters... skip appropriate
90  if ( (i>=10) && (i-2)%10 == 0 ) { continue; }
91  if ( (i>=100) && (i-3)%10 == 0 ) { continue; }
92  if ( (i>=1000) && (i-4)%10 == 0 ) { continue; }
93  out << ".";
94  }
95  out << std::endl;
96  for ( Size i = 1; i<=nres; i++ ) {
97  if ( mm.get_bb( i ) ) out << 'F'; //cuttable
98  else out << '.';
99  }
100  out << std::endl;
101 }
102 
103 Template::Template( std::string const& name, pose::PoseCOP pose, core::sequence::DerivedSequenceMapping const& mapping )
104 : name_ ( name )
105 {
106  tr.Error << "STUB ERROR: Template::Template(pose, mapping) constructure, not really finished yet !!! " << std::endl;
107  pose_ = pose;
108  mapping_ = mapping;
109  reverse_mapping_ = mapping;
111  good_ = false;
112 }
113 
115  std::string const& name,
116  pose::PoseCOP pose,
117  std::string const& map_file,
118  int offset,
119  Real score )
120 : pose_( pose ),
121  name_( name ),
122  score_( score )
123 {
124  using namespace basic::options;
125  using namespace basic::options::OptionKeys;
126 
127  good_ = false;
128 
129  // read alignment
130  tr.Info << "template " << name << " read alignment file: " << map_file << std::endl;
132 
133  // offset it because pdb starts at offset rather than sequence pos nr 1
134  if ( offset >= 0 ) { //ignore negative offsets -- in agreement with myself
135  mapping_.set_offset( offset-1 ); //offset 1 --> do nothing
136  } else { //find offset automatically
137  std::string pdb_seq = pose_->sequence();
138  size_t found = pdb_seq.find( mapping_.seq2().substr(0,25) ); // look where query sequence starts in pdb-file
139  if ( found!=std::string::npos ) { //found
140  offset = mapping_.start_seq2() - found;
141  mapping_.set_offset( offset-1 );
142  } else {
143  tr.Error << "query sequence could not be found for model " + name << std::endl;
144  tr.Error << "pdb_seq: " << pdb_seq << std::endl;
145  tr.Error << "mapping_.seq2()" << mapping_.seq2() << std::endl;
146  std::ofstream out( "BAD_SEQUENCES", std::ios_base::out | std::ios_base::app );
147  out << name << " "
148  << " Expecting: " << mapping_.seq2()
149  << " PDBseq: " << pdb_seq << std::endl;
150  return ;
151  }
152  }
153 
154  if ( option[ templates::min_align_pos ].user() ) {
155  Size const min_align_pos( option[ templates::min_align_pos ] );
156  for ( Size pos = 1; pos < min_align_pos && pos <= mapping_.size1(); ++pos ) {
157  mapping_[ pos ] = 0;
158  }
159  }
160 
161  if ( option[ templates::max_align_pos ].user() ) {
162  Size const max_align_pos( option[ templates::max_align_pos ] );
163  for ( Size pos = max_align_pos+1; pos <= mapping_.size1(); ++pos ) {
164  mapping_[ pos ] = 0;
165  }
166  }
167 
168  // get the reverse mapping: template --> target
171 
172  // get strand pairings
174  tr.Info << "strand pairings \n" << *strand_pairings_;
175 
176  // for shits and giggles -- write out the pairings for the target
177  core::scoring::dssp::PairingList template_pairings, target_pairings;
178  strand_pairings().get_beta_pairs( template_pairings );
179  map_pairings2target( template_pairings, target_pairings );
180  core::scoring::dssp::StrandPairingSet target_strand_pairings( target_pairings );
181  tr.Info << " strand_pairings of " << name << " aligned to target \n" << target_strand_pairings << std::endl;
182 
183  // for information purpose: write sequence where alignment starts. should be the same as seen in hhr file
184  std::string seq = pose_->sequence();
185  Size tpos, pos = 1; while ( (tpos = mapping_[ pos++ ])<=0 ) ; //go to first aligned residue
186  tr.Info << "template sequence " << seq.substr( tpos-1 ) << std::endl; //string count from 0
187  tr.Debug <<"compare with " << mapping_.seq2() << std::endl;
188  tr.Info << "first 10 aligned residues:" << std::endl;
189  for ( Size i = pos-1; i<= pos + 9; i++ ) {
190  tr.Info << i << " " << mapping_[ i ] << " " << ( mapping_[ i ] ? pose_->residue( mapping_[i] ).name1() : '_' ) << std::endl;
191  }
192  tr.Trace << mapping_;
193 
194  good_ = true;
195 }
196 
197 Size
198 Template::pick_large_frags( FragSet& frag_set, core::fragment::SingleResidueFragDataOP srfd_type, Size ncopies ) const {
199  // get vector of boolean for residues that are aligned
200  // change that into vector of max_frag_length
201  // [ a a _ _ a a a a a _ _ a ] --> 2 1 0 0 5 4 3 2 1 0 0 x
202  typedef utility::vector1< Size > FragLengthMap;
203  FrameList template_frames;
204  Size const nres( std::min( pose_->total_residue(), reverse_mapping_.size1() ) );
205  FragLengthMap frag_length( nres, 0);
206  for ( Size pos1 = nres; pos1 >= 2; pos1-- ) {
207  //if pos1 is aligned .. assign appropriate lengths for each residue in continuous stretch, e.g., 5 4 3 2 1
208  if ( reverse_mapping_[ pos1 ] ) {
209  Size cont_length( 0 );
210  for ( Size pos2 = pos1;
211  pos2 >= 2 && //avoids out-of-range in reverse_mapping_[ pos2 - 1 ] ...
212  reverse_mapping_[ pos2 ]; // &&
213  pos2-- ) {
214  cont_length++;
215  frag_length[ pos2 ] = cont_length;
216  if ( reverse_mapping_[ pos2 - 1 ] + 1 != reverse_mapping_[ pos2 ] ) break;
217  }
218  //forward outer loop to next unknown position
219  pos1 = pos1 - cont_length + 1; //+ 1 since at end of loop there will be a pos1-- ...
220  } // if pos1 aligned
221  }
222  if ( reverse_mapping_[ 1 ] ) {
223  frag_length[ 1 ] = frag_length[ 2 ] + 1;
224  }
225  if ( tr.Trace.visible() ) {
226  tr.Trace << "frag_lengths:\n pos length";
227  for ( Size pos = 1; pos <= nres; pos++ ) {
228  tr.Trace << "\n" << pos << " " << frag_length[ pos ];
229  }
230  tr.Trace << std::endl; //flush
231  tr.Trace << "reverse mapping " << reverse_mapping_ << std::endl;
232  }
233 
234  using namespace basic::options;
235  using namespace basic::options::OptionKeys;
236  Size const min_size( option[ templates::min_frag_size ] );
237  Size const min_padding( option[ templates::min_padding ] ); //min_padding residues distance to gaps
238  Size const opt_max_shrink( option[ templates::max_shrink ] );
239  Size const shrink_step( option[ templates::shrink_step ]);
240  Size const pos_step( option[ templates::shrink_pos_step ]);
241  // for each position pick largest possible fragment and a couple of smaller ones...
242  // have some option to control this.
243  for ( Size pos = 1; pos<= nres; pos += std::max( (int) frag_length[ pos ], 1) ) {
244  Size const max_size( frag_length[ pos ] );
245  if ( max_size < min_size ) continue;
246  Size const max_shrink( std::min( (int) opt_max_shrink, (int) max_size - (int) min_size ) );
247  // take whole aligned region and up to max_shrink smaller fragments
248  for ( Size shrink = min_padding; shrink <= max_shrink; shrink+=shrink_step ) {
249  Size const size( max_size - shrink );
250 
251  // a fragment of requested size
252  FragDataCOP frag_type( FragDataOP( new AnnotatedFragData( name(), pos, FragData( srfd_type, size )) ) );
253 
254  // pick this fragment for different frame positions and add to template_frames.
255  for ( Size offset = min_padding; offset <= max_size - size; offset+=pos_step ) {
256  FrameOP aFrame( new Frame( pos + offset, frag_type ) );
257  for ( Size i = 1; i<=ncopies; i++ ) {
258  aFrame->steal( *pose_ );
259 
260  }
261  template_frames.push_back( aFrame );
262  }
263  }
264  }
265  tr.Trace << template_frames << std::endl;
266  map2target( template_frames );
267  frag_set.add( template_frames );
268  return template_frames.size();
269 }
270 
271 //@detail pick ncopies identical fragments from template ( default ncopies = 1, higher number allow to give template frags higher weight )
272 // until proper weighting in fragments is set up
273 Size
274 Template::steal_frags( FrameList const& frames, FragSet &accumulator, Size ncopies ) const {
275  FrameList template_frames;
276  map2template( frames, template_frames );
277  Size total( 0 );
278  for ( FrameList::iterator it = template_frames.begin(),
279  eit = template_frames.end(); it!=eit; ++it ) {
280  tr.Trace << name() << " pick frags at " << (*it)->start() << " " << (*it)->end() << " " << pose_->total_residue() << std::endl;
281  for ( Size ct = 1; ct <= ncopies; ct ++ ) {
282  if ( (*it)->steal( *pose_ ) ) total++;
283  }
284  }
285 
286  //this should always work, since we created only alignable frames
287  map2target( template_frames );
288  accumulator.add( template_frames );
289  return total;
290 }
291 
292 bool
294  out.Pos1(map[in.Pos1()]);
295  out.Pos2(map[in.Pos2()]);
296  out.Orientation(in.Orientation());
297  out.Pleating(in.Pleating()); // how is this different from calling out = in?
298  return !( out.Pos1() <= 0 || out.Pos2() <= 0 );
299 }
300 
301 void
303  for ( core::scoring::dssp::PairingsList::const_iterator it = in.begin(),
304  eit = in.end(); it!=eit; ++it ) {
306  if ( map_pairing( *it, pairing, mapping_ ) ) out.push_back( pairing );
307  }
308 }
309 
310 void
312  for ( core::scoring::dssp::PairingsList::const_iterator it = in.begin(),
313  eit = in.end(); it!=eit; ++it ) {
315  if ( map_pairing( *it, pairing, reverse_mapping_ ) ) out.push_back( pairing );
316  tr.Trace << "template: "<<*it<< " target: " << pairing;
317  }
318 }
319 
320 void
321 Template::map2target( FrameList const& frames, FrameList& target_frames ) const {
322  for ( FrameList::const_iterator it=frames.begin(),
323  eit = frames.end(); it!=eit; ++it ) {
324  FrameOP frame = (*it)->clone_with_frags();
325  if ( frame->align( reverse_mapping_ ) ) {
326  target_frames.push_back( frame );
327  }
328  }
329 }
330 
331 void
332 Template::map2target( FrameList &frames ) const {
333  for ( FrameList::const_iterator it=frames.begin(),
334  eit = frames.end(); it!=eit; ++it ) {
335  FrameOP frame = (*it);
336  Size start( frame->start() );
337  tr.Trace << start << " " << frame->end() << std::endl;
338  if ( !frame->align( reverse_mapping_ ) ) {
339  std::cerr << start << " could not align frame " << *frame << std::endl;
340  utility_exit_with_message("Could not align frame ");
341  }
342  }
343 }
344 
345 void
346 Template::map2template( FrameList &frames ) const {
347  for ( FrameList::const_iterator it=frames.begin(),
348  eit = frames.end(); it!=eit; ++it ) {
349  if ( !(*it)->align( mapping_ ) ) {
350  tr.Error << "could not align frame " << **it << std::endl;
351  utility_exit_with_message("Could not align frame ");
352  }
353  }
354 }
355 
356 void
357 Template::map2template( FrameList const& target_frames, FrameList& template_frames ) const {
358  for ( FrameList::const_iterator it=target_frames.begin(),
359  eit = target_frames.end(); it!=eit; ++it ) {
360  FrameOP frame = (*it)->clone();
361  *frame = **it; //copy fragments
362  if ( frame->align( mapping_ ) ) {
363  template_frames.push_back( frame );
364  }
365  }
366 }
367 
368 using namespace core::scoring::constraints;
369 
370 void Template::read_constraints( std::string const& cst_file ) {
371  cstfile_ = cst_file; //lazy read
372 }
373 
374 void Template::_read_constraints( std::string const& cst_file ) const {
375  ConstraintSetOP cstset = ConstraintIO::get_instance()->read_constraints( cst_file, ConstraintSetOP( new ConstraintSet ), *pose_ );
376  typedef utility::vector1< ConstraintCOP > FlatList;
377  FlatList all_cst = cstset->get_all_constraints();
378  for ( FlatList::const_iterator it = all_cst.begin(),
379  eit = all_cst.end(); it!=eit; ++it ) {
380  ConstraintCOP ptr = *it;
381  AtomPairConstraintCOP ptr2 = utility::pointer::dynamic_pointer_cast< core::scoring::constraints::AtomPairConstraint const > ( ptr );
382  if ( ptr2 ) {
383  AtomPairConstraintOP valued_cst = utility::pointer::const_pointer_cast< AtomPairConstraint >(ptr2);
385  } else {
386  tr.Warning << "WARNING: constraint found that is not AtomPairConstraint... will be ignored by Template" << std::endl;
387  }
388  }
389 }
390 
391 void
393  NamedAtomPairConstraintList const& template_list,
394  NamedAtomPairConstraintList& target_list ) const
395 {
396  using namespace core::id;
397 
398  target_list.clear();
399  for ( NamedAtomPairConstraintList::const_iterator it = template_list.begin(),
400  eit = template_list.end(); it!=eit; ++it ) {
401  Obsolet_NamedAtomPairConstraintOP new_cst = (*it)->mapto( reverse_mapping_ );
402  if ( new_cst ) {
403  target_list.push_back( new_cst );
404  } else {
405  tr.Trace << "map2target: could not align constraint " << **it << std::endl;
406  }
407  }
408 }
409 
410 void
412  NamedAtomPairConstraintList const& target_list,
413  NamedAtomPairConstraintList& template_list ) const
414 {
415  using namespace core::id;
416  template_list.clear();
417  for ( NamedAtomPairConstraintList::const_iterator it = target_list.begin(),
418  eit = target_list.end(); it!=eit; ++it ) {
419  Obsolet_NamedAtomPairConstraintOP new_cst = (*it)->mapto( mapping_ );
420  if ( new_cst ) {
421  template_list.push_back( new_cst );
422  } else {
423  tr.Trace << "map2template: could not align constraint " << **it << std::endl;
424  }
425  }
426 }
427 
428 void
430  NamedAtomPairConstraintList const& target_list,
431  NamedAtomPairConstraintList& culled_list ) const
432 {
433  using namespace core::scoring::constraints;
434  for ( NamedAtomPairConstraintList::const_iterator it = target_list.begin(),
435  eit = target_list.end(); it!=eit; ++it ) {
436  AtomPairConstraintOP cst = (*it)->mapto( mapping_, *pose_ );
437  if ( cst ) {
438  tr.Trace << "test: (target)" << cst->atom(1) << " " << cst->atom(2) << std::endl;
439  if ( cst->score( *pose_ ) < 1.0 ) {
440  culled_list.push_back( *it );
441  } else {
442  tr.Trace <<"cull constraint with score: " << cst->score( *pose_ ) << " "
443  << cst->atom(1) << " " << cst->atom(2) << std::endl;
444  } // cull this
445  } else { //atoms present
446  culled_list.push_back( *it ); //constraint cannot be tested on template... keep it !
447  }
448  }
449 }
450 
451 } //abinitio
452 } //protocols
core::scoring::dssp::StrandPairingSetOP strand_pairings_
Definition: Template.hh:162
core::sequence::DerivedSequenceMapping reverse_mapping_
Definition: Template.hh:152
void map2target(core::fragment::FrameList const &template_frames, core::fragment::FrameList &target_frames) const
core::sequence::DerivedSequenceMapping mapping_
Definition: Template.hh:151
void cull_violators(NamedAtomPairConstraintList const &target_list, NamedAtomPairConstraintList &culled_list) const
Definition: Template.cc:429
void map_pairings2target(core::scoring::dssp::PairingList const &in, core::scoring::dssp::PairingList &out) const
Definition: Template.cc:311
Declarations for the MoveMap class.
utility::pointer::shared_ptr< AtomPairConstraint > AtomPairConstraintOP
utility::pointer::shared_ptr< FragData const > FragDataCOP
Definition: FragData.fwd.hh:31
utility::pointer::shared_ptr< SingleResidueFragData > SingleResidueFragDataOP
core::sequence::DerivedSequenceMapping simple_mapping_from_file(std::string const &filename)
Read in a SequenceMapping from a file. File format is super-simple, it just contains single lines lik...
Definition: util.cc:205
A class specifying DOFs to be flexible or fixed.
Definition: MoveMap.hh:87
utility::pointer::shared_ptr< Pose const > PoseCOP
Definition: Pose.fwd.hh:28
utility::pointer::shared_ptr< Constraint const > ConstraintCOP
static THREAD_LOCAL basic::Tracer tr("protocols.abinitio.Templates")
utility::vector1< Pairing > PairingList
utility::pointer::shared_ptr< ConstraintSet > ConstraintSetOP
platform::Size Size
Definition: types.hh:30
Declarations for the TorsionID class.
void set_offset(int setting)
Definition for functions used in definition of constraints.
rosetta project type declarations
static void register_options()
Definition: Template.cc:63
Template(std::string const &name, core::pose::PoseCOP, core::sequence::DerivedSequenceMapping const &mapping)
Definition: Template.cc:103
utility::vector1< Pairing > PairingsList
NamedAtomPairConstraintList cstset_
Definition: Template.hh:160
utility::pointer::shared_ptr< Obsolet_NamedAtomPairConstraint > Obsolet_NamedAtomPairConstraintOP
utility::vector1< core::scoring::constraints::Obsolet_NamedAtomPairConstraintOP > NamedAtomPairConstraintList
Definition: Template.hh:59
platform::Real Real
Definition: types.hh:35
void reverse()
go from an A->B mapping to a B->A mapping
std::string const & name() const
Definition: Template.hh:125
core::scoring::dssp::StrandPairingSet const & strand_pairings() const
Definition: Template.hh:113
utility::pointer::shared_ptr< FragData > FragDataOP
Definition: FragData.fwd.hh:28
core::pose::PoseCOP pose_
Definition: Template.hh:155
Method declarations and simple accessor definitions for the Residue class.
void get_beta_pairs(core::scoring::dssp::PairingList &) const
Size pick_large_frags(core::fragment::FragSet &frag_set, core::fragment::SingleResidueFragDataOP frag_type, core::Size ncopies=1) const
Definition: Template.cc:198
bool map_pairing(core::scoring::dssp::Pairing const &, core::scoring::dssp::Pairing &, core::sequence::DerivedSequenceMapping const &map) const
Definition: Template.cc:293
Size size1() const
size of target sequence
utility::pointer::shared_ptr< StrandPairingSet > StrandPairingSetOP
utility::pointer::shared_ptr< Frame > FrameOP
Definition: Frame.fwd.hh:28
void _read_constraints(std::string const &cst_file) const
Definition: Template.cc:374
bool get_bb(Size const seqpos) const
Returns if BB torsions are movable or not for residue
Definition: MoveMap.hh:524
void read_constraints(std::string const &cst_file)
Definition: Template.cc:370
This is quite possibly the laziest form of class naming ever. What does the name of this class tell y...
Size steal_frags(core::fragment::FrameList const &target_frames, core::fragment::FragSet &accumulator, Size ncopies=1) const
good for continuous fragments: no torsions from insertions are mapped
Definition: Template.cc:274
void map_pairings2template(core::scoring::dssp::PairingList const &in, core::scoring::dssp::PairingList &out) const
Definition: Template.cc:302
utility::pointer::shared_ptr< AtomPairConstraint const > AtomPairConstraintCOP
void dump_movemap(kinematics::MoveMap const &mm, Size nres, std::ostream &out)
Definition: Template.cc:86
void map2template(core::fragment::FrameList const &target_frames, core::fragment::FrameList &template_frames) const
Pose class.