Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OperatorFilter.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/simple_filters/OperatorFilter.cc
11 /// @brief
12 /// @author Gabi Pszolla & Sarel Fleishman
13 
14 
15 //Unit Headers
20 #include <utility/tag/Tag.hh>
21 //Project Headers
22 #include <basic/Tracer.hh>
23 #include <core/pose/Pose.hh>
25 #include <boost/foreach.hpp>
26 #define foreach BOOST_FOREACH
27 #include <utility/string_util.hh>
29 namespace protocols{
30 namespace simple_filters {
31 
32 static basic::Tracer TR( "protocols.simple_filters.Operator" );
33 using namespace protocols::filters;
34 
37 
39 OperatorFilterCreator::keyname() const { return "Operator"; }
40 
41 //default ctor
43 protocols::filters::Filter( "Operator" ),
44 operation_( PRODUCT ),
45 threshold_( 0.0 ),
46 negate_( false )
47 {
48  filters_.clear();
49 }
50 
52 
53 void
54 Operator::reset_baseline( core::pose::Pose const & pose, bool const attempt_read_from_checkpoint ){
55  foreach( protocols::filters::FilterOP filter, filters() ){
56  if( filter->get_type() == "Sigmoid" ){
57  SigmoidOP sigmoid_filter( dynamic_cast< Sigmoid * >( filter() ) );
58  runtime_assert( sigmoid_filter );
59  sigmoid_filter->reset_baseline( pose, attempt_read_from_checkpoint );
60  TR<<"Resetting Sigmoid filter's baseline"<<std::endl;
61  }
62  else if( filter->get_type() == "Operator" ){ //recursive call
63  OperatorOP operator_filter( dynamic_cast< Operator * >( filter() ) );
64  runtime_assert( operator_filter );
65  operator_filter->reset_baseline( pose, attempt_read_from_checkpoint );
66  TR<<"Resetting Operator filter's baseline"<<std::endl;
67  }
68  else if( filter->get_type() == "CompoundStatement" ){///all RosettaScripts user-defined filters with confidence!=1 are compoundstatements
69  CompoundFilterOP comp_filt_op( dynamic_cast< CompoundFilter * >( filter() ) );
70  runtime_assert( comp_filt_op );
71  for( protocols::filters::CompoundFilter::CompoundStatement::iterator cs_it = comp_filt_op->begin(); cs_it != comp_filt_op->end(); ++cs_it ){
72  protocols::filters::FilterOP f( cs_it->first );
73  if( f->get_type() == "Sigmoid" ){
74  SigmoidOP sigmoid_filter( dynamic_cast< Sigmoid * >( f() ) );
75  runtime_assert( sigmoid_filter );
76  sigmoid_filter->reset_baseline( pose, attempt_read_from_checkpoint );
77  TR<<"Resetting Sigmoid filter's baseline"<<std::endl;
78  }
79  else if( f->get_type() == "Operator" ){ //recursive call
80  OperatorOP operator_filter( dynamic_cast< Operator * >( f() ) );
81  runtime_assert( operator_filter );
82  operator_filter->reset_baseline( pose, attempt_read_from_checkpoint );
83  TR<<"Resetting Operator filter's baseline"<<std::endl;
84  }
85  }//for cs_it
86  }//elseif CompoundStatement
87  }//foreach
88 }
89 
90 void
92  utility::vector1< FilterOP > erase_filters;
93  erase_filters.clear();
94  foreach( FilterOP filter, filters_ ){
95  if( filter->get_type() == "Sigmoid" ){
96  SigmoidOP sigmoid_filter( dynamic_cast< Sigmoid * >( filter() ) );
97  runtime_assert( sigmoid_filter );
98  if( sigmoid_filter->filter()->get_type() == "RelativePose" ){
99  TR<<"Replicating and changing RelativePose's filter pdb fname. File names: ";
100  foreach( std::string const fname, relative_pose_names_ ){
101  SigmoidOP new_sigmoid( *sigmoid_filter );
102  RelativePoseFilterOP relative_pose( dynamic_cast< RelativePoseFilter * >( new_sigmoid->filter()() ) );
103  runtime_assert( relative_pose );
104  relative_pose->pdb_name( fname );
105  add_filter( new_sigmoid );
106  TR<<fname<<", ";
107  }
108  TR<<std::endl;
109  erase_filters.push_back( filter );
110  }
111  }
112  }
113  foreach( FilterOP erase_f, erase_filters ){
114  filters_.erase( std::find( filters_.begin(), filters_.end(), erase_f ) );
115  }
116 }
117 
118 
119 void
121 {
122  std::string const op( tag->getOption< std::string >( "operation" ) );
123  if( op=="SUM" )
124  operation( SUM );
125  if( op=="PRODUCT" )
126  operation( PRODUCT );
127  if( op=="NORMALIZED_SUM" )
129  if( op=="MAX" )
130  operation( MAX );
131  if( op=="MIN" )
132  operation( MIN );
133  if( op=="SUBTRACT" )
134  operation( SUBTRACT );
135  if( op=="ABS" )
136  operation( ABS );
137  if( op == "BOOLEAN_OR" )
139  if( op != "SUM" && op != "PRODUCT" && op != "NORMALIZED_SUM" && op != "MAX" && op != "MIN" && op != "SUBTRACT" && op != "ABS" && op != "BOOLEAN_OR" )
140  utility_exit_with_message( "Operation " + op + " not recognized" );
141  threshold( tag->getOption< core::Real >( "threshold", 0 ) );
142  negate( tag->getOption< bool >( "negate", false ) );
143 
144  utility::vector1< std::string > const filter_names( utility::string_split( tag->getOption< std::string >( "filters" ), ',' ) );
145  foreach( std::string const fname, filter_names ){
147  TR<<"Adding filter "<<fname<<std::endl;
148  }
149  if( ( operation() == SUBTRACT || operation() == BOOLEAN_OR ) && filters_.size() != 2 )
150  utility_exit_with_message( "Operation "+op+" requested, but the number of filters provided is different than 2. I only know how to "+op+" one filter from another" );
151  if( operation() == ABS && filters_.size() != 1 )
152  utility_exit_with_message( "Operation ABS requested, but the number of filters provided is different than 1. I only know how to take the absolute value of one filter" );
153  multi_relative( tag->getOption< bool >( "multi_relative", false ) );
154  if( multi_relative() ){
155  TR<<"multi_relative is set. Duplicating filters to include relative_pose_names."<<std::endl;
156  relative_pose_names_ = utility::string_split( tag->getOption< std::string >( "relative_pose_names" ), ',' );
158  }
159  TR<<" using operator "<<op<<" with "<< filters_.size()<<" filters "<<std::endl;
160 }
161 
162 bool
163 Operator::apply( core::pose::Pose const & pose ) const {
164  core::Real const val ( compute( pose ) );
165  TR<<"Filter returns "<<val<<std::endl;
166  if( negate() )
167  return( val <= threshold() );
168  else
169  return( val >= threshold() );
170 }
171 
172 void
173 Operator::report( std::ostream &o, core::pose::Pose const & pose ) const {
174  core::Real const val = compute( pose );
175  o << "Operator returns "<<val<<std::endl;
176 }
177 
179 Operator::report_sm( core::pose::Pose const & pose ) const {
180  core::Real const val( compute( pose ) );
181  TR<<"Operator returning: "<<val<<std::endl;
182  return( val );
183 }
184 
187  core::pose::Pose const & pose
188 ) const {
189  core::Real val( 0.0 );
190  if( operation() == SUBTRACT || operation() == BOOLEAN_OR ){
191  runtime_assert( filters().size() == 2 );
192  core::Real const val1( filters()[ 1 ]->report_sm( pose ) );
193  core::Real const val2( filters()[ 2 ]->report_sm( pose ) );
194  core::Real const difference( negate() ? val2 - val1 : val1 - val2 );
195  core::Real const boolean_or( negate() ? -( val1 + val2 - val1 * val2 ) : val1 + val2 - val1 * val2 );
196  TR<<"Filters' values "<<val1<<" "<<val2;
197  if( operation() == SUBTRACT ){
198  TR<<" and the difference is "<<difference<<std::endl;
199  return difference;
200  }
201  else if( operation() == BOOLEAN_OR ){
202  TR<<" the boolean or value is "<<boolean_or<<std::endl;
203  return boolean_or;
204  }
205  }
206  if( operation() == ABS ){
207  runtime_assert( filters().size() == 1 );
208  core::Real const val( filters()[ 1 ]->report_sm( pose ) );
209  core::Real const abs_val( negate() ? -std::abs( val ) : std::abs( val ) );
210  TR<<"Filter returns "<<val<<" and its absolute value is "<<abs_val<<std::endl;
211  return abs_val;
212  }
213  if( operation() == PRODUCT )
214  val = 1.0;
215  if( operation() == MIN )
216  val = 9999999.999;
217  if( operation() == MAX )
218  val = -99999999.999;
219  foreach( protocols::filters::FilterOP f, filters() ){
220  core::Real const filter_val( f()->report_sm( pose ) );
221  TR<<"Filter "<<f()->get_type()<<" return "<<filter_val<<std::endl;
222  if( operation() == SUM || operation() == NORMALIZED_SUM )
223  val += filter_val;
224  if( operation() == PRODUCT )
225  val *= filter_val;
226  if( operation() == MIN ){
227  if( filter_val <= val )
228  val = filter_val;
229  }
230  if( operation() == MAX ){
231  if( filter_val >= val )
232  val = filter_val;
233  }
234  }
235  if( operation() == NORMALIZED_SUM )
236  val /= (core::Real) filters().size();
237  if( negate() )
238  val = -1.0 * val;
239 
240  TR<<"Operator returns "<<val<<std::endl;
241  return( val );
242 }
243 
245 Operator::filters() const{ return filters_; }
246 
247 void
249 }
250 }