Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JumpSetup.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 /// @file
11 /// @brief
12 /// @detailed
13 /// @author Oliver Lange
14 ///
15 
16 
17 // Unit Headers
19 
20 // Package Headers
21 
22 // Project Headers
23 #include <core/pose/Pose.hh>
24 // AUTO-REMOVED #include <core/pose/util.hh>
25 
27 // AUTO-REMOVED #include <core/kinematics/ShortestPathInFoldTree.hh>
28 
30 #include <core/fragment/Frame.hh>
31 // AUTO-REMOVED #include <core/fragment/JumpingFrame.hh>
33 #ifdef WIN32
34 #include <core/fragment/FragID.hh>
35 #endif
36 // AUTO-REMOVED
37 
38 //#include <core/scoring/constraints/ConstraintForest.hh>
39 
40 // ObjexxFCL Headers
41 #include <ObjexxFCL/format.hh>
42 
43 // Utility headers
44 #include <basic/Tracer.hh>
45 #include <utility/io/izstream.hh>
46 #include <utility/vector1.hh>
47 
48 //numeric headers
49 #include <numeric/random/random.hh>
50 
51 //// C++ headers
52 #include <cstdlib>
53 #include <string>
54 #include <fstream>
55 
56 
57 
58 namespace protocols {
59 namespace jumping {
60 
61 /// @details Auto-generated virtual destructor
63 
64 using namespace core;
65 using namespace fragment;
66 using namespace ObjexxFCL;
67 
68 static basic::Tracer tr("protocols.jumping");
69 
70 
73  OrderedFragSetOP frags = new OrderedFragSet;
74  FrameList jump_geometries;
76  mm,
77  true, /*bWithBBTorsions*/
78  jump_geometries
79  );
80  frags->add( jump_geometries );
81  return frags;
82 }
83 
84 void
86  utility::io::izstream data( fname.c_str() );
87  tr.Info << "read jump-definitions from " << fname << std::endl;
88  if ( !data ) {
89  std::cerr << "ERROR:: Unable to open constraints file: "
90  << fname << std::endl;
91  std::exit( 1 );
92  }
93 
94  std::string line;
95 
96  while ( getline(data, line ) ) {
97  std::istringstream in( line );
98  Interval jump, cuts;
99  in >> jump.start_ >> jump.end_ >> cuts.start_;
100  if ( in ) {
101  in >> cuts.end_;
102  } else {
103  cuts.end_=cuts.start_;
104  }
105  // if ( in ) {
106  // tr.Warning << "Ignore remaining line: " << line << std::endl;
107  // }
108  using namespace ObjexxFCL::fmt;
109  tr.Debug << "read Jumps: " << RJ(4, jump.start_) << RJ( 4, jump.end_ ) << RJ( 4, cuts.start_ ) << RJ( 4, cuts.end_ )<< std::endl;
110  add_jump( jump, cuts );
111  }
112 
113 }
114 
115 //* ----------------------- JumpSelector ----------------------------*
116 
118  : total_weight_( 0 ), min_loop_length_( 5 ), loop_extension_(2), nr_jumps_min_(3), nr_jumps_max_(6) {}
120  : secstruct_( ss), total_weight_( 0 ), min_loop_length_( 5 ), loop_extension_(2), nr_jumps_min_(3), nr_jumps_max_(6) {}
121 
123 
124 
125 void
127  utility::io::izstream data( fname.c_str() );
128  tr.Info << "read jump-definitions from " << fname << std::endl;
129  if ( !data ) {
130  utility_exit_with_message("ERROR: could not open file " + fname );
131  }
132 
133  std::string line;
134 
135  while ( getline(data, line ) ) {
136  if ( line.find("nr_jumps") !=std::string::npos ) {
137  std::istringstream in( line );
138  std::string tag;
139  in >> tag >> nr_jumps_min_ >> nr_jumps_max_;
140  tr.Debug << "read nr_jumps: min " << nr_jumps_min_ << "... max " << nr_jumps_max_ << std::endl;
141  continue;
142  } else if ( line.find("loop_params") !=std::string::npos ) {
143  std::istringstream in( line );
144  std::string tag;
145  in >> tag >> min_loop_length_ >> loop_extension_;
146  tr.Debug << "read min_loop_length: " << min_loop_length_ << " loop_extension " << loop_extension_ << std::endl;
147  continue;
148  }
149  std::istringstream in( line );
150  Interval jump; Real weight;
151  in >> jump.start_ >> jump.end_ >> weight;
152  if ( jump.start_ > jump.end_ ) {
153  Size dum = jump.start_;
154  jump.start_ = jump.end_;
155  jump.end_ = dum;
156  };
157  using namespace ObjexxFCL::fmt;
158  tr.Debug << "read Jumps: " << RJ(7, jump.start_) << RJ( 7, jump.end_ ) << RJ( 7, weight ) <<std::endl;
159  add_jump( jump, weight );
160  }
161 }
162 
163 void
164 dump_tags_( FArray1D_int const& tags, Size nres, std::ostream& out ) {
165  out << "r_fold_cst: ";
166  for ( Size i = 1; i<=nres; i++ ) {
167  if ( (i-1)%10 == 0 ) { out << i; continue; }
168  //large numbers take several characters... skip appropriate
169  if ( (i>=10) && (i-2)%10 == 0 ) { continue; }
170  if ( (i>=100) && (i-3)%10 == 0 ) { continue; }
171  if ( (i>=1000) && (i-4)%10 == 0 ) { continue; }
172  out << ".";
173  }
174  out << "\nr_fold_cst: ";
175  for ( Size i = 1; i<=nres; i++ ) {
176  if ( tags( i ) <= -10 ) out << 'C'; //cuttable
177  if ( tags( i ) == -1 ) out << 'L'; //short loop
178  if ( tags( i ) == 0 ) out << '.';
179  if ( tags( i ) > 0 ) out << tags( i );
180  }
181  out << std::endl;
182 }
183 
184 void
185 fill_tags_( FArray1D_int& tags, Size fill_pos, int tag, Size nres ) {
186  Size pos = fill_pos;
187  while ( tags( pos ) >= -1 ) {
188  tags( pos ) = tag;
189  if ( --pos == 0 ) break;
190  //dump_tags_( tags, nres, std::cout );
191  }
192 
193  runtime_assert( pos == 0 || tags( pos ) == -10 ); //it should always be stopped by loop
194  pos = fill_pos;
195  while ( tags( pos ) >= -1 ) {
196  tags( pos++ ) = tag;
197  if ( pos > nres ) break;
198  // dump_tags_( tags, nres, std::cout );
199  }
200  runtime_assert( pos > nres || tags( pos ) <= -10 );
201 }
202 
203 
204 // Utility headers
205 static numeric::random::RandomGenerator RG(489134); // <- Magic number, do not change it!
206 
207 JumpSample
209  typedef utility::vector1< Interval > CutList;
211  JumpList jump_list;
212  //init tag-array
213  // negative for loop regions
214  // -1 for short loops(no jumps), -10 for long loops (cuttable)
215  // 0 for free regions
216  runtime_assert( min_loop_length_ > 0 );
217 
218  Size nr_jumps = nr_jumps_min_ + static_cast< int >( RG.uniform() * (nr_jumps_max_-nr_jumps_min_) );
219  tr.Info << "generate " << nr_jumps << "jumps for following secstruct:\n";
220  tr.Info << secstruct_ << std::endl;
221 
222  Size nres = secstruct_.size();
223  FArray1D_int tags( nres , 0 );
224  for ( Size i = 0; i < nres; i++ ) {
225  if ( secstruct_[i] == 'L' ) {
226  for ( int ii=std::max((int)i-(int)loop_extension_,0); ii<= std::min( (int) i + (int)loop_extension_, (int) nres-1); ii++) {
227  tags(ii+1)= -10;
228  }
229  }
230  }
231  if ( tr.Trace.visible() ) dump_tags_( tags, nres, std::cout );
232 
233  // detect loops that are too short for cut-points
234  for ( Size i = 1; i <= nres; i++ ) {
235  bool bShort = false;
236  if ( tags( i ) == -10 ) {
237 
238  // extend loops to each side
239  for ( Size j = i; j <= i+min_loop_length_ && j <= nres; j++ ) {
240  if ( tags( j ) > -10 ) {
241  bShort = true;
242  break;
243  }
244  }
245  while ( tags( i ) == -10 ) { //go to end of current loop
246  if ( bShort ) tags( i ) = -1;
247  i++;
248  if ( i > nres ) break;
249  }
250  }
251  }
252 
253  // detect non-loop regions that are too small.
254  Size min_free_size_ = 1;
255  for ( Size i = 1; i<=nres; i++ ) {
256  if ( tags( i ) == 0 ) {
257  Size ct = 0;
258  while ( tags( i+ct ) == 0 ) { ct++; if ( ct+i > nres ) break; }
259  if ( ct <= min_free_size_ ) {
260  for ( Size jj=0; jj<ct; jj++) {
261  tags( i+jj ) = -1;
262  }
263  }
264  i += (ct-1);
265  }
266  }
267 
268  if ( tr.Trace.visible() ) dump_tags_( tags, nres, std::cout );
269 
270  Size nr = 0;
271  Size attempts = 1;
272  while ( nr < nr_jumps && attempts < 1000 ) {
273  attempts += 1;
274  Interval aJump = select_random();
275  int start_tag = tags( aJump.start_ );
276  int stop_tag = tags( aJump.end_ );
277  // bool bReTag = start_tag > 0 && stop_tag > 0;
278  if ( start_tag == stop_tag && start_tag > 0 ) continue; //disallow jump
279  if ( start_tag < 0 || stop_tag < 0 ) continue; //don't jump into loop regions
280  ++nr; // from now on we know that the jump will be inserted
281 
282  int new_tag = start_tag;
283  int replace_tag = stop_tag;
284  if ( new_tag < stop_tag ) {
285  new_tag = stop_tag;
286  replace_tag = start_tag;
287  }
288  // now new_tag should be the larger of the start/stop-tags
289  if ( new_tag == 0 ) new_tag = nr; //still zero: both start and stop are zero
290  if ( replace_tag > 0 ) { //both start/stop > 0 needs to retag
291  /// replace all < replace_tag > with < new_tag >
292  for ( Size i = 1; i <= nres; i++ ) {
293  if ( tags( i ) == replace_tag ) tags(i) = new_tag;
294  }
295  }
296  tr.Info << "after " << attempts << " attempts, jump at " << aJump.start_ << " -- " << aJump.end_ << " with tag " << new_tag << std::endl ;
297  fill_tags_( tags, aJump.start_, new_tag, nres );
298  fill_tags_( tags, aJump.end_, new_tag, nres );
299  if ( tr.Trace.visible() ) dump_tags_( tags, nres, std::cout );
300  jump_list.push_back( aJump );
301  }
302  if ( attempts >= 1000 ) tr.Warning << "failed to find " << nr_jumps << " jumps in "
303  <<attempts<<" attempts. Only " << nr << " possible jumps found\n";
304  typedef utility::vector1< CutList > MetaCutList; //take as many cuts from MetaCutList as jumps -- take one cutregion from each MetaCuts
305  // determine cut-regions
306  // not quite optimal, yet
307  // a 1 CC 2 CC 2 CC 1 jump-setup will always be cut in first two loops, but it should sample both cut possibilities for jump 1
308  //
309 
310  MetaCutList all_cuts;
311  std::map< int, bool > tag_list; // store all observed tags, only cut if one of these is found in downstream region (towards C-term)
312  for ( Size pos=1; pos <= nres; pos ++ ) {
313  if ( tags( pos ) > 0 ) {
314  // add tag to tag_list
315  tag_list[ tags( pos ) ] = true; // boolean doesn't really matter
316  CutList cut_list;
317  // move to end of jump region
318  while ( tags( pos ) > 0 ) { ++pos; if ( pos > nres) break; };
319  if ( pos > nres ) break;
320 
321  runtime_assert ( tags( pos ) <= 0 );
322  // go up until you find another jump region -- add all loops in between to possible cut-regions
323  // if you find chain-break before next jump region no cut-point is needed.
324  while ( tags( pos ) <= 0 ) {
325 
326  // skip possible white-space
327  while ( tags( pos ) > -10 ) { ++pos; if ( pos > nres) break; };
328  if ( pos > nres ) break;
329 
330  // now we are at beginning of new cut region
331  runtime_assert ( tags( pos ) == -10 );
332  Interval cut;
333  cut.start_ = pos;
334 
335  // skip to end of cutable-region
336  while ( tags( pos ) == -10 ) { ++pos; if ( pos > nres) break; };
337  if ( pos > nres ) break;
338 
339  runtime_assert( tags( pos-1 ) == -10 && tags( pos ) > -10 );
340  cut.end_ = pos-1;
341  cut_list.push_back( cut );
342  tr.Trace << "cut: possible " << cut.start_ << " " << cut.end_ << std::endl;
343  }
344  if ( pos > nres ) break;
345  runtime_assert( tags( pos ) > 0 );
346  // new jump region-- add recent collection of cut-regions to meta-list
347  // only add if down-stream jumps are connected to up-stream
348  bool bConnected = false;
349  for ( std::map< int , bool>::const_iterator it = tag_list.begin(), eit = tag_list.end();
350  it!=eit && !bConnected;
351  ++it ) {
352  for ( Size ii = pos; ii<=nres && !bConnected; ii++ ) {
353  if ( it->first == tags ( ii ) ) bConnected = true;
354  if ( bConnected ) tr.Trace << "add cuts since tag " << it->first << " has been found at pos " << ii << std::endl;
355  }
356  }
357  if ( bConnected ) all_cuts.push_back( cut_list );
358  } // tags (pos) > 0
359  } // for loop
360 
361  runtime_assert( all_cuts.size() >= jump_list.size() );
362  MetaCutList::const_iterator cut_pool_it = all_cuts.begin();
363  JumpSetup my_jumps( nres );
364  for ( JumpList::const_iterator it = jump_list.begin(), eit = jump_list.end();
365  it!=eit; ++it ) {
366  int cut_frame_num = static_cast< int >( RG.uniform() * cut_pool_it->size() ) + 1;
367  tr.Trace << "chosen jump-cut: " << it->start_ << " " << it->end_ << " cut:" << (*cut_pool_it)[cut_frame_num].start_
368  << " " << (*cut_pool_it)[cut_frame_num].end_ << std::endl;
369  my_jumps.add_jump( *it, (*cut_pool_it)[cut_frame_num] );
370  ++cut_pool_it;
371  }
372  return JumpSample(my_jumps); //create random cut-points in given cut-regions
373 } // create_jump_sample
374 
375 Interval
377  // FArray1D_int freq(size(),0);
378  // Size Nsample = 100000;
379  // for ( int ii=1; ii < Nsample; ii++ ) {
380 
381  Real ran = RG.uniform() * total_weight_;
382  Real cumsum = 0.0;
383  const_iterator it = begin();
384  const_iterator eit = end();
385 
386  int ct = 1;
387  cumsum += it->weight_;
388  while ( cumsum < ran ) {
389  runtime_assert ( it != eit );
390  it++; ct++;
391  cumsum += it->weight_;
392  }
393  // freq( ct ) += 1;
394  runtime_assert( it != eit );
395 
396 // }
397 // const_iterator it = begin();
398 // for ( int i = 1; i<= size(); i++ ) {
399 // std::cout << 1.0*freq( i )/ ( 1.0*Nsample) << " " << it->weight_/total_weight_ << std::endl;
400 // ++it;
401 // }
402  return it->jump_;
403 }
404 
405 //JumpsFromConstraintForest::JumpsFromConstraintForest(
406 // const core::Size total_residue,
407 // core::scoring::constraints::ConstraintForestOP forest,
408 // ObjexxFCL::FArray1D_float const& cut_probability
409 //) :
410 // total_residue_(total_residue),
411 // forest_(forest),
412 // cut_prob_(cut_probability)
413 //{}
414 //
415 //JumpSample
416 //JumpsFromConstraintForest::create_jump_sample() const {
417 // // STOPGAP SOLUTION: generate new jumps each time. Need to take into account
418 // // that constraints will change too.
419 // // THIS HAS TO CHANGE.
420 // //forest_->generate_random_sample(); this is and has to be called where constraints are generated
421 // return JumpSample(total_residue_, forest_->get_pairings(), cut_prob_);
422 //}
423 
424 //JumpsFromConstraintForest::~JumpsFromConstraintForest() { }
425 //
426 //JumpSample
427 //JumpsFromConstraintForest::clean_jumps( JumpSample const& js ) const
428 //{
429 // std::cerr << "ERROR: JumpSetup::clean_jumps() not implemented" << std::endl;
430 // return js;
431 //}
432 
433 
434 } //jumping
435 } //protocols