Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FragmentScoreManager.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 && 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/scores/FragmentScoreManager.hh
11 /// @brief
12 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
13 
14 
15 // package headers
21 
64 
65 #include <basic/Tracer.hh>
66 
67 #include <utility/io/izstream.hh>
68 
69 // C++
70 #include <algorithm>
71 #include <map>
72 #include <utility>
73 
75 #include <utility/vector1.hh>
76 
77 static basic::Tracer tr("protocols.frag_picker.FragmentScoreManager");
78 
79 namespace protocols {
80 namespace frag_picker {
81 namespace scores {
82 
83 /// @details Auto-generated virtual destructor
85 
86 /// @brief creates an empty score map
87 /// @detailed this is the recommended way to create FragmentScoreMap objects since FragmentScoreManager knows exactly
88 /// what is the correct size of the map i.e. how many scoring terms have been registered.
90 
91  return new FragmentScoreMap(score_weights_.size());
92 }
93 
94 
96  return elem1->get_priority() > elem2->get_priority();
97 }
98 
100  FragmentScoringMethodOP scoring_term,
101  Real weight) {
102 
103  std::map<FragmentScoringMethodOP, Real> weights;
104  scores_.push_back(scoring_term);
105  score_weights_.push_back(weight);
106 
107  for (Size i = 1; i <= scores_.size(); i++) {
108  std::pair<FragmentScoringMethodOP, Real> p(scores_[i],
109  score_weights_[i]);
110  weights.insert(p);
111  }
112 
113  sort(scores_.begin(), scores_.end(), sort_scores);
114 
115  for (Size i = 1; i <= scores_.size(); i++) {
116  scores_[i]->set_id(i);
117  score_weights_[i] = weights.find(scores_[i])->second;
118  }
119  width_.insert(std::pair<FragmentScoringMethodOP, Size>(scoring_term,
120  default_width_));
121  precision_.insert(std::pair<FragmentScoringMethodOP, Size>(scoring_term,
123 }
124 
126 
127  if (!f->was_modified())
128  return f->get_most_recent_total_score();
129 
130  Real total = 0;
131  utility::vector1<Real> &s = f->get_score_components();
132 
133  assert (s.size() == score_weights_.size());
134  for (Size i = 1; i <= score_weights_.size(); i++)
135  total += score_weights_[i] * s[i];
136  f->recent_total_ = total;
137  f->was_modified_ = false;
138 
139  return total;
140 }
141 
143 
144  for (Size iScore = 1; iScore <= scores_.size(); ++iScore) {
145  if (( zeros_score_later_ ) && ( fabs(score_weights_[iScore]) < 0.000001 )) continue;
146  CachingScoringMethodOP score =
147  dynamic_cast<CachingScoringMethod*> (scores_[iScore].get());
148  if (score != 0)
149  score->do_caching(chunk);
150  }
151 }
152 
154 
155  for (Size iScore = 1; iScore <= scores_.size(); ++iScore) {
156  CachingScoringMethodOP score =
157  dynamic_cast<CachingScoringMethod*> (scores_[iScore].get());
158  if (score != 0)
159  score->clean_up();
160  }
161 }
162 
164 
165  out
166  << "Fragment scoring scheme used:\n-----------------------------------------\n";
167  out << "id fragment score method name weight\n";
168  for (Size i = 1; i <= scores_.size(); i++)
169  out << std::setw(3) << scores_[i]->get_id() << " " << std::setw(30)
170  << scores_[i]->get_score_name() << std::setw(5)
171  << score_weights_[i] << "\n";
172  out << "-----------------------------------------\n" << std::endl;
173 }
174 
176  FragmentCandidateOP, FragmentScoreMapOP> > const& pairs,
177  std::ostream& out) {
178 
179  using namespace ObjexxFCL::fmt;
180  if ( pairs.size() == 0 ) return;
181  bool if_quota = false;
182  if( pairs[1].second->get_quota_score() < 999.98 ) if_quota = true;
183  out << "#" << RJ(10, "query_pos ");
184  out << RJ(10, "vall_pos ");
185  out << RJ(6, "pdbid");
186  out << " c ss ";
187  utility::vector1<Size> w(scores_.size()+4);
188  for (Size i = 1; i <= scores_.size(); i++) {
189  w[i] = width_.find(scores_[i])->second;
190  out << " " << scores_[i]->get_score_name();
191  if (scores_[i]->get_score_name().length() > w[i])
192  w[i] = scores_[i]->get_score_name().length();
193  }
194  if(if_quota) {
195  w[scores_.size()+1] = 10; // 10 characters for the total quota score
196  w[scores_.size()+2] = 9; // 9 characters for the total score
197  w[scores_.size()+3] = 10; // 10 characters for the pool name
198  out << " QUOTA_TOT TOTAL POOL_NAME FRAG_ID"<<std::endl;
199  } else {
200  w[scores_.size()+1] = 9; // 9 characters for the total score
201  out << " TOTAL FRAG_ID"<<std::endl;
202  }
203  for (Size iF = 1; iF <= pairs.size(); ++iF) {
204  if ( !pairs[iF].first || !pairs[iF].second ) {
205  tr.Warning << "final_frag candidate " << iF << " is corrupted. skipping... " << std::endl;
206  continue;
207  }
208  FragmentCandidateOP fr = pairs[iF].first;
209  FragmentScoreMapOP sc = pairs[iF].second;
210  VallResidueOP r = fr->get_residue(1);
211  out << " " << I(10, fr->get_first_index_in_query());
212 // out << " " << I(10, fr->get_first_index_in_vall());
213  out << " " << I(10, r->resi());
214  out << " " << RJ(5, fr->get_pdb_id());
215  out << " " << fr->get_chain_id();
216  out << " " << fr->get_middle_ss();
217 
218  for (Size i = 1; i <= scores_.size(); i++) {
219  Size p = precision_.find(scores_[i])->second;
220  out << " " << F(w[i], p, sc->get_score_components()[i]);
221  }
222  if(if_quota) {
223  out << " " << F(w[scores_.size()+1],TOTAL_PRECISION,sc->get_quota_score());
224  out << " " << F(w[scores_.size()+1],TOTAL_PRECISION,total_score(sc));
225  out << " " << std::setw(w[scores_.size()+3])<<fr->get_pool_name();
226  } else
227  out << F(w[scores_.size()+1],TOTAL_PRECISION,total_score(sc));
228 
229  assert ( fr->key() > 0 );
230  assert ( fr->key() < 4000000 ); // Put your Vall's size here
231 
232  out << I(10, fr->key() ) << std::endl;
233 
234  }
235 }
236 
238  FragmentScoreMapOP empty_map) {
239 
240  for (Size iScore = 1; iScore <= scores_.size(); iScore++) {
241  if (( zeros_score_later_ ) && ( fabs(score_weights_[iScore]) < 0.000001 ))
242  continue;
243  if (!scores_[iScore]->score(candidate, empty_map))
244  return false;
245  }
246  return true;
247 }
248 
250  FragmentCandidateOP candidate,
251  FragmentScoreMapOP empty_map)
252 {
253  for ( Size iScore = 1; iScore <= scores_.size(); iScore++ ) {
254  if ( fabs(score_weights_[iScore]) < 0.000001 )
255  scores_[iScore]->score(candidate, empty_map);
256  }
257  return true;
258 }
259 
260 
262  FragmentCandidateOP candidate,
263  FragmentScoreMapOP empty_map)
264 {
265  for ( Size iScore = 1; iScore <= scores_.size(); iScore++ ) {
266  if (( zeros_score_later_ ) && ( fabs(score_weights_[iScore]) < 0.000001 ))
267  continue;
269  dynamic_cast<CachingScoringMethod*> (scores_[iScore].get());
270  if ( s != 0 ) {
271  if ( !s->cached_score(candidate, empty_map) )
272  return false;
273  } else {
274  if ( !scores_[iScore]->score(candidate, empty_map) )
275  return false;
276  }
277  }
278  return true;
279 }
280 
282  MakeFragmentScoringMethodOP scoring_term_maker) {
283  registered_makers_[scoring_term_maker->get_score_name()]
284  = scoring_term_maker;
285 }
286 
288 
289  default_precision_ = 2;
290  default_width_ = 6;
291  zeros_score_later_ = true;
292 
335 
336 }
337 
339  std::string const & score_name, Size priority, Real weight,
340  Real lowest, bool use_lowest, FragmentPickerOP picker, std::string config_line) {
341 
342  std::map<std::string, MakeFragmentScoringMethodOP>::iterator m =
343  registered_makers_.find(score_name);
344  if (m == registered_makers_.end()) {
345  utility_exit_with_message("[ERROR]: unknown score type " + score_name
346  + "!");
347  }
348 
349  add_scoring_method(m->second->make(priority, lowest, use_lowest, picker, config_line),
350  weight);
351 }
352 
354  FragmentPickerOP picker) {
355 
356  utility::io::izstream input(file_name.c_str());
357  if (!input) {
358  utility_exit_with_message("[ERROR]: can't open file " + file_name + "!");
359  }
360 
361  std::string line;
362  std::string score_name;
363  Size priority;
364  Real weight;
365  std::string low_string;
366  Real lowest;
367  bool use_lowest;
368  std::string remark("");
369 
370  while (getline(input, line)) {
371  if (line[0] == '#')
372  continue;
373 
374  std::istringstream line_stream(line);
375  line_stream >> score_name >> priority >> weight >> low_string;
376 
377  if (low_string == "-") {
378  lowest = 0.0;
379  use_lowest = false;
380  } else {
381  std::istringstream low_stream(low_string);
382  //lowest = static_cast<Real>(low_string);
383  low_stream >> lowest;
384  use_lowest = true;
385  }
386 
387  remark = "";
388  if (line_stream.good()) {
389  line_stream >> remark;
390  }
391 
392 // std::cout << line << std::endl;
393 // std::cout << score_name << " " << priority << " " << weight << " " << low_string << " " << use_lowest << " " << remark << std::endl;
394 
395  create_scoring_method(score_name, priority, weight, lowest, use_lowest, picker, remark);
396  }
397 }
398 
399 } // scores
400 } // frag_picker
401 } // protocols
402 
403