Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CartesianHybridize.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
11 /// @brief
12 /// @author Yifan Song
13 /// @author Frank DiMaio
14 
16 
19 #include <basic/datacache/BasicDataCache.hh>
20 
21 //#include <protocols/viewer/viewers.hh>
23 #include <protocols/moves/Mover.hh>
29 #include <protocols/relax/util.hh>
30 
32 
38 
40 #include <protocols/loops/Loops.hh>
41 
42 #include <core/scoring/rms_util.hh>
43 #include <core/sequence/util.hh>
47 
48 #include <core/pose/Pose.hh>
49 #include <core/pose/PDBInfo.hh>
50 #include <core/pose/Remarks.hh>
53 
54 #include <core/pose/util.hh>
55 
58 
65 
71 
75 #include <core/scoring/Energies.hh>
76 
82 
85 #include <core/fragment/FragSet.hh>
86 #include <core/fragment/Frame.hh>
89 
90 // symmetry
96 
97 
98 #include <utility/excn/Exceptions.hh>
99 #include <utility/file/file_sys_util.hh>
100 
101 #include <numeric/random/random.hh>
102 #include <numeric/xyzVector.hh>
103 #include <numeric/xyz.functions.hh>
104 #include <numeric/model_quality/rms.hh>
105 
106 #include <basic/options/option.hh>
107 #include <basic/options/option_macros.hh>
108 #include <basic/options/keys/OptionKeys.hh>
109 #include <basic/options/keys/in.OptionKeys.gen.hh>
110 #include <basic/options/keys/cm.OptionKeys.gen.hh>
111 
112 #include <basic/Tracer.hh>
113 #include <boost/unordered/unordered_map.hpp>
114 
115 namespace protocols {
116 //namespace comparative_modeling {
117 namespace hybridization {
118 
120 
121 static basic::Tracer TR("protocols.hybridization.CartesianHybridize");
122 
124  init();
125 }
126 
128  utility::vector1 < core::pose::PoseOP > const & templates_in,
129  utility::vector1 < core::Real > const & template_wts_in,
130  utility::vector1 < protocols::loops::Loops > const & template_chunks_in,
131  utility::vector1 < protocols::loops::Loops > const & template_contigs_in,
132  core::fragment::FragSetOP fragments9_in ) : ncycles_(DEFAULT_NCYCLES) {
133  init();
134 
135  templates_ = templates_in;
136  template_wts_ = template_wts_in;
137  template_contigs_ = template_contigs_in;
138  fragments9_ = fragments9_in;
139 
140  // make sure all data is there
141  runtime_assert( templates_.size() == template_wts_.size() );
142  runtime_assert( templates_.size() == template_contigs_.size() );
143 
144  // normalize weights
145  core::Real weight_sum = 0.0;
146  for (int i=1; i<=(int)templates_.size(); ++i) weight_sum += template_wts_[i];
147  for (int i=1; i<=(int)templates_.size(); ++i) template_wts_[i] /= weight_sum;
148 
149  // map resids to frames
150  for (core::fragment::FrameIterator i = fragments9_->begin(); i != fragments9_->end(); ++i) {
151  core::Size position = (*i)->start();
152  library_[position] = **i;
153  }
154 
155  // use chunks to subdivide contigs
156  core::Size ntempls = templates_.size();
157  for( core::Size tmpl = 1; tmpl <= ntempls; ++tmpl) {
158  core::Size ncontigs = template_contigs_[tmpl].size(); // contigs to start
159  for (int i=1; i<=(int)ncontigs; ++i) {
160  core::Size cstart = template_contigs_[tmpl][i].start(), cstop = template_contigs_[tmpl][i].stop();
161  bool spilt_chunk=false;
162 
163  // assumes sorted
164  for (int j=2; j<=(int)template_chunks_in[tmpl].size(); ++j) {
165  core::Size j0start = template_chunks_in[tmpl][j-1].start(), j0stop = template_chunks_in[tmpl][j-1].stop();
166  core::Size j1start = template_chunks_in[tmpl][j].start(), j1stop = template_chunks_in[tmpl][j].stop();
167 
168  bool j0incontig = ((j0start>=cstart) && (j0stop<=cstop));
169  bool j1incontig = ((j1start>=cstart) && (j1stop<=cstop));
170 
171  if (j0incontig && j1incontig) {
172  spilt_chunk=true;
173  core::Size cutpoint = (j0stop+j1start)/2;
174  template_contigs_[tmpl].add_loop( cstart, cutpoint-1 );
175  TR.Debug << "Make subfrag " << cstart << " , " << cutpoint-1 << std::endl;
176  cstart=cutpoint;
177  } else if(spilt_chunk && j0incontig && !j1incontig) {
178  spilt_chunk=false;
179  template_contigs_[tmpl].add_loop( cstart, cstop );
180  TR.Debug << "Make subfrag " << cstart << " , " << cstop << std::endl;
181  }
182  }
183  }
184  template_contigs_[tmpl].sequential_order();
185  }
186  TR.Debug << "template_contigs:" << std::endl;
187  for (int i=1; i<= (int)template_contigs_.size(); ++i) {
188  TR.Debug << "templ. " << i << std::endl << template_contigs_[i] << std::endl;
189  }
190 }
191 
192 void
194  using namespace basic::options;
195  using namespace basic::options::OptionKeys;
196 
197  increase_cycles_ = option[cm::hybridize::stage2_increase_cycles]();
198  no_global_frame_ = option[cm::hybridize::no_global_frame]();
199  linmin_only_ = option[cm::hybridize::linmin_only]();
200  cartfrag_overlap_ = 2;
201 
202  // default scorefunction
204 }
205 
206 void
208  lowres_scorefxn_ = scorefxn_in->clone();
209 
210  min_scorefxn_ = scorefxn_in->clone();
211 
212  //bonds_scorefxn_ = new core::scoring::symmetry::SymmetricScoreFunction();
213  bonds_scorefxn_ = scorefxn_in->clone();
214  bonds_scorefxn_->reset();
220 
223 }
224 
225 void
227  // superimpose frag
229  numeric::xyzVector< core::Real > preT(0,0,0), postT(0,0,0);
230  R.xx() = R.yy() = R.zz() = 1;
231  R.xy() = R.yx() = R.zx() = R.zy() = R.yz() = R.xz() = 0;
232 
233  if (superpose) {
234  core::Size len = frag.size();
235  core::Size aln_len = std::min( (core::Size)9999, len ); //fpd can change 9999 to some max alignment sublength
236  core::Size aln_start = numeric::random::random_range(frag.start(), len-aln_len+frag.start() );
237 
238  // don't try to align really short frags
239  if (len > 2) {
240  ObjexxFCL::FArray2D< core::Real > final_coords( 3, 4*aln_len );
241  ObjexxFCL::FArray2D< core::Real > init_coords( 3, 4*aln_len );
242 
243  for (int ii=0; ii<(int)aln_len; ++ii) {
244  int i=aln_start+ii;
245  numeric::xyzVector< core::Real > x_1 = templ.residue(i).atom(" C ").xyz();
246  numeric::xyzVector< core::Real > x_2 = templ.residue(i).atom(" O ").xyz();
247  numeric::xyzVector< core::Real > x_3 = templ.residue(i).atom(" CA ").xyz();
248  numeric::xyzVector< core::Real > x_4 = templ.residue(i).atom(" N ").xyz();
249  preT += x_1+x_2+x_3+x_4;
250 
251  numeric::xyzVector< core::Real > y_1 = pose.residue(templ.pdb_info()->number(i)).atom(" C ").xyz();
252  numeric::xyzVector< core::Real > y_2 = pose.residue(templ.pdb_info()->number(i)).atom(" O ").xyz();
253  numeric::xyzVector< core::Real > y_3 = pose.residue(templ.pdb_info()->number(i)).atom(" CA ").xyz();
254  numeric::xyzVector< core::Real > y_4 = pose.residue(templ.pdb_info()->number(i)).atom(" N ").xyz();
255  postT += y_1+y_2+y_3+y_4;
256 
257  for (int j=0; j<3; ++j) {
258  init_coords(j+1,4*ii+1) = x_1[j];
259  init_coords(j+1,4*ii+2) = x_2[j];
260  init_coords(j+1,4*ii+3) = x_3[j];
261  init_coords(j+1,4*ii+4) = x_4[j];
262  final_coords(j+1,4*ii+1) = y_1[j];
263  final_coords(j+1,4*ii+2) = y_2[j];
264  final_coords(j+1,4*ii+3) = y_3[j];
265  final_coords(j+1,4*ii+4) = y_4[j];
266  }
267  }
268  preT /= 4*len;
269  postT /= 4*len;
270  for (int i=1; i<=4*(int)len; ++i) {
271  for ( int j=0; j<3; ++j ) {
272  init_coords(j+1,i) -= preT[j];
273  final_coords(j+1,i) -= postT[j];
274  }
275  }
276 
277  // get optimal superposition
278  // rotate >init< to >final<
279  ObjexxFCL::FArray1D< numeric::Real > ww( 4*len, 1.0 );
280  ObjexxFCL::FArray2D< numeric::Real > uu( 3, 3, 0.0 );
281  numeric::Real ctx;
282 
283  numeric::model_quality::findUU( init_coords, final_coords, ww, 4*len, uu, ctx );
284  R.xx( uu(1,1) ); R.xy( uu(2,1) ); R.xz( uu(3,1) );
285  R.yx( uu(1,2) ); R.yy( uu(2,2) ); R.yz( uu(3,2) );
286  R.zx( uu(1,3) ); R.zy( uu(2,3) ); R.zz( uu(3,3) );
287  }
288  }
289 
290  // xyz copy fragment to pose
291  for (int i=(int)frag.start(); i<=(int)frag.stop(); ++i) {
292  for (int j=1; j<=(int)templ.residue(i).natoms(); ++j) {
293  core::id::AtomID src(j,i), tgt(j, templ.pdb_info()->number(i));
294  pose.set_xyz( tgt, postT + (R*(templ.xyz( src )-preT)) );
295  }
296  }
297 }
298 
299 
300 void
302  core::Size start = frame.start(),len = frame.length();
303 
304  // we might want to tune this
305  // it might make sense to change this based on gap width
306  // for really large gaps make it one sided to emulate fold-tree fragment insertion
307  int aln_len = cartfrag_overlap_;
308  runtime_assert( cartfrag_overlap_>=1 && cartfrag_overlap_<=len/2);
309 
310  core::Size nres = pose.total_residue();
311 
312  //symmetry
316  dynamic_cast<core::conformation::symmetry::SymmetricConformation &> ( pose.conformation()) );
317  symm_info = SymmConf.Symmetry_Info();
318  nres = symm_info->num_independent_residues();
319  }
320 
321  while (!pose.residue(nres).is_protein()) nres--;
322  bool nterm = (start == 1);
323  bool cterm = (start == nres-8);
324 
325  // insert frag
326  core::pose::Pose pose_copy = pose;
327 
328  ObjexxFCL::FArray1D< numeric::Real > ww( 2*4*aln_len, 1.0 );
329  ObjexxFCL::FArray2D< numeric::Real > uu( 3, 3, 0.0 );
330  numeric::xyzVector< core::Real > com1(0,0,0), com2(0,0,0);
331 
332  for (int i=0; i<(int)len; ++i) {
334  }
335  for (int tries = 0; tries<80; ++tries) {
336  ww = 1.0;
337  uu = 0.0;
338  com1 = numeric::xyzVector< core::Real >(0,0,0);
339  com2 = numeric::xyzVector< core::Real >(0,0,0);
340 
341  // grab coords
342  ObjexxFCL::FArray2D< core::Real > init_coords( 3, 2*4*aln_len );
343  for (int ii=-aln_len; ii<aln_len; ++ii) {
344  int i = (ii>=0) ? (nterm?len-ii-1:ii) : (cterm?-ii-1:len+ii);
345  numeric::xyzVector< core::Real > x_1 = pose.residue(start+i).atom(" C ").xyz();
346  numeric::xyzVector< core::Real > x_2 = pose.residue(start+i).atom(" O ").xyz();
347  numeric::xyzVector< core::Real > x_3 = pose.residue(start+i).atom(" CA ").xyz();
348  numeric::xyzVector< core::Real > x_4 = pose.residue(start+i).atom(" N ").xyz();
349  com1 += x_1+x_2+x_3+x_4;
350  for (int j=0; j<3; ++j) {
351  init_coords(j+1,4*(ii+aln_len)+1) = x_1[j];
352  init_coords(j+1,4*(ii+aln_len)+2) = x_2[j];
353  init_coords(j+1,4*(ii+aln_len)+3) = x_3[j];
354  init_coords(j+1,4*(ii+aln_len)+4) = x_4[j];
355  }
356  }
357  com1 /= 2.0*4.0*aln_len;
358  for (int ii=0; ii<2*4*aln_len; ++ii) {
359  for ( int j=0; j<3; ++j ) init_coords(j+1,ii+1) -= com1[j];
360  }
361 
362  core::Size toget = numeric::random::random_range( 1, frame.nr_frags() );
363  frame.apply( toget, pose_copy );
364 
365  // grab new coords
366  ObjexxFCL::FArray2D< core::Real > final_coords( 3, 2*4*aln_len );
367  for (int ii=-aln_len; ii<aln_len; ++ii) {
368  int i = (ii>=0) ? (nterm?len-ii-1:ii) : (cterm?-ii-1:len+ii);
369  numeric::xyzVector< core::Real > x_1 = pose_copy.residue(start+i).atom(" C ").xyz();
370  numeric::xyzVector< core::Real > x_2 = pose_copy.residue(start+i).atom(" O ").xyz();
371  numeric::xyzVector< core::Real > x_3 = pose_copy.residue(start+i).atom(" CA ").xyz();
372  numeric::xyzVector< core::Real > x_4 = pose_copy.residue(start+i).atom(" N ").xyz();
373  com2 += x_1+x_2+x_3+x_4;
374  for (int j=0; j<3; ++j) {
375  final_coords(j+1,4*(ii+aln_len)+1) = x_1[j];
376  final_coords(j+1,4*(ii+aln_len)+2) = x_2[j];
377  final_coords(j+1,4*(ii+aln_len)+3) = x_3[j];
378  final_coords(j+1,4*(ii+aln_len)+4) = x_4[j];
379  }
380  }
381  com2 /= 2.0*4.0*aln_len;
382  for (int ii=0; ii<2*4*aln_len; ++ii) {
383  for ( int j=0; j<3; ++j ) final_coords(j+1,ii+1) -= com2[j];
384  }
385 
386  numeric::Real ctx;
387  float rms;
388 
389  numeric::model_quality::findUU( final_coords, init_coords, ww, 2*4*aln_len, uu, ctx );
390  numeric::model_quality::calc_rms_fast( rms, final_coords, init_coords, ww, 2*4*aln_len, ctx );
391 
392  //fpd another place where we might want to tune parameters
393  if (rms < 0.5) break;
394  if (tries >= 20 && rms < 1) break;
395  if (tries >= 40 && rms < 2) break;
396  if (tries >= 60 && rms < 4) break;
397  }
399  R.xx( uu(1,1) ); R.xy( uu(2,1) ); R.xz( uu(3,1) );
400  R.yx( uu(1,2) ); R.yy( uu(2,2) ); R.yz( uu(3,2) );
401  R.zx( uu(1,3) ); R.zy( uu(2,3) ); R.zz( uu(3,3) );
402 
403  // apply rotation to ALL atoms
404  // x_i' <- = R*x_i + com1;
405  for ( Size i = 0; i < len; ++i ) {
406  for ( Size j = 1; j <= pose.residue_type(start+i).natoms(); ++j ) {
407  core::id::AtomID id( j, start+i );
408  pose.set_xyz( id, R * ( pose_copy.xyz(id) - com2) + com1 );
409  }
410  }
411 }
412 
413 
414 void
416  using namespace basic::options;
417  using namespace basic::options::OptionKeys;
418  using namespace core::pose::datacache;
419 
420  //protocols::viewer::add_conformation_viewer( pose.conformation(), "hybridize" );
423  tocen->apply( pose );
424 
425  // minimizer
426  core::optimization::MinimizerOptions options( "linmin", 0.01, true, false, false );
427  core::optimization::MinimizerOptions options_minilbfgs( "lbfgs_armijo_nonmonotone", 0.01, true, false, false );
428  options_minilbfgs.max_iter(5);
429  core::optimization::MinimizerOptions options_lbfgs( "lbfgs_armijo_nonmonotone", 0.01, true, false, false );
430  options_lbfgs.max_iter(200);
433  mm.set_bb ( true );
434  mm.set_chi ( true );
435  mm.set_jump( true );
436 
437  //fpd -- this should really be automated somewhere
440  }
441 
442  core::Real max_cart = lowres_scorefxn_->get_weight( core::scoring::cart_bonded );
443  core::Real max_cart_angle = lowres_scorefxn_->get_weight( core::scoring::cart_bonded_angle );
444  core::Real max_cart_length = lowres_scorefxn_->get_weight( core::scoring::cart_bonded_length );
445  core::Real max_cart_torsion = lowres_scorefxn_->get_weight( core::scoring::cart_bonded_torsion );
447  core::Real max_vdw = lowres_scorefxn_->get_weight( core::scoring::vdw );
448 
449  // for i = 1 to n cycles
450  core::Size NMACROCYCLES = 4;
451  TR << "RUNNING FOR " << NMACROCYCLES << " MACROCYCLES" << std::endl;
452 
453  Pose pose_in = pose;
454  core::Size nres = pose.total_residue();
455  if (pose.residue(nres).aa() == core::chemical::aa_vrt) nres--;
456  core::Size n_prot_res = pose.total_residue();
457  while (!pose.residue(n_prot_res).is_protein()) n_prot_res--;
458 
459 
460  for (Size ires=n_prot_res+1; ires<=pose.total_residue(); ++ires) {
461  mm.set_bb (ires, false);
462  mm.set_chi(ires, false);
463 
464  core::Real MAXDIST = 15.0;
465  core::Real COORDDEV = 3.0;
466 
467  for (Size iatom=1; iatom<=pose.residue(ires).nheavyatoms(); ++iatom) {
468 
469  for (Size jres=ires; jres<=pose.total_residue(); ++jres) {
470  for (Size jatom=1; jatom<=pose.residue(jres).nheavyatoms(); ++jatom) {
471  if ( ires == jres && iatom >= jatom) continue;
472  core::Real dist = pose.residue(ires).xyz(iatom).distance( pose.residue(jres).xyz(jatom) );
473  if ( dist <= MAXDIST ) {
474  pose.add_constraint(
476  core::id::AtomID(jatom,jres),
478  )
479  );
480  }
481  }
482  }
483  }
484  }
485 
486  // 10% of the time, skip moves in the global frame
487  bool no_ns_moves = no_global_frame_; // (numeric::random::uniform() <= 0.1);
488 
489 sampler:
490  for (core::Size m=1; m<=NMACROCYCLES; ++m) {
491  core::Real bonded_weight = max_cart;
492  if (m==1) bonded_weight = 0.0*max_cart;
493  if (m==2) bonded_weight = 0.01*max_cart;
494  if (m==3) bonded_weight = 0.1*max_cart;
495 
496  core::Real bonded_weight_angle = max_cart_angle;
497  if (m==1) bonded_weight_angle = 0.0*max_cart_angle;
498  if (m==2) bonded_weight_angle = 0.01*max_cart_angle;
499  if (m==3) bonded_weight_angle = 0.1*max_cart_angle;
500 
501  core::Real bonded_weight_length = max_cart_length;
502  if (m==1) bonded_weight_length = 0.0*max_cart_length;
503  if (m==2) bonded_weight_length = 0.01*max_cart_length;
504  if (m==3) bonded_weight_length = 0.1*max_cart_length;
505 
506  core::Real bonded_weight_torsion = max_cart_torsion;
507  if (m==1) bonded_weight_torsion = 0.0*max_cart_torsion;
508  if (m==2) bonded_weight_torsion = 0.01*max_cart_torsion;
509  if (m==3) bonded_weight_torsion = 0.1*max_cart_torsion;
510 
511  core::Real cst_weight = max_cst;
512  if (m==1) cst_weight = 2*max_cst;
513  if (m==2) cst_weight = 2*max_cst;
514  if (m==3) cst_weight = 2*max_cst;
515 
516  core::Real vdw_weight = max_vdw;
517  if (m==1) vdw_weight = 0.1*max_vdw;
518  if (m==2) vdw_weight = 0.1*max_vdw;
519  if (m==3) vdw_weight = 0.1*max_vdw;
520 
521  TR << "CYCLE " << m << std::endl;
522  TR << " setting bonded weight = " << bonded_weight << std::endl;
523  TR << " setting bonded angle weight = " << bonded_weight_angle << std::endl;
524  TR << " setting bonded length weight = " << bonded_weight_length << std::endl;
525  TR << " setting bonded torsion weight = " << bonded_weight_torsion << std::endl;
526  TR << " setting cst weight = " << cst_weight << std::endl;
527  TR << " setting vdw weight = " << vdw_weight << std::endl;
528 
529  lowres_scorefxn_->set_weight( core::scoring::cart_bonded, bonded_weight );
530  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_angle, bonded_weight_angle );
531  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_length, bonded_weight_length );
532  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_torsion, bonded_weight_torsion );
533  lowres_scorefxn_->set_weight( core::scoring::atom_pair_constraint, cst_weight );
534  lowres_scorefxn_->set_weight( core::scoring::vdw, vdw_weight );
535 
536  (*lowres_scorefxn_)(pose);
538 
540  if (m==4) neffcycles /=2;
541  for (int n=1; n<=(int)neffcycles; ++n) {
542  // possible actions:
543  // 1 - insert homologue frag, global frame
544  // 2 - insert homologue frag, local frame
545  // 3 - insert sequence frag
546  core::Real action_picker = numeric::random::uniform();
547  core::Size action = 0;
548  if (m==1) {
549  action = no_ns_moves?2:1;
550  if (action_picker < 0.2) action = 3;
551  } else if (m==2) {
552  action = 2;
553  if (action_picker < 0.2) action = 3;
554  } else if (m==3) {
555  action = 2;
556  if (action_picker < 0.2) action = 3;
557  } else if (m==4) {
558  action = 3;
559  }
560 
561  std::string action_string;
562  if (action == 1) action_string = "fragNS";
563  if (action == 2) action_string = "frag";
564  if (action == 3) action_string = "picker";
565 
566  if (action == 1 || action == 2) {
567  core::Size templ_id = numeric::random::random_range( 1, templates_.size() );
568  core::Size nfrags = template_contigs_[templ_id].num_loop();
569  // remove non-protein frags
570  while (templates_[templ_id]->pdb_info()->number(template_contigs_[templ_id][nfrags].start()) >
571  (int)n_prot_res) {
572  --nfrags;
573  }
574  //randomly pick frag
575  core::Size frag_id = numeric::random::random_range( 1, nfrags );
576  protocols::loops::LoopOP frag = new protocols::loops::Loop ( template_contigs_[templ_id][frag_id] );
577 
578  if (frag->size() > 14)
579  action_string = action_string+"_15+";
580  else if (frag->size() <= 4)
581  action_string = action_string+"_0-4";
582  else
583  action_string = action_string+"_5-14";
584 
585  apply_frag( pose, *templates_[templ_id], *frag, (action==2) );
586 
587  if (action == 1) {
588  //fpd assume this was initialized elsewhere
589  runtime_assert( pose.data().has( CacheableDataType::TEMPLATE_HYBRIDIZATION_HISTORY ) );
590  TemplateHistory &history =
591  *( static_cast< TemplateHistory* >( pose.data().get_ptr( CacheableDataType::TEMPLATE_HYBRIDIZATION_HISTORY )() ));
592  history.set( frag->start(), frag->stop(), templ_id );
593  }
594  }
595 
596  if (action == 3) {
597  // pick an insert position based on gap
598  utility::vector1<core::Real> residuals( n_prot_res , 0.0 );
599  utility::vector1<core::Real> max_residuals(3,0);
600  utility::vector1<int> max_poses(4,-1);
601  for (int i=1; i<(int)n_prot_res; ++i) {
602  if (!pose.residue_type(i).is_protein()){
603  residuals[i] = -1;
604  }
605  else if (pose.fold_tree().is_cutpoint(i+1)) {
606  residuals[i] = -1;
607  } else {
609  c0 = pose.residue(i).atom(" C ").xyz();
610  n1 = pose.residue(i+1).atom(" N ").xyz();
611  core::Real d2 = c0.distance( n1 );
612  residuals[i] = (d2-1.328685)*(d2-1.328685);
613  if ( residuals[i] > max_residuals[1]) {
614  max_residuals[3] = max_residuals[2]; max_residuals[2] = max_residuals[1]; max_residuals[1] = residuals[i];
615  max_poses[3] = max_poses[2]; max_poses[2] = max_poses[1]; max_poses[1] = i;
616  } else if ( residuals[i] > max_residuals[2]) {
617  max_residuals[3] = max_residuals[2]; max_residuals[2] = residuals[i];
618  max_poses[3] = max_poses[2]; max_poses[2] = i;
619  } else if ( residuals[i] > max_residuals[3]) {
620  max_residuals[3] = residuals[i];
621  max_poses[3] = i;
622  }
623  }
624  }
625 
626  // 25% chance of random position
627  max_poses[ 4 ] = numeric::random::random_range(1,n_prot_res);
628  int select_position = numeric::random::random_range(1,4);
629  if (select_position == 4)
630  action_string = action_string+"_rand";
631  core::Size max_pos = max_poses[ select_position ];
632 
633  // select random pos in [i-8,i]
634  core::Size insert_pos = max_pos - numeric::random::random_range(3,5);
635  insert_pos = std::min( insert_pos, n_prot_res-8);
636  insert_pos = std::max( (int)insert_pos, 1);
637 
638  if (library_.find(insert_pos) != library_.end())
639  apply_frame (pose, library_[insert_pos]);
640  }
641 
642  // MC
643  try {
644  (*min_scorefxn_)(pose);
645  if ( m<4 || linmin_only_ )
646  minimizer.run( pose, mm, *min_scorefxn_, options );
647  else
648  minimizer.run( pose, mm, *min_scorefxn_, options_minilbfgs );
649 
650  mc->boltzmann( pose , action_string );
651  } catch( utility::excn::EXCN_Base& excn ) {
652  //fpd hbond fail? start over
653  pose = pose_in;
654  goto sampler;
655  }
656 
657  if (n%100 == 0) {
658  TR << "Step " << n << std::endl;
659  mc->show_scores();
660  mc->show_counters();
661  }
662 
663  }
664  mc->recover_low(pose);
665  }
666 
667  //for (int i=1; i<=templates_.size(); ++i) {
668  // std::ostringstream oss;
669  // oss << "templ"<<i<<".pdb";
670  // templates_[i]->dump_pdb( oss.str() );
671  //}
672 
673  // final minimization
674  try {
675  (*min_scorefxn_)(pose); minimizer.run( pose, mm, *min_scorefxn_, options_lbfgs );
676  (*nocst_scorefxn_)(pose); minimizer.run( pose, mm, *nocst_scorefxn_, options_lbfgs );
677  (*bonds_scorefxn_)(pose); minimizer.run( pose, mm, *bonds_scorefxn_, options_lbfgs );
678  (*nocst_scorefxn_)(pose); minimizer.run( pose, mm, *nocst_scorefxn_, options_lbfgs );
679  } catch( utility::excn::EXCN_Base& excn ) {
680  //fpd hbond fail? start over
681  pose = pose_in;
682  goto sampler;
683  }
684 
685  lowres_scorefxn_->set_weight( core::scoring::cart_bonded, max_cart );
686  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_angle, max_cart_angle );
687  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_length, max_cart_length );
688  lowres_scorefxn_->set_weight( core::scoring::cart_bonded_torsion, max_cart_torsion );
690  lowres_scorefxn_->set_weight( core::scoring::vdw, max_vdw );
691  (*lowres_scorefxn_)(pose);
692 }
693 
694 }
695 }