Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MatchConsolidator.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 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file protocols/match/output/MatchConsolidator.cc
12 /// @brief
13 /// @author Alex Zanghellini (zanghell@u.washington.edu)
14 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com), porting to mini
15 
16 // Unit headers
18 
19 // Package headers
20 #include <protocols/match/Hit.hh>
21 
25 
26 // Utility headers
27 #include <utility/exit.hh>
28 // AUTO-REMOVED #include <utility/sort_predicates.hh>
29 // AUTO-REMOVED #include <utility/vector1.functions.hh>
30 
31 // C++ headers
32 #include <algorithm>
33 // AUTO-REMOVED #include <iostream>
34 
35 #include <utility/vector0.hh>
36 #include <utility/vector1.hh>
37 
38 
39 namespace protocols {
40 namespace match {
41 namespace output {
42 
43 MatchConsolidator::MatchConsolidator() : n_to_output_per_group_( 5 ) {}
44 
46 
47 void
49 {
50  runtime_assert( grouper_ );
51  runtime_assert( evaluator_ );
53  match_groups_.clear();
54  grouper_->reset();
55 
56 }
57 
58 void
60  match const & m
61 )
62 {
63  runtime_assert( grouper_ );
64  runtime_assert( evaluator_ );
65  runtime_assert( writer_ );
66  //runtime_assert( match_dspos1_groups_.size() == 0 );
68 
69  if( !this->passes_filters( m ) ) return;
70 
71  Size group = grouper_->assign_group_for_match( m );
72 
73  if ( group > match_groups_.size() ) {
74  match_groups_.resize( group );
76  }
77  Real score = evaluator_->score( m );
78  match_groups_[ group ]->add_match( m, score );
79  //std::cout << ".";
80 }
81 
82 void
84  match_dspos1 const & m
85 )
86 {
87  runtime_assert( grouper_ );
88  runtime_assert( evaluator_ );
89  runtime_assert( writer_ );
90  //runtime_assert( match_groups_.size() == 0 );
92 
93  if( !this->passes_filters( m ) ) return;
94 
95  Size group = grouper_->assign_group_for_match( m );
96 
97  if ( group > match_groups_.size() ) {
98  match_groups_.resize( group );
100  }
101  Real score = evaluator_->score( m );
102  match_groups_[ group ]->add_match_dspos1( m, score );
103  //std::cout << ".";
104 }
105 
106 
107 void
109 {
110  /// Can only handle regular match objects or match_dspos1's but not both.
111  //runtime_assert( match_dspos1_groups_.size() == 0 || match_groups_.size() == 0 );
112 
113  runtime_assert( grouper_ );
114  runtime_assert( evaluator_ );
115 
116  //std::cout << std::endl;
117  //std::cout << "match groups: " << match_groups_.size() << std::endl;
118 
119  for ( Size ii = 1; ii <= match_groups_.size(); ++ii ) {
120  //std::cout << ":";
121  for ( Size jj = 1; jj <= match_groups_[ ii ]->n_kept_matches(); ++jj ) {
122  if ( ! match_groups_[ ii ]->dspos1_mode() ) {
123  writer_->record_match( match_groups_[ ii ]->kept_match( jj ) );
124  } else {
125  writer_->record_match( match_groups_[ ii ]->kept_match_dspos1( jj ) );
126  }
127  }
128  }
130 }
131 
132 void
134 {
135 
136  /*for ( Size ii = 1; ii <= match_groups_.size(); ++ii ) {
137  Size ii_n_matches = match_groups_[ ii ].size();
138  utility::vector1< match > ii_matches( ii_n_matches );
139  utility::vector1< Real > ii_scores( ii_n_matches, 0.0 );
140  std::copy( match_groups_[ ii ].begin(), match_groups_[ ii ].end(), ii_matches.begin() );
141  for ( Size jj = 1; jj <= ii_n_matches; ++jj ) {
142  ii_scores[ jj ] = evaluator_->score( ii_matches[ jj ] );
143  }
144  utility::vector1< Size > top_score_indices( n_to_output_per_group_ );
145 
146  // either sort or, if it's going to be fast, use utility::arg_least_several
147  if ( n_to_output_per_group_ > 50 ) {
148  utility::vector1< std::pair< Real, Size > > score_index_pairs( ii_n_matches );
149  for ( Size jj = 1; jj <= ii_n_matches; ++jj ) {
150  score_index_pairs[ jj ] = std::make_pair( ii_scores[ jj ], jj );
151  }
152  std::sort( score_index_pairs.begin(), score_index_pairs.end(), utility::SortFirst< Real, Size >() );
153  for ( Size jj = 1; jj <= n_to_output_per_group_; ++jj ) {
154  if ( jj > ii_n_matches ) {
155  top_score_indices.resize( ii_n_matches );
156  break;
157  }
158  top_score_indices[ jj ] = score_index_pairs[ jj ].second;
159  }
160  } else {
161  utility::arg_least_several( ii_scores, top_score_indices );
162  }
163 
164  for ( Size jj = 1; jj <= top_score_indices.size(); ++jj ) {
165  writer_->record_match( ii_matches[ top_score_indices[ jj ]] );
166  }
167  }*/
168 }
169 
170 void
172 {
173 
174 /* for ( Size ii = 1; ii <= match_dspos1_groups_.size(); ++ii ) {
175  Size ii_n_matches = match_dspos1_groups_[ ii ].size();
176  utility::vector1< match_dspos1 > ii_matches( ii_n_matches );
177  utility::vector1< Real > ii_scores( ii_n_matches, 0.0 );
178  std::copy( match_dspos1_groups_[ ii ].begin(), match_dspos1_groups_[ ii ].end(), ii_matches.begin() );
179  for ( Size jj = 1; jj <= ii_n_matches; ++jj ) {
180  ii_scores[ jj ] = evaluator_->score( ii_matches[ jj ] );
181  }
182  utility::vector1< Size > top_score_indices( n_to_output_per_group_ );
183 
184  // either sort or, if it's going to be fast, use utility::arg_least_several
185  if ( n_to_output_per_group_ > 50 ) {
186  utility::vector1< std::pair< Real, Size > > score_index_pairs( ii_n_matches );
187  for ( Size jj = 1; jj <= ii_n_matches; ++jj ) {
188  score_index_pairs[ jj ] = std::make_pair( ii_scores[ jj ], jj );
189  }
190  std::sort( score_index_pairs.begin(), score_index_pairs.end(), utility::SortFirst< Real, Size >() );
191  for ( Size jj = 1; jj <= n_to_output_per_group_; ++jj ) {
192  if ( jj > ii_n_matches ) {
193  top_score_indices.resize( ii_n_matches );
194  break;
195  }
196  top_score_indices[ jj ] = score_index_pairs[ jj ].second;
197  }
198  } else {
199  utility::arg_least_several( ii_scores, top_score_indices );
200  }
201 
202  for ( Size jj = 1; jj <= top_score_indices.size(); ++jj ) {
203  writer_->record_match( ii_matches[ top_score_indices[ jj ]] );
204  }
205  }*/
206 }
207 
208 
209 void
211 {
212  n_to_output_per_group_ = setting;
213 }
214 
215 void
217 {
218  grouper_ = grouper;
219 }
220 
221 void
223 {
224  evaluator_ = evaluator;
225 }
226 
227 void
229 {
230  if ( grouper_ ) grouper_->reset();
231 }
232 
234 
236  Size n_to_keep,
237  bool dspos1_mode
238 ) :
239  n_top_matches_to_keep_( n_to_keep ),
240  dspos1_mode_( dspos1_mode ),
241  scores_heap_( n_to_keep ),
242  best_matches_( dspos1_mode ? 0 : n_to_keep ),
243  best_match_dspos1s_( dspos1_mode ? n_to_keep : 0 )
244 {
245 }
246 
247 void
249 {
250  assert( ! dspos1_mode_ );
251  Size new_pos = index_for_new_match( score );
252  if ( new_pos != 0 ) {
253  best_matches_[ new_pos ] = m;
254  }
255 }
256 
257 void
259 {
260  assert( dspos1_mode_ );
261  Size new_pos = index_for_new_match( score );
262  if ( new_pos != 0 ) {
263  best_match_dspos1s_[ new_pos ] = m;
264  }
265 }
266 
269 {
270  return scores_heap_.size();
271 }
272 
273 match const &
275 {
276  assert( ! dspos1_mode_ );
277  assert( (int) which_match <= scores_heap_.size() );
278  return best_matches_[ which_match ];
279 }
280 
281 match_dspos1 const &
283 {
284  assert( ! dspos1_mode_ );
285  assert( (int) which_match <= scores_heap_.size() );
286  return best_match_dspos1s_[ which_match ];
287 }
288 
289 
292 {
293  Real negscore = -1 * score;
294  if ( scores_heap_.size() < scores_heap_.capacity() ) {
295  int new_pos( scores_heap_.size() + 1 );
296  bool err;
297  scores_heap_.heap_insert( new_pos, negscore, err );
298  assert( ! err );
299  return new_pos;
300  } else {
301  if ( scores_heap_.heap_head() < negscore ) {
302  int old_pos; float oldscore; bool err;
303  scores_heap_.heap_extract( old_pos, oldscore, err );
304  assert( ! err );
305  assert( negscore > oldscore );
306  scores_heap_.heap_insert( old_pos, negscore, err );
307  assert( ! err );
308  return old_pos; // overwrite this position
309  } // else return 0 .. moved below to appease the compiler
310  }
311  return 0;
312 }
313 
314 }
315 }
316 }