Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundedCollector.hh
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/BoundedCollector.hh
11 /// @brief
12 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
13 
14 #ifndef INCLUDED_protocols_frag_picker_BoundedCollector_hh
15 #define INCLUDED_protocols_frag_picker_BoundedCollector_hh
16 
17 // package headers
19 // AUTO-REMOVED #include <protocols/frag_picker/BoundedPriorityQueue.hh>
25 
27 
28 // utility headers
29 #include <core/types.hh>
30 
31 // AUTO-REMOVED #include <basic/prof.hh>
32 
33 /// ObjexxFCL headers
34 #include <ObjexxFCL/format.hh>
35 
36 #include <iostream>
37 
38 #include <utility/vector1.hh>
39 
40 //Auto using namespaces
41 namespace std { } using namespace std; // AUTO USING NS
42 //Auto using namespaces end
43 
44 //#include <algorithm>
45 
46 namespace protocols {
47 namespace frag_picker {
48 
49 
50 /// @brief Keeps the N best fragments candidates for the final selection
51 /// @detailed The purpose of a collector is to keep the best fragment candidates to the end
52 /// of vall processing. In particular, the capacity of this collector may be larger than
53 /// the number of fragments one wants to get
54 template< class StrictWeakOrdering >
55 class BoundedCollector: public CandidatesCollector {
56 public:
59 
60 public:
61 
62  /// @brief create a collector for a given size of a query sequence
64  Size query_size,
65  Size max_frags_per_pos,
66  StrictWeakOrdering fragment_comparator,
67  Size n_score_terms,
68  Size buffer_factor = 5
69  ) {
70 
71  FragmentCandidateOP worst_f = new FragmentCandidate(1,1,0,1);
72  scores::FragmentScoreMapOP worst_s = new scores::FragmentScoreMap(n_score_terms);
73  for(Size i=1;i<=n_score_terms;i++)
74  worst_s->set_score_component(99999.9999,i);
75 
76  for (Size i = 1; i <= query_size; i++) {
78  scores::FragmentScoreMapOP >, StrictWeakOrdering > queue(
79  fragment_comparator, max_frags_per_pos,max_frags_per_pos*buffer_factor);
80  queue.set_worst( std::pair<FragmentCandidateOP,
81  scores::FragmentScoreMapOP>(worst_f,worst_s) );
82  storage_.push_back(queue);
83  }
84  }
85 
86  /// @brief Insert a fragment candidate to the container
87  inline bool add( ScoredCandidate new_canditate) {
88 
89  return storage_[new_canditate.first->get_first_index_in_query()].push(
90  new_canditate);
91  }
92 
93  /// @brief Check how many candidates have been already collected for a given position
94  /// APL Note: you cannot have inlined virtual functions
95  inline Size count_candidates(Size seq_pos) const {
96  return storage_[seq_pos].size();
97  }
98 
99  /// @brief Check how many candidates have been already collected for all positions
100  /// APL Note: you cannot have inlined virtual functions
101  inline Size count_candidates() const {
102 
103  Size response = 0;
104  for(Size i=1;i<=storage_.size();++i)
105  response += storage_[i].size();
106  return response;
107  }
108 
109  /// @brief Check the size of query sequence that this object knows.
110  /// This is mainly to be able to check if it is the same as in the other parts of
111  /// fragment picking machinery.
112  inline Size query_length() const {
113  return storage_.size();
114  }
115 
116  /// @brief Inserts candidates from another Collector for a give position in the query
117  /// Candidates may or may not get inserted depending on the candidate
118  void insert(Size pos, CandidatesCollectorOP collector) {
119  BoundedCollectorOP c = dynamic_cast<BoundedCollector*> (collector());
120  if (c == 0) {
121  utility_exit_with_message("Cant' cast candidates' collector to BoundedCollector.");
122  }
123  ScoredCandidatesVector1 & content = c->get_candidates(pos);
124  for(Size l=1;l<=content.size();l++) storage_.at(pos).push( content[l] );
125  }
126 
127  /// @brief returns all stored fragment candidates that begins at a given position in a query
128  inline ScoredCandidatesVector1 & get_candidates( Size position_in_query ) {
129  return storage_.at(position_in_query).expose_data();
130  }
131 
132  inline void clear() {
133 
134  for (Size i_pos = 1; i_pos <= storage_.size(); ++i_pos)
135  storage_[i_pos].clear();
136  }
137 
138  /// @brief prints how many candidates have been collected for each position and how good they are
139  void print_report(std::ostream& out, scores::FragmentScoreManagerOP scoring) const {
140  using namespace ObjexxFCL::fmt;
141  out
142  << "\n pos count best worst | pos count best worst | pos count best worst |\n";
143  Size cnt = 0;
144  for (Size i_pos = 1; i_pos <= storage_.size(); ++i_pos) {
145 
146  if (storage_[i_pos].size() <= 1) {
147  out << I(4, i_pos) << " 0 |";
148  } else {
149  out << I(4, i_pos) << " " << I(6, storage_[i_pos].size())
150  << " ";
151  out << F(8, 2, scoring->total_score(
152  storage_[i_pos].peek_front().second)) << " ";
153  out << F(8, 2, scoring->total_score(
154  storage_[i_pos].peek_back().second)) << " |";
155  }
156  ++cnt;
157  if (cnt % 3 == 0)
158  out << '\n';
159  }
160  out << std::endl;
161  }
162 
163 private:
165  scores::FragmentScoreMapOP> , StrictWeakOrdering> > storage_;
166 };
167 
168 // Concrete version for PyRosetta
169 class BoundedCollector_CompareTotalScore : public BoundedCollector<CompareTotalScore>
170 {
171 public:
172  BoundedCollector_CompareTotalScore(Size query_size, Size max_frags_per_pos, CompareTotalScore fragment_comparator,Size n_score_terms,Size buffer_factor = 5)
173  : BoundedCollector<CompareTotalScore>(query_size, max_frags_per_pos, fragment_comparator, n_score_terms, buffer_factor) {};
174 };
175 
176 } // frag_picker
177 } // protocols
178 
179 
180 #endif /* INCLUDED_protocols_frag_picker_BoundedCollector_HH */