Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PackRotamersMoverPartGreedy.cc
Go to the documentation of this file.
1 /// @file
2 /// @brief Partly greedy pack rotamers mover. given a set of target residues, choose their neighbors greedily and then
3 /// @brief pack with usual simulated annealing maintaining these neighbor choices
4 /// @author Sagar Khare (khares@uw.edu)
5 
6 // Unit headers
10 
13 #include <core/pose/selection.hh>
14 
19 #include <core/pose/Pose.hh>
26 #include <core/scoring/Energies.hh>
28 #include <basic/Tracer.hh>
30 #include <utility/exit.hh>
31 #include <utility/tag/Tag.hh>
33 
34 //#include <core/pack/task/operation/TaskOperation.fwd.hh>
35 //#include <utility/vector0.hh>
36 #include <utility/vector1.hh>
37 //#include <utility/string_util.hh>
38 // Numeric Headers
39 #include <numeric/random/random.hh>
40 #include <numeric/random/random_permutation.hh>
41 #include <utility/sort_predicates.hh>
42 
43 
44 namespace protocols {
45 namespace enzdes {
46 
47 using namespace core;
48 using namespace pack;
49 using namespace task;
50 using namespace operation;
51 using namespace scoring;
52 
53 using basic::Warning;
54 using basic::t_warning;
55 static basic::Tracer TR("protocols.enzdes.PackRotamersMoverPartGreedy");
56 static numeric::random::RandomGenerator trg_RG(10805); // <- Magic number, do not change it!!!
57 
60 {
62 }
63 
66 {
67  return new PackRotamersMoverPartGreedy;
68 }
69 
71  ScoreFunctionOP scorefxn,
72  PackerTaskOP task,
73  utility::vector1 <core::Size> target_residues
74  ) :
75 
76  protocols::moves::Mover("PackRotamersMoverPartGreedy"),
77  scorefxn_repack_ (scorefxn),
78  scorefxn_minimize_ (scorefxn),
79  task_ (task),
80  target_residues_ (target_residues)
81 {}
82 
84  protocols::moves::Mover("PackRotamersMoverPartGreedy"),
85  scorefxn_repack_(0),
86  scorefxn_minimize_(0),
87  task_factory_(0),
88  target_residues_(0),
89  threshold_(0),
90  n_best_(0)
91 {}
92 
95 {
96  return "PackRotamersMoverPartGreedy";
97 }
98 
99 
101 
102 void
104  TagPtr const tag,
105  protocols::moves::DataMap & datamap,
106  Filters_map const &,
108  Pose const & pose
109 )
110 {
111  //task operations
112  if( tag->hasOption("task_operations") ) task_factory( protocols::rosetta_scripts::parse_task_operations( tag, datamap ) );
113  else task_factory_ = NULL;
114  //Scorefunctions
115  std::string const scorefxn_repack( tag->getOption<std::string>( "scorefxn_repack", "score12" ) );
116  std::string const scorefxn_repack_greedy( tag->getOption<std::string>( "scorefxn_repack_greedy", "score12" ) );
117  std::string const scorefxn_minimize( tag->getOption<std::string>( "scorefxn_minimize", "score12" ) );
118  using namespace core::scoring;
119  scorefxn_repack_ = new ScoreFunction( *datamap.get< ScoreFunction * >( "scorefxns", scorefxn_repack ) );
120  scorefxn_repack_greedy_ = new ScoreFunction( *datamap.get< ScoreFunction * >( "scorefxns", scorefxn_repack_greedy ) );
121  scorefxn_minimize_ = new ScoreFunction( *datamap.get< ScoreFunction * >( "scorefxns", scorefxn_minimize ) );
122  //target residues for greedy opt around
123  if( tag->hasOption("target_residues") ) {
124  target_residues_ = core::pose::get_resnum_list(tag, "target_residues",pose);
125  }
126  use_cstids_ = false;
127  if( tag->hasOption("target_cstids") ) {
128  cstid_list_ = tag->getOption<std::string>("target_cstids", "" );
129  use_cstids_ = true;
130  }
131  //distance threshold for neighbor check in greedy opt
132  threshold_ = tag->getOption<core::Real>("distance_threshold", 8.0 );
133  //if choose targets based on N-best residues
134  if( tag->hasOption("choose_best_n") ) {
135  n_best_ = tag->getOption<core::Size>("choose_best_n", 0 );
136  }
137 }
138 
142 }
143 
146 {
147  return new PackRotamersMoverPartGreedy( *this );
148 }
149 
152 {
153  return new PackRotamersMoverPartGreedy;
154 }
155 
156 
157 void
159 {
160  //create task
161  using namespace core::pack::task;
162  core::pose::Pose greedy_pose = pose;
163  TR<<"Creating packer task based on specified task operations..."<< std::endl;
164  if ( task_factory_ !=0 ){
166  task_ = task_factory_->create_task_and_apply_taskoperations( greedy_pose );
167  }
168  else{
170  task_ = tf->create_task_and_apply_taskoperations( greedy_pose );
171  }
172  (*scorefxn_repack_)(greedy_pose);
173  // make sure we have some target residues
174  if (use_cstids_){ // enzdes csts if specified
177  target_residues_.insert( target_residues_.begin(), trg_res.begin(), trg_res.end() );
178  std::unique( target_residues_.begin(), target_residues_.end() );
179 
180  }
181  if (n_best_>0){
182  utility::vector1< core::Size > n_best_res = choose_n_best( greedy_pose, n_best_ );
183  target_residues_.insert( target_residues_.begin(), n_best_res.begin(), n_best_res.end() );
184  std::unique( target_residues_.begin(), target_residues_.end() );
185  }
186  runtime_assert(target_residues_.size()>0);
187  //randomly shuffle targets
188  random_permutation( target_residues_ , trg_RG );
189  //Make sure target residues are held fixed
190  for( utility::vector1< core::Size >::const_iterator pos_it = target_residues_.begin(); pos_it != target_residues_.end(); ++pos_it ){
191  task_->nonconst_residue_task( *pos_it ).prevent_repacking();
192  }
193  //Convert to Ala
195  for (core::Size ii=1;ii<=greedy_pose.total_residue();++ii){
196  if (task_->being_designed(ii)) toAla.push_back( ii );
197  }
198  protocols::toolbox::pose_manipulation::construct_poly_ala_pose( greedy_pose, toAla, true, true, true );
199  //Then, greedy opt around user specified target residues, this will modify the task and pose
201  //greedy_pose.dump_pdb("after_greedy.pdb");
202  //Now just replace the target_residues and the greedily optimized positions in original pose with greedy pose, set task to reflect these
203  for( utility::vector1< core::Size >::const_iterator pos_it = target_residues_.begin(); pos_it != target_residues_.end(); ++pos_it ){
204  pose.replace_residue( *pos_it, greedy_pose.residue(*pos_it), true);
205  }
207  if (!(greedy_pose.residue(*res).name3() == "ALA")) pose.replace_residue( *res, greedy_pose.residue(*res), true);
208  }
209  //old task is no longer valid. create a new one.
210  PackerTaskOP task = task_factory_->create_task_and_apply_taskoperations( pose );
211  TR<<" Restrcting the following greedily found residues to repack: ";
213  {
214  if (!(greedy_pose.residue(*res).name3() == "ALA")) task->nonconst_residue_task( *res ).restrict_to_repacking();
215  TR<< *res << "+";
216  }
217  TR<< std::endl;
218  task->initialize_from_command_line().or_include_current( true );
219  //Next hand over these to regular PackRotamers mover and we're done!
221  pack->apply( pose );
222 
223 }
224 
225 void
227  core::pose::Pose & pose,
231 )
232 {
233  using namespace core::pack;
234  using namespace core::pack::task;
235 
236  core::pose::Pose cur_pose = pose;
237 
238 
239  for( utility::vector1< core::Size >::const_iterator pos_it = target_residues.begin(); pos_it != target_residues.end(); ++pos_it ){
240  TR<<"Considering target residue "<< cur_pose.residue( *pos_it ).name3() << *pos_it <<": "<<std::endl;
241  utility::vector1< core::Size > neighbors = compute_designable_neighbors( *pos_it, task, cur_pose );
242  utility::vector1< core::Size > cur_neighbors = neighbors;
243  utility::vector1< bool > allow_minimization (pose.total_residue(), false);
244  for (utility::vector1< core::Size>::const_iterator iter = neighbors.begin(); iter != neighbors.end(); ++iter){
245  allow_minimization[ *iter ] = true;
246  }
247  allow_minimization[*pos_it] = true;
248  // Iterate neighbors.size() times
249  TR << "Total number of neighbors are "<<neighbors.size()<<std::endl;
250  for (core::Size ii=1;ii<=neighbors.size();++ii){
251  protocols::toolbox::pose_manipulation::construct_poly_ala_pose( cur_pose, cur_neighbors, false, false, true );
252  // cur_pose.dump_pdb("polyA_"+utility::to_string(ii)+".pdb");
253  TR << "At iteration "<< ii << ", current neighbor size is "<< cur_neighbors.size()<<std::endl;
254  core::Size best_neigh (0);
255  //calculate energy in polyAla state before choosing
256  core::Real best_energy (cur_pose.energies().total_energies()[core::scoring::total_score]);
257  TR <<" Poly Ala energy is :"<< best_energy<<std::endl;
258  core::pose::Pose inner_pose = cur_pose;
259  // Iterate over all neighbors at which no choice has been made so far and choose best in the context of current state
260  for( utility::vector1< core::Size >::const_iterator neigh_it = cur_neighbors.begin(); neigh_it != cur_neighbors.end(); ++neigh_it ){
261  core::pose::Pose working_pose = inner_pose;
262  // make a task
264  core::pack::task::PackerTaskOP mutate_residue = mut_res->create_task_and_apply_taskoperations( working_pose );
265  mutate_residue->initialize_from_command_line().or_include_current( true );
266  // upweight interactions of this position to those within the cluster (to favor intracluster interactions)
268  upweight_1.push_back( *neigh_it );
269  utility::vector1< core::Size > upweight_2 = neighbors;
270  upweight_2.push_back( *pos_it );
271  //upweight interactions between this position and neighbors
272  IGEdgeReweighterOP ig_up = new protocols::toolbox::ResidueGroupIGEdgeUpweighter( 2.5, upweight_1 , upweight_2 );
273  mutate_residue->set_IGEdgeReweights()->add_reweighter( ig_up );
274  // fix everything else except this position
276 
277  for (core::Size i=1; i<=pose.total_residue(); ++i){
278  if (i== *neigh_it) mutate_residue->nonconst_residue_task( i ).restrict_absent_canonical_aas( keep_aas );
279  else mutate_residue->nonconst_residue_task( i ).prevent_repacking();
280  }
281  // Now run design on one position
283  prm->apply( working_pose );
284  //Minimize the sidechains of all designed residues so far
286  movemap->set_jump( false );
287  movemap->set_bb( false );
288  movemap->set_chi( allow_minimization );
289  protocols::simple_moves::MinMoverOP minmover = new protocols::simple_moves::MinMover( movemap, scorefxn_minimize_, "dfpmin_armijo_nonmonotone_atol", 0.02, true /*use_nblist*/);
290  minmover->apply(working_pose);
291 
292  //Check energy
293  core::Real this_energy = working_pose.energies().total_energies()[core::scoring::total_score];
294  //TR << "Energy for best residue at position "<< *neigh_it << "is "<< this_energy << std::endl;
295  // see if this energy is best so far. if so replace this neighbor in cur_pose
296  if ( (this_energy<best_energy) || (best_neigh==0) ) {
297  //TR<< "Best neighbor at position "<<*neigh_it <<" is "<< working_pose.residue( *neigh_it ).name3() << " with energy "<< this_energy<<std::endl;
298  cur_pose = working_pose;
299  best_neigh = *neigh_it;
300  best_energy = this_energy;
301  }
302  } // all currently unchosen neighbors so far
303  //Now that a choice has been made at the position best_neigh, inform cur_neighbors and task to take this position out from future design
304  update_task_and_neighbors( best_neigh, task, cur_neighbors );
305  } //iterate neighbors.size() times around each target residue
306  } //all target residues
307  pose = cur_pose;
308 } // greedy around
309 
312 core::Size const & position,
314 core::pose::Pose const & pose
315 )
316 {
318  TR << "neighbors of position "<< position << " are: ";
319  core::conformation::Residue const res_pos (pose.residue( position ));
320  for (core::Size ii=1;ii<=pose.total_residue();++ii){
321  if ( pose.residue( ii ).is_protein() && task->being_designed( ii ) ){
322  core::conformation::Residue const res_ii (pose.residue( ii ));
323  core::Real distance = res_ii.xyz( res_ii.nbr_atom() ).distance( res_pos.xyz(res_pos.nbr_atom()) );
324  if (distance<=threshold_) {
325  neighbors.push_back( ii );
326  TR <<ii<<" + ";
327  }
328  }
329  }
330  TR<<std::endl;
331 // TR<< "In function total neighbors are"<< neighbors.size() <<std::endl;
332  return neighbors;
333 }
334 
335 void
337 core::Size const & best_neigh,
339 utility::vector1< core::Size > & current_neighbors
340 )
341 {
342  task->nonconst_residue_task( best_neigh ).restrict_to_repacking();
343  restrict_to_repacking_.push_back( best_neigh );
344  utility::vector1< core::Size > temp_neighbors;
345  for( utility::vector1< core::Size >::const_iterator neigh_it = current_neighbors.begin(); neigh_it != current_neighbors.end(); ++neigh_it ){
346  if (*neigh_it != best_neigh) temp_neighbors.push_back(*neigh_it);
347  else TR<<" Updated current neighbors by removing "<<*neigh_it << " from it."<<std::endl;
348  }
349  current_neighbors = temp_neighbors;
350 
351 
352 }
353 
354 void
356  task_factory_ = p;
357 }
358 
359 void
361  task_ = task;
362 }
363 
364 void
366  target_residues_ = trg_res;
367 }
368 
371 
372  using namespace core::graph;
373  using namespace core::scoring;
374  std::list< std::pair< core::Size, core::Real > > residue_energies;
375  utility::vector1<core::Size> chosen_residues;
376  core::pose::Pose nonconst_pose( pose );
377  (*scorefxn_repack_)(nonconst_pose);
378  EnergyMap const curr_weights = nonconst_pose.energies().weights();
379  core::Size res (1);
380  for (core::Size ii=1;ii<=pose.total_residue();++ii){
381  if (pose.residue( ii ).is_ligand()) res = ii;
382  }
383  //Fill residue_energies of interface residues by traversing energy graph
384  for( EdgeListConstIterator egraph_it = nonconst_pose.energies().energy_graph().get_node( res )->const_edge_list_begin(); egraph_it != nonconst_pose.energies().energy_graph().get_node( res )->const_edge_list_end(); ++egraph_it){
385  core::Size const int_resi = (*egraph_it)->get_other_ind( res );
386  EnergyEdge const * Eedge = static_cast< EnergyEdge const * > (*egraph_it);
387  core::Real intE = Eedge->dot( curr_weights );
388  residue_energies.push_back( std::make_pair( int_resi, intE));
389  }//for each egraph_it
390 
391  //Sort and return n_best
392  residue_energies.sort( utility::SortSecond< core::Size, core::Real >() );
393  core::Size counter(1);
394  for( std::list< std::pair<core::Size, core::Real> >::iterator it = residue_energies.begin();counter<=n_best; ++it){
395  chosen_residues.push_back( it->first );
396  TR << "Chose residue "<<it->first<<" with interface energy "<<it->second <<std::endl;
397  counter++;
398  }
399  return chosen_residues;
400 }
401 /*core::PackerEnergy PackRotamersMoverPartGreedy::run_with_ig( Pose & pose, utility::vector0< int > rot_to_pack, InteractionGraphBaseOP ig) const
402 {
403  return pack_rotamers_run( pose, task(), rotamer_sets(), ig, rot_to_pack );
404 }
405 */
406 
407 }//moves
408 }//protocols