Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StructureScoresFeatures.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/StructureScoresFeatures.cc
11 /// @brief report structure score features to a features database
12 /// @author Matthew O'Meara
13 
14 // Unit Headers
17 
18 //External
19 #include <cppdb/frontend.h>
20 #include <boost/uuid/uuid.hpp>
21 #include <boost/uuid/uuid_io.hpp>
22 
23 //Basic Headers
24 #include <basic/database/sql_utils.hh>
25 #include <basic/database/schema_generator/PrimaryKey.hh>
26 #include <basic/database/schema_generator/ForeignKey.hh>
27 #include <basic/database/schema_generator/Column.hh>
28 #include <basic/database/schema_generator/Schema.hh>
29 #include <basic/database/schema_generator/Constraint.hh>
30 
31 // Platform Headers
32 #include <basic/options/option.hh>
33 #include <basic/options/keys/inout.OptionKeys.gen.hh>
35 #include <core/pose/Pose.hh>
37 #include <core/scoring/Energies.hh>
44 #include <utility/sql_database/DatabaseSessionManager.hh>
45 #include <utility/vector1.hh>
46 #include <utility/tag/Tag.hh>
47 #include <basic/database/sql_utils.hh>
48 #include <utility/tools/make_vector.hh>
49 
50 #include <basic/database/insert_statement_generator/InsertGenerator.hh>
51 #include <basic/database/insert_statement_generator/RowData.hh>
52 
53 // C++ Headers
54 #include <sstream>
55 #include <string>
56 
58 #include <utility/excn/Exceptions.hh>
59 #include <utility/vector0.hh>
60 
61 
62 namespace protocols{
63 namespace features{
64 
65 using std::string;
66 using std::stringstream;
67 using std::endl;
68 using core::pose::Pose;
69 using core::graph::Graph;
86 using core::Size;
87 using core::Real;
91 using utility::vector1;
92 using utility::sql_database::sessionOP;
94 using cppdb::statement;
95 using cppdb::result;
96 using basic::database::insert_statement_generator::InsertGenerator;
97 using basic::database::insert_statement_generator::RowDataBaseOP;
98 using basic::database::insert_statement_generator::RowData;
99 
101  scfxn_(getScoreFunction())
102 {}
103 
105  ScoreFunctionOP scfxn
106 ) :
107  scfxn_(scfxn)
108 {
109  if ( scfxn_ == 0 ) {
110  utility_exit_with_message( "StructureScoresFeatures may not be constructed with a null-pointer ScoreFunctionOP" );
111  }
112 }
113 
115  StructureScoresFeatures const & src
116 ) :
118  scfxn_(src.scfxn_)
119 {}
120 
122 
123 string
124 StructureScoresFeatures::type_name() const { return "StructureScoresFeatures"; }
125 
126 void
128  utility::sql_database::sessionOP db_session
129 ) const {
130  using namespace basic::database::schema_generator;
131 
132  //******structure_scores******//
133  Column batch_id("batch_id", new DbInteger(), false);
134  Column struct_id("struct_id", new DbUUID(), false);
135  Column score_type_id("score_type_id", new DbInteger(), false);
136  Column score_value("score_value", new DbInteger(), false);
137 
138  utility::vector1<Column> pkey_cols;
139  pkey_cols.push_back(batch_id);
140  pkey_cols.push_back(struct_id);
141  pkey_cols.push_back(score_type_id);
142 
143  Columns foreign_key_columns1;
144  foreign_key_columns1.push_back(struct_id);
145  vector1< std::string > reference_columns1;
146  reference_columns1.push_back("struct_id");
147  ForeignKey foreign_key1(foreign_key_columns1, "structures", reference_columns1, true);
148 
149 
150  Columns foreign_key_columns2;
151  foreign_key_columns2.push_back(batch_id);
152  foreign_key_columns2.push_back(score_type_id);
153  vector1< std::string > reference_columns2;
154  reference_columns2.push_back("batch_id");
155  reference_columns2.push_back("score_type_id");
156  ForeignKey foreign_key2(foreign_key_columns2, "score_types", reference_columns2, true);
157 
158  Schema structure_scores("structure_scores", PrimaryKey(pkey_cols));
159  structure_scores.add_column(score_value);
160 
161  structure_scores.add_foreign_key(foreign_key1);
162  structure_scores.add_foreign_key(foreign_key2);
163 
164  structure_scores.write(db_session);
165 
166 }
167 
170  utility::vector1<std::string> dependencies;
171  dependencies.push_back("StructureFeatures");
172  dependencies.push_back("ScoreTypeFeatures");
173  return dependencies;
174 }
175 
176 void
178  TagPtr const tag,
179  DataMap & data,
180  Filters_map const & /*filters*/,
181  Movers_map const & /*movers*/,
182  Pose const & /*pose*/
183 ) {
184  if(tag->hasOption("scorefxn")){
185  string scorefxn_name = tag->getOption<string>("scorefxn");
186  scfxn_ = data.get<ScoreFunction*>("scorefxns", scorefxn_name);
187  } else {
188  stringstream error_msg;
189  error_msg
190  << "The " << type_name() << " reporter requires a 'scorefxn' tag:" << endl
191  << endl
192  << " <feature name=" << type_name() <<" scorefxn=(name_of_score_function) />" << endl;
193  throw utility::excn::EXCN_RosettaScriptsOption(error_msg.str());
194  }
195 }
196 
197 
198 Size
200  Pose const & pose,
201  vector1< bool > const & relevant_residues,
202  boost::uuids::uuid struct_id,
203  sessionOP db_session
204 ){
205  insert_structure_score_rows(pose, relevant_residues, struct_id, db_session);
206  return 0;
207 }
208 
210  boost::uuids::uuid struct_id,
211  utility::sql_database::sessionOP db_session
212 ){
213 
214  std::string statement_string = "DELETE FROM structure_scores WHERE struct_id = ?;\n";
215  statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
216  stmt.bind(1,struct_id);
217  basic::database::safely_write_to_database(stmt);
218 
219 }
220 
221 void
223  Pose const & pose_in,
224  vector1<bool> const & relevant_residues,
225  EnergyMap & emap
226 ) const {
227  // We only get const access to the pose, so make a copy of it so it
228  // can be scored. TODO: This is a bit wasteful, as we don't actually
229  // need a copy of the coodinates part of the pose only the
230  // energies part of the pose.
231  Pose pose = pose_in;
232  (*scfxn_)(pose);
233 
234  vector1<bool> relevant_and_virtual_residues(relevant_residues);
235 
236  // if relevant residues includes all of the residues, then include
237  // the whole structure scores, otherwise just the one-body and
238  // two-body scores.
239  bool all_residues(true);
240 
241  // Since some scores terms, such as elec_dens_fast and constraints,
242  // use virtual residues to be compatible with the two-body scoring
243  // framework, include virtual residues with the relevant residues so
244  // these scores get computed.
245  for(Size i = 1; i <= pose.total_residue(); ++i){
246  if(pose.residue( i ).aa() == aa_vrt){
247  relevant_and_virtual_residues[i] = true;
248  } else if (relevant_and_virtual_residues[i] == false){
249  all_residues = false;
250  }
251  }
252  if(all_residues){
253  emap = pose.energies().total_energies();
254  } else {
255  scfxn_->get_sub_score(pose, relevant_and_virtual_residues, emap);
256  }
257 }
258 
259 
260 void
262  Pose const & pose_in,
263  vector1< bool > const & relevant_residues,
264  boost::uuids::uuid struct_id,
265  sessionOP db_session
266 ) const {
267 
268  InsertGenerator structure_scores_insert("structure_scores");
269  structure_scores_insert.add_column("batch_id");
270  structure_scores_insert.add_column("struct_id");
271  structure_scores_insert.add_column("score_type_id");
272  structure_scores_insert.add_column("score_value");
273 
274  EnergyMap emap;
275  compute_energies(pose_in, relevant_residues, emap);
276 
277  core::Real total_score= 0.0;
278  Size const batch_id(get_batch_id(struct_id, db_session));
279  RowDataBaseOP struct_id_data = new RowData<boost::uuids::uuid>("struct_id",struct_id);
280  RowDataBaseOP batch_id_data = new RowData<Size>("batch_id",batch_id);
281 
282  for(Size score_type_id=1; score_type_id <= n_score_types; ++score_type_id){
283  ScoreType type(static_cast<ScoreType>(score_type_id));
284  Real const score_value( (*scfxn_)[type] * emap[type] );
285  if(!score_value) continue;
286  total_score += score_value;
287 
288  RowDataBaseOP score_type_id_data = new RowData<Size>("score_type_id",score_type_id);
289  RowDataBaseOP score_value_data = new RowData<Real>("score_value",score_value);
290 
291  structure_scores_insert.add_row(
292  utility::tools::make_vector(batch_id_data,struct_id_data,score_type_id_data,score_value_data));
293 
294  }
295 
296  // add the total_score type
297  RowDataBaseOP total_id_data = new RowData<Size>("score_type_id",n_score_types);
298  RowDataBaseOP total_score_data = new RowData<Real>("score_value",total_score);
299 
300  structure_scores_insert.add_row(
301  utility::tools::make_vector(batch_id_data,struct_id_data,total_id_data,total_score_data));
302  structure_scores_insert.write_to_database(db_session);
303  }
304 
305 } // namesapce
306 } // namespace