Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScoreFunctionFeatures.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/features/ScoreFunctionFeatures.cc
11 /// @brief report the score parameters to a features database
12 /// @author Matthew O'Meara
13 
14 // Unit Headers
17 
18 //External
19 #include <boost/uuid/uuid.hpp>
20 
21 //Basic Headers
22 #include <basic/database/sql_utils.hh>
23 #include <basic/database/schema_generator/PrimaryKey.hh>
24 #include <basic/database/schema_generator/ForeignKey.hh>
25 #include <basic/database/schema_generator/Column.hh>
26 #include <basic/database/schema_generator/Schema.hh>
27 #include <basic/database/schema_generator/Constraint.hh>
28 
29 // Platform Headers
30 #include <basic/options/option.hh>
31 #include <basic/options/keys/inout.OptionKeys.gen.hh>
32 #include <core/graph/Graph.fwd.hh>
44 #include <utility/sql_database/DatabaseSessionManager.hh>
45 #include <utility/vector1.hh>
46 #include <basic/database/sql_utils.hh>
47 #include <utility/tag/Tag.hh>
48 
49 // External Headers
50 #include <cppdb/frontend.h>
51 
52 // C++ Headers
53 #include <sstream>
54 #include <utility/excn/Exceptions.hh>
55 #include <string>
56 
57 
58 namespace protocols{
59 namespace features{
60 
61 using std::string;
62 using std::stringstream;
63 using std::endl;
64 using basic::database::safely_prepare_statement;
65 using core::pose::Pose;
66 using core::graph::Graph;
84 using core::Size;
85 using core::Real;
86 using utility::vector1;
87 using utility::sql_database::sessionOP;
88 using cppdb::statement;
89 using cppdb::result;
90 
92  scfxn_(getScoreFunction()),
93  scfxn_name_(getScoreFunctionName())
94 {}
95 
97  ScoreFunctionOP scfxn,
98  std::string const & scfxn_name
99 ) :
100  scfxn_(scfxn),
101  scfxn_name_(scfxn_name)
102 {
103  if ( scfxn_ == 0 ) {
104  utility_exit_with_message( "ScoreFunctionFeatures may not be constructed with a null-pointer ScoreFunctionOP" );
105  }
106 }
107 
109  ScoreFunctionFeatures const & src
110 ) :
112  scfxn_(src.scfxn_),
113  scfxn_name_(src.scfxn_name_)
114 {}
115 
117 
118 string
119 ScoreFunctionFeatures::type_name() const { return "ScoreFunctionFeatures"; }
120 
121 void
123  sessionOP db_session
124 ) const {
127  db_session);
128 }
129 
130 void
132  sessionOP db_session
133 ) const {
134  using namespace basic::database::schema_generator;
135 
136  Column batch_id("batch_id", new DbInteger(), true);
137  Column score_function_name("score_function_name", new DbText(255), true);
138  Column score_type_id("score_type_id", new DbInteger(), true);
139  Column weight("weight", new DbReal(), true);
140 
141  utility::vector1<Column> pkey_cols;
142  pkey_cols.push_back(batch_id);
143  pkey_cols.push_back(score_function_name);
144  pkey_cols.push_back(score_type_id);
145 
146  Columns foreign_key_columns;
147  foreign_key_columns.push_back(batch_id);
148  foreign_key_columns.push_back(score_type_id);
149  vector1< string > reference_columns;
150  reference_columns.push_back("batch_id");
151  reference_columns.push_back("score_type_id");
152  ForeignKey foreign_key(foreign_key_columns, "score_types", reference_columns, true);
153 
154  Schema table("score_function_weights", PrimaryKey(pkey_cols));
155  table.add_foreign_key(foreign_key);
156  table.add_column(weight);
157 
158  table.write(db_session);
159 
160 }
161 
164  vector1<string> dependencies;
165  dependencies.push_back("ScoreTypeFeatures");
166  return dependencies;
167 }
168 
169 
170 void
172  utility::tag::TagPtr const tag,
174  protocols::filters::Filters_map const & /*filters*/,
175  protocols::moves::Movers_map const & /*movers*/,
176  Pose const & /*pose*/
177 ) {
178  if(tag->hasOption("scorefxn")){
179  scfxn_name_ = tag->getOption<string>("scorefxn");
180  scfxn_ = data.get<ScoreFunction*>("scorefxns", scfxn_name_);
181  } else {
182  stringstream error_msg;
183  error_msg
184  << "The " << type_name() << " reporter requires a 'scorefxn' tag:" << endl
185  << endl
186  << " <feature name=" << type_name() <<" scorefxn=(name_of_score_function) />" << endl;
187  throw utility::excn::EXCN_RosettaScriptsOption(error_msg.str());
188  }
189 }
190 
191 Size
193  Pose const &,
194  vector1< bool > const &,
195  boost::uuids::uuid const struct_id,
196  sessionOP db_session
197 ){
198  Size const batch_id(get_batch_id(struct_id, db_session));
199 
200  insert_score_function_weights_rows(batch_id, db_session);
201  scfxn_->energy_method_options().insert_score_function_method_options_rows(
202  batch_id, scfxn_name_, db_session);
203  return 0;
204 
205 }
206 
207 void
209  boost::uuids::uuid,
210  utility::sql_database::sessionOP ){}
211 
212 void
214  core::Size batch_id,
215  sessionOP db_session
216 ) const {
217 
218  string statement_string;
219  switch(db_session->get_db_mode()){
220  case utility::sql_database::DatabaseMode::sqlite3:
221  statement_string = "INSERT OR IGNORE INTO score_function_weights (batch_id, score_function_name, score_type_id, weight) VALUES (?,?,?,?);";
222  break;
223  case utility::sql_database::DatabaseMode::mysql:
224  case utility::sql_database::DatabaseMode::postgres:
225  statement_string = "INSERT IGNORE INTO score_function_weights (batch_id, score_function_name, score_type_id, weight) VALUES (?,?,?,?);";
226  break;
227  default:
228  utility_exit_with_message(
229  "Unrecognized database mode: '" +
230  name_from_database_mode(db_session->get_db_mode()) + "'");
231  }
232 
233  statement stmt(safely_prepare_statement(statement_string, db_session));
234 
235  for(Size score_type_id=1; score_type_id <= n_score_types; ++score_type_id){
236  ScoreType type(static_cast<ScoreType>(score_type_id));
237 
238  Real const weight( scfxn_->weights()[type] );
239  if(!weight) continue;
240 
241  string const score_type( ScoreTypeManager::name_from_score_type(type) );
242  stmt.bind(1, batch_id);
243  stmt.bind(2, scfxn_name_);
244  stmt.bind(3, score_type_id);
245  stmt.bind(4, weight);
246  basic::database::safely_write_to_database(stmt);
247 
248  }
249 }
250 
251 } // namesapce
252 } // namespace