Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QuotaSelector.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/frag_picker/quota/QuotaSelector.hh
11 /// @brief provides a quota selector based on difefrent secondary structure predicitons
12 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
13 
18 
19 // package headers
23 
24 // utility headers
25 #include <core/types.hh>
26 #include <basic/Tracer.hh>
27 
28 // C++
29 #include <algorithm>
30 
31 #include <utility/vector1.hh>
32 
33 
34 namespace protocols {
35 namespace frag_picker {
36 namespace quota {
37 
38 static basic::Tracer trQuotaSelector(
39  "protocols.frag_picker.quota.QuotaSelector");
40 
41 QuotaSelector::QuotaSelector(Size frags_per_pos,Size pos_in_query,QuotaCollectorOP collector) :
42  FragmentSelectingRule(frags_per_pos),collector_(collector) {
43 
44  trQuotaSelector.Trace << "Selector set up for pos. "<<pos_in_query<<std::endl;
45  q_pos_ = pos_in_query;
46  collector_ = collector;
47 }
48 
49 
50 Size QuotaSelector::next_from_pool( ScoredCandidatesVector1 const& pool,Size recently_taken,std::set<Size> & in_use) {
51  if( recently_taken >= pool.size() )
52  return 0;
53  recently_taken++;
54 
55  for(Size i=recently_taken;i<=pool.size();i++) {
56  if ( pool[i].first == 0 ) {
57  trQuotaSelector.Warning << "Fragment candidate at pos. " << i
58  << " from a pool is null - nothing to select\n"
59  << " maybe your vall database is to small?" << std::endl;
60  return 0;
61  }
62  if( in_use.find( pool[i].first->key() ) == in_use.end() ) {
63  in_use.insert( pool[i].first->key() );
64  return i;
65  }
66  }
67 
68  return 0;
69 }
70 
71 
73  bool operator() (std::pair<Size,Size> i,std::pair<Size,Size> j) { return (i.second>j.second); }
75 
77  bool operator() (std::pair<Size,Size> i,std::pair<Size,Size> j) { return (i.second<j.second); }
79 
81  bool operator() (std::pair<Size,Real> i,std::pair<Size,Real> j) { return (i.second>j.second); }
83 
84 
86 
87  Size total = 0;
89  for(Size i=1;i<=q_limits.size();i++) {
90  total += q_limits[i];
91  tmp.push_back( std::pair<Size,Size>(i,q_limits[i]) );
92  }
93  Size diff = target_total - total;
94  std::sort(tmp.begin(),tmp.end(),mysorter_biggest_first);
95  trQuotaSelector.Trace << "Pushing the quota limits from total "<<total<<" to "<<target_total
96  <<", the first pool has size "<<tmp[1].second<<std::endl;
97  while(2==2) {
98  for(Size i=1;i<=q_limits.size();i++) {
99  if(diff==0)
100  return;
101  q_limits[ tmp[i].first ] ++;
102  diff--;
103  }
104  }
105 }
106 
107 
109 
110  Size total = 0;
112  for(Size i=1;i<=q_limits.size();i++) {
113  total += q_limits[i];
114  tmp.push_back( std::pair<Size,Size>(i,q_limits[i]) );
115  }
116  Size diff = target_total - total;
117  std::sort(tmp.begin(),tmp.end(),mysorter_biggest_first);
118  trQuotaSelector.Trace << "Pushing the quota limits from total "<<total<<" to "<<target_total
119  <<", the first pool has size "<<tmp[1].second<<" and takes extra "<<diff<<std::endl;
120  q_limits[ tmp[1].first ] += diff;
121 }
122 
124  utility::vector1<Real> & q_fractions) {
125 
126 
127  Size total = 0;
128  for(Size i=1;i<=q_limits.size();i++) {
129  total += q_limits[i];
130  }
131  Size diff = target_total - total;
132 
133  trQuotaSelector.Trace << "Pushing the quota limits from total "<<total<<" to "<<target_total
134  <<std::endl;
135 
137  while (diff > 0) {
138  tmp.clear();
139  for(Size i=1;i<=q_limits.size();i++) {
140  Real missing = q_fractions[i] - ((Real) q_limits[i]) / target_total;
141  tmp.push_back( std::pair<Size,Real>(i,missing) );
142  }
143  std::sort(tmp.begin(),tmp.end(),mysorter_missing_fraction_biggest_first);
144  q_limits[ tmp[1].first ] ++;
145 // for(Size i=1;i<=q_limits.size();i++)
146 // trQuotaSelector.Trace << "pool id,old fraction, new fraction : "<<tmp[i].first<<" "<<tmp[i].second<<" "<<
147 // ((Real) q_limits[ tmp[i].first ]) / target_total<<std::endl;
148  diff--;
149  }
150 }
151 
152 
155  ScoredCandidatesVector1& output_selection
156 ) {
157 
158  utility::vector1<Size> last_selected(collector_->count_pools(q_pos_),0);
159  Size n = 0;
160  std::set<Size> nice_collection;
161  bool something_taken = false;
162  utility::vector1<Size> q_limits;
164  Size total = 0;
165  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
166  q_limits.push_back( collector_->get_pool(q_pos_,i)->total_size() );
167  total += collector_->get_pool(q_pos_,i)->total_size();
168  q_cnt.push_back(0);
169  }
170  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
171  if(total == 0)
172  q_limits[i] = 0;
173  else
174  q_limits[i] = (q_limits[i] * frags_per_pos() / total);
175  }
176 
177  push_the_limits( q_limits, frags_per_pos() );
178  do {
179  something_taken = false;
180  for ( Size i=1;i<=collector_->count_pools(q_pos_);++i ) {
181  ScoredCandidatesVector1 const& fs = collector_->get_pool(q_pos_,i)->get_candidates(0);
182  if ( q_cnt[i] == q_limits[i] )
183  continue;
184  Size ifrag = next_from_pool( fs, last_selected[i], nice_collection );
185  if ( ifrag > 0 ) {
186  output_selection.push_back( fs[ifrag] );
187  n++;
188  something_taken = true;
189  q_cnt[i]++;
190  }
191  }
192  } while((n < frags_per_pos()) && (something_taken==true));
193  trQuotaSelector.Trace<<"Position "<<q_pos_<<" done, "<< output_selection.size()<<" fragments selected."<<std::endl;
194 }
195 
196 
199  ScoredCandidatesVector1& output_selection
200 ) {
201  if( collector_->count_candidates() == 0 )
202  return;
203  utility::vector1<Size> last_selected(collector_->count_pools(q_pos_),0);
204  Size frag_size = collector_->get_pool(q_pos_,1)->get_candidates(0)[1].first->get_length();
205  Size n = 0;
206  std::set<Size> nice_collection;
207  bool something_taken = false;
208  utility::vector1<Size> q_limits;
210  utility::vector1<Real> q_frac;
211  Real total = 0.0;
212  for(Size i=1;i<=collector_->count_pools(q_pos_);++i)
213  total += collector_->get_pool(q_pos_,i)->get_fraction();
214  total = 25.0 / total;
215  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
216  q_limits.push_back( (Size)(collector_->get_pool(q_pos_,i)->get_fraction() * total) );
217  q_cnt.push_back(0);
218  q_frac.push_back( collector_->get_pool(q_pos_,i)->get_fraction() );
219  }
220 
221  push_the_limits( q_limits, 25, q_frac );
222  if(trQuotaSelector.Trace.visible()) {
223  trQuotaSelector.Trace<<"Position: "<<q_pos_<<" frag_size:"<<frag_size<<" pool allowances for the first 25 fragments are:\n";
224  for(Size i=1;i<=collector_->count_pools(q_pos_);++i)
225  trQuotaSelector.Trace<<collector_->get_pool(q_pos_,i)->get_pool_name()<<" "<<q_limits[i]
226  <<" ("<<collector_->get_pool(q_pos_,i)->get_fraction()*25<<")\n";
227  trQuotaSelector.Trace<<std::endl;
228  }
229  do {
230  something_taken = false;
231  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
232  if( q_cnt[i] == q_limits[i] )
233  continue;
234  QuotaPoolOP the_pool = collector_->get_pool(q_pos_,i);
235  ScoredCandidatesVector1 const& fs = the_pool->get_candidates(0);
236  Size ifrag = next_from_pool( fs, last_selected[i], nice_collection );
237  if( ifrag > 0 ) {
238  fs[ifrag].second->set_quota_score( the_pool->quota_score( fs[ifrag] ) );
239  fs[ifrag].first->set_pool_name( the_pool->get_pool_name() );
240 
241  output_selection.push_back( fs[ifrag] );
242  n++;
243  something_taken = true;
244  q_cnt[i]++;
245  }
246  }
247  } while((n < 25) && (something_taken==true));
248 
249  utility::vector1<Real> tmp(FragmentPicker::log_25_.max_pools(),1000000000000.0);
250  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
251  QuotaPoolOP p = collector_->get_pool(q_pos_,i);
252  tmp[FragmentPicker::log_25_.tag_map_[p->get_pool_name()]] = (q_limits[i] - p->get_fraction()*25);
253  }
254  FragmentPicker::log_25_.log(frag_size,q_pos_,tmp);
255 
256  Size fr_per_pos = frags_per_pos();
257  total = 0.0;
258  for(Size i=1;i<=collector_->count_pools(q_pos_);++i)
259  total += collector_->get_pool(q_pos_,i)->get_fraction();
260  total = ((Real) fr_per_pos) / total;
261  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
262  q_limits[i] = (Size)(collector_->get_pool(q_pos_,i)->get_fraction() * total);
263  }
264  push_the_limits( q_limits, fr_per_pos , q_frac);
265  if(trQuotaSelector.Trace.visible()) {
266  trQuotaSelector.Trace<<"Position: "<<q_pos_<<" frag_size:"<<frag_size<<" pool allowances are:\n";
267  for(Size i=1;i<=collector_->count_pools(q_pos_);++i)
268  trQuotaSelector.Trace<<collector_->get_pool(q_pos_,i)->get_pool_name()<<" "<<q_limits[i]
269  <<" ("<<collector_->get_pool(q_pos_,i)->get_fraction()*fr_per_pos<<")\n";
270  trQuotaSelector.Trace<<std::endl;
271  }
272  do {
273  something_taken = false;
274  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
275  if( q_cnt[i] == q_limits[i] )
276  continue;
277  QuotaPoolOP the_pool = collector_->get_pool(q_pos_,i);
278  ScoredCandidatesVector1 const& fs = the_pool->get_candidates(0);
279  Size ifrag = next_from_pool( fs, last_selected[i], nice_collection );
280  last_selected[i] = ifrag;
281  if( ifrag > 0 ) {
282  fs[ifrag].second->set_quota_score( the_pool->quota_score( fs[ifrag] ) );
283  fs[ifrag].first->set_pool_name( the_pool->get_pool_name() );
284  output_selection.push_back( fs[ifrag] );
285  n++;
286  something_taken = true;
287  q_cnt[i]++;
288  }
289  }
290  } while((n < fr_per_pos) && (something_taken==true));
291 
292  utility::vector1<Real> tmp2(FragmentPicker::log_200_.max_pools(),1000000000000.0);
293  for(Size i=1;i<=collector_->count_pools(q_pos_);++i) {
294  QuotaPoolOP p = collector_->get_pool(q_pos_,i);
295  tmp2[FragmentPicker::log_200_.tag_map_[p->get_pool_name()]] = (q_limits[i] - p->get_fraction()*fr_per_pos);
296  }
297  FragmentPicker::log_200_.log(frag_size,q_pos_,tmp2);
298 
299 // trQuotaSelector.Debug<<"Position "<<q_pos_<<" done, "<< output_selection.size()<<" fragments selected."<<std::endl;
300 }
301 
302 
303 } // quota
304 } // frag_picker
305 } // protocols