Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SingleFragmentMover.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/nonlocal/SingleFragmentMover.cc
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 // Unit header
15 
16 // C/C++ headers
17 #include <iostream>
18 #include <string>
19 
20 // Utility headers
21 #include <basic/Tracer.hh>
22 #include <numeric/random/random.hh>
23 #include <utility/exit.hh>
24 #include <utility/vector1.hh>
25 #include <utility/tag/Tag.hh>
26 
27 // Project headers
28 #include <core/types.hh>
30 #include <core/fragment/FragSet.hh>
31 #include <core/fragment/Frame.hh>
35 #include <core/pose/Pose.hh>
37 #include <protocols/moves/Mover.hh>
40 
41 // Package headers
45 
47 
48 namespace protocols {
49 namespace nonlocal {
50 
51 static basic::Tracer TR("protocols.nonlocal.SingleFragmentMover");
52 
53 
54 SingleFragmentMover::SingleFragmentMover() : Parent("SingleFragmentMover") {}
55 
57  const core::kinematics::MoveMapOP& movable)
58  : Parent("SingleFragmentMover") {
59  initialize(fragments, movable, PolicyFactory::get_policy("uniform", fragments));
60 }
61 
63  const core::kinematics::MoveMapOP& movable,
64  const PolicyOP& policy)
65  : Parent("SingleFragmentMover") {
66  initialize(fragments, movable, policy);
67 }
68 
70  const core::kinematics::MoveMapOP& movable,
71  const PolicyOP& policy) {
72  assert(fragments);
73  assert(movable);
74  assert(policy);
75 
76  // Initialize member variables
77  fragments_ = fragments;
78  movable_ = movable;
79  policy_ = policy;
80 
81  // Create a position-indexable lookup for core::fragment::Frame's
83 }
84 
86  using core::Size;
90 
91  // ensure that preconditions on the input have been met
92  assert(pose.total_residue() > 0);
93  assert(pose.fold_tree().check_fold_tree());
94  assert(valid());
95 
96  // determine whether the pose is in fullatom mode. if so, warn the user and
97  // convert it to centroid mode automatically.
98  // bool was_fullatom = // Unused variable causes warning.
99  to_centroid(&pose);
100 
101  // reuse <chunks_> when possible
102  const FoldTree& current_tree = pose.fold_tree();
103  if (!previous_tree_ || *previous_tree_ != current_tree) {
104  chunks_.clear();
105  probs_.clear();
106  initialize_chunks(current_tree);
107  previous_tree_ = new core::kinematics::FoldTree(current_tree);
108  }
109 
110  // randomly select the insertion position
111  const Chunk* chunk = random_chunk();
112  if (!chunk) {
113  TR.Warning << "No move possible-- 0 chunks" << std::endl;
114  return;
115  }
116 
117  Size insertion_pos = chunk->choose();
118 
119  while (!movable_->get_bb(insertion_pos))
120  insertion_pos = chunk->choose();
121 
122  // delegate responsibility for choosing the fragment to the policy
123  const Frame& frame = library_[insertion_pos];
124  Size fragment_pos = policy_->choose(frame, pose);
125  frame.apply(*movable_, fragment_pos, pose);
126  TR.Debug << "Inserted fragment at position " << insertion_pos << std::endl;
127 }
128 
130  return "SingleFragmentMover";
131 }
132 
134  return new SingleFragmentMover(*this);
135 }
136 
138  return new SingleFragmentMover();
139 }
140 
145  const core::pose::Pose& pose) {
150  using std::string;
151 
152  // required flags
153  // fragments, movemap
154  string fragments_file = tag->getOption<string>("fragments");
155  FragSetOP fragments = FragmentIO().read_data(fragments_file);
156 
157  // initially, all backbone torsions are movable
158  MoveMapOP movable = new MoveMap();
159  protocols::rosetta_scripts::parse_movemap(tag, pose, movable, data);
160 
161  // optional flags
162  // policy -- default => uniform
163  string policy_type = tag->getOption<string>("policy", "uniform");
164  PolicyOP policy = PolicyFactory::get_policy(policy_type, fragments);
165 
166  initialize(fragments, movable, policy);
167 }
168 
169 void SingleFragmentMover::parse_def( utility::lua::LuaObject const & def,
170  utility::lua::LuaObject const & ,
171  utility::lua::LuaObject const & ,
177  using std::string;
178 
179  // required flags
180  // fragments, movemap
181  string fragments_file = def["fragments"].to<string>();
182  FragSetOP fragments = FragmentIO().read_data(fragments_file);
183 
184  // initially, all backbone torsions are movable
185  MoveMapOP movable = new MoveMap();
186  movable->set_bb( true );
187  movable->set_chi( true );
188  movable->set_jump( true );
189  if( def["movemap"] )
190  protocols::elscripts::parse_movemapdef(def["movemap"], movable);
191 
192  // optional flags
193  // policy -- default => uniform
194  string policy_type = def["policy"] ? def["policy"].to<string>() : "uniform";
195  PolicyOP policy = PolicyFactory::get_policy(policy_type, fragments);
196 
197  initialize(fragments, movable, policy);
198 }
199 
201  return fragments_ && movable_ && policy_;
202 }
203 
206  for (i = fragments_->begin(); i != fragments_->end(); ++i) {
207  Size position = (*i)->start();
208  library_[position] = **i;
209  }
210 }
211 
213  using core::Size;
214  using core::Real;
215 
216  // Assumption: constant-length fragments at each insertion position
217  Size fragment_len = fragments_->max_frag_length();
218 
219  // Last position to examine
220  Size last_pos = fragments_->max_pos() - fragment_len + 1;
221 
222  Size p = fragments_->min_pos();
223  do {
224  Size q = p + 1;
225  while (!tree.is_cutpoint(q++)) {}
226 
227  // During fold tree construction, it may happen that the distance between
228  // the <p> and the next cutpoint is less than fragment length. In this case,
229  // the Region::start() exceeds Region::stop(). It's impossible to perform
230  // fragment insertions on this region given the current fragment library.
231  RegionOP region = new Region(p, q - fragment_len);
232  if (region->increasing()) {
233  // Ensure that the chunk is valid before adding it to the list. Mainly, this
234  // means that there must be at least 1 movable residue.
235  Chunk chunk(region, movable_);
236 
237  if (chunk.is_movable()) {
238  TR.Debug << "Added chunk: " << region->start() << "-" << region->stop() << std::endl;
239  chunks_.push_back(chunk);
240  } else {
241  TR.Debug << "Skipped chunk: " << region->start() << "-" << region->stop() << ": no movable positions" << std::endl;
242  }
243  }
244 
245  p = q - 1;
246  } while (p < last_pos);
247 
248  // Assign probabilities of selection to each Chunk according to its length
249  Real N = fragments_->max_pos();
250  Real sum = 0;
251  for (Size i = 1; i <= chunks_.size(); ++i) {
252  Real prob = chunks_[i].length() / N;
253  probs_.push_back(prob);
254  sum += prob;
255  }
256 
257  // Normalize probabilities
258  for (Size i = 1; i <= probs_.size(); ++i) {
259  probs_[i] /= sum;
260  TR << "P(c_" << i << ")=" << probs_[i] << std::endl;
261  }
262 }
263 
265  using core::Real;
266  using core::Size;
267  using utility::vector1;
268 
269  assert(chunks_.size() > 0);
270  vector1<Real> fitnesses(probs_);
271 
272  // convert to a CDF
273  Size n = fitnesses.size();
274  for (Size i = 2; i <= n; ++i)
275  fitnesses[i] += fitnesses[i-1];
276 
277  // random number from 0 to f[n] (inclusive)
278  Real x = numeric::random::uniform();
279 
280  // this could be done more efficiently with binary search
281  for (Size i = 2; i <= n; ++i) {
282  if (fitnesses[i-1] < x && x <= fitnesses[i])
283  return &chunks_[i];
284  }
285  return &chunks_[1];
286 }
287 
289  assert(pose);
290  if (pose->is_fullatom()) {
291  TR.Warning << "Input pose is full atom (centroid required)" << std::endl;
292  TR.Warning << "Performing implicit conversion..." << std::endl;
293  core::util::switch_to_residue_type_set(*pose, "centroid");
294  return true;
295  }
296  return false;
297 }
298 
299 } // namespace nonlocal
300 } // namespace protocols