OpenMS
Loading...
Searching...
No Matches
MapAlignmentAlgorithmIdentification.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Hendrik Weisser $
6// $Authors: Eva Lange, Clemens Groepl, Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
22
23#include <cmath> // for "abs"
24#include <limits> // for "max"
25#include <map>
26
27namespace OpenMS
28{
29 /* Concept for FeatureMap or ConsensusMap*/
30 template <typename MapType>
31 concept IsFCMap = std::same_as<MapType, OpenMS::FeatureMap> || std::same_as<MapType, OpenMS::ConsensusMap>;
32
33 class AnnotatedMSRun;
34
62 public ProgressLogger
63 {
64public:
67
70
71 // Set a reference for the alignment
72 template <typename DataType> void setReference(const DataType& data)
73 {
74 reference_.clear();
75 if (data.empty()) return; // empty input resets the reference
76 SeqToList rt_data;
77 // set these here because "checkParameters_" may not have been called yet:
78 use_feature_rt_ = param_.getValue("use_feature_rt").toBool();
79 score_cutoff_ = param_.getValue("score_cutoff").toBool();
80 score_type_ = StringUtils::toStr(param_.getValue("score_type"));
81 min_score_ = param_.getValue("min_score");
82 use_adducts_ = param_.getValue("use_adducts").toBool();
83 bool sorted = getRetentionTimes_(data, rt_data);
84 computeMedians_(rt_data, reference_, sorted);
85
86 if (reference_.empty())
87 {
88 throw Exception::MissingInformation(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Could not extract retention time information from the reference file");
89 }
90 }
91
101 template <typename DataType>
102 void align(const std::vector<DataType>& data,
103 std::vector<TransformationDescription>& transformations,
104 Int reference_index = -1)
105 {
106 // is reference one of the input files?
107 bool use_internal_reference = (reference_index >= 0);
108 // Drop any reference_ left over from a previous align() call before
109 // checkParameters_ counts it as an extra run; setReference() further
110 // down repopulates reference_ for this invocation. External references
111 // set explicitly via setReference() are preserved (reference_index < 0).
112 if (use_internal_reference) reference_.clear();
113
114 checkParameters_(data.size());
115 startProgress(0, 3, "aligning maps");
116
117 reference_index_ = reference_index;
118 if (use_internal_reference)
119 {
120 if (reference_index >= Int(data.size()))
121 {
122 throw Exception::IndexOverflow(__FILE__, __LINE__,
123 OPENMS_PRETTY_FUNCTION,
124 reference_index, data.size());
125 }
126 setReference(data[reference_index]);
127 }
128
129 // one set of RT data for each input map, except reference (if any):
130 std::vector<SeqToList> rt_data(data.size() - use_internal_reference);
131 bool all_sorted = true;
132 for (Size i = 0, j = 0; i < data.size(); ++i)
133 {
134 if ((reference_index >= 0) && (i == Size(reference_index)))
135 {
136 continue; // skip reference map, if any
137 }
138 all_sorted &= getRetentionTimes_(data[i], rt_data[j++]);
139 }
140 setProgress(1);
141
142 if (!use_internal_reference && reference_.empty())
143 {
144 alignToAutoReference_(rt_data, transformations, all_sorted);
145 }
146 else
147 {
148 computeTransformations_(rt_data, transformations, all_sorted);
149 }
150 // a reference taken from the input maps must not carry over into the next call:
151 if (reference_index_ >= 0) reference_.clear();
152 setProgress(2);
153
154 setProgress(3);
155 endProgress();
156 }
157
158protected:
159
161 typedef std::map<std::string, DoubleList> SeqToList;
162
164 typedef std::map<std::string, double> SeqToValue;
165
168
171
174
176 bool use_feature_rt_{};
177
179 bool use_adducts_{};
180
182 bool consensus_reference_{};
183
185 Size auto_reference_min_points_{};
186
189
191 bool score_cutoff_{};
192
194 std::string score_type_;
195
201 bool passesScoreCutoff_(const PeptideIdentification& peptide, const PeptideHit& hit) const
202 {
203 if (!score_cutoff_) return true;
204 return peptide.isHigherScoreBetter() ? hit.getScore() >= min_score_ : hit.getScore() <= min_score_;
205 }
206
216 void computeMedians_(SeqToList& rt_data, SeqToValue& medians,
217 bool sorted = false);
218
228 SeqToList& rt_data);
229
238 // "id_data" can't be "const" here or template resolution will fail
239 bool getRetentionTimes_(const IdentificationData& id_data, SeqToList& rt_data);
240
256 bool getRetentionTimes_(const IsFCMap auto& features, SeqToList& rt_data)
257 {
258 for (auto feat_it = features.cbegin(); feat_it != features.cend(); ++feat_it)
259 {
260 if (use_feature_rt_)
261 {
262 // find the peptide ID closest in RT to the feature centroid (among those that pass the score cut-off):
263 std::string sequence;
264 double rt_distance = std::numeric_limits<double>::max();
265 bool any_passing = false;
267 feat_it->getPeptideIdentifications().begin(); pep_it !=
268 feat_it->getPeptideIdentifications().end(); ++pep_it)
269 {
270 if (!pep_it->getHits().empty())
271 {
272 double current_distance = fabs(pep_it->getRT() -
273 feat_it->getRT());
274 if (current_distance < rt_distance)
275 {
276 const PeptideHit* best_hit = getBestScoringHit(pep_it->getHits(), pep_it->isHigherScoreBetter());
277 if (best_hit && passesScoreCutoff_(*pep_it, *best_hit))
278 {
279 sequence = best_hit->getSequence().toString();
280 rt_distance = current_distance;
281 any_passing = true;
282 }
283 }
284 }
285 }
286
287 // a feature without a passing identification has no sequence (and is no alignment point)
288 if (any_passing) rt_data[sequence].push_back(feat_it->getRT());
289 }
290 else
291 {
292 getRetentionTimes_(feat_it->getPeptideIdentifications(), rt_data);
293 }
294 }
295
296 if (!use_feature_rt_ &&
297 param_.getValue("use_unassigned_peptides").toBool())
298 {
299 getRetentionTimes_(features.getUnassignedPeptideIdentifications(),
300 rt_data);
301 }
302
303 // remove duplicates (can occur if a peptide ID was assigned to several
304 // features due to overlap or annotation tolerance):
305 for (SeqToList::iterator rt_it = rt_data.begin(); rt_it != rt_data.end();
306 ++rt_it)
307 {
308 DoubleList& rt_values = rt_it->second;
309 sort(rt_values.begin(), rt_values.end());
310 DoubleList::iterator it = unique(rt_values.begin(), rt_values.end());
311 rt_values.resize(it - rt_values.begin());
312 }
313 return true; // RTs were already sorted for duplicate detection
314 }
315
324 void computeTransformations_(std::vector<SeqToList>& rt_data,
325 std::vector<TransformationDescription>&
326 transforms, bool sorted = false, bool verbose = true);
327
337 Int selectReference_(const std::vector<SeqToList>& rt_data) const;
338
351 void alignToAutoReference_(std::vector<SeqToList>& rt_data,
352 std::vector<TransformationDescription>& transforms,
353 bool sorted);
354
366 void alignToInput_(std::vector<SeqToList>& rt_data, Size index,
367 std::vector<TransformationDescription>& transforms,
368 bool sorted, bool verbose);
369
377 void checkParameters_(const Size runs);
378
385
392
401 const PeptideHit* getBestScoringHit(const std::vector<PeptideHit>& hits, const bool is_higher_score_better);
402
403private:
404
407
410
411 };
412
413} // namespace OpenMS
std::string toString() const
returns the peptide as string with modifications embedded in brackets
A base class for all classes handling default parameters.
Definition DefaultParamHandler.h:66
Int overflow exception.
Definition Exception.h:211
Not all required information provided.
Definition Exception.h:155
typename VecMember::const_iterator const_iterator
Definition ExposedVector.h:69
Definition IdentificationData.h:87
A map alignment algorithm based on peptide identifications from MS2 spectra.
Definition MapAlignmentAlgorithmIdentification.h:63
bool getRetentionTimes_(const PeptideIdentificationList &peptides, SeqToList &rt_data)
Collect retention time data from peptide IDs.
const PeptideHit * getBestScoringHit(const std::vector< PeptideHit > &hits, const bool is_higher_score_better)
Get the best-scoring PeptideHit from a list of hits.
void setReference(const DataType &data)
Definition MapAlignmentAlgorithmIdentification.h:72
bool getRetentionTimes_(const IdentificationData &id_data, SeqToList &rt_data)
Collect retention time data from spectrum matches.
~MapAlignmentAlgorithmIdentification() override
Destructor.
void checkParameters_(const Size runs)
Check that parameter values are valid.
void getReference_()
Get reference retention times.
bool getRetentionTimes_(const IsFCMap auto &features, SeqToList &rt_data)
Collect retention time data from peptide IDs contained in feature maps or consensus maps.
Definition MapAlignmentAlgorithmIdentification.h:256
Int reference_index_
Index of input file to use as reference (if any)
Definition MapAlignmentAlgorithmIdentification.h:167
MapAlignmentAlgorithmIdentification & operator=(const MapAlignmentAlgorithmIdentification &)
Assignment operator intentionally not implemented -> private.
std::map< std::string, double > SeqToValue
Type to store one representative retention time per peptide sequence.
Definition MapAlignmentAlgorithmIdentification.h:164
SeqToValue reference_
Reference retention times (per peptide sequence)
Definition MapAlignmentAlgorithmIdentification.h:170
double min_score_
Minimum score to reach for a peptide to be considered.
Definition MapAlignmentAlgorithmIdentification.h:188
std::map< std::string, DoubleList > SeqToList
Type to store retention times given for individual peptide sequences.
Definition MapAlignmentAlgorithmIdentification.h:161
Size min_run_occur_
Minimum number of runs a peptide must occur in.
Definition MapAlignmentAlgorithmIdentification.h:173
void computeTransformations_(std::vector< SeqToList > &rt_data, std::vector< TransformationDescription > &transforms, bool sorted=false, bool verbose=true)
Compute retention time transformations from RT data grouped by peptide sequence.
std::string score_type_
Score type to use for filtering.
Definition MapAlignmentAlgorithmIdentification.h:194
MapAlignmentAlgorithmIdentification()
Default constructor.
Int selectReference_(const std::vector< SeqToList > &rt_data) const
Choose the input map that shares the most identified sequences with every other map as the reference.
IdentificationData::ScoreTypeRef handleIdDataScoreType_(const IdentificationData &id_data)
Helper function to find/define the score type for processing IdentificationData.
void align(const std::vector< DataType > &data, std::vector< TransformationDescription > &transformations, Int reference_index=-1)
Align feature maps, consensus maps, or peptide identifications.
Definition MapAlignmentAlgorithmIdentification.h:102
void computeMedians_(SeqToList &rt_data, SeqToValue &medians, bool sorted=false)
Compute the median retention time for each peptide sequence.
void alignToInput_(std::vector< SeqToList > &rt_data, Size index, std::vector< TransformationDescription > &transforms, bool sorted, bool verbose)
Compute RT transformations with one of the input maps as the reference.
bool passesScoreCutoff_(const PeptideIdentification &peptide, const PeptideHit &hit) const
Does hit of peptide pass the score cut-off ('score_cutoff', 'min_score')?
Definition MapAlignmentAlgorithmIdentification.h:201
MapAlignmentAlgorithmIdentification(const MapAlignmentAlgorithmIdentification &)
Copy constructor intentionally not implemented -> private.
void alignToAutoReference_(std::vector< SeqToList > &rt_data, std::vector< TransformationDescription > &transforms, bool sorted)
Compute RT transformations without a given reference, as parameter auto_reference asks.
Represents a single spectrum match (candidate) for a specific tandem mass spectrum (MS/MS).
Definition PeptideHit.h:52
double getScore() const
returns the PSM score
const AASequence & getSequence() const
returns the peptide sequence
Container for peptide identifications from multiple spectra.
Definition PeptideIdentificationList.h:66
Represents the set of candidates (SpectrumMatches) identified for a single precursor spectrum.
Definition PeptideIdentification.h:66
bool isHigherScoreBetter() const
returns the peptide score orientation
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:27
Definition MapAlignmentAlgorithmIdentification.h:31
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
std::vector< double > DoubleList
Vector of double precision real types.
Definition TypeAliases.h:31
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Wrapper that adds operator< to iterators, so they can be used as (part of) keys in maps/sets or multi...
Definition MetaData.h:20