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