Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemplateJumpSetup.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 // Package Headers
20 
21 // Project Headers
22 #include <core/types.hh>
24 #include <core/fragment/FragSet.hh>
25 #include <core/fragment/Frame.hh>
30 
31 //numeric headers
32 #include <numeric/random/random.hh>
33 #include <numeric/random/random_permutation.hh>
34 
35 // Utility headers
36 #include <utility/io/izstream.hh>
37 #include <utility/vector1.hh>
38 #include <basic/Tracer.hh>
39 #include <utility/io/util.hh>
40 
41 // C++ headers
42 #include <cstdlib>
43 #include <string>
44 #include <vector>
45 
46 #include <utility/vector0.hh>
47 
48 
49 static basic::Tracer tr("protocols.abinitio.Templates");
50 
51 namespace protocols {
52 namespace abinitio {
53 
54 static numeric::random::RandomGenerator RG(1259812489); // <- Magic number, do not change it!
55 
56 using namespace core;
57 using namespace fragment;
58 using namespace jumping;
59 
62 
64  TemplatesCOP templates,
66  PairingStatisticsCOP strand_stats,
67  core::scoring::dssp::PairingsList const& helix_pairings
68 ) : templates_( templates ),
69  secstruct_( secstruct ),
70  strand_stats_( strand_stats ),
71  helix_pairings_( helix_pairings )
72 {}
73 
76  if ( templates_ ) {
77  tr.Debug << "create JumpSample from Templates: nres = " << templates_->target_total_residue() << "\n" << templates_->pairings() << std::endl;
78  }
79  runtime_assert( strand_stats_ );
80 
81  if ( !strand_stats_->nr_models() ) {
82  core::scoring::dssp::PairingList no_jumps; //create empty pairing list
83  return JumpSample( secstruct_->total_residue(), no_jumps, *secstruct_ );
84  }
85 
86  // pick a topology
87  std::string topol_id;
88  core::scoring::dssp::StrandPairingSet const& target_strands( strand_stats_->suggest_topology( topol_id ) );
89  tr.Debug << "topology selected! " << target_strands << std::endl;
90  if ( !target_strands.size() ) {
91  core::scoring::dssp::PairingList no_jumps; //create empty pairing list
92  return JumpSample( secstruct_->total_residue(), no_jumps, *secstruct_ );
93  }
94 
95  // generate random sequence from 1 .. N
96  utility::vector1< Size > strand_ids;
97  Real total_weight(0.0); //avoids nan
98  for ( Size i = 1; i <= target_strands.size(); i ++ ) {
99  total_weight += strand_stats_->strand_weight( target_strands.strand_pairing( i ) );
100  strand_ids.push_back( i );
101  }
102 
103  numeric::random::random_permutation( strand_ids, RG );
104  numeric::random::random_permutation( strand_ids, RG ); //want it really random
105  tr.Debug << "total weight: " << total_weight << " size: " << target_strands.size() << "strand_ids.size(): "
106  << strand_ids.size()
107  << " permutated sequence: ";
108  utility::io::write_vector( tr.Debug, strand_ids );
109  tr.Debug << std::endl;
110 
111  // take first 2..nr_strands strand_ids
112  Size const rg_nrstrands( std::min( static_cast< int >( RG.uniform() * (target_strands.size()) ) + 2 , (int) target_strands.size() ) );
113  tr.Debug << "random choice: aim for selection of " << rg_nrstrands << std::endl;
114  core::scoring::dssp::PairingList target_pairings;
115  Size nstrand_selected( 0 );
116  for ( Size i = 1; i<= target_strands.size(); i++ ) {
117  //decide if we pair this strand:
118  Real weight( strand_stats_->strand_weight( target_strands.strand_pairing( strand_ids[ i ] ) ) );
119  if ( total_weight > 0 && weight/total_weight < 0.005 ) continue;
120  nstrand_selected++;
121  tr.Debug << "pre-selected " << target_strands.strand_pairing( strand_ids[ i ] ) << std::endl;
122  core::scoring::dssp::PairingList possible_pairings;
123  target_strands.strand_pairing( strand_ids[ i ] ).get_beta_pairs( possible_pairings );
124  tr.Debug << "pre-selected " << target_strands.strand_pairing( strand_ids[ i ] )
125  << "provides: " << possible_pairings.size() << " pairings"<< std::endl;
126  // bias selection of individual pairing to center of strand
127  // we do that now simply by adding the center pairings twice
128  if ( possible_pairings.size() > 2 ) {
129  Size const ori_size = possible_pairings.size();
130  for ( Size i=2; i<=ori_size - 1; i++) {
131  possible_pairings.push_back( possible_pairings[ i ] );
132  }
133  }
134  Size const rg_pairing( static_cast< int > ( RG.uniform() * possible_pairings.size() ) + 1 );
135  tr.Debug << "RANDOM: choose pairing " << rg_pairing << std::endl;
136  //check if pairing is in templates...
137  bool found( false );
138  if ( templates_ ) {
139  for ( Templates::const_iterator it=templates_->begin(),
140  eit = templates_->end(); it!=eit && !found; ++it ) {
141  //check if at least 1 template has pairing .. we know all its pairings already aligned in targe-sequence...
142  // its in the strand_stats_
143  found = strand_stats_->topology( it->first ).has_pairing( possible_pairings[ rg_pairing ]);
144  }
145  }
146  if ( found || !templates_ ) target_pairings.push_back( possible_pairings[ rg_pairing ]);
147  }
148 
149  // go throu selected pairings and throw out stupid stuff...
150  // 1.) don't want to have two pairings with same register in close vicinity...
151  // 2.) don't want to apply very local pairings very often ... use only 20% of time
152  core::scoring::dssp::PairingList final_selection;
153  for ( core::scoring::dssp::PairingList::iterator it = target_pairings.begin(), eit = target_pairings.end();
154  it!=eit; ++it ) {
155 
156  bool ignore( false );
157 
158  // is a very short sequence separation?
159  if ( std::abs( (int) it->Pos2() - (int) it->Pos1() ) < 15 ) {
160  tr.Debug << "pairing " << *it << " is close in sequence..." << std::endl;
161  if ( RG.uniform() < 0.2 ) {
162  tr.Debug << "selected with 20% chance " << std::endl;
163  final_selection.push_back( *it );
164  } else ignore = true;
165  }
166 
167  // are already pairings selected with same register and not far away ?
168  Size const my_reg = it->get_register();
169 
170  for ( core::scoring::dssp::PairingList::iterator fit = final_selection.begin(), efit = final_selection.end();
171  fit!=efit && !ignore; ++fit ) {
172  if ( my_reg == fit->get_register() ) {
173  if ( std::abs( (int) it->Pos1() - (int) fit->Pos1() ) < 15
174  || std::abs( (int) it->Pos1() - (int) fit->Pos2() ) < 15
175  || std::abs( (int) it->Pos2() - (int) fit->Pos1() ) < 15
176  || std::abs( (int) it->Pos2() - (int) fit->Pos2() ) < 15 ) {
177  ignore = true;
178  }
179  }
180  }
181  if ( !ignore ) final_selection.push_back( *it );
182  else tr.Debug << "pairing " << *it << " ignored because it is redundant " << std::endl;
183  }
184 
185  if ( helix_pairings_.size() ) {
186  Size const nr_helix_jumps(
187  std::min( static_cast< int >( (RG.uniform()+0.3) * ( helix_pairings_.size() ) + 1 ), (int) helix_pairings_.size() ) );
189  numeric::random::random_permutation( perm , RG );
190  for ( Size i = 1; i<= nr_helix_jumps; i++ ) {
191  final_selection.push_back( helix_pairings_[ i ] );
192  }
193  }
194  tr.Debug << "selected jumps: " << final_selection << std::endl;
195  // TODO in distant future: take care of bulges if there are any:
196  return JumpSample( secstruct_->total_residue(), final_selection, *secstruct_ );
197 }
198 
200 TemplateJumpSetup::clean_jumps( JumpSample const& target_jumps ) const {
201  runtime_assert( strand_stats_ );
202 
203  if ( !strand_stats_->nr_models() ) {
204  core::scoring::dssp::PairingList no_jumps; //create empty pairing list
205  return jumping::JumpSample( secstruct_->total_residue(), no_jumps, *secstruct_ );
206  }
207 
208  core::scoring::dssp::PairingList filtered_jumps;
209 
210  core::fragment::FrameList jump_frames;
212  mm.set_bb( true );
213  mm.set_jump( true );
214 
215  target_jumps.generate_jump_frames( jump_frames,mm );
216 
217  // treat each jump individually ---> want to select frags only from templates that have compatible pairings
218  for ( FrameList::iterator jump_frame = jump_frames.begin();
219  jump_frame != jump_frames.end(); ++jump_frame ) {
220  core::scoring::dssp::Pairing target_pairing( target_jumps.get_pairing( (*jump_frame)->start(), (*jump_frame)->stop() ) );
221  bool found( false );
222  for ( PairingStatistics::const_iterator it = strand_stats_->begin(); !found && it != strand_stats_->end(); ++it ) {
223  found = it->second.pairing().has_pairing( target_pairing );
224  }
225  if ( found ) {
226  filtered_jumps.push_back( target_pairing );//add
227  }
228  }
229  return jumping::JumpSample( secstruct_->total_residue(), filtered_jumps, *secstruct_ );
230 }
231 
233  core::scoring::dssp::PairingList::const_iterator iter = find( helix_pairings_.begin(), helix_pairings_.end(), p );
234  if ( iter != helix_pairings_.end() ) return true;
236  rev.reverse();
237  iter = find( helix_pairings_.begin(), helix_pairings_.end(), rev );
238  return ( iter != helix_pairings_.end() );
239 }
240 
241 ///@brief returns an ordered FragSet that is compatible with the JumpSample
242 /// default: generate jumps from ss-library according to JumpSample
245  tr.Debug <<" generate jump fragments... " << std::endl;
246  FragSetOP jump_frags = new OrderedFragSet;
247  //generate fragments from all homologes
248  core::fragment::FrameList jump_frames;
249  core::scoring::dssp::PairingsList library_pairings;
250 
251  target_jumps.generate_jump_frames( jump_frames,mm );
252 
253  // treat each jump individually ---> want to select frags only from templates that have compatible pairings
254  for ( FrameList::iterator jump_frame = jump_frames.begin();
255  jump_frame != jump_frames.end(); ++jump_frame ) {
256  core::scoring::dssp::Pairing target_pairing( target_jumps.get_pairing( (*jump_frame)->start(), (*jump_frame)->stop() ) );
257  Size nr_frags( 0 );
258  tr.Debug << "get frags for pairing " << target_pairing << std::endl;
259  if ( templates_ ) {
260  if ( !is_helix_jump( target_pairing ) ) {
261  for ( Templates::const_iterator it=templates_->begin(),
262  eit = templates_->end(); it!=eit; ++it ) {
263 
264  //check if template has pairing .. we know all its pairings already aligned in targe-sequence...
265  // its in the strand_stats_
266  std::string const& model_id( it->first );
267  if ( strand_stats_->topology( model_id ).has_pairing( target_pairing ) ) {
268  nr_frags++;
269  FrameList aFrame; aFrame.push_back( *jump_frame );
270  it->second->steal_frags( aFrame, *jump_frags );
271  }
272  }
273  } else { //get here if pairing is helix jump
274  tr.Debug << "has been found in helix-list blindly collect all jump-geometries from models with an H" << std::endl;
275  FrameList aFrame; aFrame.push_back( *jump_frame );
276  for ( Templates::TemplateList::const_iterator it = templates_->helixjump_picks().begin(),
277  eit = templates_->helixjump_picks().end(); it != eit; ++it ) {
278  nr_frags++;
279  (*it)->steal_frags( aFrame, *jump_frags );
280  }
281  }
282  }
283  if ( nr_frags == 0 && templates_ ) {
284  utility_exit_with_message("selected a pairing for which no pairing could be found in templates... shouldn't ever happen");
285  }
286  if ( nr_frags == 0 ) {
287  //use standard ss-library
288  library_pairings.push_back( target_pairing );
289  }
290  }
291 
293  library_pairings,
294  mm,
295  true /*with Torsion*/,
296  *jump_frags
297  );
298 
299  return jump_frags; //it is better to generate each time, since JumpingFoldConstraints might add other fragments ( steal native etc )
300 }
301 
303  TemplatesCOP templates,
305  PairingStatisticsCOP strand_stats,
306  core::scoring::dssp::PairingsList const& helix_pairings,
307  BaseJumpSetupOP jump_def
308 ) : TemplateJumpSetup( templates , secstruct, strand_stats, helix_pairings ),
309  jump_def_( jump_def )
310 {}
311 
313  TemplateJumpSetup const& templ,
314  BaseJumpSetupOP jump_def
315 ) : TemplateJumpSetup( templ ),
316  jump_def_( jump_def )
317 {}
318 
321  return jump_def_->create_jump_sample();
322 }
323 
324 } //abinitio
325 } //protocols
326 
327 #if 0
328  // take first 2..nr_strands strand_ids
329  Size const rg_nrstrands( std::min( static_cast< int >( RG.uniform() * (target_strands.size()) ) + 2 , (int) target_strands.size() ) );
330  tr.Debug << "random choice: aim for selection of " << rg_nrstrands << std::endl;
331  core::scoring::dssp::PairingList target_pairings;
332  Size nstrand_selected( 0 );
333  for ( Size i = 1; i<= target_strands.size(); i++ ) {
334  //decide if we pair this strand:
335  Real weight( strand_stats_->strand_weight( target_strands.strand_pairing( strand_ids[ i ] ) ) );
336  if ( total_weight > 0 && weight*rg_nrstrands / total_weight < RG.uniform() ) continue;
337  nstrand_selected++;
338  tr.Debug << "pre-selected " << target_strands.strand_pairing( strand_ids[ i ] ) << std::endl;
339  core::scoring::dssp::PairingList possible_pairings;
340  target_strands.strand_pairing( strand_ids[ i ] ).get_beta_pairs( possible_pairings );
341  tr.Debug << "pre-selected " << target_strands.strand_pairing( strand_ids[ i ] )
342  << "provides: " << possible_pairings.size() << " pairings"<< std::endl;
343  // bias selection of individual pairing to center of strand
344  // we do that now simply by adding the center pairings twice
345  if ( possible_pairings.size() > 2 ) {
346  Size const ori_size = possible_pairings.size();
347  for ( Size i=2; i<=ori_size - 1; i++) {
348  possible_pairings.push_back( possible_pairings[ i ] );
349  }
350  }
351  Size const rg_pairing( static_cast< int > ( RG.uniform() * possible_pairings.size() ) + 1 );
352  tr.Debug << "RANDOM: choose pairing " << rg_pairing << std::endl;
353  //check if pairing is in templates...
354  bool found( false );
355  if ( templates_ ) {
356  for ( Templates::const_iterator it=templates_->begin(),
357  eit = templates_->end(); it!=eit && !found; ++it ) {
358  //check if at least 1 template has pairing .. we know all its pairings already aligned in targe-sequence...
359  // its in the strand_stats_
360  found = strand_stats_->topology( it->first ).has_pairing( possible_pairings[ rg_pairing ]);
361  }
362  }
363 
364  if ( found || !templates_ ) target_pairings.push_back( possible_pairings[ rg_pairing ]);
365  }
366 
367  // go throu selected pairings and throw out stupid stuff...
368  // 1.) don't want to have two pairings with same register in close vicinity...
369  // 2.) don't want to apply very local pairings very often ... use only 20% of time
370 #endif