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