24 #include <basic/database/sql_utils.hh>
28 #include <basic/Tracer.hh>
29 #include <basic/datacache/CacheableString.fwd.hh>
30 #include <basic/options/keys/parser.OptionKeys.gen.hh>
31 #include <basic/options/option.hh>
53 #include <utility/vector0.hh>
54 #include <utility/vector1.hh>
55 #include <utility/tag/Tag.hh>
56 #include <utility/string_util.hh>
57 #include <basic/database/schema_generator/PrimaryKey.hh>
58 #include <basic/database/schema_generator/ForeignKey.hh>
59 #include <basic/database/schema_generator/Column.hh>
60 #include <basic/database/schema_generator/Schema.hh>
61 #include <basic/resource_manager/ResourceManager.hh>
62 #include <basic/resource_manager/util.hh>
68 #include <boost/foreach.hpp>
69 #include <boost/uuid/uuid.hpp>
70 #include <boost/uuid/uuid_io.hpp>
74 #include <utility/excn/Exceptions.hh>
100 #define foreach BOOST_FOREACH
105 using basic::Warning;
106 using basic::datacache::CacheableString;
107 using basic::database::safely_prepare_statement;
108 using basic::database::safely_write_to_database;
109 using basic::database::safely_read_from_database;
110 using basic::database::get_db_session;
111 using basic::database::set_cache_size;
124 using cppdb::cppdb_error;
125 using cppdb::statement;
138 using basic::database::parse_database_connection;
141 using std::accumulate;
142 using std::stringstream;
143 using boost::uuids::uuid;
148 using utility::sql_database::DatabaseSessionManager;
149 using utility::sql_database::session;
150 using utility::sql_database::sessionOP;
152 static Tracer
TR(
"protocols.features.ReportToDB");
157 sample_source_(
"Rosetta: Unknown Protocol"),
158 use_transactions_(true),
160 remove_xray_virt_(false),
165 features_reporters_(),
174 sample_source_(
"Rosetta: Unknown Protocol"),
175 use_transactions_(true),
177 remove_xray_virt_(false),
182 features_reporters_(),
190 sessionOP db_session,
191 string const & sample_source,
192 bool use_transactions,
195 db_session_(db_session),
196 sample_source_(sample_source),
197 use_transactions_(use_transactions),
198 cache_size_(cache_size),
199 remove_xray_virt_(false),
204 features_reporters_(),
212 db_session_(src.db_session_),
213 sample_source_(src.sample_source_),
215 use_transactions_(src.use_transactions_),
216 cache_size_(src.cache_size_),
217 remove_xray_virt_(src.remove_xray_virt_),
218 protocol_id_(src.protocol_id_),
219 batch_id_(src.batch_id_),
220 task_factory_(src.task_factory_),
222 protocol_features_(src.protocol_features_),
223 batch_features_(src.batch_features_),
224 structure_features_(src.structure_features_),
225 features_reporters_(src.features_reporters_),
226 initialized(src.initialized)
228 TR <<
"ReportToDB copy ctor called" << std::endl;
235 using basic::options::option;
236 using namespace basic::options::OptionKeys;
239 option.add_relevant( parser::protocol );
254 if( tag->hasOption(
"sample_source") ){
257 TR <<
"Field 'sample_source' required for use of ReportToDB in Rosetta Scripts." << endl;
258 TR <<
"The sample_source should describe where the samples came from. To access the description run \"sqlite3 'select description from sample_source' fname.db3\"" << endl;
259 TR <<
"For example: Top4400 natives from Richardson Lab. Reduce placed hydrogens with -correct flag." << endl;
265 if( tag->hasOption(
"name") ){
266 name_=tag->getOption<
string>(
"name");
268 TR <<
"Field 'name' required for use of ReportToDB in Rosetta Scripts." << endl;
276 if(tag->hasOption(
"protocol_id")){
279 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
296 if(tag->hasOption(
"use_transactions")){
304 if(tag->hasOption(
"cache_size")){
312 if(tag->hasOption(
"remove_xray_virt")){
327 if(tag->hasOption(
"db")){
328 throw utility::excn::EXCN_RosettaScriptsOption(
"The 'db' tag has been deprecated. Please use 'database_name' instead.");
331 if(tag->hasOption(
"db_mode")){
332 throw utility::excn::EXCN_RosettaScriptsOption(
"The 'database_mode' tag has been deprecated. Please use 'database_mode' instead.");
335 if(tag->hasOption(
"separate_db_per_mpi_process")){
336 throw utility::excn::EXCN_RosettaScriptsOption(
"The 'parse_separate_db_per_mpi_process' tag has been deprecated. Please use 'database_parse_separate_db_per_mpi_process' instead.");
342 if(tag->hasOption(
"resource_description")){
343 std::string resource_description = tag->getOption<
string>(
"resource_description");
344 if ( ! basic::resource_manager::ResourceManager::get_instance()->has_resource_with_description( resource_description ) )
346 throw utility::excn::EXCN_Msg_Exception
347 (
"You specified a resource_description of " + resource_description +
348 " for ReportToDB, but the ResourceManager doesn't have a resource with that description" );
350 db_session_ = basic::resource_manager::get_resource< utility::sql_database::session >( resource_description );
392 for(; begin !=
end; ++begin){
393 TagPtr feature_tag= *begin;
396 if(feature_tag->getName() !=
"feature"){
397 TR.Error <<
"Please include only tags with name 'feature' as subtags of ReportToDB" << endl;
398 TR.Error <<
"Tag with name '" << feature_tag->getName() <<
"' is invalid" << endl;
399 throw utility::excn::EXCN_RosettaScriptsOption(
"");
404 feature_tag, data, filters, movers, pose));
423 foreach(
string const dependency,
424 test_features_reporter->features_reporter_dependencies()){
427 if(dependency ==
"ProtocolFeatures" || dependency ==
"BatchFeatures" || dependency ==
"StructureFeatures"){
433 if(features_reporter->type_name() == dependency){
439 stringstream error_msg;
441 <<
"For batch '" <<
name_ <<
"'," << endl
442 <<
"the dependencies for the '" << test_features_reporter->type_name() <<
"'"
443 <<
" reporter are not satisfied because the '" << dependency <<
"' has not been defined yet." << endl
444 <<
"These are the FeaturesReporters that have been defined:" << endl
445 <<
"\tProtocolFeatures (included by default)" << endl
446 <<
"\tStructureFeatures (included by default)" << endl;
449 <<
"\t" << features_reporter->type_name() << endl;
451 utility_exit_with_message(error_msg.str());
493 TR <<
"Removing virtual residue left behind by xray refinement" << endl;
502 <<
"Reporting features for "
503 << accumulate(relevant_residues.begin(), relevant_residues.end(), 0)
505 <<
" total residues in the pose "
507 <<
" for batch '" <<
name_ <<
"'." << endl;
509 return relevant_residues;
517 using namespace basic::database::schema_generator;
519 Schema features_reporters(
520 "features_reporters",
521 PrimaryKey( Column(
"report_name",
new DbTextKey())));
526 string select_string =
529 " features_reporters\n"
532 statement select_stmt(safely_prepare_statement(select_string,
db_session_));
534 string insert_string =
"INSERT INTO features_reporters (report_name) VALUES (?);";
535 statement insert_stmt(safely_prepare_statement(insert_string,
db_session_));
538 string const report_name(reporter->type_name());
539 select_stmt.bind(1,report_name);
541 result res(safely_read_from_database(select_stmt));
543 insert_stmt.bind(1, report_name);
544 safely_write_to_database(insert_stmt);
551 using namespace basic::database::schema_generator;
553 Schema batch_reports(
"batch_reports");
554 Column report_name(
"report_name",
new DbTextKey());
555 Column batch_id(
"batch_id",
new DbInteger());
557 batch_reports.add_foreign_key(
558 ForeignKey(batch_id,
"batches",
"batch_id",
true ));
559 batch_reports.add_foreign_key(
560 ForeignKey(report_name,
"features_reporters",
"report_name",
true ));
563 batch_reports_unique.push_back(batch_id);
564 batch_reports_unique.push_back(report_name);
565 batch_reports.add_constraint(
new UniqueConstraint(batch_reports_unique) );
575 }
catch(cppdb_error error){
576 stringstream err_msg;
578 <<
"The ReportToDB Mover failed to write the 'features_reporters' table "
579 <<
"to the database for batch '" <<
name_ <<
"'." << endl
580 <<
"Error Message:" << endl << error.what() << endl;
581 utility_exit_with_message(err_msg.str());
586 }
catch(cppdb_error error){
587 stringstream err_msg;
589 <<
"The ReportToDB Mover failed to write the 'batch_reports' table "
590 <<
"to the database." << endl
591 <<
"Error Message:" << endl << error.what() << endl;
592 utility_exit_with_message(err_msg.str());
626 }
catch (cppdb_error error){
627 stringstream err_msg;
629 <<
"Failed to report structure features for:" << endl
631 <<
"\tbatch name: '" <<
name_ <<
"'" << endl
632 <<
"\tbatch_id: '" <<
batch_id_ <<
"'" << endl
633 <<
"Error Message:" << endl << error.what() << endl;
634 utility_exit_with_message(err_msg.str());
642 uuid
const struct_id,
654 TR <<
"Reporting " << report_name << std::endl;
659 }
catch (cppdb_error error){
660 stringstream err_msg;
662 <<
"Failed to report features for the "
663 <<
"'" << report_name <<
"' reporter with:" << endl
666 <<
"\tbatch name: '" <<
name_ <<
"' " << endl
667 <<
"\tbatch_id: '" <<
batch_id_ <<
"'" << endl
668 <<
"\tstruct_id: '" << struct_id <<
"'" << endl
669 <<
"Error Message:" << endl << error.what() << endl;
670 utility_exit_with_message(err_msg.str());