OpenMS
ScoredProcessingResult.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hendrik Weisser $
6 // $Authors: Hendrik Weisser $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 
13 namespace OpenMS
14 {
15  namespace IdentificationDataInternal
16  {
19  {
21 
23  AppliedProcessingSteps::nth_index<1>::type& getStepsAndScoresByStep()
24  {
25  return steps_and_scores.get<1>();
26  }
27 
29  const AppliedProcessingSteps::nth_index<1>::type&
31  {
32  return steps_and_scores.get<1>();
33  }
34 
41  {
42  auto step_pos =
43  steps_and_scores.get<1>().find(step.processing_step_opt);
44  if (step_pos == steps_and_scores.get<1>().end()) // new step
45  {
46  steps_and_scores.push_back(step);
47  }
48  else // existing step - add or update scores
49  {
50  steps_and_scores.get<1>().modify(
51  step_pos, [&](AppliedProcessingStep& old_step)
52  {
53  for (const auto& pair : step.scores)
54  {
55  old_step.scores[pair.first] = pair.second;
56  }
57  });
58  }
59  }
60 
63  const std::map<ScoreTypeRef, double>& scores =
64  std::map<ScoreTypeRef, double>())
65  {
66  AppliedProcessingStep applied(step_ref, scores);
67  addProcessingStep(applied);
68  }
69 
71  void addScore(ScoreTypeRef score_type, double score,
72  const std::optional<ProcessingStepRef>&
73  processing_step_opt = std::nullopt)
74  {
75  AppliedProcessingStep applied(processing_step_opt);
76  applied.scores[score_type] = score;
77  addProcessingStep(applied);
78  }
79 
82  {
83  // merge applied processing steps and scores:
84  for (const auto& step : other.steps_and_scores)
85  {
86  addProcessingStep(step);
87  }
88  // merge meta info - existing entries may be overwritten:
89  addMetaValues(other);
90 
91  return *this;
92  }
93 
101  std::pair<double, bool> getScore(ScoreTypeRef score_ref) const
102  {
103  std::tuple<double, std::optional<ProcessingStepRef>, bool> result =
104  getScoreAndStep(score_ref);
105  return std::make_pair(std::get<0>(result), std::get<2>(result));
106  }
107 
113  std::pair<double, bool> getScore(ScoreTypeRef score_ref,
114  std::optional<ProcessingStepRef>
115  processing_step_opt) const
116  {
117  auto step_pos = steps_and_scores.get<1>().find(processing_step_opt);
118  if (step_pos != steps_and_scores.get<1>().end())
119  {
120  auto score_pos = step_pos->scores.find(score_ref);
121  if (score_pos != step_pos->scores.end())
122  {
123  return std::make_pair(score_pos->second, true);
124  }
125  }
126  // not found:
127  return std::make_pair(std::numeric_limits<double>::quiet_NaN(), false);
128  }
129 
137  std::tuple<double, std::optional<ProcessingStepRef>, bool>
138  getScoreAndStep(ScoreTypeRef score_ref) const
139  {
140  // give priority to scores from later processing steps:
141  for (const auto& step : boost::adaptors::reverse(steps_and_scores))
142  {
143  auto pos = step.scores.find(score_ref);
144  if (pos != step.scores.end())
145  {
146  return std::make_tuple(pos->second, step.processing_step_opt, true);
147  }
148  }
149  // not found:
150  return std::make_tuple(std::numeric_limits<double>::quiet_NaN(),
151  std::nullopt, false);
152  }
153 
161  std::tuple<double, std::optional<ScoreTypeRef>, bool>
163  {
164  // check steps starting with most recent:
165  for (const auto& step : boost::adaptors::reverse(steps_and_scores))
166  {
167  auto top_score = step.getScoresInOrder(true);
168  if (!top_score.empty())
169  {
170  return std::make_tuple(top_score[0].second, top_score[0].first,
171  true);
172  }
173  }
174  return std::make_tuple(std::numeric_limits<double>::quiet_NaN(),
175  std::nullopt, false); // no score available
176  }
177 
180  {
181  Size counter = 0;
182  for (const auto& step : steps_and_scores)
183  {
184  counter += step.scores.size();
185  }
186  return counter;
187  }
188 
189  protected:
195  {
196  }
197 
200  };
201 
202  }
203 }
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:35
void addMetaValues(const MetaInfoInterface &from)
function to copy all meta values from one object to this one
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
boost::multi_index_container< AppliedProcessingStep, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::ordered_unique< boost::multi_index::member< AppliedProcessingStep, std::optional< ProcessingStepRef >, &AppliedProcessingStep::processing_step_opt > > > > AppliedProcessingSteps
Definition: AppliedProcessingStep.h:107
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:330
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Definition: AppliedProcessingStep.h:30
std::map< ScoreTypeRef, double > scores
Map of scores and their types.
Definition: AppliedProcessingStep.h:39
std::optional< ProcessingStepRef > processing_step_opt
(Optional) reference to the processing step
Definition: AppliedProcessingStep.h:36
Wrapper that adds operator< to iterators, so they can be used as (part of) keys in maps/sets or multi...
Definition: MetaData.h:20
Base class for ID data with scores and processing steps (and meta info)
Definition: ScoredProcessingResult.h:19
void addProcessingStep(const AppliedProcessingStep &step)
Add an applied processing step.
Definition: ScoredProcessingResult.h:40
AppliedProcessingSteps steps_and_scores
Definition: ScoredProcessingResult.h:20
std::pair< double, bool > getScore(ScoreTypeRef score_ref) const
Look up a score by score type.
Definition: ScoredProcessingResult.h:101
Size getNumberOfScores() const
Return the number of scores associated with this result.
Definition: ScoredProcessingResult.h:179
ScoredProcessingResult(const AppliedProcessingSteps &steps_and_scores=AppliedProcessingSteps())
Constructor.
Definition: ScoredProcessingResult.h:191
void addScore(ScoreTypeRef score_type, double score, const std::optional< ProcessingStepRef > &processing_step_opt=std::nullopt)
Add a score (possibly connected to a processing step)
Definition: ScoredProcessingResult.h:71
const AppliedProcessingSteps::nth_index< 1 >::type & getStepsAndScoresByStep() const
Return the applied processing steps (incl. scores) as a set ordered by processing step reference (opt...
Definition: ScoredProcessingResult.h:30
std::tuple< double, std::optional< ScoreTypeRef >, bool > getMostRecentScore() const
Get the (primary) score from the most recent processing step.
Definition: ScoredProcessingResult.h:162
AppliedProcessingSteps::nth_index< 1 >::type & getStepsAndScoresByStep()
Return the applied processing steps (incl. scores) as a set ordered by processing step reference (opt...
Definition: ScoredProcessingResult.h:23
ScoredProcessingResult & merge(const ScoredProcessingResult &other)
Merge in data from another object.
Definition: ScoredProcessingResult.h:81
std::pair< double, bool > getScore(ScoreTypeRef score_ref, std::optional< ProcessingStepRef > processing_step_opt) const
Look up a score by score type and processing step.
Definition: ScoredProcessingResult.h:113
void addProcessingStep(ProcessingStepRef step_ref, const std::map< ScoreTypeRef, double > &scores=std::map< ScoreTypeRef, double >())
Add a processing step (and associated scores, if any)
Definition: ScoredProcessingResult.h:62
ScoredProcessingResult(const ScoredProcessingResult &)=default
Copy c'tor.
std::tuple< double, std::optional< ProcessingStepRef >, bool > getScoreAndStep(ScoreTypeRef score_ref) const
Look up a score and associated processing step by score type.
Definition: ScoredProcessingResult.h:138