Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RRReporterHuman.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 src/protocols/rotamer_recovery/RRReporterHuman.cc
11 /// @author Matthew O'Meara (mattjomeara@gmail.com)
12 /// Adapted from:
13 /// James Thompson's code.
14 
15 /// and apps::pilot::doug::rotamer_prediction_benchmark()
16 
17 
18 // Unit Headers
21 
22 
23 // Project Headers
24 #include <basic/Tracer.hh>
25 #include <basic/datacache/CacheableString.fwd.hh>
29 #include <core/pose/Pose.hh>
30 #include <core/pose/util.hh>
31 #include <core/types.hh>
33 
34 // Utility Headers
35 #include <utility/vector1.hh>
36 
37 // Numeric Headers
38 #include <numeric/statistics.functions.hh>
39 
40 // ObjecxxFCL Headers
41 #include <ObjexxFCL/format.hh>
42 #include <ObjexxFCL/string.functions.hh>
43 
44 //C++ Headers
45 #include <ostream>
46 #include <string>
47 #include <map>
48 
49 using std::endl;
50 using std::ostream;
51 using std::string;
52 using std::map;
53 using basic::Tracer;
54 using basic::datacache::CacheableString;
55 using core::Size;
56 using core::Real;
60 using core::pose::Pose;
63 using utility::vector1;
64 using numeric::statistics::mean;
65 using numeric::statistics::std_dev_with_provided_mean;
66 
67 using ObjexxFCL::string_of;
68 using ObjexxFCL::fmt::A;
69 using ObjexxFCL::fmt::F;
70 
71 namespace protocols {
72 namespace rotamer_recovery {
73 
74 static Tracer TR("protocol.rotamer_recovery.RRReporterHuman");
75 
76 
77 
79  initialized_( false ),
80  native_pose_(),
81  nat_bb_bins_(),
82  nat_rots_(),
83  nat_chis_(),
84  res_scores_(),
85  res_recovered_()
86 {}
87 
89  Pose const & native_pose
90 ) :
91  initialized_( false ),
92  native_pose_(),
93  nat_bb_bins_(),
94  nat_rots_(),
95  nat_chis_(),
96  res_scores_(),
97  res_recovered_()
98 {
99  set_native( native_pose );
100 }
101 
103 {}
104 
106  PerNativeRRReporterHuman const & src ) :
107  ReferenceCount( src ),
108  initialized_( src.initialized_ ),
109  native_pose_( src.native_pose_ ),
110  nat_bb_bins_( src.nat_bb_bins_ ),
111  nat_rots_( src.nat_rots_ ),
112  nat_chis_( src.nat_chis_ ),
113  res_scores_(src.res_scores_),
114  res_recovered_(src.res_recovered_)
115 {}
116 
117 
118 ///@detail returns
119 /// 'O' <- cis-omega
120 /// 'G' <- alpha-L
121 /// 'E' <- E
122 /// 'A' <- helical
123 /// 'B' <- extended
124 /// 'X' <- otherwise
125 char
127  Real const phi,
128  Real const psi,
129  Real const omega
130 ) {
131  if ( std::abs( omega ) < 90 ) {
132  return 'O'; // cis-omega
133  } else if ( phi >= 0.0 ) {
134  if ( -100 < psi && psi <= 100 ) {
135  return 'G'; // alpha-L
136  } else {
137  return 'E'; // E
138  }
139  } else {
140  if ( -125 < psi && psi <= 50 ) {
141  return 'A'; // helical
142  } else {
143  return 'B'; // extended
144  }
145  }
146  return 'X';
147 }
148 
149 
150 
151 void
153  Pose const & native_pose) {
154  if ( !native_pose_ && !initialized_ ){
155  native_pose_ = new Pose( native_pose ); // deep copy please
156  } else {
157  TR << "Attempting to set the native pose in PerNativeRRReporterHuman after it has already been initialized." << endl;
158  utility_exit();
159  }
160 
161  for ( Size ii = 1; ii <= native_pose.total_residue(); ++ii ) {
162  Residue const & res( native_pose.residue(ii) );
163  nat_bb_bins_.push_back(
165  res.mainchain_torsion(1),
166  res.mainchain_torsion(2),
167  res.mainchain_torsion(3)
168  )
169  );
170 
171  RotVector rot;
172  rotamer_from_chi( res, rot );
173  nat_rots_.push_back( rot );
174 
175  //vector1< Real > chi_vec;
177 
178  for ( Size jj = 1; jj <= res.nchi(); ++jj ) {
179  chi_vec.push_back( core::Size( native_pose.chi(jj,ii) ) );
180  }
181  nat_chis_.push_back( chi_vec );
182 
183  }
184 
185  res_scores_.resize( native_pose.total_residue() );
186  res_recovered_.resize( native_pose.total_residue() );
187 
188  initialized_ = true;
189 }
190 
191 void
193  Pose const &,
194  Residue const & res,
195  Real const score,
196  bool const recovered)
197 {
198  if( !initialized_ ){
199  utility_exit_with_message("Attempting to report rotamer recovery when the native has not be set");
200  }
201 
202  res_scores_[ res.seqpos() ].push_back( score );
203  res_recovered_[ res.seqpos() ].push_back( recovered );
204 }
205 
206 bool
208 ) const {
209  return initialized_;
210 }
211 
212 void
214  ostream & out,
215  Size const column_width,
216  Size const precision
217 ) const {
218  out << "#Structure: " << tag_from_pose( *native_pose_ ) << endl;
219 
220  for(Size ii=1; ii <= native_pose_->total_residue(); ++ii ){
221  if( res_scores_[ii].size() == 0 ) continue;
222 
223  Real mean_score = mean(
224  res_scores_[ii].begin(), res_scores_[ii].end(), Real(0) );
225 
226  Real std_dev_score = std_dev_with_provided_mean(
227  res_scores_[ii].begin(), res_scores_[ii].end(), mean_score );
228 
229  out << A(column_width,string_of(ii));
230  out << A(column_width,nat_bb_bins_[ii]);
231  for( Size i=1; i <= 4; ++i ){
232  if( i <= nat_rots_[ii].size() ){
233  out << F(column_width,
234  precision,
235  static_cast< long double >(nat_rots_[ii][i]));
236  } else {
237  out << A(column_width, "");
238  }
239  }
240  out << F(column_width,precision,mean_score);
241  out << F(column_width,precision,std_dev_score);
242  out << endl;
243  }
244 }
245 
246 
248  protocol_name_(),
249  protocol_params_(),
250  comparer_name_(),
251  comparer_params_(),
252  column_width_(12),
253  precision_(4),
254  per_native_recovery_(),
255  residues_considered_(0),
256  rotamers_recovered_(0),
257  recovery_score_mean_(0),
258  recovery_score_m2_(0)
259 {}
260 
262  RRReporter(),
263  protocol_name_(src.protocol_name_),
264  protocol_params_(src.protocol_params_),
265  comparer_name_(src.comparer_name_),
266  comparer_params_(src.comparer_params_),
267  column_width_(src.column_width_),
268  precision_(src.precision_),
269  per_native_recovery_(src.per_native_recovery_),
270  residues_considered_(src.residues_considered_),
271  rotamers_recovered_(src.rotamers_recovered_),
272  recovery_score_mean_(src.recovery_score_mean_),
273  recovery_score_m2_(src.recovery_score_m2_)
274 {}
275 
277 
278 void
280  string const & protocol_name,
281  string const & protocol_params
282 ){
283  protocol_name_ = protocol_name;
284  protocol_params_ = protocol_params;
285 }
286 
287 void
289  string const & comparer_name,
290  string const & comparer_params
291 ){
292  comparer_name_ = comparer_name;
293  comparer_params_ = comparer_params;
294 }
295 
296 void
298 
299  per_native_recovery_.clear();
304 }
305 
306 void
308  Pose const & pose1,
309  Pose const & pose2,
310  Residue const & /*res1*/,
311  Residue const & res2,
312  Real const score,
313  bool const recovered
314 ) {
316  rotamers_recovered_ += recovered;
317 
318  // Update mean and M2 with online algorithm
319  // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
320  Real delta = score - recovery_score_mean_;
321  recovery_score_mean_ += delta/residues_considered_;
322  recovery_score_m2_ += delta*(score - recovery_score_mean_); // uses new mean
323 
324  string pname = tag_from_pose( pose1 );
325  PerNativeRRReporterHuman & native_recovery( per_native_recovery_[ pname ] );
326  if( ! native_recovery.initialized() ){
327  native_recovery.set_native( pose1 );
328  }
329  native_recovery.report_rotamer_recovery( pose2, res2, score, recovered );
330 
331 }
332 
333 void
334 RRReporterHuman::write_header( ostream & out ) const {
335 
336  out << "#Rotamer Recovery Benchmark" << endl;
337  out << "#Protocol: " << protocol_name_ << endl;
338  out << "#Protocol Paremeters: " << protocol_params_ << endl;
339  out << "#Comparer: " << comparer_name_ << endl;
340  out << "#Comparer Paremeters: " << comparer_params_ << endl;
341  out << "#Number of native structures: " << per_native_recovery_.size() << endl;
342  out << "#Number of residues in decoys: " << residues_considered_ << endl;
343 
344  Real recovery_rate = static_cast< Real >(rotamers_recovered_) / static_cast< Real >(residues_considered_);
345 
346  out << "#Recovery rate: " << recovery_rate << endl;
347  out << "#" << comparer_name_ << " Recovery score mean: " << recovery_score_mean_ << endl;
348 
349  Real recovery_score_sample_variance = recovery_score_m2_ / static_cast< Real >(residues_considered_);
350 
351  out << "#" << comparer_name_ << " Recovery score sample variance: " << recovery_score_sample_variance << endl;
352 
353  out << endl << endl;
354 
355  out << A( column_width_, "resi_idx" );
356  out << A( column_width_, "nat_bb_bin" );
357  out << A( column_width_, "nat_rot1" );
358  out << A( column_width_, "nat_rot2" );
359  out << A( column_width_, "nat_rot3" );
360  out << A( column_width_, "nat_rot4" );
361  out << A( column_width_, "E[score]" );
362  out << A( column_width_, "SD[score]" );
363  out << endl;
364 
365 }
366 
367 void
368 RRReporterHuman::show( ostream & out ) const {
369 
370  write_header( out );
371  out << endl;
372 
373  for(map< string, PerNativeRRReporterHuman >::const_iterator
374  nat_it = per_native_recovery_.begin(),
375  nat_it_end = per_native_recovery_.end();
376  nat_it != nat_it_end; ++nat_it)
377  {
378  nat_it->second.show( out );
379  out << endl;
380  }
381 
382 }
383 
384 void
386  TR << "Recovered " << rotamers_recovered_ << " rotamers"
387  << " at " << residues_considered_ << " residues"
388  << " for a recovery rate of " << recovery_rate() << "." << endl;
389 }
390 
391 Real
394 }
395 
396 
397 } // rotamer_recovery
398 } // protocols
399