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