Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoopHashLibrary.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/loops/LoopHashLibrary.cc
11 /// @brief
12 /// @author Mike Tyka
13 /// @author Ken Jung
14 
18 #include <protocols/loops/util.hh>
20 // AUTO-REMOVED #include <protocols/frag_picker/PdbIdChunkFilter.hh>
21 
22 #include <core/kinematics/Edge.hh>
23 // AUTO-REMOVED #include <core/chemical/ResidueTypeSet.hh>
24 // AUTO-REMOVED #include <core/chemical/ResidueType.hh>
25 // AUTO-REMOVED #include <core/chemical/util.hh>
27 // AUTO-REMOVED #include <core/conformation/ResidueFactory.hh>
28 // AUTO-REMOVED #include <core/conformation/Residue.hh>
30 
32 #include <basic/options/option.hh>
34 // AUTO-REMOVED #include <core/io/pdb/pose_io.hh>
35 // AUTO-REMOVED #include <core/pose/Pose.hh>
36 // AUTO-REMOVED #include <core/pose/annotated_sequence.hh>
37 // AUTO-REMOVED #include <core/scoring/constraints/util.hh>
38 // AUTO-REMOVED #include <core/scoring/constraints/CoordinateConstraint.hh>
39 // AUTO-REMOVED #include <core/scoring/Energies.hh>
42 #include <basic/Tracer.hh>
43 #include <core/scoring/rms_util.hh>
47 #include <protocols/loops/Loop.hh>
49 #include <protocols/loops/Loops.hh>
53 #include <basic/options/keys/in.OptionKeys.gen.hh>
54 #include <basic/options/keys/out.OptionKeys.gen.hh>
55 #include <basic/options/keys/lh.OptionKeys.gen.hh>
56 #include <basic/options/keys/relax.OptionKeys.gen.hh>
57 
58 #include <numeric/random/random.hh>
59 #include <numeric/random/random_permutation.hh>
60 
61 
62 //Auto Headers
63 #include <utility/vector1.hh>
64 //Auto Headers
66 #include <core/pose/util.hh>
69 #include <cstdio>
70 
71 
72 #if defined(WIN32) || defined(__CYGWIN__)
73  #include <ctime>
74 #endif
75 
76 using namespace core::scoring;
77 using namespace core;
78 using namespace core::pose;
79 using namespace conformation;
80 using namespace kinematics;
81 using namespace protocols::frag_picker;
82 
83 
84 
85 
86 namespace protocols {
87 namespace loophash {
88 
89  static basic::Tracer TR("LoopHashLibrary");
90 
91 
92  LoopHashLibrary::LoopHashLibrary( const utility::vector1< core::Size > &init_sizes, const core::Size num_partitions, const core::Size assigned_num):
93  options( "dfpmin", 0.2, true , false ),
94  options2( "dfpmin", 0.02,true , false )
95  {
96  // create the score functions needed for the grafting process
98  do_sanity_check_ = true ;
99  for ( core::Size i=1; i<= init_sizes.size(); ++i) hash_sizes_.push_back( init_sizes[i] );
100  setup_hash_maps();
101  num_partitions_ = num_partitions;
102  assigned_num_ = assigned_num;
103  extra_ = true;
104  loopdb_range_.first = 0;
105  loopdb_range_.second = 0;
106  db_path_ = basic::options::option[basic::options::OptionKeys::lh::db_path]();
107  assigned_string_ = ""; // This is because I don't know if an initialized string is null or empty
108  // we don't want db names like "part0of20" so we increment assigned_num by one to get "part1of20"
109  if ( num_partitions_ > 1 ) assigned_string_ = ".part" + utility::to_string( assigned_num + 1 ) + "of" + utility::to_string( num_partitions_);
110 
111  }
112 
113 
114  void
116  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
117  TR << "Hash: " << *it << std::endl;
118  hash_[ *it ].mem_foot_print();
119  }
120  TR << "BackboneDB: " << bbdb_.get_mem_foot_print() << std::endl;
121  }
122 
123 
124  void
126  {
127  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
128  TR.Info << "HASHSIZE: " << *it << std::endl;
129  LoopHashMap newhashmap( *it );
130  hash_[ *it ] = newhashmap;
131  }
132  }
133 
134  LoopHashMap &
136  {
137  if( hash_.count( size ) == 1 ) return hash_[ size ];
138  // and if that's not true something is wrong
139  throw EXCN_Invalid_Hashmap( size );
140 
141  // We should never get here -this is just to satisfy the compiler.
142  return hash_[ 0 ];
143  }
144 
145  void
147  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
148  hash_[ *it ].sort();
149  }
150  }
151 
152  void
154  {
155  long starttime = time(NULL);
156  TR.Info << "Saving bbdb_ (BackboneDatabase) " << assigned_string_ << " with extras" << std::endl;
157  bbdb_.write_db( db_path_ + "backbone" + assigned_string_ + ".db" );
158  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
159  TR.Info << "Saving loopdb (LoopHashDatabase) " << assigned_string_ << " with loop size " << *it << std::endl;
160  hash_[ *it ].write_db(db_path_ + "loopdb." + utility::to_string( *it ) + assigned_string_ + ".db" );
161  }
162  long endtime = time(NULL);
163  TR << "Save LoopHash Library: " << endtime - starttime << " seconds " << std::endl;
164  }
165 
166  void
168  {
169  long starttime = time(NULL);
170  TR.Info << "Deleting database files " << assigned_string_ << std::endl;
171  std::string dbstring = db_path_ + "backbone" + assigned_string_ + ".db";
172  if ( remove( dbstring.c_str() ) != 0 ) throw EXCN_DB_IO_Failed( dbstring , "delete" );
173  TR.Info << "bbdb deletion successful" << std::endl;
174  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
175  std::string dbstring = db_path_ + "loopdb." + utility::to_string( *it ) + assigned_string_ + ".db" ;
176  if( remove( dbstring.c_str() ) != 0 ) throw EXCN_DB_IO_Failed( dbstring, "delete" );
177  TR.Info << "loopdb size " << utility::to_string( *it ) << " deletion successful" << std::endl;
178  }
179  long endtime = time(NULL);
180  TR << "Deleted LoopHash Library: " << endtime - starttime << " seconds " << std::endl;
181  }
182 
183  void
185  {
186  long starttime = time(NULL);
187  TR.Info << "Reading bbdb_ (BackboneDatabase) " << assigned_string_ << " with extras" << std::endl;
188  bbdb_.read_db( db_path_ + "backbone" + assigned_string_ + ".db", extra_ );
189  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
190  TR.Info << "Reading loopdb (LoopHashDatabase) " << assigned_string_ << " with loop size " << *it << std::endl;
191  hash_[ *it ].read_db( db_path_ + "loopdb." + utility::to_string( *it ) + assigned_string_ + ".db" );
192  }
193  long endtime = time(NULL);
194  TR << "Read LoopHash Library from disk: " << endtime - starttime << " seconds " << std::endl;
195  }
196 
197  void
199  {
200  // Currently, reads a slice of the backbonedb and whatever proteins
201  // are included, the loops from those proteins are loaded.
202  long starttime = time(NULL);
203 
204  TR.Info << "Reading merged bbdb_ (BackboneDatabase) " << assigned_string_;
205  if( extra_ ) TR.Info << " with extras";
206  TR.Info << std::endl;
207  // Indices of homologs is returned in homolog_map
208  std::map< core::Size, bool > homolog_map;
209  std::string db_filename = db_path_ + "backbone.db";
210  TR.Info << "Reading " << db_filename << std::endl;
211  bbdb_.read_db( db_filename, extra_, num_partitions_, assigned_num_, loopdb_range_, homolog_map );
212  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
213  TR.Info << "Reading loopdb (LoopHashDatabase) " << assigned_string_ << " with loop size " << *it << std::endl;
214  // pass the range to the loophashmap so it knows which loops to read
215  // also pass the map of homologs
216  db_filename = db_path_ + "loopdb." + utility::to_string( *it ) + ".db";
217  hash_[ *it ].read_db(db_filename, loopdb_range_, homolog_map );
218  }
219  long endtime = time(NULL);
220  TR << "Read MergedLoopHash Library from disk: " << endtime - starttime << " seconds " << std::endl;
221  }
222 
223 
224  void
226  LoopHashLibraryOP second_lib,
227  utility::vector1< core::Real> rms_cutoffs )
228  {
229  long starttime = time(NULL);
230 
231  // Might want to split this function into subroutines, its kinda big
232 
233  // Concat the entire second_bbdb to the master one
234  // Can later add a removal step, where proteins who aren't referenced can be removed
235  core::Size index_offset;
236  if( extra_ != second_lib->get_extra() ) {
237  throw EXCN_bbdb_Merge_Failed( extra_, second_lib->get_extra() );
238  }
239  if ( ! merge_bbdb( second_lib->backbone_database(), index_offset ) ) {
240  throw EXCN_bbdb_Merge_Failed( "bbdb merge failed for unknown reasons" );
241  }
242 
243  TR.Debug << "BBDB concated" << std::endl;
244  core::Size rms_cutoff_counter = 1; // Because hash_sizes is using an iterator instead of an index
245  for( std::vector< core::Size >::const_iterator jt = hash_sizes_.begin(); jt != hash_sizes_.end(); ++jt ){
246 
247  core::Size loop_size = *jt;
248  core::Real rms_cutoff = rms_cutoffs[ rms_cutoff_counter++ ];
249 
250  LoopHashMap &hashmap = gethash( loop_size );
251  LoopHashMap &second_hashmap = second_lib->gethash( loop_size );
252  TR.Debug << "Hashmaps loaded for frag size " << loop_size <<std::endl;
253 
254  // do NOT use bucket interface, boost can't guarantee 1 bucket = 1 key even with rehash
255  //iterate through all the members of second_loopdb
256  std::pair< BackboneIndexMap::iterator, BackboneIndexMap::iterator > range;
257  second_hashmap.bbdb_range( range );
258 
259  std::vector < BackboneSegment > bs_vec_;
260  std::vector < LeapIndex > leap_vec_;
261  boost::uint64_t key = 0;
262  for( BackboneIndexMap::iterator it = range.first; it != range.second; it++ ) {
263  bool same_as_last = false;
264  bool add_this_ = true;
265 
266  //Now grab key of that loop
267  core::Size bb_index = it->second; //technically it->first == cp.key
268  LeapIndex cp = second_hashmap.get_peptide( bb_index );
269  // if the key is the same as the last checked loop, keep bs_vec
270  if( key == cp.key ) same_as_last = true;
271  key = cp.key;
272 
273  // lookup the seconddb loop bs
274  BackboneSegment bs_;
275  second_lib->backbone_database().get_backbone_segment( cp.index, cp.offset, loop_size , bs_ );
276 
277  if( rms_cutoff != 0 ) {
278  if( !same_as_last ) {
279  //Grab loops from the main loopdb that correspond to that key
280  std::vector < core::Size > leap_index_equals;
281  //hashmap.lookup_withkey( key, leap_index_equals );
282  hashmap.radial_lookup_withkey( key, 3, leap_index_equals );
283 
284  bs_vec_.clear();
285  leap_vec_.clear();
286  //Need to generate vector of equal backbone segments of the master lib for the RMS check
287  for( std::vector < core::Size >::const_iterator itx = leap_index_equals.begin();
288  itx != leap_index_equals.end();
289  ++itx ){
290  core::Size bb_index_equals = *itx;
291  LeapIndex cp_equals = hashmap.get_peptide( bb_index_equals );
292  BackboneSegment bs_equals;
293  bbdb_.get_backbone_segment( cp_equals.index, cp_equals.offset , loop_size , bs_equals );
294  bs_vec_.push_back( bs_equals );
295  leap_vec_.push_back( cp_equals );
296  //if( cp_equals.key != key ) TR.Info<< "These keys don't match: " << cp_equals.key << " " << key << std::endl;
297  }
298  }
299  // Now do an RMS check against every bs in the master lib bucket
300  for( core::Size j = 0; j < bs_vec_.size(); j++ ) {
301  core::Real BBrms = get_rmsd( bs_vec_[j], bs_ );
302  if ( BBrms < rms_cutoff ) {
303  // If any bs is within rms_cutoff RMS, then we don't add it
304  // and also don't check any others
305  add_this_ = false;
306  TR.Debug << "RMS too close, skipping this frag" << std::endl;
307  //if(cp.key != leap_vec_[j].key )TR.Info << "keys " << cp.key << " " <<leap_vec_[j].key << std::endl;
308  continue;
309  }
310  }
311  }
312  if( add_this_ ) {
313  LeapIndex leap_index;
314  // Since the second bbdb was just concatenated onto the end of the master bbdb
315  // new index is just the size of the master bbdb + the original ba of the loop
316  leap_index.index = index_offset + cp.index;
317  leap_index.offset = cp.offset;
318  leap_index.key = key;
319  hashmap.add_leap( leap_index, key );
320  // Might be better to have the slaves do RMS checks when they do their partitions
321  // then the following line won't be needed
322  bs_vec_.push_back( bs_ );
323  leap_vec_.push_back( leap_index );
324  }
325  }
326  }
327  long endtime = time(NULL);
328  TR << "Merged LoopHash Library: " << endtime - starttime << " seconds " << std::endl;
329  }
330 
331  bool LoopHashLibrary::merge_bbdb( const BackboneDB & second_bbdb, core::Size & index_offset ) {
332  index_offset = bbdb_.size();
333  core::Size extra_key_offset = bbdb_.extra_size();
334  BBData tmp;
335  for(core::Size i = 0; i < second_bbdb.size(); i++ ) {
336  second_bbdb.get_protein( i, tmp );
337  if ( extra_ ) {
338  BBExtraData tmp_extra;
339  second_bbdb.get_extra_data( tmp.extra_key, tmp_extra );
340  bbdb_.add_extra_data( tmp_extra );
341  // need to modify the extra_key in BBData now
342  tmp.extra_key = tmp.extra_key + extra_key_offset;
343  }
344  bbdb_.add_protein( tmp );
345  }
346  return true;
347  }
348 
349  void
351  {
352  using namespace basic::options;
353  using namespace basic::options::OptionKeys;
354 
355  // Either obtain structural data from a vall file
356  if ( option[in::file::vall].user() ){
357 
358  // by default read extra data from Vall
359  extra_ = true;
360 
361  VallProviderOP chunks = new VallProvider();
362  // Use partition information generate line numbers
363  core::Size vall_nlines = chunks->vallNumLines(option[in::file::vall]()[1]);
364  core::Size startline = 2;
365  core::Size endline = vall_nlines;
366 
367  //Use an overlap of one to avoid off-by-one errors
368  if (assigned_num_ != 0 ) startline = vall_nlines * assigned_num_ / num_partitions_ - 1;
369  if (assigned_num_ != (num_partitions_ - 1) ) endline = vall_nlines * ( assigned_num_ + 1 ) / num_partitions_ + 1;
370 
371  // Read Vall
372  chunks->vallChunksFromLibrary(option[in::file::vall]()[1], startline, endline );
373 
374  core::Size nchunks = chunks->size();
375  for( core::Size i=1; i <= nchunks; ++i ){
376  // Now the total number refers to within in partition
377  TR.Info << i << "/" << nchunks << " in " << assigned_string_ << std::endl;
378  VallChunkOP chunk = chunks->at(i);
379  core::pose::PoseOP newpose = chunk->get_pose();
380  extract_data_from_pose( *newpose, chunk->size(), chunk );
381  }
382  }
383 
384  // also obtain data from input structures
385 
387  if ( option[ in::file::fullatom ]() ) {
389  } else {
391  }
393  core::Size counter = 0;
394  while( input.has_another_pose() ) {
395  core::pose::Pose pose;
396  input.fill_pose( pose, *rsd_set ); // no other way to increment the inputstream
397  if ( (num_partitions_ > 1) && (counter++ % num_partitions_ != assigned_num_) ) continue;
398  extract_data_from_pose( pose );
399  }
400  }
401 
402 
403 
404  void
406  {
407  using namespace core::scoring;
408 
411 
412 
424  }
425 
426 
427 
428 
429  void
431  const core::pose::Pose& src_pose,
432  core::pose::Pose& tgt_pose,
434  )
435  {
436 
437  core::optimization::MinimizerOptions options( "dfpmin", 0.2 , true , false );
438  core::optimization::MinimizerOptions options2( "dfpmin", 0.02 ,true , false );
439 
440 
441  kinematics::MoveMap final_mm;
442  final_mm.set_bb(true);
443 
444  core::pose::Pose pose(tgt_pose);
445 
446  // Set up contraints
447  protocols::loops::Loops exclude_region;
448  exclude_region.add_loop( myloop );
449  add_coordinate_constraints_to_pose( pose, tgt_pose, exclude_region );
450 
451  // copy over stretch of phi/psi/omega angles
452 
453  core::pose::transfer_phi_psi( src_pose, pose, myloop.start(), myloop.stop() );
454 
455  core::optimization::AtomTreeMinimizer().run( pose, final_mm, scorefxn_rama_cst, options );
456 
457  core::Real premin_rms = core::scoring::CA_rmsd( pose, tgt_pose );
458  TR.Info << "Graft: Premin RMS: " << premin_rms << std::endl;
459  TR.Info << "Graft: Min Score3 " << std::endl;
460  //scorefxn_cen_cst.show( TR.Info, *newpose );
461  core::optimization::AtomTreeMinimizer().run( pose, final_mm, scorefxn_cen_cst, options2 );
462 
463  transfer_phi_psi( pose, tgt_pose );
464  }
465 
466 
467 
468 
469 
470  void
472  {
473  using namespace basic::options;
474  using namespace basic::options::OptionKeys;
475 
476  std::string prefix = option[ out::prefix ]();
477  core::Size skim_size = option[ lh::skim_size ]();
478 
479  for(int round = 0; round < 100; round ++ ){
480  //core::Size count;
481  static int casecount = 0;
482  core::pose::Pose opose = pose;
483  std::vector< core::io::silent::SilentStructOP > lib_structs;
484 
485  TR.Info << "Loophash apply function ! " << std::endl;
486 
487  // fix any shitty backbone angles.
488 
489  // Set up contraints
491  //protocols::relax::FastRelax *qrelax = new protocols::relax::FastRelax( fascorefxn, 1 );
492  protocols::relax::FastRelaxOP relax = new protocols::relax::FastRelax( fascorefxn, option[ OptionKeys::relax::sequence_file ]() );
493 
494  // convert pose to centroid pose:
497 
498  core::Size starttime2 = time(NULL);
499  get_all( pose, lib_structs, 1, 0, 20,1400.0, 0.5, 4.0 );
500  core::Size endtime2 = time(NULL);
501  TR.Info << "FOUND " << lib_structs.size() << " alternative states in time: " << endtime2 - starttime2 << std::endl;
502 
503  //std::random__shuffle( lib_structs.begin(), lib_structs.end());
504  numeric::random::random_permutation(lib_structs.begin(), lib_structs.end(), numeric::random::RG);
505 
506  std::vector< core::io::silent::SilentStructOP > select_lib_structs;
507 
508  for( core::Size k=0;k< std::min(skim_size, lib_structs.size() ) ;k++){
509  select_lib_structs.push_back( lib_structs[k] );
510  }
511 
512  core::pose::Pose native_pose;
513  core::import_pose::pose_from_pdb( native_pose, option[ in::file::native ]() );
514 
515  { // Save centorids
517  std::string silent_file_ = option[ OptionKeys::out::file::silent ]() + ".centroid.out" ;
518  for( core::Size h = 0; h < select_lib_structs.size(); h++){
519  core::pose::Pose rpose;
520  select_lib_structs[h]->fill_pose( rpose );
521  core::Real rms = scoring::CA_rmsd( native_pose, rpose );
522  select_lib_structs[h]->add_energy( "round", round, 1.0 );
523  select_lib_structs[h]->add_energy( "rms", rms, 1.0 );
524  select_lib_structs[h]->set_decoy_tag( "S_" + string_of( round ) + "_" + string_of( h ) );
525  sfd.write_silent_struct( *(select_lib_structs[h]) , silent_file_ );
526  }
527 
528  }
529 
530 
531  core::Real bestscore = MAXIMAL_FLOAT;
532  core::Size bestindex = 0;
533  // Batch relax the result:
534 
535  core::Size starttime = time(NULL);
536  relax->batch_apply( select_lib_structs );
537  core::Size endtime = time(NULL);
538  TR.Info << "Batchrelax time: " << endtime - starttime << " for " << select_lib_structs.size() << " structures " << std::endl;
539 
540 
541  for( core::Size h = 0; h < select_lib_structs.size(); h++){
542  TR.Info << "DOING: " << h << " / " << select_lib_structs.size() << std::endl;
543  core::pose::Pose rpose;
544 
545  select_lib_structs[h]->fill_pose( rpose );
546 
547  //core::Real score = scoring::CA_rmsd( native_pose, rpose );
548  core::Real score = (*fascorefxn)(rpose);
549  TR.Info << "score: " << h << " " << score << std::endl;
550 
551  if( score < bestscore ){
552  bestscore = score;
553  bestindex = h;
554  pose = rpose;
555  }
556  }
557  casecount++;
558  //test_loop_sample( pose, pose.total_residue() );
559 
560  core::Real bestrms = scoring::CA_rmsd( native_pose, pose );
561  TR.Info << "BESTSCORE: " << bestscore << "BESTRMS" << bestrms << std::endl;
562  //pose.dump_pdb( "lhb_" + prefix + "_" + utility::to_string( round ) + ".pdb" );
563 
564 
566  std::string silent_file_ = option[ OptionKeys::out::file::silent ]();
567  for( core::Size h = 0; h < select_lib_structs.size(); h++){
568 
569  if( h == bestindex ) {
570  core::pose::Pose rpose;
571  select_lib_structs[h]->fill_pose( rpose );
572  core::Real rms = scoring::CA_rmsd( native_pose, rpose );
573  select_lib_structs[h]->add_energy( "round", round, 1.0 );
574  select_lib_structs[h]->add_energy( "rms", rms, 1.0 );
575  select_lib_structs[h]->set_decoy_tag( "S_" + string_of( round ) + "_" + string_of( h ) );
576  sfd.write_silent_struct( *(select_lib_structs[h]) , silent_file_ );
577  }
578  }
579 
580  }
581 
582  }
583 
584 
585  void
587  core::pose::Pose& pose,
588  core::Size &fir,
589  core::Size &fjr,
590  core::Real min_rms,
591  core::Real max_rms
592  )
593  {
594  using namespace core;
595  using namespace core::pose;
596  using namespace conformation;
597  using namespace kinematics;
598  using namespace numeric::geometry::hashing;
599 
600  core::pose::Pose original_pose = pose;
601 
602  Size nres = pose.total_residue();
603  Size ir, jr;
604  //Size newpep_index = 0;
605 
606  //core::Size backbone_offset;
607  //bbdb_.add_pose( pose, backbone_offset );
608 
609  int runcount=0;
610  runcount++;
611 
612  fir = 0;
613  fjr = 0;
614 
615  while( runcount++ < 1000 ){
616 
617  // pick a random loop length
618  core::Size loop_size = hash_sizes_[ numeric::random::random_range(0,hash_sizes_.size()-1) ]; // Note that hash_sizes_ is a std::vector, not a vector1
619 
620  // pick a starting residue
621  ir = numeric::random::random_range(2,nres - loop_size - 1);
622  jr = ir + loop_size;
623  if ( ir > nres ) continue;
624  if ( jr > nres ) continue;
625 
626  // find any loops
627 
628  BackboneSegment pose_bs;
629  pose_bs.read_from_pose( pose, ir, loop_size );
630 
631  Real6 t;
632  if(!get_rt_over_leap( original_pose, ir, jr, t )) continue;
633 
634  LoopHashMap &hashmap = gethash( loop_size );
635  std::vector < core::Size > leap_index_bucket;
636  hashmap.lookup( t, leap_index_bucket );
637 
638  TR.Info << "G: " << runcount << " " << ir << " " << jr << " " << leap_index_bucket.size() << " " << t[1] << " " << t[2] << " " << t[3] << " " << t[4] << " " << t[5] << " " << t[6] << std::endl;
639 
640  if( leap_index_bucket.size() == 0) continue;
641  TR.Info << "B: " << leap_index_bucket.size() << std::endl;
642  std::vector < core::Size > filter_leap_index_bucket;
643  for( std::vector < core::Size >::const_iterator it = leap_index_bucket.begin();
644  it != leap_index_bucket.end();
645  ++it ){
646 
647  //LeapIndex *cp = (LeapIndex*)(*it);
648  core::Size retrieve_index = (core::Size) (*it);
649  LeapIndex cp = hashmap.get_peptide( retrieve_index );
650 
651  // Also retrieve the backbone structures
652  BackboneSegment new_bs;
653  bbdb_.get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
654 
655  core::Real BBrms = get_rmsd( pose_bs, new_bs );
656  if( ( BBrms > min_rms ) && ( BBrms < max_rms ) ){
657  filter_leap_index_bucket.push_back( *it );
658  }
659  }
660 
661  if( filter_leap_index_bucket.size() == 0) continue;
662 
663  core::Size loop_choice = numeric::random::random_range(0, filter_leap_index_bucket.size() - 1);
664 
665 
666  // APPLY LOOP and return
667 
668  // Also retrieve the backbone structures
669 
670  fir = ir;
671  fjr = jr;
672 
673  core::Size retrieve_index = (core::Size) (filter_leap_index_bucket[loop_choice]);
674  LeapIndex cp = hashmap.get_peptide( retrieve_index );
675 
676 
677  BackboneSegment new_bs;
678  bbdb_.get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
679 
680 
681  core::Real BBrms = get_rmsd( pose_bs, new_bs );
682  TR.Info << "Applying: " << ir << " " << jr << " " << BBrms << nres << loop_size << std::endl;;
683  pose_bs.print();
684 
685  new_bs.apply_to_pose( pose, ir );
686  return;
687  }
688 
689  }
690 
691 
692  void
694  core::pose::Pose& start_pose,
695  std::vector< core::io::silent::SilentStructOP > &lib_structs,
696  core::Size start_res,
697  core::Size stop_res,
698 
699  core::Real min_bbrms,
700  core::Real max_bbrms,
701  core::Real min_rms,
702  core::Real max_rms
703  )
704  {
705  using namespace core;
706  using namespace core::pose;
707  using namespace conformation;
708  using namespace kinematics;
709  using namespace numeric::geometry::hashing;
710  using namespace optimization;
711  using namespace id;
712 
713  core::pose::Pose original_pose = start_pose;
714  core::pose::Pose edit_pose = start_pose;
715 
716 
717 
718  kinematics::MoveMap final_mm;
719  final_mm.set_bb(true);
720  // setup movemap & minimisation
721 
722  // for ( Size ii=ir; ii<= jr; ++ii ) {
723  // final_mm.set_bb( ii, true );
724  // if ( newpose->residue(ii).aa() == chemical::aa_pro ) final_mm.set( TorsionID( phi_torsion, BB, ii ), false );
725  // }
726 
727  Size nres = start_pose.total_residue();
728  Size ir, jr;
729  //Size newpep_index = 0;
730 
731  //core::Size backbone_offset;
732  //bbdb_.add_pose( pose, backbone_offset );
733 
734  int runcount=0;
735  runcount++;
736 
737  // figure out start and stop residues
738  if ( stop_res == 0 ) stop_res = nres; // to do the whole protein just set stop_res to 0
739  start_res = std::min( start_res, (core::Size)2 ); // dont start before 2 - WHY ?
740 
741  for( ir = 2; ir < nres; ir ++ ){
742  for( core::Size k = 0; k < hash_sizes_.size(); k ++ ){
743  core::Size loop_size = hash_sizes_[ k ];
744 
745  jr = ir + loop_size;
746  if ( ir > nres ) continue;
747  if ( jr > nres ) continue;
748 
749  // get the rigid body transform for the current segment
750  BackboneSegment pose_bs;
751  pose_bs.read_from_pose( start_pose, ir, loop_size );
752  Real6 t;
753  if(!get_rt_over_leap( original_pose, ir, jr, t )) continue;
754 
755  // Look up the bin index of that transform in the hash map
756  LoopHashMap &hashmap = gethash( loop_size );
757  std::vector < core::Size > leap_index_bucket;
758  hashmap.lookup( t, leap_index_bucket );
759 
760  TR.Info << "G: " << runcount << " " << ir << " " << jr << " " << leap_index_bucket.size() << " " << t[1] << " " << t[2] << " " << t[3] << " " << t[4] << " " << t[5] << " " << t[6] << std::endl;
761 
762 
763 
764  // Now for every hit, get the internal coordinates and make a short list of replacement loops
765  // according to the RMS criteria
766 
767  if( leap_index_bucket.size() == 0) continue;
768  std::vector < core::Size > filter_leap_index_bucket;
769  for( std::vector < core::Size >::const_iterator it = leap_index_bucket.begin();
770  it != leap_index_bucket.end();
771  ++it ){
772 
773  // Get the actual strucure index (not just the bin index)
774  core::Size retrieve_index = (core::Size) (*it);
775  LeapIndex cp = hashmap.get_peptide( retrieve_index );
776 
777  // Retrieve the actual backbone structure
778  BackboneSegment new_bs;
779  bbdb_.get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
780 
781  // Check the values against against any RMS limitations imposed by the caller
782  core::Real BBrms = get_rmsd( pose_bs, new_bs );
783  if( ( BBrms > min_bbrms ) && ( BBrms < max_bbrms ) ){
784  filter_leap_index_bucket.push_back( *it );
785  }
786  }
787 
788  // If no loops pass the previous filter - abort
789  if( filter_leap_index_bucket.size() == 0) continue;
790 
791  // Now go through the chosen loops in random order
792  core::Size explore_count = 0;
793  //std::random__shuffle( filter_leap_index_bucket.begin(), filter_leap_index_bucket.end());
794  numeric::random::random_permutation(filter_leap_index_bucket.begin(), filter_leap_index_bucket.end(), numeric::random::RG);
795 
796  for( std::vector < core::Size >::const_iterator it = filter_leap_index_bucket.begin();
797  it != filter_leap_index_bucket.end();
798  ++it ){
799 
800  explore_count ++;
801 
802  clock_t starttime = clock();
803 
804 
805  core::Size retrieve_index = *it;
806  LeapIndex cp = hashmap.get_peptide( retrieve_index );
807 
808  BackboneSegment new_bs;
809  bbdb_.get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
810 
811  //core::Real BBrms = get_rmsd( pose_bs, new_bs );
812  // Distance measures of end point
813 
814  /* no longer applicable since leapindex doesnt have transform info
815  if( TR.Debug.visible() ){
816  core::Real xyzdist = sqrt(sqr(t[1] - cp.vecx) + sqr(t[2] - cp.vecy) + sqr(t[3] - cp.vecz));
817  core::Real ang1 = t[4] - cp.rotx; while( ang1 > 180 ) ang1 -= 360.0; while( ang1 < -180.0 ) ang1 += 360.0;
818  core::Real ang2 = t[5] - cp.roty; while( ang2 > 180 ) ang2 -= 360.0; while( ang2 < -180.0 ) ang2 += 360.0;
819  core::Real ang3 = t[6] - cp.rotz; while( ang3 > 180 ) ang3 -= 360.0; while( ang3 < -180.0 ) ang3 += 360.0;
820  core::Real angdist = sqrt(sqr(ang1)+sqr(ang2)+sqr(ang3) );
821  TR.Info << " X: " << xyzdist << " " << angdist << " " << cp.rotx << " " << cp.roty << " " << cp.rotz << std::endl;
822  }*/
823 
824  // set newpose
825  protocols::loops::Loops exclude_region;
826  exclude_region.add_loop( protocols::loops::Loop( ir, jr ) );
827  core::pose::Pose newpose( start_pose );
828  core::pose::transfer_phi_psi( start_pose, newpose );
829  add_coordinate_constraints_to_pose( newpose, original_pose, exclude_region );
830  new_bs.apply_to_pose( newpose, ir );
831  //scorefxn_rama_cst.show( TR.Info, *newpose );
832 
833 
834  // just for comparison with cut!
835  //core::pose::PoseOP newpose2( new Pose( original_pose ) );
836  //new_bs.apply_to_pose( *newpose2, ir, true );
837  //newpose2->dump_pdb("rep_" + utility::to_string( ir ) + "_" + utility::to_string( jr ) + "_" + utility::to_string( int(xyzdist) ) + "_" + utility::to_string( int(angdist) ) + ".cut.pdb" );
838 
839 
840 
841 
842  //scorefxn_rama_cst.show( TR.Info, *newpose );
843  //newpose->dump_pdb("rep_" + utility::to_string( ir ) + "_" + utility::to_string( jr ) + "_" + utility::to_string( int(xyzdist) ) + "_" + utility::to_string( int(angdist) ) + ".bef.pdb" );
844  AtomTreeMinimizer().run( newpose, final_mm, scorefxn_rama_cst, options );
845  //scorefxn_rama_cst.show( TR.Info, *newpose );
846  //newpose->dump_pdb("rep_" + utility::to_string( ir ) + "_" + utility::to_string( jr ) + "_" + utility::to_string( int(xyzdist) ) + "_" + utility::to_string( int(angdist) ) + ".aft.pdb" );
847  //newpose->dump_pdb("rep_" + utility::to_string( ir ) + "_" + utility::to_string( jr ) + "_" + utility::to_string( int(xyzdist) ) + "_" + utility::to_string( int(angdist) ) + ".pdb" );
848 
849  core::Real premin_rms = core::scoring::CA_rmsd( newpose, original_pose );
850  TR.Info << "Premin RMS: " << premin_rms << std::endl;
851  TR.Info << "Min Score3 " << std::endl;
852  //scorefxn_cen_cst.show( TR.Info, *newpose );
853  AtomTreeMinimizer().run( newpose, final_mm, scorefxn_cen_cst, options2 );
854  //scorefxn_cen_cst.show( TR.Info, *newpose );
855 
856  // get final RMS
857 
858  core::Real final_rms = core::scoring::CA_rmsd( newpose, original_pose );
859  TR.Info << "Final RMS: " << final_rms << std::endl;
860  if ( ( final_rms < max_rms ) && ( final_rms > min_rms ) ){
861 
862  core::pose::Pose mynewpose( start_pose );
863 
864  transfer_phi_psi( newpose, mynewpose );
865 
867  new_struct->fill_struct( mynewpose );
868  lib_structs.push_back( new_struct );
869  }
870 
871  //if ( lib_structs.size() > 2 ) return;
872 
873  clock_t endtime = clock();
874 
875  TR.Info << "Clocks: " << endtime - starttime << std::endl;
876 
877  }
878  }
879  }
880 
881  }
882 
883 
884  void
886  extract_data_from_pose( pose, pose.total_residue() );
887  }
888 
889  void
891  {
892  using namespace core;
893  using namespace core::pose;
894  using namespace conformation;
895  using namespace kinematics;
896  using namespace numeric::geometry::hashing;
897 
898  Size ir, jr;
899  //Size newpep_index = 0;
900  core::Size index;
901  bbdb_.add_pose( pose, nres, index, chunk );
902 
903  static int runcount=0;
904  runcount++;
905 
906  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
907  TR.Info << "Setting up hash: Size: " << *it << std::endl;
908  Size loop_size = *it;
909 
910  LoopHashMap &hashmap = gethash( loop_size );
911  if( loop_size + 2 > nres ) continue;
912  for( ir = 2; ir < ( nres - loop_size ); ir ++ ){
913 
914  jr = ir+loop_size;
915 
916  Real6 t;
917  if(!get_rt_over_leap_fast( pose, ir, jr, t )) return;
918  LeapIndex leap_index;
919  leap_index.index = index;
920  leap_index.offset = (ir-1)*3;
921 
922 
923  TR.Debug << "ADD: "
924  << runcount << " "
925  << ir << " "
926  << jr << " "
927  << t[1] << " "
928  << t[2] << " "
929  << t[3] << " "
930  << t[4] << " "
931  << t[5] << " "
932  << t[6] << " "
933  << leap_index.index << " "
934  << leap_index.offset;
935  TR.Debug << std::endl;
936  hashmap.add_leap( leap_index, t );
937 
938  BackboneSegment pose_bs;
939  pose_bs.read_from_pose( pose, ir, loop_size );
940  }
941  }
942 
943 
944 
945  // reset the fold tree
946  FoldTree f;
947  f.add_edge( 1, pose.total_residue() , Edge::PEPTIDE );
948  if( f.reorder(1) == false ){
949  TR.Error << "ERROR During resetting reordering of fold tree - am ignoring this LOOP ! Cannot continue " << std::endl;
950  return; // continuing leads to a segfault - instead ignore this loop !
951  }
952  pose.fold_tree( f );
953 
954  }
955 
956 
958  using namespace core;
959  using namespace core::pose;
960  using namespace conformation;
961  using namespace kinematics;
962  using namespace numeric::geometry::hashing;
963 
964  Size jr;
965  core::Size index=0;
966  core::Size loop_size = hash_sizes_[0];
967  jr = ir+loop_size;
968  Real6 t;
969  LeapIndex leap_index;
970  if(!get_rt_over_leap_fast( pose, ir, jr, t )) return false;
971  leap_index.index = index;
972  leap_index.offset = (ir-1)*3;
973  LoopHashMap &hashmap = gethash( loop_size );
974 
975  BackboneSegment pose_bs;
976  pose_bs.read_from_pose( pose, ir, loop_size );
977 
978  if( deposit ){
979  bbdb_.add_pose( pose, pose.total_residue(), index, NULL );
980 
981  TR << "ADD: "
982  << ir << " " << jr << " "
983  << t[1] << " " << t[2] << " " << t[3] << " " << t[4] << " " << t[5] << " " << t[6] << " "
984  << leap_index.index << " "
985  << leap_index.offset
986  << std::endl;
987  hashmap.add_leap( leap_index, t );
988 
989  pose_bs.print();
990  }
991 
992  // Now read it back.
993 
994  std::vector < core::Size > leap_index_bucket;
995  TR << "Radial lookup ... " << std::endl;
996  hashmap.radial_lookup( 0, t, leap_index_bucket );
997 
998  core::Size example_index = leap_index_bucket[0];
999  TR << "Get the actual strucure index (not just the bin index) << " << std::endl;
1000 
1001  LeapIndex cp = hashmap.get_peptide( example_index );
1002 
1003  // Retrieve the actual backbone structure
1004  BackboneSegment new_bs;
1005  this->backbone_database().get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
1006  new_bs.print();
1007 
1008  bool result = new_bs.compare(pose_bs,0.1);
1009 
1010  if(result) TR << "TEST OK " << std::endl; else TR << "TEST FAIL" << std::endl;
1011  TR << "Done testing!" << std::endl;
1012  return result;
1013  }
1014 
1015 
1017  {
1018  using namespace core;
1019  using namespace core::pose;
1020  using namespace conformation;
1021  using namespace kinematics;
1022  using namespace numeric::geometry::hashing;
1023 
1024  core::pose::Pose original_pose = pose;
1025 
1026  Size ir, jr;
1027 
1028  core::Size index;
1029  bbdb_.add_pose( pose, nres, index );
1030 
1031  static int runcount=0;
1032  runcount++;
1033 
1034  for( std::vector< core::Size >::const_iterator it = hash_sizes_.begin(); it != hash_sizes_.end(); ++it ){
1035  TR.Info << "Setting up hash: Size: " << *it << std::endl;
1036  Size loop_size = *it;
1037 
1038  LoopHashMap &hashmap = gethash( loop_size );
1039 
1040  for( ir = 2; ir < ( nres - loop_size ); ir ++ ){
1041  jr = ir+loop_size;
1042 
1043  Real6 t;
1044  if(!get_rt_over_leap( original_pose, ir, jr, t )) continue;
1045  LeapIndex leap_index;
1046  leap_index.index = index;
1047  leap_index.offset = (ir-1)*3;
1048 
1049 
1050  TR.Info << "ADD: "
1051  << runcount << " "
1052  << ir << " "
1053  << jr << " "
1054  << t[1] << " "
1055  << t[2] << " "
1056  << t[3] << " "
1057  << t[4] << " "
1058  << t[5] << " "
1059  << t[6] << " "
1060  << leap_index.index << " "
1061  << leap_index.offset;
1062  TR.Info << std::endl;
1063  hashmap.add_leap( leap_index, t );
1064 
1065  BackboneSegment pose_bs;
1066  pose_bs.read_from_pose( pose, ir, loop_size );
1067  pose_bs.print();
1068 
1069  // sanity check one
1070  {
1071  BackboneSegment check_bs;
1072  bbdb_.get_backbone_segment( leap_index.index, leap_index.offset, hashmap.get_loop_size() , check_bs );
1073  check_bs.print();
1074 
1075 
1076  Pose tmp_pose = original_pose;
1077  TR.Info << tmp_pose.fold_tree() << std::endl;
1078  check_bs.apply_to_pose( tmp_pose, ir );
1079  }
1080 
1081 
1082  if( do_sanity_check_ ){
1083  // Now retrieve everything in that bin: - sanity check:
1084 
1085  std::vector < core::Size > leap_index_bucket;
1086 
1087  // change this to looking up with key instead of transform
1088  hashmap.lookup_withkey( leap_index.key, leap_index_bucket );
1089 
1090  core::Size sani_count = 0;
1091 
1092  for( std::vector < core::Size >::const_iterator it = leap_index_bucket.begin();
1093  it != leap_index_bucket.end();
1094  ++it ){
1095  sani_count++;
1096 
1097  core::Size retrieve_index = (core::Size) (*it);
1098  LeapIndex cp = hashmap.get_peptide( retrieve_index );
1099 
1100  // Also retrieve the backbone structures
1101  BackboneSegment new_bs;
1102  bbdb_.get_backbone_segment( cp.index, cp.offset, hashmap.get_loop_size() , new_bs );
1103 
1104  core::Real BBrms = get_rmsd( pose_bs, new_bs );
1105  if( BBrms > 10.0 && leap_index_bucket.size() >= 2 ){
1106 
1107  TR.Info << "RMS: " << BBrms << std::endl;
1108  TR.Info << "SANI: "
1109  << runcount << " "
1110  << ir << " "
1111  << jr << " "
1112  << cp.index << " "
1113  << cp.offset << " "
1114  << cp.key << " "
1115  << std::endl;
1116 
1117  new_bs.print();
1118  // construct a pose with the alternative
1119  Pose tmp_pose = original_pose;
1120  TR.Info << tmp_pose.fold_tree() << std::endl;
1121  new_bs.apply_to_pose( tmp_pose, ir );
1122 
1123  Real6 t;
1124  get_rt_over_leap( tmp_pose, ir, jr, t );
1125  TR.Info << "R6CHECKHERE: " << t[1] << " " << t[2] << " " <<t[3] << " " <<t[4] << " " <<t[5] << " " <<t[6] << std::endl;
1126 
1127  }
1128  }
1129 
1130 
1131 
1132  }
1133 
1134  }
1135  }
1136 
1137  // reset the fold tree
1138  FoldTree f;
1139  f.add_edge( 1, pose.total_residue() , Edge::PEPTIDE );
1140  if( f.reorder(1) == false ){
1141  TR.Error << "ERROR During reordering of fold tree - am ignoring this LOOP ! I am done. " << std::endl;
1142  return; // continuing leads to a segfault - instead ignore this loop !
1143  }
1144  pose.fold_tree( f );
1145 
1146  }
1147 
1148 
1149 
1150 } // namespace loops
1151 } // namespace protocols
1152 
1153 
1154 
1155