Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PoseConformationFeatures.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/PoseConformationFeatures.cc
11 /// @brief report comments stored with each pose
12 /// @author Matthew O'Meara
13 
14 // Unit Headers
16 
17 //External
18 #include <boost/uuid/uuid.hpp>
19 #include <boost/uuid/uuid_io.hpp>
20 
21 // Project Headers
22 #include <core/chemical/AA.hh>
27 #include <core/io/pdb/file_data.hh>
29 #include <core/kinematics/Jump.hh>
30 #include <core/pose/Pose.hh>
32 #include <core/types.hh>
33 #include <basic/database/schema_generator/PrimaryKey.hh>
34 #include <basic/database/schema_generator/ForeignKey.hh>
35 #include <basic/database/schema_generator/Column.hh>
36 #include <basic/database/schema_generator/Schema.hh>
37 #include <basic/database/schema_generator/Constraint.hh>
38 
39 #include <basic/database/insert_statement_generator/InsertGenerator.hh>
40 #include <basic/database/insert_statement_generator/RowData.hh>
41 
42 // Basic Headers
43 #include <basic/options/option.hh>
44 #include <basic/options/keys/inout.OptionKeys.gen.hh>
45 #include <basic/database/sql_utils.hh>
46 #include <basic/Tracer.hh>
47 
48 
49 // Numeric Headers
50 #include <numeric/xyzVector.hh>
51 #include <numeric/xyzMatrix.hh>
52 
53 // Utility Headers
54 #include <utility/vector1.hh>
55 #include <utility/sql_database/DatabaseSessionManager.hh>
56 #include <utility/tools/make_vector.hh>
57 
58 // Boost Headers
59 #include <boost/foreach.hpp>
60 #define foreach BOOST_FOREACH
61 
62 // External Headers
63 #include <cppdb/frontend.h>
64 
65 // C++ Headers
66 #include <cmath>
67 #include <sstream>
68 
69 namespace protocols{
70 namespace features{
71 
72 static basic::Tracer TR("protocols.features.PoseConformationFeatures");
73 
74 using std::string;
75 using std::stringstream;
76 using std::endl;
77 using core::Real;
78 using core::Size;
79 using core::Vector;
82 using core::pose::Pose;
83 using core::pose::PoseOP;
95 using numeric::xyzMatrix;
96 using numeric::xyzVector;
97 using utility::vector1;
98 using utility::sql_database::DatabaseSessionManager;
99 using utility::sql_database::sessionOP;
100 using cppdb::statement;
101 using cppdb::result;
102 using basic::database::insert_statement_generator::InsertGenerator;
103 using basic::database::insert_statement_generator::RowDataBaseOP;
104 using basic::database::insert_statement_generator::RowData;
105 
106 string
107 PoseConformationFeatures::type_name() const { return "PoseConformationFeatures"; }
108 
109 void
110 PoseConformationFeatures::write_schema_to_db(utility::sql_database::sessionOP db_session) const{
111 
112  using namespace basic::database::schema_generator;
113 
114  Column struct_id("struct_id", new DbUUID(), false /*not null*/, false /*don't autoincrement*/);
115 
116  /******pose_conformations******/
117  Schema pose_conformations("pose_conformations");
118  pose_conformations.add_foreign_key(ForeignKey(struct_id, "structures", "struct_id", true /*defer*/));
119 
120  pose_conformations.add_column( Column("annotated_sequence", new DbText()) );
121  pose_conformations.add_column( Column("total_residue", new DbInteger()) );
122  pose_conformations.add_column( Column("fullatom", new DbInteger()) );
123 
124  pose_conformations.write(db_session);
125 
126  /******fold_trees******/
127  Schema fold_trees("fold_trees");
128  fold_trees.add_foreign_key(ForeignKey(struct_id, "structures", "struct_id", true /*defer*/));
129 
130  fold_trees.add_column( Column("start_res", new DbInteger()) );
131  fold_trees.add_column( Column("start_atom", new DbText()) );
132  fold_trees.add_column( Column("stop_res", new DbInteger()) );
133  fold_trees.add_column( Column("stop_atom", new DbText()) );
134  fold_trees.add_column( Column("label", new DbInteger()) );
135  fold_trees.add_column( Column("keep_stub_in_residue", new DbInteger()) );
136 
137  fold_trees.write(db_session);
138 
139  /******jumps******/
140  Schema jumps("jumps");
141  jumps.add_foreign_key(ForeignKey(struct_id, "structures", "struct_id", true /*defer*/));
142  jumps.add_column( Column("jump_id", new DbInteger()) );
143  jumps.add_column( Column("xx", new DbDouble()) );
144  jumps.add_column( Column("xy", new DbDouble()) );
145  jumps.add_column( Column("xz", new DbDouble()) );
146  jumps.add_column( Column("yx", new DbDouble()) );
147  jumps.add_column( Column("yy", new DbDouble()) );
148  jumps.add_column( Column("yz", new DbDouble()) );
149  jumps.add_column( Column("zx", new DbDouble()) );
150  jumps.add_column( Column("zy", new DbDouble()) );
151  jumps.add_column( Column("zz", new DbDouble()) );
152  jumps.add_column( Column("x", new DbDouble()) );
153  jumps.add_column( Column("y", new DbDouble()) );
154  jumps.add_column( Column("z", new DbDouble()) );
155 
156  jumps.write(db_session);
157 
158  /******chain_endings******/
159  Schema chain_endings("chain_endings");
160  chain_endings.add_foreign_key(ForeignKey(struct_id, "structures", "struct_id", true /*defer*/));
161  chain_endings.add_column( Column("end_pos", new DbInteger()) );
162 
163  chain_endings.write(db_session);
164 }
165 
168  utility::vector1<std::string> dependencies;
169  dependencies.push_back("StructureFeatures");
170  return dependencies;
171 }
172 
173 Size
175  Pose const & pose_orig,
176  vector1< bool > const & relevant_residues,
177  boost::uuids::uuid struct_id,
178  sessionOP db_session
179 ){
180  vector1< Size > residue_indices;
181  for(Size i = 1; i <= relevant_residues.size(); ++i){
182  if(relevant_residues[i]) residue_indices.push_back(i);
183  }
184 
185  if (residue_indices.size() == pose_orig.n_residue()){
186  return report_features_implementation(pose_orig, struct_id, db_session);
187  }
188  // else...
189  Pose pose;
190  pose_from_pose( pose, pose_orig, residue_indices );
191  return report_features_implementation(pose, struct_id, db_session);
192 }
193 
194 Size
196  Pose const & pose,
197  boost::uuids::uuid struct_id,
198  sessionOP db_session
199 ){
200  FoldTree const & fold_tree(pose.conformation().fold_tree());
201  //assume non-trivial fold_tree only if more than one edge, i.e., EDGE 1 <nres> -1
202  //cppdb::transaction transact_guard(*db_session);
203 
204  InsertGenerator fold_tree_insert("fold_trees");
205  fold_tree_insert.add_column("struct_id");
206  fold_tree_insert.add_column("start_res");
207  fold_tree_insert.add_column("start_atom");
208  fold_tree_insert.add_column("stop_res");
209  fold_tree_insert.add_column("stop_atom");
210  fold_tree_insert.add_column("label");
211  fold_tree_insert.add_column("keep_stub_in_residue");
212 
213  RowDataBaseOP struct_id_data = new RowData<boost::uuids::uuid>("struct_id",struct_id);
214 
216  it = fold_tree.begin(), it_end = fold_tree.end(); it != it_end; ++it) {
217 
218  int start_res(it->start()), stop_res(it->stop()), label(it->label());
219  string start_atom(it->start_atom()), stop_atom(it->stop_atom());
220  bool keep_stub_in_residue(it->keep_stub_in_residue());
221 
222 
223  RowDataBaseOP start_res_data = new RowData<int>("start_res",start_res);
224  RowDataBaseOP start_atom_data = new RowData<string>("start_atom",start_atom);
225  RowDataBaseOP stop_res_data = new RowData<int>("stop_res",stop_res);
226  RowDataBaseOP stop_atom_data = new RowData<string>("stop_atom",stop_atom);
227  RowDataBaseOP label_data = new RowData<int>("label",label);
228  RowDataBaseOP keep_stub_data = new RowData<bool>("keep_stub_in_residue",keep_stub_in_residue);
229 
230  fold_tree_insert.add_row(
231  utility::tools::make_vector(struct_id_data,start_res_data,start_atom_data,stop_res_data,stop_atom_data,label_data,keep_stub_data));
232  }
233 
234  fold_tree_insert.write_to_database(db_session);
235 
236  InsertGenerator jump_insert("jumps");
237  jump_insert.add_column("struct_id");
238  jump_insert.add_column("jump_id");
239  jump_insert.add_column("xx");
240  jump_insert.add_column("xy");
241  jump_insert.add_column("xz");
242  jump_insert.add_column("yx");
243  jump_insert.add_column("yy");
244  jump_insert.add_column("yz");
245  jump_insert.add_column("zx");
246  jump_insert.add_column("zy");
247  jump_insert.add_column("zz");
248  jump_insert.add_column("x");
249  jump_insert.add_column("y");
250  jump_insert.add_column("z");
251 
252  for (Size nr = 1; nr <= fold_tree.num_jump(); nr++)
253  {
254  Jump const & jump(pose.jump(nr));
255  xyzMatrix< Real > const & r(jump.get_rotation());
256  Real xx(r.xx()), xy(r.xy()), xz(r.xz());
257  Real yx(r.yx()), yy(r.yy()), yz(r.yz());
258  Real zx(r.zx()), zy(r.zy()), zz(r.zz());
259  Vector const & t(jump.get_translation());
260  Real x(t.x()), y(t.y()), z(t.z());
261 
262  RowDataBaseOP jump_id_data = new RowData<Size>("jump_id",nr);
263  RowDataBaseOP xx_data = new RowData<Real>("xx",xx);
264  RowDataBaseOP xy_data = new RowData<Real>("xy",xy);
265  RowDataBaseOP xz_data = new RowData<Real>("xz",xz);
266  RowDataBaseOP yx_data = new RowData<Real>("yx",yx);
267  RowDataBaseOP yy_data = new RowData<Real>("yy",yy);
268  RowDataBaseOP yz_data = new RowData<Real>("yz",yz);
269  RowDataBaseOP zx_data = new RowData<Real>("zx",zx);
270  RowDataBaseOP zy_data = new RowData<Real>("zy",zy);
271  RowDataBaseOP zz_data = new RowData<Real>("zz",zz);
272  RowDataBaseOP x_data = new RowData<Real>("x",x);
273  RowDataBaseOP y_data = new RowData<Real>("y",y);
274  RowDataBaseOP z_data = new RowData<Real>("z",z);
275  jump_insert.add_row(
276  utility::tools::make_vector(struct_id_data,jump_id_data,xx_data,xy_data,xz_data,yx_data,yy_data,yz_data,zx_data,zy_data,zz_data,x_data,y_data,z_data));
277  }
278  jump_insert.write_to_database(db_session);
279 
280  InsertGenerator chain_ending_insert("chain_endings");
281  chain_ending_insert.add_column("struct_id");
282  chain_ending_insert.add_column("end_pos");
283  foreach(Size end_pos, pose.conformation().chain_endings())
284  {
285  RowDataBaseOP end_pos_data = new RowData<Size>("end_pos",end_pos);
286 
287  chain_ending_insert.add_row(
288  utility::tools::make_vector(struct_id_data,end_pos_data));
289  }
290  chain_ending_insert.write_to_database(db_session);
291 
292  string annotated_sequence(pose.annotated_sequence(true));
293 
294 
295  InsertGenerator pose_conformation_insert("pose_conformations");
296  pose_conformation_insert.add_column("struct_id");
297  pose_conformation_insert.add_column("annotated_sequence");
298  pose_conformation_insert.add_column("total_residue");
299  pose_conformation_insert.add_column("fullatom");
300 
301  RowDataBaseOP annotated_sequence_data = new RowData<string>("annotated_sequence",annotated_sequence);
302  RowDataBaseOP total_residue_data = new RowData<Size>("total_residue",pose.total_residue());
303  RowDataBaseOP fullatom_data = new RowData<bool>("fullatom",pose.is_fullatom());
304 
305  pose_conformation_insert.add_row(
306  utility::tools::make_vector(struct_id_data,annotated_sequence_data,total_residue_data,fullatom_data));
307 
308  pose_conformation_insert.write_to_database(db_session);
309  return 0;
310 }
311 
313  boost::uuids::uuid struct_id,
314  sessionOP db_session
315 ){
316 
317  statement conf_stmt(basic::database::safely_prepare_statement("DELETE FROM pose_conformations WHERE struct_id = ?;\n",db_session));
318  conf_stmt.bind(1,struct_id);
319  basic::database::safely_write_to_database(conf_stmt);
320 
321  statement fold_stmt(basic::database::safely_prepare_statement("DELETE FROM fold_trees WHERE struct_id = ?;\n",db_session));
322  fold_stmt.bind(1,struct_id);
323  basic::database::safely_write_to_database(fold_stmt);
324 
325  statement jump_stmt(basic::database::safely_prepare_statement("DELETE FROM jumps WHERE struct_id = ?;\n",db_session));
326  jump_stmt.bind(1,struct_id);
327  basic::database::safely_write_to_database(jump_stmt);
328 
329  statement chain_stmt(basic::database::safely_prepare_statement("DELETE FROM chain_endings WHERE struct_id = ?;",db_session));
330  chain_stmt.bind(1,struct_id);
331  basic::database::safely_write_to_database(chain_stmt);
332 }
333 
334 void
336  sessionOP db_session,
337  boost::uuids::uuid struct_id,
338  Pose & pose
339 ){
340  load_sequence(db_session, struct_id, pose);
341  load_fold_tree(db_session, struct_id, pose);
342  load_jumps(db_session, struct_id, pose);
343  load_chain_endings(db_session, struct_id, pose);
344 }
345 
346 void
348  sessionOP db_session,
349  boost::uuids::uuid struct_id,
350  Pose & pose
351 ){
352 
353  if(!basic::database::table_exists(db_session, "pose_conformations")){
354  TR << "WARNING: pose_conformations table does not exist and thus respective data will not be added to the pose!" << std::endl;
355  return;
356  }
357 
358  std::string statement_string =
359  "SELECT\n"
360  " annotated_sequence,\n"
361  " total_residue,\n"
362  " fullatom\n"
363  "FROM\n"
364  " pose_conformations\n"
365  "WHERE\n"
366  " pose_conformations.struct_id = ?;";
367  statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
368  stmt.bind(1,struct_id);
369  result res(basic::database::safely_read_from_database(stmt));
370 
371  if(!res.next()){
372  stringstream error_message;
373  error_message << "Unable to locate structure with struct_id '" << to_string(struct_id) << "'";
374  utility_exit_with_message(error_message.str());
375  }
376  string annotated_sequence;
377  Size total_residue, fullatom;
378 
379  res >> annotated_sequence >> total_residue >> fullatom;
380 
381  ResidueTypeSetCAP residue_set(ChemicalManager::get_instance()->residue_type_set(
382  fullatom ? FA_STANDARD : CENTROID));
383  make_pose_from_sequence(pose, annotated_sequence, *residue_set);
384  runtime_assert(pose.total_residue() == total_residue );
385 }
386 
387 
388 void
390  sessionOP db_session,
391  boost::uuids::uuid struct_id,
392  Pose & pose
393 ){
394 
395  if(!basic::database::table_exists(db_session, "fold_trees")){
396  TR << "WARNING: fold_trees table does not exist and thus respective data will not be added to the pose!" << std::endl;
397  return;
398  }
399 
400  statement stmt = (*db_session) <<
401  "SELECT\n"
402  " start_res,\n"
403  " start_atom,\n"
404  " stop_res,\n"
405  " stop_atom,\n"
406  " label,\n"
407  " keep_stub_in_residue\n"
408  "FROM\n"
409  " fold_trees\n"
410  "WHERE\n"
411  " fold_trees.struct_id=?;" << struct_id;
412 
413  result res(basic::database::safely_read_from_database(stmt));
414 
415  FoldTree t = FoldTree();
416  while(res.next()){
417  int start_res, stop_res, label;
418  string start_atom, stop_atom;
419  int keep_stub_in_residue;
420  res >> start_res >> start_atom >> stop_res >> stop_atom >> label >> keep_stub_in_residue;
421  if(label == -2 || label > 0){ //CHEMICAL or JUMP
422  t.add_edge(Edge(
423  start_res, stop_res, label, start_atom, stop_atom, keep_stub_in_residue));
424  } else {
425  t.add_edge(Edge(start_res, stop_res, label, "", "", keep_stub_in_residue));
426  }
427  }
428  // TODO verify that pose.fold_tree(t) is ok (not cleared from the stack)
429  pose.fold_tree(t);
430 
431  TR.Debug << "Fold tree loaded" << std::endl;
432 }
433 
434 void
436  sessionOP db_session,
437  boost::uuids::uuid struct_id,
438  Pose & pose
439 ){
440  if(!basic::database::table_exists(db_session, "jumps")){
441  TR << "WARNING: jumps table does not exist and thus respective data will not be added to the pose!" << std::endl;
442  return;
443  }
444 
445  //note the Conformation object sorts the chain_endings after they are passed in.
446  std::string statement_string =
447  "SELECT\n"
448  " jump_id,\n"
449  " xx,\n"
450  " xy,\n"
451  " xz,\n"
452  " yx,\n"
453  " yy,\n"
454  " yz,\n"
455  " zx,\n"
456  " zy,\n"
457  " zz,\n"
458  " x,\n"
459  " y,\n"
460  " z\n"
461  "FROM\n"
462  " jumps\n"
463  "WHERE\n"
464  " jumps.struct_id=?;";
465  statement stmt(
466  basic::database::safely_prepare_statement(statement_string,db_session));
467 
468  stmt.bind(1,struct_id);
469  result res(basic::database::safely_read_from_database(stmt));
470  while(res.next()){
471  Size jump_id;
472  Real xx, xy, xz, yx, yy, yz, zx, zy, zz, x, y, z;
473  res >> jump_id;
474  res >> xx >> xy >> xz >> yx >> yy >> yz >> zx >> zy >> zz >> x >> y >> z;
476  xx, xy, xz, yx, yy, yz, zx, zy, zz));
477  xyzVector< Real > t(x, y, z);
478  pose.set_jump(jump_id, Jump(RT(r,t)));
479  }
480 
481  TR.Debug << "Jumps loaded" << std::endl;
482 }
483 
484 void
486  sessionOP db_session,
487  boost::uuids::uuid struct_id,
488  Pose & pose
489 ){
490 
491  if(!basic::database::table_exists(db_session, "chain_endings")){
492  TR << "WARNING: chain_endings table does not exist and thus respective data will not be added to the pose!" << std::endl;
493  return;
494  }
495 
496  //note the Conformation object sorts the chain_endings after they are passed in.
497 
498  std::string statement_string =
499  "SELECT\n"
500  " end_pos\n"
501  "FROM\n"
502  " chain_endings\n"
503  "WHERE\n"
504  " chain_endings.struct_id=?;";
505 
506  statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
507  stmt.bind(1,struct_id);
508 
509 
510  result res(basic::database::safely_read_from_database(stmt));
511 
512  vector1< Size > chain_endings;
513  while(res.next()){
514  Size end_pos;
515  res >> end_pos;
516  chain_endings.push_back(end_pos);
517  }
518  pose.conformation().chain_endings(chain_endings);
519 
520  TR.Debug << "Chain endings loaded" << std::endl;
521 }
522 
523 
524 
525 } // namespace
526 } // namespace