Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CoordConstraintClaimer.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 TopologyBroker
11 /// @brief top-class (Organizer) of the TopologyBroker mechanism
12 /// @detailed responsibilities:
13 /// @author Oliver Lange
14 
15 // Unit Headers
17 
18 // Package Headers
21 
22 // Project Headers
26 
28 
29 #include <core/pose/Pose.hh>
30 #include <core/pose/util.hh>
31 // #include <core/kinematics/MoveMap.hh>
32 // #include <core/fragment/FragSet.hh>
33 // #include <protocols/simple_moves/FragmentMover.hh>
34 
37 // AUTO-REMOVED #include <core/io/pdb/pose_io.hh>
38 
39 #include <protocols/loops/Loop.hh>
41 
42 
43 // ObjexxFCL Headers
44 
45 // Utility headers
47 
48 //#include <utility/io/izstream.hh>
49 #include <utility/io/ozstream.hh> //fur dump cst
50 //#include <utility/io/util.hh>
51 #include <basic/Tracer.hh>
52 //#include <basic/options/option.hh>
53 #include <numeric/random/random.hh>
54 //// C++ headers
55 #include <fstream>
56 
57 #include <core/id/NamedStubID.hh>
61 #include <utility/vector1.hh>
62 
63 //Auto Headers
64 #include <ObjexxFCL/FArray2D.hh>
65 
66 // option key includes
67 
68 static basic::Tracer tr("protocols.topo_broker",basic::t_info);
69 static numeric::random::RandomGenerator RG(1882234234);
70 
71 namespace protocols {
72 namespace topology_broker {
73 
74 using namespace core;
75 using namespace scoring::constraints;
76 using namespace ObjexxFCL;
77 
79  filename_( "NO_FILE"),
80  constraints_( NULL ),
81  root_( 1 ),
82  bRegenerateFromInputPose_ ( false ),
83  cst_pose_ ( NULL ),
84  bCentroid_( true ),
85  bFullatom_( false ),
86  bLocal_( false )
87 {}
88 
90  filename_( filename ),
91  constraints_( NULL ),
92  root_( 1 ),
93  bRegenerateFromInputPose_ ( false ),
94  cst_pose_ ( NULL ),
95  bCentroid_( true ),
96  bFullatom_( false ),
97  bLocal_( false )
98 {}
99 
101 
103  if ( !constraints_ && !bUseXYZ_in_cstfile_ ) {
104  if ( !cst_pose_ ) throw EXCN_Input( "CoordConstraintClaimer::new_decoy(): in broker setup provide PDB_FILE or say USE_XYZ_FROM_CSTFILE");
106  }
107 }
108 
111  sequence_ = ""; //force new stealing
112  cst_pose_ = new core::pose::Pose( pose );
113  }
114  if ( bSuperimpose_ ) {
115  sequence_ = ""; //force new superposition
116  }
117  new_decoy();
118 }
119 
121  if ( bLocal_ ) {
122  Size new_root( root_ );
124  broker().relay_message( msg );
125  tr.Info << label() << " got a returned message: " << msg << std::endl;
126 
127  if ( msg.received() ) new_root=msg.good_fix_pos_;
128  else throw EXCN_Input( "no fixed region (looked for "+root_from_label_+") found to use as reference for CoordinateConstraints");
129 
130  if ( !constraints_ && bUseXYZ_in_cstfile_ ) {
131  //in this case we haven't been able to read the constraints file yet, since this is the first time a valid pose exists...
132  cst_pose_ = new pose::Pose( broker().current_pose() );
134  }
135 
136  if ( new_root != root_ ) {
137  root_ = new_root;
138  tr.Info << "set new root for CoordinateConstraints to " << root_ << std::endl;
139  set_cst_root();
140  }
141  runtime_assert( root_ != 0 );
142  }
143  if ( !bLocal_ ) new_claims.push_back( new RootClaim( this, root_, DofClaim::NEED_TO_KNOW ) );
144 }
145 
150  filename_ );
151  sequence_ = "";
152 }
153 
155  // Size new_root;
156 
157  std::string const new_sequence ( pose.annotated_sequence( true ) );
158  bool fullatom( pose.is_fullatom() );
159 
160  //filter --- constraints can be active/inactive for full/centroid poses
161  if ( fullatom && !bFullatom_ ) return;
162  if ( !fullatom && !bCentroid_ ) return;
163 
164  /// new sequence ? --- re-configure constraints ( atomnumbers! )
165  if ( sequence_ != new_sequence ) {
166  tr.Debug << "new sequence -- remap constraints " << std::endl;
167 
168  /// we should have a cst_pose_ now
169  if ( !cst_pose_ ) throw EXCN_Input( "CoordConstraintClaimer::add_constraints(): \
170  in broker setup either provide PDB_FILE or set CST_FROM_INPUT_POSE");
171 
172  //constraints_ should be defined, since new_decoy()
173  runtime_assert( constraints_ );
174 
175  //get constraints
176  if ( !bUseXYZ_in_cstfile_ ) {
177  // get new constrains from cst_pose --- this takes care of atom-renumbering
178  constraints_ = constraints_->steal_def_clone( *cst_pose_, pose );
179  } else { //need to remap constraints or read from file
180  constraints_ = constraints_->remapped_clone( *cst_pose_, pose );
181  }
182 
183  if ( bSuperimpose_ ) {
184  superimpose( pose );
185  }
186  // debug output
187  if ( tr.Trace.visible() ) {
188  utility::io::ozstream dump_cst( label()+"_coord_claimer.cst" );
189  constraints_->show_definition( dump_cst, pose );
190  cst_pose_->dump_pdb("cst_pose.pdb");
191  }
192  }
193 
194  //finally -- add constraints to pose
195  tr.Debug << "add constraints "<< label() << std::endl;
196  pose.add_constraints( constraints_->get_all_constraints() );
197 
198  //keep sequence -- to watch for changes
199  sequence_ = new_sequence;
200 }
201 
203  runtime_assert( cst_pose_ );
204  id::StubID cst_fix_stub_ID( core::pose::named_stub_id_to_stub_id( id::NamedStubID( "N","CA","C", root_ ), *cst_pose_ ));
205  runtime_assert( constraints_ );
206  ConstraintCOPs all_cst = constraints_->get_all_constraints();
207  ConstraintSetOP new_set = new ConstraintSet;
208  for ( ConstraintCOPs::const_iterator it = all_cst.begin(), eit = all_cst.end(); it!=eit; ++it ) {
209  ConstraintOP new_cst = (*it)->clone();
210  LocalCoordinateConstraintOP ll_cst = dynamic_cast< LocalCoordinateConstraint* > ( new_cst.get() );
211  runtime_assert( ll_cst ); //only these should be in the constraint set!
212  ll_cst->set_fixed_stub( cst_fix_stub_ID );
213  new_set->add_constraint( ll_cst );
214  }
215  constraints_=new_set;
216 }
217 
219 
220  //empty constraint set
222 
223  //make local copy of region-definition
224  loops::Loops rigid( rigid_ );
225 
226  //if no regions defined ---> replace it with 1..nres
227  if ( !rigid.size() ) rigid.add_loop( 1, cst_pose.total_residue(), 0 );
228 
229  //go thru regions and generate constraints
230  for ( loops::Loops::const_iterator it = rigid.begin(), eit = rigid.end();
231  it!=eit; ++it ) {
232  for ( Size pos = it->start(); pos <= it->stop(); ++pos ) {
233 
234  //generate constraint for ( CA, pos )
235  conformation::Residue const & rsd( cst_pose.residue( pos ) );
236  id::AtomID cst_atomID( cst_pose.residue_type(pos).atom_index("CA"), pos );
237  id::StubID cst_fix_stub_ID( pose::named_stub_id_to_stub_id( id::NamedStubID( "N","CA","C", root_ ), cst_pose ) );
238  Vector ai(
239  RG.uniform()*perturb_,
240  RG.uniform()*perturb_,
241  RG.uniform()*perturb_
242  );
243  Vector xyz( rsd.xyz( cst_atomID.atomno() ) + ai );
244  if ( bLocal_ ) {
246  cst_atomID,
247  cst_fix_stub_ID,
248  xyz,
249  cst_func_) );
250 
251  } else {
253  cst_atomID,
254  id::AtomID( 1, root_ ) /*this is completely ignored! */,
255  xyz,
256  cst_func_) );
257  }
258  }
259 
260  }
261  if ( tr.Debug.visible() ) {
262  tr.Debug << "CoordConstraintClaimer generate constraints" << std::endl;
263  constraints_->show_definition( std::cout, cst_pose );
264  }
265 }
266 
269  cst_filename_, new ConstraintSet, cst_pose );
270 
271  if ( tr.Debug.visible() ) {
272  tr.Debug << "CoordConstraintClaimer: have read constraints from file:" << std::endl;
273  constraints_->show_definition( std::cout, cst_pose );
274  }
275 }
276 
278  ConstraintCOPs all_cst = constraints_->get_all_constraints();
279  ConstraintSetOP new_set = new ConstraintSet;
280 
281  core::Size const natoms( all_cst.size() );
282  ObjexxFCL::FArray1D_double weights( natoms, 1.0 );
283  ObjexxFCL::FArray2D_double ref_coords( 3, natoms, 0.0 );
284  ObjexxFCL::FArray2D_double coords( 3, natoms, 0.0 );
285 
286  if ( superimpose_regions_.size() ) {
287  for ( Size i = 1; i<=natoms; ++i ) {
289  weights( i ) = 1.0;
290  } else {
291  weights( i ) = 0.0;
292  }
293  }
294  }
295 
296  Size n = 1;
297  if ( tr.Trace.visible() ) {
298  utility::io::ozstream dump_cst( label()+"_before_fit.cst" );
299  constraints_->show_definition( dump_cst, pose );
300  }
301 
302  for ( ConstraintCOPs::const_iterator it = all_cst.begin(), eit = all_cst.end(); it!=eit; ++it, ++n ) {
303  // ConstraintOP new_cst = (*it)->clone();
304  LocalCoordinateConstraintCOP ll_cst = dynamic_cast< LocalCoordinateConstraint const* > ( it->get() );
305  runtime_assert( ll_cst ); //only these should be in the constraint set!
306  Vector xyz_ref( pose.xyz( ll_cst->atom( 1 ) ) );
307  //n = ll_cst->atom( 1 ).rsd(); //uncomment for debugging
308  tr.Trace << "constraint for atom " << ll_cst->atom( 1 ) << std::endl;
309  Vector xyz_cst( ll_cst->xyz_target( pose ) );
310  for ( Size d=1; d<=3; ++d ) {
311  ref_coords( d, n ) = xyz_ref( d );
312  coords( d, n ) = xyz_cst( d );
313  }
314  }
315 
316  //fitting
317  FArray1D_double transvec( 3 );
318  FArray1D_double ref_transvec( 3 );
319 // if ( tr.Trace.visible() ) { //if uncommented use test_n instead of n for filling of coordinates -- only works if all residues have a xzy!
320 // toolbox::dump_as_pdb( "cst_xyz_before_fit.pdb", natoms, coords );
321 // toolbox::dump_as_pdb( "cst_ref_fit.pdb", natoms, ref_coords );
322 // };
323 
324  toolbox::reset_x( natoms, coords, weights, transvec );
325  toolbox::reset_x( natoms, ref_coords, weights, ref_transvec );
327  toolbox::fit_centered_coords( natoms, weights, ref_coords, coords, R );
328 // if ( tr.Trace.visible() ) {
329 // toolbox::dump_as_pdb( "cst_xyz_after_fit.pdb", natoms, coords, -ref_transvec );
330 // };
331 
332  //filling xyz_constraints with new coords
333  n = 1;
334  for ( ConstraintCOPs::const_iterator it = all_cst.begin(), eit = all_cst.end(); it!=eit; ++it, ++n ) {
335  ConstraintOP new_cst = (*it)->clone();
336  LocalCoordinateConstraintOP ll_cst = dynamic_cast< LocalCoordinateConstraint* > ( new_cst.get() );
337  Vector xyz_cst;
338  // n = ll_cst->atom( 1 ).rsd();
339  for ( Size d=1; d<=3; ++d ) {
340  xyz_cst( d ) = coords( d, n ) - ref_transvec( d );
341  }
342  ll_cst->set_xyz_target( xyz_cst, pose );
343  new_set->add_constraint( ll_cst );
344  }
345  constraints_ = new_set;
346 }
347 
348 ///@detail read setup file
349 /*
350  PDB_FILE cst_pose
351  CST_FILE definition of CoordinateConstraints: which atoms, which potential
352  LABEL some name --- not important
353  ROOT where to put the reference atom
354  CST_FROM_INPUT_POSE take cst-xyz from threading model, or silent-in model
355  POTENTIAL a cst-func, e.g., BOUNDED 0 0 4
356 */
359  cst_filename_ = "NoFile";
360  filename_ = "NoFile";
361  root_ = 1;
362  root_from_label_ = "ALL";
363  perturb_ = 0;
364  bUseXYZ_in_cstfile_ = false;
365  bSuperimpose_ = false;
367 }
368 
369 bool CoordConstraintClaimer::read_tag( std::string tag, std::istream& is ) {
370  loops::PoseNumberedLoopFileReader loop_file_reader;
371  loop_file_reader.hijack_loop_reading_code_set_loop_line_begin_token( "RIGID" );
372  bool const strict = false; /*prohibit_single_residue_loops ? no*/
373 
374  if ( tag == "pdb_file" || tag == "PDB_FILE" ) {
375  is >> filename_;
376  read_cst_pose();
377  } else if ( tag == "CST_FILE" ) {
378  is >> cst_filename_;
379  } else if ( tag == "REGION" ) {
380  loops::SerializedLoopList loops = loop_file_reader.read_pose_numbered_loops_file( is, type(), strict );
381  rigid_ = loops::Loops( loops );
382  } else if ( tag == "region_file" ) {
383  std::string file;
384  is >> file;
385  std::ifstream infile( file.c_str() );
386 
387  if (!infile.good()) {
388  utility_exit_with_message( "[ERROR] Error opening RBSeg file '" + file + "'" );
389  }
390  loops::SerializedLoopList loops = loop_file_reader.read_pose_numbered_loops_file( is, file, strict );
391  rigid_ = loops::Loops( loops ); // <==
392  } else if ( tag == "ROOT" ) {
393  is >> root_;
394  } else if ( tag == "ASK_FOR_ROOT" ) {
395  bLocal_ = true;
397  is >> root_from_label_;
398  } else if ( tag == "CST_FROM_INPUT_POSE" ) {
400  } else if ( tag == "USE_XYZ_FROM_CSTFILE" ) { // I made this an explicit option, since it is probably often a bad choice!
401  bUseXYZ_in_cstfile_ = true;
402  } else if ( tag == "NO_CENTROID" ) {
403  bCentroid_ = false;
404  } else if ( tag == "FULLATOM" ) {
405  bFullatom_ = true;
406  } else if ( tag == "PERTURB" ) {
407  is >> perturb_;
408  } else if ( tag == "SUPERIMPOSE" ) {
409  bSuperimpose_ = true;
410  } else if ( tag == "SUPERIMPOSE_REGION" ) {
411  loops::SerializedLoopList loops = loop_file_reader.read_pose_numbered_loops_file( is, type(), strict );
413  } else if ( tag == "SUPERIMPOSE_REGION_FILE" ) {
414  std::string file;
415  is >> file;
416  std::ifstream infile( file.c_str() );
417 
418  if (!infile.good()) {
419  utility_exit_with_message( "[ERROR] Error opening RBSeg file '" + file + "'" );
420  }
421  loops::SerializedLoopList loops = loop_file_reader.read_pose_numbered_loops_file( infile, file, strict );
422  superimpose_regions_ = loops::Loops( loops ); // <==
423  } else if ( tag == "POTENTIAL" ) {
424  std::string func_type;
425  is >> func_type;
427  cst_func_->read_data( is );
428  } else return Parent::read_tag( tag, is );
429  return true;
430 }
431 
433  if ( cst_pose_ ) {
434  if ( cst_filename_ != "NoFile" ) {
436  } else {
437  if ( !cst_func_) {
438  throw EXCN_Input( "POTENTIAL not specified for " + type() );
439  }
441  }
442  }
443  sequence_ = ""; //so we refresh the constraint_set at first call to add_constraints
444 
445 }
446 
447 
448 } //topology_broker
449 } //protocols