Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DOFHistogramRecorder.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/simple_moves/DOFHistogramRecorder.cc
11 ///
12 /// @brief
13 /// @author
14 
15 
16 // Unit header or inline function header
18 
19 // Other project headers or inline function headers
21 #include <core/id/DOF_ID_Range.hh>
24 #include <core/pose/Pose.hh>
25 
26 // External library headers
27 
28 // C++ headers
29 #include <iomanip>
30 #include <sstream>
31 #include <set>
32 
33 //Auto Headers
34 #include <utility/vector0.hh>
35 #include <utility/vector1.hh>
36 #include <numeric/MultiDimensionalHistogram.hh>
37 
38 
39 // Operating system headers
40 
41 // Forward declarations
42 
43 
44 namespace protocols {
45 namespace simple_moves {
46 
48  num_bins_(10)
49 {}
50 
52 {}
53 
55 {
56  // copy constructor not allowed
57  runtime_assert(false);
58 }
59 
62 {
63  // assignment not allowed
64  runtime_assert(false);
65  return * this;
66 }
67 
68 void
70  core::pose::Pose const & pose,
72 )
73 {
75  residue_dof_ranges.resize(pose.total_residue());
76 
77  for (core::Size i = 1; i <= dof_ranges.size(); ++i) {
78 
79  core::id::AtomID parent_id( pose.atom_tree().atom(dof_ranges[i].dof_id().atom_id()).parent()->id() );
80  residue_dof_ranges[parent_id.rsd()].insert(dof_ranges[i]);
81  }
82 
83  for (core::Size i = 1; i <= residue_dof_ranges.size(); ++i) {
84 
85  if (residue_dof_ranges[i].size()) {
86 
87  dofs_.resize(dofs_.size()+1);
88  histograms_.resize(histograms_.size()+1);
89  dof_values_.resize(dof_values_.size()+1);
90 
92  numeric::MultiDimensionalHistogram & histogram(histograms_[histograms_.size()]);
94 
95  std::ostringstream res_label;
96  res_label << pose.residue_type(i).name3() << ' ' << i;
97  histogram.label(res_label.str());
98 
99  utility::vector1<core::id::DOF_ID_Range> r_dof_ranges(residue_dof_ranges[i].begin(), residue_dof_ranges[i].end());
100 
101  dofs.resize(r_dof_ranges.size());
102  histogram.num_dimensions(r_dof_ranges.size());
103  dof_values.resize(r_dof_ranges.size());
104 
105  for (core::Size j = 1; j <= r_dof_ranges.size(); ++j) {
106 
107  dofs[j] = r_dof_ranges[j].dof_id();
108 
109  std::ostringstream label;
110  label << r_dof_ranges[j].dof_id();
111  histogram.set_dimension(j, num_bins_, r_dof_ranges[j].min(), r_dof_ranges[j].max(), label.str());
112  }
113  }
114  }
115 }
116 
117 void
119  core::pose::Pose const & pose
120 )
121 {
122  for (core::Size i = 1; i <= dofs_.size(); ++i) {
123  for (core::Size j = 1; j <= dofs_[i].size(); ++j) {
124  dof_values_[i][j] = pose.dof(dofs_[i][j]);
125  }
126  histograms_[i].record(dof_values_[i]);
127  }
128 }
129 
130 void
132  std::ostream & os
133 ) const
134 {
135  for (core::Size i = 1; i <= dofs_.size(); ++i) {
136 
137  numeric::MultiDimensionalHistogram const & histogram(histograms_[i]);
138 
139  utility::vector1<utility::vector1<core::Real> > expected_frequencies_all(dofs_[i].size());
140  utility::vector1<utility::vector1<core::Real> > expected_frequencies_subset(1);
141 
142  for (core::Size j = 1; j <= dofs_[i].size(); ++j) {
143  expected_frequencies_all[j] = uniform_dof_distribution(
144  dofs_[i][j].type(), histogram.num_bins()[j], histogram.start()[j], histogram.end()[j]
145  );
146 
147  expected_frequencies_subset[1] = expected_frequencies_all[j];
148  utility::vector1<core::Size> const dims(1, j);
149 
150  core::Real mse = histogram.collapse(dims).mean_squared_error(expected_frequencies_subset);
151 
152  os << histogram.dim_labels()[j] << ": " << mse << std::endl;
153  }
154  }
155 }
156 
157 std::ostream & operator << (
158  std::ostream & os,
159  DOFHistogramRecorder const & dof_recorder)
160 {
161  for (core::Size i = 1; i <= dof_recorder.histograms().size(); ++i) {
162 
163  os << dof_recorder.histograms()[i];
164 
165  // print out collapsed histograms for debugging purposes
166 // utility::vector1<core::Size> dim_to_keep(1);
167 // for (core::Size j = 1; j <= dof_recorder.histograms()[i].num_dimensions(); ++j) {
168 //
169 // dim_to_keep[1] = j;
170 // numeric::MultiDimensionalHistogram mdhist(dof_recorder.histograms()[i].collapse(dim_to_keep));
171 // os << mdhist;
172 //
173 // core::id::DOF_Type dof_type(dof_recorder.dofs()[i][j].type());
174 // utility::vector1<core::Real> expected(
175 // uniform_dof_distribution(dof_type, mdhist.num_bins()[1], mdhist.start()[1], mdhist.end()[1])
176 // );
177 //
178 // for (core::Size i = 1; i < expected.size(); ++i) os << expected[i] << " ";
179 // os << std::endl;
180 // }
181  }
182 
183  return os;
184 }
185 
189  core::Size num_bins,
190  core::Real min,
191  core::Real max
192 )
193 {
194  utility::vector1<core::Real> frequencies;
195 
196  if (dof_type == core::id::PHI) {
197 
198  frequencies.resize(num_bins, 1.0/num_bins);
199 
200  } else if (dof_type == core::id::THETA) {
201 
202  frequencies.resize(num_bins);
203  core::Real frequencies_total(0);
204 
205  core::Real const delta((max-min)/num_bins);
206  for (core::Size i = 1; i <= num_bins; ++i) {
207 
208  core::Real const interval_start(min+(i-1)*delta);
209  core::Real const interval_end(min+i*delta);
210 
211  // integral of sin function
212  frequencies[i] = cos(interval_start) - cos(interval_end);
213  frequencies_total += frequencies[i];
214  }
215 
216  for (core::Size i = 1; i <= num_bins; ++i) {
217  frequencies[i] /= frequencies_total;
218  }
219 
220  } else {
221  // nothing else supported yet
222  runtime_assert(false);
223  }
224 
225  return frequencies;
226 }
227 
228 
229 } // namespace moves
230 } // namespace protocols