Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rotamer_trials.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 core/pack/rotamer_trials.cc
11 /// @brief rotamer trials module
12 /// @author Andrew Leaver-Fay (leaverfa@email.unc.edu)
13 
14 // Unit headers
16 
17 // Package headers
18 
23 // AUTO-REMOVED #include <core/pack/rotamer_set/symmetry/SymmetricRotamerSet_.hh>
27 
28 // Project headers
29 #include <core/types.hh>
30 
31 #include <core/graph/Graph.hh>
32 
33 #include <core/pose/Pose.hh>
34 
36 // AUTO-REMOVED #include <core/scoring/hbonds/hbonds.hh>
37 // AUTO-REMOVED #include <core/scoring/hbonds/HBondSet.hh>
38 
40 
41 #include <basic/prof.hh> // should this guy go into utilities?
42 #include <basic/Tracer.hh>
43 
44 // Utility headers
45 #include <utility/vector1.hh>
46 #include <utility/vector1.functions.hh>
47 
48 // Numeric Headers
49 #include <numeric/random/random.hh>
50 #include <numeric/random/random_permutation.hh>
51 
53 // AUTO-REMOVED #include <core/conformation/symmetry/util.hh>
54 
55 
56 
57 // STL headers
58 #ifdef WIN32
59 #include <ctime>
60 #endif
61 
63 #include <utility/vector0.hh>
64 
65 
66 namespace core {
67 namespace pack {
68 
71 
72 static numeric::random::RandomGenerator rottrials_RG(10801); // <- Magic number, do not change it!!!
73 
74 static basic::Tracer TR( "core.pack.rotamer_trials" );
75 
78  task::PackerTask const & the_task,
79  pose::Pose & pose
80 );
81 
82 void
84  pose::Pose & pose,
85  scoring::ScoreFunction const & scfxn,
86  task::PackerTaskCOP input_task
87 )
88 {
89  using namespace numeric::random;
90 
91  //fpd safety check for symmetry
92  if ( core::pose::symmetry::is_symmetric( pose ) ) {
93  symmetric_rotamer_trials( pose, scfxn, input_task );
94  return;
95  }
96 
97  //clock_t starttime = clock();
98  PROF_START( basic::ROTAMER_TRIALS );
99 
100  pack_scorefxn_pose_handshake( pose, scfxn);
101 
103 
104  utility::vector1< uint > residues_for_trials( repackable_residues( *input_task ));
105  // Replace this random shuffle with one based on rosetta's RNG
106  //random__shuffle(residues_for_trials.begin(), residues_for_trials.end() );
107  random_permutation( residues_for_trials, rottrials_RG );
108 
109  task::PackerTaskOP rottrial_task( input_task->clone() );
110 
111  rottrial_task->set_bump_check( false );
112  rottrial_task->or_include_current( true );
113  rottrial_task->temporarily_fix_everything();
114 
115  // this will call setup fxns for each scoring method, eg HBondEnergy will
116  // compute backbone hbonds to prepare for hbchecking,
117  // PairEnergy will update actcoords...
118  scfxn.setup_for_packing( pose, rottrial_task->repacking_residues(), rottrial_task->designing_residues() );
119 
121 
122  //Energy last_energy( 0.0 );
123  graph::GraphOP packer_neighbor_graph = create_packer_graph( pose, scfxn, input_task );
124 
125  bool replaced_residue( false );
126 
127  Size const num_in_trials = residues_for_trials.size();
128  for (Size ii = 1; ii <= num_in_trials; ++ii)
129  {
130  pose.update_residue_neighbors(); // will return if uptodate
131 
132  int resid = residues_for_trials[ ii ];
133  conformation::Residue const & trial_res = pose.residue( resid );
134 
135  //pretend this is a repacking and only this residue is being repacked
136  //while all other residues are being held fixed.
137  rottrial_task->temporarily_set_pack_residue( resid, true );
138 
139  rotamer_set::RotamerSetOP rotset = rsf.create_rotamer_set( trial_res );
140  rotset->set_resid( resid );
141  rotset->build_rotamers( pose, scfxn, *rottrial_task, packer_neighbor_graph );
142 
143  scfxn.prepare_rotamers_for_packing( pose, *rotset );
144 
145  utility::vector1< core::PackerEnergy > one_body_energies( rotset->num_rotamers() );
146 
147  rotset->compute_one_body_energies(
148  pose, scfxn, *rottrial_task,
149  packer_neighbor_graph, one_body_energies );
150 
151  //select the best rotamer
152  Size bestrot = utility::arg_min( one_body_energies );
153 
154  //don't replace if the best rotamer is the one that's already assigned
155  TR.Trace << "rottrial at position " << resid << " nrot= " << rotset->num_rotamers() << std::endl;
156  if ( bestrot != rotset->id_for_current_rotamer() )
157  {
158  replaced_residue = true;
159  conformation::ResidueOP newresidue( rotset->rotamer( bestrot )->clone() );//create_residue() );
160  pose.replace_residue ( resid, *newresidue, false );
161  scfxn.update_residue_for_packing( pose, resid );
162  TR.Trace << "rottrial accept: " << resid << " bestrot: " << bestrot << ' ' << one_body_energies[ bestrot ];
163  if( rotset->id_for_current_rotamer() != 0 ){ //more output in this case
164  //sml this output is protected because id_for_current_rotamer is incompatible with forced mutations (eg PIKAA)
165  TR.Trace << ' ' << one_body_energies[ rotset->id_for_current_rotamer() ] << ' ' <<
166  one_body_energies[ bestrot ] - one_body_energies[ rotset->id_for_current_rotamer() ];
167  }
168  TR.Trace << std::endl;
169  }
170 
171  rottrial_task->temporarily_set_pack_residue( resid, false );
172 
173  }
174  //clock_t stoptime = clock();
175  PROF_STOP ( basic::ROTAMER_TRIALS );
176  //std::cout << "Rotamer trials took " << ((double) stoptime - starttime ) / CLOCKS_PER_SEC << " seconds" << std::endl;
177  if ( replaced_residue ) {
178  // rescore here to make Energies GOOD
179  scfxn( pose );
180  }
181 }
182 
185 {
186  utility::vector1< int > to_be_packed( the_task.num_to_be_packed() );
187  uint count = 0;
188  for (uint ii = 1; ii <= the_task.total_residue(); ++ii )
189  {
190  if ( the_task.pack_residue( ii ) )
191  {
192  ++count;
193  to_be_packed[ count ] = ii;
194  }
195  }
196  return to_be_packed;
197 }
198 
199 void
201  pose::Pose & pose,
202  scoring::ScoreFunction const & scfxn,
203  task::PackerTaskCOP non_symmetric_task
204 )
205 {
206  using namespace numeric::random;
207  using namespace conformation::symmetry;
208 
209 //clock_t starttime = clock();
210  PROF_START( basic::ROTAMER_TRIALS );
211 
212  task::PackerTaskCOP input_task = make_new_symmetric_PackerTask_by_requested_method(pose,non_symmetric_task);
213 
214  pack_scorefxn_pose_handshake( pose, scfxn);
215 
217 
218  utility::vector1< uint > residues_for_trials( symmetric_repackable_residues( *input_task, pose ));
219  // Replace this random shuffle with one based on rosetta's RNG
220  //random__shuffle(residues_for_trials.begin(), residues_for_trials.end() );
221  random_permutation( residues_for_trials, rottrials_RG );
222 
223  task::PackerTaskOP rottrial_task( input_task->clone() );
224 
225  rottrial_task->set_bump_check( false );
226  rottrial_task->or_include_current( true );
227  rottrial_task->temporarily_fix_everything();
228 
229  // this will call setup fxns for each scoring method, eg HBondEnergy will
230  // compute backbone hbonds to prepare for hbchecking,
231  // PairEnergy will update actcoords...
232  scfxn.setup_for_packing( pose, rottrial_task->repacking_residues(), rottrial_task->designing_residues() );
233 
235 
236  //Energy last_energy( 0.0 );
237  graph::GraphOP packer_neighbor_graph = create_packer_graph( pose, scfxn, input_task );
238 
239  bool replaced_residue( false );
240 
241  Size const num_in_trials = residues_for_trials.size();
242  for (Size ii = 1; ii <= num_in_trials; ++ii)
243  {
244  pose.update_residue_neighbors(); // will return if uptodate
245 
246  int resid = residues_for_trials[ ii ];
247  conformation::Residue const & trial_res = pose.residue( resid );
248 
249  //pretend this is a repacking and only this residue is being repacked
250  //while all other residues are being held fixed.
251  rottrial_task->temporarily_set_pack_residue( resid, true );
252 
253  rotamer_set::RotamerSetOP rotset = rsf.create_rotamer_set( trial_res );
254  rotset->set_resid( resid );
255  rotset->build_rotamers( pose, scfxn, *rottrial_task, packer_neighbor_graph );
256 
257  scfxn.prepare_rotamers_for_packing( pose, *rotset );
258 
259  utility::vector1< core::PackerEnergy > one_body_energies( rotset->num_rotamers() );
260 
261  rotset->compute_one_body_energies(
262  pose, scfxn, *rottrial_task,
263  packer_neighbor_graph, one_body_energies );
264 
265  //select the best rotamer
266  Size bestrot = utility::arg_min( one_body_energies );
267 
268  //don't replace if the best rotamer is the one that's already assigned
269  TR.Trace << "rottrial at position " << resid << " nrot= " << rotset->num_rotamers() << std::endl;
270  if ( bestrot != rotset->id_for_current_rotamer() )
271  {
272  replaced_residue = true;
273  conformation::ResidueOP newresidue( rotset->rotamer( bestrot )->clone() );//create_residue() );
274  pose.replace_residue ( resid, *newresidue, false );
275  scfxn.update_residue_for_packing( pose, resid );
276  // clone to symmetrical positions
277  SymmetricConformation const & SymmConf ( dynamic_cast<SymmetricConformation const &> ( pose.conformation()) );
278  SymmetryInfoCOP symm_info( SymmConf.Symmetry_Info() );
279 
280  //fpd replace_residue is symmetric now ... still need to update scorefxn though
281  for ( std::vector< Size>::const_iterator
282  clone = symm_info->bb_clones( resid ).begin(),
283  clone_end = symm_info->bb_clones( resid ).end();
284  clone != clone_end; ++clone ){
285  // conformation::ResidueOP sym_rsd = newresidue->clone();
286  // sym_rsd->orient_onto_residue(pose.residue( *clone) );
287  // pose.replace_residue ( *clone, *sym_rsd, false );
288  scfxn.update_residue_for_packing( pose, *clone );
289  }
290  TR.Trace << "rottrial accept: " << resid << " bestrot: " << bestrot << ' ' << one_body_energies[ bestrot ];
291  if( rotset->id_for_current_rotamer() != 0 ){ //more output in this case
292  //sml this output is protected because id_for_current_rotamer is incompatible with forced mutations (eg PIKAA)
293  TR.Trace << ' ' << one_body_energies[ rotset->id_for_current_rotamer() ] << ' ' <<
294  one_body_energies[ bestrot ] - one_body_energies[ rotset->id_for_current_rotamer() ];
295 
296  }
297  TR.Trace << std::endl;
298  }
299 
300  rottrial_task->temporarily_set_pack_residue( resid, false );
301 
302  }
303  //clock_t stoptime = clock();
304  PROF_STOP ( basic::ROTAMER_TRIALS );
305  //std::cout << "Rotamer trials took " << ((double) stoptime - starttime ) / CLOCKS_PER_SEC << " seconds" << std::endl;
306  if ( replaced_residue ) {
307  // rescore here to make Energies GOOD
308  scfxn( pose );
309  }
310 
311 }
312 
315  task::PackerTask const & the_task,
316  pose::Pose & pose
317 )
318 {
319  using namespace conformation::symmetry;
320 
321  // find SymmInfo
322  SymmetricConformation const & SymmConf (
323  dynamic_cast<SymmetricConformation const &> ( pose.conformation() ) );
324  SymmetryInfoCOP symm_info( SymmConf.Symmetry_Info() );
325 
326  // First find out how many residues to pack. Silly, lets come up with a better way of sizing this array...
327  uint num_molten = 0;
328  for (uint res = 1; res <= the_task.total_residue(); ++res ) {
329  if ( the_task.pack_residue( res ) && symm_info->fa_is_independent( res )
330  && res <= symm_info->num_total_residues() )
331  {
332  ++num_molten;
333  }
334  }
335  utility::vector1< int > to_be_packed( num_molten );
336  uint count = 0;
337  for (uint ii = 1; ii <= the_task.total_residue(); ++ii )
338  {
339  if ( the_task.pack_residue( ii ) && symm_info->fa_is_independent( ii )
340  && ii <= symm_info->num_total_residues() )
341  {
342  ++count;
343  to_be_packed[ count ] = ii;
344  }
345  }
346  return to_be_packed;
347 }
348 
349 
350 } //end namespace core
351 } //end namespace pack