OpenMS  2.7.0
ConfidenceScoring.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-2021.
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: Hannes Roest, Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <cmath> // for "exp"
38 #include <ctime> // for "time" (random number seed)
39 #include <limits> // for "infinity"
40 #include <random>
41 #include <boost/bimap.hpp>
42 #include <boost/bimap/multiset_of.hpp>
43 
49 
53 
54 namespace OpenMS
55 {
56 
57  class OPENMS_DLLAPI ConfidenceScoring :
58  public ProgressLogger
59  {
60  public:
61 
63  explicit ConfidenceScoring(bool test_mode_ = false)
64  {
65  if (!test_mode_) shuffler_ = Math::RandomShuffler(0);
66  else shuffler_ = Math::RandomShuffler(time(nullptr));// seed with current time
67  }
68 
69  virtual ~ConfidenceScoring() {}
70 
71  protected:
72 
74  typedef boost::bimap<double, boost::bimaps::multiset_of<double> >
76 
78  struct GLM_
79  {
80  double intercept;
81  double rt_coef;
82  double int_coef;
83 
84  double operator()(double diff_rt, double dist_int)
85  {
86  double lm = intercept + rt_coef * diff_rt * diff_rt +
87  int_coef * dist_int;
88  return 1.0 / (1.0 + exp(-lm));
89  }
90  } glm_;
91 
93  struct RTNorm_
94  {
95  double min_rt;
96  double max_rt;
97 
98  double operator()(double rt)
99  {
100  return (rt - min_rt) / (max_rt - min_rt) * 100;
101  }
102  } rt_norm_;
103 
105 
107 
109 
111 
113 
116 
118 
121 
124 
127 
130  void extractIntensities_(BimapType& intensity_map, Size n_transitions,
131  DoubleList& intensities);
132 
137  double feature_rt, DoubleList& feature_intensities,
138  const std::set<String>& transition_ids = std::set<String>());
139 
141  void scoreFeature_(Feature& feature);
142 
143  public:
144 
145  void initialize(const TargetedExperiment& library, const Size n_decoys, const Size n_transitions, const TransformationDescription& rt_trafo)
146  {
147  library_ = library;
148  n_decoys_ = n_decoys;
149  n_transitions_ = n_transitions;
150  rt_trafo_ = rt_trafo;
151  }
152 
153  void initializeGlm(double intercept, double rt_coef, double int_coef)
154  {
155  glm_.intercept = intercept;
156  glm_.rt_coef = rt_coef;
157  glm_.int_coef = int_coef;
158  }
159 
172  void scoreMap(FeatureMap & features)
173  {
174  // are there enough assays in the library?
175  Size n_assays = library_.getPeptides().size();
176  if (n_assays < 2)
177  {
178  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
179  "There need to be at least 2 assays in the library for ConfidenceScoring.");
180 
181  }
182  if (n_assays - 1 < n_decoys_)
183  {
184  OPENMS_LOG_WARN << "Warning: Parameter 'decoys' (" << n_decoys_
185  << ") is higher than the number of unrelated assays in the "
186  << "library (" << n_assays - 1 << "). "
187  << "Using all unrelated assays as decoys." << std::endl;
188  }
189  if (n_assays - 1 <= n_decoys_) n_decoys_ = 0; // use all available assays
190 
191  decoy_index_.resize(n_assays);
192  for (Size i = 0; i < n_assays; ++i) decoy_index_[i] = boost::numeric_cast<Int>(i);
193 
194  // build mapping between assays and transitions:
195  OPENMS_LOG_DEBUG << "Building transition map..." << std::endl;
196  for (Size i = 0; i < library_.getTransitions().size(); ++i)
197  {
198  const String& ref = library_.getTransitions()[i].getPeptideRef();
199  transition_map_[ref].push_back(boost::numeric_cast<Int>(i));
200  }
201  // find min./max. RT in the library:
202  OPENMS_LOG_DEBUG << "Determining retention time range..." << std::endl;
203  rt_norm_.min_rt = std::numeric_limits<double>::infinity();
204  rt_norm_.max_rt = -std::numeric_limits<double>::infinity();
205  for (std::vector<TargetedExperiment::Peptide>::const_iterator it =
206  library_.getPeptides().begin(); it != library_.getPeptides().end();
207  ++it)
208  {
209  double current_rt = getAssayRT_(*it);
210  if (current_rt == -1.0) continue; // indicates a missing value
211  rt_norm_.min_rt = std::min(rt_norm_.min_rt, current_rt);
212  rt_norm_.max_rt = std::max(rt_norm_.max_rt, current_rt);
213  }
214 
215  // log scoring progress:
216  OPENMS_LOG_DEBUG << "Scoring features..." << std::endl;
217  startProgress(0, features.size(), "scoring features");
218 
219  for (FeatureMap::Iterator feat_it = features.begin();
220  feat_it != features.end(); ++feat_it)
221  {
222  OPENMS_LOG_DEBUG << "Feature " << feat_it - features.begin() + 1
223  << " (ID '" << feat_it->getUniqueId() << "')"<< std::endl;
224  scoreFeature_(*feat_it);
225  setProgress(feat_it - features.begin());
226  }
227  endProgress();
228 
229  }
230 
231  };
232 
233 }
234 
#define OPENMS_LOG_DEBUG
Macro for general debugging information.
Definition: LogStream.h:470
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:460
Definition: ConfidenceScoring.h:59
double scoreAssay_(const TargetedExperiment::Peptide &assay, double feature_rt, DoubleList &feature_intensities, const std::set< String > &transition_ids=std::set< String >())
void scoreMap(FeatureMap &features)
Score a feature map -> make sure the class is properly initialized.
Definition: ConfidenceScoring.h:172
void chooseDecoys_()
Randomize the list of decoy indexes.
TargetedExperiment library_
assay library
Definition: ConfidenceScoring.h:104
Math::RandomShuffler shuffler_
random shuffler for container
Definition: ConfidenceScoring.h:117
IntList decoy_index_
indexes of assays to use as decoys
Definition: ConfidenceScoring.h:106
Size n_decoys_
number of decoys to use (per feature/true assay)
Definition: ConfidenceScoring.h:108
double getAssayRT_(const TargetedExperiment::Peptide &assay)
Get the retention time of an assay.
virtual ~ConfidenceScoring()
Definition: ConfidenceScoring.h:69
TransformationDescription rt_trafo_
RT transformation to map measured RTs to assay RTs.
Definition: ConfidenceScoring.h:115
void extractIntensities_(BimapType &intensity_map, Size n_transitions, DoubleList &intensities)
boost::bimap< double, boost::bimaps::multiset_of< double > > BimapType
Mapping: Q3 m/z <-> transition intensity (maybe not unique!)
Definition: ConfidenceScoring.h:75
double manhattanDist_(DoubleList x, DoubleList y)
Manhattan distance.
Map< String, IntList > transition_map_
assay (ID) -> transitions (indexes)
Definition: ConfidenceScoring.h:110
ConfidenceScoring(bool test_mode_=false)
Constructor.
Definition: ConfidenceScoring.h:63
void scoreFeature_(Feature &feature)
Score a feature.
void initializeGlm(double intercept, double rt_coef, double int_coef)
Definition: ConfidenceScoring.h:153
Size n_transitions_
number of transitions to consider
Definition: ConfidenceScoring.h:112
void initialize(const TargetedExperiment &library, const Size n_decoys, const Size n_transitions, const TransformationDescription &rt_trafo)
Definition: ConfidenceScoring.h:145
A method or algorithm argument contains illegal values.
Definition: Exception.h:656
A container for features.
Definition: FeatureMap.h:105
Base::iterator Iterator
Definition: FeatureMap.h:143
An LC-MS feature.
Definition: Feature.h:72
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:52
Definition: MathFunctions.h:352
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
A more convenient string class.
Definition: String.h:61
Represents a peptide (amino acid sequence)
Definition: TargetedExperimentHelper.h:360
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:65
const std::vector< Peptide > & getPeptides() const
const std::vector< ReactionMonitoringTransition > & getTransitions() const
returns the transition list
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:63
UInt64 getUniqueId() const
Non-mutable access to unique id - returns the unique id.
Definition: UniqueIdInterface.h:105
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:55
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:62
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Binomial GLM.
Definition: ConfidenceScoring.h:79
double rt_coef
Definition: ConfidenceScoring.h:81
double int_coef
Definition: ConfidenceScoring.h:82
double intercept
Definition: ConfidenceScoring.h:80
double operator()(double diff_rt, double dist_int)
Definition: ConfidenceScoring.h:84
Helper for RT normalization (range 0-100)
Definition: ConfidenceScoring.h:94
double operator()(double rt)
Definition: ConfidenceScoring.h:98
double min_rt
Definition: ConfidenceScoring.h:95
double max_rt
Definition: ConfidenceScoring.h:96