Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.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/util.cc
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 // Unit headers
15 
16 // C/C++ headers
17 #include <iostream>
18 #include <string>
19 
20 #ifdef WIN32
21 #include <iterator>
22 #endif
23 
24 // Utility headers
25 #include <basic/Tracer.hh>
26 #include <basic/options/option.hh>
27 #include <basic/options/keys/OptionKeys.hh>
28 #include <basic/options/keys/abinitio.OptionKeys.gen.hh>
29 #include <basic/options/keys/rigid.OptionKeys.gen.hh>
30 #include <numeric/random/random.hh>
31 #include <numeric/xyzVector.hh>
32 #include <utility/exit.hh>
33 #include <utility/vector1.hh>
34 
35 // Project headers
36 #include <core/types.hh>
39 #include <core/io/pdb/pose_io.hh>
40 // AUTO-REMOVED #include <core/kinematics/FoldTree.hh>
42 #include <core/id/NamedAtomID.hh>
43 #include <core/pose/Pose.hh>
46 // AUTO-REMOVED #include <core/sequence/util.hh>
48 #include <core/scoring/Energies.hh>
51 #include <protocols/jd2/Job.hh>
53 #include <protocols/loops/Loop.hh>
54 #include <protocols/loops/Loops.hh>
55 
56 namespace protocols {
57 namespace nonlocal {
58 
59 static basic::Tracer TR("protocols.nonlocal.util");
60 
62  using namespace basic::options;
63  using namespace basic::options::OptionKeys;
64  chunks_by_CA_CA_distance(pose, chunks, option[OptionKeys::rigid::max_ca_ca_dist]());
65 }
66 
67 void chunks_by_CA_CA_distance(const core::pose::Pose& pose, protocols::loops::LoopsOP chunks, double threshold) {
68  using core::Size;
70  using numeric::xyzVector;
72  using utility::vector1;
73 
74  assert(chunks);
75  assert(threshold > 0);
76 
77  vector1<Size> violated_residues;
78  violated_residues.push_back(1);
79  for (Size i = 2; i <= pose.total_residue(); ++i) {
80  const xyzVector<double>& prev_xyz = pose.xyz(NamedAtomID("CA", i - 1));
81  const xyzVector<double>& curr_xyz = pose.xyz(NamedAtomID("CA", i));
82 
83  double distance = prev_xyz.distance(curr_xyz);
84  if (distance > threshold) {
85  // Residues j and j - 1 are separated by more than max_ca_ca_dist Angstroms
86  violated_residues.push_back(i);
87  }
88  }
89  violated_residues.push_back(pose.total_residue() + 1);
90 
91  // violated_residues = [ 1, ..., n ]
92  for (Size i = 2; i <= violated_residues.size(); ++i) {
93  const Size prev_start = violated_residues[i - 1];
94  const Size curr_start = violated_residues[i];
95  const Size prev_stop = curr_start - 1;
96 
97  // Add the chunk
98  Loop chunk(prev_start, prev_stop);
99  chunks->add_loop(chunk);
100  TR.Debug << "Added chunk " << chunk.start() << " " << chunk.stop() << std::endl;
101  }
102 }
103 
105  core::Size num_residues,
106  const protocols::loops::LoopsOP aligned_regions,
107  const protocols::loops::LoopsOP unaligned_regions) {
109 
110  Loops combined;
111  for (Loops::const_iterator i = aligned_regions->begin(); i != aligned_regions->end(); ++i)
112  combined.push_back(*i);
113  for (Loops::const_iterator i = unaligned_regions->begin(); i != unaligned_regions->end(); ++i)
114  combined.push_back(*i);
115 
116  // trim back the final loop to nres() - min_chunk_sz
117  combined[combined.size()].set_stop(num_residues - min_chunk_sz + 1);
118  return combined;
119 }
120 
121 void limit_chunk_size(core::Size min_chunk_sz,
122  core::Size max_chunk_sz,
123  protocols::loops::LoopsOP & regions) {
124  using core::Size;
127  using namespace basic::options;
128  using namespace basic::options::OptionKeys;
129 
130  assert(regions);
131  assert(min_chunk_sz <= max_chunk_sz);
132 
134  for (Loops::const_iterator i = regions->begin(); i != regions->end(); ++i) {
135  utility::vector1<Loop> pieces;
136  decompose(min_chunk_sz, max_chunk_sz, *i, &pieces);
137 
138  for (utility::vector1<Loop>::const_iterator j = pieces.begin(); j != pieces.end(); ++j)
139  output->push_back(*j);
140  }
141  regions = output;
142 }
143 
144 void decompose(core::Size min_chunk_sz,
145  core::Size max_chunk_sz,
146  const protocols::loops::Loop& loop,
148  using core::Size;
150  using utility::vector1;
151  assert(pieces);
152 
153  // Base case
154  if (loop.length() <= max_chunk_sz) {
155  pieces->push_back(loop);
156  return;
157  }
158 
159  Size midpoint = loop.start() + loop.length() / 2;
160  Loop candidate_left(loop.start(), midpoint - 1);
161  Loop candidate_right(midpoint, loop.stop());
162 
163  // Impossible to partition <loop> and satisfy min_chunk_sz requirement
164  if (candidate_left.length() < min_chunk_sz ||
165  candidate_right.length() < min_chunk_sz) {
166  pieces->push_back(loop);
167  return;
168  }
169 
170  // TODO(cmiles) consider including additional information (e.g. secondary
171  // structure, structural conservation) to inform pivot selection
172  Size pivot = numeric::random::random_range(
173  loop.start() + min_chunk_sz - 1,
174  loop.stop() - min_chunk_sz + 1);
175 
176  // Recursively decompose the left- and right-hand sides of the pivot
177  Loop left(loop.start(), pivot - 1);
178  vector1<Loop> pieces_left;
179  decompose(min_chunk_sz, max_chunk_sz, left, &pieces_left);
180 
181  Loop right(pivot, loop.stop());
182  vector1<Loop> pieces_right;
183  decompose(min_chunk_sz, max_chunk_sz, right, &pieces_right);
184 
185  // Update the output parameter
186  std::copy(pieces_left.begin(), pieces_left.end(), std::back_inserter(*pieces));
187  std::copy(pieces_right.begin(), pieces_right.end(), std::back_inserter(*pieces));
188 }
189 
191  const core::Size unaligned_region_min_sz,
192  protocols::loops::LoopsOP & aligned_regions,
193  protocols::loops::LoopsOP & unaligned_regions) {
194  using namespace basic::options;
195  using namespace basic::options::OptionKeys;
196  using core::Size;
197 
198  assert(aligned_regions);
199  assert(unaligned_regions);
200 
201  Size pose_space_num_residues = 0;
202  for (Size ii = 1; ii <= alignment.length(); ++ii)
203  pose_space_num_residues = std::max(pose_space_num_residues, alignment.sequence(1)->resnum(ii));
204 
206  pose_space_num_residues, unaligned_region_min_sz, alignment, unaligned_regions);
207  *aligned_regions = unaligned_regions->invert(pose_space_num_residues);
208 }
209 
211  const std::string& filename) {
212  using namespace basic::options;
213  using namespace basic::options::OptionKeys;
214  if (option[OptionKeys::abinitio::debug]())
215  core::io::pdb::dump_pdb(pose, filename);
216 }
217 
222 
223  JobDistributor* jd2 = JobDistributor::get_instance();
224  InnerJobCOP inner = jd2->current_job()->inner_job();
225  return (protocols::comparative_modeling::ThreadingJob const * const) inner();
226 }
227 
229  core::Size rsd_idx,
230  core::scoring::ScoreType scoretype,
231  core::pose::Pose const & pose
232 ) {
233  using namespace core::scoring;
234  assert( rsd_idx <= pose.total_residue() );
235  EnergyMap const & rsd_energies(pose.energies().residue_total_energies(rsd_idx)); // unweighted scores
236  return rsd_energies[ scoretype ];
237 }
238 
239 } // namespace nonlocal
240 } // namespace protocols