Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.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 protocols/RosettaScripts/util.cc
11 /// @brief Utility functions useful in RosettaScripts.
12 /// @authors Sarel Fleishman (sarelf@u.washington.edu), Jacob Corn (jecorn@u.washington.edu),
13 /// Rocco Moretti (rmoretti@u.washington.edu), Eva-Maria Strauch (evas01@uw.edu)
14 
15 // Unit Headers
18 
19 // Project Headers
20 #include <core/types.hh>
24 #include <core/pose/Pose.hh>
25 #include <core/pose/PDBPoseMap.hh>
26 #include <core/pose/PDBInfo.hh>
28 #include <boost/foreach.hpp>
29 #define foreach BOOST_FOREACH
33 #include <protocols/moves/Mover.hh>
34 #include <core/id/types.hh>
35 
36 // Basic Headers
37 #include <basic/options/option.hh>
38 #include <basic/options/keys/inout.OptionKeys.gen.hh>
39 
40 // Utility Headers
41 #include <basic/Tracer.hh>
42 #include <utility/string_util.hh>
43 #include <utility/vector1.hh>
44 #include <utility/tag/Tag.hh>
45 #include <utility/sql_database/types.hh>
46 #include <utility/vector0.hh>
47 #include <utility/excn/Exceptions.hh>
48 
49 static basic::Tracer TR( "protocols.RosettaScripts.util" );
50 
51 namespace protocols {
52 namespace rosetta_scripts {
53 
54 using namespace core::scoring;
55 using namespace protocols::moves;
56 using namespace core;
57 using namespace std;
58 using utility::vector1;
59 
60 /// @details This is essentially a shameless copy of Justin's PackRotamersMover::parse_task_operations. In truth
61 /// DesignRepackMover should disappear into Justin's better organized class, but this will wait... (SJF)
64 {
65  using namespace core::pack::task;
66  using namespace core::pack::task::operation;
67 
68  TaskFactoryOP new_task_factory( new TaskFactory );
69  if ( ! tag->hasOption("task_operations") ) return new_task_factory;
70  TR<<"Object "<<tag->getOption< std::string >( "name", "no_name" )<<" reading the following task_operations: ";
71  return( parse_task_operations( tag->getOption< std::string >( "task_operations"), data ) );
72 }
73 
76 {
77  using namespace core::pack::task;
78  using namespace core::pack::task::operation;
79 
80  TaskFactoryOP new_task_factory( new TaskFactory );
81  std::string const t_o_val( task_list );
82  typedef utility::vector1< std::string > StringVec;
83  StringVec const t_o_keys( utility::string_split( t_o_val, ',' ) );
84  TR<<"Adding the following task operations\n";
85  for ( StringVec::const_iterator t_o_key( t_o_keys.begin() ), end( t_o_keys.end() );
86  t_o_key != end; ++t_o_key ) {
87  if ( data.has( "task_operations", *t_o_key ) ) {
88  new_task_factory->push_back( data.get< TaskOperation * >( "task_operations", *t_o_key ) );
89  TR<<*t_o_key<<' ';
90  } else {
91  throw utility::excn::EXCN_RosettaScriptsOption("TaskOperation " + *t_o_key + " not found in DataMap.");
92  }
93  }
94  TR<<std::endl;
95  return new_task_factory;
96 }
97 
98 ///option to add or refer to a Taskfactory through the datamap, similar to how to add/refer to movemap OPs (EMS)
100 parse_task_operations( utility::tag::TagPtr const tag, protocols::moves::DataMap & data, core::pack::task::TaskFactoryOP & task_factory /*, bool const reset_taskfactory */)
101 {
102  using namespace core::pack::task;
103  using namespace core::pack::task::operation;
104 
105  if ( tag->hasOption("task_factory" ) ){
106  std::string const name( tag->getOption< std::string >("task_factory") );
107  TR <<"taskfacotry name: " << name << std::endl;
108 
109  if( data.has( "TaskFactory", name ) ){
110  task_factory = data.get< TaskFactory *>( "TaskFactory", name );
111  TR<<"found helper task_factory: "<< name <<" for mover: "<<tag->getName()<< std::endl;
112  }
113 
114  else{ // ( !data.has( "TaskFactory", name ) ){
115  std::string tf_string = "TaskFactory";
116  task_factory = new TaskFactory;
117  data.add( tf_string , name , task_factory );
118  TR<<"adding new TaskFactory to the datamap: "<< name <<std::endl;
119  }
120  }
121 
122  if ( ! tag->hasOption("task_operations") ) return task_factory;
123 
124  std::string const t_o_val( tag->getOption<std::string>("task_operations") );
125  typedef utility::vector1< std::string > StringVec;
126  StringVec const t_o_keys( utility::string_split( t_o_val, ',' ) );
127  TR<<"Adding the following task operations to mover "<<tag->getName()<<" called "<<tag->getOption<std::string>( "name", "no_name" )<<":\n";
128 
129  for ( StringVec::const_iterator t_o_key( t_o_keys.begin() ), end( t_o_keys.end() );
130  t_o_key != end; ++t_o_key ) {
131 
132  if ( data.has( "task_operations", *t_o_key ) ) {
133  task_factory->push_back( data.get< TaskOperation * >( "task_operations", *t_o_key ) );
134  TR<<*t_o_key<<' ';
135  }
136  else {
137  throw utility::excn::EXCN_RosettaScriptsOption("TaskOperation " + *t_o_key + " not found in DataMap.");
138  }
139  }
140  TR<<std::endl;
141  return task_factory;
142 }
143 
146 {
149  typedef std::string String;
150 
151  utility::vector1< TaskOperationOP > task_operations;
152  String const t_o_val( tag->getOption<String>("task_operations", "" ) );
153  if( t_o_val != "" ){
154  utility::vector1< String > const t_o_keys( utility::string_split( t_o_val, ',' ) );
155  for ( utility::vector1< String >::const_iterator t_o_key( t_o_keys.begin() ), end( t_o_keys.end() );
156  t_o_key != end; ++t_o_key ) {
157  if ( data.has( "task_operations", *t_o_key ) ) {
158  task_operations.push_back( data.get< TaskOperation* >( "task_operations", *t_o_key ) );
159  } else {
160  throw utility::excn::EXCN_RosettaScriptsOption("TaskOperation " + *t_o_key + " not found in DataMap.");
161  }
162  }
163  }
164  return task_operations;
165 }
166 
167 
168 
169 /// @details Utility function to find a scorefunction from parser-provided data. This is essentially a shameless
170 /// copy of Justin's PackRotamersMover::parse_score_function.
172 parse_score_function( utility::tag::TagPtr const tag, protocols::moves::DataMap const & data, std::string const dflt_key/*="score12"*/ )
173 {
174  std::string const scorefxn_key( tag->getOption<std::string>("scorefxn", dflt_key ) );
175  if ( ! data.has( "scorefxns", scorefxn_key ) ) {
176  throw utility::excn::EXCN_RosettaScriptsOption("ScoreFunction " + scorefxn_key + " not found in DataMap.");
177  }
178  return data.get< ScoreFunction* >( "scorefxns", scorefxn_key );
179 }
180 
183 
184  if( in_tag->hasOption("reference_name") ){
185  core::pose::PoseOP refpose(NULL);
186  std::string refpose_name(in_tag->getOption<std::string>( "reference_name") );
187 
188  if( !data_map.has("spm_ref_poses",refpose_name) ){
189  refpose = new core::pose::Pose();
190  data_map.add("spm_ref_poses",refpose_name,refpose );
191  }
192  else refpose = data_map.get<core::pose::Pose *>("spm_ref_poses",refpose_name );
193 
194  return refpose;
195  }
196  else std::cerr << "WARNING: saved_reference_pose function called even though tag has no 'reference_name' entry. something's unclean somewhere." << std::endl;
197  return NULL;
198 }
199 
200 /// @brief utility function for parse_movemap which goes over each of the tags in a movemap section
201 void
203  using namespace core::kinematics;
204  using namespace utility::tag;
205 
206  foreach( TagPtr const tag, in_tag->getTags() ){
207  std::string const name( tag->getName() );
208  runtime_assert( name == "Jump" || name == "Chain" || name == "Span" );
209  if( name == "Jump" ){
210  core::Size const num( tag->getOption< core::Size >( "number" ) );
211  bool const setting( tag->getOption< bool >( "setting" ) );
212  if( num == 0 ) mm->set_jump( setting ); // set all jumps if number==0
213  else mm->set_jump( num, setting );
214  }
215  if( name == "Chain" ){
216  core::Size const num( tag->getOption< core::Size >( "number" ) );
217  bool const chi( tag->getOption< bool >( "chi" ) );
218  bool const bb( tag->getOption< bool >( "bb" ) );
219  core::Size const chain_begin( pose.conformation().chain_begin( num ) );
220  core::Size const chain_end( pose.conformation().chain_end( num ) );
221  for( core::Size i( chain_begin ); i <= chain_end; ++i ){
222  mm->set_chi( i, chi );
223  mm->set_bb( i, bb );
224  }
225  bool const bondangle( tag->getOption< bool >( "bondangle", false ) );
226  bool const bondlength( tag->getOption< bool >( "bondlength", false ) );
227  if (bondangle || bondlength) {
228  for( core::Size i( chain_begin ); i <= chain_end; ++i ){
229  for( core::Size j=1; j<=pose.residue_type(i).natoms(); ++j ){
230  mm->set( core::id::DOF_ID(core::id::AtomID(j,i), core::id::THETA ), bondangle );
231  mm->set( core::id::DOF_ID(core::id::AtomID(j,i), core::id::D ), bondlength );
232  }
233  }
234  }
235  }
236  if( name == "Span" ){
237  core::Size const begin( tag->getOption< core::Size >( "begin" ) );
238  core::Size const end( tag->getOption< core::Size >( "end" ) );
239  runtime_assert( end >= begin );
240  bool const chi( tag->getOption< bool >( "chi" ) );
241  bool const bb( tag->getOption< bool >( "bb" ) );
242  for( core::Size i( begin ); i <= end; ++i ){
243  mm->set_chi( i, chi );
244  mm->set_bb( i, bb );
245  }
246  bool const bondangle( tag->getOption< bool >( "bondangle", false ) );
247  bool const bondlength( tag->getOption< bool >( "bondlength", false ) );
248  if (bondangle || bondlength) {
249  for( core::Size i( begin ); i <= end; ++i ){
250  for( core::Size j=1; j<=pose.residue_type(i).natoms(); ++j ){
251  mm->set( core::id::DOF_ID(core::id::AtomID(j,i), core::id::THETA ), bondangle );
252  mm->set( core::id::DOF_ID(core::id::AtomID(j,i), core::id::D ), bondlength );
253  }
254  }
255  }
256  }
257  }//foreach tag
258 }
259 
260 /// @brief variant of parse_movemap that takes in a datamap and searches it for already existing movemaps
261 void
262 parse_movemap( utility::tag::TagPtr const in_tag, core::pose::Pose const & pose, core::kinematics::MoveMapOP & mm, protocols::moves::DataMap & data, bool const reset_map ){
263  using utility::tag::TagPtr;
264  using namespace core::kinematics;
265 
266  if( in_tag() == NULL ) return;
267 
268  utility::vector1< TagPtr > const branch_tags( in_tag->getTags() );
270  for( tag_it = branch_tags.begin(); tag_it!=branch_tags.end(); ++tag_it ){
271  if( (*tag_it)->getName() == "MoveMap" ){
272  break;
273  }
274  }
275  if( reset_map ){
276  mm->set_bb( true );
277  mm->set_chi( true );
278  mm->set_jump( true );
279  }
280  if( tag_it == branch_tags.end() ) return;
281 
282  if( (*tag_it)->hasOption( "name" ) ){
283  std::string const name( (*tag_it)->getOption< std::string >( "name" ) );
284  if( data.has( "movemaps", name ) ){
285  mm = data.get< MoveMap * >( "movemaps", name );
286  TR<<"Found movemap "<<name<<" on datamap"<<std::endl;
287  }
288  else{
289  data.add( "movemaps", name, mm );
290  TR<<"Adding movemap "<<name<<" to datamap"<<std::endl;
291  }
292  }
293  foreach_movemap_tag( *tag_it, pose, mm );
294 }
295 
296 ///@details modifies an existing movemap according to tag
297 /// the movemap defaults to move all bb, chi, and jumps.
298 void
300  using utility::tag::TagPtr;
301  using namespace core::kinematics;
302 
303  if( in_tag() == NULL ) return;
304 
305  utility::vector1< TagPtr > const branch_tags( in_tag->getTags() );
307  for( tag_it = branch_tags.begin(); tag_it!=branch_tags.end(); ++tag_it ){
308  if( (*tag_it)->getName() == "MoveMap" ){
309  break;
310  }
311  }
312  mm->set_bb( true );
313  mm->set_chi( true );
314  mm->set_jump( true );
315  if( tag_it == branch_tags.end() ) return;
316 
317  if( (*tag_it)->hasOption( "name" ) ){
318  TR<<"ERROR in "<<*tag_it<<'\n';
319  throw utility::excn::EXCN_RosettaScriptsOption("Tag called with option name but this option is not available to this mover. Note that FastRelax cannot work with a prespecified movemap b/c its movemap is parsed at apply time. Sorry." );
320  }
321  if( tag_it == branch_tags.end() ) return;
322 
323  foreach_movemap_tag( *tag_it, pose, mm );
324 }
325 
327 parse_filter( std::string const filter_name, protocols::filters::Filters_map const & filters ){
328  protocols::filters::Filters_map::const_iterator filter_it( filters.find( filter_name ) );
329  if( filter_it == filters.end() )
330  throw utility::excn::EXCN_RosettaScriptsOption( "Filter "+filter_name+" not found" );
331  return filter_it->second;
332 }
333 
335 parse_mover( std::string const mover_name, protocols::moves::Movers_map const & movers ){
336  protocols::moves::Movers_map::const_iterator mover_it( movers.find( mover_name ) );
337  if( mover_it == movers.end() )
338  throw utility::excn::EXCN_RosettaScriptsOption("Mover "+mover_name+" not found" );
339  return mover_it->second;
340 }
341 
342 /// @brief utility function for parsing xyzVector
344 parse_xyz_vector( utility::tag::TagPtr const xyz_vector_tag ){
345  if ( ! xyz_vector_tag->hasOption("x") ) throw utility::excn::EXCN_RosettaScriptsOption("xyz_vector requires 'x' coordinates option");
346  if ( ! xyz_vector_tag->hasOption("y") ) throw utility::excn::EXCN_RosettaScriptsOption("xyz_vector requires 'y' coordinates option");
347  if ( ! xyz_vector_tag->hasOption("z") ) throw utility::excn::EXCN_RosettaScriptsOption("xyz_vector requires 'z' coordinates option");
348 
350  xyz_vector_tag->getOption<core::Real>("x"),
351  xyz_vector_tag->getOption<core::Real>("y"),
352  xyz_vector_tag->getOption<core::Real>("z")
353  );
354 
355  return xyz_v;
356 
357 }
358 
359 /// @brief Return the number of the residue on source that is nearest to res on target. If the distance
360 /// is greater than 2.0 returns 0 to indicate error
362 find_nearest_res( core::pose::Pose const & source, core::pose::Pose const & target, core::Size const res, core::Size const chain/*=0*/ ){
363  core::Real min_dist( 100000 ); core::Size nearest_res( 0 );
364  for( core::Size i = 1; i <= source.total_residue(); ++i ){
365  if( source.residue( i ).is_ligand() ) continue;
366  if( chain && source.residue( i ).chain() != chain ) continue;
367  core::Real const dist( target.residue( res ).xyz( "CA" ).distance( source.residue( i ).xyz( "CA" ) ) );
368  if( dist <= min_dist ){
369  min_dist = dist;
370  nearest_res = i;
371  }
372  }
373  if( min_dist <= 3.0 ) return nearest_res;
374  else return 0;
375 }
376 
378 residue_packer_states( core::pose::Pose const & pose, core::pack::task::TaskFactoryCOP tf, bool const designable, bool const packable/*but not designable*/) {
379  utility::vector1< core::Size > designable_vec, packable_vec, both;
380  designable_vec.clear(); packable_vec.clear(); both.clear();
381  core::pack::task::PackerTaskOP packer_task( tf->create_task_and_apply_taskoperations( pose ) );
382  for( core::Size resi=1; resi<=pose.total_residue(); ++resi ){
383  if( packer_task->being_designed( resi ) )
384  designable_vec.push_back( resi );
385  else if( packer_task->being_packed( resi ) )
386  packable_vec.push_back( resi );
387  }
388  if( designable && packable ){
389  both.insert( both.begin(), designable_vec.begin(), designable_vec.end() );
390  both.insert( both.end(), packable_vec.begin(), packable_vec.end() );
391  return both;
392  }
393  if( designable )
394  return designable_vec;
395  return packable_vec;
396 }
397 } //RosettaScripts
398 } //protocols