forked from RosettaCommons/rosetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosettascripts.hh
294 lines (240 loc) · 10.9 KB
/
rosettascripts.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
// vi: set ts=2 noet:
//
// (c) Copyright Rosetta Commons Member Institutions.
// (c) This file is part of the Rosetta software suite and is made available under license.
// (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
// (c) For more information, see http://www.rosettacommons.org. Questions about this can be
// (c) addressed to University of Washington CoMotion, email: [email protected].
/// @file test/util/rosettascripts.hh
/// @brief helper functions for unit testing RosettaScripts functionality.
/// @author Rocco Moretti ([email protected])
// Note: I don't claim this is a complete toolkit for unittesting RosettaScripts components.
// Feel free to add anything here that makes it simpler for you to add Unit tests for RosettaScripts functionality
// // Sample parse_my_tag() test
//
// basic::datacache::DataMap data;
//
// prime_Data( data ); // Adds score12, commandline scorefunctions, null mover, true/false filters - optional
//
// filters["stubby"] = new StubFilter( true, -3.14 );
//
// ScoreFunctionOP rep_only = ScoreFunctionFactory::create_score_function( "fa_rep_only" );
// data.add( "scorefxns", "rep", rep_only );
//
// MyMover testmover;
// TagCOP tag = tagptr_from_string("<MyTest name=test filter=stubby mover=null reps=4>\n"
// "</MyTest>"); // Remember that C++ has implicit string literal concatenation, but note that the \n is required for the tag parser
// testmover.parse_my_tag( tag, data, filters, movers, pose );
//
//
// TS_ASSERT_EQUALS( testmover.reps(), 4 )
// //... etc.
#ifndef INCLUDED_util_rosettascripts_HH
#define INCLUDED_util_rosettascripts_HH
// Test headers
#include <cxxtest/TestSuite.h>
// Core headers
#include <core/pose/Pose.fwd.hh>
#include <core/scoring/ScoreFunction.hh>
#include <core/scoring/ScoreFunctionFactory.hh>
#include <core/select/residue_selector/ResidueSelector.fwd.hh>
#include <core/select/residue_selector/ResidueSelectorFactory.hh>
#include <core/pack/task/operation/TaskOperation.fwd.hh>
#include <core/pack/task/operation/TaskOperationFactory.hh>
#include <core/types.hh>
// Protocol headers
#include <protocols/moves/Mover.fwd.hh>
#include <protocols/moves/MoverFactory.hh>
#include <protocols/filters/Filter.hh>
#include <protocols/filters/FilterFactory.hh>
#include <protocols/filters/BasicFilters.hh>
#include <protocols/moves/NullMover.hh>
// Utility headers
#include <utility/tag/Tag.hh>
#include <utility/pointer/owning_ptr.hh>
#include <basic/datacache/DataMap.hh>
// C++ headers
#include <string>
#include <sstream>
//Bring out items to ease typing
using protocols::filters::Filter;
using protocols::filters::FilterOP;
using protocols::moves::Mover;
using protocols::moves::MoverOP;
using utility::tag::TagCOP;
using basic::datacache::DataMap;
using protocols::filters::TrueFilter;
using protocols::filters::FalseFilter;
using protocols::moves::NullMover;
/// @brief setup data map with *some* of the the RosettaScript defaults
inline void prime_Data( basic::datacache::DataMap & data ) {
using namespace core::scoring;
core::scoring::ScoreFunctionOP commandline_sfxn = get_score_function();
// hpark, May 2017:
// adding ref2015/talaris2013 together is NOT compatible. Let's keep default only
//core::scoring::ScoreFunctionOP talaris2013 = ScoreFunctionFactory::create_score_function(TALARIS_2013);
//core::scoring::ScoreFunctionOP talaris2014 = ScoreFunctionFactory::create_score_function(TALARIS_2014);
data.add( "scorefxns", "commandline", commandline_sfxn );
//data.add( "scorefxns", "talaris2013", talaris2013 );
//data.add( "scorefxns", "talaris2014", talaris2014 );
data.add( "movers", "null", utility::pointer::make_shared< protocols::moves::NullMover >() );
data.add( "filters", "true_filter", utility::pointer::make_shared< protocols::filters::TrueFilter >() );
data.add( "filters", "false_filter", utility::pointer::make_shared< protocols::filters::FalseFilter >() );
}
/// @brief Generate a tagptr from a string
/// For parse_my_tag tests, only do the relevant tag, not the full <ROSETTASCRIPTS> ... </ROSETTASCRIPTS> wrapped tag.
inline TagCOP tagptr_from_string(std::string input) {
std::stringstream instream( input );
return utility::tag::Tag::create( instream );
}
/// @brief Construct a mover from an XML string.
template <class MoverSubclass>
utility::pointer::shared_ptr<MoverSubclass> parse_tag(std::string tag_string) {
std::istringstream tag_stream(tag_string);
utility::tag::TagCOP tag = utility::tag::Tag::create(tag_stream);
basic::datacache::DataMap data;
prime_Data( data );
protocols::moves::MoverOP base_mover(
protocols::moves::MoverFactory::get_instance()->newMover( tag, data )
);
utility::pointer::shared_ptr<MoverSubclass> mover =
utility::pointer::dynamic_pointer_cast<MoverSubclass>(base_mover);
TSM_ASSERT("Instantiated the wrong type of mover", mover.get());
return mover;
}
/// @brief Construct a mover from an XML string.
template <class FilterSubclass>
utility::pointer::shared_ptr<FilterSubclass> parse_filter_tag(std::string tag_string) {
std::istringstream tag_stream(tag_string);
utility::tag::TagCOP tag = utility::tag::Tag::create(tag_stream);
basic::datacache::DataMap data;
prime_Data( data );
protocols::filters::FilterOP base_filter(
protocols::filters::FilterFactory::get_instance()->newFilter( tag, data )
);
utility::pointer::shared_ptr<FilterSubclass> filter =
utility::pointer::dynamic_pointer_cast<FilterSubclass>(base_filter);
TSM_ASSERT("Instantiated the wrong type of filter", filter.get());
return filter;
}
/// @brief Construct a task operation from an XML string.
template <class TaskOperationSubclass>
utility::pointer::shared_ptr<TaskOperationSubclass> parse_taskop_tag(std::string tag_string, basic::datacache::DataMap & data) {
std::istringstream tag_stream(tag_string);
utility::tag::TagCOP tag = utility::tag::Tag::create(tag_stream);
prime_Data(data);
core::pack::task::operation::TaskOperationOP base_taskop(
core::pack::task::operation::TaskOperationFactory::get_instance()->newTaskOperation(tag->getName(), data, tag) );
utility::pointer::shared_ptr<TaskOperationSubclass> taskop =
utility::pointer::dynamic_pointer_cast<TaskOperationSubclass>(base_taskop);
TSM_ASSERT("Instantiated the wrong type of task operation", taskop.get());
return taskop;
}
/// @brief Construct a task operation from an XML string.
template <class TaskOperationSubclass>
utility::pointer::shared_ptr<TaskOperationSubclass> parse_taskop_tag(std::string tag_string) {
basic::datacache::DataMap data;
prime_Data(data);
return parse_taskop_tag<TaskOperationSubclass>(tag_string, data);
}
/// @brief Construct a residue selector from an XML string.
template <class ResidueSelectorSubclass>
utility::pointer::shared_ptr<ResidueSelectorSubclass> parse_selector_tag(std::string tag_string, basic::datacache::DataMap & data) {
std::istringstream tag_stream(tag_string);
utility::tag::TagCOP tag = utility::tag::Tag::create(tag_stream);
prime_Data(data);
core::select::residue_selector::ResidueSelectorOP base_selector(
core::select::residue_selector::ResidueSelectorFactory::get_instance()->new_residue_selector(tag->getName(), tag, data) );
utility::pointer::shared_ptr<ResidueSelectorSubclass> selector =
utility::pointer::dynamic_pointer_cast<ResidueSelectorSubclass>(base_selector);
TSM_ASSERT("Instantiated the wrong type of residue selector", selector.get());
return selector;
}
/// @brief Construct a residue selector from an XML string.
template <class ResidueSelectorSubclass>
utility::pointer::shared_ptr<ResidueSelectorSubclass> parse_selector_tag(std::string tag_string) {
basic::datacache::DataMap data;
prime_Data(data);
return parse_selector_tag<ResidueSelectorSubclass>(tag_string, data);
}
/// @brief A simple filter for helping to test nested classes
/// will apply() with the given truth value,
/// report_sm() with the given value,
/// and report() with the truth.
/// The num_* functions will keep track of how often each are called.
class StubFilter : public Filter {
public:
StubFilter( bool truth = true, core::Real value = 0, std::string tag = "" ) :
truth_(truth),
value_(value),
tag_(tag)
{}
FilterOP clone() const { return utility::pointer::make_shared< StubFilter >( *this ); }
FilterOP fresh_instance() const { return utility::pointer::make_shared< StubFilter >(); }
void set( bool truth, core::Real value) { truth_ = truth; value_ = value; }
bool apply( core::pose::Pose const & ) const { ++num_apply; return truth_; }
core::Real report_sm( core::pose::Pose const & ) const { ++num_report_sm; return value_;}
void report( std::ostream & ostream, core::pose::Pose const & ) const {
++num_report;
ostream << "StubFilter " << tag_ << ": " << truth_ << " " << value_ << std::endl;
}
void reset_counts() {
num_apply = 0;
num_report_sm = 0;
num_report = 0;
}
public: // Yes, public - deliberately so people can easily change them, if they want to
bool truth_;
core::Real value_;
std::string tag_;
mutable core::Size num_apply = 0;
mutable core::Size num_report_sm = 0;
mutable core::Size num_report = 0;
};
typedef utility::pointer::shared_ptr< StubFilter > StubFilterOP;
typedef utility::pointer::shared_ptr< StubFilter const > StubFilterCOP;
/// @brief A simple filter for helping to test nested classes
/// will apply() with the given truth value,
/// When called, report_sm() will cycle through the given list of values
class StubMultiFilter : public Filter {
public:
StubMultiFilter( bool truth = true,std::string tag = "" ) :
truth_(truth),
pos_(1),
tag_(tag)
{}
FilterOP clone() const { return utility::pointer::make_shared< StubMultiFilter >( *this ); }
FilterOP fresh_instance() const { return utility::pointer::make_shared< StubMultiFilter >(); }
void set( utility::vector1<core::Real> const & values, bool truth=true, std::string tag="") {
values_ = values;
truth_ = truth;
tag_ = tag;
}
void push_back( core::Real value ) { values_.push_back( value); }
void set_pos( core::Size pos = 1) { pos_ = pos; }
bool apply( core::pose::Pose const & ) const { return truth_; }
core::Real get_next_value() const {
if ( pos_ == 0 || pos_ > values_.size() ) { pos_ = 1; }
++pos_; // Overflow taken care of next time around
return values_[ pos_ - 1 ];
}
core::Real report_sm( core::pose::Pose const & ) const {
return get_next_value();
}
void report( std::ostream & ostream, core::pose::Pose const & ) const {
ostream << "StubMultiFilter " << tag_ << ": " << truth_ << " ";
for ( core::Size ii(1); ii <= values_.size(); ++ii ) {
ostream << values_[ ii ] << " ";
}
ostream << std::endl;
}
public: // Yes, public - deliberately so people can easily change them, if they want to
bool truth_;
mutable core::Size pos_;
std::string tag_;
utility::vector1<core::Real> values_;
};
typedef utility::pointer::shared_ptr< StubMultiFilter > StubMultiFilterOP;
typedef utility::pointer::shared_ptr< StubMultiFilter const > StubMultiFilterCOP;
#endif