OpenMS  2.7.0
IDConflictResolverAlgorithm.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hendrik Weisser $
32 // $Authors: Hendrik Weisser, Lucia Espona, Moritz Freidank $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
40 
41 //-------------------------------------------------------------
42 // Doxygen docu
43 //-------------------------------------------------------------
44 
45 
46 
47 namespace OpenMS
48 {
49 
57 class OPENMS_DLLAPI IDConflictResolverAlgorithm
58 {
59 public:
66  static void resolve(FeatureMap & features, bool keep_matching = false);
67 
74  static void resolve(ConsensusMap & features, bool keep_matching = false);
75 
81  static void resolveBetweenFeatures(FeatureMap & features);
82 
88  static void resolveBetweenFeatures(ConsensusMap & features);
89 
90 protected:
91 
92  template<class T>
93  static void resolveConflict_(T & map, bool keep_matching)
94  {
95  // annotate as not part of the resolution
96  for (PeptideIdentification & p : map.getUnassignedPeptideIdentifications())
97  {
98  p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
99  }
100 
101  for (auto & c : map)
102  {
103  c.setMetaValue("feature_id", String(c.getUniqueId()));
104  if (!keep_matching)
105  {
106  resolveConflict_(c.getPeptideIdentifications(),
107  map.getUnassignedPeptideIdentifications(),
108  c.getUniqueId());
109  }
110  else
111  {
112  resolveConflictKeepMatching_(c.getPeptideIdentifications(),
113  map.getUnassignedPeptideIdentifications(),
114  c.getUniqueId());
115  }
116  }
117  }
118 
119  // compare peptide IDs by score of best hit (hits must be sorted first!)
120  // (note to self: the "static" is necessary to avoid cryptic "no matching
121  // function" errors from gcc when the comparator is used below)
123  const PeptideIdentification & right);
124 
125  static void resolveConflict_(
126  std::vector<PeptideIdentification> & peptides,
127  std::vector<PeptideIdentification> & removed,
128  UInt64 uid);
129 
131  std::vector<PeptideIdentification> & peptides,
132  std::vector<PeptideIdentification> & removed,
133  UInt64 uid);
134 
135  template<class T>
136  static void resolveBetweenFeatures_(T & map)
137  {
138  // unassigned peptide identifications in this map
139  std::vector<PeptideIdentification>& unassigned = map.getUnassignedPeptideIdentifications();
140 
141  // A std::map tracking the set of unique features.
142  // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
143  typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
144  FeatureSet feature_set;
145 
146  // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
147  // the feature with the highest intensity for this sequence.
148  for (typename T::value_type& element : map)
149  {
150  std::vector<PeptideIdentification>& pep_ids = element.getPeptideIdentifications();
151 
152  if (!pep_ids.empty())
153  {
154  if (pep_ids.size() != 1)
155  {
156  // Should never happen. In IDConflictResolverAlgorithm TOPP tool
157  // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
158  throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
159  }
160 
161  // Make sure best hit is in front, i.e. sort hits first.
162  pep_ids.front().sort();
163  const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
164 
165  if (!hits.empty())
166  {
167  const PeptideHit& highest_score_hit = hits.front();
168 
169  // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
170  std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
171 
172  // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
173  // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
174  // store a pointer to `feature_in_set` in `feature_set`.
175  typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
176  if (feature_in_set != feature_set.end())
177  {
178  // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
179 
180  if (feature_in_set->second->getIntensity() < element.getIntensity())
181  {
182  // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
183  std::vector<PeptideIdentification>& obsolete = feature_in_set->second->getPeptideIdentifications();
184  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
185  std::vector<PeptideIdentification> pep_ids_empty;
186  feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
187 
188  // Replace feature in the set.
189  feature_in_set->second = &(element);
190  }
191  else
192  {
193  // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
194  std::vector<PeptideIdentification>& obsolete = element.getPeptideIdentifications();
195  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
196  std::vector<PeptideIdentification> pep_ids_empty;
197  element.setPeptideIdentifications(pep_ids_empty);
198  }
199  }
200  else
201  {
202  // Feature is not yet in our set -- add it.
203  feature_set[pair] = &(element);
204  }
205  }
206  }
207  }
208  }
209 
210 };
211 
212 }// namespace OpenMS
213 
A container for consensus elements.
Definition: ConsensusMap.h:88
A method or algorithm argument contains illegal values.
Definition: Exception.h:656
A container for features.
Definition: FeatureMap.h:105
Resolves ambiguous annotations of features with peptide identifications.
Definition: IDConflictResolverAlgorithm.h:58
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:93
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:136
Representation of a peptide hit.
Definition: PeptideHit.h:57
const AASequence & getSequence() const
returns the peptide sequence without trailing or following spaces
Represents the peptide hits for a spectrum.
Definition: PeptideIdentification.h:65
A more convenient string class.
Definition: String.h:61
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
const double c
Definition: Constants.h:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47