Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PeakCalibrator.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 FragmentSampler.cc
11 /// @brief ab-initio fragment assembly protocol for proteins
12 /// @detailed
13 /// Contains currently: Classic Abinitio
14 ///
15 ///
16 /// @author Oliver Lange
17 
18 // Unit Headers
20 
21 // Package Headers
24 
25 // Project Headers
26 
27 // Utility headers
28 #include <basic/Tracer.hh>
29 
30 //// C++ headers
31 #include <cmath>
32 #include <map>
33 
34 #include <utility/vector1.hh>
35 
36 
37 static basic::Tracer tr("protocols.noesy_assign.crosspeaks");
38 
39 using core::Real;
40 using namespace core;
41 using namespace basic;
42 //using namespace basic::options;
43 //using namespace basic::options::OptionKeys;
44 
45 #ifdef _WIN32
46 #include <float.h> // REQUIRED FOR WINDOWS
47 #endif
48 
49 const char* CALIBRATOR_TYPE_NAMES[]={"NONE","BACKBONE","BETA_NON_METHYL","METHYL","SIDECHAIN" };
50 
51 namespace protocols {
52 namespace noesy_assign {
53 
54 /// @details Auto-generated virtual destructor
55 PeakCalibratorMap::~PeakCalibratorMap() {}
56 
57 /// @details Auto-generated virtual destructor
58 PeakCalibrator::~PeakCalibrator() {}
59 
60 PeakCalibratorMap::PeakCalibratorMap( CrossPeakList& list, PeakCalibratorOP calibrator_template ) {
61  PeakCalibratorOP fresh_calibrator = calibrator_template->fresh_instance();
62  for ( CrossPeakList::iterator it = list.begin(); it != list.end(); ++it ) {
63  std::pair< CalibratorMap::iterator, bool > last_insert;
64  last_insert = calibrators_.insert( std::make_pair( (*it)->filename(), fresh_calibrator ) );
65  if ( last_insert.second ) { //true if new element was inserted
66  fresh_calibrator = calibrator_template->fresh_instance();
67  }
68  (last_insert.first->second)->add_peak( *it );
69  }
70  for ( CalibratorMap::iterator it=calibrators_.begin(); it!=calibrators_.end(); ++it ) {
71  it->second->init_calibrator();
72  }
73 }
74 
75 void PeakCalibratorMap::set_new_upper_bounds() {
76  for ( CalibratorMap::iterator it=calibrators_.begin(); it!=calibrators_.end(); ++it ) {
77  it->second->set_new_upper_bounds();
78  }
79 }
80 
81 void PeakCalibratorMap::do_calibration() {
82  for ( CalibratorMap::iterator it=calibrators_.begin(); it!=calibrators_.end(); ++it ) {
83  tr.Info << "Calibrate " << it->first << "..." << std::endl;
84  it->second->do_calibration();
85  }
86 }
87 
88 void PeakCalibratorMap::set_target_and_tolerance( core::Real target, core::Real tolerance ) {
89  for ( CalibratorMap::iterator it=calibrators_.begin(); it!=calibrators_.end(); ++it ) {
90  it->second->set_target_and_tolerance( target, tolerance );
91  }
92 }
93 
94 void PeakCalibratorMap::eliminate_violated_constraints() {
95  for ( CalibratorMap::iterator it=calibrators_.begin(); it!=calibrators_.end(); ++it ) {
96  it->second->eliminate_violated_constraints();
97  }
98 }
99 
100 PeakCalibrator::PeakCalibrator( int target_sign )
101  : max_type_direct_( BETA_NON_METHYL + 1 ),
102  target_sign_( target_sign )
103 {
105  if ( !params.atom_dependent_calibration_ ) {
106  max_type_ = BACKBONE + 1;
108  } else {
110  }
111 }
112 
113 
115  target_ = target;
116  tolerance_ = tolerance;
117 }
118 
120  for ( Size type=BACKBONE; type < max_type_; ++type ) {
121  accumulated_count_[ type ] = 0;
122  accumulated_target_[ type ] = 0;
123  target_values_[ type ].clear();
124  }
125 }
126 
127 
130  bool const use_median( params.calibration_use_median_ );
131  bool finished = true;
132  for ( Size type = BACKBONE; type < max_type_direct_; ++type ) {
133  if ( accumulated_count_[ type ] ) {
134  // tr.Debug << " acc. target: " << accumulated_target_[ type ] << " acc. count: " << accumulated_count_[ type ] << std::endl;
135  core::Real average_target = accumulated_target_[ type ] / accumulated_count_[ type ];
136  core::Real median;
137  if ( use_median ) {
138  target_values_[ type ].sort();
139  TargetValues::const_iterator it = target_values_[type].begin();
140  for ( Size i=1; i<= target_values_[ type ].size()/2; ++i ) {
141  ++it;
142  }
143  median = *it;
144  average_target = median;
145  accumulated_target_[ type ] = median * accumulated_count_[ type ];
146  }
147 #ifdef _WIN32
148  if ( _isnan(average_target) || !_finite( average_target)) continue; // REQUIRED FOR WINDOWS
149 #else
150  if ( std::isnan(average_target) || std::isinf( average_target)) continue;
151 #endif
152  if ( target_sign_* ( average_target - target_) < -tolerance_ ) {
153  interpolate_too_small( type );
154  finished = false;
155  } else if ( target_sign_* ( average_target - target_ ) > tolerance_ ) {
156  interpolate_too_big( type );
157  finished = false;
158  }
159  }
160  }
163  return finished;
164 }
165 
168  calibration_constant_[ type ] = exp( 0.5*( log( calibration_constant_lows_[ type ] ) + log( calibration_constant_highs_[ type ] ) ) );
169 }
170 
173  calibration_constant_[ type ] = exp( 0.5*( log( calibration_constant_lows_[ type ] )+ log( calibration_constant_highs_[ type ] ) ) );
174 }
175 
177  for ( core::Size type = BACKBONE; type < max_type_; ++type ) {
178  if ( types.test( type ) ) {
179  accumulated_count_[ type ] += 1;
180  accumulated_target_[ type ] += target;
181  target_values_[ type ].push_back( target );
182  }
183  }
184  // tr.Debug << "acc. " << accumulated_target_[ BACKBONE ] << std::endl;
185 }
186 
187 // void PeakCalibrator::reset_statistics() {
188 // for ( Size type=BACKBONE; type < max_type_; ++type ) {
189 // accumulated_count_[ type ] = 0;
190 // accumulated_dist_[ type ] = 0;
191 // }
192 // }
193 
195  for ( Size type=BACKBONE; type < max_type_; ++type ) {
196  calibration_constant_[ type ] = 1e10;
197  calibration_constant_lows_[ type ] = 1;
198  calibration_constant_highs_[ type ] = 1e20;
199  }
200 }
201 
203  bool finished = false;
204  Size max_cycles = 50;
205 
207 
208  tr.Info << "Calibration .... for " << peaks_.size() << " crosspeaks " << std::endl;
209 
210  // Q_backbone_ = calibration_constant_[ BACKBONE ];
211  tr.Info << "value target ";
212  for ( core::Size type=BACKBONE; type< (Size) max_type_; type++ ) {
213  tr.Info << " " << CALIBRATOR_TYPE_NAMES[type];
214  }
215  tr.Info << std::endl;
216  while ( !finished && max_cycles ) {
217  --max_cycles;
219  set_new_upper_bounds(); //compute statistics about upper bounds
220  // show_statistics( tr.Info );
221  tr.Info << target_;
222  tr.Info << " ";
223  for ( core::Size type=BACKBONE; type< (Size) max_type_; type++ ) {
224  if ( accumulated_count_[ type ] ) {
225  tr.Info << " " << accumulated_target_[ type ] / accumulated_count_[ type ];
226  } else tr.Info << " -1 ";
227  tr.Info << " " << calibration_constant_[ type ];
228  }
229  tr.Info << std::endl;
230  finished = interpolate_on_statistics();
231  }
232 }
233 
234 // void PeakCalibrator::interpolate( PeakCalibrator const& cal1, PeakCalibrator const& cal2 ) {
235 // Q_backbone_ = exp( 0.5*( log( cal1.Q_backbone_) + log( cal2.Q_backbone_ ) ));
236 // Q_nonmethyl_beta_ = exp( 0.5*( log(cal1.Q_nonmethyl_beta_) + log( cal2.Q_nonmethyl_beta_) ));
237 // Q_methyl_ = 3.0 * Q_backbone_;
238 // Q_nonmethyl_sidechain_ = 1.5 * Q_nonmethyl_beta_;
239 // }
240 
242  peaks_.push_back( peak );
243 }
244 
246  core::Size ct( 1 );
247  for ( utility::vector1< CrossPeakOP >::iterator it = peaks_.begin(); it != peaks_.end(); ++it, ++ct ) {
248  TypeCumulator types;
249  (*it)->calibrate( *this, types );
250  core::Real dist( (*it)->distance_bound() );
251  // if ( dist <= 0.01 ) continue;
252  if ( dist == 0 ) continue;
253  collect_upperbound_statistics( ct, types );
254  }
255 }
256 
257 
259  using namespace core::chemical; //for AA
261  if ( !params.atom_dependent_calibration_ ) return BACKBONE;
262  std::string const& name = atom.atom();
263  if ( name == "HA" || name == "H" || name == "1H" || name == "2H" || name == "QA" || name =="1HA" || name=="2HA" ) {
264  return BACKBONE;
265  }
266  if ( name.find("B") != std::string::npos ) {
267  if ( aa==aa_ala ) return METHYL;
268  return BETA_NON_METHYL;
269  }
270  if ( name.find("G") != std::string::npos ) {
271  if ( aa==aa_val || aa==aa_thr ) return METHYL;
272  }
273  if ( name.find("G2") != std::string::npos && aa==aa_ile ) {
274  return METHYL;
275  }
276  if ( name.find("D") != std::string::npos && ( aa==aa_leu || aa==aa_ile ) ) {
277  return METHYL;
278  }
279  if ( name.find("E") != std::string::npos && ( aa==aa_met ) ) {
280  return METHYL;
281  }
282  return SIDECHAIN;
283 }
284 
285 
286 }
287 }