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