OpenMS
Loading...
Searching...
No Matches
AnnotatedMSRun.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: David Voigt, Timo Sachsenberg $
7// -------------------------------------------------------------------------------------------------------------------------------------
8
9#pragma once
10
11#include <OpenMS/OpenMSConfig.h>
16#include <boost/range/combine.hpp>
17
18#include <functional>
19#include <vector>
20
21namespace OpenMS
22{
23 class PeptideIdentification;
24
25 class MSSpectrum;
26
37 class OPENMS_DLLAPI AnnotatedMSRun
38 {
39 public:
40 using SpectrumIdRef = std::pair<MSSpectrum&, PeptideIdentification&>;
41 using ConstSpectrumIdRef = std::pair<const MSSpectrum&, const PeptideIdentification&>;
44
45
47 AnnotatedMSRun() = default;
48
53 explicit AnnotatedMSRun(MSExperiment&& experiment) : data(std::move(experiment))
54 {};
55
58
60 AnnotatedMSRun(const AnnotatedMSRun&) = default;
63
65 ~AnnotatedMSRun() = default;
66
68 bool operator==(const AnnotatedMSRun& rhs) const
69 {
70 return data == rhs.data &&
71 peptide_ids_ == rhs.peptide_ids_ &&
72 protein_ids_ == rhs.protein_ids_;
73 }
74
76 bool operator!=(const AnnotatedMSRun& rhs) const
77 {
78 return !(*this == rhs);
79 }
80
85 std::vector<ProteinIdentification>& getProteinIdentifications()
86 {
87 return protein_ids_;
88 }
89
94 const std::vector<ProteinIdentification>& getProteinIdentifications() const
95 {
96 return protein_ids_;
97 }
98
103 void setProteinIdentifications(const std::vector<ProteinIdentification>& ids)
104 {
105 protein_ids_ = ids;
106 }
107
112 void setProteinIdentifications(std::vector<ProteinIdentification>&& ids)
113 {
114 protein_ids_ = std::move(ids);
115 }
116
122
128
134
140
146
152
157 void setMSExperiment(MSExperiment&& experiment);
158
163 void setMSExperiment(const MSExperiment& experiment);
164
169 inline auto cbegin() const
170 {
171 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
172 return PairIterator(data.getSpectra().cbegin(), peptide_ids_.cbegin());
173 }
174
179 inline auto begin()
180 {
181 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
182 return PairIterator(data.getSpectra().begin(), peptide_ids_.begin());
183 }
184
189 inline auto begin() const
190 {
191 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
192 return PairIterator(data.getSpectra().cbegin(), peptide_ids_.cbegin());
193 }
194
199 inline auto end()
200 {
201 return PairIterator(data.getSpectra().end(), peptide_ids_.end());
202 }
203
208 inline auto end() const
209 {
210 return PairIterator(data.getSpectra().end(), peptide_ids_.end());
211 }
212
217 inline auto cend() const
218 {
219 return PairIterator(data.getSpectra().cend(), peptide_ids_.cend());
220 }
221
227 inline SpectrumIdRef operator[](size_t idx)
228 {
229 if (idx >= peptide_ids_.size())
230 {
231 throw Exception::IndexOverflow(__FILE__, __LINE__,
232 OPENMS_PRETTY_FUNCTION,
233 idx, peptide_ids_.size());
234 }
235 if (idx >= data.getSpectra().size())
236 {
237 throw Exception::IndexOverflow(__FILE__, __LINE__,
238 OPENMS_PRETTY_FUNCTION,
239 idx, data.getSpectra().size());
240 }
241 return {data.getSpectra()[idx], peptide_ids_[idx]};
242 }
243
249 inline ConstSpectrumIdRef operator[](size_t idx) const
250 {
251 if (idx >= peptide_ids_.size())
252 {
253 throw Exception::IndexOverflow(__FILE__, __LINE__,
254 OPENMS_PRETTY_FUNCTION,
255 idx, peptide_ids_.size());
256 }
257 if (idx >= data.getSpectra().size())
258 {
259 throw Exception::IndexOverflow(__FILE__, __LINE__,
260 OPENMS_PRETTY_FUNCTION,
261 idx, data.getSpectra().size());
262 }
263 return {data.getSpectra()[idx], peptide_ids_[idx]};
264 }
265
272 template<typename T1, typename T2>
274 {
275 using iterator_category = std::forward_iterator_tag;
276 using difference_type = std::ptrdiff_t;
277
283 PairIterator(T1 ptr1, T2 ptr2) : m_ptr1(ptr1), m_ptr2(ptr2)
284 {}
285
291 {
292 ++m_ptr1;
293 ++m_ptr2;
294 return *this;
295 }
296
302 {
303 auto tmp(*this);
304 ++(*this);
305 return tmp;
306 }
307
313 {
314 return std::make_pair(std::ref(*m_ptr1), std::ref(*m_ptr2));
315 }
316
323 inline friend bool operator==(const PairIterator& a, const PairIterator& b)
324 {
325 return a.m_ptr1 == b.m_ptr1 && a.m_ptr2 == b.m_ptr2;
326 }
327
334 inline friend bool operator!=(const PairIterator& a, const PairIterator& b)
335 {
336 return !(a == b);
337 }
338
339 private:
342 };
343
346
347 private:
348
349 // Helper to enforce invariant
350 void checkPeptideIdSize_(const char* function_name) const;
351
353 std::vector<ProteinIdentification> protein_ids_;
355 };
356} // namespace OpenMS
357
358// Hash function specialization for AnnotatedMSRun
359namespace std
360{
368 template<>
369 struct hash<OpenMS::AnnotatedMSRun>
370 {
371 std::size_t operator()(const OpenMS::AnnotatedMSRun& run) const noexcept
372 {
373 // Start with hash of the MSExperiment size (number of spectra)
374 std::size_t seed = OpenMS::hash_int(run.getMSExperiment().size());
375
376 // Hash the number of chromatograms
377 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getMSExperiment().getChromatograms().size()));
378
379 // Hash the number of peptide identifications
380 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getPeptideIdentifications().size()));
381
382 // Hash the number of protein identifications
383 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getProteinIdentifications().size()));
384
385 // Hash identifying properties from protein identifications
386 for (const auto& prot_id : run.getProteinIdentifications())
387 {
388 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(prot_id.getIdentifier()));
389 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(prot_id.getSearchEngine()));
390 OpenMS::hash_combine(seed, OpenMS::hash_int(prot_id.getHits().size()));
391 }
392
393 // Hash identifying properties from peptide identifications
394 for (const auto& pep_id : run.getPeptideIdentifications())
395 {
396 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(pep_id.getIdentifier()));
397 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(pep_id.getScoreType()));
398 OpenMS::hash_combine(seed, OpenMS::hash_int(pep_id.getHits().size()));
399 OpenMS::hash_combine(seed, OpenMS::hash_float(pep_id.getSignificanceThreshold()));
400 }
401
402 // Hash identifying properties from spectra
403 for (const auto& spectrum : run.getMSExperiment().getSpectra())
404 {
405 OpenMS::hash_combine(seed, OpenMS::hash_float(spectrum.getRT()));
406 OpenMS::hash_combine(seed, OpenMS::hash_int(spectrum.getMSLevel()));
407 OpenMS::hash_combine(seed, OpenMS::hash_int(spectrum.size()));
408 }
409
410 return seed;
411 }
412 };
413} // namespace std
Class for storing MS run data with peptide and protein identifications.
Definition AnnotatedMSRun.h:38
const PeptideIdentificationList & getPeptideIdentifications() const
Get all peptide identifications for all spectra (const version)
std::vector< ProteinIdentification > protein_ids_
Definition AnnotatedMSRun.h:353
MSExperiment & getMSExperiment()
Get the MSExperiment.
PeptideIdentificationList & getPeptideIdentifications()
Get all peptide identifications for all spectra.
~AnnotatedMSRun()=default
Destructor.
void setProteinIdentifications(const std::vector< ProteinIdentification > &ids)
set the protein identifications
Definition AnnotatedMSRun.h:103
AnnotatedMSRun(MSExperiment &&experiment)
Move constructor for efficiently loading a MSExperiment without a deep copy.
Definition AnnotatedMSRun.h:53
AnnotatedMSRun::PairIterator< std::vector< MSSpectrum >::const_iterator, PeptideIdentificationList::const_iterator > ConstIterator
Definition AnnotatedMSRun.h:345
SpectrumIdRef operator[](size_t idx)
Access a spectrum and its associated peptide identification.
Definition AnnotatedMSRun.h:227
bool operator!=(const AnnotatedMSRun &rhs) const
Inequality operator.
Definition AnnotatedMSRun.h:76
bool operator==(const AnnotatedMSRun &rhs) const
Equality operator.
Definition AnnotatedMSRun.h:68
void setProteinIdentifications(std::vector< ProteinIdentification > &&ids)
Set the protein identifications (move version)
Definition AnnotatedMSRun.h:112
AnnotatedMSRun()=default
Default constructor.
AnnotatedMSRun(const AnnotatedMSRun &)=default
Copy constructor.
auto end()
Get an iterator to the end of the data.
Definition AnnotatedMSRun.h:199
void checkPeptideIdSize_(const char *function_name) const
AnnotatedMSRun & operator=(AnnotatedMSRun &&)=default
void setPeptideIdentifications(PeptideIdentificationList &&ids)
Set all peptide identifications for all spectra (move version)
void setMSExperiment(const MSExperiment &experiment)
Set the MSExperiment.
AnnotatedMSRun::PairIterator< std::vector< MSSpectrum >::iterator, PeptideIdentificationList::iterator > Iterator
Definition AnnotatedMSRun.h:344
std::vector< ProteinIdentification > & getProteinIdentifications()
Get the protein identification.
Definition AnnotatedMSRun.h:85
MSExperiment data
Definition AnnotatedMSRun.h:354
void setMSExperiment(MSExperiment &&experiment)
Set the MSExperiment.
auto cbegin() const
Get a const iterator to the beginning of the data.
Definition AnnotatedMSRun.h:169
AnnotatedMSRun(AnnotatedMSRun &&)=default
Move constructor.
PeptideIdentificationList peptide_ids_
Definition AnnotatedMSRun.h:352
ConstSpectrumIdRef operator[](size_t idx) const
Access a spectrum and its associated peptide identification (const version)
Definition AnnotatedMSRun.h:249
auto begin() const
Get a const iterator to the beginning of the data.
Definition AnnotatedMSRun.h:189
auto begin()
Get an iterator to the beginning of the data.
Definition AnnotatedMSRun.h:179
auto end() const
Get a const iterator to the end of the data.
Definition AnnotatedMSRun.h:208
const MSExperiment & getMSExperiment() const
Get the MSExperiment (const version)
std::pair< MSSpectrum &, PeptideIdentification & > SpectrumIdRef
Definition AnnotatedMSRun.h:40
void setPeptideIdentifications(const PeptideIdentificationList &ids)
Set all peptide identifications for all spectra.
auto cend() const
Get a const iterator to the end of the data.
Definition AnnotatedMSRun.h:217
std::pair< const MSSpectrum &, const PeptideIdentification & > ConstSpectrumIdRef
Definition AnnotatedMSRun.h:41
AnnotatedMSRun & operator=(const AnnotatedMSRun &)=default
const std::vector< ProteinIdentification > & getProteinIdentifications() const
Get the protein identification (const version)
Definition AnnotatedMSRun.h:94
Int overflow exception.
Definition Exception.h:211
typename VecMember::iterator iterator
Definition ExposedVector.h:68
typename VecMember::const_iterator const_iterator
Definition ExposedVector.h:69
The representation of a chromatogram.
Definition MSChromatogram.h:30
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
Container for peptide identifications from multiple spectra.
Definition PeptideIdentificationList.h:66
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::size_t hash_int(T value) noexcept
Hash for an integer type.
Definition HashUtils.h:107
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
std::size_t hash_float(T value) noexcept
Hash for a floating point type (float or double).
Definition HashUtils.h:142
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
Iterator for pairs of spectra and peptide identifications.
Definition AnnotatedMSRun.h:274
friend bool operator!=(const PairIterator &a, const PairIterator &b)
Inequality operator.
Definition AnnotatedMSRun.h:334
PairIterator operator++(int)
Post-increment operator.
Definition AnnotatedMSRun.h:301
std::forward_iterator_tag iterator_category
Definition AnnotatedMSRun.h:275
auto operator*()
Dereference operator.
Definition AnnotatedMSRun.h:312
friend bool operator==(const PairIterator &a, const PairIterator &b)
Equality operator.
Definition AnnotatedMSRun.h:323
PairIterator(T1 ptr1, T2 ptr2)
Constructor.
Definition AnnotatedMSRun.h:283
T1 m_ptr1
Definition AnnotatedMSRun.h:340
std::ptrdiff_t difference_type
Definition AnnotatedMSRun.h:276
T2 m_ptr2
Definition AnnotatedMSRun.h:341
PairIterator & operator++()
Pre-increment operator.
Definition AnnotatedMSRun.h:290
std::size_t operator()(const OpenMS::AnnotatedMSRun &run) const noexcept
Definition AnnotatedMSRun.h:371