Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FlexbbRotamerSets.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/flexpack/FlexbbRotamerSets.cc
11 /// @brief
12 /// @author Florian Richter (floric@u.washington.edu)
13 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com), oct 08
14 
15 
17 
21 
22 /// Project headers
24 #include <core/fragment/Frame.hh>
25 #include <core/graph/Graph.hh>
26 #include <core/pose/Pose.hh>
27 #include <core/scoring/Energies.hh>
31 #include <basic/options/option.hh>
32 #include <basic/options/keys/packing.OptionKeys.gen.hh>
36 #include <basic/Tracer.hh>
37 
38 // needed for windows build
39 #ifdef WIN32
40 #include <platform/types.hh> // ssize_t
41 #endif
42 
43 /// Package headers
44 #include <core/io/pdb/pose_io.hh>
45 #include <utility/string_util.hh>
46 
47 
48 /// ObjexxFCL headers
49 #include <ObjexxFCL/format.hh>
50 #include <ObjexxFCL/FArray4D.hh>
51 
52 // C++
53 #include <fstream>
54 
57 #include <utility/vector1.hh>
58 
59 
60 namespace protocols {
61 namespace flexpack {
62 namespace rotamer_set {
63 
64 using namespace ObjexxFCL;
65 
66 basic::Tracer TR( "protocols.flexpack.rotamer_set.FlexbbRotamerSets" );
67 
69  : task_( task )
70 {
71 }
72 
74 
75 
77 FlexbbRotamerSets::rotset_for_moltenres( Size molten_resid, Size bbconf /*= 1*/ ) const
78 { return rotamers_[ molten_resid ][ bbconf ]; }
79 
81 FlexbbRotamerSets::rotset_for_residue( Size resid, Size bbconf /*= 1*/ ) const
82 { return rotamers_[ resid_2_moltenres_[ resid] ][ bbconf ]; }
83 
85 FlexbbRotamerSets::rotamer_for_residue( Size resid, Size rotindex_on_residue ) const
86 { return rotamer_for_moltenres( resid_2_moltenres_[ resid ], rotindex_on_residue ); }
87 
88 
89 /// @brief function to set up the internal mapping data structures
90 void
92  core::pose::Pose const & pose,
94 )
95 {
96 
97  //to accurately build the different backbone conformations, we need a modifiable copy of the pose
98  core::pose::Pose helper_pose = pose;
99 
100 
101  core::Size num_frames = frames.size();
102  nbbconfs_ = 0;
103 
104  flexsegment_span_.resize( num_frames );
105  nbbconfs_for_flexseg_.resize( num_frames );
106 
107 
108  // Question: when are we setting up the other data structures?
109  // either this function or in a separate one (just like set_task in the fixbb RotamerSet)
110  // or in a different one? for now, the following segment of code is a duplication of
111  // the code in core::pack::rotamer_set::RotamerSets::set_task
112  nmoltenres_ = task_->num_to_be_packed();
113  total_residue_ = pose.total_residue();
114 
117  rotamers_.resize( nmoltenres_ );
124 
125 
127 
128  core::Size count_moltenres(0);
129  for( core::Size ii = 1; ii<= total_residue_; ++ii){
130 
131  if( task_->pack_residue( ii ) ){
132  count_moltenres++;
133  resid_2_moltenres_[ ii ] = count_moltenres;
134  moltenres_2_resid_[ count_moltenres ] = ii;
135  moltenres_2_flexseg_[ count_moltenres ] = 0; //will be filled with the correct values later
136  }
137  else resid_2_moltenres_[ ii ] = 0;
138  }
139  assert( count_moltenres == nmoltenres_ );
140 
141  //for( utility::vector1< core::fragment::FrameOP >::const_iterator frame_it = frames.begin(); frame_it != frames.end(); ++frame_it ){
142  for( core::Size frame_count = 1; frame_count <= num_frames; ++frame_count ){
143 
144  core::Size cur_frame_start( frames[ frame_count ]->start() ), cur_frame_end( frames[ frame_count]->end() );
145 
146  flexsegment_span_[ frame_count ] = std::pair< core::Size, core::Size>( cur_frame_start, cur_frame_end ) ;
147  nbbconfs_for_flexseg_[ frame_count ] = frames[ frame_count ]->nr_frags();
148  nbbconfs_ += nbbconfs_for_flexseg_[ frame_count ];
149 
150  for( core::Size frameres_count = cur_frame_start; frameres_count <= cur_frame_end; ++frameres_count){
151 
152  moltenres_2_flexseg_[ resid_2_moltenres_[ frameres_count ] ] = frame_count;
153 
154  }
155 
156  //now we need to translate the information in the fragments of this frame into actual 3D residue coordinates
157  for( core::Size frag_count = 1; frag_count <= frames[ frame_count ]->nr_frags(); ++frag_count ){
159  build_residue_vector_from_fragment( helper_pose, frames[ frame_count ], frag_count, fragment_res );
160 
161  for( core::Size rescount = cur_frame_start; rescount <= cur_frame_end; ++rescount ){
162  conformations_for_flexible_segments_[ resid_2_moltenres_[ rescount ] ].push_back( fragment_res[ rescount - cur_frame_start + 1] );
163  }
164 
165  } //iterator over all fragments
166 
167  } //iterator over all frames
168 
169  //finally, we have to fill the conformations_for_flexseg array for all moltenres that are not part of a frame
170  for( core::Size ii = 1; ii <= nmoltenres_; ++ii){
171 
172  if( conformations_for_flexible_segments_[ ii ].size() == 0 ) { //means this was not part of any fragment
174  }
175  } //loop over moltenres
176 
177 } //set_frames
178 
181 {
182  return nbbconfs_;
183 }
184 
185 
186 
187 /// @brief function to figure out the flexpack neighbor graph, see core::pack::create_packer_graph for fixbb version
188 /// @brief problem: Ca/Cb not fixed, so we need to add a certain distance to the interaction energies. this distance
189 /// @brief will depend on the average deviation of the CBs in the flexible segments for each residue
192  core::pose::Pose const & pose,
193  core::scoring::ScoreFunction const & sfxn,
195 ) const
196 {
197 
199 
200  for( core::Size i = 1; i <= total_residue_; ++i ) {
201  if( resid_2_moltenres_[ i ] != 0 ) residue_radii[ i ] += determine_res_cb_deviation( pose, resid_2_moltenres_[ i ] );
202  }
203 
204  return core::pack::create_packer_graph( pose, sfxn, task, total_residue_, residue_radii );
205 
206 } //flexpack_neighbor_graph
207 
208 
209 /// @brief function to determine the maximum deviation in the position of Cbs for the bbconfs of one residue
210 /// This should use the "neighbor atom" instead of CB... CB exist for neither GLY nor non-protein residues.
213  core::pose::Pose const & pose,
214  core::Size const moltenres
215 ) const
216 {
217 
218  core::Distance max_sq_dist( 0.0 );
219  core::Size const resid = moltenres_2_resid_[ moltenres ];
220 
221  core::PointPosition cur_xyz = pose.residue( resid ).xyz( pose.residue( resid ).nbr_atom() );
222 
223  //utility::vector1< core::conformation::ResidueCOP > const & confs_this_position = conformations_for_flexible_segments[ resid ];
224 
226  res_it != conformations_for_flexible_segments_[ moltenres ].end(); ++res_it){
227 
228  core::Distance sq_dist = cur_xyz.distance_squared( (*res_it)->xyz( (*res_it)->nbr_atom() ) );
229 
230  if( sq_dist > max_sq_dist ) max_sq_dist = sq_dist;
231  }
232 
233  return sqrt( max_sq_dist);
234 
235 } //determine_res_cb_deviation
236 
237 
238 
239 void
241  core::pose::Pose const & pose,
242  core::scoring::ScoreFunction const & sfxn,
243  core::graph::Graph const & flexpack_neighbor_graph
244 )
245 {
246 
247  for( core::Size ii = 1; ii <= nmoltenres_; ++ii){
248 
249  core::Size cur_resid = moltenres_2_resid_[ ii ];
250  //for( core::Size jj = 1; jj<= conformations_for_flexible_segments_[ cur_resid ].size(); ++jj ){
252  res_it != conformations_for_flexible_segments_[ ii ].end(); ++res_it){
253 
254  FlexbbRotamerSetOP rotset = new FlexbbRotamerSet();
255  rotset->set_owner( this );
256  rotset->set_resid( cur_resid );
257  rotset->set_existing_residue( *res_it );
258 
259  rotset->build_rotamers(pose, sfxn, *task_, &flexpack_neighbor_graph );
260 
261  rotamers_[ ii ].push_back( rotset );
262  //TR << "Built: " << rotset->num_rotamers() << " for moltenres " << ii
263  // << " (resid: " << cur_resid << ") ";
264  //if ( conformations_for_flexible_segments_[ ii ].size() != 1 ) {
265  // TR << " backone # " << rotamers_[ii].size();
266  //}
267  //TR << std::endl;;
268  } //loop over bb conformations for moltenres
269 
270  } //loop over moltenres
271 
273 
274 
275  if( basic::options::option[basic::options::OptionKeys::packing::dump_rotamer_sets].user() ){
276 
277  this->dump_pdbs( pose, "flex_rotamers");
278  }
279 
280 } //build_rotamers
281 
282 
283 
284 
285 /// @brief function to dump all the rotamer sets out to pdb files, more or less only used for debugging
286 /// @brief currently modelled after the core/pack/rotamer_set/RotamerSets dump_pdb function
287 void
288 FlexbbRotamerSets::dump_pdbs( core::pose::Pose const & pose, std::string const & filename_base ) const
289 {
290 
291  // model 0 -- just the non-moving residues
292  // model N -- Nth rotamer from each set
293  using ObjexxFCL::fmt::I;
294 
295  std::string fix_filename = filename_base + "_fixbb.pdb";
296 
297  // open file
298  std::ofstream base_out( fix_filename.c_str() );
299 
300  // write model 0
301  Size model(0), atom_counter(0);
302 
303  base_out << "MODEL" << I(9,model) << '\n';
304  for ( Size i=1; i<= pose.total_residue(); ++i ) {
305  if ( task_->pack_residue(i) ) continue;
306  core::io::pdb::dump_pdb_residue( pose.residue(i), atom_counter, base_out );
307  }
308  base_out << "ENDMDL\n";
309 
310 
311  while ( true ) {
312  bool found_a_rotamer( false );
313  ++model;
314 
315  for ( Size ii=1; ii<= nmoltenres_; ++ii ) {
316  //Size const resid( moltenres_2_resid[ ii ] );
317  FlexbbRotamerSetOP rotset( rotamers_[ ii ][1] );
318  if ( rotset->num_rotamers() >= model ) {
319  if ( !found_a_rotamer ) {
320  found_a_rotamer = true;
321  base_out << "MODEL" << I(9,model) << '\n';
322  }
323  core::io::pdb::dump_pdb_residue( *(rotset->rotamer( model )), atom_counter, base_out );
324  }
325  } //loop over moltenres
326  if ( found_a_rotamer ) {
327  base_out << "ENDMDL\n";
328  } else {
329  break;
330  }
331  }
332  base_out.close();
333 
334  //now the stuff for flexible segments
335  core::Size curconf(2);
336 
337  bool all_confs_covered(false);
338 
339  while( ! all_confs_covered ){
340 
341  std::string cur_filename = filename_base + "_flexconf" + utility::to_string( curconf - 1 ) + ".pdb";
342  std::ofstream cur_out( cur_filename.c_str() );
343 
344  all_confs_covered = true;
345 
346  model = 0;
347  atom_counter = 0;
348 
349  while( true ){
350 
351  bool found_a_rotamer( false );
352  ++model;
353 
354  for( core::Size ii =1; ii <= nmoltenres_; ++ii ){
355 
356  if( rotamers_[ ii ].size() < curconf ) continue;
357  else{
358 
359  all_confs_covered = false;
360  FlexbbRotamerSetOP rotset( rotamers_[ ii ][ curconf ] );
361 
362  if( rotset->num_rotamers() >= model ){
363  if( !found_a_rotamer ){
364  found_a_rotamer = true;
365  cur_out << "MODEL" << I(9,model) << '\n';
366  }
367  core::io::pdb::dump_pdb_residue( *(rotset->rotamer( model )), atom_counter, cur_out );
368  }
369  }
370 
371  } //loop over moltenres
372  if( found_a_rotamer ) cur_out << "ENDMDL\n";
373 
374  else break;
375  } // loop over models
376 
377  cur_out.close();
378  curconf++;
379 
380  } //loop over all confs
381 
382 
383 } //dump_pdbs
384 
387 {
388  return nrotamers_for_moltenres_[ moltres ];
389 }
390 
392 {
393  return nmoltenres_;
394 }
395 
397 {
398  return total_residue_;
399 }
400 
403 {
404  return moltenres_2_resid_[ moltres ];
405 }
406 
407 
410 {
411  return resid_2_moltenres_[ resid ];
412 }
413 
414 
417 {
418  return moltenres_for_rotamer_[ rotid ];
419 }
420 
421 
424 {
425  return moltenres_2_resid_[ moltenres_for_rotamer_[ rotid ]];
426 }
427 
428 
431 {
432  Size moltenres = moltenres_for_rotamer_[ rotid ];
433  Size rotid_on_node = rotid - nrotoffset_for_moltenres_[ moltenres ];
434  Size bb = bbconf_for_rotamer_of_moltenres_[ moltenres ][ rotid_on_node ];
435  Size rotid_on_bb = rotid - nrotoffset_for_moltenres_bbconf_[ moltenres ][ bb ];
436  return rotamers_[ moltenres ][ bb ]->rotamer( rotid_on_bb );
437 
438 }
439 
442 {
443  Size bb = bbconf_for_rotamer_of_moltenres_[ moltenres_id ][ rotamerid ];
444  Size rotid_on_bb = rotamerid + nrotoffset_for_moltenres_[ moltenres_id ] - nrotoffset_for_moltenres_bbconf_[ moltenres_id ][ bb ];
445  return rotamers_[ moltenres_id ][ bb ]->rotamer( rotid_on_bb );
446 }
447 
448 
451 {
452  return nrotoffset_for_moltenres_[ moltres ];
453 }
454 
455 
456 /// @details -- unimplemented!
459 {
460  utility_exit_with_message("UNIMPLEMENTED");
461  return 0;
462 }
463 
464 /// @brief convert moltenres rotid to id in full rotamer enumeration
465 
467 FlexbbRotamerSets::moltenres_rotid_2_rotid( core::uint /*moltenres*/, core::uint /*moltenresrotid */) const
468 {
469  utility_exit_with_message("UNIMPLEMENTED");
470  return 0;
471 }
472 
473 void
475  core::pose::Pose & pose,
477  core::Size frag_num,
479 )
480 {
481 
482  if( frame->apply( frag_num, pose ) != frame->length() ) utility_exit_with_message("unknown error when trying to apply a fragment to a pose in setting up FlexbbRotamerSets.");
483 
484  for( core::Size rescount = frame->start(); rescount <= frame->end(); ++rescount ){
485 
486  fragment_res.push_back( new core::conformation::Residue ( pose.residue( rescount ) ) );
487  }
488 } //build residue vector from fragment
489 
490 
491 
494 {
495  if( moltenres_2_flexseg_[ moltenres ] == 0 ) return 1;
496 
497  else return nbbconfs_for_flexseg_[ moltenres_2_flexseg_[ moltenres ] ];
498 }
499 
502 {
503  if( resid_2_moltenres_[ resid ] == 0 ) return 1;
504 
505  else return nbbconfs_for_moltenres( resid_2_moltenres_[ resid ] );
506 }
507 
508 //core::conformation::ResidueCOP
509 //FlexbbRotamerSets::rotamer_for_moltenres( core::Size moltenres, core::Size rotindex_on_residue ) const
510 //{
511 // core::Size bb_conf( bbconf_for_rotamer_of_moltenres_[ moltenres ][ rotindex_on_residue + nrotoffset_for_moltenres_[ moltenres ] ] );
512 // return rotamers_[ moltenres ][ bb_conf ]->rotamer( rotindex_on_residue - nrotoffset_for_moltenres_bbconf_[ moltenres ][ bb_conf ] );
513 //}
514 
515 //core::conformation::ResidueCOP
516 //FlexbbRotamerSets::rotamer( core::Size rotindex ) const
517 //{
518 // core::Size moltenres( moltenres_for_rotamer_[ rotindex ] );
519 // core::Size bb_conf( bbconf_for_rotamer_of_moltenres_[ moltenres ][ rotindex ] );
520 // return rotamers_[ moltenres ][ bb_conf ]->rotamer( rotindex - nrotoffset_for_moltenres_bbconf_[ moltenres ][ bb_conf ] );
521 //}
522 
523 void
525 {
526  nrotamers_ = 0;
527  moltenres_for_rotamer_.clear();
528 
529  for( core::Size ii = 1; ii <= nmoltenres_; ++ii){
530  nrotamers_for_moltenres_[ ii ] = 0;
531 
534  bbconf_for_rotamer_of_moltenres_[ ii ].clear();
535 
536  assert( nrots_for_moltenres_bbconf_[ ii ].size() == rotamers_[ ii ].size() );
537 
538  for( core::Size jj = 1; jj <= rotamers_[ ii ].size(); ++jj){
539 
540  core::Size cur_numrots = rotamers_[ ii ][ jj ]->num_rotamers();
541 
542  nrots_for_moltenres_bbconf_[ ii ][ jj ] = cur_numrots;
543  //TR << "nrots_for_moltenres_bbconf_[ "<<ii<<" ][ " <<jj <<" ] is " << nrots_for_moltenres_bbconf_[ ii ][ jj ] << ", ";
544  nrotamers_for_moltenres_[ ii ] += cur_numrots;
545  //TR << "nrotamers_for_moltenres_[ " << ii << " ] is " << nrotamers_for_moltenres_[ ii ] << " ; ";
546 
547  for( core::Size rot = 1; rot <= cur_numrots; ++rot ){
548  moltenres_for_rotamer_.push_back( ii );
549  bbconf_for_rotamer_of_moltenres_[ ii ].push_back( jj );
550  }
551 
552  if( jj > 1 ){
554  //TR << "nrotoffset_for_moltenres_bbconf_[ " << ii << " ][ " << jj << " ] is " << nrotoffset_for_moltenres_bbconf_[ ii ][ jj ] << std::endl;
555  }
556  else{
557  if( ii > 1 ){
559  //TR << "nrotoffset_for_moltenres_bbconf_[ " << ii << " ][ " << jj << " ] is " << nrotoffset_for_moltenres_bbconf_[ ii ][ jj ] << std::endl;
560  }
561 
562  else{
563  nrotoffset_for_moltenres_bbconf_[ ii ][ jj ] = 0;
564  //TR << "nrotoffset_for_moltenres_bbconf_[ " << ii << " ][ " << jj << " ] is " << nrotoffset_for_moltenres_bbconf_[ ii ][ jj ] << std::endl;
565  }
566  }
567 
568 
569  } //iterator over rotamer sets for moltenres
570 
571  if( ii > 1 ) nrotoffset_for_moltenres_[ ii ] = nrotoffset_for_moltenres_[ ii - 1 ] + nrotamers_for_moltenres_[ ii - 1 ] ;
572  else nrotoffset_for_moltenres_[ ii ] = 0;
573  //TR << "nrotoffset_for_moltenres_[ " << ii << " ] is " << nrotoffset_for_moltenres_[ ii ] << std::endl;
574 
575  nrotamers_ += nrotamers_for_moltenres_[ ii ];
576 
577  } //loop over moltenres
578 
579 
580 } //update_offset_data
581 
582 
583 void
585  Pose const & pose,
586  ScoreFunction const & sfxn,
587  core::graph::GraphCOP flexpack_neighbor_graph,
589 ) const
590 {
591  using namespace interaction_graph;
592 
593  /// Dispatch based on the downcast.
594  if ( dynamic_cast< OTFFlexbbInteractionGraph * > ( &flexbb_ig ) ) {
595  OTFFlexbbInteractionGraph & otfig =
596  static_cast< OTFFlexbbInteractionGraph & > ( flexbb_ig );
597 
598  compute_one_body_energies_for_otf_ig( pose, sfxn, flexpack_neighbor_graph, otfig );
599  } else {
600  assert( dynamic_cast< PrecomputedFlexbbInteractionGraph * > ( &flexbb_ig ) );
601  PrecomputedFlexbbInteractionGraph & precomp_ig =
602  static_cast< PrecomputedFlexbbInteractionGraph & > ( flexbb_ig );
603 
604  precompute_all_energies( pose, sfxn, flexpack_neighbor_graph, precomp_ig );
605  }
606 }
607 
608 void
610  Pose const & /*pose*/,
611  ScoreFunction const & /*sfxn*/,
612  core::graph::GraphCOP /*flexpack_neighbor_graph*/,
614 ) const
615 {
616  std::cout << "Yo Mama so fat!" << std::endl;
617 
618 } // precompute all energies
619 
620 
621 void
623  Pose const & pose,
624  ScoreFunction const & sfxn,
627 ) const
628 {
629  using namespace utility;
630  using namespace core::chemical;
631  using namespace core::scoring;
632  using namespace core::scoring::methods;
633 
634  /// 1. Compute energies with the static portions of the structure.
635  for ( Size ii = 1; ii <= nmoltenres_; ++ii ) {
636  compute_onebody_interactions_with_background( ii, pose, sfxn, flexpack_neighbor_graph, flexbb_ig );
637  }
638 
639  vector1< vector1< Size > > regular_representatives( nmoltenres_ );
640  vector1< vector1< Size > > proline_representatives( nmoltenres_ );
641  vector1< vector1< Size > > glycine_representatives( nmoltenres_ );
642 
643  /// 2. Collect a set of representative rotamers for each backbone conformation for each moltenresidue.
644  /// These representatives will be used for computing bb/bb and bb/sc energies. They should span the
645  /// distinct kinds of backbones; for proteins, these are a. proline backbones (with Npro atom type for
646  /// the backbone N) b. glycine backbones (with an extra 2HA where a CB would usually be) and c. regular
647  /// backbones (for the other 18 amino acids). If you wanted to design RNA/DNA hybrid molecules
648  /// and the backbone were gaining and loosing the 2' hydroxyl, between RNA and DNA substitutions
649  /// during then you would have to modify the code below as well as the code
650  /// inside the OTFFlexbbInteracionGraph
651  for ( Size ii = 1; ii <= nmoltenres_; ++ii ) {
652  Size iinbb = nbbconfs_for_moltenres( ii );
653  regular_representatives[ ii ].resize( iinbb );
654  proline_representatives[ ii ].resize( iinbb );
655  glycine_representatives[ ii ].resize( iinbb );
656  std::fill( regular_representatives[ ii ].begin(), regular_representatives[ ii ].end(), 0 );
657  std::fill( proline_representatives[ ii ].begin(), proline_representatives[ ii ].end(), 0 );
658  std::fill( glycine_representatives[ ii ].begin(), glycine_representatives[ ii ].end(), 0 );
659  for ( Size jj = 1; jj <= iinbb; ++jj ) {
660  FlexbbRotamerSetCOP jjrotset = rotamers_[ ii ][ jj ];
661  Size jjntypes = jjrotset->get_n_residue_groups();
662  bool jjregfound( false ), jjprofound( false ), jjglyfound( false );
663  for ( Size kk = 1; kk <= jjntypes; ++kk ) {
664  Size kkrep = jjrotset->get_residue_type_begin( kk );
665  AA kkaa = jjrotset->rotamer( kkrep )->aa();
666  if ( ! jjprofound && kkaa == aa_pro ) {
667  proline_representatives[ ii ][ jj ] = kkrep;
668  jjprofound = true;
669  } else if ( ! jjglyfound && kkaa == aa_gly ) {
670  glycine_representatives[ ii ][ jj ] = kkrep;
671  jjglyfound = true;
672  } else if ( ! jjregfound && kkaa != aa_gly && kkaa != aa_pro ) {
673  regular_representatives[ ii ][ jj ] = kkrep;
674  jjregfound = true;
675  }
676  if ( jjprofound && jjglyfound && jjregfound ) break;
677  }
678  }
679  }
680 
681  for ( Size ii = 1; ii <= nmoltenres_; ++ii ) {
682  Size ii_resid = moltenres_2_resid_[ ii ];
684  li = flexpack_neighbor_graph->get_node( ii_resid )->const_upper_edge_list_begin(),
685  liend = flexpack_neighbor_graph->get_node( ii_resid )->const_upper_edge_list_end();
686  li != liend; ++li ) {
687  Size const jj_resid = (*li)->get_second_node_ind();
688  Size const jj = resid_2_moltenres_[ jj_resid ];
689 
690  if( jj == 0 ) continue;
691 
693  ii, jj,
694  regular_representatives,
695  proline_representatives,
696  glycine_representatives,
697  pose, sfxn, flexbb_ig );
698  }
699  }
700 
701  /// Long range interactions
702  // Iterate across the long range energy functions and use the iterators generated
703  // by the LRnergy container object
705  lr_iter = sfxn.long_range_energies_begin(),
706  lr_end = sfxn.long_range_energies_end();
707  lr_iter != lr_end; ++lr_iter ) {
708  LREnergyContainerCOP lrec = pose.energies().long_range_container( (*lr_iter)->long_range_type() );
709  if ( !lrec || lrec->empty() ) continue; // only score non-empty energies.
710  // Potentially O(N^2) operation...
711 
712  #ifdef WIN32
713  using namespace platform;
714  #endif
715 
716  for ( uint ii = 1; ii <= nmoltenres_; ++ ii ) {
717  uint const ii_resid = moltenres_2_resid_[ ii ];
718 
720  rni = lrec->const_upper_neighbor_iterator_begin( ii_resid ),
721  rniend = lrec->const_upper_neighbor_iterator_end( ii_resid );
722  (*rni) != (*rniend); ++(*rni) ) {
723  Size const jj_resid = rni->upper_neighbor_id();
724 
725  uint const jj = resid_2_moltenres_[ jj_resid ];
726  if ( ii > jj ) continue; // compute against upper, moltenres neighbors only
727  assert( jj != 0 );
728 
729  if ( ! flexbb_ig.get_edge_exists( ii, jj ) ) {
730  flexbb_ig.add_edge( ii, jj );
731  }
732  //std::cout << "flexbb_ig.note_long_range_interactions_exist_for_edge( " << ii << ", " << jj << ");" << std::endl;
734  }
735  }
736  }
737 
738 
739 }
740 
741 /// @details Computes the backbone/backbone and backbone/sidechain energies for a
742 /// pair of molten residues. The interactions are separated into three kinds of backbone
743 /// types: regular backbones, proline backbones and glycine backbones. The different
744 /// solvation for proline's backbone nitrogen makes the proline correction necessary;
745 /// the new carbon-hbond term makes the glycine correction necessary.
746 void
748  Size lowermoltenres,
749  Size uppermoltenres,
750  utility::vector1< utility::vector1< Size > > const & regular_representatives,
751  utility::vector1< utility::vector1< Size > > const & proline_representatives,
752  utility::vector1< utility::vector1< Size > > const & glycine_representatives,
753  Pose const & pose,
754  ScoreFunction const & sfxn,
756 ) const
757 {
758  //std::cout << "ONE BODY ENERGIES: " << lowermoltenres << " " << uppermoltenres << std::endl;
759  ///bool sought_pair( lowermoltenres == 27 && uppermoltenres == 28 );
760  bool sought_pair = false;
761 
762  flexbb_ig.add_edge( lowermoltenres, uppermoltenres );
763 
764  utility::vector1< Size > const & lregrep( regular_representatives[ lowermoltenres ] );
765  utility::vector1< Size > const & uregrep( regular_representatives[ uppermoltenres ] );
766  utility::vector1< Size > const & lprorep( proline_representatives[ lowermoltenres ] );
767  utility::vector1< Size > const & uprorep( proline_representatives[ uppermoltenres ] );
768  utility::vector1< Size > const & lglyrep( glycine_representatives[ lowermoltenres ] );
769  utility::vector1< Size > const & uglyrep( glycine_representatives[ uppermoltenres ] );
770 
771  bool const sameflexseg = flexsegid_for_moltenres( lowermoltenres ) == flexsegid_for_moltenres( uppermoltenres );
772 
773  Size const lnbb = nbbconfs_for_moltenres( lowermoltenres );
774  Size const unbb = nbbconfs_for_moltenres( uppermoltenres );
775 
776  Size const lnrots = nrotamers_for_moltenres_[ lowermoltenres ];
777  Size const unrots = nrotamers_for_moltenres_[ uppermoltenres ];
778 
779  Size const BBREG = 1;
780  Size const BBPRO = 2;
781  Size const BBGLY = 3;
782  Size const NBBCLASSES = 3;
783 
784  /// 1. backbone backbone energies.
785  FArray4D< PackerEnergy > bbbb_energies( NBBCLASSES, NBBCLASSES, lnbb, unbb, 0.0 );
786 
787  utility::vector1< Size > lower_bbrepresentatives( NBBCLASSES );
788  utility::vector1< Size > upper_bbrepresentatives( NBBCLASSES );
789  for ( Size ii = 1; ii <= lnbb; ++ii ) {
790  FlexbbRotamerSetCOP iirotset( rotamers_[ lowermoltenres ][ ii ] );
791  //std::fill( lower_bbrepresentatives.begin(), lower_bbrepresentatives.end(), 0 );
792  lower_bbrepresentatives[ BBREG ] = lregrep[ ii ];
793  lower_bbrepresentatives[ BBPRO ] = lprorep[ ii ];
794  lower_bbrepresentatives[ BBGLY ] = lglyrep[ ii ];
795  for ( Size jj = 1; jj <= unbb; ++jj ) {
796  FlexbbRotamerSetCOP jjrotset( rotamers_[ uppermoltenres ][ jj ] );
797  //std::fill( upper_bbrepresentatives.begin(), upper_bbrepresentatives.end(), 0 );
798  upper_bbrepresentatives[ BBREG ] = uregrep[ jj ];
799  upper_bbrepresentatives[ BBPRO ] = uprorep[ jj ];
800  upper_bbrepresentatives[ BBGLY ] = uglyrep[ jj ];
801 
802  for ( Size kk = 1; kk <= NBBCLASSES; ++kk ) {
803  if ( lower_bbrepresentatives[ kk ] == 0 ) continue;
804  core::conformation::Residue const & kkres( * (iirotset->rotamer( lower_bbrepresentatives[ kk ] )) );
805  for ( Size ll = 1; ll <= NBBCLASSES; ++ll ) {
806  if ( upper_bbrepresentatives[ ll ] == 0 ) continue;
807  core::conformation::Residue const & llres( * (jjrotset->rotamer( upper_bbrepresentatives[ ll ] )) );
808  bbbb_energies( kk, ll, ii, jj ) =
809  core::pack::rotamer_set::RotamerSets::get_bb_bbE( pose, sfxn, kkres, llres );
810  if ( sought_pair ) {
811  std::cout << "bbbbE: " << ii << " " << jj << " " << kk << " " << ll << " " << bbbb_energies( kk, ll, ii, jj ) << std::endl;
812  }
813  }
814  }
815  }
816  }
817 
818  if ( sought_pair ) {
819  for ( Size ii = 1; ii <= lnbb; ++ii ) {
820 
821  FlexbbRotamerSetCOP iirotset( rotamers_[ lowermoltenres ][ ii ] );
822  for ( Size jj = 1; jj <= unbb; ++jj ) {
823  FlexbbRotamerSetCOP jjrotset( rotamers_[ uppermoltenres ][ jj ] );
824 
825  for ( Size kk = 1; kk <= iirotset->get_n_residue_types(); ++kk ) {
826  Size kkrep = iirotset->get_residue_type_begin( kk );
827  core::conformation::Residue const & kkrot( * iirotset->rotamer( kkrep ));
828  for ( Size ll = 1; ll <= jjrotset->get_n_residue_types(); ++ll ) {
829  Size llrep = jjrotset->get_residue_type_begin( ll );
830  core::conformation::Residue const & llrot(* jjrotset->rotamer( llrep ));
831 
832  std::cout << "BBBB: " << ii << " " << jj << " " <<
833  kkrot.aa() << " " << llrot.aa() << " e: " <<
834  core::pack::rotamer_set::RotamerSets::get_bb_bbE( pose, sfxn, kkrot, llrot ) << std::endl;
835  }
836  }
837  }
838  }
839  }
840 
841  { // scope
842  /// Sidechain/backbone energies for the lower-residue's rotamers.
843  utility::vector1< PackerEnergy > scbb_energies_lower( NBBCLASSES );
844  for ( Size ii = 1; ii <= lnrots; ++ii ) {
845  Size iibb = bbconf_for_rotamer_of_moltenres_[ lowermoltenres ][ ii ];
846  core::conformation::Residue const & iirot( *( rotamer_for_moltenres( lowermoltenres, ii )));
847  Size iibbclass = ( iirot.aa() == core::chemical::aa_pro ? BBPRO : iirot.aa() == core::chemical::aa_gly ? BBGLY : BBREG );
848 
849  for ( Size jj = 1, jje = (sameflexseg ? 1 : unbb); jj <= jje; ++jj ) {
850  Size jjbb = sameflexseg ? iibb : jj;
851  FlexbbRotamerSetCOP jjrotset( rotamers_[ uppermoltenres ][ jjbb ] );
852  upper_bbrepresentatives[ BBREG ] = uregrep[ jjbb ];
853  upper_bbrepresentatives[ BBPRO ] = uprorep[ jjbb ];
854  upper_bbrepresentatives[ BBGLY ] = uglyrep[ jjbb ];
855  std::fill( scbb_energies_lower.begin(), scbb_energies_lower.end(), 0.0f );
856  for ( Size kk = 1; kk <= NBBCLASSES; ++kk ) {
857  if ( upper_bbrepresentatives[ kk ] == 0 ) continue;
858  core::conformation::Residue const & kkrot( *(jjrotset->rotamer( upper_bbrepresentatives[ kk ] )) );
859  scbb_energies_lower[ kk ] =
860  core::pack::rotamer_set::RotamerSets::get_sc_bbE( pose, sfxn, iirot, kkrot );
861  if ( sought_pair ) {
862  std::cout << "lower bbscE: " << ii << " " << jj << " " << kk << " " << scbb_energies_lower[ kk ] << std::endl;
863  }
864 
865  }
866  if ( upper_bbrepresentatives[ BBREG ] != 0 &&
867  upper_bbrepresentatives[ BBPRO ] == 0 &&
868  upper_bbrepresentatives[ BBGLY ] == 0 ) {
869  // Neither a proline correction nor a glycine correction.
870  // Set the bb/bb and bb/sc energies.
871  flexbb_ig.set_ProCorrection_values_for_edge( lowermoltenres, uppermoltenres,
872  lowermoltenres, ii, jjbb,
873  bbbb_energies( iibbclass, BBREG, iibb, jjbb ), 0.0,
874  scbb_energies_lower[ BBREG ], 0.0 );
875  }
876  if ( upper_bbrepresentatives[ BBPRO ] != 0 ) {
877  flexbb_ig.set_ProCorrection_values_for_edge( lowermoltenres, uppermoltenres,
878  lowermoltenres, ii, jjbb,
879  bbbb_energies( iibbclass, BBREG, iibb, jjbb ), bbbb_energies( iibbclass, BBPRO, iibb, jjbb ),
880  scbb_energies_lower[ BBREG ], scbb_energies_lower[ BBPRO ] );
881 
882  }
883  if ( upper_bbrepresentatives[ BBGLY ] != 0 ) {
884  flexbb_ig.set_GlyCorrection_values_for_edge( lowermoltenres, uppermoltenres,
885  lowermoltenres, ii, jjbb,
886  bbbb_energies( iibbclass, BBREG, iibb, jjbb ), bbbb_energies( iibbclass, BBGLY, iibb, jjbb ),
887  scbb_energies_lower[ BBREG ], scbb_energies_lower[ BBGLY ] );
888  }
889  }
890  }
891  }
892 
893  { // scope
894  /// Sidechain/backbone energies for the upper-residue's rotamers.
895  utility::vector1< PackerEnergy > scbb_energies_upper( NBBCLASSES );
896  for ( Size ii = 1; ii <= unrots; ++ii ) {
897  Size iibb = bbconf_for_rotamer_of_moltenres_[ uppermoltenres ][ ii ];
898  core::conformation::Residue const & iirot( *( rotamer_for_moltenres( uppermoltenres, ii )));
899  Size iibbclass = ( iirot.aa() == core::chemical::aa_pro ? BBPRO : iirot.aa() == core::chemical::aa_gly ? BBGLY : BBREG );
900 
901  for ( Size jj = 1, jje = sameflexseg ? 1 : lnbb; jj <= jje; ++jj ) {
902  Size jjbb = sameflexseg ? iibb : jj;
903  FlexbbRotamerSetCOP jjrotset( rotamers_[ lowermoltenres ][ jjbb ] );
904  lower_bbrepresentatives[ BBREG ] = lregrep[ jjbb ];
905  lower_bbrepresentatives[ BBPRO ] = lprorep[ jjbb ];
906  lower_bbrepresentatives[ BBGLY ] = lglyrep[ jjbb ];
907  std::fill( scbb_energies_upper.begin(), scbb_energies_upper.end(), 0.0 );
908 
909  for ( Size kk = 1; kk <= NBBCLASSES; ++kk ) {
910  if ( lower_bbrepresentatives[ kk ] == 0 ) continue;
911  core::conformation::Residue const & kkrot( *(jjrotset->rotamer( lower_bbrepresentatives[ kk ] )) );
912  scbb_energies_upper[ kk ] =
913  core::pack::rotamer_set::RotamerSets::get_sc_bbE( pose, sfxn, iirot, kkrot );
914  if ( sought_pair ) {
915  std::cout << "upper bbscE: " << ii << " " << jj << " " << kk << " " << scbb_energies_upper[ kk ] <<
916  " aa " << iirot.aa() <<
917  " coord: " << iirot.xyz( iirot.nheavyatoms() ).x() <<
918  " " << iirot.xyz( iirot.nheavyatoms() ).y() <<
919  " " << iirot.xyz( iirot.nheavyatoms() ).z() << std::endl;
920  }
921  }
922  if ( lower_bbrepresentatives[ BBREG ] != 0 &&
923  lower_bbrepresentatives[ BBPRO ] == 0 &&
924  lower_bbrepresentatives[ BBGLY ] == 0 ) {
925  // Neither a proline correction nor a glycine correction.
926  // Set the bb/bb and bb/sc energies.
927  flexbb_ig.set_ProCorrection_values_for_edge( lowermoltenres, uppermoltenres,
928  uppermoltenres, ii, jjbb,
929  bbbb_energies( BBREG, iibbclass, jjbb, iibb ), 0.0,
930  scbb_energies_upper[ BBREG ], 0.0 );
931  }
932  if ( lower_bbrepresentatives[ BBPRO ] != 0 ) {
933  flexbb_ig.set_ProCorrection_values_for_edge( lowermoltenres, uppermoltenres,
934  uppermoltenres, ii, jjbb,
935  bbbb_energies( BBREG, iibbclass, jjbb, iibb ), bbbb_energies( BBPRO, iibbclass, jjbb, iibb ),
936  scbb_energies_upper[ BBREG ], scbb_energies_upper[ BBPRO ] );
937 
938  }
939  if ( lower_bbrepresentatives[ BBGLY ] != 0 ) {
940  flexbb_ig.set_GlyCorrection_values_for_edge( lowermoltenres, uppermoltenres,
941  uppermoltenres, ii, jjbb,
942  bbbb_energies( BBREG, iibbclass, jjbb, iibb ), bbbb_energies( BBGLY, iibbclass, jjbb, iibb ),
943  scbb_energies_upper[ BBREG ], scbb_energies_upper[ BBGLY ] );
944  }
945  }
946  }
947  }
948 }
949 
950 void
952  Size moltenres,
953  Pose const & pose,
954  ScoreFunction const & sfxn,
957 ) const
958 {
959 
960 
961  utility::vector1< PackerEnergy > all_one_body_energies( nrotamers_for_moltenres( moltenres ), 0.0 );
962  for ( Size ii = 1, iie = nbbconfs_for_moltenres( moltenres ); ii <= iie; ++ii ) {
963 
964 
965  FlexbbRotamerSetCOP iirotset( rotamers_[ moltenres ][ ii ] );
966 
967  /// Build the rotamer tries now so that they are ready for rotamer/bg
968  /// energy calculations. Delete the tries at the end of this operation!
969  /// Warning -- this must absolutely be rethought when precomputing all
970  /// energies upfront.
971  sfxn.prepare_rotamers_for_packing( pose, const_cast< FlexbbRotamerSet & > (*iirotset) );
972 
973  Size const iinrots = iirotset->num_rotamers();
974  utility::vector1< PackerEnergy > ii_one_body_energies( iinrots );
975  iirotset->compute_one_body_energies(
976  pose, sfxn, *task_, flexpack_neighbor_graph, ii_one_body_energies );
977  Size const ii_offset = nrotoffset_for_moltenres_bbconf_[ moltenres ][ ii ] - nrotoffset_for_moltenres_[ moltenres ];
978  for ( Size jj = 1; jj <= iinrots; ++jj ) {
979  //if ( moltenres == 17 && jj == 11 ) { std::cout << " OneBodyEnergy: " << jj << " " << ii_offset << " " << jj+ii_offset << " " << ii_one_body_energies[ jj ] << std::endl; }
980  //if ( moltenres == 27 ) { std::cout << "OneBodyEnergy " << moltenres << " " << jj + ii_offset << " " << ii_one_body_energies[ jj ] << std::endl;}
981  all_one_body_energies[ jj + ii_offset ] = ii_one_body_energies[ jj ];
982  }
983 
984  /// For the sake of reducing memory load, clear the tries out as soon as
985  /// the one body energies are finished being calculated.
986  for ( Size jj = 1; jj <= core::scoring::methods::n_energy_methods; ++jj ) {
987  (const_cast< FlexbbRotamerSet & > (*iirotset)).store_trie( jj, 0 );
988  }
989  }
990 
991  flexbb_ig.add_to_nodes_one_body_energy( moltenres, all_one_body_energies );
992 
993 }
994 
995 
996 }
997 }
998 }
999 
1000