Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IRCollection.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/motifs/IRCollection.cc
11 /// @brief Implmentation of interaction motifs
12 
13 #include <protocols/loops/Loops.hh>
21 // AUTO-REMOVED #include <core/io/pdb/pose_io.hh>
22 #include <core/pose/Pose.hh>
23 #include <core/pose/PDBInfo.hh>
24 #include <basic/Tracer.hh>
25 // AUTO-REMOVED #include <core/scoring/etable/Etable.hh>
26 #include <core/scoring/Energies.hh>
32 #include <utility/string_util.hh>
33 // AUTO-REMOVED #include <core/scoring/constraints/CoordinateConstraint.hh>
35 #include <core/types.hh>
36 
37 #include <basic/options/option.hh>
38 #include <basic/options/keys/motifs.OptionKeys.gen.hh>
39 #include <basic/options/keys/out.OptionKeys.gen.hh>
40 
41 // AUTO-REMOVED #include <fstream>
42 #include <iostream>
43 #include <map>
44 #include <algorithm>
45 
46 #include <utility/vector1.hh>
47 
48 
49 namespace protocols {
50 namespace motifs {
51 
52  static basic::Tracer irt( "protocols.motifs.IRCollection", basic::t_info );
53 
55 
57  {
58  core::Size build_pos( 2 );
59 
60  // Loop over all build sites
61  for( core::Size target_index = 1, end_index = build_sites.size() ; target_index <= end_index ; ++target_index ) {
62  core::Size target_pos( build_sites[ target_index ] );
63 
64  // Loop over motifs in libray, look for those that apply
65  for( MotifCOPs::const_iterator motif_itr_itr = motifs.begin(), end_itr = motifs.end() ; motif_itr_itr != end_itr ; ++motif_itr_itr ) {
66  MotifCOP motif_itr( *motif_itr_itr );
67 
68  // Check for applicability
69  if( !motif_itr->apply_check( pose, target_pos ) ) continue;
70 
71 // irt << "Found a motif that applies!" << std::endl;
72  // Build the set of inverse rotamers - note that I don't have a great idea for which phi/psi angles to
73  // use for building. The rotamer libraries are bb-dependent, so...
74  bool is_it_forward( false );
75  core::pack::rotamer_set::RotamerSetOP rot_set = motif_itr->build_inverted_rotamers( pose, target_pos, is_it_forward, build_pos );
76 
77  // Store the information
78  motif_source_.push_back( motif_itr );
79  motif_forward_.push_back( is_it_forward );
80  target_positions_.push_back( target_pos );
81  rotamer_sets_.push_back( rot_set );
82 
83  }
84  }
85  }
86 
89  {
90  core::Size accumulated_nirs( 0 );
91  for( core::Size index = 1, end_index = rotamer_sets_.size() ; index <= end_index ; ++index ) {
92  accumulated_nirs += rotamer_sets_[ index ]->num_rotamers();
93  }
94  return accumulated_nirs;
95  }
96 
98  core::pose::Pose & pose,
99  protocols::loops::LoopsOP const flexible_regions,
100  utility::vector1< core::Size > & closest_pos,
101  utility::vector1< core::Real > & closest_rmsd
102  )
103  {
104  core::Size this_nir( 0 );
105 
106  // Loop over rotamer sets in the collection
107  for( core::Size index = 1, end_index = rotamer_sets_.size() ; index <= end_index ; ++index ) {
109  // Loop over rotamers in set
110  for( core::Size rindex = 1, end_rindex = rotset->num_rotamers() ; rindex <= end_rindex ; ++rindex ) {
111  core::conformation::Residue const & this_rotamer( *rotset->rotamer( rindex ) );
112  // Loop over backbone positions to consider
113 
114  this_nir++;
115 
116  for( core::Size test_pos = 1, end_pos = pose.total_residue() ; test_pos <= end_pos ; ++test_pos ) {
117 
118  if( !flexible_regions->is_loop_residue( test_pos ) ) {
119  continue;
120  }
121 
122  core::Real test_rmsd = backbone_stub_match( pose.residue( test_pos ), this_rotamer );
123  if( test_rmsd < closest_rmsd[ this_nir ] ) {
124  closest_pos[ this_nir ] = test_pos;
125  closest_rmsd[ this_nir ] = test_rmsd;
126  }
127 
128  }
129  }
130  }
131  }
132 
134  {
135  // Set up the initial state stuff and fire off the actual
136  // recursive routine
137 
138  reset_unique_id();
139 
140  core::Size start_depth( 1 ); // Current recursion depth
141  std::map< core::Size, MotifCOP > setpos; // previously incorporated positions
142  std::map< core::Size, core::conformation::ResidueCOP > setpos_ir; // previously incorporated positions
143  std::map< core::Size, bool > setpos_forward_info; // whether previous motifs are forward or reverse
144 
145  // makes loop residues alanine, except pro and gly, which stay the same
146  mutate_loops_for_search( pose, *flexible_regions );
147 
148  // Relax away any clashes in the initial loop
150  core::scoring::methods::EnergyMethodOptions options( score_fxn->energy_method_options() );
151  options.exclude_DNA_DNA( false );
153  hb_options->exclude_DNA_DNA( false );
154  hb_options->use_hb_env_dep( false );
155  options.hbond_options( *hb_options );
156  score_fxn->set_energy_method_options( options );
157  protocols::loops::loop_mover::refine::LoopMover_Refine_Backrub pre_loop_refine( flexible_regions, score_fxn );
158  pre_loop_refine.apply( pose );
159 
160  pose.dump_pdb( "alanine.pdb" );
161 
162  try_for_more( pose, flexible_regions, setpos, setpos_ir, setpos_forward_info, start_depth );
163 
164  return;
165  }
166 
167  // This is the handler that tries to incorporate motifs into a flexible backbone region ad nauseum.
168  // It does the logic for which motifs to incorporate and spawns further branchings of the tree search
169  // recursively. The one notable thing it does not do is the attempt at loop closure/relaxtion onto
170  // the motif itself.
172  core::pose::Pose & start_pose,
173  protocols::loops::LoopsOP const flexible_regions,
174  std::map< core::Size, MotifCOP > setpos, // Note: Passed by value (not by reference) intentionally
175  std::map< core::Size, core::conformation::ResidueCOP > setpos_ir, // Note: Passed by value (not by reference) intentionally
176  std::map< core::Size, bool > setpos_forward_info, // Note: Passed by value (not by reference) intentionally
177  core::Size depth
178  )
179  {
180  // Here we set up a number of parameters, cutoffs, etc. that should be data members of this
181  // class. The class itself should be renamed from IRCollection to MotifSearch or something
182  // to denote that it does the motif incorporation and is not just a bag of inverse rotamers.
183 
184  core::Size const nirs( nirotamers() );
185 
186  utility::vector1< core::Size > closest_pos( nirs, 0 );
187  utility::vector1< core::Real > closest_rmsd( nirs, 9999.0 );
188 
189  core::Real motif_rmsd_cutoff( basic::options::option[ basic::options::OptionKeys::motifs::close_enough ]() );
190  core::Size max_depth( basic::options::option[ basic::options::OptionKeys::motifs::max_depth ]() );
191 
192  core::Size this_nir( 0 ); // IRs are stored in rotamer sets - this is maintained to count up in a flat array
193 
194  irt << "Entering search at recursion depth " << depth << std::endl;
195 
196  if( depth <= max_depth ) {
197 
198  // Print out the positions with motifs already incorporated
199  for( std::map< core::Size, MotifCOP >::const_iterator itr = setpos.begin(), e_itr = setpos.end() ; itr != e_itr ; ++itr ) {
200  irt << "Position " << itr->first << " is set." << std::endl;
201  }
202 
203  // Find the closest backbone stubs for each inverse rotamer
204  find_closest_backbone( start_pose, flexible_regions, closest_pos, closest_rmsd );
205 
206  // Loop through the full set of inverse rotamers
207  for( core::Size index = 1, end_index = rotamer_sets_.size() ; index <= end_index ; ++index ) {
209 
210 
211  // Loop over rotamers in set
212  for( core::Size rindex = 1, end_rindex = rotset->num_rotamers() ; rindex <= end_rindex ; ++rindex ) {
213  core::conformation::Residue const & this_rotamer( *rotset->rotamer( rindex ) );
214  // Loop over backbone positions to consider
215 
216  this_nir++;
217  core::Size const this_pos( closest_pos[ this_nir ] );
218 
219  // Here we disqualify positions that are already set, and those not in the defined flexible region
220  if( setpos.find( this_pos ) != setpos.end() ||
221  !flexible_regions->is_loop_residue( this_pos ) ) {
222  continue;
223  }
224 
225  // Here we disqualify inverse rotamers that are too far away
226  if( closest_rmsd[ this_nir ] > motif_rmsd_cutoff ) {
227  continue;
228  }
229 
230  // Add a test to make sure this inverse rotamer doesn't clash with any of those previouisly incorporated
231 
232 
233  // Attempt the incorporation for this rotamer
234  irt << "Attempting to incorporate a motif at position " << this_pos << " with initial rmsd of " <<
235  closest_rmsd[ this_nir ] << std::endl;
236 
237  core::pose::Pose pose;
238  pose = start_pose;
239 
240  // Loop closure attempt
241  if( successful_loop_closure( pose, flexible_regions, setpos, setpos_ir, setpos_forward_info, this_rotamer, this_pos, motif_source_[ index ], motif_forward_[ index ] ) ) {
242 
243  core::pose::Pose new_start_pose;
244  new_start_pose = pose;
245 
246  std::map< core::Size, MotifCOP > new_setpos( setpos );
247  new_setpos[ this_pos ] = motif_source_[ index ];
248  std::map< core::Size, core::conformation::ResidueCOP > new_setpos_ir( setpos_ir );
249  new_setpos_ir[ this_pos ] = this_rotamer;
250  std::map< core::Size, bool > new_setpos_forward_info( setpos_forward_info );
251  new_setpos_forward_info[ this_pos ] = motif_forward_[ index ];
252 
253  try_for_more( new_start_pose, flexible_regions, new_setpos, new_setpos_ir, new_setpos_forward_info, depth+1 );
254 
255  }
256  }
257  }
258  }
259 
260  // Manipulate the pdb info to hide info about the motif in the pdb file
261 
262  // Score and output the file
263  std::string unique_name( "test" );
264 
265 // std::string unique_name( make_loop_pdb_name( setpos, unique_id() ) );
267  std::string motif_filename( make_motif_filename( setpos, setpos_forward_info, start_pose ) );
268  std::string my_output_path( basic::options::option[ basic::options::OptionKeys::out::path::pdb ]() );
269  std::string filename( unique_name + "_" + motif_filename + "_" + utility::to_string( unique_id() ) + ".pdb" );
270  std::string try_filename( my_output_path + "/" + unique_name + "_" + motif_filename + "_" + utility::to_string( unique_id() ) + ".pdb" );
271 
272 // core::scoring::ScoreFunctionOP score_fxn( core::scoring::ScoreFunctionFactory::create_score_function( "soft_rep_design" ) );;
274  core::scoring::methods::EnergyMethodOptions options( score_fxn->energy_method_options() );
275  options.exclude_DNA_DNA( false );
277  hb_options->exclude_DNA_DNA( false );
278  hb_options->use_hb_env_dep( false );
279  options.hbond_options( *hb_options );
280  score_fxn->set_energy_method_options( options );
281 
282  start_pose.dump_scored_pdb( filename, *score_fxn );
283 
284  irt << "Finished search at recursion depth " << depth << std::endl;
285 
286  return;
287  }
288 
289 
290  bool
292  core::pose::Pose & pose,
293  protocols::loops::LoopsOP flexible_regions,
294  std::map< core::Size, MotifCOP > & setpos,
295  std::map< core::Size, core::conformation::ResidueCOP > & setpos_ir,
296  std::map< core::Size, bool > & setpos_forward_info,
297  core::conformation::Residue const & this_rotamer,
298  core::Size const this_pos,
299  MotifCOP this_motif,
300  bool const this_forward_info
301  )
302  {
303  using namespace core::scoring;
304 
305  core::Real coordinate_cst_weight( 10.0 );
306  core::Real num_inverse_rotamers( 1.0 + setpos.size() ); // not an int because I need it for math
307 
308 // ScoreFunctionOP score_fxn( ScoreFunctionFactory::create_score_function( "soft_rep_design" ) );;
310  methods::EnergyMethodOptions options( score_fxn->energy_method_options() );
311  options.exclude_DNA_DNA( false );
313  hb_options->exclude_DNA_DNA( false );
314  hb_options->use_hb_env_dep( false );
315  options.hbond_options( *hb_options );
316  score_fxn->set_energy_method_options( options );
317  score_fxn->set_weight( coordinate_constraint, coordinate_cst_weight );
318 
319  // Get bb constraints to this inverse rotamer
320 
322  add_motif_bb_constraints( bb_cst_set, pose, this_pos, this_rotamer );
323 
324  // For previously set rotamers, set the sidechain constraints only
325  for( std::map< core::Size, MotifCOP >::const_iterator itr = setpos.begin(), e_itr = setpos.end() ; itr != e_itr ; ++itr ) {
326  core::Size const prev_pos( itr->first );
327  add_motif_sc_constraints( bb_cst_set, pose, prev_pos, *(setpos_ir[ prev_pos ]), itr->second, setpos_forward_info[ prev_pos ] );
328  }
329 
330  pose.constraint_set( bb_cst_set );
331 
332  (*score_fxn)(pose);
333  irt << "Before backbone refinement constraints score is " << pose.energies().total_energies()[ coordinate_constraint ] / num_inverse_rotamers << std::endl;
334 
335  // Perform some kind of loop relaxation
336  protocols::loops::loop_mover::refine::LoopMover_Refine_Backrub loop_refine( flexible_regions, score_fxn );
337 
338  loop_refine.apply( pose );
339 
340  // Evaluate the constraints energy to see how we did
341  (*score_fxn)(pose);
342  core::Real bb_constraint_check( pose.energies().total_energies()[ coordinate_constraint ] / num_inverse_rotamers );
343  irt << "After backbone refinement constraints score is " << bb_constraint_check << std::endl;
344 
345  pose.remove_constraints();
346 
347  if( bb_constraint_check > 1.0 ) {
348  return false;
349  }
350 
351  // Move the inverse rotamer onto the backbone
352  pose.replace_residue( this_pos, this_rotamer, true );
353 
354  // Now put constraints on the actual atoms in the motif
355 
357  add_motif_sc_constraints( sc_cst_set, pose, this_pos, this_rotamer, this_motif, this_forward_info );
358 
359  // For previously set rotamers, set their sidechain constraints as well
360  for( std::map< core::Size, MotifCOP >::const_iterator itr = setpos.begin(), e_itr = setpos.end() ; itr != e_itr ; ++itr ) {
361  core::Size const prev_pos( itr->first );
362  add_motif_sc_constraints( sc_cst_set, pose, prev_pos, *(setpos_ir[ prev_pos ]), itr->second, setpos_forward_info[ prev_pos ] );
363  }
364 
365  pose.constraint_set( sc_cst_set );
366 
367  (*score_fxn)(pose);
368  irt << "Before sidechain refinement constraints score is " << pose.energies().total_energies()[ coordinate_constraint ] / num_inverse_rotamers << std::endl;
369 
370  protocols::loops::loop_mover::refine::LoopMover_Refine_Backrub sc_loop_refine( flexible_regions, score_fxn );
371 
372  sc_loop_refine.apply( pose );
373 
374  // Evaluate the constraints energy to see how we did
375  (*score_fxn)(pose);
376  core::Real sc_constraint_check( pose.energies().total_energies()[ coordinate_constraint ] / num_inverse_rotamers );
377  irt << "After sidechain refinement constraints score is " << sc_constraint_check << std::endl;
378 
379  pose.remove_constraints();
380 
381  if( sc_constraint_check > 1.0 ) {
382  return false;
383  }
384 
385  return true;
386  }
387 
390  std::map< core::Size, MotifCOP > & setpos,
391  std::map< core::Size, bool > & setpos_forward_info,
392  core::pose::Pose & pose
393  )
394  {
395  std::string accum_string;
396  for( std::map< core::Size, MotifCOP >::const_iterator itr = setpos.begin(), e_itr = setpos.end() ; itr != e_itr ; ++itr ) {
397  core::Size const this_pos( itr->first );
398  if( setpos_forward_info[ this_pos ] ) {
399  accum_string += itr->second->restype_name2() + utility::to_string( pose.pdb_info()->number( this_pos ) );
400  } else {
401  accum_string += itr->second->restype_name1() + utility::to_string( pose.pdb_info()->number( this_pos ) );
402  }
403  }
404  return accum_string;
405  }
406 
407 }
408 }