Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResidueConformationFeatures.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/ResidueConformationFeatures.cc
11 /// @brief report idealized torsional DOFs Statistics Scientific Benchmark
12 /// @author Matthew O'Meara
13 /// @author Sam DeLuca
14 
15 // Unit Headers
17 
18 //External
19 #include <boost/uuid/uuid.hpp>
20 
21 // Project Headers
22 #include <core/chemical/AA.hh>
25 #include <core/pose/Pose.hh>
26 #include <core/pose/util.hh>
27 #include <core/types.hh>
28 #include <core/id/AtomID.hh>
29 
30 // Utility Headers
31 #include <numeric/xyzVector.hh>
32 #include <utility/vector1.hh>
33 #include <utility/sql_database/DatabaseSessionManager.hh>
34 #include <utility/tools/make_vector.hh>
35 
36 //Basic Headers
37 #include <basic/database/sql_utils.hh>
38 #include <basic/database/schema_generator/PrimaryKey.hh>
39 #include <basic/database/schema_generator/ForeignKey.hh>
40 #include <basic/database/schema_generator/Column.hh>
41 #include <basic/database/schema_generator/Schema.hh>
42 #include <basic/database/schema_generator/Constraint.hh>
43 #include <basic/options/option.hh>
44 #include <basic/options/keys/out.OptionKeys.gen.hh>
45 #include <basic/options/keys/inout.OptionKeys.gen.hh>
46 #include <basic/database/insert_statement_generator/InsertGenerator.hh>
47 #include <basic/database/insert_statement_generator/RowData.hh>
48 // External Headers
49 #include <cppdb/frontend.h>
50 #include <boost/uuid/uuid_io.hpp>
51 
52 // C++ Headers
53 #include <cmath>
54 #include <sstream>
55 
56 namespace protocols{
57 namespace features{
58 
59 using std::string;
60 using core::Size;
61 using core::Real;
62 using core::pose::Pose;
65 using basic::database::table_exists;
66 using utility::vector1;
67 using utility::sql_database::sessionOP;
68 using cppdb::statement;
69 using cppdb::result;
70 using basic::database::insert_statement_generator::InsertGenerator;
71 using basic::database::insert_statement_generator::RowDataBaseOP;
72 using basic::database::insert_statement_generator::RowData;
73 
74 string
76  return "ResidueConformationFeatures";
77 }
78 
79 void
80 ResidueConformationFeatures::write_schema_to_db(utility::sql_database::sessionOP db_session) const{
81  using namespace basic::database::schema_generator;
82 
83  //******nonprotein_residue_conformation******//
84  Column struct_id("struct_id", new DbUUID(), false);
85  Column seqpos("seqpos", new DbInteger(), false);
86  Column phi("phi", new DbDouble(), false);
87  Column psi("psi", new DbDouble(), false);
88  Column omega("omega", new DbDouble(), false);
89 
90  utility::vector1<Column> non_prot_res_pkeys;
91  non_prot_res_pkeys.push_back(struct_id);
92  non_prot_res_pkeys.push_back(seqpos);
93 
94 
95 
96  utility::vector1<Column> fkey_cols;
97  fkey_cols.push_back(struct_id);
98  fkey_cols.push_back(seqpos);
99 
100  utility::vector1<std::string> fkey_reference_cols;
101  fkey_reference_cols.push_back("struct_id");
102  fkey_reference_cols.push_back("resNum");
103 
104  Schema nonprotein_residue_conformation("nonprotein_residue_conformation", PrimaryKey(non_prot_res_pkeys));
105  nonprotein_residue_conformation.add_column(struct_id);
106  nonprotein_residue_conformation.add_column(seqpos);
107  nonprotein_residue_conformation.add_column(phi);
108  nonprotein_residue_conformation.add_column(psi);
109  nonprotein_residue_conformation.add_column(omega);
110  nonprotein_residue_conformation.add_foreign_key(ForeignKey(fkey_cols, "residues", fkey_reference_cols, true));
111 
112  nonprotein_residue_conformation.write(db_session);
113 
114  //******nonprotein_residue_angles******//
115  Column chinum("chinum", new DbInteger(), false);
116  Column chiangle("chiangle", new DbDouble(), false);
117 
118  utility::vector1<Column> non_prot_res_angle_keys;
119  non_prot_res_angle_keys.push_back(struct_id);
120  non_prot_res_angle_keys.push_back(seqpos);
121  non_prot_res_angle_keys.push_back(chinum);
122 
123  Schema nonprotein_residue_angles("nonprotein_residue_angles", PrimaryKey(non_prot_res_angle_keys));
124  nonprotein_residue_angles.add_column(struct_id);
125  nonprotein_residue_angles.add_column(seqpos);
126  nonprotein_residue_angles.add_column(chinum);
127  nonprotein_residue_angles.add_column(chiangle);
128  nonprotein_residue_angles.add_foreign_key(ForeignKey(fkey_cols, "residues", fkey_reference_cols, true));
129 
130  nonprotein_residue_angles.write(db_session);
131 
132  //******residue_atom_coords******//
133  Column atomno("atomno", new DbInteger(), false);
134  Column x("x", new DbDouble(), false);
135  Column y("y", new DbDouble(), false);
136  Column z("z", new DbDouble(), false);
137 
138  utility::vector1<Column> res_atm_coords_pkeys;
139  res_atm_coords_pkeys.push_back(struct_id);
140  res_atm_coords_pkeys.push_back(seqpos);
141  res_atm_coords_pkeys.push_back(atomno);
142 
143  Schema residue_atom_coords("residue_atom_coords", PrimaryKey(res_atm_coords_pkeys));
144  residue_atom_coords.add_column(struct_id);
145  residue_atom_coords.add_column(seqpos);
146  residue_atom_coords.add_column(atomno);
147  residue_atom_coords.add_column(x);
148  residue_atom_coords.add_column(y);
149  residue_atom_coords.add_column(z);
150  residue_atom_coords.add_foreign_key(ForeignKey(fkey_cols, "residues", fkey_reference_cols, true));
151 
152  residue_atom_coords.write(db_session);
153 
154 }
155 
158  utility::vector1<std::string> dependencies;
159  dependencies.push_back("ResidueFeatures");
160  return dependencies;
161 }
162 
163 Size
165  Pose const & pose,
166  vector1< bool > const & relevant_residues,
167  boost::uuids::uuid struct_id,
168  sessionOP db_session
169 ){
170  //check to see if this structure is ideal
171 
172  bool ideal = true;
173  if(!basic::options::option[basic::options::OptionKeys::out::file::force_nonideal_structure]())
174  {
175  core::conformation::Conformation const & conformation(pose.conformation());
176  for(core::Size resn=1; resn <= pose.n_residue();++resn){
177  if(!relevant_residues[resn]) continue;
178  bool residue_status(core::conformation::is_ideal_position(resn,conformation));
179  if(!residue_status){
180  ideal = false;
181  break;
182  }
183  }
184  }else
185  {
186  ideal = false;
187  }
188 
189  InsertGenerator conformation_insert("nonprotein_residue_conformation");
190  conformation_insert.add_column("struct_id");
191  conformation_insert.add_column("seqpos");
192  conformation_insert.add_column("phi");
193  conformation_insert.add_column("psi");
194  conformation_insert.add_column("omega");
195 
196  InsertGenerator angle_insert("nonprotein_residue_angles");
197  angle_insert.add_column("struct_id");
198  angle_insert.add_column("seqpos");
199  angle_insert.add_column("chinum");
200  angle_insert.add_column("chiangle");
201 
202  InsertGenerator atom_insert("residue_atom_coords");
203  atom_insert.add_column("struct_id");
204  atom_insert.add_column("seqpos");
205  atom_insert.add_column("atomno");
206  atom_insert.add_column("x");
207  atom_insert.add_column("y");
208  atom_insert.add_column("z");
209 
210 
211  RowDataBaseOP struct_id_data = new RowData<boost::uuids::uuid>("struct_id",struct_id);
212  for (Size i = 1; i <= pose.total_residue(); ++i) {
213  if(!relevant_residues[i]) continue;
214 
215  Residue const & resi = pose.residue(i);
216  if(resi.aa() <= num_canonical_aas){
217  continue;
218  }
219  //runtime_assert(resi.aa() <= num_canonical_aas);
220  Real phi (0.0);
221  Real psi (0.0);
222  Real omega(0.0);
223  if(!resi.is_ligand()){
224  phi = resi.mainchain_torsion(1);
225  psi = resi.mainchain_torsion(2);
226  omega= resi.mainchain_torsion(3);
227  }
228 
229  RowDataBaseOP seqpos_data = new RowData<Size>("seqpos",i);
230  RowDataBaseOP phi_data = new RowData<Real>("phi",phi);
231  RowDataBaseOP psi_data = new RowData<Real>("psi",psi);
232  RowDataBaseOP omega_data = new RowData<Real>("omega",omega);
233 
234  conformation_insert.add_row(
235  utility::tools::make_vector(struct_id_data,seqpos_data,phi_data,psi_data,omega_data));
236 
237 
238 
239  for(core::Size chi_num = 1; chi_num <= resi.nchi();++chi_num){
240  core::Real chi_angle = resi.chi(chi_num);
241 
242  RowDataBaseOP chinum_data = new RowData<Size>("chinum",chi_num);
243  RowDataBaseOP chiangle_data = new RowData<Real>("chiangle",chi_angle);
244  angle_insert.add_row(utility::tools::make_vector(
245  struct_id_data,seqpos_data,chinum_data,chiangle_data));
246 
247  }
248  if(!ideal || resi.is_ligand()){ // always store coords for a ligand
249  for(Size atom = 1; atom <= resi.natoms(); ++atom){
250  core::Vector coords = resi.xyz(atom);
251 
252  RowDataBaseOP atom_data = new RowData<Size>("atomno",atom);
253  RowDataBaseOP x_data = new RowData<Real>("x",coords.x());
254  RowDataBaseOP y_data = new RowData<Real>("y",coords.y());
255  RowDataBaseOP z_data = new RowData<Real>("z",coords.z());
256 
257  atom_insert.add_row(utility::tools::make_vector(
258  struct_id_data,seqpos_data,atom_data,x_data,y_data,z_data));
259 
260 
261  }
262  }
263  }
264 
265  conformation_insert.write_to_database(db_session);
266  angle_insert.write_to_database(db_session);
267  atom_insert.write_to_database(db_session);
268 
269  return 0;
270 }
271 
272 void
274  boost::uuids::uuid struct_id,
275  sessionOP db_session
276 ){
277 
278  statement conformation_stmt(basic::database::safely_prepare_statement("DELETE FROM nonprotein_residue_conformation WHERE struct_id = ?;\n",db_session));
279  conformation_stmt.bind(1,struct_id);
280  basic::database::safely_write_to_database(conformation_stmt);
281  statement angle_stmt(basic::database::safely_prepare_statement("DELETE FROM nonprotein_residue_angles WHERE struct_id = ?;\n",db_session));
282  angle_stmt.bind(1,struct_id);
283  basic::database::safely_write_to_database(angle_stmt);
284  statement coords_stmt(basic::database::safely_prepare_statement("DELETE FROM residue_atom_coords WHERE struct_id = ?;",db_session));
285  coords_stmt.bind(1,struct_id);
286  basic::database::safely_write_to_database(coords_stmt);
287 
288 }
289 
290 void
292  sessionOP db_session,
293  boost::uuids::uuid struct_id,
294  Pose & pose
295 ){
296  load_conformation(db_session, struct_id, pose);
297 }
298 
299 void
301  sessionOP db_session,
302  boost::uuids::uuid struct_id,
303  Pose & pose
304 ){
305  if(!table_exists(db_session, "nonprotein_residue_conformation")) return;
306  if(!table_exists(db_session, "nonprotein_residue_angles")) return;
307 
308  if(pose.is_fullatom()){
309 
310  std::string protein_string =
311  "SELECT\n"
312  " seqpos,\n"
313  " phi,\n"
314  " psi,\n"
315  " omega\n"
316  "FROM\n"
317  " nonprotein_residue_conformation\n"
318  "WHERE\n"
319  " nonprotein_residue_conformation.struct_id=?;";
320 
321  statement protein_stmt(basic::database::safely_prepare_statement(protein_string,db_session));
322  protein_stmt.bind(1,struct_id);
323 
324  std::string conformation_string =
325  "SELECT\n"
326  " seqpos,\n"
327  " chinum,\n"
328  " chiangle\n"
329  "FROM\n"
330  " nonprotein_residue_angles\n"
331  "WHERE\n"
332  " nonprotein_residue_angles.struct_id=?;";
333 
334  statement conformation_stmt(basic::database::safely_prepare_statement(conformation_string,db_session));
335  conformation_stmt.bind(1,struct_id);
336 
337 
338  result protein_res(basic::database::safely_read_from_database(protein_stmt));
339  while(protein_res.next()){
340  Size seqpos;
341  Real phi,psi,omega;
342  protein_res >> seqpos >> phi >> psi >> omega;
343  if (pose.residue_type(seqpos).is_protein()){
344  pose.set_phi(seqpos,phi);
345  pose.set_psi(seqpos,psi);
346  pose.set_omega(seqpos,omega);
347  }
348  }
349  result res_conformation(basic::database::safely_read_from_database(conformation_stmt));
350  while(res_conformation.next()){
351  //Size nchi(pose.residue_type(seqpos).nchi());
352  Size seqpos;
353  Size chinum;
354  Real chiangle;
355  set_coords_for_residue(db_session,struct_id,seqpos,pose);
356  res_conformation >> seqpos >> chinum >> chiangle;
357  pose.set_chi(chinum,seqpos,chiangle);
358  }
359 
360  }else{
361 
362  std::string protein_string =
363  "SELECT\n"
364  " seqpos,\n"
365  " phi,\n"
366  " psi,\n"
367  " omega\n"
368  "FROM\n"
369  " nonprotein_residue_conformation\n"
370  "WHERE\n"
371  " nonprotein_residue_conformation.struct_id=?;";
372 
373  statement protein_stmt(basic::database::safely_prepare_statement(protein_string,db_session));
374  protein_stmt.bind(1,struct_id);
375 
376  result protein_res(basic::database::safely_read_from_database(protein_stmt));
377  while(protein_res.next()){
378  Size seqpos;
379  Real phi,psi,omega;
380  protein_res >> seqpos >> phi >> psi >> omega;
381  if (!pose.residue_type(seqpos).is_protein()){
382  // WARNING why are you storing non-protein in the ProteinSilentReport?
383  continue;
384  }
385  set_coords_for_residue(db_session,struct_id,seqpos,pose);
386  pose.set_phi(seqpos,phi);
387  pose.set_psi(seqpos,psi);
388  pose.set_omega(seqpos,omega);
389  }
390 
391  }
392 }
393 
394 //This should be factored out into a non-member function.
396  sessionOP db_session,
397  boost::uuids::uuid struct_id,
398  Size seqpos,
399  Pose & pose
400 ){
401 
402  std::string statement_string =
403  "SELECT\n"
404  " atomno,\n"
405  " x,\n"
406  " y,\n"
407  " z\n"
408  "FROM\n"
409  " residue_atom_coords\n"
410  "WHERE\n"
411  "residue_atom_coords.struct_id=? AND residue_atom_coords.seqpos=?;";
412 
413  statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
414  stmt.bind(1,struct_id);
415  stmt.bind(2,seqpos);
416 
417  result res(basic::database::safely_read_from_database(stmt));
418  while(res.next()){
419  Size atomno;
420  Real x,y,z;
421  res >> atomno >> x >> y >> z;
422 
423  core::id::AtomID atom_id(atomno,seqpos);
424  core::Vector coords(x,y,z);
425  pose.set_xyz(atom_id,coords);
426  }
427 
428 
429 }
430 
431 
432 } // features
433 } // protocols