Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DatabaseJobOutputter.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 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file protocols/features/DatabaseJobOutputter.cc
12 /// @brief Job Outputter to a database
13 /// @author Matthew O'Meara (mattjomeara@gmail.com)
14 
17 #include <protocols/jd2/Job.hh>
18 
19 // Project Headers
21 
22 // Platform Headers
23 #include <core/pose/util.hh>
24 
25 ///Utility headers
26 #include <basic/options/option.hh>
27 #include <basic/options/keys/inout.OptionKeys.gen.hh>
28 #include <basic/options/keys/out.OptionKeys.gen.hh>
29 #include <basic/Tracer.hh>
30 #include <basic/database/sql_utils.hh>
31 #include <utility/exit.hh>
32 #include <utility/vector1.hh>
33 #include <utility/sql_database/DatabaseSessionManager.hh>
34 
35 // External Headers
36 #include <cppdb/frontend.h>
37 
38 // C++ Headers
39 #include <string>
40 
41 //Auto Headers
43 static basic::Tracer tr("protocols.features.DatabaseJobOutputter");
44 
45 namespace protocols {
46 namespace features {
47 
48 using std::string;
49 using core::Size;
50 using core::pose::Pose;
57 using utility::sql_database::DatabaseSessionManager;
58 using utility::sql_database::sessionOP;
59 using basic::database::get_db_session;
60 using cppdb::result;
61 
62 
64  protocols::jd2::FileJobOutputter(),
65  protein_silent_report_(new ProteinSilentReport()),
66  database_name_(),
67  database_pq_schema_()
68 {
70  sessionOP db_session(
71  get_db_session(database_name_, database_pq_schema_));
72  protein_silent_report_->initialize(db_session);
73 
74 }
75 
77  //DO NOT PUT THINGS HERE - it is not guaranteed to get called - use flush below instead.
78 }
79 
80 void
82  using namespace basic::options;
83  using namespace basic::options::OptionKeys;
84 
85  if (option.has(inout::dbms::database_name) &&
86  option[inout::dbms::database_name].user()){
87  set_database_name(option[inout::dbms::database_name]);
88  }
89 
90  if (option.has(inout::dbms::pq_schema) &&
91  option[inout::dbms::pq_schema].user()){
92  set_database_pq_schema(option[inout::dbms::pq_schema]);
93  }
94 
95 }
96 
97 void
99  using namespace basic::options;
100  using namespace basic::options::OptionKeys;
101  option.add_relevant( inout::dbms::database_name );
102  option.add_relevant( inout::dbms::pq_schema );
103  option.add_relevant( inout::dbms::host );
104  option.add_relevant( inout::dbms::user );
105  option.add_relevant( inout::dbms::password );
106  option.add_relevant( inout::dbms::port );
107  option.add_relevant( inout::dbms::separate_db_per_mpi_process );
108  option.add_relevant( out::resume_batch);
109 
110 }
111 
112 void
114  string const & database_pq_schema
115 ) {
116  database_name_ = database_pq_schema;
117 }
118 
119 string
121  return database_pq_schema_;
122 }
123 
124 void
126  string const & database_name
127 ) {
128  database_name_ = database_name;
129 }
130 
133  if(database_name_ == ""){
134  utility_exit_with_message(
135  "To use the DatabaseJobInputter, please specify the database "
136  "where the input is data is stored, eg. via the -inout:dbms:database_name "
137  "<database_name> option system flag.");
138  }
139  return database_name_;
140 }
141 
142 
144 }
145 
147  JobCOP job,
148  Pose const & pose
149 ) {
150 
151  // If this is bottle neck, consider hanging on to the db_session
152  // rather than recreating it each time.
153 
154  sessionOP db_session(get_db_session(database_name_, database_pq_schema_));
155  protein_silent_report_->apply(pose, db_session, output_name(job));
156 }
157 
158 /// @brief this function is intended for saving mid-protocol poses; for example
159 /// the final centroid structure in a combined centroid/fullatom protocol.
161  JobCOP,
162  Pose const & pose,
163  string const & tag,
164  int , /*default -1 */
165  bool /*default false*/
166 ) {
167 
168  sessionOP db_session(get_db_session(database_name_, database_pq_schema_));
169  protein_silent_report_->apply(pose, db_session, tag);
170 
171 }
172 
173 /////////////////////////////////state of output functions/////////////////////////////////
175  JobCOP job
176 ) {
177  using namespace basic::options;
178  using namespace basic::options::OptionKeys;
179 
180  // did we complete the job later ?
181  if ( job->completed() ) {
182  return true;
183  }
184  sessionOP db_session(get_db_session(database_name_, database_pq_schema_));
185 
186  //It is possible for the mpi distributor to call this function
187  //before the database has even been initialized
188 
189  protein_silent_report_->initialize(db_session);
190 
191  result res;
192  if(option[out::resume_batch].user())
193  {
194  utility::vector1<core::Size> batch_ids(option[out::resume_batch].value());
195  core::Size placeholder_count = batch_ids.size();
196  std::string placeholder_block= "(?";
197  for(Size j = 1; j < placeholder_count; ++j)
198  {
199  placeholder_block += ",?";
200  }
201  placeholder_block += ")";
202 
203  std::string job_completion_string = "SELECT count(*) FROM sampled_structures WHERE tag=? AND batch_id IN " +placeholder_block+";";
204  cppdb::statement job_completion_statement(basic::database::safely_prepare_statement(job_completion_string,db_session));
205  job_completion_statement.bind(1,output_name(job));
206  for(Size i = 1; i <= batch_ids.size();++i)
207  {
208  core::Size column_index =i+1;
209  job_completion_statement.bind(column_index,batch_ids[i]);
210  }
211  res = basic::database::safely_read_from_database(job_completion_statement);
212 
213  }else
214  {
215  std::string job_completion_string = "SELECT count(*) FROM sampled_structures WHERE tag=? and batch_id = ?;";
216  cppdb::statement job_completion_statement(basic::database::safely_prepare_statement(job_completion_string,db_session));
217  job_completion_statement.bind(1,output_name(job));
218  job_completion_statement.bind(2,protein_silent_report_->get_batch_id());
219  res = basic::database::safely_read_from_database(job_completion_statement);
220  }
221 
222  res.next();
223  Size already_written;
224  res >> already_written;
225  return already_written;
226 
227 
228 }
229 
230 /// @details
231 /// Database tags should preserve the FULL NAME such that we don't end up with
232 /// duplicate tags. This will cause problems on BOINC if changed.
234  return affixed_numbered_name( job );
235 }
236 
237 //CREATOR SECTION
240 {
241  return "DatabaseJobOutputter";
242 }
243 
246  return new DatabaseJobOutputter;
247 }
248 
249 } // namespace features
250 } // namespace protocols