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:
43 static void resolve(FeatureMap& features, bool keep_matching = false);
44
53 static void resolve(ConsensusMap& features, bool keep_matching = false);
54
72
90
96 static void resolveBetweenFeatures(FeatureMap& features);
97
103 static void resolveBetweenFeatures(ConsensusMap& features);
104
106 struct OPENMS_DLLAPI UnresolvedIdentifications
107 {
110 Size removed = 0;
114 Size multiply_identified_spectra = 0;
117 Size without_spectrum_reference = 0;
120 Size inconsistent_score_direction = 0;
122 std::string example;
123 };
124
145
146protected:
147
148 template<class T>
149 static void resolveConflict_(T& map, bool keep_matching)
150 {
151 // annotate as not part of the resolution
152 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
153 {
154 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
155 }
156
157 for (auto& c : map)
158 {
159 c.setMetaValue("feature_id",StringUtils::toStr(c.getUniqueId()));
160 if (!keep_matching)
161 {
162 resolveConflict_(c.getPeptideIdentifications(),
163 map.getUnassignedPeptideIdentifications(),
164 c.getUniqueId());
165 }
166 else
167 {
168 resolveConflictKeepMatching_(c.getPeptideIdentifications(),
169 map.getUnassignedPeptideIdentifications(),
170 c.getUniqueId());
171 }
172 }
173 }
174
175 // compare peptide IDs by score of best hit (hits must be sorted first!)
176 // (note to self: the "static" is necessary to avoid cryptic "no matching
177 // function" errors from gcc when the comparator is used below)
179 const PeptideIdentification & right);
180
181 static void resolveConflict_(
182 PeptideIdentificationList & peptides,
184 UInt64 uid);
185
187 PeptideIdentificationList & peptides,
189 UInt64 uid);
190
192 PeptideIdentificationList & peptides,
194 UInt64 uid);
195
196 template<class T>
197 static void rankAggregation_(T& map)
198 {
199 // annotate unassigned IDs as not part of the resolution
200 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
201 {
202 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
203 }
204
205 for (auto& c : map)
206 {
207 c.setMetaValue("feature_id",StringUtils::toStr(c.getUniqueId()));
208 resolveAggregateConflict_(c.getPeptideIdentifications(),
209 map.getUnassignedPeptideIdentifications(),
210 c.getUniqueId());
211 }
212 }
213
214 template<class T>
215 static void resolveBetweenFeatures_(T & map)
216 {
217 // unassigned peptide identifications in this map
218 PeptideIdentificationList& unassigned = map.getUnassignedPeptideIdentifications();
219
220 // A std::map tracking the set of unique features.
221 // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
222 typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
223 FeatureSet feature_set;
224
225 // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
226 // the feature with the highest intensity for this sequence.
227 for (typename T::value_type& element : map)
228 {
229 PeptideIdentificationList& pep_ids = element.getPeptideIdentifications();
230
231 if (!pep_ids.empty())
232 {
233 if (pep_ids.size() != 1)
234 {
235 // Should never happen. In IDConflictResolverAlgorithm TOPP tool
236 // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
237 throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
238 }
239
240 // Make sure best hit is in front, i.e. sort hits first.
241 pep_ids.front().sort();
242 const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
243
244 if (!hits.empty())
245 {
246 const PeptideHit& highest_score_hit = hits.front();
247
248 // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
249 std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
250
251 // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
252 // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
253 // store a pointer to `feature_in_set` in `feature_set`.
254 typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
255 if (feature_in_set != feature_set.end())
256 {
257 // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
258
259 if (feature_in_set->second->getIntensity() < element.getIntensity())
260 {
261 // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
262 PeptideIdentificationList& obsolete = feature_in_set->second->getPeptideIdentifications();
263 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
264 PeptideIdentificationList pep_ids_empty;
265 feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
266
267 // Replace feature in the set.
268 feature_in_set->second = &(element);
269 }
270 else
271 {
272 // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
273 PeptideIdentificationList& obsolete = element.getPeptideIdentifications();
274 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
275 PeptideIdentificationList pep_ids_empty;
276 element.setPeptideIdentifications(pep_ids_empty);
277 }
278 }
279 else
280 {
281 // Feature is not yet in our set -- add it.
282 feature_set[pair] = &(element);
283 }
284 }
285 }
286 }
287 }
288
289};
290
291}// namespace OpenMS
292
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:122
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 resolveConflict_(T &map, bool keep_matching)
Definition IDConflictResolverAlgorithm.h:149
static void rankAggregation_(T &map)
Definition IDConflictResolverAlgorithm.h:197
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:215
What reduceToOnePerSpectrum() found and did. All counts are over the input list.
Definition IDConflictResolverAlgorithm.h:107
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