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