Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ChemicalManager.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 /// @begin ChemicalManager
11 ///
12 /// @brief
13 /// Chemical manager class
14 ///
15 /// @detailed
16 /// The Chemical Manager is a singleton class, which means that it can only been initialized once (exist once in memory). Once initialized,
17 /// you can call it by simply access it via:
18 ///
19 /// core::chemical::AtomTypeSetCAP atom_types =
20 /// core::chemical::ChemicalManager::get_instance()->atom_type_set("fa_standard");
21 ///
22 /// You can substitute AtomTypeSet, with whatever is seen below (residue_type_set, mm_atom_type_set, orbital_type_set).
23 /// In the below functions, the "tag_in" refers to fullatom, centroid, which basically tells what type of set to load in.
24 /// The chemical manager will call functions within the AtomTypeSet, MMAtomTypeSet, ResidueTypeSet, etc etc. The classes type set
25 /// reads in files from the database to create atom types, residue types, and mmatom types. The information from those files are stored
26 /// in the type class.
27 ///
28 ///
29 ///
30 /// @authors
31 /// Andrew Leaver-Fay (leaverfa@email.unc.edu)
32 /// Steven Combs - comments
33 ///
34 ///
35 /// @last_modified December 6 2010
36 /////////////////////////////////////////////////////////////////////////
37 
38 
48 #include <core/chemical/util.hh>
49 
50 #include <utility/vector1.hh>
51 #include <utility/io/izstream.hh>
52 // Project headers
53 #include <basic/database/sql_utils.hh>
54 #include <basic/database/open.hh>
55 #include <basic/Tracer.hh>
56 
57 #include <basic/options/option.hh>
58 #include <utility/file/file_sys_util.hh>
59 #include <utility/sql_database/DatabaseSessionManager.hh>
60 #include <utility/sql_database/types.hh>
61 
62 
63 // option key includes
64 
65 #include <basic/options/keys/in.OptionKeys.gen.hh>
66 #include <basic/options/keys/corrections.OptionKeys.gen.hh>
67 #include <basic/options/keys/rna.OptionKeys.gen.hh>
68 
70 
71 
72 
73 namespace core {
74 namespace chemical {
75 
76 static basic::Tracer tr("core.chemical.ChemicalManager");
77 
78 /// @brief set initial value as no instance
79 ChemicalManager* ChemicalManager::instance_( 0 );
80 
81 /// @brief static function to get the instance of ( pointer to) this singleton class
83 {
84  if ( instance_ == 0 )
85  {
86  instance_ = new ChemicalManager();
87  }
88  return instance_;
89 }
90 
91 /// @brief private constructor to guarantee the singleton
93 
94 /// @details if the tag is not in the map, input it from a database file and add it
95 /// to the map for future look-up.
98 {
99  AtomTypeSets::const_iterator iter( atom_type_sets_.find( tag ) );
100  if ( iter == atom_type_sets_.end() ) {
101  // read from file
102  std::string const directory( basic::database::full_name( "chemical/atom_type_sets/"+tag+"/" ) );
103  AtomTypeSetOP new_set( new AtomTypeSet( directory ) );
104  // optionally add extra parameters from files given on the command line (see util.hh)
106  // optionally add extra parameters from files given on the command line (see util.hh)
108  // store the new atom type set
109  iter = atom_type_sets_.insert( std::make_pair( tag, new_set ) ).first;
110  }
111  return iter->second();
112 }
113 
114 /// @details if the tag is not in the map, input it from a database file and add it
115 /// to the map for future look-up.
118 {
119  ElementSets::const_iterator iter( element_sets_.find( tag ) );
120  if ( iter == element_sets_.end() ) {
121  // read from file
122  std::string const filename( basic::database::full_name( "chemical/element_sets/"+tag+"/element_properties.txt" ));
123  ElementSetOP new_set( new ElementSet() );
124  new_set->read_file( filename );
125  iter = element_sets_.insert( std::make_pair( tag, new_set ) ).first;
126  }
127  return iter->second();
128 }
129 
130 /// @details if the tag is not in the map, input it from a database file and add it
131 /// to the map for future look-up.
134 {
135  OrbitalTypeSets::const_iterator iter( orbital_type_sets_.find( tag ) );
136  if ( iter == orbital_type_sets_.end() ) {
137  // read from file
138  std::string const directory( basic::database::full_name( "chemical/orbital_type_sets/"+tag+"/" ) );
139  orbitals::OrbitalTypeSetOP new_set( new orbitals::OrbitalTypeSet( directory ) );
140  iter = orbital_type_sets_.insert( std::make_pair( tag, new_set ) ).first;
141  }
142  return iter->second();
143 }
144 
145 /// @details if the tag is not in the map, input it from a database file and add it
146 /// to the map for future look-up.
149 {
150  MMAtomTypeSets::const_iterator iter( mm_atom_type_sets_.find( tag ) );
151  if ( iter == mm_atom_type_sets_.end() ) {
152  // read from file
153  std::string const filename( basic::database::full_name( "chemical/mm_atom_type_sets/"+tag+"/mm_atom_properties.txt" ));
154  MMAtomTypeSetOP new_set( new MMAtomTypeSet() );
155  new_set->read_file( filename );
156  iter = mm_atom_type_sets_.insert( std::make_pair( tag, new_set ) ).first;
157  }
158  return iter->second();
159 }
160 
161 
162 // /// @details if the tag is not in the map, input it from a database file and add it
163 // /// to the map for future look-up.
164 //CSDAtomTypeSetCAP
165 //ChemicalManager::csd_atom_type_set( std::string const & tag )
166 //{
167 // CSDAtomTypeSets::const_iterator iter( csd_atom_type_sets_.find( tag ) );
168 // if ( iter == csd_atom_type_sets_.end() ) {
169  // read from file
170 // std::string const filename( basic::database::full_name( "chemical/csd_atom_type_sets/"+tag+"/csd_atom_properties.txt" ));
171 // CSDAtomTypeSetOP new_set( new CSDAtomTypeSet() );
172 // new_set->read_file( filename );
173 // iter = csd_atom_type_sets_.insert( std::make_pair( tag, new_set ) ).first;
174 // }
175 // return iter->second();
176 //}
177 
178 
179 
180 
181 ///@ details if the tag is not in the map, input it from a database file and add it
182 ///to the map for future look-up.
185 {
186 
187  using namespace basic;
188 
189  bool const use_corrected_rna_geo = basic::options::option[ basic::options::OptionKeys::rna::corrected_geo ];
190  if (use_corrected_rna_geo && tag == "rna") {
191  tag = "rna_phenix";
192  }
193 
194  ResidueTypeSets::const_iterator iter( residue_type_sets_.find( tag ) );
195  if ( iter == residue_type_sets_.end() ) {
196  // Look for additional residue .params files specified on the cmd line
197  std::vector< std::string > extra_params_files;
198  std::vector< std::string > extra_patch_files;
199  std::vector<core::chemical::ResidueTypeOP> extra_residues;
200 
201  if(tag == FA_STANDARD) {
202 
203  //this whole thing is desperately in need of some method extraction
204  utility::options::FileVectorOption & fvec
205  = basic::options::option[ basic::options::OptionKeys::in::file::extra_res_fa ];
206  for(Size i = 1, e = fvec.size(); i <= e; ++i) {
207  utility::file::FileName fname = fvec[i];
208  extra_params_files.push_back(fname.name());
209  }
210 
211  utility::options::PathVectorOption & pvec
212  = basic::options::option[basic::options::OptionKeys::in::file::extra_res_path];
213  // convert Pathname->string->char*, glob it, convert char*->string
214  for(Size i=1, e= pvec.size(); i<=e; i++){
216  std::string directory=pvec[i].name();
217 
218  utility::file::list_dir(directory, files);
219  tr.Debug<< std::endl;
220  for(size_t j=1; j<= files.size(); j++){
221  if (files[j].find("param")!=std::string::npos){
222  tr.Debug << files[j]<< ", ";
223  std::string path= directory+'/'+files[j];
224  extra_params_files.push_back(path);
225  }
226  }
227  tr.Debug<< std::endl;
228  }
229 
230  utility::options::PathVectorOption & pvec_batch
231  = basic::options::option[basic::options::OptionKeys::in::file::extra_res_batch_path];
232  for(Size i=1, e= pvec_batch.size(); i<=e; i++){
234  std::string directory=pvec_batch[i].name();
235 
236  utility::file::list_dir(directory, subdirs);
237  tr.Debug<< std::endl;
238  for(size_t j=1; j<= subdirs.size();++j)
239  {
240  if(subdirs[j] == "." || subdirs[j] == "..")
241  {
242  continue;
243  }
245  utility::file::list_dir(directory+"/"+subdirs[j],files);
246  for(size_t k=1; k<= files.size(); k++){
247  if (files[k].find("param")!=std::string::npos){
248  tr.Debug << files[k]<< ", ";
249  std::string path= directory+'/'+subdirs[j]+'/'+files[k];
250  extra_params_files.push_back(path);
251  }
252  }
253  }
254 
255  }
256 
257  utility::options::FileVectorOption & mdlvec
258  = basic::options::option[basic::options::OptionKeys::in::file::extra_res_mol];
259  core::chemical::AtomTypeSetCAP atom_types = atom_type_set("fa_standard");
260  core::chemical::ElementSetCAP elements = element_set("fa_standard");
261  core::chemical::MMAtomTypeSetCAP mm_atom_types = mm_atom_type_set("fa_standard");
262  core::chemical::orbitals::OrbitalTypeSetCAP orbital_types = orbital_type_set("fa_standard");
263  for(Size i=1, e = mdlvec.size(); i <= e;++i)
264  {
265  utility::file::FileName filename = mdlvec[i];
266  core::chemical::sdf::MolFileParser parser(filename.name());
267  parser.parse_mol_file(atom_types, elements, mm_atom_types, orbital_types);
268  extra_residues.push_back(parser.GetResidueTypeOP());
269  }
270 
271  if(basic::options::option[basic::options::OptionKeys::in::file::extra_res_database].user())
272  {
273  utility::sql_database::DatabaseMode::e database_mode(
274  utility::sql_database::database_mode_from_name(
275  basic::options::option[basic::options::OptionKeys::in::file::extra_res_database_mode]));
276  std::string database_name(basic::options::option[basic::options::OptionKeys::in::file::extra_res_database]);
277  std::string database_pq_schema(basic::options::option[basic::options::OptionKeys::in::file::extra_res_pq_schema]);
278 
279 
280  utility::sql_database::sessionOP db_session(
281  basic::database::get_db_session(database_mode, database_name, database_pq_schema));
282 
283  ResidueDatabaseIO residue_database_interface;
284 
285  if(basic::options::option[basic::options::OptionKeys::in::file::extra_res_database_resname_list].user())
286  {
287  utility::file::FileName residue_list = basic::options::option[basic::options::OptionKeys::in::file::extra_res_database_resname_list];
288  utility::io::izstream residue_name_file(residue_list);
289  std::string residue_name;
290  while(residue_name_file >> residue_name)
291  {
292  //residue_name_file >> residue_name;
293  //tr <<residue_name <<std::endl;
294  ResidueTypeOP new_residue(
295  residue_database_interface.read_residuetype_from_database(
296  atom_types,
297  elements,
298  mm_atom_types,
299  orbital_types,
300  "fa_standard",
301  residue_name,
302  db_session));
303  extra_residues.push_back(new_residue);
304  }
305 
306  }else
307  {
308  utility::vector1<std::string> residue_names_in_database( residue_database_interface.get_all_residues_in_database(db_session));
309  for(Size index =1; index <= residue_names_in_database.size();++index)
310  {
311  ResidueTypeOP new_residue(
312  residue_database_interface.read_residuetype_from_database(
313  atom_types,
314  elements,
315  mm_atom_types,
316  orbital_types,
317  "fa_standard",
318  residue_names_in_database[index],
319  db_session));
320  extra_residues.push_back(new_residue);
321  }
322  }
323  }
324 
325  // Patches
326  utility::options::FileVectorOption & pfvec
327  = basic::options::option[ basic::options::OptionKeys::in::file::extra_patch_fa ];
328  for(Size i = 1, e = pfvec.size(); i <= e; ++i) {
329  extra_patch_files.push_back( pfvec[i].name());
330  }
331 
332  } else if(tag == CENTROID) {
333  utility::options::FileVectorOption & fvec
334  = basic::options::option[ basic::options::OptionKeys::in::file::extra_res_cen ];
335  for(Size i = 1, e = fvec.size(); i <= e; ++i) {
336  utility::file::FileName fname = fvec[i];
337  extra_params_files.push_back(fname.name());
338  }
339  // Patches
340  utility::options::FileVectorOption & pfvec
341  = basic::options::option[ basic::options::OptionKeys::in::file::extra_patch_cen ];
342  for(Size i = 1, e = pfvec.size(); i <= e; ++i) {
343  extra_patch_files.push_back( pfvec[i].name());
344  }
345  }
346 
347  // generically specify extra res (not necessarily part of fa_standard) -- will get added to
348  // any and every residue_type_set instantiated.
349  utility::options::FileVectorOption & fvec
350  = basic::options::option[ basic::options::OptionKeys::in::file::extra_res ];
351  for(Size i = 1, e = fvec.size(); i <= e; ++i) {
352  utility::file::FileName fname = fvec[i];
353  extra_params_files.push_back(fname.name());
354  }
355 
356  // read from file
357  tr.Debug << "CHEMICAL_MANAGER: read residue types: " << tag << std::endl;
358  // redirecting to new icoor folder
359  std::string temp_str( basic::database::full_name( "chemical/residue_type_sets/"+tag ) );
360  if(tag == FA_STANDARD) {
361  if (basic::options::option[basic::options::OptionKeys::corrections::chemical::icoor_05_2009]) {
362  temp_str += "_05.2009_icoor";
363  }
364  }
365  temp_str += "/";
366 
367 
368  std::string const directory( temp_str );
369  ResidueTypeSetOP new_set( new ResidueTypeSet( tag, directory, extra_params_files, extra_patch_files ) );
370  ResidueTypeSetCAP new_setCAP(*new_set);
371 
372  for(core::Size index =0 ;index < extra_residues.size();++index)
373  {
374  //tr << extra_residues[index]->name3() <<std::endl;
375  new_set->add_residue_type(extra_residues[index]);
376  extra_residues[index]->residue_type_set(new_setCAP);
377  ResidueTypeSetCAP new_set_cap(new_set.get());
378  extra_residues[index]->residue_type_set(new_set_cap);
379  }
380 
381  iter = residue_type_sets_.insert( std::make_pair( tag, new_set ) ).first;
382 
383  }
384  return iter->second();
385 }
386 
387 
388 ///@ details if the tag is not in the map, input it from a database file and add it
389 ///to the map for future look-up.
392 {
393  // trigger initialization if necessary:
394  residue_type_set( tag );
395 
396  return *( residue_type_sets_.find( tag )->second );
397 }
398 
399 /// @details if the tag is not in the map, input it from a database file and add it
400 /// to the map for future look-up.
403 {
404  IdealBondLengthSets::const_iterator iter( ideal_bond_length_sets_.find( tag ) );
405  if ( iter == ideal_bond_length_sets_.end() ) {
406  // read from file
407  std::string const filename( basic::database::full_name( "chemical/atom_type_sets/"+tag+"/ideal_bond_lengths.txt" ));
408  IdealBondLengthSetOP new_set( new IdealBondLengthSet() );
409  new_set->read_file( filename );
410  iter = ideal_bond_length_sets_.insert( std::make_pair( tag, new_set ) ).first;
411  }
412  return iter->second();
413 }
414 
415 // global data
416 /// @brief tag name for querying fullatom chemical type set.
417 std::string const FA_STANDARD( "fa_standard" );
418 /// @brief tag name for querying centroid chemical type set.
419 std::string const CENTROID( "centroid" );
420 /// @brief tag name for querying coarse-grained chemical type set.
421 std::string const COARSE_TWO_BEAD( "coarse_two_bead" );
422 /// @brief tag name for querying hybrid fullatom+centroid chemical type set.
423 std::string const HYBRID_FA_STANDARD_CENTROID( "hybrid_fa_standard_centroid" );
424 /// @brief tag name for querying RNA chemical type set.
425 std::string const RNA = "rna";
426 /// @brief tag name for querying COARSE_RNA chemical type set.
427 std::string const COARSE_RNA( "coarse_rna" );
428 
429 } // namespace core
430 } // namespace chemical
431 
432 
433 /// THIS TURNED OUT TO BE MORE TROUBLE THAN IT WAS WORTH: but maybe some day...
434 
435 
436 /**
437 /// @details Duplicate a ResidueTypeSet, preparatory to modifying it in some way DOES NOT DUPLICATE ITS ATOMTYPESETS
438 /// Uses ResidueTypeSet::clone()
439 
440 void
441 ChemicalManager::copy_residue_type_set(
442  std::string const & old_name,
443  std::string const & new_name
444  )
445 {
446  residue_type_set( old_name ); // triggers initialization if necessary
447  if ( residue_type_sets_.find( new_name ) ) {
448  utility_exit_with_message( "new name is already being used!" );
449  }
450  residue_type_sets_.insert( std::make_pair( new_name, residue_type_sets_.find( old_name )->second->clone() ) );
451 }
452 
453 /// @details Duplicate an AtomTypeSet, preparatory to modifying it in some way
454 /// Uses AtomTypeSet::clone()
455 
456 void
457 ChemicalManager::copy_atom_type_set(
458  std::string const & old_name,
459  std::string const & new_name
460  )
461 {
462  atom_type_set( old_name ); // triggers initialization if necessary
463  if ( atom_type_sets_.find( new_name ) ) {
464  utility_exit_with_message( "new name is already being used!" );
465  }
466  atom_type_sets_.insert( std::make_pair( new_name, atom_type_sets_.find( old_name )->second->clone() ) );
467 }
468 **/
469