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