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
105protected:
106
107 template<class T>
108 static void resolveConflict_(T& map, bool keep_matching)
109 {
110 // annotate as not part of the resolution
111 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
112 {
113 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
114 }
115
116 for (auto& c : map)
117 {
118 c.setMetaValue("feature_id", String(c.getUniqueId()));
119 if (!keep_matching)
120 {
121 resolveConflict_(c.getPeptideIdentifications(),
122 map.getUnassignedPeptideIdentifications(),
123 c.getUniqueId());
124 }
125 else
126 {
127 resolveConflictKeepMatching_(c.getPeptideIdentifications(),
128 map.getUnassignedPeptideIdentifications(),
129 c.getUniqueId());
130 }
131 }
132 }
133
134 // compare peptide IDs by score of best hit (hits must be sorted first!)
135 // (note to self: the "static" is necessary to avoid cryptic "no matching
136 // function" errors from gcc when the comparator is used below)
138 const PeptideIdentification & right);
139
140 static void resolveConflict_(
141 PeptideIdentificationList & peptides,
143 UInt64 uid);
144
146 PeptideIdentificationList & peptides,
148 UInt64 uid);
149
151 PeptideIdentificationList & peptides,
153 UInt64 uid);
154
155 template<class T>
156 static void rankAggregation_(T& map)
157 {
158 // annotate unassigned IDs as not part of the resolution
159 for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
160 {
161 p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
162 }
163
164 for (auto& c : map)
165 {
166 c.setMetaValue("feature_id", String(c.getUniqueId()));
167 resolveAggregateConflict_(c.getPeptideIdentifications(),
168 map.getUnassignedPeptideIdentifications(),
169 c.getUniqueId());
170 }
171 }
172
173 template<class T>
174 static void resolveBetweenFeatures_(T & map)
175 {
176 // unassigned peptide identifications in this map
177 PeptideIdentificationList& unassigned = map.getUnassignedPeptideIdentifications();
178
179 // A std::map tracking the set of unique features.
180 // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
181 typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
182 FeatureSet feature_set;
183
184 // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
185 // the feature with the highest intensity for this sequence.
186 for (typename T::value_type& element : map)
187 {
188 PeptideIdentificationList& pep_ids = element.getPeptideIdentifications();
189
190 if (!pep_ids.empty())
191 {
192 if (pep_ids.size() != 1)
193 {
194 // Should never happen. In IDConflictResolverAlgorithm TOPP tool
195 // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
196 throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
197 }
198
199 // Make sure best hit is in front, i.e. sort hits first.
200 pep_ids.front().sort();
201 const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
202
203 if (!hits.empty())
204 {
205 const PeptideHit& highest_score_hit = hits.front();
206
207 // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
208 std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
209
210 // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
211 // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
212 // store a pointer to `feature_in_set` in `feature_set`.
213 typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
214 if (feature_in_set != feature_set.end())
215 {
216 // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
217
218 if (feature_in_set->second->getIntensity() < element.getIntensity())
219 {
220 // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
221 PeptideIdentificationList& obsolete = feature_in_set->second->getPeptideIdentifications();
222 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
223 PeptideIdentificationList pep_ids_empty;
224 feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
225
226 // Replace feature in the set.
227 feature_in_set->second = &(element);
228 }
229 else
230 {
231 // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
232 PeptideIdentificationList& obsolete = element.getPeptideIdentifications();
233 unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
234 PeptideIdentificationList pep_ids_empty;
235 element.setPeptideIdentifications(pep_ids_empty);
236 }
237 }
238 else
239 {
240 // Feature is not yet in our set -- add it.
241 feature_set[pair] = &(element);
242 }
243 }
244 }
245 }
246 }
247
248};
249
250}// namespace OpenMS
251
A container for consensus elements.
Definition ConsensusMap.h:67
A method or algorithm argument contains illegal values.
Definition Exception.h:630
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 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)
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:108
static void rankAggregation_(T &map)
Definition IDConflictResolverAlgorithm.h:156
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:174
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
A more convenient string class.
Definition String.h:32
uint64_t UInt64
Unsigned integer type (64bit)
Definition Types.h:47
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19