Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SilentTrajectoryRecorder.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/SilentTrajectoryRecorder.cc
11 ///
12 /// @brief
13 /// @author Oliver Lange
14 
15 
16 // Unit header or inline function header
19 
20 // Other project headers or inline function headers
23 #include <core/pose/Pose.hh>
24 #include <core/scoring/Energies.hh>
27 #include <protocols/canonical_sampling/ThermodynamicMover.hh> // required for Windows build
29 #include <utility/tag/Tag.hh>
30 #include <protocols/jd2/util.hh>
31 #include <utility/string_util.hh>
32 
34 
35 // just for tracer output
36 #include <protocols/jd2/Job.hh>
37 #include <protocols/jd2/util.hh>
38 
39 // External library headers
40 #include <basic/options/option_macros.hh>
41 #include <basic/Tracer.hh>
42 #include <core/types.hh>
43 
44 // C++ headers
45 #include <iomanip>
46 
47 // Operating system headers
48 
49 // Forward declarations
50 OPT_1GRP_KEY( Integer, trajectory, score_stride )
51 
52 static basic::Tracer tr( "protocols.canonical_sampling.SilentTrajectoryRecorder" );
53 
54 bool protocols::canonical_sampling::SilentTrajectoryRecorder::options_registered_( false );
55 
56 void protocols::canonical_sampling::SilentTrajectoryRecorder::register_options() {
57  using namespace basic::options;
58  using namespace OptionKeys;
59  if ( options_registered_ ) return;
60  options_registered_ = true;
62  NEW_OPT( trajectory::score_stride, "write x-times score-only before a decoy is written", 1 );
63 }
64 
65 
66 
67 namespace protocols {
68 namespace canonical_sampling {
69 using namespace core;
70 
72 SilentTrajectoryRecorderCreator::keyname() const {
73  return SilentTrajectoryRecorderCreator::mover_name();
74 }
75 
77 SilentTrajectoryRecorderCreator::create_mover() const {
78  return new SilentTrajectoryRecorder;
79 }
80 
82 SilentTrajectoryRecorderCreator::mover_name() {
83  return "SilentTrajectoryRecorder";
84 }
85 
86 SilentTrajectoryRecorder::SilentTrajectoryRecorder() {
87  using namespace basic::options;
88  using namespace OptionKeys;
89  if ( options_registered_ ) {
90  score_stride_ = option[ OptionKeys::trajectory::score_stride ]();
91  }
92 }
93 
94 SilentTrajectoryRecorder::SilentTrajectoryRecorder(
95  SilentTrajectoryRecorder const & other
96 ) :
97  TrajectoryRecorder(other),
98  score_stride_(other.score_stride_)
99 {}
100 
103 {
105 }
106 
109 {
110  return new SilentTrajectoryRecorder;
111 }
112 
115 {
116  return "SilentTrajectoryRecorder";
117 }
118 
119 void
121  utility::tag::TagPtr const tag,
122  protocols::moves::DataMap& data /* data */,
123  protocols::filters::Filters_map const& filters /* filters */,
124  protocols::moves::Movers_map const& movers /* movers */,
125  core::pose::Pose const& pose /* pose */
126 ) {
127  Parent::parse_my_tag( tag, data, filters, movers, pose );
128  score_stride_ = tag->getOption< core::Size >( "score_stride", 100 );
129 }
130 
131 void
133  core::pose::Pose const & pose,
134  protocols::canonical_sampling::MetropolisHastingsMoverCAP metropolis_hastings_mover //= 0
135 ) {
136  runtime_assert( jd2::jd2_used() );
137  std::string filename( metropolis_hastings_mover ? metropolis_hastings_mover->output_file_name(file_name(), cumulate_jobs(), cumulate_replicas()) : file_name() );
138  core::Size mc = model_count();
139  tr.Debug << "write model " << filename << " count: " << mc << std::endl;
140  jd2::output_intermediate_pose( pose, filename, mc, ( mc % score_stride_ ) != 0 && mc > 1 ); //write always first a structure
141 }
142 
143 bool
145  core::pose::Pose & pose,
147  core::Size& cycle,
148  core::Size& temp_level,
149  core::Real& temperature
150 ) {
151  core::scoring::constraints::ConstraintCOPs csts = pose.constraint_set()->get_all_constraints(); // copy cst info before restart
152 
153  std::string filename( metropolis_hastings_mover.output_file_name(file_name(), cumulate_jobs(), cumulate_replicas()) );
155  utility::file::FileName physical_filename( jd2_filename );
156  physical_filename.base( jd2_filename.base()+"_"+filename );
157 
158  //check existence of file
159  temp_level = 1;
160  temperature = -1.0;
161  cycle = 0;
162  //co
163  //check for correct tags in file
164  tr.Info << "restarting from trajector file " << physical_filename << ". Reading tags now ..." << std::endl;
165  io::silent::SilentFileData sfd( physical_filename );
166  std::ostringstream replica_id_str;
167  replica_id_str << std::setw(3) << std::setfill('0') << jd2::current_replica();
168  std::string tag = jd2::current_output_name()+"_"+replica_id_str.str();
169  tr.Info << "tag to match: " << tag << std::endl;
170  utility::vector1< std::string > matched_tags_in_file;
171  bool found = sfd.matched_tags( tag, "last", matched_tags_in_file );
172  tr.Info << "if matched_tags?: " << found << std::endl;
173  if ( found ) {
174  sfd.read_file( sfd.filename(), matched_tags_in_file );
175  runtime_assert( sfd.size() == 1 );
176  sfd.begin()->fill_pose( pose );
177 
178  pose.data().clear(); // otherwise the order of score options will change after restart
179  pose.add_constraints( csts ); // recover cst from the copy
180 
181  std::string decoy_tag=matched_tags_in_file.front();
182  tr.Info << "decoy_tag matched: " << decoy_tag << std::endl;
183  Size ind=decoy_tag.find_last_of( '_' );
184  cycle = utility::string2int(decoy_tag.substr( ind+1 ) )*stride();
185  tr.Info << "cycle number got from the decoy_tag: " << cycle << std::endl;
186  if ( sfd.begin()->has_energy( "temp_level" ) ) {
187  temp_level = (Size) sfd.begin()->get_energy( "temp_level" );
188  }
189  if ( sfd.begin()->has_energy( "temperature" )) {
190  temperature = sfd.begin()->get_energy( "temperature" );
191  }
192  tr.Info << "temp_level: " << temp_level << std::endl;
193  sfd.begin()->print_scores( tr.Info );
194  }
195  return found;
196 }
197 
198 void
200  core::pose::Pose & pose,
201  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover,
202  core::Size cycle //default=0; non-zero if trajectory is restarted
203 ) {
204  Parent::initialize_simulation(pose, metropolis_hastings_mover,cycle);
205  tr.Info << std::setprecision( 3 );
206  tr.Debug << std::setprecision( 3 );
207 }
208 
209 void
211  protocols::canonical_sampling::MetropolisHastingsMover const & metropolis_hastings_mover
212 )
213 { ///this is only for tracer output !
214  protocols::moves::MonteCarlo const& mc( *(metropolis_hastings_mover.monte_carlo()) );
215  Pose const& pose( mc.last_accepted_pose() );
216  if (step_count() % std::max(stride(),(core::Size)500) == 0) {
218  tr.Info << step_count() << " E=" << pose.energies().total_energy();
219  //output what is in job-object (e.g. temperature )
220  for ( jd2::Job::StringRealPairs::const_iterator it( job->output_string_real_pairs_begin()), end(job->output_string_real_pairs_end()); it != end; ++it ) {
221  tr.Info << " " << it->first << "=" << it->second;
222  }
223  mc.show_counters();
224  tr.Info << std::endl;
225  }
226 
227  Parent::observe_after_metropolis(metropolis_hastings_mover);
228 }
229 
230 
231 } // namespace canonical_sampling
232 } // namespace protocols