Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SplineFunc.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 core/scoring/constraints/SplineFunc.cc
11 /// @brief Constraint function for looking up data in a histogram over which a spline is created
12 /// @detailed The HistogramFunc constraints function allows one to input a histogram (in most cases, a knowledge-based potential).
13 /// This class loads a lookup table (your histogram), provides an interface for accessing it, creates a spline, and returns a value based on the spline generated from the knowledge-based potential.
14 /// @author Stephanie Hirst (stephanie.j.hirst@vanderbilt.edu)
15 
16 // Unit Headers
18 
19 // Package Headers
21 #include <basic/options/keys/constraints.OptionKeys.gen.hh>
22 #include <basic/options/option.hh>
23 #include <basic/database/open.hh>
24 
25 // Project Headers
26 #include <basic/Tracer.hh>
27 
28 // Utility and Numeric Headers
29 #include <utility/io/izstream.hh>
30 #include <utility/vector1.hh>
31 #include <numeric/interpolation/spline/SplineGenerator.hh>
32 #include <numeric/interpolation/spline/Interpolator.hh>
33 #include <numeric/interpolation/util.hh>
34 
35 // C++ Headers
36 #include <iostream>
37 #include <sstream>
38 #include <string>
39 
40 static
41 basic::Tracer TR( "core.scoring.constraints.SplineFunc");
42 
43 namespace core {
44  namespace scoring {
45  namespace constraints {
46 
48  exp_val_(0),
49  filename_(""),
50  KB_description_(""),
51  weight_(0),
52  bin_size_(0),
53  lower_bound_x_(0),
54  lower_bound_y_(0),
55  upper_bound_x_(0),
56  upper_bound_y_(0),
57  lower_bound_dy_(0),
58  upper_bound_dy_(0),
59  bins_vect_(),
60  bins_vect_size_(0),
61  potential_vect_(),
62  potential_vect_size_(0),
63  interpolator_()
64  {}
65 
67  {}
68 
69  // get_ functions to obtain values of member variables (mostly for unit test)
71  {
72  return exp_val_;
73  }
74 
76  {
77  return filename_;
78  }
79 
81  {
82  return KB_description_;
83  }
84 
86  {
87  return weight_;
88  }
89 
91  {
92  return bin_size_;
93  }
94 
96  {
97  return lower_bound_x_;
98  }
99 
101  {
102  return upper_bound_x_;
103  }
104 
106  {
107  return lower_bound_y_;
108  }
109 
111  {
112  return upper_bound_y_;
113  }
114 
116  {
117  return lower_bound_dy_;
118  }
119 
121  {
122  return upper_bound_dy_;
123  }
124 
125  // Read in data (e.g., experimental distance), weight, and histogram filename. Bind filename to stream.
126  void SplineFunc::read_data( std::istream &in)
127  {
128 
129  utility::io::izstream potential_file;
130 
131  // If constraints::epr_distance specified, read in histogram from database
132  if( basic::options::option[ basic::options::OptionKeys::constraints::epr_distance]() )
133  {
134  in >> KB_description_ >> exp_val_ >> weight_ >> bin_size_;
135  filename_ = basic::database::full_name("scoring/constraints/epr_distance_potential.histogram");
136  //basic::database::open( potential_file, "scoring/constraints/epr_distance_potential.histogram");
137  }
138 
139  // Else, read in potential specified in constraints file
140  else{
142  //potential_file.open( filename_.c_str());
143  }
144 
145  numeric::interpolation::spline::SplineGenerator common_spline(numeric::interpolation::spline_from_file(filename_,bin_size_));
146  interpolator_ = common_spline.get_interpolator();
147  lower_bound_x_ = common_spline.get_lbx();
148  //std::cout << "now in SplineFunc..." << std::endl;
149  //std::cout << "histogram_lbx: " << lower_bound_x_ << std::endl;
150  upper_bound_x_ = common_spline.get_ubx();
151  //std::cout << "histogram_ubx: " << upper_bound_x_ << std::endl;
152  lower_bound_y_ = common_spline.get_lby();
153  //std::cout << "histogram_lby: " << lower_bound_y_ << std::endl;
154  upper_bound_x_ = common_spline.get_ubx();
155  //std::cout << "" << "histogram_uby: " << upper_bound_y_ << std::endl;
156  lower_bound_dy_ = common_spline.get_lbdy();
157  upper_bound_dy_ = common_spline.get_ubdy();
158  bins_vect_size_ = common_spline.get_num_points();
159 
160  } // read_data()
161 
162  /// @brief Returns the value of this SplineFunc evaluated at distance x.
163  // Insert spline into a lookup table and get potential
165  {
166  core::Real potential_energy;
167  core::Real delta_potential_energy;
168  core::Real weighted_potential_energy;
169 
170  // This will be added an EPR-specific class later
171  if (KB_description_ == "EPR_DISTANCE")
172  {
173  // calculate sl-cb
174  core::Real diff = exp_val_ - x; // for AtomPair constraints, x is the distance between atoms/residues in the model.
175 
176  // The upper and lower bounds of the x-axis are hard coded. This is a hack but will be fixed later!
177  if( diff >= -15.0 && diff <= 15.0 )
178  {
179  interpolator_->interpolate( diff, potential_energy, delta_potential_energy);
180  weighted_potential_energy = ( weight_*potential_energy);
181  return weighted_potential_energy;
182  }
183  else
184  {
185  // Return potential = 0 for sl-cb values outside of lowest and highest values in histogram
186  potential_energy = 0;
187  return potential_energy;
188  }
189  }
190  else // for other cases that are not EPR Distance restraints, just return potential for x, not sl-cb
191  {
192  if(KB_description_ == "difference")
193  {
194  //std::cout << "using difference!" << std::endl;
195  //calculate exp_distance - CB distance
196  core::Real diff = exp_val_ - x; // for AtomPair constraints, x is the distance between atoms/residues in the model
197 
198  //std::cout << "x is: " << x << " and difference is: " << diff << " and weight is: " << weight_ << std::endl;
199  if( diff >= lower_bound_x_ && diff <= upper_bound_x_ )
200  {
201  interpolator_->interpolate( diff, potential_energy, delta_potential_energy);
202  weighted_potential_energy = ( weight_*potential_energy);
203  return weighted_potential_energy;
204  }
205  else
206  {
207  // Return potential = 0 for x values outside of lowest and highest values in histogram
208  potential_energy = 0;
209  return potential_energy;
210  }
211  //std::cout << "potential energy is: " << potential_energy<< " and " << weighted_potential_energy << std::endl;
212  }
213  else
214  {
215  if( x >= lower_bound_x_ && x <= upper_bound_x_ )
216  {
217  interpolator_->interpolate( x, potential_energy, delta_potential_energy);
218  weighted_potential_energy = ( weight_*potential_energy);
219  return weighted_potential_energy;
220  }
221  else
222  {
223  // Return potential = 0 for x values outside of lowest and highest values in histogram
224  potential_energy = 0;
225  return potential_energy;
226  }//else
227  }//else
228  }
229 
230  } // SplineFunc::func()
231 
232  /// @brief Returns the value of the first derivative of this SplineFunc at distance x.
234  {
235  core::Real potential_energy;
236  core::Real delta_potential_energy;
237 
238  // This will be added an EPR-specific class later
239  if (KB_description_ == "EPR_DISTANCE")
240  {
241  // calculate sl-cb
242  core::Real diff = exp_val_ - x; // for AtomPair constraints, x is the distance between atoms/residues in the model.
243  if( diff >= -15.0 && diff <= 16.0 )
244  {
245  interpolator_->interpolate( diff, potential_energy, delta_potential_energy);
246  return ( weight_*delta_potential_energy);
247  }
248 
249  // Return delta_potential = 0 for sl-cb values outside of lowest and highest values in histogram
250  return 0;
251  }
252  else // for other cases that are not EPR Distance restraints, just return potential for x, not sl-cb
253  {
254  if( x >= lower_bound_x_ && x <= upper_bound_x_ )
255  {
256  interpolator_->interpolate( x, potential_energy, delta_potential_energy);
257  return ( weight_*delta_potential_energy);
258  }
259 
260  // Return delta_potential_energy = 0 for x values outside of lowest and highest values in histogram
261  return 0;
262  }
263 
264  } // SplineFunc::dfunc()
265 
266  /// @brief show the definition of this SplineFunc to the specified output stream.
267  void SplineFunc::show_definition( std::ostream &out ) const
268  {
269  out << "SPLINEFUNC:" << "\t" << "filename: " << filename_ << "\t" << "Description: " << KB_description_ << "\t"
270  << "exp_val: " << exp_val_ << "\t" << "weight: " << weight_ << "\t" << "bin_size: " << bin_size_ << std::endl;
271  }
272 
273  /// @brief show some sort of stringified representation of the violations for this constraint.
274  core::Size SplineFunc::show_violations( std::ostream &out, core::Real x, core::Size verbose_level, core::Real threshold) const
275  {
276  core::Real potential_energy;
277  core::Real delta_potential_energy;
278  core::Real weighted_potential_energy;
279 
280  if ( KB_description_ == "EPR_DISTANCE")
281  {
282  // calculate sl-cb
283  core::Real diff = exp_val_ - x; // for AtomPair constraints, x is the distance between atoms/residues in the model.
284 
285  // calculate potential_energy again
286  if( diff >= -15.0 && diff <= 16.0 )
287  {
288  interpolator_->interpolate( diff, potential_energy, delta_potential_energy);
289  weighted_potential_energy = (weight_*potential_energy);
290  }
291  else
292  {
293  // Return potential = 0 for sl-cb values outside of lowest and highest values in histogram
294  weighted_potential_energy = 0;
295  }
296 
297  // output according to specified verbosity level
298  if ( verbose_level > 100 )
299  {
300  out << "SPLINEFUNC:" << "\t" << "Description: " << KB_description_ << "\t"
301  << "exp_val: " << exp_val_ << "\t" << "model_dist: " << x << "\t" << "dsl-dcb: " << diff << "\t"
302  << "weighted_potential: " << weighted_potential_energy << "\t"
303  << "weight:" << "\t" << weight_ << std::endl;
304  }
305  else if ( verbose_level > 70 )
306  {
307  out << "SPLINEFUNC:" << "\t" << "Description: " << KB_description_ << "\t"
308  << "exp_val: " << exp_val_ << "\t" << "model_dist: " << x << "\t" << "dsl-dcb: " << diff << std::endl;
309  }
310  }
311  else // for other cases that are not EPR Distance restraints, just return potential for x, not sl-cb
312  {
313  if( x >= lower_bound_x_ && x <= upper_bound_x_ )
314  {
315  interpolator_->interpolate( x, potential_energy, delta_potential_energy);
316  weighted_potential_energy = (weight_*potential_energy);
317  }
318  else
319  {
320  // Return potential = 0 for x values outside of lowest and highest values in histogram
321  weighted_potential_energy = 0;
322  }
323 
324  // output according to specified verbosity level
325  if ( verbose_level > 100 )
326  {
327  out << "SPLINEFUNC:" << "\t" << "Description: " << KB_description_ << "\t" << "exp_val: " << exp_val_ << "\t"
328  << "model_dist: " << x << "\t" << "exp_val - x: " << exp_val_ - x << "\t" << "weighted_potential: " << weighted_potential_energy
329  << "\t" << "weight:" << weight_ << std::endl;
330  }
331  else if ( verbose_level > 70 )
332  {
333  out << "SPLINEFUNC:" << "\t" << "Description: " << KB_description_ << "\t"
334  << "exp_val: " << exp_val_ << "\t" << "model_dist: " << x << std::endl;
335  }
336  }
337 
338  return Func::show_violations( out, x, verbose_level, threshold);
339 
340  } // show_violations()
341 
342  } // constraints
343  } // scoring
344 } // core