OpenMS
ScoredProcessingResult.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hendrik Weisser $
32 // $Authors: Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 
39 namespace OpenMS
40 {
41  namespace IdentificationDataInternal
42  {
45  {
47 
49  AppliedProcessingSteps::nth_index<1>::type& getStepsAndScoresByStep()
50  {
51  return steps_and_scores.get<1>();
52  }
53 
55  const AppliedProcessingSteps::nth_index<1>::type&
57  {
58  return steps_and_scores.get<1>();
59  }
60 
67  {
68  auto step_pos =
69  steps_and_scores.get<1>().find(step.processing_step_opt);
70  if (step_pos == steps_and_scores.get<1>().end()) // new step
71  {
72  steps_and_scores.push_back(step);
73  }
74  else // existing step - add or update scores
75  {
76  steps_and_scores.get<1>().modify(
77  step_pos, [&](AppliedProcessingStep& old_step)
78  {
79  for (const auto& pair : step.scores)
80  {
81  old_step.scores[pair.first] = pair.second;
82  }
83  });
84  }
85  }
86 
89  const std::map<ScoreTypeRef, double>& scores =
90  std::map<ScoreTypeRef, double>())
91  {
92  AppliedProcessingStep applied(step_ref, scores);
93  addProcessingStep(applied);
94  }
95 
97  void addScore(ScoreTypeRef score_type, double score,
98  const std::optional<ProcessingStepRef>&
99  processing_step_opt = std::nullopt)
100  {
101  AppliedProcessingStep applied(processing_step_opt);
102  applied.scores[score_type] = score;
103  addProcessingStep(applied);
104  }
105 
108  {
109  // merge applied processing steps and scores:
110  for (const auto& step : other.steps_and_scores)
111  {
112  addProcessingStep(step);
113  }
114  // merge meta info - existing entries may be overwritten:
115  addMetaValues(other);
116 
117  return *this;
118  }
119 
127  std::pair<double, bool> getScore(ScoreTypeRef score_ref) const
128  {
129  std::tuple<double, std::optional<ProcessingStepRef>, bool> result =
130  getScoreAndStep(score_ref);
131  return std::make_pair(std::get<0>(result), std::get<2>(result));
132  }
133 
139  std::pair<double, bool> getScore(ScoreTypeRef score_ref,
140  std::optional<ProcessingStepRef>
141  processing_step_opt) const
142  {
143  auto step_pos = steps_and_scores.get<1>().find(processing_step_opt);
144  if (step_pos != steps_and_scores.get<1>().end())
145  {
146  auto score_pos = step_pos->scores.find(score_ref);
147  if (score_pos != step_pos->scores.end())
148  {
149  return std::make_pair(score_pos->second, true);
150  }
151  }
152  // not found:
153  return std::make_pair(std::numeric_limits<double>::quiet_NaN(), false);
154  }
155 
163  std::tuple<double, std::optional<ProcessingStepRef>, bool>
164  getScoreAndStep(ScoreTypeRef score_ref) const
165  {
166  // give priority to scores from later processing steps:
167  for (const auto& step : boost::adaptors::reverse(steps_and_scores))
168  {
169  auto pos = step.scores.find(score_ref);
170  if (pos != step.scores.end())
171  {
172  return std::make_tuple(pos->second, step.processing_step_opt, true);
173  }
174  }
175  // not found:
176  return std::make_tuple(std::numeric_limits<double>::quiet_NaN(),
177  std::nullopt, false);
178  }
179 
187  std::tuple<double, std::optional<ScoreTypeRef>, bool>
189  {
190  // check steps starting with most recent:
191  for (const auto& step : boost::adaptors::reverse(steps_and_scores))
192  {
193  auto top_score = step.getScoresInOrder(true);
194  if (!top_score.empty())
195  {
196  return std::make_tuple(top_score[0].second, top_score[0].first,
197  true);
198  }
199  }
200  return std::make_tuple(std::numeric_limits<double>::quiet_NaN(),
201  std::nullopt, false); // no score available
202  }
203 
206  {
207  Size counter = 0;
208  for (const auto& step : steps_and_scores)
209  {
210  counter += step.scores.size();
211  }
212  return counter;
213  }
214 
215  protected:
221  {
222  }
223 
226  };
227 
228  }
229 }
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:61
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:127
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:133
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:355
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Definition: AppliedProcessingStep.h:56
std::map< ScoreTypeRef, double > scores
Map of scores and their types.
Definition: AppliedProcessingStep.h:65
std::optional< ProcessingStepRef > processing_step_opt
(Optional) reference to the processing step
Definition: AppliedProcessingStep.h:62
Wrapper that adds operator< to iterators, so they can be used as (part of) keys in maps/sets or multi...
Definition: MetaData.h:46
Base class for ID data with scores and processing steps (and meta info)
Definition: ScoredProcessingResult.h:45
void addProcessingStep(const AppliedProcessingStep &step)
Add an applied processing step.
Definition: ScoredProcessingResult.h:66
AppliedProcessingSteps steps_and_scores
Definition: ScoredProcessingResult.h:46
std::pair< double, bool > getScore(ScoreTypeRef score_ref) const
Look up a score by score type.
Definition: ScoredProcessingResult.h:127
Size getNumberOfScores() const
Return the number of scores associated with this result.
Definition: ScoredProcessingResult.h:205
ScoredProcessingResult(const AppliedProcessingSteps &steps_and_scores=AppliedProcessingSteps())
Constructor.
Definition: ScoredProcessingResult.h:217
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:97
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:56
std::tuple< double, std::optional< ScoreTypeRef >, bool > getMostRecentScore() const
Get the (primary) score from the most recent processing step.
Definition: ScoredProcessingResult.h:188
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:49
ScoredProcessingResult & merge(const ScoredProcessingResult &other)
Merge in data from another object.
Definition: ScoredProcessingResult.h:107
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:139
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:88
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:164