Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RBSegment.cc
Go to the documentation of this file.
1 //
2 // (c) Copyright Rosetta Commons Member Institutions.
3 // (c) This file is part of the Rosetta software suite and is made available under license.
4 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
5 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
6 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
7 
8 /// @file
9 /// @brief
10 /// @author Frank DiMaio
11 /// @author Srivatsan Raman
13 // AUTO-REMOVED #include <protocols/rbsegment_Moves/RBSegmentMover.hh>
14 
15 // Rosetta Headers
16 // AUTO-REMOVED #include <core/pose/Pose.hh>
17 // AUTO-REMOVED #include <core/conformation/Residue.hh>
18 // AUTO-REMOVED #include <core/scoring/Ramachandran.hh>
19 //#include <core/scoring/ScoringManager.hh>
20 // AUTO-REMOVED #include <core/kinematics/MoveMap.hh>
21 // AUTO-REMOVED #include <basic/basic.hh>
22 #include <basic/Tracer.hh>
23 
24 // Random number generator
25 // AUTO-REMOVED #include <numeric/xyzVector.io.hh>
26 #include <numeric/random/random.hh>
27 // AUTO-REMOVED #include <ObjexxFCL/FArray1D.hh>
28 
29 //
30 #include <string>
31 #include <fstream>
32 
34 #include <protocols/loops/Loop.hh>
35 #include <protocols/loops/Loops.hh>
36 #include <utility/vector1.hh>
37 #include <map>
38 
39 
40 namespace protocols {
41 namespace rbsegment_relax {
42 
43 using namespace core;
44 
45 basic::Tracer TR_seg("RBSegment");
46 
47 static numeric::random::RandomGenerator RG(431112); // <- Magic number, do not change it!
48 
49 /////////////////////
50 ///@brief helper function
52  return X*X;
53 }
54 
55 //////////////////////////////////////////////////////////
56 ///@brief Helper function tokenizes a str
57 /////////////////////////////////////////////////////////
58 // don't use use string_utils.hh:split or string_split instead
59 void Tokenize(const std::string &str,
61  const std::string &delimiters=" ") {
62  tokens.clear();
63  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
64  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
65 
66  while (std::string::npos != pos || std::string::npos != lastPos) {
67  tokens.push_back(str.substr(lastPos, pos - lastPos));
68  lastPos = str.find_first_not_of(delimiters, pos);
69  pos = str.find_first_of(delimiters, lastPos);
70  }
71 }
72 
73 
74 void RBSegment::set_movement( core::Real sigAxisR, core::Real sigAxisT, core::Real sigOffAxisR, core::Real sigOffAxisT) {
75  if (sigOffAxisR == 0 && sigOffAxisT == 0) {
76  TR_seg << "Setting movement parameters: [" << segments_[1].start() << "...] :"
77  << sigAxisR << " , " << sigAxisT << std::endl;
78  } else {
79  TR_seg << "Setting movement parameters: [" << segments_[1].start() << "...] :"
80  << sigAxisR << " , " << sigAxisT << " , " << sigOffAxisR << " , " << sigOffAxisT << std::endl;
81  }
82  sigAxisR_ = sigAxisR;
83  sigAxisT_ = sigAxisT;
84  sigOffAxisR_ = sigOffAxisR;
85  sigOffAxisT_ = sigOffAxisT;
86 }
87 
88 void RBSegment::get_movement( core::Real &sigAxisR, core::Real &sigAxisT, core::Real &sigOffAxisR, core::Real &sigOffAxisT) const {
89  sigAxisR = sigAxisR_;
90  sigAxisT = sigAxisT_;
91  sigOffAxisR = sigOffAxisR_;
92  sigOffAxisT = sigOffAxisT_;
93 }
94 
95 
96 
97 //////////////////////////////////////////////////////////
98 ///@brief Parses an RB segment file into a vector of RBsegments
99 /////////////////////////////////////////////////////////
104  bool autoGenerateLoops /* = false */,
105  int nres /* =0 */,
106  utility::vector1< core::Size > cutpoints /*=utility::vector1< core::Size >(0)*/
107 ) {
108  rbsegs.clear();
109  loops.clear();
110 
111  std::ifstream infile( filename.c_str() );
112 
113  if (!infile.good()) {
114  TR_seg << "[ERROR] Error opening RBSeg file '" << filename << "'" << std::endl;
115  exit(1);
116  }
117 
119  utility::vector1< utility::vector1< int > > compound_segments;
122  std::map < int, RBSegment > simple_segments;
123  utility::vector1 < RBSegment > simple_seg_list;
124 
125  std::string line;
127 
128  while( getline(infile,line) ) {
129  Tokenize( line, tokens ) ;
130 
131  if( tokens.size() > 0 ) {
132  /////////////////////////////
133  //// segment (simple)
134  if ( tokens[1] == "SEGMENT" || tokens[1] == "SEG" ) {
135  if ( tokens.size() < 5 ) {
136  TR_seg << "[ERROR] Error parsing line '" << line << "'" << std::endl;
137  exit(1);
138  }
139  int id = atoi( tokens[2].c_str() );
140  if (simple_segments.find( id ) != simple_segments.end() ) {
141  TR_seg << "[ERROR] Segment id " << id << " defined twice in RBSeg file!" << std::endl;
142  exit(1);
143  }
144  int seg_start=atoi( tokens[3].c_str() ), seg_end = atoi( tokens[4].c_str() );
145  char ss_key = tokens[5][0];
146  simple_segments[id] = RBSegment( seg_start, seg_end, ss_key );
147 
148  if (tokens.size() == 7)
149  simple_segments[id].set_movement( atof( tokens[6].c_str() ),
150  atof( tokens[7].c_str() ) );
151  else if (tokens.size() == 9)
152  simple_segments[id].set_movement( atof( tokens[6].c_str() ),
153  atof( tokens[7].c_str() ),
154  atof( tokens[8].c_str() ),
155  atof( tokens[9].c_str() ) );
156 
157  simple_seg_list.push_back( simple_segments[id] );
158  /////////////////////////////
159  //// lock
160  } else if ( tokens[1] == "LOCKED" ) {
161  if ( tokens.size() < 5 ) {
162  TR_seg << "[ERROR] Error parsing line '" << line << "'" << std::endl;
163  exit(1);
164  }
165  utility::vector1< int > thisLock;
166  for (core::Size i=4; i<=tokens.size(); ++i)
167  thisLock.push_back( atoi( tokens[i].c_str() ) );
168  locked_segments.push_back( thisLock );
169 
170  locked_mvmt.push_back(
171  std::pair< core::Real, core::Real > ( atof( tokens[2].c_str() ), atof( tokens[3].c_str() ) ) );
172  /////////////////////////////
173  //// compound
174  } else if ( tokens[1] == "COMPOUND" ) {
175  /*
176  if ( tokens.size() < 5 ) {
177  TR_seg << "[ERROR] Error parsing line '" << line << "'" << std::endl;
178  exit(1);
179  }
180  utility::vector1< int > thisCompound;
181  for (core::Size i=4; i<=tokens.size(); ++i)
182  thisCompound.push_back( atoi( tokens[i].c_str() ) );
183  compound_segments.push_back( thisCompound );
184 
185  compound_mvmt.push_back(
186  std::pair< core::Real, core::Real > ( atof( tokens[2].c_str() ), atof( tokens[3].c_str() ) ) );
187  */
188  TR_seg.Error << "[ERROR] compund segments unsupported ... ignoring line '" << line << "'" << std::endl;
189  } else if ( tokens[1] == "LOOP" ) {
190  if ( autoGenerateLoops ) {
191  TR_seg.Warning << "[WARNING] LOOP specified in RBSegment file but autoGenerateLoops is true!"
192  << " Ignoring line." << std::endl;
193  continue;
194  }
195 
196  if ( tokens.size() < 3 ) {
197  TR_seg << "[ERROR] Error parsing line '" << line << "'" << std::endl;
198  exit(1);
199  }
200  core::Size start_res = (core::Size) atoi(tokens[2].c_str());
201  core::Size end_res = (core::Size) atoi(tokens[3].c_str());
202  core::Size cutpt = 0; // default - let LoopRebuild choose cutpoint
203  core::Real skip_rate = 0.0; // default - never skip
204  std::string extend_loop_str;
205  bool extend_loop = false;
206 
207  if (tokens.size() > 3)
208  cutpt = (core::Size) atoi(tokens[4].c_str());
209  if (tokens.size() > 4)
210  skip_rate = atof(tokens[5].c_str());
211  if (tokens.size() > 5)
212  extend_loop_str = tokens[6];
213 
214  if (extend_loop_str.length() > 0)
215  extend_loop = true;
216 
217  loops.push_back( protocols::loops::Loop(start_res, end_res, cutpt, skip_rate, extend_loop) );
218  } else if ( tokens[1][0] != '#' ) {
219  TR_seg << "[WARNING] Skipping line '" << line << "'" << std::endl;
220  }
221  }
222  }
223 
224  // auto-gen loops (if necessary)
225  if ( autoGenerateLoops && simple_seg_list.size() > 0 ) {
226  std::sort( simple_seg_list.begin(), simple_seg_list.end(), RB_lt());
227  int start_res=1, end_res=simple_seg_list[1][1].start()-1;
228  //int cutpt = (start_res+end_res)/2;
229  int nsegs = simple_seg_list.size();
230 
231  if (end_res > start_res)
232  loops.push_back( protocols::loops::Loop(start_res, end_res, 0, 0.0, false) );
233  else if (end_res == start_res)
234  loops.push_back( protocols::loops::Loop(start_res, end_res/*+1*/, 0, 0.0, false) );
235  for (int i=1; i<nsegs; ++i) {
236  start_res = simple_seg_list[i][1].end()+1;
237  end_res = simple_seg_list[i+1][1].start()-1;
238  //cutpt = (start_res+end_res)/2; // set but never used ~Labonte
239  if (end_res > start_res)
240  loops.push_back( protocols::loops::Loop(start_res, end_res, 0, 0.0, false) );
241  else if (end_res == start_res)
242  loops.push_back( protocols::loops::Loop(start_res/*-1*/, end_res/*+1*/, 0, 0.0, false) );
243  else // end_res < start_res
244  loops.push_back( protocols::loops::Loop(end_res/*-1*/, start_res/*+1*/, 0, 0.0, false) );
245  }
246  start_res = simple_seg_list[nsegs][1].end()+1;
247  end_res = nres;
248  //cutpt = (start_res+end_res)/2; // set but never used ~Labonte
249  if (end_res > start_res)
250  loops.push_back( protocols::loops::Loop(start_res, end_res, 0, 0.0, false) );
251  else if (end_res == start_res)
252  loops.push_back( protocols::loops::Loop(start_res/*-1*/, end_res, 0, 0.0, false) );
253 
254  // split loops on cutpoints
255  std::sort( cutpoints.begin(), cutpoints.end() );
256  for (int i=1; i<=(int)cutpoints.size(); ++i) {
257  for (int j=1; j<=(int)loops.size(); ++j) {
258  if (cutpoints[i] > loops[j].start() && cutpoints[i] < loops[j].stop()) {
259  // make 2 new loops
260  protocols::loops::Loop l1(loops[j].start(), cutpoints[i]), l2(cutpoints[i]+1, loops[j].stop());
261  loops[j] = l1; // replace the original loop
262  loops.push_back( l2 ); // add second half to end
263  }
264  }
265  }
266  }
267 
268  // now assemble compound segments
269  rbsegs.clear();
270 
271  // create _locked_ segments, removing constituent simple segments from map as compound seg is created
272  for (core::Size i=1; i<=locked_segments.size(); ++i) {
273  utility::vector1< RBSegment > thisLockSeg;
274  for (core::Size j=1; j<=locked_segments[i].size(); ++j) {
275  int id = locked_segments[i][j];
276  if (simple_segments.find( id ) == simple_segments.end() ) {
277  TR_seg << "[ERROR] Segment id " << id << " undefined or used in multiple locked compound segments" << std::endl;
278  exit(1);
279  }
280  thisLockSeg.push_back( simple_segments[ id ] );
281  simple_segments.erase( id );
282  }
283  RBSegment toAdd( thisLockSeg );
284  toAdd.set_movement( locked_mvmt[i].first, locked_mvmt[i].second );
285  rbsegs.push_back( toAdd );
286  }
287 
288  // create _fixed_ segments, keeping constituent simple segments in map
289  for (core::Size i=1; i<=compound_segments.size(); ++i) {
290  utility::vector1< RBSegment > thisCompoundSeg;
291  for (core::Size j=1; j<=compound_segments[i].size(); ++j) {
292  int id = compound_segments[i][j];
293  if (simple_segments.find( id ) == simple_segments.end() ) {
294  TR_seg << "[ERROR] Segment id " << id << " undefined or used in multiple locked compound segments" << std::endl;
295  exit(1);
296  }
297  thisCompoundSeg.push_back( simple_segments[ id ] );
298  }
299  RBSegment toAdd( thisCompoundSeg );
300  toAdd.set_movement( compound_mvmt[i].first, compound_mvmt[i].second );
301  rbsegs.push_back( toAdd );
302  }
303 
304  // place remaining simple structs in vector
305  // ids no longer matter
306  for ( std::map < int, RBSegment >::iterator i=simple_segments.begin(); i!=simple_segments.end(); ++i ) {
307  rbsegs.push_back( i->second );
308  }
309 
310  // sort by start residue
311  std::sort( rbsegs.begin(), rbsegs.end(), RB_lt());
312  std::sort( loops.v_begin(), loops.v_end(), protocols::loops::Loop_lt());
313 
314  // debug ... print in the data structure
315  for (core::Size i=1; i<=rbsegs.size(); ++i) {
316  if (rbsegs[i].isSimple()) {
317  TR_seg << "[debug] SIMPLE rb " << rbsegs[i][1].start() << "--" << rbsegs[i][1].end() << " [type " << (int)rbsegs[i][1].type() << "] " << " r = " << rbsegs[i].getSigAxisR()
318  << " t = " << rbsegs[i].getSigAxisT()
319  << " r_o = " << rbsegs[i].getSigOffAxisR()
320  << " t_o = " << rbsegs[i].getSigOffAxisT() << std::endl;
321  } else {
322  TR_seg << "[debug] COMPOUND rb r = " << rbsegs[i].getSigAxisR()
323  << " t = " << rbsegs[i].getSigAxisT()
324  << " r_o = " << rbsegs[i].getSigOffAxisR()
325  << " t_o = " << rbsegs[i].getSigOffAxisT() << std::endl;
326  for (core::Size j=1; j<=rbsegs[i].nContinuousSegments(); ++j) {
327  TR_seg << rbsegs[i][j].start() << "--" << rbsegs[i][j].end() << " [type " << (int)rbsegs[i][j].type() << "] ";
328  }
329  TR_seg << std::endl;
330  }
331  }
332  for (core::Size i=1; i<=loops.size(); ++i) {
333  TR_seg << "[debug] LOOP " << loops[i].start() << "--" << loops[i].stop()
334  << " " << loops[i].cut() << " [skip " << loops[i].skip_rate() << "]" << std::endl;
335  }
336 }
337 
338 
339 //////////////////////////////////////////////////////////
340 ///@brief Select a single RB segment to perturb + attached loops
341 /////////////////////////////////////////////////////////
343  utility::vector1< RBSegment > const &rbsegs_in,
344  protocols::loops::Loops const &loops_in,
345  utility::vector1< RBSegment > &rbsegs_selected,
346  protocols::loops::Loops &loops_selected
347 )
348 {
349  rbsegs_selected.clear();
350  loops_selected.clear();
351 
352  int nRBSegs = (int)rbsegs_in.size();
353  if (nRBSegs == 0)
354  return;
355 
356  int k;
357  if (nRBSegs == 1)
358  k=1;
359  else
360  k = RG.random_range(1, nRBSegs);
361 
362  rbsegs_selected.push_back( rbsegs_in[k] );
363 
364  // now find _all_ attached loops
365  for (core::Size i=1; i<=loops_in.size(); ++i) {
366  for (core::Size j=1; j<=rbsegs_in[k].nContinuousSegments(); ++j) {
367  bool adjLoopN = loops_in[i].stop() == rbsegs_in[k][j].start()-1;
368  bool adjLoopC = loops_in[i].start() == rbsegs_in[k][j].end()+1;
369  if ( adjLoopN || adjLoopC ) {
370  protocols::loops::Loop bounding_loop = loops_in[i];
371 
372  loops_selected.push_back( bounding_loop );
373  }
374  }
375  }
376 
377  std::cerr << rbsegs_selected.size() << " rigid body segments input\n";
378  std::cerr << loops_selected.size() << " loops\n";
379 } // void LoopRebuild::select_loops
380 
381 
382 
384  utility::vector1 < RBSegment > remappedSimpleSegs;
385  for ( core::Size i=1 ; i<=segments_.size(); ++i ) {
386  int oldS = (int)segments_[i].start(),
387  oldE = (int)segments_[i].end();
388  int newS, newE;
389 
390  if (mapping[ oldS ] != 0 ) {
391  newS = mapping[ oldS ];
392  } else {
393  TR_seg << "[ ERROR ] mapping(" << segments_[i].start() << ") not found!" << std::endl;
394  exit(1);
395  }
396 
397  if (mapping[ oldE ] != 0 ) {
398  newE = mapping[ oldE ];
399  } else {
400  TR_seg << "[ ERROR ] mapping(" << segments_[i].end() << ") not found!" << std::endl;
401  exit(1);
402  }
403  TR_seg << "[ DEBUG ] remapping(" << oldS << "," << oldE << ") to (" << newS << "," << newE << ")" << std::endl;
404 
405  remappedSimpleSegs.push_back( RBSegment( newS, newE, segments_[i].type() ) );
406  }
407  return RBSegment( remappedSimpleSegs );
408 }
409 
410 
412  for (core::Size i=1; i<=segs_in.size(); ++i) {
413  if (!segs_in[i].isSimple()) {
414  TR_seg << "[ ERROR ] Error parsing RBSeg file: trying to nest compound segments." << std::endl;
415  exit(1);
416  } else {
417  segments_.push_back( segs_in[i][1] );
418  }
419  }
420  sigAxisR_ = sigAxisT_ = sigOffAxisR_ = sigOffAxisT_ = 0.0;
421 }
422 
423 
424 }
425 }