All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
22 namespace OpenMS
23 {
24 
32 class OPENMS_DLLAPI IDConflictResolverAlgorithm
33 {
34 public:
43  static void resolve(FeatureMap& features, bool keep_matching = false);
44 
53  static void resolve(ConsensusMap& features, bool keep_matching = false);
54 
60  static void resolveBetweenFeatures(FeatureMap& features);
61 
67  static void resolveBetweenFeatures(ConsensusMap& features);
68 
69 protected:
70 
71  template<class T>
72  static void resolveConflict_(T& map, bool keep_matching)
73  {
74  // annotate as not part of the resolution
75  for (PeptideIdentification& p : map.getUnassignedPeptideIdentifications())
76  {
77  p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
78  }
79 
80  for (auto& c : map)
81  {
82  c.setMetaValue("feature_id", String(c.getUniqueId()));
83  if (!keep_matching)
84  {
85  resolveConflict_(c.getPeptideIdentifications(),
86  map.getUnassignedPeptideIdentifications(),
87  c.getUniqueId());
88  }
89  else
90  {
91  resolveConflictKeepMatching_(c.getPeptideIdentifications(),
92  map.getUnassignedPeptideIdentifications(),
93  c.getUniqueId());
94  }
95  }
96  }
97 
98  // compare peptide IDs by score of best hit (hits must be sorted first!)
99  // (note to self: the "static" is necessary to avoid cryptic "no matching
100  // function" errors from gcc when the comparator is used below)
102  const PeptideIdentification & right);
103 
104  static void resolveConflict_(
105  PeptideIdentificationList & peptides,
106  PeptideIdentificationList & removed,
107  UInt64 uid);
108 
110  PeptideIdentificationList & peptides,
111  PeptideIdentificationList & removed,
112  UInt64 uid);
113 
114  template<class T>
115  static void resolveBetweenFeatures_(T & map)
116  {
117  // unassigned peptide identifications in this map
118  PeptideIdentificationList& unassigned = map.getUnassignedPeptideIdentifications();
119 
120  // A std::map tracking the set of unique features.
121  // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
122  typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
123  FeatureSet feature_set;
124 
125  // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
126  // the feature with the highest intensity for this sequence.
127  for (typename T::value_type& element : map)
128  {
129  PeptideIdentificationList& pep_ids = element.getPeptideIdentifications();
130 
131  if (!pep_ids.empty())
132  {
133  if (pep_ids.size() != 1)
134  {
135  // Should never happen. In IDConflictResolverAlgorithm TOPP tool
136  // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
137  throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
138  }
139 
140  // Make sure best hit is in front, i.e. sort hits first.
141  pep_ids.front().sort();
142  const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
143 
144  if (!hits.empty())
145  {
146  const PeptideHit& highest_score_hit = hits.front();
147 
148  // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
149  std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
150 
151  // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
152  // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
153  // store a pointer to `feature_in_set` in `feature_set`.
154  typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
155  if (feature_in_set != feature_set.end())
156  {
157  // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
158 
159  if (feature_in_set->second->getIntensity() < element.getIntensity())
160  {
161  // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
162  PeptideIdentificationList& obsolete = feature_in_set->second->getPeptideIdentifications();
163  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
164  PeptideIdentificationList pep_ids_empty;
165  feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
166 
167  // Replace feature in the set.
168  feature_in_set->second = &(element);
169  }
170  else
171  {
172  // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
173  PeptideIdentificationList& obsolete = element.getPeptideIdentifications();
174  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
175  PeptideIdentificationList pep_ids_empty;
176  element.setPeptideIdentifications(pep_ids_empty);
177  }
178  }
179  else
180  {
181  // Feature is not yet in our set -- add it.
182  feature_set[pair] = &(element);
183  }
184  }
185  }
186  }
187  }
188 
189 };
190 
191 }// namespace OpenMS
192 
A container for consensus elements.
Definition: ConsensusMap.h:68
A method or algorithm argument contains illegal values.
Definition: Exception.h:629
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
VectorElement & front() noexcept
Get first element.
Definition: ExposedVector.h:267
iterator begin() noexcept
Definition: ExposedVector.h:104
iterator end() noexcept
Definition: ExposedVector.h:108
A container for features.
Definition: FeatureMap.h:82
Resolves ambiguous annotations of features with peptide identifications.
Definition: IDConflictResolverAlgorithm.h:33
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 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 resolveConflict_(T &map, bool keep_matching)
Definition: IDConflictResolverAlgorithm.h:72
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:115
Represents a single spectrum match (candidate) for a specific tandem mass spectrum (MS/MS).
Definition: PeptideHit.h:50
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:63
const std::vector< PeptideHit > & getHits() const
returns the peptide hits as const
void sort()
Sorts the hits by score.
A more convenient string class.
Definition: String.h:34
uint64_t UInt64
Unsigned integer type (64bit)
Definition: Types.h:47
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19