Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DatabaseStatements.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 src/protocols/features/ProteinSilentReport_util.cc
11 /// @author Sam DeLuca
12 
14 
15 #include <utility/sql_database/DatabaseSessionManager.hh>
16 #include <utility/excn/Exceptions.hh>
17 #include <basic/database/sql_utils.hh>
18 
19 // External Headers
20 #include <cppdb/frontend.h>
21 #include <utility/exit.hh>
22 #include <utility/string_util.hh>
23 #include <boost/uuid/uuid.hpp>
24 #include <boost/uuid/uuid_io.hpp>
25 
26 
27 #include <utility/vector1.hh>
28 
29 
30 
31 namespace protocols {
32 namespace features {
33 
34 ////// Functions only available to this CC file //////
35 
36 template <class T>
38  cppdb::statement statement,
39  T // dummy variable. I hate dummy variables, but that's the best I can do for now
40 ){
41  cppdb::result result(basic::database::safely_read_from_database(statement));
42  T value;
43  if(result.next())
44  {
45  result >> value;
46  return value;
47  }else{
48  throw utility::excn::EXCN_Msg_Exception("No result found");
49  }
50 }
51 
53  utility::sql_database::sessionOP db_session,
54  std::string const & input_tag,
55  core::Size const & protocol_id
56 ){
57  if( input_tag.empty()){
58  std::string statement_string =
59  "SELECT\n"
60  " count(*)\n"
61  "FROM\n"
62  " structures"
63  "INNER JOIN\n"
64  " batches ON structures.batch_id = batches.batch_id\n"
65  "WHERE\n"
66  " batches.protocol_id = ?;";
67  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
68  statement.bind(1,protocol_id);
69  return statement;
70  }else{
71  std::string statement_string =
72  "SELECT\n"
73  " count(*)\n"
74  "FROM\n"
75  " structures\n"
76  "INNER JOIN\n"
77  " batches ON structures.batch_id = batches.batch_id\n"
78  "WHERE\n"
79  " structures.input_tag=?\n"
80  "AND\n"
81  " batches.protocol_id=?;";
82  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
83  statement.bind(1,input_tag);
84  statement.bind(2,protocol_id);
85  return statement;
86  }
87 }
88 
90  utility::sql_database::sessionOP db_session,
91  core::Size const & protocol_id,
92  std::string const & score_term
93 )
94 {
95 
96  std::string statement_string =
97  "SELECT\n"
98  "score_type_id\n"
99  "FROM\n"
100  "score_types\n"
101  "INNER JOIN\n"
102  "batches ON score_types.batch_id = batches.batch_id\n"
103  "WHERE\n"
104  "batches.protocol_id=?\n"
105  "AND\n"
106  "score_type_name=?\n";
107 
108  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
109  statement.bind(1,protocol_id);
110  statement.bind(2,score_term);
111  try{
112  return get_something_from_database(statement, core::Size());
113  }catch(utility::excn::EXCN_Msg_Exception &){
114  throw;
115  //utility_exit_with_message("No score_term "+score_term+" with protocol_id "+utility::to_string(protocol_id));
116  }
117 }
118 
120  utility::sql_database::sessionOP db_session,
121  std::string const & score_term,
122  core::Size const & cutoff_index,
123  std::string const & input_tag,
124  core::Size const & protocol_id
125 ){
126  if(input_tag.empty()){
127  std::string statement_string =
128  "SELECT\n"
129  " job_string_real_data.struct_id\n"
130  "FROM\n"
131  " job_string_real_data\n"
132  "INNER JOIN\n"
133  " structures ON job_string_real_data.struct_id = structures.struct_id\n"
134  "INNER JOIN\n"
135  " batches ON structures.batch_id = batches.batch_id\n"
136  "WHERE\n"
137  " job_string_real_data.data_key = ? AND batches.protocol_id = ?\n"
138  "ORDER BY\n"
139  " job_string_real_data.data_value\n"
140  "LIMIT ?,1;";
141  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
142  statement.bind(1,score_term);
143  statement.bind(2,protocol_id);
144  statement.bind(3,cutoff_index-1);
145  return statement;
146  }
147  else{
148 
149  std::string statement_string =
150  "SELECT\n"
151  " job_string_real_data.struct_id\n"
152  "FROM\n"
153  " job_string_real_data\n"
154  "INNER JOIN\n"
155  " structures ON job_string_real_data.struct_id = structures.struct_id\n"
156  "INNER JOIN\n"
157  " batches ON structures.batch_id = batches.batch_id\n"
158  "WHERE\n"
159  " job_string_real_data.data_key = ?\n"
160  "AND\n"
161  " structures.input_tag = ?\n"
162  "AND\n"
163  " batches.protocol_id = ?\n"
164  "ORDER BY\n"
165  " job_string_real_data.data_value\n"
166  "LIMIT ?,1;";
167  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
168  statement.bind(1,score_term);
169  statement.bind(2,input_tag);
170  statement.bind(3,protocol_id);
171  statement.bind(4,cutoff_index-1);
172  return statement;
173  }
174 }
175 
177  utility::sql_database::sessionOP db_session,
178  core::Size const & score_type_id,
179  core::Size const & cutoff_index,
180  std::string const & input_tag,
181  core::Size const & protocol_id
182 ){
183  if(input_tag.empty()){
184  std::string statement_string =
185  "SELECT\n"
186  " structure_scores.struct_id\n"
187  "FROM\n"
188  " structure_scores\n"
189  "INNER JOIN\n"
190  " structures ON structure_scores.struct_id = structures.struct_id\n"
191  "INNER JOIN\n"
192  " batches ON structures.batch_id = batches.batch_id\n"
193  "WHERE\n"
194  " structure_scores.score_type_id = ?\n"
195  "AND\n"
196  " batches.protocol_id = ?\n"
197  "ORDER BY\n"
198  " structure_scores.score_value\n"
199  "LIMIT ?,1;";
200  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
201  statement.bind(1,score_type_id);
202  statement.bind(2,protocol_id);
203  statement.bind(3,cutoff_index-1);
204  return statement;
205  }
206  else{
207  std::string statement_string =
208  "SELECT\n"
209  " structure_scores.struct_id\n"
210  "FROM\n"
211  " structure_scores\n"
212  "INNER JOIN\n"
213  " structures ON structure_scores.struct_id = structures.struct_id\n"
214  "INNER JOIN\n"
215  " batches ON structures.batch_id = batches.batch_id\n"
216  "WHERE\n"
217  " structure_scores.score_type_id = ?\n"
218  "AND\n"
219  " structures.input_tag = ?\n"
220  "AND\n"
221  " batches.protocol_id =?\n"
222  "ORDER BY\n"
223  " structure_scores.score_value\n"
224  "LIMIT ?,1;";
225  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
226  statement.bind(1,score_type_id);
227  statement.bind(2,input_tag);
228  statement.bind(3,protocol_id);
229  statement.bind(4,cutoff_index-1);
230  return statement;
231  }
232 }
233 
235  utility::sql_database::sessionOP db_session,
236  std::string const & score_term,
237  std::string const & input_tag,
238  core::Size const & protocol_id
239 ){
240  if(input_tag.empty()){
241  std::string statement_string =
242  "SELECT\n"
243  " job_string_real_data.struct_id\n"
244  "FROM\n"
245  " job_string_real_data\n"
246  "INNER JOIN\n"
247  " structures ON job_string_real_data.struct_id = structures.struct_id\n"
248  "INNER JOIN\n"
249  " batches ON structures.batch_id = batches.batch_id\n"
250  "WHERE\n"
251  " job_string_real_data.data_key = ?\n"
252  "AND\n"
253  " batches.protocol_id = ?\n"
254  "ORDER BY\n"
255  " job_string_real_data.data_value DESC\n"
256  "LIMIT 1;";
257  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
258  statement.bind(1,score_term);
259  statement.bind(2,protocol_id);
260  return statement;
261  }
262  else{
263  std::string statement_string =
264  "SELECT\n"
265  " job_string_real_data.struct_id\n"
266  "FROM\n"
267  " job_string_real_data\n"
268  "INNER JOIN\n"
269  " structures ON job_string_real_data.struct_id = structures.struct_id\n"
270  "INNER JOIN\n"
271  " batches ON structures.batch_id = batches.batch_id\n"
272  "WHERE\n"
273  " job_string_real_data.data_key = ?\n"
274  "AND\n"
275  " structures.input_tag = ?\n"
276  "AND\n"
277  " batches.protocol_id = ?\n"
278  "ORDER BY\n"
279  " job_string_real_data.data_value DESC\n"
280  "LIMIT 1;";
281  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
282  statement.bind(1,score_term);
283  statement.bind(2,input_tag);
284  statement.bind(3,protocol_id);
285  return statement;
286  }
287 }
288 
290  utility::sql_database::sessionOP db_session,
291  core::Size const & score_type_id,
292  std::string const & input_tag,
293  core::Size const & protocol_id
294 ){
295  if(input_tag.empty()){
296  std::string statement_string =
297  "SELECT\n"
298  " structure_scores.struct_id\n"
299  "FROM\n"
300  " structure_scores\n"
301  "INNER JOIN\n"
302  " structures ON structure_scores.struct_id = structures.struct_id\n"
303  "INNER JOIN\n"
304  " batches ON structures.batch_id = batches.batch_id\n"
305  "WHERE\n"
306  " structure_scores.score_type_id = ?\n"
307  "AND\n"
308  " batches.protocol_id = ?\n"
309  "ORDER BY\n"
310  " structure_scores.score_value DESC\n"
311  "LIMIT 1;";
312  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
313  statement.bind(1,score_type_id);
314  statement.bind(2,protocol_id);
315  return statement;
316  }
317  else{
318  std::string statement_string =
319  "SELECT\n"
320  " structure_scores.struct_id\n"
321  "FROM\n"
322  " structure_scores\n"
323  "INNER JOIN\n"
324  " structures ON structure_scores.struct_id = structures.struct_id\n"
325  "INNER JOIN\n"
326  " batches ON structures.batch_id = batches.batch_id\n"
327  "WHERE\n"
328  " structure_scores.score_type_id = ?\n"
329  "AND\n"
330  " structures.input_tag = ?\n"
331  "AND\n"
332  " batches.protocol_id = ?\n"
333  "ORDER BY\n"
334  " structure_scores.score_value DESC\n"
335  "LIMIT 1;";
336  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
337  statement.bind(1,score_type_id);
338  statement.bind(2,input_tag);
339  statement.bind(3,protocol_id);
340  return statement;
341  }
342 }
343 
344 ////// Functions declared in the .hh file //////
345 
347  utility::sql_database::sessionOP db_session,
348  core::Size const & protocol_id,
349  std::string const & input_tag
350 ){
351  cppdb::statement statement = get_structure_count_statement(db_session, input_tag,protocol_id);
352  return get_something_from_database(statement, core::Size());
353 }
354 
355 boost::uuids::uuid
357  utility::sql_database::sessionOP db_session,
358  std::string const & score_term,
359  core::Size const & cutoff_index,
360  core::Size const & protocol_id,
361  std::string const & input_tag
362 ){
363  cppdb::statement statement = get_nth_lowest_score_from_job_data_statement(db_session, score_term, cutoff_index, input_tag,protocol_id);
364 
365  try{
366  return get_something_from_database(statement, boost::uuids::uuid());
367  }catch(utility::excn::EXCN_Msg_Exception){
368  utility_exit_with_message("No nth lowest "+score_term+" with input_tag "+input_tag+ ", where n="+utility::to_string(cutoff_index));
369  }
370 
371  // shouldn't get here
372  return boost::uuids::uuid();
373 }
374 
375 boost::uuids::uuid
377  utility::sql_database::sessionOP db_session,
378  core::Size const & score_type_id,
379  core::Size const & cutoff_index,
380  core::Size const & protocol_id,
381  std::string const & input_tag
382 ){
383  cppdb::statement statement = get_nth_lowest_score_from_score_data_statement(db_session, score_type_id, cutoff_index, input_tag,protocol_id);
384 
385  try{
386  return get_something_from_database(statement, boost::uuids::uuid());
387  }catch(utility::excn::EXCN_Msg_Exception){
388  utility_exit_with_message("No nth lowest score_type_id: "+utility::to_string(score_type_id)+", with input_tag "+input_tag+ ", where n="+utility::to_string(cutoff_index));
389  }
390 
391  // shouldn't get here
392  return boost::uuids::uuid();
393 }
394 
396  utility::sql_database::sessionOP db_session,
397  std::string const & score_term,
398  core::Size const & protocol_id,
399  std::string const & input_tag
400 ){
401  return get_struct_id_with_nth_lowest_score_from_job_data(db_session, score_term, 1, protocol_id, input_tag);
402 }
403 
405  utility::sql_database::sessionOP db_session,
406  core::Size const & score_type_id,
407  core::Size const & protocol_id,
408  std::string const & input_tag
409 ){
410  return get_struct_id_with_nth_lowest_score_from_score_data(db_session, score_type_id, 1, protocol_id, input_tag);
411 }
412 
414  utility::sql_database::sessionOP db_session,
415  std::string const & score_term,
416  core::Size const & protocol_id,
417  std::string const & input_tag )
418 {
419  cppdb::statement statement = get_highest_score_from_job_data_statement(db_session, score_term, input_tag, protocol_id);
420 
421  try{
422  return get_something_from_database(statement, boost::uuids::uuid());
423  }catch(utility::excn::EXCN_Msg_Exception){
424  utility_exit_with_message("No "+score_term+" with input_tag "+input_tag);
425  }
426 
427  // shouldn't get here
428  return boost::uuids::uuid();
429 }
430 
432  utility::sql_database::sessionOP db_session,
433  core::Size const & score_type_id,
434  core::Size const & protocol_id,
435  std::string const & input_tag
436 ){
437  cppdb::statement statement = get_highest_score_from_score_data_statement(db_session, score_type_id, input_tag,protocol_id);
438 
439  try{
440  return get_something_from_database(statement, boost::uuids::uuid());
441  }catch(utility::excn::EXCN_Msg_Exception){
442  utility_exit_with_message("No score_type_id: "+utility::to_string(score_type_id)+", with input_tag "+input_tag);
443  }
444 
445  // shouldn't get here
446  return boost::uuids::uuid();
447 }
448 
451  utility::sql_database::sessionOP db_session,
452  boost::uuids::uuid const & struct_id,
453  std::string const & score_term
454 ){
455 
456  std::string statement_string =
457  "SELECT\n"
458  " job_string_real_data.data_value\n"
459  "FROM\n"
460  " job_string_real_data\n"
461  "WHERE\n"
462  " job_string_real_data.data_key = ?\n"
463  "AND\n"
464  " job_string_real_data.struct_id = ?\n;";
465  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
466  statement.bind(1,score_term);
467  statement.bind(2,struct_id);
468 
469  try{
470  return get_something_from_database(statement, core::Real());
471  }catch(utility::excn::EXCN_Msg_Exception){
472  utility_exit_with_message("No "+score_term+" with struct_id "+to_string(struct_id));
473  }
474 
475  // shouldn't get here
476  return 0;
477 }
478 
481  utility::sql_database::sessionOP db_session,
482  boost::uuids::uuid const & struct_id,
483  core::Size const & score_type_id)
484 {
485 
486  std::string statement_string =
487  "SELECT\n"
488  " structure_scores.score_value\n"
489  "FROM\n"
490  " structure_scores\n"
491  "WHERE\n"
492  " structure_scores.struct_id = ?\n"
493  "AND\n"
494  " structure_scores.score_type_id = ?;";
495  cppdb::statement statement(basic::database::safely_prepare_statement(statement_string,db_session));
496  statement.bind(1,struct_id);
497  statement.bind(2,score_type_id);
498 
499  try{
500  return get_something_from_database(statement, core::Real());
501  }catch(utility::excn::EXCN_Msg_Exception){
502  utility_exit_with_message("No score_type_id:"+utility::to_string(score_type_id)+", with struct_id "+to_string(struct_id));
503  }
504 
505  // shouldn't get here
506  return 0;
507 }
508 
509 }
510 }