Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BackrubSidechainMover.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/backrub/BackrubSidechainMover.cc
11 /// @brief BackrubSidechainMover methods implemented
12 /// @author
13 
14 
15 // Unit Headers
18 
19 // Package Headers
20 
21 // Project Headers
26 // AUTO-REMOVED #include <protocols/canonical_sampling/ThermodynamicObserver.hh> // needed for Windows build
28 #include <core/pose/selection.hh>
33 #include <core/pose/PDBInfo.hh>
34 #include <core/pose/Pose.hh>
35 #include <numeric/MultiDimensionalHistogram.fwd.hh>
36 
37 // Utility Headers
38 #include <basic/Tracer.hh>
39 #include <core/types.hh>
40 #include <numeric/angle.functions.hh>
41 #include <numeric/constants.hh>
42 #include <numeric/conversions.hh>
43 #include <numeric/random/random.hh>
44 #include <utility/exit.hh>
45 #include <utility/io/ozstream.hh>
46 #include <utility/string_util.hh>
47 #include <utility/tag/Tag.hh>
48 
49 //Auto Headers
52 #include <utility/vector0.hh>
53 #include <utility/vector1.hh>
54 #include <numeric/MultiDimensionalHistogram.hh>
55 
56 //Auto Headers
57 #include <utility/excn/Exceptions.hh>
59 
60 // C++ Headers
61 
62 using basic::T;
63 using basic::Error;
64 using basic::Warning;
65 
66 static basic::Tracer TR( "protocols.backrub.BackrubSidechainMover" );
67 static numeric::random::RandomGenerator RG(2772);
68 
69 namespace protocols {
70 namespace backrub {
71 
75 }
76 
79  return new BackrubSidechainMover;
80 }
81 
84  return "BackrubSidechain";
85 }
86 
88 ) :
89  protocols::canonical_sampling::ThermodynamicMover(),
90  backrub_mover_(new protocols::backrub::BackrubMover),
91  sidechain_mover_(new protocols::simple_moves::sidechain_moves::SidechainMover),
92  record_statistics_(false),
93  statistics_filename_("brsc_stats.txt")
94 {
95  backrub_mover_->set_min_atoms(7);
96  backrub_mover_->set_max_atoms(7);
97 }
98 
100  BackrubSidechainMover const & mover
101 ) :
102  //utility::pointer::ReferenceCount(),
103  protocols::canonical_sampling::ThermodynamicMover(mover),
104  valid_segments_(mover.valid_segments_),
105  last_valid_segment_index_(mover.last_valid_segment_index_),
106  last_chi1_pre_(mover.last_chi1_pre_),
107  last_chi1_post_(mover.last_chi1_post_),
108  record_statistics_(mover.record_statistics_),
109  statistics_filename_(mover.statistics_filename_),
110  proposal_hists_(mover.proposal_hists_),
111  accept_hists_(mover.accept_hists_)
112 {
113  if (mover.backrub_mover_) {
114  backrub_mover_ = dynamic_cast<protocols::backrub::BackrubMover *>(mover.backrub_mover_->clone()());
115  runtime_assert(backrub_mover_);
116  }
117  if (mover.sidechain_mover_) {
119  runtime_assert(sidechain_mover_);
120  }
121 }
122 
124 
127 {
128  return new protocols::backrub::BackrubSidechainMover( *this );
129 }
130 
133 {
134  return new BackrubSidechainMover;
135 }
136 
139 {
140  return "BackrubSidechainMover";
141 }
142 
143 void
145  utility::tag::TagPtr const tag,
147  protocols::filters::Filters_map const & /*filters*/,
148  protocols::moves::Movers_map const & /*movers*/,
149  core::pose::Pose const & pose
150 )
151 {
152  if ( tag->hasOption("pivot_residues") ) {
153  set_pivot_residues(core::pose::get_resnum_list(tag, "pivot_residues", pose));
154  }
155 
157 
158  if ( tag->hasOption("task_operations") ) {
159 
160  std::string const t_o_val( tag->getOption<std::string>("task_operations") );
161  typedef utility::vector1< std::string > StringVec;
162  StringVec const t_o_keys( utility::string_split( t_o_val, ',' ) );
163  for ( StringVec::const_iterator t_o_key( t_o_keys.begin() ), end( t_o_keys.end() );
164  t_o_key != end; ++t_o_key ) {
165  if ( data.has( "task_operations", *t_o_key ) ) {
166  new_task_factory->push_back( data.get< core::pack::task::operation::TaskOperation* >( "task_operations", *t_o_key ) );
167  } else {
168  throw utility::excn::EXCN_RosettaScriptsOption("TaskOperation " + *t_o_key + " not found in protocols::moves::DataMap.");
169  }
170  }
171 
172  } else {
173 
174  new_task_factory->push_back( new core::pack::task::operation::RestrictToRepacking );
175  }
176 
177  set_task_factory(new_task_factory);
178 
179  set_prob_uniform( tag->getOption<core::Real>( "prob_uniform", prob_uniform() ) );
180  set_prob_withinrot( tag->getOption<core::Real>( "prob_withinrot", prob_withinrot() ) );
181  set_prob_random_pert_current( tag->getOption<core::Real>( "prob_random_pert_current", prob_random_pert_current() ) );
182  set_preserve_detailed_balance( tag->getOption<bool>( "preserve_detailed_balance", preserve_detailed_balance() ) );
183  set_require_mm_bend( tag->getOption<bool>( "require_mm_bend", require_mm_bend() ) );
184  set_record_statistics( tag->getOption<bool>( "record_statistics", record_statistics() ) );
185  set_statistics_filename( tag->getOption<std::string>( "statistics_filename", statistics_filename() ) );
186 
187  update_segments(pose);
188 }
189 
190 void
192  core::pose::Pose const & pose
193 )
194 {
195  if (!(backrub_mover_->get_input_pose() && backrub_mover_->get_input_pose()->fold_tree() == pose.fold_tree())) {
196  backrub_mover_->set_input_pose(new core::pose::Pose(pose));
197  }
198 
199  backrub_mover_->set_input_pose(new core::pose::Pose(pose));
200  backrub_mover_->clear_segments();
201  backrub_mover_->add_mainchain_segments();
202 
203  sidechain_mover_->init_task(pose);
204 
205  valid_segments_.clear();
206 
207  for (core::Size i = 1; i <= backrub_mover_->num_segments(); ++i) {
208  BackrubSegment const & segment(backrub_mover_->segment(i));
209  if (pose.residue(segment.start_atomid().rsd()).atom_name(segment.start_atomid().atomno()) == " CA " &&
210  pose.residue(segment.end_atomid().rsd()).atom_name(segment.end_atomid().atomno()) == " CA " &&
211  segment.size() == 7) {
212  core::Size middle_rsd = (segment.start_atomid().rsd() + segment.end_atomid().rsd())/2;
213  if (sidechain_mover_->residue_packed()[middle_rsd]) {
214  //TR << "Adding segment start:" << segment.start_atomid() << "end:"
215  // << segment.end_atomid() << "size: " << segment.size() << std::endl;
216  valid_segments_.push_back(i);
217  }
218  }
219  }
220 
221  if (record_statistics_) reset_statistics();
222 }
223 
224 void
226  core::pose::Pose & pose,
227  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover,
228  core::Size cycle //default=0; non-zero if trajectory is restarted
229 )
230 {
231  if (!(valid_segments_.size() && backrub_mover_->get_input_pose() && backrub_mover_->get_input_pose()->fold_tree() == pose.fold_tree())) {
232  update_segments(pose);
233  }
234 
235  // because the segments have already been set up, they shouldn't be set up again in the next call
236  backrub_mover_->initialize_simulation(pose, metropolis_hastings_mover,cycle);
237  sidechain_mover_->initialize_simulation(pose, metropolis_hastings_mover,cycle);
238 
239  if (record_statistics_) reset_statistics();
240 }
241 
242 void
244  core::pose::Pose & pose
245 )
246 {
247  if (!(valid_segments_.size() && backrub_mover_->get_input_pose() && backrub_mover_->get_input_pose()->fold_tree() == pose.fold_tree())) {
248  update_segments(pose);
249  }
250 
251  last_valid_segment_index_ =RG.random_range(1, valid_segments_.size());
252  BackrubSegment const & segment(backrub_mover_->segment(valid_segments_[last_valid_segment_index_]));
253  core::Size middle_rsd = (segment.start_atomid().rsd() + segment.end_atomid().rsd())/2;
254 
255  last_chi1_pre_ = numeric::conversions::radians(pose.chi(1, middle_rsd));
256 
257  sidechain_mover_->next_resnum(middle_rsd);
258  sidechain_mover_->apply(pose);
259 
260  last_chi1_post_ = numeric::conversions::radians(pose.chi(1, middle_rsd));
261 
262  backrub_mover_->set_next_segment_id(valid_segments_[last_valid_segment_index_]);
263  backrub_mover_->apply(pose);
264 
265  update_type();
266 }
267 
268 void
270  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover
271 )
272 {
273  if (record_statistics_) record_histograms(metropolis_hastings_mover.monte_carlo()->mc_accepted());
274 }
275 
276 void
278  core::pose::Pose & /*pose*/,
279  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover
280 )
281 {
282  if (record_statistics_) {
283 
284  std::ostringstream filename;
285  if (metropolis_hastings_mover.output_name() != "") {
286  filename << metropolis_hastings_mover.output_name() << "_";
287  }
288  filename << statistics_filename();
289 
290  utility::io::ozstream statistics_stream(filename.str());
291  output_statistics(statistics_stream);
292  statistics_stream.close();
293  }
294 }
295 
298 {
299  return backrub_mover_->pivot_residues();
300 }
301 
302 void
304  utility::vector1<core::Size> const & pivot_residues
305 )
306 {
307  backrub_mover_->set_pivot_residues(pivot_residues);
308 }
309 
312 {
313  return sidechain_mover_->task_factory();
314 }
315 
316 void
319 )
320 {
321  task_factory->push_back(new core::pack::task::operation::RestrictToRepacking);
322  sidechain_mover_->set_task_factory(task_factory);
323 }
324 
327 {
328  return sidechain_mover_->prob_uniform();
329 }
330 
331 void
333  core::Real prob_uniform
334 )
335 {
336  sidechain_mover_->set_prob_uniform(prob_uniform);
337 }
338 
341 {
342  return sidechain_mover_->prob_withinrot();
343 }
344 
345 void
347  core::Real prob_withinrot
348 )
349 {
350  sidechain_mover_->set_prob_withinrot(prob_withinrot);
351 }
352 
355 {
356  return sidechain_mover_->prob_random_pert_current();
357 }
358 
359 void
361  core::Real prob_pert
362 )
363 {
364  sidechain_mover_->set_prob_random_pert_current(prob_pert);
365 }
366 
367 bool
369 {
370  return backrub_mover_->preserve_detailed_balance() && sidechain_mover_->preserve_detailed_balance();
371 }
372 
373 void
375  bool preserve_detailed_balance
376 )
377 {
378  backrub_mover_->set_preserve_detailed_balance(preserve_detailed_balance);
379  sidechain_mover_->set_preserve_detailed_balance(preserve_detailed_balance);
380 }
381 
382 bool
384 {
385  return backrub_mover_->require_mm_bend();
386 }
387 
388 void
390  bool require_mm_bend
391 )
392 {
393  backrub_mover_->set_require_mm_bend(require_mm_bend);
394 }
395 
398  core::pose::Pose & //pose
399 )
400 {
402 }
403 
404 bool
406 {
407  return record_statistics_;
408 }
409 
410 void
412  bool record_statistics
413 )
414 {
415  bool const needs_reset(record_statistics_ != record_statistics);
416  record_statistics_ = record_statistics;
417  if (needs_reset) reset_statistics();
418 }
419 
420 std::string const &
422 {
423  return statistics_filename_;
424 }
425 
426 void
428  std::string const & statistics_filename
429 )
430 {
431  statistics_filename_ = statistics_filename;
432 }
433 
434 void
436 {
437  setup_histograms();
438 }
439 
440 void
442  std::ostream & out
443 )
444 {
445  for (core::Size i = 1; i <= proposal_hists_.size(); ++i) {
446  out << proposal_hists_[i] << accept_hists_[i];
447  }
448 }
449 
450 void
452 {
453  if (!(record_statistics_ && backrub_mover_->get_input_pose())) {
454  proposal_hists_.resize(0);
455  accept_hists_.resize(0);
456  return;
457  }
458 
459  core::pose::Pose const & pose(*backrub_mover_->get_input_pose());
460 
461  core::Real max_angle_disp(ceil(numeric::conversions::degrees(backrub_mover_->max_angle_disp_7())));
462  core::Size const num_bins(static_cast<core::Size>(2*max_angle_disp));
463  numeric::conversions::to_radians(max_angle_disp);
464 
465  proposal_hists_.resize(valid_segments_.size());
466  accept_hists_.resize(valid_segments_.size());
467 
468  for (core::Size i = 1; i <= valid_segments_.size(); ++i) {
469 
470  core::Size res_num(backrub_mover_->segment(valid_segments_[i]).start_atomid().rsd()+1);
471 
472  std::ostringstream res_label_stream;
473  res_label_stream << pose.residue(res_num).name3() << " "
474  << pose.pdb_info()->chain(res_num) << " "
475  << pose.pdb_info()->number(res_num);
476 
477  std::ostringstream proposal_label_stream;
478  proposal_label_stream << res_label_stream.str() << " Proposal";
479  proposal_hists_[i].label(proposal_label_stream.str());
480  proposal_hists_[i].reset_counts();
481  proposal_hists_[i].num_dimensions(3);
482  proposal_hists_[i].set_dimension(1, num_bins, -max_angle_disp, max_angle_disp, "backrub_disp");
483  proposal_hists_[i].set_dimension(2, 3, 0, numeric::constants::r::pi_2, "chi1_pre");
484  proposal_hists_[i].set_dimension(3, 3, 0, numeric::constants::r::pi_2, "chi1_post");
485 
486  std::ostringstream accept_label_stream;
487  accept_label_stream << res_label_stream.str() << " Accept";
488  accept_hists_[i].label(accept_label_stream.str());
489  accept_hists_[i].reset_counts();
490  accept_hists_[i].num_dimensions(3);
491  accept_hists_[i].set_dimension(1, num_bins, -max_angle_disp, max_angle_disp, "backrub_disp");
492  accept_hists_[i].set_dimension(2, 3, 0, numeric::constants::r::pi_2, "chi1_pre");
493  accept_hists_[i].set_dimension(3, 3, 0, numeric::constants::r::pi_2, "chi1_post");
494  }
495 }
496 
497 void
499  bool accepted
500 )
501 {
502  if (proposal_hists_.size() == 0) setup_histograms();
503 
504  //BackrubSegment const & segment(backrub_mover_->segment(backrub_mover_->last_segment_id()));
505  //core::Size middle_rsd = (segment.start_atomid().rsd() + segment.end_atomid().rsd())/2;
506  //TR << "Changed residue " << middle_rsd << " chi1 from "
507  // << numeric::conversions::degrees(last_chi1_pre_) << " to "
508  // << numeric::conversions::degrees(last_chi1_post_) << " backrub angle "
509  // << numeric::conversions::degrees(backrub_mover_->last_angle()) << " accepted "
510  // << accepted << std::endl;
511 
513  values[1] = backrub_mover_->last_angle();
514  values[2] = numeric::nonnegative_principal_angle_radians(last_chi1_pre_);
515  values[3] = numeric::nonnegative_principal_angle_radians(last_chi1_post_);
516 
517  proposal_hists_[last_valid_segment_index_].record(values);
518  if (accepted) accept_hists_[last_valid_segment_index_].record(values);
519 }
520 
521 void
523 {
524  std::stringstream mt;
525 
526  char bin_letters[] = {'p', 't', 'm'};
527 
528  core::Size chi1_pre_bin(static_cast<core::Size>(floor(numeric::nonnegative_principal_angle_radians(last_chi1_pre_)/numeric::constants::r::pi_2_over_3)));
529  if (chi1_pre_bin == 3) chi1_pre_bin = 2;
530  core::Size chi1_post_bin(static_cast<core::Size>(floor(numeric::nonnegative_principal_angle_radians(last_chi1_post_)/numeric::constants::r::pi_2_over_3)));
531  if (chi1_post_bin == 3) chi1_post_bin = 2;
532 
533  mt << "brsc_" << bin_letters[chi1_pre_bin] << bin_letters[chi1_post_bin] << "_"
534  << (sidechain_mover_->last_nchi() ? (sidechain_mover_->last_uniform() ? "unif" : (sidechain_mover_->last_withinrot() ? "withinrot" : "rot")) : "none");
535 
536  std::string const new_type(mt.str());
537  type(new_type);
538 }
539 
540 } //moves
541 } //protocols
542