OpenMS  2.8.0
MapAlignmentAlgorithmIdentification.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: Eva Lange, Clemens Groepl, Hendrik Weisser $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
47 
48 #include <cmath> // for "abs"
49 #include <limits> // for "max"
50 #include <map>
51 
52 namespace OpenMS
53 {
73  public DefaultParamHandler,
74  public ProgressLogger
75  {
76 public:
79 
82 
83  // Set a reference for the alignment
84  template <typename DataType> void setReference(DataType& data)
85  {
86  reference_.clear();
87  if (data.empty()) return; // empty input resets the reference
88  SeqToList rt_data;
89  // set these here because "checkParameters_" may not have been called yet:
90  use_feature_rt_ = param_.getValue("use_feature_rt").toBool();
91  score_cutoff_ = param_.getValue("score_cutoff").toBool();
92  score_type_ = (std::string)param_.getValue("score_type");
93  bool sorted = getRetentionTimes_(data, rt_data);
94  computeMedians_(rt_data, reference_, sorted);
95 
96  if (reference_.empty())
97  {
98  throw Exception::MissingInformation(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Could not extract retention time information from the reference file");
99  }
100  }
101 
111  template <typename DataType>
112  void align(std::vector<DataType>& data,
113  std::vector<TransformationDescription>& transformations,
114  Int reference_index = -1)
115  {
116  checkParameters_(data.size());
117  startProgress(0, 3, "aligning maps");
118 
119  reference_index_ = reference_index;
120  // is reference one of the input files?
121  bool use_internal_reference = (reference_index >= 0);
122  if (use_internal_reference)
123  {
124  if (reference_index >= Int(data.size()))
125  {
126  throw Exception::IndexOverflow(__FILE__, __LINE__,
127  OPENMS_PRETTY_FUNCTION,
128  reference_index, data.size());
129  }
130  setReference(data[reference_index]);
131  }
132 
133  // one set of RT data for each input map, except reference (if any):
134  std::vector<SeqToList> rt_data(data.size() - use_internal_reference);
135  bool all_sorted = true;
136  for (Size i = 0, j = 0; i < data.size(); ++i)
137  {
138  if ((reference_index >= 0) && (i == Size(reference_index)))
139  {
140  continue; // skip reference map, if any
141  }
142  all_sorted &= getRetentionTimes_(data[i], rt_data[j++]);
143  }
144  setProgress(1);
145 
146  computeTransformations_(rt_data, transformations, all_sorted);
147  setProgress(2);
148 
149  setProgress(3);
150  endProgress();
151  }
152 
153 protected:
154 
156  typedef std::map<String, DoubleList> SeqToList;
157 
159  typedef std::map<String, double> SeqToValue;
160 
163 
166 
169 
172 
175 
177  double min_score_;
178 
181 
184 
186  bool (*better_) (double, double) = [](double, double) {return true;};
187 
197  void computeMedians_(SeqToList& rt_data, SeqToValue& medians,
198  bool sorted = false);
199 
208  bool getRetentionTimes_(std::vector<PeptideIdentification>& peptides,
209  SeqToList& rt_data);
210 
219  // "id_data" can't be "const" here or template resolution will fail
221 
230  bool getRetentionTimes_(PeakMap& experiment, SeqToList& rt_data);
231 
246  template <typename MapType>
247  bool getRetentionTimes_(MapType& features, SeqToList& rt_data)
248  {
249  if (!score_cutoff_)
250  {
251  better_ = [](double, double)
252  {return true;};
253  }
254  else if (features[0].getPeptideIdentifications()[0].isHigherScoreBetter())
255  {
256  better_ = [](double a, double b)
257  { return a >= b; };
258  }
259  else
260  {
261  better_ = [](double a, double b)
262  { return a <= b; };
263  }
264 
265  for (typename MapType::Iterator feat_it = features.begin();
266  feat_it != features.end(); ++feat_it)
267  {
268  if (use_feature_rt_)
269  {
270  // find the peptide ID closest in RT to the feature centroid:
271  String sequence;
272  double rt_distance = std::numeric_limits<double>::max();
273  bool any_hit = false;
274  for (std::vector<PeptideIdentification>::iterator pep_it =
275  feat_it->getPeptideIdentifications().begin(); pep_it !=
276  feat_it->getPeptideIdentifications().end(); ++pep_it)
277  {
278  if (!pep_it->getHits().empty())
279  {
280  any_hit = true;
281  double current_distance = fabs(pep_it->getRT() -
282  feat_it->getRT());
283  if (current_distance < rt_distance)
284  {
285  pep_it->sort();
286  if (better_(pep_it->getHits()[0].getScore(), min_score_))
287  {
288  sequence = pep_it->getHits()[0].getSequence().toString();
289  rt_distance = current_distance;
290  }
291  }
292  }
293  }
294 
295  if (any_hit) rt_data[sequence].push_back(feat_it->getRT());
296  }
297  else
298  {
299  getRetentionTimes_(feat_it->getPeptideIdentifications(), rt_data);
300  }
301  }
302 
303  if (!use_feature_rt_ &&
304  param_.getValue("use_unassigned_peptides").toBool())
305  {
306  getRetentionTimes_(features.getUnassignedPeptideIdentifications(),
307  rt_data);
308  }
309 
310  // remove duplicates (can occur if a peptide ID was assigned to several
311  // features due to overlap or annotation tolerance):
312  for (SeqToList::iterator rt_it = rt_data.begin(); rt_it != rt_data.end();
313  ++rt_it)
314  {
315  DoubleList& rt_values = rt_it->second;
316  sort(rt_values.begin(), rt_values.end());
317  DoubleList::iterator it = unique(rt_values.begin(), rt_values.end());
318  rt_values.resize(it - rt_values.begin());
319  }
320  return true; // RTs were already sorted for duplicate detection
321  }
322 
330  void computeTransformations_(std::vector<SeqToList>& rt_data,
331  std::vector<TransformationDescription>&
332  transforms, bool sorted = false);
333 
341  void checkParameters_(const Size runs);
342 
349 
356 
357 private:
358 
361 
364 
365  };
366 
367 } // namespace OpenMS
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
Int overflow exception.
Definition: Exception.h:247
Not all required information provided.
Definition: Exception.h:188
Representation of spectrum identification results and associated data.
Definition: IdentificationData.h:95
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:73
Iterator begin()
Definition: MSExperiment.h:150
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:104
Iterator end()
Definition: MSExperiment.h:160
A map alignment algorithm based on peptide identifications from MS2 spectra.
Definition: MapAlignmentAlgorithmIdentification.h:75
void computeTransformations_(std::vector< SeqToList > &rt_data, std::vector< TransformationDescription > &transforms, bool sorted=false)
Compute retention time transformations from RT data grouped by peptide sequence.
bool getRetentionTimes_(std::vector< PeptideIdentification > &peptides, SeqToList &rt_data)
Collect retention time data from peptide IDs.
bool getRetentionTimes_(PeakMap &experiment, SeqToList &rt_data)
Collect retention time data from peptide IDs annotated to spectra.
bool use_adducts_
Consider differently adducted IDs as different?
Definition: MapAlignmentAlgorithmIdentification.h:174
~MapAlignmentAlgorithmIdentification() override
Destructor.
std::map< String, double > SeqToValue
Type to store one representative retention time per peptide sequence.
Definition: MapAlignmentAlgorithmIdentification.h:159
void checkParameters_(const Size runs)
Check that parameter values are valid.
void getReference_()
Get reference retention times.
String score_type_
Score type to use for filtering.
Definition: MapAlignmentAlgorithmIdentification.h:183
Int reference_index_
Index of input file to use as reference (if any)
Definition: MapAlignmentAlgorithmIdentification.h:162
void align(std::vector< DataType > &data, std::vector< TransformationDescription > &transformations, Int reference_index=-1)
Align feature maps, consensus maps, peak maps, or peptide identifications.
Definition: MapAlignmentAlgorithmIdentification.h:112
bool score_cutoff_
Actually use the above defined score_cutoff? Needed since it is hard to define a non-cutting score fo...
Definition: MapAlignmentAlgorithmIdentification.h:180
bool use_feature_rt_
Use feature RT instead of RT from best peptide ID in the feature?
Definition: MapAlignmentAlgorithmIdentification.h:171
SeqToValue reference_
Reference retention times (per peptide sequence)
Definition: MapAlignmentAlgorithmIdentification.h:165
double min_score_
Minimum score to reach for a peptide to be considered.
Definition: MapAlignmentAlgorithmIdentification.h:177
Size min_run_occur_
Minimum number of runs a peptide must occur in.
Definition: MapAlignmentAlgorithmIdentification.h:168
std::map< String, DoubleList > SeqToList
Type to store retention times given for individual peptide sequences.
Definition: MapAlignmentAlgorithmIdentification.h:156
bool getRetentionTimes_(IdentificationData &id_data, SeqToList &rt_data)
Collect retention time data from spectrum matches.
MapAlignmentAlgorithmIdentification & operator=(const MapAlignmentAlgorithmIdentification &)
Assignment operator intentionally not implemented -> private.
MapAlignmentAlgorithmIdentification()
Default constructor.
IdentificationData::ScoreTypeRef handleIdDataScoreType_(const IdentificationData &id_data)
Helper function to find/define the score type for processing IdentificationData.
void setReference(DataType &data)
Definition: MapAlignmentAlgorithmIdentification.h:84
void computeMedians_(SeqToList &rt_data, SeqToValue &medians, bool sorted=false)
Compute the median retention time for each peptide sequence.
MapAlignmentAlgorithmIdentification(const MapAlignmentAlgorithmIdentification &)
Copy constructor intentionally not implemented -> private.
bool getRetentionTimes_(MapType &features, SeqToList &rt_data)
Collect retention time data from peptide IDs contained in feature maps or consensus maps.
Definition: MapAlignmentAlgorithmIdentification.h:247
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
A more convenient string class.
Definition: String.h:60
int Int
Signed integer type.
Definition: Types.h:102
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:62
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Wrapper that adds operator< to iterators, so they can be used as (part of) keys in maps/sets or multi...
Definition: MetaData.h:46