OpenMS  2.5.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-2020.
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 
55 namespace OpenMS
56 {
57 
58 class OPENMS_DLLAPI IDConflictResolverAlgorithm
59 {
60 public:
67  static void resolve(FeatureMap & features, bool keep_matching = false);
68 
75  static void resolve(ConsensusMap & features, bool keep_matching = false);
76 
82  static void resolveBetweenFeatures(FeatureMap & features);
83 
89  static void resolveBetweenFeatures(ConsensusMap & features);
90 
91 protected:
92 
93  template<class T>
94  static void resolveConflict_(T & map, bool keep_matching)
95  {
96  // annotate as not part of the resolution
97  for (PeptideIdentification & p : map.getUnassignedPeptideIdentifications())
98  {
99  p.setMetaValue("feature_id", "not mapped"); // not mapped to a feature
100  }
101 
102  for (auto & c : map)
103  {
104  c.setMetaValue("feature_id", String(c.getUniqueId()));
105  if (!keep_matching)
106  {
107  resolveConflict_(c.getPeptideIdentifications(),
108  map.getUnassignedPeptideIdentifications(),
109  c.getUniqueId());
110  }
111  else
112  {
113  resolveConflictKeepMatching_(c.getPeptideIdentifications(),
114  map.getUnassignedPeptideIdentifications(),
115  c.getUniqueId());
116  }
117  }
118  }
119 
120  // compare peptide IDs by score of best hit (hits must be sorted first!)
121  // (note to self: the "static" is necessary to avoid cryptic "no matching
122  // function" errors from gcc when the comparator is used below)
123  static bool compareIDsSmallerScores_(const PeptideIdentification & left,
124  const PeptideIdentification & right);
125 
126  static void resolveConflict_(
127  std::vector<PeptideIdentification> & peptides,
128  std::vector<PeptideIdentification> & removed,
129  UInt64 uid);
130 
131  static void resolveConflictKeepMatching_(
132  std::vector<PeptideIdentification> & peptides,
133  std::vector<PeptideIdentification> & removed,
134  UInt64 uid);
135 
136  template<class T>
137  static void resolveBetweenFeatures_(T & map)
138  {
139  // unassigned peptide identifications in this map
140  std::vector<PeptideIdentification>& unassigned = map.getUnassignedPeptideIdentifications();
141 
142  // A std::map tracking the set of unique features.
143  // Uniqueness criterion/key is a pair <charge, sequence> for each feature. The peptide sequence may be modified, i.e. is not stripped.
144  typedef std::map<std::pair<Int, AASequence>, typename T::value_type*> FeatureSet;
145  FeatureSet feature_set;
146 
147  // Create a std::map `feature_set` mapping pairs <charge, sequence> to a pointer to
148  // the feature with the highest intensity for this sequence.
149  for (typename T::value_type& element : map)
150  {
151  std::vector<PeptideIdentification>& pep_ids = element.getPeptideIdentifications();
152 
153  if (!pep_ids.empty())
154  {
155  if (pep_ids.size() != 1)
156  {
157  // Should never happen. In IDConflictResolverAlgorithm TOPP tool
158  // IDConflictResolverAlgorithm::resolve() is called before IDConflictResolverAlgorithm::resolveBetweenFeatures().
159  throw OpenMS::Exception::IllegalArgument(__FILE__, __LINE__, __FUNCTION__, "Feature does contain multiple identifications.");
160  }
161 
162  // Make sure best hit is in front, i.e. sort hits first.
163  pep_ids.front().sort();
164  const std::vector<PeptideHit>& hits = pep_ids.front().getHits();
165 
166  if (!hits.empty())
167  {
168  const PeptideHit& highest_score_hit = hits.front();
169 
170  // Pair <charge, sequence> of charge of the new feature and the sequence of its highest scoring peptide hit.
171  std::pair<Int, AASequence> pair = std::make_pair(element.getCharge(), highest_score_hit.getSequence());
172 
173  // If a <charge, sequence> pair is not yet in the FeatureSet or new feature `feature_in_set`
174  // has higher intensity than its counterpart `feature_set[<charge, sequence>]`
175  // store a pointer to `feature_in_set` in `feature_set`.
176  typename FeatureSet::iterator feature_in_set = feature_set.find(pair);
177  if (feature_in_set != feature_set.end())
178  {
179  // Identical (charge, sequence) key found. Remove annotations from either the old or new feature.
180 
181  if (feature_in_set->second->getIntensity() < element.getIntensity())
182  {
183  // Remove annotations from the old low-intensity feature. But only after moving these annotations to the unassigned list.
184  std::vector<PeptideIdentification>& obsolete = feature_in_set->second->getPeptideIdentifications();
185  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
186  std::vector<PeptideIdentification> pep_ids_empty;
187  feature_in_set->second->setPeptideIdentifications(pep_ids_empty);
188 
189  // Replace feature in the set.
190  feature_in_set->second = &(element);
191  }
192  else
193  {
194  // Remove annotations from the new low-intensity feature. But only after moving these annotations to the unassigned list.
195  std::vector<PeptideIdentification>& obsolete = element.getPeptideIdentifications();
196  unassigned.insert(unassigned.end(), obsolete.begin(), obsolete.end());
197  std::vector<PeptideIdentification> pep_ids_empty;
198  element.setPeptideIdentifications(pep_ids_empty);
199  }
200  }
201  else
202  {
203  // Feature is not yet in our set -- add it.
204  feature_set[pair] = &(element);
205  }
206  }
207  }
208  }
209  }
210 
211 };
212 
213 }// namespace OpenMS
214 
ConsensusXMLFile.h
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
FileHandler.h
OpenMS::UInt64
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
OpenMS::ConsensusXMLFile::store
void store(const String &filename, const ConsensusMap &consensus_map)
Stores a consensus map to file.
OpenMS::Exception::IllegalArgument
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
OpenMS::PeptideHit::getSequence
const AASequence & getSequence() const
returns the peptide sequence without trailing or following spaces
OpenMS::String
A more convenient string class.
Definition: String.h:58
ConsensusMap.h
FeatureXMLFile.h
OpenMS::Constants::c
const double c
OpenMS::IDConflictResolverAlgorithm::resolve
static void resolve(FeatureMap &features, bool keep_matching=false)
Resolves ambiguous annotations of features with peptide identifications. The the filtered identificat...
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::FileTypes::FEATUREXML
OpenMS feature file (.featureXML)
Definition: FileTypes.h:65
OpenMS::IDConflictResolverAlgorithm::resolveBetweenFeatures
static void resolveBetweenFeatures(FeatureMap &features)
In a single (feature/consensus) map, features with the same (possibly modified) sequence and charge s...
OpenMS::FeatureXMLFile::load
void load(const String &filename, FeatureMap &feature_map)
loads the file with name filename into map and calls updateRanges().
FeatureMap.h
OpenMS::FileTypes::Type
Type
Actual file types enum.
Definition: FileTypes.h:58
OpenMS::IDConflictResolverAlgorithm
Definition: IDConflictResolverAlgorithm.h:58
OpenMS::IDConflictResolverAlgorithm::resolveBetweenFeatures_
static void resolveBetweenFeatures_(T &map)
Definition: IDConflictResolverAlgorithm.h:137
OpenMS::FileHandler::getType
static FileTypes::Type getType(const String &filename)
Tries to determine the file type (by name or content)
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:79
OpenMS::FeatureXMLFile::store
void store(const String &filename, const FeatureMap &feature_map)
stores the map feature_map in file with name filename.
OpenMS::ConsensusXMLFile::load
void load(const String &filename, ConsensusMap &map)
Loads a consensus map from file and calls updateRanges.
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:95
OpenMS::PeptideIdentification
Represents the peptide hits for a spectrum.
Definition: PeptideIdentification.h:62
IDConflictResolverAlgorithm.h
OpenMS::FeatureXMLFile
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:68
OpenMS::IDConflictResolverAlgorithm::resolveConflict_
static void resolveConflict_(T &map, bool keep_matching)
Definition: IDConflictResolverAlgorithm.h:94
PeptideIdentification.h
OpenMS::DataProcessing::FILTERING
Data filtering or extraction.
Definition: DataProcessing.h:71
TOPPBase.h
OpenMS::ConsensusXMLFile
This class provides Input functionality for ConsensusMaps and Output functionality for alignments and...
Definition: ConsensusXMLFile.h:61
OpenMS::PeptideHit
Representation of a peptide hit.
Definition: PeptideHit.h:54