OpenMS
Loading...
Searching...
No Matches
IDConflictResolverAlgorithm.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: Hendrik Weisser, Lucia Espona, Moritz Freidank $
7// --------------------------------------------------------------------------
8
9#pragma once
10
15
16//-------------------------------------------------------------
17// Doxygen docu
18//-------------------------------------------------------------
19
20
21
22namespace OpenMS
23{
24
32class OPENMS_DLLAPI IDConflictResolverAlgorithm
33{
34public:
47 static void resolve(FeatureMap& features, bool keep_matching = false);
48
61 static void resolve(ConsensusMap& features, bool keep_matching = false);
62
83 static void annotateTransfers(ConsensusMap& features);
84
102
120
126 static void resolveBetweenFeatures(FeatureMap& features);
127
133 static void resolveBetweenFeatures(ConsensusMap& features);
134
136 struct OPENMS_DLLAPI UnresolvedIdentifications
137 {
140 Size removed = 0;
144 Size multiply_identified_spectra = 0;
147 Size without_spectrum_reference = 0;
150 Size inconsistent_score_direction = 0;
152 std::string example;
153 };
154
175
176protected:
177
178 template<class T>
179 static void resolveConflict_(T& map, bool keep_matching)
180 {
181 // annotate as not part of the resolution
182 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
183 {
184 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
185 }
186
187 for (auto& c : map)
188 {
189 c.setMetaValue("feature_id",StringUtils::toStr(c.getUniqueId()));
190 if (!keep_matching)
191 {
192 resolveConflict_(c.getPeptideIdentifications(),
193 map.getUnassignedPeptideIdentifications(),
194 c.getUniqueId());
195 }
196 else
197 {
198 resolveConflictKeepMatching_(c.getPeptideIdentifications(),
199 map.getUnassignedPeptideIdentifications(),
200 c.getUniqueId());
201 }
202 }
203 }
204
205 // compare peptide IDs by score of best hit (hits must be sorted first!)
206 // (note to self: the "static" is necessary to avoid cryptic "no matching
207 // function" errors from gcc when the comparator is used below)
209 const PeptideIdentification & right);
210
211 static void resolveConflict_(
212 PeptideIdentificationList & peptides,
214 UInt64 uid);
215
217 PeptideIdentificationList & peptides,
219 UInt64 uid);
220
222 PeptideIdentificationList & peptides,
224 UInt64 uid);
225
226 template<class T>
227 static void rankAggregation_(T& map)
228 {
229 // annotate unassigned IDs as not part of the resolution
230 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
231 {
232 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
233 }
234
235 for (auto& c : map)
236 {
237 c.setMetaValue("feature_id",StringUtils::toStr(c.getUniqueId()));
238 resolveAggregateConflict_(c.getPeptideIdentifications(),
239 map.getUnassignedPeptideIdentifications(),
240 c.getUniqueId());
241 }
242 }
243
244 template<class T>
245 static void resolveBetweenFeatures_(T & map)
246 {
247 // unassigned peptide identifications in this map
248 PeptideIdentificationList& unassigned = map.getUnassignedPeptideIdentifications();
249
250 // A std::map tracking the set of unique features.
251 // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
252 typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
253 FeatureSet feature_set;
254
255 // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
256 // the feature with the highest intensity for this sequence.
257 for (typename T::value_type& element : map)
258 {
259 PeptideIdentificationList& pep_ids = element.getPeptideIdentifications();
260
261 if (!pep_ids.empty())
262 {
263 if (pep_ids.size() != 1)
264 {
265 // Should never happen. In IDConflictResolverAlgorithm TOPP tool
266 // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
267 throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
268 }
269
270 // Make sure best hit is in front, i.e. sort hits first.
271 pep_ids.front().sort();
272 const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
273
274 if (!hits.empty())
275 {
276 const PeptideHit& highest_score_hit = hits.front();
277
278 // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
279 std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
280
281 // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
282 // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
283 // store a pointer to `feature_in_set` in `feature_set`.
284 typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
285 if (feature_in_set != feature_set.end())
286 {
287 // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
288
289 if (feature_in_set->second->getIntensity() < element.getIntensity())
290 {
291 // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
292 PeptideIdentificationList& obsolete = feature_in_set->second->getPeptideIdentifications();
293 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
294 PeptideIdentificationList pep_ids_empty;
295 feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
296
297 // Replace feature in the set.
298 feature_in_set->second = &(element);
299 }
300 else
301 {
302 // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
303 PeptideIdentificationList& obsolete = element.getPeptideIdentifications();
304 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
305 PeptideIdentificationList pep_ids_empty;
306 element.setPeptideIdentifications(pep_ids_empty);
307 }
308 }
309 else
310 {
311 // Feature is not yet in our set -- add it.
312 feature_set[pair] = &(element);
313 }
314 }
315 }
316 }
317 }
318
319};
320
321}// namespace OpenMS
322
A container for consensus elements.
Definition ConsensusMap.h:67
A method or algorithm argument contains illegal values.
Definition Exception.h:633
iterator insert(const_iterator where, T from, T to)
Definition ExposedVector.h:194
size_t size() const noexcept
Definition ExposedVector.h:128
bool empty() const noexcept
Definition ExposedVector.h:140
iterator begin() noexcept
Definition ExposedVector.h:104
VectorElement & front() noexcept
Get first element.
Definition ExposedVector.h:267
iterator end() noexcept
Definition ExposedVector.h:108
A container for features.
Definition FeatureMap.h:78
Resolves ambiguous annotations of features with peptide identifications.
Definition IDConflictResolverAlgorithm.h:33
static UnresolvedIdentifications reduceToOnePerSpectrum(PeptideIdentificationList &ids)
Reduce identifications a quantification workflow cannot tell apart to one per spectrum.
static void resolveAllHitRankAggregation(ConsensusMap &features)
Resolves ambiguous annotations of consensus features with peptide identifications using rank aggregat...
static void resolveConflict_(PeptideIdentificationList &peptides, PeptideIdentificationList &removed, UInt64 uid)
std::string example
First reduced group, as "<spectrum reference> / <sequence> / charge <n>", for logging.
Definition IDConflictResolverAlgorithm.h:152
static void resolve(FeatureMap &features, bool keep_matching=false)
Resolves ambiguous annotations of features with peptide identifications. The the filtered identificat...
static void resolveBetweenFeatures(FeatureMap &features)
In a single (feature/consensus) map, features with the same (possibly modified) sequence and charge s...
static void resolveAllHitRankAggregation(FeatureMap &features)
Resolves ambiguous annotations of features with peptide identifications using rank aggregation.
static void resolveConflictKeepMatching_(PeptideIdentificationList &peptides, PeptideIdentificationList &removed, UInt64 uid)
static void resolveBetweenFeatures(ConsensusMap &features)
In a single (feature/consensus) map, features with the same (possibly modified) sequence and charge s...
static bool compareIDsSmallerScores_(const PeptideIdentification &left, const PeptideIdentification &right)
static void resolveAggregateConflict_(PeptideIdentificationList &peptides, PeptideIdentificationList &removed, UInt64 uid)
static void annotateTransfers(ConsensusMap &features)
Annotates the runs a consensus feature's identification was transferred to (match-between-runs).
static void resolveConflict_(T &map, bool keep_matching)
Definition IDConflictResolverAlgorithm.h:179
static void rankAggregation_(T &map)
Definition IDConflictResolverAlgorithm.h:227
static void resolve(ConsensusMap &features, bool keep_matching=false)
Resolves ambiguous annotations of consensus features with peptide identifications....
static void resolveBetweenFeatures_(T &map)
Definition IDConflictResolverAlgorithm.h:245
What reduceToOnePerSpectrum() found and did. All counts are over the input list.
Definition IDConflictResolverAlgorithm.h:137
Represents a single spectrum match (candidate) for a specific tandem mass spectrum (MS/MS).
Definition PeptideHit.h:52
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
void sort()
Sorts the hits by score.
const std::vector< PeptideHit > & getHits() const
returns the peptide hits as const
uint64_t UInt64
Unsigned integer type (64bit)
Definition Types.h:47
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19