Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CalculatorFilter.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 sw=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/filters/CalculatorFilter.cc
11 /// @brief Combine several filters in a (semi) arbitrary calculation
12 /// @author Rocco Moretti (rmoretti@u.washington.edu)
13 
16 
17 #include <core/pose/Pose.hh>
18 #include <utility/tag/Tag.hh>
22 #include <basic/Tracer.hh>
23 #include <core/types.hh>
25 
26 #include <utility/vector1.hh>
27 #include <numeric/Calculator.hh>
28 #include <numeric/random/random.hh>
29 
30 // Boost Headers
31 #include <boost/foreach.hpp>
32 #define foreach BOOST_FOREACH
33 
34 namespace protocols {
35 namespace filters {
36 
37 static basic::Tracer TR( "protocols.filters.CalculatorFilter" );
38 
39 ///@brief default ctor
41  Filter( "CalculatorFilter" ),
42  threshold_(0)
43 {}
44 
46  Filter( "CalculatorFilter" ),
47  threshold_(0)
48 {
49  calc_ = new numeric::Calculator(equation);
50 }
51 
53  Filter( "CalculatorFilter" ),
54  calc_(other.calc_),
55  values_(other.values_),
56  filters_(other.filters_),
57  threshold_(other.threshold_)
58 {}
59 
60 
62 
63 bool
65 {
66  core::Real value = compute(pose);
67  return value <= threshold_;
68 }
69 
72 {
73  return( compute(pose) );
74 }
75 
76 void
77 CalculatorFilter::report( std::ostream & out, core::pose::Pose const & pose) const
78 {
79  out<<"CalculatorFilter returns "<< compute(pose) <<std::endl;
80 }
81 
82 void
84  if( ! filter ) {
85  utility_exit_with_message("Calculator filter can't use non-existant (null pointer) filter.");
86  }
87  filters_[name] = filter;
88 }
89 
90 void
92  values_[name] = value;
93 }
94 
97  assert(calc_);
98  std::map< std::string, core::Real > vars(values_);
99  for( std::map<std::string, protocols::filters::FilterOP>::const_iterator iter(filters_.begin()); iter != filters_.end(); ++iter) {
100  assert(iter->second);
101  vars[ iter->first ] = (iter->second)->report_sm( pose );
102  }
103  numeric::Real value(999999);
104  if( calc_->compute(vars, value) ) {
105  TR.Error << "Problem calculating equation in CalculatorFilter - resultant value likely garbage." << std::endl;
106  }
107  return value;
108 }
109 
110 void
113  protocols::filters::Filters_map const & filters,
115  core::pose::Pose const &)
116 {
117  std::string equation = tag_ptr->getOption< std::string >( "equation" );
118  threshold_ = tag_ptr->getOption<core::Real>( "threshold", 0.0 );
119 
120  foreach(utility::tag::TagPtr sub_tag_ptr, tag_ptr->getTags() ){
121  std::string varname( sub_tag_ptr->getOption<std::string>( "name" ) );
122 
123  if( sub_tag_ptr->hasOption("filter") || sub_tag_ptr->hasOption("filter_name") ) {
124  std::string filter_name;
125  if( sub_tag_ptr->hasOption("filter_name") ) {
126  filter_name = sub_tag_ptr->getOption<std::string>( "filter_name" );
127  } else {
128  filter_name = sub_tag_ptr->getOption<std::string>( "filter" );
129  }
130  add_filter( varname, protocols::rosetta_scripts::parse_filter( filter_name , filters ) );
131  } else if ( sub_tag_ptr->hasOption("value") ) {
132  add_constant( varname, sub_tag_ptr->getOption<core::Real>( "value" ) );
133  } else {
134  utility_exit_with_message("CalculatorFilter subtag must have either filter or value set.");
135  }
136  }
137 
138  // Now do a quick sanity check for the equation parsing.
139  calc_ = new numeric::Calculator( equation );
140  std::map< std::string, core::Real > vars(values_);
141  for( std::map<std::string, protocols::filters::FilterOP>::iterator iter(filters_.begin()); iter != filters_.end(); ++iter) {
142  vars[ iter->first ] = 1.0 + 0.00001 * numeric::random::uniform(); // Additional random to avoid "1/(alpha - beta)" type situations.
143  }
144  numeric::Real dummy;
145  if( calc_->compute(vars, dummy) ) {
146  utility_exit_with_message("Bad equation in CalculatorFilter: " + equation);
147  }
148 }
149 
150 FilterOP
152 
154 CalculatorFilterCreator::keyname() const { return "CalculatorFilter"; }
155 
156 } // filters
157 } // protocols