Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MetricRecorder.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/canonical_sampling/MetricRecorder.cc
11 ///
12 /// @brief
13 /// @author
14 
15 
16 // Unit header or inline function header
19 
20 // Other project headers or inline function headers
21 // AUTO-REMOVED #include <core/io/raw_data/ScoreStruct.hh>
22 #include <core/pose/Pose.hh>
23 #include <core/scoring/Energies.hh>
25 #include <protocols/jd2/Job.hh>
26 #include <protocols/jd2/util.hh>
30 // AUTO-REMOVED #include <protocols/canonical_sampling/ThermodynamicMover.hh> // required for Windows build
32 #include <core/pose/selection.hh>
33 #include <utility/tag/Tag.hh>
34 
35 // External library headers
36 
37 // C++ headers
38 #include <algorithm>
39 // AUTO-REMOVED #include <iomanip>
40 #include <sstream>
41 
42 #include <core/id/TorsionID.hh>
43 #include <utility/vector0.hh>
44 #include <utility/vector1.hh>
45 
46 
47 // Operating system headers
48 
49 // Forward declarations
50 
51 
52 namespace protocols {
53 namespace canonical_sampling {
54 
58 }
59 
62  return new MetricRecorder;
63 }
64 
67  return "MetricRecorder";
68 }
69 
71  stride_(1),
72  cumulate_jobs_(false),
73  cumulate_replicas_(false),
74  prepend_output_name_(false),
75  step_count_(0),
76  last_flush_(0)
77 {}
78 
80 {}
81 
83  MetricRecorder const & other
84 ) :
85  protocols::canonical_sampling::ThermodynamicObserver(other),
86  file_name_(other.file_name_),
87  stride_(other.stride_),
88  cumulate_jobs_(other.cumulate_jobs_),
89  cumulate_replicas_(other.cumulate_replicas_),
90  prepend_output_name_(other.prepend_output_name_),
91  step_count_(other.step_count_),
92  torsion_ids_(other.torsion_ids_),
93  last_flush_(other.last_flush_)
94 {}
95 
98 {
99  // assignment not allowed
100  runtime_assert(false);
101  return * this;
102 }
103 
106 {
108 }
109 
112 {
113  return new MetricRecorder;
114 }
115 
118 {
119  return "MetricRecorder";
120 }
121 
122 void
124  utility::tag::TagPtr const tag,
125  protocols::moves::DataMap & /* data */,
126  protocols::filters::Filters_map const & /* filters */,
127  protocols::moves::Movers_map const & /* movers */,
128  core::pose::Pose const & pose
129 )
130 {
131  stride_ = tag->getOption< core::Size >( "stride", 100 );
132  cumulate_jobs_ = tag->getOption< bool >( "cumulate_jobs", false );
133  cumulate_replicas_ = tag->getOption< bool >( "cumulate_replicas", false );
134  prepend_output_name_ = tag->getOption< bool >( "prepend_output_name", false );
135  file_name_ = tag->getOption< std::string >( "filename", "metrics.txt" );
136 
137  utility::vector0< utility::tag::TagPtr > const subtags( tag->getTags() );
138 
139  for( utility::vector0< utility::tag::TagPtr >::const_iterator subtag_it = subtags.begin(); subtag_it != subtags.end(); ++subtag_it ) {
140 
141  TagPtr const subtag = *subtag_it;
142 
144 
145  if (subtag->getName() == "Torsion") {
146 
147  std::string rsd = subtag->getOption< std::string >( "rsd" );
148  std::string type = subtag->getOption< std::string >( "type" );
149  core::Size torsion = subtag->getOption< core::Size >( "torsion" );
150  std::string name = subtag->getOption< std::string >( "name", "" );
151  if (subtag->hasOption("name")) name = subtag->getOptions().find("name")->second;
152 
153  add_torsion(pose, rsd, type, torsion, name);
154 
155  } else {
156  utility_exit_with_message("Parsed unknown metric type in MetricRecorder");
157  }
158  }
159 }
160 
161 std::string const &
163 {
164  return file_name_;
165 }
166 
167 void
169  std::string const & file_name
170 )
171 {
173 }
174 
177 {
178  return stride_;
179 }
180 
181 void
183  core::Size stride
184 )
185 {
186  stride_ = stride;
187 }
188 
189 bool
191 {
192  return cumulate_jobs_;
193 }
194 
195 void
197  bool cumulate_jobs
198 )
199 {
201 }
202 
203 bool
205 {
206  return cumulate_replicas_;
207 }
208 
209 void
211  bool cumulate_replicas
212 )
213 {
215 }
216 
217 bool
219 {
220  return prepend_output_name_;
221 }
222 
223 void
225  bool prepend_output_name
226 )
227 {
229 }
230 
231 void
233  core::id::TorsionID const & torsion_id,
234  std::string name // = ""
235 )
236 {
237  runtime_assert(torsion_id.valid());
238 
239  if (name == "") {
240 
241  std::ostringstream name_stream;
242  name_stream << torsion_id;
243  name = name_stream.str();
244  }
245 
246  torsion_ids_.push_back(std::pair<core::id::TorsionID, std::string>(torsion_id, name));
247 }
248 
249 void
251  core::pose::Pose const & pose,
252  std::string const & rsd,
253  std::string type,
254  core::Size torsion,
255  std::string name // = ""
256 )
257 {
258  core::Size parsed_rsd = core::pose::parse_resnum(rsd, pose);
259 
260  std::transform(type.begin(), type.end(), type.begin(), toupper);
261  core::id::TorsionType parsed_type;
262  if (type == "BB") {
263  parsed_type = core::id::BB;
264  } else if (type == "CHI") {
265  parsed_type = core::id::CHI;
266  } else if (type == "JUMP") {
267  parsed_type = core::id::JUMP;
268  } else {
269  utility_exit_with_message("Unknown torsion type");
270  }
271 
272  core::id::TorsionID torsion_id(parsed_rsd, parsed_type, torsion);
273 
274  add_torsion(torsion_id, name);
275 }
276 
277 void
279  core::pose::Pose const & pose,
280  protocols::canonical_sampling::MetropolisHastingsMoverCAP metropolis_hastings_mover //= 0
281 )
282 {
283  step_count_ = 0;
284  recorder_stream_.close();
285  update_after_boltzmann(pose, metropolis_hastings_mover);
286 }
287 
288 void
290  core::pose::Pose const & pose,
291  protocols::canonical_sampling::MetropolisHastingsMoverCAP metropolis_hastings_mover //= 0
292 )
293 {
294  if (recorder_stream_.filename() == "") {
295  std::ostringstream file_name_stream;
296  if (prepend_output_name_ && !metropolis_hastings_mover) {
298  }
299  file_name_stream << file_name_;
300  recorder_stream_.open(file_name_stream.str());
301  }
302 
304  core::Size nstruct_index( job ? job->nstruct_index() : 1 );
305  std::string output_name( metropolis_hastings_mover ? metropolis_hastings_mover->output_name() : "" );
306 
308 
309  TemperingBaseCAP tempering = 0;
310  if (metropolis_hastings_mover) {
311  tempering = dynamic_cast< TemperingBase const * >( metropolis_hastings_mover->tempering()() );
312  }
313 
314  if (step_count_ == 0) {
315 
316  // output header if not cumulating, replica exchange inactive, or this is the first replica
317  if ((!cumulate_jobs_ || nstruct_index == 1) && (!cumulate_replicas_ || replica <= 1)) {
318 
319  recorder_stream_ << "Trial";
320  if (cumulate_jobs_ && output_name.length()) recorder_stream_ << '\t' << "Job";
321  if (cumulate_replicas_ && replica) recorder_stream_ << '\t' << "Replica";
322  if (tempering) recorder_stream_ << '\t' << "Temperature";
323  recorder_stream_ << '\t' << "Score";
324 
325  for (core::Size i = 1; i <= torsion_ids_.size(); ++i) {
326  recorder_stream_ << '\t' << torsion_ids_[i].second;
327  }
328 
329  recorder_stream_ << std::endl;
330  recorder_stream_.flush();
331  }
332  last_flush_ = time(NULL);
333  }
334 
335  if (step_count_ % stride_ == 0) {
336 
338  if (cumulate_jobs_ && output_name.length()) recorder_stream_ << '\t' << output_name;
339  if (cumulate_replicas_ && replica) recorder_stream_ << '\t' << replica;
340  if (tempering) recorder_stream_ << '\t' << metropolis_hastings_mover->monte_carlo()->temperature();
341  recorder_stream_ << '\t' << pose.energies().total_energy();
342 
343  for (core::Size i = 1; i <= torsion_ids_.size(); ++i) {
344  recorder_stream_ << '\t' << pose.torsion(torsion_ids_[i].first);
345  }
346 
347  recorder_stream_ << std::endl;
348 
349  time_t now = time(NULL);
350  if (now-last_flush_ > 10) {
351  recorder_stream_.flush();
352  last_flush_ = now;
353  }
354  }
355 
356  ++step_count_;
357 }
358 
359 void
361  core::pose::Pose & pose
362 )
363 {
364  update_after_boltzmann(pose, 0);
365 }
366 
367 void
370  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover,
371  core::Size //default=0; non-zero if trajectory is restarted
372 )
373 {
374  std::string original_file_name(file_name());
375 
376  file_name(metropolis_hastings_mover.output_file_name(file_name(), cumulate_jobs_, cumulate_replicas_));
377 
378  reset(
379  metropolis_hastings_mover.monte_carlo()->last_accepted_pose(),
380  &metropolis_hastings_mover
381  );
382 
383  file_name(original_file_name);
384 }
385 
386 void
388  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover
389 )
390 {
392  metropolis_hastings_mover.monte_carlo()->last_accepted_pose(),
393  &metropolis_hastings_mover
394  );
395 }
396 
397 void
401 )
402 {
403  recorder_stream_.close();
404 }
405 
406 
407 } // namespace canonical_sampling
408 } // namespace protocols