Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Mover_LoopHashRefine.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 Mike Tyka
11 /// @brief
12 
13 // Must place this on top because of an ambiguous symbol issue with byte
14 #ifdef BOINC
15  #include <protocols/boinc/boinc.hh> // REQUIRED FOR WINDOWS
16 #endif
17 
18 // libRosetta headers
20 
21 // AUTO-REMOVED #include <core/chemical/ResidueTypeSet.hh>
22 
23 // AUTO-REMOVED #include <core/chemical/ChemicalManager.hh>
24 // AUTO-REMOVED #include <core/kinematics/FoldTree.hh>
25 #include <core/pose/util.hh>
26 #include <core/kinematics/Jump.hh>
27 #include <core/kinematics/RT.hh>
28 #include <basic/options/option.hh>
29 // AUTO-REMOVED #include <core/import_pose/pose_stream/util.hh>
30 // AUTO-REMOVED #include <core/io/pdb/pose_io.hh>
31 #include <core/pose/Pose.hh>
33 // AUTO-REMOVED #include <core/conformation/ResidueFactory.hh>
34 // AUTO-REMOVED #include <core/scoring/constraints/util.hh>
35 // AUTO-REMOVED #include <core/scoring/constraints/CoordinateConstraint.hh>
36 // AUTO-REMOVED #include <core/scoring/Energies.hh>
39 #include <basic/Tracer.hh>
40 #include <core/scoring/rms_util.hh>
43 // AUTO-REMOVED #include <core/io/silent/BinaryProteinSilentStruct.hh>
44 // AUTO-REMOVED #include <core/io/silent/ProteinSilentStruct.hh>
45 // AUTO-REMOVED #include <core/io/silent/SilentStructFactory.hh>
46 
47 // AUTO-REMOVED #include <core/init.hh>
48 // AUTO-REMOVED #include <numeric/HomogeneousTransform.hh>
49 // AUTO-REMOVED #include <protocols/loops/Loop.hh>
50 // AUTO-REMOVED #include <protocols/wum/SilentStructStore.hh>
52 // AUTO-REMOVED #include <protocols/loops/Loops.hh>
53 // #include <protocols/match/Hit.fwd.hh>
54 // AUTO-REMOVED #include <protocols/match/Hit.hh>
55 #include <numeric/geometry/hashing/SixDHasher.hh>
56 #include <protocols/moves/Mover.hh>
57 // AUTO-REMOVED #include <protocols/topology_broker/TopologyBroker.hh>
58 // AUTO-REMOVED #include <protocols/topology_broker/util.hh>
59 // AUTO-REMOVED #include <protocols/frag_picker/VallProvider.hh>
60 // AUTO-REMOVED #include <utility/excn/Exceptions.hh>
61 #include <utility/exit.hh>
62 #include <utility/fixedsizearray1.hh>
63 #include <utility/pointer/owning_ptr.hh>
64 // AUTO-REMOVED #include <core/kinematics/MoveMap.hh>
65 
66 // AUTO-REMOVED #include <core/optimization/AtomTreeMinimizer.hh>
68 
75 
76 #include <numeric/random/random.hh>
77 
78 // C++ headers
79 //#include <cstdlib>
80 
81 #include <iostream>
82 #include <string>
83 // AUTO-REMOVED #include <cstdio>
84 
85 // option key includes
86 // AUTO-REMOVED #include <basic/options/keys/broker.OptionKeys.gen.hh>
87 #include <basic/options/keys/in.OptionKeys.gen.hh>
88 #include <basic/options/keys/out.OptionKeys.gen.hh>
89 #include <basic/options/keys/lh.OptionKeys.gen.hh>
90 #include <basic/options/keys/relax.OptionKeys.gen.hh>
91 
96 #include <utility/vector1.hh>
97 #include <utility/excn/EXCN_Base.hh>
98 
99 // Numeric Headers
100 #include <numeric/random/random.hh>
101 #include <numeric/random/random_permutation.hh>
102 
103 
104 #ifdef WIN32
105  #include <ctime>
106 #endif
107 
108 
109 static basic::Tracer TR("main");
110 
111 using namespace protocols::moves;
112 using namespace core::scoring;
113 using namespace core;
114 using namespace core::pose;
115 using namespace conformation;
116 using namespace kinematics;
117 using namespace numeric::geometry::hashing;
118 using namespace protocols::frag_picker;
119 using namespace protocols::loophash;
120 
121 
122 namespace protocols {
123 namespace loophash {
124 
125 static numeric::random::RandomGenerator RG(9479); // <- Magic number, do not change it!!!
126 
127 void
128 Mover_LoopHashRefine::apply( core::pose::Pose& pose )
129 {
130  if( !library_ ) return;
131 
132  using namespace basic::options;
133  using namespace basic::options::OptionKeys;
134 
135  std::string prefix = option[ out::prefix ]();
136  core::Size skim_size = option[ lh::skim_size ]();
137 
138  LocalInserter_SimpleMinOP simple_inserter( new LocalInserter_SimpleMin() );
139  LoopHashSampler lsampler( library_, simple_inserter );
140 
141 
143  protocols::relax::FastRelax relax( fascorefxn, option[ OptionKeys::relax::sequence_file ]() );
144  core::pose::PoseOP native_pose;
145  if( option[ in::file::native ].user() ){
146  native_pose = new Pose;
147  core::import_pose::pose_from_pdb( *native_pose, option[ in::file::native ]() );
148  relax.set_native_pose( native_pose );
149  }
150 
151  for(int round = 0; round < option[ OptionKeys::lh::rounds ]; round ++ ){
152  std::string checkpoint_id = "chk" + string_of( round );
153  if (!checkpoints_.recover_checkpoint( pose, get_current_tag(), checkpoint_id, true, true )){
154  core::pose::Pose opose = pose;
155  std::vector< core::io::silent::SilentStructOP > lib_structs;
156 
157  // convert pose to centroid pose and apply loophasher
160  core::Size starttime2 = time(NULL);
161 
162  core::Size lcount = 0;
163  while( lib_structs.size() < skim_size ){
164  core::Size resnum = numeric::random::random_range(1,pose.total_residue()-2);
165 
166  lsampler.set_start_res ( resnum );
167  lsampler.set_stop_res ( resnum );
168  lsampler.build_structures( pose, lib_structs );
169  lcount++;
170  TR.Info << "Lcount: " << lcount << std::endl;
171  }
172  core::Size endtime2 = time(NULL);
173  TR.Info << "FOUND " << lib_structs.size() << " alternative states in time: " << endtime2 - starttime2 << std::endl;
174 
175  // write out the centroid structures if desired
176  if(( option[ OptionKeys::lh::write_centroid_structs ]() ) ||
177  ( option[ OptionKeys::lh::centroid_only ]() )){
179  std::string silent_file_ = option[ OptionKeys::out::file::silent ]();
180  silent_file_ += ".centroid.out" ;
181  for( core::Size h = 0; h < lib_structs.size(); h++){
182  core::pose::Pose rpose;
183  lib_structs[h]->fill_pose( rpose );
184  lib_structs[h]->add_energy( "round", round, 1.0 );
185  if( native_pose ){
186  core::Real rms = scoring::CA_rmsd( *native_pose, rpose );
187  core::Real gdtmm = scoring::CA_gdtmm( *native_pose, rpose );
188  lib_structs[h]->add_energy( "rms", rms, 1.0 );
189  lib_structs[h]->add_energy( "gdtmm", gdtmm, 1.0 );
190  }
191  lib_structs[h]->set_decoy_tag( "S_" + string_of( round ) + "_" + string_of( h ) );
192  lib_structs[h]->sort_silent_scores();
193  sfd.write_silent_struct( *(lib_structs[h]) , silent_file_ );
194  }
195  }
196 
197  // abort if centroid is all we're doing
198  if( option[ OptionKeys::lh::centroid_only ]() ) break;
199 
200  // Choose a set of structures to refine/relax
201  //std::random__shuffle( lib_structs.begin(), lib_structs.end());
202  numeric::random::random_permutation(lib_structs.begin(), lib_structs.end(), numeric::random::RG);
203 
204  std::vector< core::io::silent::SilentStructOP > select_lib_structs;
205  for( core::Size k=0;k< std::min(skim_size, lib_structs.size() ) ;k++){
206  select_lib_structs.push_back( lib_structs[k] );
207  }
208 
209 
210  // Batch relax the result:
211  core::Real bestscore = MAXIMAL_FLOAT;
212  core::Size bestindex = 0;
213  core::Size starttime = time(NULL);
214  relax.batch_apply( select_lib_structs );
215  core::Size endtime = time(NULL);
216  TR.Info << "Batchrelax time: " << endtime - starttime << " for " << select_lib_structs.size() << " structures " << std::endl;
217 
218 
219  for( core::Size h = 0; h < select_lib_structs.size(); h++){
220  TR.Info << "DOING: " << h << " / " << select_lib_structs.size() << std::endl;
221  core::pose::Pose rpose;
222  select_lib_structs[h]->fill_pose( rpose );
223  core::Real score = (*fascorefxn)(rpose);
224  TR.Info << "score: " << h << " " << score << std::endl;
225  if( score < bestscore ){
226  bestscore = score;
227  bestindex = h;
228  pose = rpose;
229  }
230  }
231 
233  std::string silent_file_ = option[ OptionKeys::out::file::silent ]();
234  for( core::Size h = 0; h < select_lib_structs.size(); h++){
235  if( h == bestindex ) {
236  core::pose::Pose rpose;
237  select_lib_structs[h]->fill_pose( rpose );
238  #ifdef BOINC_GRAPHICS
239  //mjo moving 'score' into ifdef to remove unused variable warning
240  core::Real score = select_lib_structs[h]->get_energy("score");
241  boinc::Boinc::update_graphics_low_energy( pose, score );
242  boinc::Boinc::update_graphics_last_accepted( pose, score );
243  #endif
244  select_lib_structs[h]->add_energy( "round", round, 1.0 );
245  select_lib_structs[h]->set_decoy_tag( "S_" + string_of( round ) + "_" + string_of( h ) );
246  select_lib_structs[h]->sort_silent_scores();
247 
248  sfd.write_silent_struct( *(select_lib_structs[h]) , silent_file_ );
249  }
250  }
251  checkpoints_.checkpoint( pose, get_current_tag(), checkpoint_id, true );
252  }
253 
254  // if in boinc mode let boinc break out prematurely! This will make user's runtimes near perfect and users happy
255  #ifdef BOINC
257  std::cerr << "BOINC Finishing normally." << std::endl;
258  break; // we're done no matter what nstruct syays
259  }
260  #endif
261  }
262 
263  TR << "Finished serial loophash." << std::endl;
264 
265 }
266 
267 
268 // this is in protocols so that it can be caled from the boinc main function
270  using namespace basic::options;
271  using namespace basic::options::OptionKeys;
272  using namespace protocols::loophash;
273 
274  utility::vector1 < core::Size > loop_sizes = option[lh::loopsizes]();
275  LoopHashLibraryOP loop_hash_library = new LoopHashLibrary( loop_sizes );
276 
277  // Run simple sampling run test or create the db ?
278  if ( option[lh::create_db]() ){;
279  loop_hash_library->create_db();
280  loop_hash_library->save_db();
281  return 0;
282  }
283 
284  Mover_LoopHashRefineOP lh_sampler = new Mover_LoopHashRefine( loop_hash_library );
285 
286  // Normal mode with external loophash library
287  loop_hash_library->load_db();
288  try{
290  } catch ( utility::excn::EXCN_Base& excn ) {
291  std::cerr << "Exception: " << std::endl;
292  excn.show( std::cerr );
293  std::cout << "Exception: " << std::endl;
294  excn.show( std::cout ); //so its also seen in a >LOG file
295  }
296 
297  return 0;
298 }
299 
300 
301 
302 }
303 }
304 
305