Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResidueDatabaseIO.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/core/chemical/ResidueDatabaseIO.cc
11 /// @author Sam DeLuca
12 /// @author Matt O'Meara
13 
14 // Project Headers
15 #include <basic/Tracer.hh>
16 #include <core/chemical/Atom.hh>
22 #include <core/kinematics/Stub.hh>
23 #include <core/types.hh>
24 #include <basic/database/sql_utils.hh>
25 #include <basic/options/option.hh>
26 #include <basic/options/keys/inout.OptionKeys.gen.hh>
27 
28 #include <basic/database/schema_generator/PrimaryKey.hh>
29 #include <basic/database/schema_generator/ForeignKey.hh>
30 #include <basic/database/schema_generator/Column.hh>
31 #include <basic/database/schema_generator/Schema.hh>
32 #include <basic/database/schema_generator/Index.hh>
33 
34 
35 #include <numeric/xyz.functions.hh>
36 #include <utility/sql_database/DatabaseSessionManager.hh>
37 #include <utility/string_util.hh>
38 #include <utility/vector1.hh>
39 
40 // Boost Headers
41 #include <boost/foreach.hpp>
42 #define foreach BOOST_FOREACH
43 
44 namespace core {
45 namespace chemical {
46 
47 using utility::sql_database::sessionOP;
48 using utility::vector1;
49 
51 {
52 
53 }
54 
56 {
57 
58 }
59 
60 void
62  sessionOP db_session
63 ) const {
74 }
75 
76 void
78  sessionOP db_session
79 ) const {
80  using namespace basic::database::schema_generator;
81 
82  Column residue_type_set_name("residue_type_set_name", new DbText(255));
83  Column version("version", new DbText());
84  Column name("name", new DbText(255));
85  Column name3("name3", new DbText(3));
86  Column name1("name1", new DbText(2)); // TODO Fix ccpdb to allow putting in just a single character rather then a one character string.
87  Column aa("aa", new DbInteger());
88  Column lower_connect("lower_connect", new DbInteger());
89  Column upper_connect("upper_connect", new DbInteger());
90  Column nbr_atom("nbr_atom", new DbInteger());
91  Column nbr_radius("nbr_radius", new DbReal());
92  Column rotamer_library("rotamer_library", new DbText());
93 
94  Columns primary_key_columns;
95  primary_key_columns.push_back(residue_type_set_name);
96  primary_key_columns.push_back(name);
97  PrimaryKey primary_key(primary_key_columns);
98 
99  Schema table("residue_type", primary_key);
100  table.add_column(version);
101  table.add_column(name3);
102  table.add_column(name1);
103  table.add_column(aa);
104  table.add_column(lower_connect);
105  table.add_column(upper_connect);
106  table.add_column(nbr_atom);
107  table.add_column(nbr_radius);
108  table.add_column(rotamer_library);
109 
110  table.write(db_session);
111 }
112 
113 void
115  sessionOP db_session
116 ) const {
117  using namespace basic::database::schema_generator;
118 
119  Column residue_type_set_name("residue_type_set_name", new DbText(255));
120  Column residue_type_name("residue_type_name", new DbText(255));
121  Column atom_index("atom_index", new DbInteger());
122  Column atom_name("atom_name", new DbText());
123  Column atom_type_name("atom_type_name", new DbText());
124  Column mm_atom_type_name("mm_atom_type_name", new DbText());
125  Column charge("charge", new DbReal());
126  Column is_backbone("is_backbone", new DbInteger());
127 
128  Columns primary_key_columns;
129  primary_key_columns.push_back(residue_type_set_name);
130  primary_key_columns.push_back(residue_type_name);
131  primary_key_columns.push_back(atom_index);
132  PrimaryKey primary_key(primary_key_columns);
133 
134  Columns index1_columns;
135  index1_columns.push_back(residue_type_set_name);
136  index1_columns.push_back(residue_type_name);
137  index1_columns.push_back(atom_name);
138  Index index1(index1_columns);
139 
140 
141  Columns foreign_key_columns;
142  foreign_key_columns.push_back(residue_type_set_name);
143  foreign_key_columns.push_back(residue_type_name);
144  vector1< std::string > reference_columns;
145  reference_columns.push_back("residue_type_set_name");
146  reference_columns.push_back("name");
147  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
148 
149  Schema table("residue_type_atom", primary_key);
150  table.add_foreign_key(foreign_key);
151  table.add_column(atom_name);
152  table.add_column(atom_type_name);
153  table.add_column(mm_atom_type_name);
154  table.add_column(charge);
155  table.add_column(is_backbone);
156  table.add_index(index1);
157 
158  table.write(db_session);
159 }
160 
161 void
163  sessionOP db_session
164 ) const {
165  using namespace basic::database::schema_generator;
166 
167  Column residue_type_set_name("residue_type_set_name", new DbText(255));
168  Column residue_type_name("residue_type_name", new DbText(255));
169  Column atom1("atom1", new DbInteger());
170  Column atom2("atom2", new DbInteger());
171  Column bond_type("bond_type", new DbInteger());
172 
173  Columns primary_key_columns;
174  primary_key_columns.push_back(residue_type_set_name);
175  primary_key_columns.push_back(residue_type_name);
176  primary_key_columns.push_back(atom1);
177  primary_key_columns.push_back(atom2);
178  PrimaryKey primary_key(primary_key_columns);
179 
180 
181  Columns foreign_key_columns;
182  foreign_key_columns.push_back(residue_type_set_name);
183  foreign_key_columns.push_back(residue_type_name);
184  vector1< std::string > reference_columns;
185  reference_columns.push_back("residue_type_set_name");
186  reference_columns.push_back("name");
187  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
188 
189  Schema table("residue_type_bond", primary_key);
190  table.add_foreign_key(foreign_key);
191  table.add_column(bond_type);
192 
193  table.write(db_session);
194 }
195 
196 
197 void
199  sessionOP db_session
200 ) const {
201  using namespace basic::database::schema_generator;
202 
203  Column residue_type_set_name("residue_type_set_name", new DbText(255));
204  Column residue_type_name("residue_type_name", new DbText(255));
205  Column atom1("atom1", new DbInteger());
206  Column atom2("atom2", new DbInteger());
207 
208  Columns primary_key_columns;
209  primary_key_columns.push_back(residue_type_set_name);
210  primary_key_columns.push_back(residue_type_name);
211  primary_key_columns.push_back(atom1);
212  primary_key_columns.push_back(atom2);
213  PrimaryKey primary_key(primary_key_columns);
214 
215 
216  Columns foreign_key_columns;
217  foreign_key_columns.push_back(residue_type_set_name);
218  foreign_key_columns.push_back(residue_type_name);
219  vector1< std::string > reference_columns;
220  reference_columns.push_back("residue_type_set_name");
221  reference_columns.push_back("name");
222  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
223 
224  Schema table("residue_type_cut_bond", primary_key);
225  table.add_foreign_key(foreign_key);
226 
227  table.write(db_session);
228 }
229 
230 void
232  sessionOP db_session
233 ) const {
234  using namespace basic::database::schema_generator;
235 
236  Column residue_type_set_name("residue_type_set_name", new DbText(255));
237  Column residue_type_name("residue_type_name", new DbText(255));
238  Column chino("chino", new DbInteger());
239  Column atom1("atom1", new DbText());
240  Column atom2("atom2", new DbText());
241  Column atom3("atom3", new DbText());
242  Column atom4("atom4", new DbText());
243 
244  Columns primary_key_columns;
245  primary_key_columns.push_back(residue_type_set_name);
246  primary_key_columns.push_back(residue_type_name);
247 
248  // perhaps it would be better to have chino here instead of the
249  // atoms themselves?
250  primary_key_columns.push_back(atom1);
251  primary_key_columns.push_back(atom2);
252  primary_key_columns.push_back(atom3);
253  primary_key_columns.push_back(atom4);
254  PrimaryKey primary_key(primary_key_columns);
255 
256 
257  Columns foreign_key_columns;
258  foreign_key_columns.push_back(residue_type_set_name);
259  foreign_key_columns.push_back(residue_type_name);
260  vector1< std::string > reference_columns;
261  reference_columns.push_back("residue_type_set_name");
262  reference_columns.push_back("name");
263  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
264 
265  Schema table("residue_type_chi", primary_key);
266  table.add_foreign_key(foreign_key);
267  table.add_column(chino);
268 
269  table.write(db_session);
270 }
271 
272 void
274  sessionOP db_session
275 ) const {
276  using namespace basic::database::schema_generator;
277 
278  Column residue_type_set_name("residue_type_set_name", new DbText(255));
279  Column residue_type_name("residue_type_name", new DbText(255));
280  Column chino("chino", new DbInteger());
281  Column mean("mean", new DbReal());
282  Column sdev("sdev", new DbReal());
283 
284  Columns primary_key_columns;
285  primary_key_columns.push_back(residue_type_set_name);
286  primary_key_columns.push_back(residue_type_name);
287  primary_key_columns.push_back(chino);
288  primary_key_columns.push_back(mean);
289  primary_key_columns.push_back(sdev);
290  PrimaryKey primary_key(primary_key_columns);
291 
292 
293  Columns foreign_key_columns;
294  foreign_key_columns.push_back(residue_type_set_name);
295  foreign_key_columns.push_back(residue_type_name);
296  vector1< std::string > reference_columns;
297  reference_columns.push_back("residue_type_set_name");
298  reference_columns.push_back("name");
299  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
300 
301  Schema table("residue_type_chi_rotamer", primary_key);
302  table.add_foreign_key(foreign_key);
303 
304  table.write(db_session);
305 }
306 
307 void
309  sessionOP db_session
310 ) const {
311  using namespace basic::database::schema_generator;
312 
313  Column residue_type_set_name("residue_type_set_name", new DbText(255));
314  Column residue_type_name("residue_type_name", new DbText(255));
315  Column chino("chino", new DbInteger());
316  Column sample("sample", new DbReal());
317  Column is_extra("is_extra", new DbInteger());
318 
319  Columns primary_key_columns;
320  primary_key_columns.push_back(residue_type_set_name);
321  primary_key_columns.push_back(residue_type_name);
322  primary_key_columns.push_back(chino);
323  primary_key_columns.push_back(sample);
324  PrimaryKey primary_key(primary_key_columns);
325 
326 
327  Columns foreign_key_columns;
328  foreign_key_columns.push_back(residue_type_set_name);
329  foreign_key_columns.push_back(residue_type_name);
330  vector1< std::string > reference_columns;
331  reference_columns.push_back("residue_type_set_name");
332  reference_columns.push_back("name");
333  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
334 
335  Schema table("residue_type_proton_chi", primary_key);
336  table.add_foreign_key(foreign_key);
337  table.add_column(is_extra);
338 
339  table.write(db_session);
340 }
341 
342 void
344  sessionOP db_session
345 ) const {
346  using namespace basic::database::schema_generator;
347 
348  Column residue_type_set_name("residue_type_set_name", new DbText(255));
349  Column residue_type_name("residue_type_name", new DbText(255));
350  Column property("property", new DbText());
351 
352  Columns primary_key_columns;
353  primary_key_columns.push_back(residue_type_set_name);
354  primary_key_columns.push_back(residue_type_name);
355  primary_key_columns.push_back(property);
356  PrimaryKey primary_key(primary_key_columns);
357 
358 
359  Columns foreign_key_columns;
360  foreign_key_columns.push_back(residue_type_set_name);
361  foreign_key_columns.push_back(residue_type_name);
362  vector1< std::string > reference_columns;
363  reference_columns.push_back("residue_type_set_name");
364  reference_columns.push_back("name");
365  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
366 
367  Schema table("residue_type_property", primary_key);
368  table.add_foreign_key(foreign_key);
369 
370  table.write(db_session);
371 }
372 
373 void
375  sessionOP db_session
376 ) const {
377  using namespace basic::database::schema_generator;
378 
379  Column residue_type_set_name("residue_type_set_name", new DbText(255));
380  Column residue_type_name("residue_type_name", new DbText(255));
381  Column variant_type("variant_type", new DbText());
382 
383  Columns primary_key_columns;
384  primary_key_columns.push_back(residue_type_set_name);
385  primary_key_columns.push_back(residue_type_name);
386  primary_key_columns.push_back(variant_type);
387  PrimaryKey primary_key(primary_key_columns);
388 
389 
390  Columns foreign_key_columns;
391  foreign_key_columns.push_back(residue_type_set_name);
392  foreign_key_columns.push_back(residue_type_name);
393  vector1< std::string > reference_columns;
394  reference_columns.push_back("residue_type_set_name");
395  reference_columns.push_back("name");
396  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
397 
398  Schema table("residue_type_variant_type", primary_key);
399  table.add_foreign_key(foreign_key);
400 
401  table.write(db_session);
402 }
403 
404 void
406  sessionOP db_session
407 ) const {
408  using namespace basic::database::schema_generator;
409 
410  Column residue_type_set_name("residue_type_set_name", new DbText(255));
411  Column residue_type_name("residue_type_name", new DbText(255));
412  Column icoor_sequence("icoor_sequence", new DbInteger());
413  Column child_atom("child_atom", new DbText());
414  Column phi("phi", new DbReal());
415  Column theta("theta", new DbReal());
416  Column distance("distance", new DbReal());
417  Column parent_atom("parent_atom", new DbText());
418  Column angle_atom("angle_atom", new DbText());
419  Column torsion_atom("torsion_atom", new DbText());
420 
421  Columns primary_key_columns;
422  primary_key_columns.push_back(residue_type_set_name);
423  primary_key_columns.push_back(residue_type_name);
424  primary_key_columns.push_back(child_atom);
425  PrimaryKey primary_key(primary_key_columns);
426 
427 
428  Columns foreign_key_columns;
429  foreign_key_columns.push_back(residue_type_set_name);
430  foreign_key_columns.push_back(residue_type_name);
431  vector1< std::string > reference_columns;
432  reference_columns.push_back("residue_type_set_name");
433  reference_columns.push_back("name");
434  ForeignKey foreign_key(foreign_key_columns, "residue_type", reference_columns, true);
435 
436  Schema table("residue_type_icoor", primary_key);
437  table.add_foreign_key(foreign_key);
438  table.add_column(icoor_sequence);
439  table.add_column(phi);
440  table.add_column(theta);
441  table.add_column(distance);
442  table.add_column(parent_atom);
443  table.add_column(angle_atom);
444  table.add_column(torsion_atom);
445 
446  table.write(db_session);
447 }
448 
449 void ResidueDatabaseIO::initialize(utility::sql_database::sessionOP db_session)
450 {
451  write_schema_to_db(db_session);
452 }
453 
455 {
456  return version_;
457 }
458 
459 
461  std::string const & residue_type_set_name,
462  core::chemical::ResidueType const & res_type,
463  utility::sql_database::sessionOP db_session)
464 {
465  std::string status_string = "SELECT * FROM residue_type WHERE residue_type_set_name = ? AND name = ?;";
466  cppdb::statement status_statement(basic::database::safely_prepare_statement(status_string,db_session));
467  status_statement.bind(1,residue_type_set_name);
468  status_statement.bind(2,res_type.name());
469  cppdb::result res(basic::database::safely_read_from_database(status_statement));
470 
471  if(res.next()) return;
472 
473  report_residue_type(residue_type_set_name, res_type, db_session);
474  report_residue_type_atom(residue_type_set_name, res_type, db_session);
475  report_residue_type_bond(residue_type_set_name, res_type, db_session);
476  report_residue_type_cut_bond(residue_type_set_name, res_type, db_session);
477  report_residue_type_chi(residue_type_set_name, res_type, db_session);
478  report_residue_type_chi_rotamer(residue_type_set_name, res_type, db_session);
479  report_residue_type_proton_chi(residue_type_set_name, res_type, db_session);
480  report_residue_type_properties(residue_type_set_name, res_type, db_session);
481  report_residue_type_variant(residue_type_set_name, res_type, db_session);
482  report_residue_type_icoor(residue_type_set_name, res_type, db_session);
483 }
484 
486  chemical::AtomTypeSetCAP atom_types,
487  chemical::ElementSetCAP elements,
488  chemical::MMAtomTypeSetCAP mm_atom_types,
489  chemical::orbitals::OrbitalTypeSetCAP orbital_atom_types,
490  std::string const & residue_type_set_name,
491  std::string const & residue_type_name,
492  utility::sql_database::sessionOP db_session
493 )
494 {
495 
496  core::chemical::ResidueTypeOP res_type(new core::chemical::ResidueType(atom_types,elements,mm_atom_types,orbital_atom_types));
497 
498 
499  read_residue_type_atom(residue_type_set_name,residue_type_name,*res_type,db_session);
500  read_residue_type(residue_type_set_name,residue_type_name,*res_type,db_session);
501  read_residue_type_bond(residue_type_set_name,residue_type_name,*res_type,db_session);
502  read_residue_type_cut_bond(residue_type_set_name,residue_type_name,*res_type,db_session);
503  read_residue_type_chi(residue_type_set_name,residue_type_name,*res_type,db_session);
504  read_residue_type_chi_rotamer(residue_type_set_name,residue_type_name,*res_type,db_session);
505  read_residue_type_properties(residue_type_set_name,residue_type_name,*res_type,db_session);
506  read_residue_type_variant(residue_type_set_name,residue_type_name,*res_type,db_session);
507  read_residue_type_icoor(residue_type_set_name,residue_type_name,*res_type,db_session);
508  res_type->finalize();
509 
510  return res_type;
511 }
512 
514  std::string residue_name,
515  core::Size atom_index,
516  utility::sql_database::sessionOP db_session
517 ) {
518 
519  std::pair<std::string,core::Size> atom_id = std::make_pair(residue_name,atom_index);
520 
521  if(atom_name_id_cache_.find(atom_id) != atom_name_id_cache_.end())
522  {
523  return atom_name_id_cache_[atom_id];
524  }
525 
526  std::string atom_query =
527  "SELECT\n"
528  " atom_index,\n"
529  " atom_name\n"
530  "FROM residue_type_atom WHERE residue_type_name=?;";
531  cppdb::statement stmt(basic::database::safely_prepare_statement(atom_query,db_session));
532  stmt.bind(1,residue_name);
533  cppdb::result res(basic::database::safely_read_from_database(stmt));
534 
535  while(res.next())
536  {
537  std::string atom_name;
538  core::Size atom_index;
539 
540  res >> atom_index >> atom_name;
541 
542  std::pair<std::string,core::Size> new_atom_id = std::make_pair(residue_name,atom_index);
543  atom_name_id_cache_.insert(std::make_pair(new_atom_id,atom_name));
544 
545  }
546 
547  return atom_name_id_cache_[atom_id];
548 
549 
550 }
551 
553  std::string const & residue_type_set_name,
554  ResidueType const & res_type,
555  utility::sql_database::sessionOP db_session
556 ) {
557 
558  std::stringstream name1; name1 << res_type.name1();
559 
560  int lower_terminus(-1), upper_terminus(-1);
561  if(res_type.is_polymer()){
562  if(!res_type.is_lower_terminus()) lower_terminus = res_type.lower_connect_atom();
563  if(!res_type.is_upper_terminus()) upper_terminus = res_type.upper_connect_atom();
564  }
565 
566  std::string statement_string = "INSERT INTO residue_type (residue_type_set_name, name, version, name3, name1, aa, lower_connect, upper_connect, nbr_atom, nbr_radius, rotamer_library) VALUES (?,?,?,?,?,?,?,?,?,?,?);";
567  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
568 
569  stmt.bind(1,residue_type_set_name);
570  stmt.bind(2,res_type.name());
571  stmt.bind(3,version_);
572  stmt.bind(4,res_type.name3());
573  stmt.bind(5,name1.str());
574  stmt.bind(6,res_type.aa());
575  stmt.bind(7,lower_terminus);
576  stmt.bind(8,upper_terminus);
577  stmt.bind(9,res_type.nbr_atom());
578  stmt.bind(10,res_type.nbr_radius());
579  stmt.bind(11,res_type.get_RotamerLibraryName());
580  basic::database::safely_write_to_database(stmt);
581 
582 }
583 
584 /// @detail this needs to get called after read_residue_type_atom
586  std::string const & residue_type_set_name,
587  std::string const & residue_type_name,
588  core::chemical::ResidueType & res_type,
589  utility::sql_database::sessionOP db_session)
590 {
591  std::string residue_type_statement =
592  "SELECT\n"
593  " version,\n"
594  " name3,\n"
595  " name1,\n"
596  " aa,\n"
597  " lower_connect,\n"
598  " upper_connect,\n"
599  " nbr_atom,\n"
600  " nbr_radius,\n"
601  " rotamer_library\n"
602  "FROM residue_type\n"
603  "WHERE residue_type_set_name = ? AND name = ?;";
604  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_type_statement,db_session));
605  stmt.bind(1,residue_type_set_name);
606  stmt.bind(2,residue_type_name);
607  cppdb::result res(basic::database::safely_read_from_database(stmt));
608 
609  if(!res.next())
610  {
611  utility_exit_with_message("could not find residue "+residue_type_name+" in "+residue_type_set_name);
612  }
613 
614  core::Real version;
615  std::string name3, name1;
616  core::Size aa;
617  int lower_connect, upper_connect;
618  core::Size nbr_atom;
619  std::string rotamer_library;
620  core::Real nbr_radius;
621 
622  res >>
623  version >>
624  name3 >>
625  name1 >>
626  aa >>
627  lower_connect >>
628  upper_connect >>
629  nbr_atom >>
630  nbr_radius >>
631  rotamer_library;
632  if(version != version_)
633  {
634  utility_exit_with_message("Version mismatch between Residue Database and Executable");
635  }
636 
637  res_type.name(residue_type_name);
638  res_type.name3(name3);
639  res_type.name1(name1[0]);
640  res_type.aa(name_from_aa(static_cast<AA>(aa)));
641 
642  if(lower_connect > 0 )
643  {
644  res_type.set_lower_connect_atom(get_atom_name_from_database_atom_index(residue_type_name,lower_connect,db_session));
645  }
646 
647  if(upper_connect > 0 )
648  {
649  res_type.set_upper_connect_atom(get_atom_name_from_database_atom_index(residue_type_name,lower_connect,db_session));
650  }
651 
652  res_type.nbr_atom(get_atom_name_from_database_atom_index(residue_type_name,nbr_atom,db_session));
653  res_type.nbr_radius(nbr_radius);
654 
655  if(rotamer_library != "")
656  {
657  utility::file::FileName rot_file( rotamer_library );
658  res_type.set_RotamerLibraryName(rotamer_library);
659  }
660 
661 }
662 
664 {
665 
666  utility::vector1<std::string> residue_names;
667 
668  std::string residue_name_statement =
669  "SELECT\n"
670  " name\n"
671  "FROM residue_type;";
672  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_name_statement,db_session));
673  cppdb::result res(basic::database::safely_read_from_database(stmt));
674  while(res.next())
675  {
676  std::string name;
677  res >> name;
678  residue_names.push_back(name);
679  }
680  return residue_names;
681 }
682 
683 void
685  std::string const & residue_type_set_name,
686  ResidueType const & res_type,
687  utility::sql_database::sessionOP db_session
688 ) {
689 
690  std::string statement_string = "INSERT INTO residue_type_atom (residue_type_set_name, residue_type_name, atom_index, atom_name, atom_type_name, mm_atom_type_name, charge, is_backbone) VALUES (?,?,?,?,?,?,?,?);";
691  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
692  // AtomTypeSet?
693  for(Size i=1; i <= res_type.natoms(); ++i){
694 
695  stmt.bind(1,residue_type_set_name);
696  stmt.bind(2,res_type.name());
697  stmt.bind(3,i);
698  stmt.bind(4,res_type.atom_name(i));
699  stmt.bind(5,res_type.atom_type(i).atom_type_name());
700  stmt.bind(6,res_type.atom(i).mm_name());
701  stmt.bind(7,res_type.atom(i).charge());
702  stmt.bind(8,res_type.atom_is_backbone(i));
703  basic::database::safely_write_to_database(stmt);
704 
705  }
706 }
707 
708 void
710  std::string const & residue_type_set_name,
711  std::string const & residue_type_name,
712  core::chemical::ResidueType & res_type,
713  utility::sql_database::sessionOP db_session)
714 {
715  std::string residue_atoms_statement =
716  "SELECT\n"
717  "atom_index,\n"
718  "atom_name,\n"
719  "atom_type_name,\n"
720  "mm_atom_type_name,\n"
721  "charge\n"
722  "FROM residue_type_atom\n"
723  "WHERE residue_type_set_name = ? AND residue_type_name = ?\n"
724  "ORDER BY atom_index;";
725  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_atoms_statement,db_session));
726  stmt.bind(1,residue_type_set_name);
727  stmt.bind(2,residue_type_name);
728  cppdb::result res(basic::database::safely_read_from_database(stmt));
729 
730  while(res.next())
731  {
732  core::Size atom_index;
733  std::string atom_name, atom_type_name,mm_atom_type_name;
734  core::Real charge;
735 
736  res >> atom_index >> atom_name >> atom_type_name >> mm_atom_type_name >> charge;
737  res_type.add_atom(atom_name,atom_type_name,mm_atom_type_name,charge);
738  }
739 
740 }
741 
742 void
744  std::string const & residue_type_set_name,
745  ResidueType const & res_type,
746  utility::sql_database::sessionOP db_session
747 ) {
748 
749  std::string statement_string = "INSERT INTO residue_type_bond (residue_type_set_name, residue_type_name, atom1, atom2, bond_type) VALUES (?,?,?,?,?);";
750  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
751  for(Size atm=1; atm <= res_type.natoms(); ++atm){
752  AtomIndices const & neighbors(res_type.bonded_neighbor(atm));
753  utility::vector1<BondName> const & types(res_type.bonded_neighbor_types(atm));
754  for(Size nbr=1; nbr <= neighbors.size(); ++nbr){
755  if(atm >= neighbors[nbr]) continue;
756 
757  stmt.bind(1,residue_type_set_name);
758  stmt.bind(2,res_type.name());
759  stmt.bind(3,atm);
760  stmt.bind(4,neighbors[nbr]);
761  stmt.bind(5,types[nbr]);
762  basic::database::safely_write_to_database(stmt);
763  }
764  }
765 }
766 
767 void
769  std::string const & residue_type_set_name,
770  std::string const & residue_type_name,
771  core::chemical::ResidueType & res_type,
772  utility::sql_database::sessionOP db_session
773 )
774 {
775  std::string bond_statement_string =
776  "SELECT\n"
777  " atom1,\n"
778  " atom2,\n"
779  " bond_type\n"
780  "FROM residue_type_bond\n"
781  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
782  cppdb::statement stmt(basic::database::safely_prepare_statement(bond_statement_string,db_session));
783  stmt.bind(1,residue_type_set_name);
784  stmt.bind(2,residue_type_name);
785  cppdb::result res(basic::database::safely_read_from_database(stmt));
786  while(res.next())
787  {
788  core::Size atom1, atom2, bond_type;
789  res >> atom1 >> atom2 >> bond_type;
790  std::string atom1_name = get_atom_name_from_database_atom_index(residue_type_name,atom1,db_session);
791  std::string atom2_name = get_atom_name_from_database_atom_index(residue_type_name,atom2,db_session);
792  res_type.add_bond(atom1_name,atom2_name,static_cast<core::chemical::BondName>(bond_type));
793  }
794 
795 
796 }
797 
798 void
800  std::string const & residue_type_set_name,
801  ResidueType const & res_type,
802  utility::sql_database::sessionOP db_session
803 ) {
804 
805  std::string statement_string = "INSERT INTO residue_type_cut_bond (residue_type_set_name, residue_type_name, atom1, atom2) VALUES (?,?,?,?);";
806  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
807 
808  for(Size i=1; i <= res_type.natoms(); ++i){
809  foreach(core::Size const j, res_type.cut_bond_neighbor(i)){
810  if(i>=j) continue;
811 
812 
813  stmt.bind(1,residue_type_set_name);
814  stmt.bind(2,res_type.name());
815  stmt.bind(3,i);
816  stmt.bind(4,j);
817  basic::database::safely_write_to_database(stmt);
818  }
819  }
820 }
821 
822 void
824  std::string const & residue_type_set_name,
825  std::string const & residue_type_name,
826  core::chemical::ResidueType & res_type,
827  utility::sql_database::sessionOP db_session
828 )
829 {
830  std::string cut_bond_statement_string =
831  "SELECT\n"
832  " atom1,\n"
833  " atom2\n"
834  "FROM residue_type_cut_bond\n"
835  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
836  cppdb::statement stmt(basic::database::safely_prepare_statement(cut_bond_statement_string,db_session));
837  stmt.bind(1,residue_type_set_name);
838  stmt.bind(2,residue_type_name);
839  cppdb::result res(basic::database::safely_read_from_database(stmt));
840  while(res.next())
841  {
842  core::Size atom1, atom2;
843  res >> atom1 >> atom2;
844  std::string atom1_name = get_atom_name_from_database_atom_index(residue_type_name,atom1,db_session);
845  std::string atom2_name = get_atom_name_from_database_atom_index(residue_type_name,atom2,db_session);
846  res_type.add_cut_bond(atom1_name,atom2_name);
847  }
848 }
849 
850 
851 void
853  std::string const & residue_type_set_name,
854  ResidueType const & res_type,
855  utility::sql_database::sessionOP db_session
856 ) {
857 
858  std::string statement_string = "INSERT INTO residue_type_chi (residue_type_set_name, residue_type_name, atom1, atom2, atom3, atom4, chino) VALUES (?,?,?,?,?,?,?);";
859  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
860 
861  for(Size i=1; i <= res_type.nchi(); ++i){
862  AtomIndices const & chi_atoms(res_type.chi_atoms(i));
863 
864  stmt.bind(1,residue_type_set_name);
865  stmt.bind(2,res_type.name());
866  stmt.bind(3,chi_atoms[1]);
867  stmt.bind(4,chi_atoms[2]);
868  stmt.bind(5,chi_atoms[3]);
869  stmt.bind(6,chi_atoms[4]);
870  stmt.bind(7,i);
871  basic::database::safely_write_to_database(stmt);
872  }
873 
874 }
875 
876 
877 void
879  std::string const & residue_type_set_name,
880  std::string const & residue_type_name,
881  core::chemical::ResidueType & res_type,
882  utility::sql_database::sessionOP db_session
883 )
884 {
885  std::string chi_statement_string =
886  "SELECT\n"
887  " chino,\n"
888  " atom1,\n"
889  " atom2,\n"
890  " atom3,\n"
891  " atom4\n"
892  "FROM residue_type_chi\n"
893  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
894  cppdb::statement stmt(basic::database::safely_prepare_statement(chi_statement_string,db_session));
895  stmt.bind(1,residue_type_set_name);
896  stmt.bind(2,residue_type_name);
897  cppdb::result res(basic::database::safely_read_from_database(stmt));
898  while(res.next())
899  {
900  core::Size chino;
901  core::Size atom1, atom2, atom3, atom4;
902  res >> chino >> atom1 >> atom2 >> atom3 >> atom4;
903  std::string atom1_name = get_atom_name_from_database_atom_index(residue_type_name,atom1,db_session);
904  std::string atom2_name = get_atom_name_from_database_atom_index(residue_type_name,atom2,db_session);
905  std::string atom3_name = get_atom_name_from_database_atom_index(residue_type_name,atom3,db_session);
906  std::string atom4_name = get_atom_name_from_database_atom_index(residue_type_name,atom4,db_session);
907  res_type.add_chi(chino,atom1_name,atom2_name,atom3_name,atom4_name);
908  }
909 }
910 
911 
912 void
914  std::string const & residue_type_set_name,
915  ResidueType const & res_type,
916  utility::sql_database::sessionOP db_session
917 ) {
918 
919  std::string statement_string = "INSERT INTO residue_type_chi_rotamer (residue_type_set_name, residue_type_name, chino, mean, sdev) VALUES (?,?,?,?,?);";
920  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
921  for(Size chi=1; chi <= res_type.nchi(); ++chi){
922  std::pair<Real, Real> mean_sdev;
923  foreach(mean_sdev, res_type.chi_rotamers(chi)){
924 
925  stmt.bind(1,residue_type_set_name);
926  stmt.bind(2,res_type.name());
927  stmt.bind(3,chi);
928  stmt.bind(4,mean_sdev.first);
929  stmt.bind(5,mean_sdev.second);
930  basic::database::safely_write_to_database(stmt);
931  }
932  }
933 }
934 
935 void
937  std::string const & residue_type_set_name,
938  std::string const & residue_type_name,
939  core::chemical::ResidueType & res_type,
940  utility::sql_database::sessionOP db_session)
941 {
942  std::string chi_rotamer_statement_string =
943  "SELECT\n"
944  " chino,\n"
945  " mean,\n"
946  " sdev\n"
947  "FROM residue_type_chi_rotamer\n"
948  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
949  cppdb::statement stmt(basic::database::safely_prepare_statement(chi_rotamer_statement_string,db_session));
950  stmt.bind(1,residue_type_set_name);
951  stmt.bind(2,residue_type_name);
952  cppdb::result res(basic::database::safely_read_from_database(stmt));
953  while(res.next())
954  {
955  core::Size chino;
956  core::Real mean, stdev;
957 
958  res >> chino >> mean >> stdev;
959  res_type.add_chi_rotamer(chino,mean,stdev);
960  }
961 }
962 
963 void
965  std::string const & residue_type_set_name,
966  ResidueType const & res_type,
967  utility::sql_database::sessionOP db_session
968 ) {
969 
970  std::string statement_string = "INSERT INTO residue_type_proton_chi (residue_type_set_name, residue_type_name, chino, sample, is_extra) VALUES (?,?,?,?,?);";
971  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
972  for(Size proton_chi=1; proton_chi <= res_type.n_proton_chi(); ++proton_chi){
973  Size const chi(res_type.proton_chi_2_chi(proton_chi));
974  foreach(Real const sample, res_type.proton_chi_samples(proton_chi)){
975 
976  stmt.bind(1,residue_type_set_name);
977  stmt.bind(2,res_type.name());
978  stmt.bind(3,chi);
979  stmt.bind(4,sample);
980  stmt.bind(5,false);
981  basic::database::safely_write_to_database(stmt);
982 
983  foreach(Real const extra_sample, res_type.proton_chi_extra_samples(proton_chi)){
984 
985  stmt.bind(1,residue_type_set_name);
986  stmt.bind(2,res_type.name());
987  stmt.bind(3,chi);
988  stmt.bind(4,sample - extra_sample);
989  stmt.bind(5,true);
990  basic::database::safely_write_to_database(stmt);
991 
992  stmt.bind(1,residue_type_set_name);
993  stmt.bind(2,res_type.name());
994  stmt.bind(3,chi);
995  stmt.bind(4,sample + extra_sample);
996  stmt.bind(5,true);
997  basic::database::safely_write_to_database(stmt);
998 
999  }
1000  }
1001  }
1002 }
1003 
1004 void
1006  std::string const & residue_type_set_name,
1007  std::string const & residue_type_name,
1009  utility::sql_database::sessionOP db_session)
1010 {
1011  std::string proton_chi_statement_string =
1012  "SELECT\n"
1013  " chino,\n"
1014  " sample,\n"
1015  " is_extra\n"
1016  "FROM residue_type_proton_chi\n"
1017  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
1018  cppdb::statement stmt(basic::database::safely_prepare_statement(proton_chi_statement_string,db_session));
1019  stmt.bind(1,residue_type_set_name);
1020  stmt.bind(2,residue_type_name);
1021  cppdb::result res(basic::database::safely_read_from_database(stmt));
1022 
1023  std::map<core::Size,utility::vector1< core::Real > > samples;
1024  std::map<core::Size,utility::vector1< core::Real > > extra_samples;
1025 
1026  while(res.next())
1027  {
1028  core::Size chino, is_extra;
1029  core::Real sample;
1030 
1031  res >> chino >> sample >> is_extra;
1032  if(is_extra)
1033  {
1034  if(extra_samples.find(chino) == extra_samples.end())
1035  {
1036  utility::vector1< core::Real> extra_sample_vect;
1037  extra_sample_vect.push_back(sample);
1038  extra_samples.insert(std::make_pair(chino,extra_sample_vect));
1039  }else
1040  {
1041  extra_samples[chino].push_back(sample);
1042  }
1043  }else
1044  {
1045  //we've never seen this chino, add it to the map
1046  if(samples.find(chino) == samples.end())
1047  {
1048  utility::vector1< core::Real> sample_vect;
1049  sample_vect.push_back(sample);
1050  samples.insert(std::make_pair(chino,sample_vect));
1051  }else
1052  {
1053  samples[chino].push_back(sample);
1054  }
1055  }
1056  }
1057 }
1058 
1059 void
1061  std::string const & residue_type_set_name,
1062  ResidueType const & res_type,
1063  utility::sql_database::sessionOP db_session
1064 ) {
1065  foreach(std::string const & property, res_type.properties()){
1066 
1067  cppdb::statement stmt = (*db_session)
1068  << "INSERT INTO residue_type_property (residue_type_set_name, residue_type_name, property) VALUES (?,?,?);"
1069  << residue_type_set_name
1070  << res_type.name()
1071  << property;
1072  basic::database::safely_write_to_database(stmt);
1073 
1074  }
1075 }
1076 
1077 void
1079  std::string const & residue_type_set_name,
1080  std::string const & residue_type_name,
1081  core::chemical::ResidueType & res_type,
1082  utility::sql_database::sessionOP db_session)
1083 {
1084  std::string residue_property_statement_string =
1085  "SELECT\n"
1086  " property\n"
1087  "FROM residue_type_property\n"
1088  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
1089  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_property_statement_string,db_session));
1090  stmt.bind(1,residue_type_set_name);
1091  stmt.bind(2,residue_type_name);
1092  cppdb::result res(basic::database::safely_read_from_database(stmt));
1093  while(res.next())
1094  {
1095  std::string property;
1096  res >> property;
1097  res_type.add_property(property);
1098  }
1099 }
1100 
1101 
1102 
1103 void
1105  std::string const & residue_type_set_name,
1106  ResidueType const & res_type,
1107  utility::sql_database::sessionOP db_session
1108 ) {
1109  foreach(std::string const & variant_type, res_type.variant_types()){
1110 
1111  cppdb::statement stmt = (*db_session)
1112  << "INSERT INTO residue_type_variant_type (residue_type_set_name, residue_type_name, variant_type) VALUES (?,?,?);"
1113  << residue_type_set_name
1114  << res_type.name()
1115  << variant_type;
1116  basic::database::safely_write_to_database(stmt);
1117 
1118  }
1119 }
1120 
1121 void
1123  std::string const & residue_type_set_name,
1124  std::string const & residue_type_name,
1125  core::chemical::ResidueType & res_type,
1126  utility::sql_database::sessionOP db_session)
1127 {
1128  std::string residue_variant_statement_string =
1129  "SELECT\n"
1130  " variant_type\n"
1131  "FROM residue_type_variant_type\n"
1132  "WHERE residue_type_set_name = ? AND residue_type_name = ?;";
1133  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_variant_statement_string,db_session));
1134  stmt.bind(1,residue_type_set_name);
1135  stmt.bind(2,residue_type_name);
1136  cppdb::result res(basic::database::safely_read_from_database(stmt));
1137  while(res.next())
1138  {
1139  std::string variant_type;
1140  res >> variant_type;
1141  res_type.add_variant_type(variant_type);
1142 
1143  }
1144 }
1145 
1146 void
1148  std::string const & residue_type_set_name,
1149  core::chemical::ResidueType const & res_type,
1150  utility::sql_database::sessionOP db_session)
1151 {
1152  std::string statement_string = "INSERT INTO residue_type_icoor (residue_type_set_name, residue_type_name, child_atom, icoor_sequence, phi, theta, distance, parent_atom, angle_atom, torsion_atom) VALUES (?,?,?,?,?,?,?,?,?,?);";
1153  cppdb::statement stmt(basic::database::safely_prepare_statement(statement_string,db_session));
1154 
1155  for(Size i=1; i <= res_type.natoms(); ++i){
1156 
1157  AtomICoor atom_icoor(res_type.icoor(i));
1158  stmt.bind(1,residue_type_set_name);
1159  stmt.bind(2,res_type.name());
1160  stmt.bind(3,res_type.atom_name(i));
1161  stmt.bind(4,atom_icoor.index());
1162  stmt.bind(5,atom_icoor.phi());
1163  stmt.bind(6,atom_icoor.theta());
1164  stmt.bind(7,atom_icoor.d());
1165  //We need to do this because the AtomICoor doesn't store atom names, just numbers, but it takes atom names as its constructor
1166  if(atom_icoor.stub_atom1().type() == ICoorAtomID::INTERNAL)
1167  {
1168  stmt.bind(8,res_type.atom_name(atom_icoor.stub_atom1().atomno()));
1169  }else if(atom_icoor.stub_atom1().type() == ICoorAtomID::CONNECT)
1170  {
1171  stmt.bind(8,"CONNECT");
1172  }else if(atom_icoor.stub_atom2().type() == ICoorAtomID::POLYMER_LOWER)
1173  {
1174  stmt.bind(8,"LOWER");
1175  }else
1176  {
1177  //should be POLYMER_UPPER
1178  stmt.bind(8,"UPPER");
1179  }
1180 
1181  if(atom_icoor.stub_atom2().type() == ICoorAtomID::INTERNAL)
1182  {
1183  stmt.bind(9,res_type.atom_name(atom_icoor.stub_atom2().atomno()));
1184  }else if(atom_icoor.stub_atom1().type() == ICoorAtomID::CONNECT)
1185  {
1186  stmt.bind(9,"CONNECT");
1187  }else if(atom_icoor.stub_atom2().type() == ICoorAtomID::POLYMER_LOWER)
1188  {
1189  stmt.bind(9,"LOWER");
1190  }else
1191  {
1192  //should be POLYMER_UPPER
1193  stmt.bind(9,"UPPER");
1194  }
1195 
1196  if(atom_icoor.stub_atom3().type() == ICoorAtomID::INTERNAL)
1197  {
1198  stmt.bind(10,res_type.atom_name(atom_icoor.stub_atom3().atomno()));
1199  }else if(atom_icoor.stub_atom1().type() == ICoorAtomID::CONNECT)
1200  {
1201  stmt.bind(10,"CONNECT");
1202  }else if(atom_icoor.stub_atom2().type() == ICoorAtomID::POLYMER_LOWER)
1203  {
1204  stmt.bind(10,"LOWER");
1205  }else
1206  {
1207  //should be POLYMER_UPPER
1208  stmt.bind(10,"UPPER");
1209  }
1210  basic::database::safely_write_to_database(stmt);
1211 
1212  }
1213 }
1214 
1215 void
1217  std::string const & residue_type_set_name,
1218  std::string const & residue_type_name,
1219  core::chemical::ResidueType & res_type,
1220  utility::sql_database::sessionOP db_session)
1221 {
1222 
1223  //ORDER BY icoor_sequence exists because the icoor assignment code is order dependent
1224  std::string residue_variant_statement_string =
1225  "SELECT\n"
1226  " child_atom,\n"
1227  " phi,\n"
1228  " theta,\n"
1229  " distance,\n"
1230  " parent_atom,\n"
1231  " angle_atom,\n"
1232  " torsion_atom\n"
1233  "FROM residue_type_icoor\n"
1234  "WHERE residue_type_set_name = ? AND residue_type_name = ?\n"
1235  "ORDER BY icoor_sequence;";
1236  cppdb::statement stmt(basic::database::safely_prepare_statement(residue_variant_statement_string,db_session));
1237  stmt.bind(1,residue_type_set_name);
1238  stmt.bind(2,residue_type_name);
1239 
1240  std::map< std::string, Vector > rsd_xyz;
1241 
1242  cppdb::result res(basic::database::safely_read_from_database(stmt));
1243  while(res.next())
1244  {
1245  std::string child_atom_name, parent_atom_name,angle_atom_name,torsion_atom_name;
1246  core::Real phi, theta, distance;
1247  res >> child_atom_name >> phi >> theta >> distance >> parent_atom_name >>angle_atom_name >>torsion_atom_name;
1248 
1249  //This code mostly came from core/chemical/residue_io. It should probably be extracted to a util function
1250  if(child_atom_name == parent_atom_name)
1251  {
1252  assert( rsd_xyz.empty() ); // first atom
1253  rsd_xyz[child_atom_name] = Vector(0.0);
1254  }else if( child_atom_name == angle_atom_name)
1255  {
1256  assert( rsd_xyz.size() == 1 && rsd_xyz.count( parent_atom_name ) ); // second atom
1257  rsd_xyz[ child_atom_name ] = Vector( distance, 0.0, 0.0 );
1258  }else
1259  {
1260  Vector torsion_xyz;
1261  if ( child_atom_name == torsion_atom_name ) {
1262  assert( rsd_xyz.size() == 2 );
1263  assert( rsd_xyz.count( parent_atom_name ) );
1264  assert( rsd_xyz.count( angle_atom_name ) ); // third atom
1265  torsion_xyz = Vector( 1.0, 1.0, 0.0 );
1266  } else {
1267  assert( rsd_xyz.count( parent_atom_name ) && rsd_xyz.count( angle_atom_name ) && rsd_xyz.count( torsion_atom_name ) );
1268  torsion_xyz = rsd_xyz[ torsion_atom_name ];
1269  }
1270  kinematics::Stub const stub( rsd_xyz[ parent_atom_name ], rsd_xyz[ angle_atom_name ], torsion_xyz );
1271  rsd_xyz[ child_atom_name ] = stub.spherical( phi, theta, distance );
1272  }
1273 
1274  // set atom_base
1275  if ( child_atom_name != "UPPER" && child_atom_name != "LOWER" && child_atom_name.substr(0,4) != "CONN" ) {
1276  // atom base only valid for genuine atoms of this residue
1277  if ( child_atom_name == parent_atom_name ) {
1278  // root of the tree
1279  if ( res_type.natoms() == 1 ) {
1280  res_type.set_atom_base( child_atom_name, child_atom_name ); // 1st child of root atom
1281  } else {
1282  res_type.set_atom_base( child_atom_name, angle_atom_name ); // 1st child of root atom
1283  }
1284  } else {
1285  res_type.set_atom_base( child_atom_name, parent_atom_name );
1286  }
1287  }
1288 
1289  // set icoor
1290  res_type.set_icoor( child_atom_name, phi, theta, distance, parent_atom_name, angle_atom_name, torsion_atom_name );
1291  }
1292 
1293  for ( Size i=1; i<= res_type.natoms(); ++i ) {
1294  std::string name( res_type.atom_name(i) );
1295  assert( rsd_xyz.count( name ) );
1296  res_type.set_ideal_xyz( name, rsd_xyz[ name ] );
1297  }
1298 
1299 
1300 }
1301 
1302 
1303 
1304 
1305 }
1306 }