Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WeightedFragmentSmoothTrialMover.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 Fragment insertion and trial, each residue has a customized weight for the frequency of insertion
12 /// @author David Kim, Yifan Song
13 
15 
16 #include <core/fragment/Frame.hh>
18 
19 #include <numeric/random/random.hh>
20 #include <utility/exit.hh>
21 
22 static numeric::random::RandomGenerator RG(8401368);
23 static basic::Tracer TR( "protocols.hybridization.WeightedFragmentSmoothTrialMover" );
24 
25 namespace protocols {
26 namespace hybridization {
27 
28 using namespace core;
29 
32  utility::vector1< core::Real > const residue_weights,
33  utility::vector1< core::Size > const anchor_residues,
34  core::Size const nr_frags,
35  simple_moves::FragmentCostOP cost ) : cost_( cost )
36 {
37  moves::Mover::type( "WeightedFragmentSmoothTrialMover" );
38  frag_libs_ = frag_libs;
39  anchor_reses_ = anchor_residues;
40  weighted_sampler_.resize(frag_libs.size());
41  update_sampler_weights(residue_weights);
42  nr_frags_ = nr_frags;
43 }
44 
46 {
47  total_frames_ = 0;
48  for (Size i_frag_set = 1; i_frag_set<=frag_libs_.size(); ++i_frag_set) {
49  utility::vector1< core::Real > frame_weights(frag_libs_[i_frag_set]->nr_frames(), 0.0);
50  for (Size i_frame = 1; i_frame <= frag_libs_[i_frag_set]->nr_frames(); ++i_frame) {
51  core::fragment::FrameIterator frame_it = frag_libs_[i_frag_set]->begin(); // first frame of the fragment library
52  std::advance(frame_it, i_frame-1); // point frame_it to the i_frame of the library
53  core::Size seqpos_start = (*frame_it)->start(); // find starting and ending residue seqpos of the inserted fragment
54  core::Size seqpos_end = (*frame_it)->end();
55 
56  // disallow insertion across anchors
57  bool cross_anchor = false;
58  for (Size i_anchor = 1; i_anchor <= anchor_reses_.size() && !cross_anchor; ++i_anchor) {
59  if (anchor_reses_[i_anchor]>=seqpos_start && anchor_reses_[i_anchor]<=seqpos_end) cross_anchor = true;
60  }
61  if (cross_anchor) continue;
62 
63  for (Size seqpos = seqpos_start; seqpos <= seqpos_end; ++seqpos) { // accumulate the weights of all residues in the fragment
64  if (seqpos < 1 || seqpos > residue_weights.size()) {
65  utility_exit_with_message("FATAL. Fragment library size doesn't match with the size of protein.");
66  }
67  frame_weights[i_frame] += residue_weights[seqpos];
68  }
69  if (frame_weights[i_frame] > 0.0) total_frames_++;
70  }
71  weighted_sampler_[i_frag_set].weights(frame_weights);
72  }
73 }
74 
76 {
77 
78  bool success ( false );
79  core::Size nfail = 0;
80  while ( !success && ( nfail < 100 ) ) {
81 
82  // pick fragment set
83  core::Size i_frag_set = RG.random_range(1, frag_libs_.size());
84  // pick insertion position
85  core::Size insert_pos = weighted_sampler_[i_frag_set].random_sample(RG);
86 
87  core::fragment::FrameIterator frame_it = frag_libs_[i_frag_set]->begin();
88  std::advance(frame_it, insert_pos-1);
89  core::Size nr_frags = frame_it->nr_frags();
90  if (nr_frags_ && nr_frags_ < nr_frags) nr_frags = nr_frags_;
91 
92  //std::cout << "WeightedFragmentSmoothTrialMover::apply" << std::endl;
94  goodfrag.reserve( 200 );
95 
96  core::Real costmin = 1000;
97  core::Size minfrag = 0;
98 
99  //compute scores
101  cost_->score( **frame_it, pose, scores );
102 
103  for ( core::Size j = 1; j <= nr_frags; ++j ) {
104  core::Real s = scores[ j ];
105  //std::cout << "SmoothFragmentMover::choose_fragment: " << j << " score: " << s << std::endl;
106  if ( s < costmin ) {
107  costmin = s;
108  minfrag = j;
109  }
110  if ( s < cost_->cutoff() ) {
111  goodfrag.push_back( j );
112  }
113  }
114  //choose randomly one fragment of all those that are below cutoff
115  //or choose the best fragment. Fail if the minimal cost is > 12.0
116  core::Size frag_num;
117  if ( goodfrag.size()< 1 ) {
118  if ( costmin > 12. ) {
119  nfail++;
120  continue;
121  }
122  frag_num = minfrag;
123  } else {
124  frag_num = goodfrag[ static_cast< int >( RG.uniform() * goodfrag.size() )+1 ];
125  }
126  frame_it->apply( frag_num, pose );
127  success = true;
128  }
129  if (!success) {
130  TR.Error << "couldn't find a fragment to insert!!" << std::endl;
131  }
132 }
133 
135 {
136  return "WeightedFragmentSmoothTrialMover";
137 }
138 
139 } // hybridization
140 } // protocols