Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
import_pose.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file core/import_pose/import_pose.cc
12 ///
13 /// @brief various functions to construct Pose object(s) from PDB(s)
14 /// @author Sergey Lyskov
15 
16 // Unit headers
18 
20 
21 #include <core/types.hh>
22 
23 #include <core/pose/Pose.hh>
24 #include <core/pose/util.hh>
25 
28 #include <core/chemical/AA.hh>
33 // AUTO-REMOVED #include <core/conformation/ResidueFactory.hh>
34 // AUTO-REMOVED #include <core/chemical/residue_io.hh>
36 
37 // AUTO-REMOVED #include <core/scoring/Energies.hh>
38 
39 // AUTO-REMOVED #include <core/id/AtomID_Map.Pose.hh>
40 // AUTO-REMOVED #include <core/id/AtomID_Mask.hh>
41 
42 // AUTO-REMOVED #include <ObjexxFCL/format.hh>
43 // AUTO-REMOVED #include <ObjexxFCL/ObjexxFCL.hh>
44 
46 #include <core/pack/optimizeH.hh>
47 // AUTO-REMOVED #include <core/io/raw_data/DisulfideFile.hh>
48 #include <core/pose/PDBInfo.hh>
49 
52 #include <core/io/pdb/file_data.hh>
54 
55 #include <basic/Tracer.hh>
56 
57 #include <ObjexxFCL/string.functions.hh>
58 
59 // Utility headers
60 #include <utility/exit.hh>
61 #include <utility/string_util.hh>
62 #include <utility/io/izstream.hh>
63 // AUTO-REMOVED #include <utility/io/ozstream.hh>
64 
65 #include <utility/vector1.hh>
66 #include <boost/foreach.hpp>
67 
68 //Auto Headers
69 #define foreach BOOST_FOREACH
70 /// A temporary copy of the pose_from_pdb code from the demo directory.
71 /// Will be phased out in favor of file_data routines soon.
72 ///
73 
74 namespace core {
75 namespace import_pose {
76 
77 using core::Size;
78 using core::SSize;
79 
80 using basic::T;
81 using basic::Error;
82 using basic::Warning;
83 
84 using namespace core::io::pdb;
85 using namespace ObjexxFCL;
86 
87 basic::Tracer TR("core.import_pose.import_pose");
88 
89 using utility::vector1;
90 
91 //
92 void
94  std::string const & s,
95  pose::Pose & pose,
96  io::pdb::FileData const &,
97  bool read_fold_tree
98 )
99 {
100  ImportPoseOptions options;
101  read_additional_pdb_data( s, pose, options, read_fold_tree );
102 }
103 
104 void
106  std::string const & s,
107  pose::Pose & pose,
108  ImportPoseOptions const & options,
109  bool read_fold_tree
110 )
111 {
112 
113  if ( (!read_fold_tree) && (!options.fold_tree_io()) ) return;
114 
115  // split on newlines
117  Size start=0, i=0;
118  while(start < s.size()) {
119  if( s[i] == '\n' || s[i] == '\r' /* || i==s.size()-1 */) {
120  lines.push_back( std::string(s.begin()+start, s.begin()+i) );
121  start = i+1;
122  }
123  i++;
124  if( i == s.size() ) {
125  lines.push_back( std::string(s.begin()+start, s.begin()+i) );
126  break;
127  }
128  }
129 
130  //
131  for ( Size i=1; i<= lines.size(); ++i ) {
132  std::string const & line( lines[i] );
133 
134  // look for fold_tree info
135  if ( line.size() >= 16 && line.substr(0,16) == "REMARK FOLD_TREE" ) {
136  std::istringstream l( line );
137  std::string tag;
139  l >> tag >> f;
140  if ( !l.fail() && Size(f.nres()) == pose.total_residue() ) {
141  TR << "setting foldtree from pdb file: " << f << std::endl;
142  pose.fold_tree( f );
143  } else {
144  TR.Fatal << "pose_io:: foldtree io failure: " << line << ' ' << pose.total_residue()
145  << ' ' << f << std::endl;
146  utility_exit();
147  }
148  }
149  }
150 
151 }
152 
153 
154 pose::PoseOP pose_from_pdb(std::string const & filename, bool read_fold_tree)
155 {
156  pose::PoseOP pose = new pose::Pose();
157  pose_from_pdb( *pose, filename, read_fold_tree);
158  return pose;
159 }
160 
161 
162 pose::PoseOP pose_from_pdb(chemical::ResidueTypeSet const & residue_set, std::string const & filename, bool read_fold_tree)
163 {
164  pose::PoseOP pose = new pose::Pose();
165  pose_from_pdb( *pose, residue_set, filename, read_fold_tree);
166  return pose;
167 }
168 
169 void
171  pose::Pose & pose,
172  chemical::ResidueTypeSet const & residue_set,
173  std::string const & filenames_string,
174  bool read_fold_tree
175 )
176 {
177  ImportPoseOptions options;
178  pose_from_pdb(pose, residue_set, filenames_string, options, read_fold_tree);
179 }
180 
181 void
183  pose::Pose & pose,
184  chemical::ResidueTypeSet const & residue_set,
185  std::string const & filenames_string,
186  ImportPoseOptions const & options,
187  bool read_fold_tree
188 )
189 {
190  utility::vector1<std::string> filenames = utility::split(filenames_string);
191 
192  std::string res;
193 
194  foreach(std::string filename, filenames){
195  utility::io::izstream file( filename );
196  if (!file) {
197  TR.Error << "PDB File:" << filename << " not found!" << std::endl;
198  utility_exit_with_message( "Cannot open PDB file \"" + filename + "\"" );
199  } else {
200  TR.Debug << "read file: " << filename << std::endl;
201  }
202  utility::slurp( file, res );
203  }
204 
205  //fpd If the conformation is not of type core::Conformation, reset it
206  if ( !pose.conformation().same_type_as_me( conformation::Conformation(), true ) ) {
208  }
209 
211  if ( fd.filename == "" ) {
212  fd.filename = utility::join(filenames, "_");
213  }
214  build_pose(fd, pose, residue_set, options);
215 
216  // set secondary structure for centroid PDBs
217  if ( residue_set.name() == core::chemical::CENTROID ) {
219  }
220 
221  // check for foldtree info
222  read_additional_pdb_data( res, pose, options, read_fold_tree );
223 }
224 
225 void
227  core::pose::Pose & pose,
228  std::string const & filename,
229  bool read_fold_tree
230 ) {
231  ImportPoseOptions options;
232  pose_from_pdb( pose, filename, options, read_fold_tree );
233 }
234 
235 void
237  pose::Pose & pose,
238  std::string const & filename,
239  ImportPoseOptions const & options,
240  bool read_fold_tree
241 ) {
242  using namespace chemical;
243 
244  ResidueTypeSetCAP residue_set( options.centroid() ?
247  );
248 
249  if ( options.rna() ) residue_set = core::chemical::ChemicalManager::get_instance()->residue_type_set( "rna" );
250 
251  core::import_pose::pose_from_pdb( pose, *residue_set, filename, options, read_fold_tree );
252 }
253 
256  utility::vector1< std::string > const & filenames,
257  bool read_fold_tree
258 ) {
259  ImportPoseOptions options;
260  return poseOPs_from_pdbs( filenames, options, read_fold_tree );
261 }
262 
265  utility::vector1< std::string > const & filenames,
266  ImportPoseOptions const & options,
267  bool read_fold_tree
268 ) {
269  using namespace chemical;
270 
271  ResidueTypeSetCAP residue_set( options.centroid() ?
274  );
275 
276  return core::import_pose::poseOPs_from_pdbs( *residue_set, filenames, options, read_fold_tree );
277 }
278 
279 /// @details Only returns fullatom poses
282  utility::vector1< std::string > const & filenames,
283  bool read_fold_tree
284 ) {
285  using namespace chemical;
286  ResidueTypeSetCAP residue_set
287  ( ChemicalManager::get_instance()->residue_type_set( FA_STANDARD ) );
288 
289  return core::import_pose::poses_from_pdbs( *residue_set, filenames, read_fold_tree );
290 }
291 
294  chemical::ResidueTypeSet const & residue_set,
295  utility::vector1< std::string > const & filenames,
296  bool read_fold_tree
297 ) {
298  using namespace chemical;
299 
300  using std::string;
301  using utility::vector1;
302  using core::pose::Pose;
303 
304  ImportPoseOptions options;
305 
306  vector1< Pose > poses;
307  typedef vector1< string >::const_iterator vec_it;
308  for ( vec_it it = filenames.begin(), end = filenames.end(); it != end; ++it ) {
309  Pose pose;
310  core::import_pose::pose_from_pdb( pose, residue_set, *it, options, read_fold_tree );
311  poses.push_back( pose );
312  }
313 
314  return poses;
315 }
316 
317 void
319  const utility::vector1<std::string>& filenames,
321 )
322 {
323  using core::pose::Pose;
324  using std::string;
325  using utility::vector1;
326  assert(poses);
327 
328  for (vector1<string>::const_iterator i = filenames.begin(); i != filenames.end(); ++i) {
329  Pose pose;
330  pose_from_pdb(pose, *i);
331  poses->push_back(pose);
332  }
333 }
334 
337  chemical::ResidueTypeSet const & residue_set,
338  utility::vector1< std::string > const & filenames,
339  ImportPoseOptions const & options,
340  bool read_fold_tree
341 ) {
342  using namespace chemical;
343 
344  using std::string;
345  using utility::vector1;
346  using core::pose::Pose;
347 
349  typedef vector1< string >::const_iterator vec_it;
350  for ( vec_it it = filenames.begin(), end = filenames.end(); it != end; ++it ) {
351  pose::PoseOP pose = new pose::Pose;
352  core::import_pose::pose_from_pdb( *pose, residue_set, *it, options, read_fold_tree );
353  poses.push_back( pose );
354  }
355 
356  return poses;
357 }
358 
359 
360 void
363  std::string const & filename,
364  bool read_fold_tree
365 )
366 {
367  using namespace chemical;
368  ResidueTypeSetCAP residue_set(
369  ChemicalManager::get_instance()->residue_type_set( FA_STANDARD )
370  );
371  core::import_pose::pose_from_pdb( poses, *residue_set, filename, read_fold_tree );
372 }
373 
374 
375 void
378  chemical::ResidueTypeSet const & residue_set,
379  std::string const & filename,
380  bool read_fold_tree
381 )
382 {
383  ImportPoseOptions options;
384  pose_from_pdb( poses, residue_set, filename, options, read_fold_tree );
385 }
386 
387 void
390  chemical::ResidueTypeSet const & residue_set,
391  std::string const & filename,
392  ImportPoseOptions const & options,
393  bool read_fold_tree
394 )
395 {
396  // Size fsize;
397  pose::Pose pose;
398  std::string all_lines, sub_lines;
399 
400  utility::io::izstream file( filename );
401  if (!file) {
402  TR.Error << "File:" << filename << " not found!" << std::endl;
403  utility_exit_with_message( "Cannot open file " + filename );
404  } else {
405  TR.Debug << "read file: " << filename << std::endl;
406  }
407 
408  utility::slurp( file, all_lines );
409  // Hacky way to make sure that we find all models. I blame society.
410  all_lines = "\n" + all_lines;
411 
412  Size pos1 = 0;
413  Size pos2 = 0;
414  Size n_models = 0;
415 
416  // count the number of poses will be reading
417  while( pos1 != std::string::npos )
418  {
419  pos1 = all_lines.find( "\nMODEL ", pos1 );
420  if ( pos1 != std::string::npos )
421  {
422  ++n_models;
423  ++pos1;
424  }
425  }
426 
427  TR.Debug << "Reading " << n_models << " poses." << std::endl;
428  // make space for all of our poses
429  if ( n_models == 0 ) n_models = 1;
430  poses.reserve( poses.size() + n_models );
431 
432  pos1 = 0;
433 
434  pos1 = all_lines.find( "\nMODEL ", pos1 );
435 
436  if ( pos1 != std::string::npos ) {
437  pos2 = 0;
438  while( pos2 != std::string::npos ) {
439  // set pos1 == "M" in MODEL
440  ++pos1;
441 
442  // pos2 = position of newline character, start somewhere after pos1
443  pos2 = all_lines.find( "\nMODEL ", pos1);
444  sub_lines = all_lines.substr( pos1, pos2-pos1 ) ;
445  pos1 = pos2;
446 
448  fd.filename = filename;
449  build_pose( fd, pose, residue_set, options );
450 
451  // check for foldtree info
452  core::import_pose::read_additional_pdb_data( sub_lines, pose, options, read_fold_tree);
453  poses.push_back( pose );
454  }
455  } else {
456  FileData fd = core::import_pose::PDB_DReader::createFileData( all_lines, options );
457  if ( fd.filename == "" ) {
458  fd.filename = filename;
459  }
460  core::import_pose::build_pose( fd, pose, residue_set, options );
461  // check for foldtree info
462  core::import_pose::read_additional_pdb_data( all_lines, pose, options, read_fold_tree);
463  poses.push_back( pose );
464  }
465 }
466 
467 void
469  pose::Pose & pose,
470  std::string const & pdbcontents,
471  ImportPoseOptions const & options,
472  std::string const & filename
473 )
474 {
475  io::pdb::FileData fd = import_pose::PDB_DReader::createFileData( pdbcontents, options );
476  fd.filename = filename;
477  chemical::ResidueTypeSetCAP residue_set
479  core::import_pose::build_pose( fd, pose, *residue_set, options );
480 
481 }
482 
483 void
485  pose::Pose & pose,
486  std::string const & pdbcontents,
487  std::string const & filename
488 )
489 {
490  ImportPoseOptions options;
491  pose_from_pdbstring( pose, pdbcontents, options, filename );
492 }
493 
494 
495 void
497  pose::Pose & pose,
498  std::string const & pdbcontents,
499  chemical::ResidueTypeSet const & residue_set,
500  std::string const & filename
501 ){
502  ImportPoseOptions options;
503  pose_from_pdbstring( pose, pdbcontents, residue_set, options, filename );
504 }
505 
506 void
508  pose::Pose & pose,
509  std::string const & pdbcontents,
510  chemical::ResidueTypeSet const & residue_set,
511  ImportPoseOptions const & options,
512  std::string const & filename
513 ){
514  io::pdb::FileData fd = import_pose::PDB_DReader::createFileData( pdbcontents, options );
515  fd.filename = filename;
516  core::import_pose::build_pose( fd, pose, residue_set, options);
517 }
518 
520  pose::Pose & pose,
521  std::istream & pdb_stream,
522  std::string const & filename,
523  ImportPoseOptions const & options
524 ){
525  std::string pdb_file_contents;
526  utility::slurp( pdb_stream, pdb_file_contents );
527 
528  chemical::ResidueTypeSetCAP residue_set( chemical::ChemicalManager::get_instance()->residue_type_set( options.residue_type_set()) );
529  pose_from_pdbstring( pose, pdb_file_contents, *residue_set, options, filename);
530  read_additional_pdb_data( pdb_file_contents, pose, options, options.read_fold_tree() );
531 }
532 
533 void
535  pose::Pose & pose,
536  std::string const & filename,
537  bool read_fold_tree
538 )
539 {
540  using namespace chemical;
541  ResidueTypeSetCAP residue_set
542  ( ChemicalManager::get_instance()->residue_type_set( CENTROID ) );
543 
544  core::import_pose::pose_from_pdb( pose, *residue_set, filename, read_fold_tree);
545 }
546 
548  io::pdb::FileData & fd,
549  pose::Pose & pose,
550  chemical::ResidueTypeSet const & residue_set
551 )
552 {
553  ImportPoseOptions options; // read from the command line
554  build_pose( fd, pose, residue_set, options);
555 }
556 
557 ///
558 /// @details Build Rosetta 3 Pose object from FileData.
559 ///
561  io::pdb::FileData & fd,
562  pose::Pose & pose,
563  chemical::ResidueTypeSet const & residue_set,
564  ImportPoseOptions const & options
565 )
566 {
567  TR.Debug << "build_pose..." << std::endl;
568  build_pose_as_is( fd, pose, residue_set, options);
569  TR.Debug << "build_pose... Ok." << std::endl;
570 }
571 
572 void build_pose_as_is2( io::pdb::FileData & fd, pose::Pose & pose, chemical::ResidueTypeSet const & residue_set, id::AtomID_Mask & missing, ImportPoseOptions const & options );
573 
574 /// @details
575 /// trying to Build pose object from pdb 'as-is'. PDB file must be _really_ clean.
576 ///
577 ///////////////////////////////////////////////////////////////////////////////
578 // "super-simple" (C) by Phil
579 //
581  io::pdb::FileData & fd,
582  pose::Pose & pose,
583  chemical::ResidueTypeSet const & residue_set,
584  ImportPoseOptions const & options
585 )
586 {
587  id::AtomID_Mask missing( false );
588 
589  io::pdb::build_pose_as_is1( fd, pose, residue_set, missing, options );
590  build_pose_as_is2( fd, pose, residue_set, missing, options );
591 }
592 
594  io::pdb::FileData & /*fd*/,
595  pose::Pose & pose,
596  chemical::ResidueTypeSet const & residue_set,
597  id::AtomID_Mask & missing,
598  ImportPoseOptions const & options
599 )
600 {
601  using namespace chemical;
602  using namespace conformation;
603  typedef std::map< std::string, double > ResidueTemps;
604  typedef std::map< std::string, ResidueTemps > Temps;
605  typedef std::map< std::string, Vector > ResidueCoords;
606  typedef std::map< std::string, ResidueCoords > Coords;
608 
609  core::pose::PDBInfoOP pdb_info( new core::pose::PDBInfo(*pose.pdb_info()) );
610 
611  if ( !options.skip_set_reasonable_fold_tree() ) {
612  set_reasonable_fold_tree( pose );
613  }
614 
615  /// optimize H if using a fullatom residue type set, and no_optH is not specified
616  if ( residue_set.name() == FA_STANDARD ) {
617  //if pack_missing_density specified, repack residues w/ missing density
618  if( options.pack_missing_sidechains() ) {
619  pack::pack_missing_sidechains( pose, missing );
620  }
621  /// optimize H if using a fullatom residue type set, and no_optH is not specified
622  if( !options.no_optH() ) {
623  pack::optimize_H_and_notify( pose, missing );
624  }
625  }
626 
627  pose.pdb_info( pdb_info );
628 }
629 
630 /// @details
631 /// All ligand residues and polymer branches have been appended by a jump. This method creates a new fold tree without
632 /// jumps through ligands, using CHEMICAL edges instead.
633 void
635 {
636  // An empty pose doesn't have jumps through ligands
637  // (Will encounter a SegFault otherwise)
638  if( pose.total_residue() == 0 ) {
639  return;
640  }
641 
642  using namespace core::chemical;
643  using namespace core::kinematics;
644  TR.Debug << "original fold tree: " << pose.fold_tree() << std::endl;
645 
646  FoldTree const & origft = pose.fold_tree();
647  FoldTree newft;
648 
649  // Copy the original fold tree edges to the new fold tree, possibly replacing
650  // some jumps to ligand residues with ligand-ligand chemical bonds.
651  // As a result, jumps must be renumbered.
652  Size last_jump_id = 0;
653  for( FoldTree::const_iterator i = origft.begin(), i_end = origft.end(); i != i_end; ++i ) {
654  Edge e = *i;
655  // Jump to a ligand residue or polymer branch?
656  if( e.is_jump() &&
657  (pose.residue_type(e.stop()).has_variant_type(BRANCH_LOWER_TERMINUS) ||
658  !pose.residue_type(e.stop()).is_polymer() ) ) {
659  Size const ii = e.stop(); // the residue at the end of the jump
660  conformation::Residue const & ii_res = pose.residue(ii);
661  // Now we'll prepare a chemical edge by first finding the connecting atoms
662  // between the two residues
663  bool found_connection_residue_in_fold_tree( false );
664  for ( Size jj = 1; jj <= pose.residue_type( ii ).n_residue_connections(); ++jj ) {
665  if(ii_res.connection_incomplete(jj)) continue; // allow incomplete connections for design
666  Size jj_res_ID= ii_res.connect_map( jj ).resid();
667  if ( jj_res_ID < ii){
668  core::conformation::Residue const & jj_res=pose.residue(jj_res_ID);
669  // Ensure that the connection is either a polymer branching or a ligand of the same chain.
671  (jj_res.chain() == ii_res.chain())) {
672  int ii_connect_ID = ii_res.connect_atom(jj_res);
673  int jj_connect_ID = jj_res.connect_atom(ii_res);
674 
675  std::string ii_connector = ii_res.atom_name(ii_connect_ID);
676  std::string jj_connector = jj_res.atom_name(jj_connect_ID);
677 
678  newft.add_edge(jj_res_ID, ii, jj_connector, ii_connector);
679 
680  found_connection_residue_in_fold_tree = true;
681  break;
682  }
683  }
684  }
685  // If we couldn't find a chemical bond to make, then we should probably just keep the jump as-is.
686  if( ! found_connection_residue_in_fold_tree ) {
687  if( pose.residue_type( ii ).n_residue_connections() > 0 ) {
688  TR.Warning << "Can't find a chemical connection for residue " << ii << " " << pose.residue_type(ii).name() << std::endl;
689  }
690  if( e.is_jump() ) e.label() = ++last_jump_id;
691  newft.add_edge(e);
692  }
693  } else { // no, just a normal peptide edge, inter-chain jump, etc.
694  if( e.is_jump() ) e.label() = ++last_jump_id;
695  newft.add_edge(e);
696  }
697  }
698 
699  runtime_assert( newft.size() > 0 || pose.total_residue() == 0 ); // valid fold tree must have 1+ edges
700 
701  TR.Debug << "new fold tree " << newft << std::endl;
702  pose.fold_tree( newft );
703 }
704 
705 } // namespace import_pose
706 } // namespace core