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>
17#include <boost/range/combine.hpp>
18
19#include <functional>
20#include <vector>
21
22namespace OpenMS
23{
24 class PeptideIdentification;
25
26 class MSSpectrum;
27
38 class OPENMS_DLLAPI AnnotatedMSRun
39 {
40 public:
41 using SpectrumIdRef = std::pair<MSSpectrum&, PeptideIdentification&>;
42 using ConstSpectrumIdRef = std::pair<const MSSpectrum&, const PeptideIdentification&>;
45
46
48 AnnotatedMSRun() = default;
49
54 explicit AnnotatedMSRun(MSExperiment&& experiment) : data(std::move(experiment))
55 {};
56
59
61 AnnotatedMSRun(const AnnotatedMSRun&) = default;
64
66 ~AnnotatedMSRun() = default;
67
69 bool operator==(const AnnotatedMSRun& rhs) const
70 {
71 return data == rhs.data &&
72 peptide_ids_ == rhs.peptide_ids_ &&
73 protein_ids_ == rhs.protein_ids_;
74 }
75
77 bool operator!=(const AnnotatedMSRun& rhs) const
78 {
79 return !(*this == rhs);
80 }
81
86 std::vector<ProteinIdentification>& getProteinIdentifications()
87 {
88 return protein_ids_;
89 }
90
95 const std::vector<ProteinIdentification>& getProteinIdentifications() const
96 {
97 return protein_ids_;
98 }
99
104 void setProteinIdentifications(const std::vector<ProteinIdentification>& ids)
105 {
106 protein_ids_ = ids;
107 }
108
113 void setProteinIdentifications(std::vector<ProteinIdentification>&& ids)
114 {
115 protein_ids_ = std::move(ids);
116 }
117
123
129
135
141
147
153
158 void setMSExperiment(MSExperiment&& experiment);
159
164 void setMSExperiment(const MSExperiment& experiment);
165
170 inline auto cbegin() const
171 {
172 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
173 return PairIterator(data.getSpectra().cbegin(), peptide_ids_.cbegin());
174 }
175
180 inline auto begin()
181 {
182 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
183 return PairIterator(data.getSpectra().begin(), peptide_ids_.begin());
184 }
185
190 inline auto begin() const
191 {
192 checkPeptideIdSize_(OPENMS_PRETTY_FUNCTION);
193 return PairIterator(data.getSpectra().cbegin(), peptide_ids_.cbegin());
194 }
195
200 inline auto end()
201 {
202 return PairIterator(data.getSpectra().end(), peptide_ids_.end());
203 }
204
209 inline auto end() const
210 {
211 return PairIterator(data.getSpectra().end(), peptide_ids_.end());
212 }
213
218 inline auto cend() const
219 {
220 return PairIterator(data.getSpectra().cend(), peptide_ids_.cend());
221 }
222
228 inline SpectrumIdRef operator[](size_t idx)
229 {
230 if (idx >= peptide_ids_.size())
231 {
232 throw Exception::IndexOverflow(__FILE__, __LINE__,
233 OPENMS_PRETTY_FUNCTION,
234 idx, peptide_ids_.size());
235 }
236 if (idx >= data.getSpectra().size())
237 {
238 throw Exception::IndexOverflow(__FILE__, __LINE__,
239 OPENMS_PRETTY_FUNCTION,
240 idx, data.getSpectra().size());
241 }
242 return {data.getSpectra()[idx], peptide_ids_[idx]};
243 }
244
250 inline ConstSpectrumIdRef operator[](size_t idx) const
251 {
252 if (idx >= peptide_ids_.size())
253 {
254 throw Exception::IndexOverflow(__FILE__, __LINE__,
255 OPENMS_PRETTY_FUNCTION,
256 idx, peptide_ids_.size());
257 }
258 if (idx >= data.getSpectra().size())
259 {
260 throw Exception::IndexOverflow(__FILE__, __LINE__,
261 OPENMS_PRETTY_FUNCTION,
262 idx, data.getSpectra().size());
263 }
264 return {data.getSpectra()[idx], peptide_ids_[idx]};
265 }
266
273 template<typename T1, typename T2>
275 {
276 using iterator_category = std::forward_iterator_tag;
277 using difference_type = std::ptrdiff_t;
278
284 PairIterator(T1 ptr1, T2 ptr2) : m_ptr1(ptr1), m_ptr2(ptr2)
285 {}
286
292 {
293 ++m_ptr1;
294 ++m_ptr2;
295 return *this;
296 }
297
303 {
304 auto tmp(*this);
305 ++(*this);
306 return tmp;
307 }
308
314 {
315 return std::make_pair(std::ref(*m_ptr1), std::ref(*m_ptr2));
316 }
317
324 inline friend bool operator==(const PairIterator& a, const PairIterator& b)
325 {
326 return a.m_ptr1 == b.m_ptr1 && a.m_ptr2 == b.m_ptr2;
327 }
328
335 inline friend bool operator!=(const PairIterator& a, const PairIterator& b)
336 {
337 return !(a == b);
338 }
339
340 private:
343 };
344
347
348 private:
349
350 // Helper to enforce invariant
351 void checkPeptideIdSize_(const char* function_name) const;
352
354 std::vector<ProteinIdentification> protein_ids_;
356 };
357} // namespace OpenMS
358
359// Hash function specialization for AnnotatedMSRun
360namespace std
361{
369 template<>
370 struct hash<OpenMS::AnnotatedMSRun>
371 {
372 std::size_t operator()(const OpenMS::AnnotatedMSRun& run) const noexcept
373 {
374 // Start with hash of the MSExperiment size (number of spectra)
375 std::size_t seed = OpenMS::hash_int(run.getMSExperiment().size());
376
377 // Hash the number of chromatograms
378 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getMSExperiment().getChromatograms().size()));
379
380 // Hash the number of peptide identifications
381 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getPeptideIdentifications().size()));
382
383 // Hash the number of protein identifications
384 OpenMS::hash_combine(seed, OpenMS::hash_int(run.getProteinIdentifications().size()));
385
386 // Hash identifying properties from protein identifications
387 for (const auto& prot_id : run.getProteinIdentifications())
388 {
389 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(prot_id.getIdentifier()));
390 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(prot_id.getSearchEngine()));
391 OpenMS::hash_combine(seed, OpenMS::hash_int(prot_id.getHits().size()));
392 }
393
394 // Hash identifying properties from peptide identifications
395 for (const auto& pep_id : run.getPeptideIdentifications())
396 {
397 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(pep_id.getIdentifier()));
398 OpenMS::hash_combine(seed, OpenMS::fnv1a_hash_string(pep_id.getScoreType()));
399 OpenMS::hash_combine(seed, OpenMS::hash_int(pep_id.getHits().size()));
400 OpenMS::hash_combine(seed, OpenMS::hash_float(pep_id.getSignificanceThreshold()));
401 }
402
403 // Hash identifying properties from spectra
404 for (const auto& spectrum : run.getMSExperiment().getSpectra())
405 {
406 OpenMS::hash_combine(seed, OpenMS::hash_float(spectrum.getRT()));
407 OpenMS::hash_combine(seed, OpenMS::hash_int(spectrum.getMSLevel()));
408 OpenMS::hash_combine(seed, OpenMS::hash_int(spectrum.size()));
409 }
410
411 return seed;
412 }
413 };
414} // namespace std
Class for storing MS run data with peptide and protein identifications.
Definition AnnotatedMSRun.h:39
const PeptideIdentificationList & getPeptideIdentifications() const
Get all peptide identifications for all spectra (const version)
std::vector< ProteinIdentification > protein_ids_
Definition AnnotatedMSRun.h:354
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:104
AnnotatedMSRun(MSExperiment &&experiment)
Move constructor for efficiently loading a MSExperiment without a deep copy.
Definition AnnotatedMSRun.h:54
AnnotatedMSRun::PairIterator< std::vector< MSSpectrum >::const_iterator, PeptideIdentificationList::const_iterator > ConstIterator
Definition AnnotatedMSRun.h:346
SpectrumIdRef operator[](size_t idx)
Access a spectrum and its associated peptide identification.
Definition AnnotatedMSRun.h:228
bool operator!=(const AnnotatedMSRun &rhs) const
Inequality operator.
Definition AnnotatedMSRun.h:77
bool operator==(const AnnotatedMSRun &rhs) const
Equality operator.
Definition AnnotatedMSRun.h:69
void setProteinIdentifications(std::vector< ProteinIdentification > &&ids)
Set the protein identifications (move version)
Definition AnnotatedMSRun.h:113
AnnotatedMSRun()=default
Default constructor.
AnnotatedMSRun(const AnnotatedMSRun &)=default
Copy constructor.
auto end()
Get an iterator to the end of the data.
Definition AnnotatedMSRun.h:200
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:345
std::vector< ProteinIdentification > & getProteinIdentifications()
Get the protein identification.
Definition AnnotatedMSRun.h:86
MSExperiment data
Definition AnnotatedMSRun.h:355
void setMSExperiment(MSExperiment &&experiment)
Set the MSExperiment.
auto cbegin() const
Get a const iterator to the beginning of the data.
Definition AnnotatedMSRun.h:170
AnnotatedMSRun(AnnotatedMSRun &&)=default
Move constructor.
PeptideIdentificationList peptide_ids_
Definition AnnotatedMSRun.h:353
ConstSpectrumIdRef operator[](size_t idx) const
Access a spectrum and its associated peptide identification (const version)
Definition AnnotatedMSRun.h:250
auto begin() const
Get a const iterator to the beginning of the data.
Definition AnnotatedMSRun.h:190
auto begin()
Get an iterator to the beginning of the data.
Definition AnnotatedMSRun.h:180
auto end() const
Get a const iterator to the end of the data.
Definition AnnotatedMSRun.h:209
const MSExperiment & getMSExperiment() const
Get the MSExperiment (const version)
std::pair< MSSpectrum &, PeptideIdentification & > SpectrumIdRef
Definition AnnotatedMSRun.h:41
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:218
std::pair< const MSSpectrum &, const PeptideIdentification & > ConstSpectrumIdRef
Definition AnnotatedMSRun.h:42
AnnotatedMSRun & operator=(const AnnotatedMSRun &)=default
const std::vector< ProteinIdentification > & getProteinIdentifications() const
Get the protein identification (const version)
Definition AnnotatedMSRun.h:95
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:275
friend bool operator!=(const PairIterator &a, const PairIterator &b)
Inequality operator.
Definition AnnotatedMSRun.h:335
PairIterator operator++(int)
Post-increment operator.
Definition AnnotatedMSRun.h:302
std::forward_iterator_tag iterator_category
Definition AnnotatedMSRun.h:276
auto operator*()
Dereference operator.
Definition AnnotatedMSRun.h:313
friend bool operator==(const PairIterator &a, const PairIterator &b)
Equality operator.
Definition AnnotatedMSRun.h:324
PairIterator(T1 ptr1, T2 ptr2)
Constructor.
Definition AnnotatedMSRun.h:284
T1 m_ptr1
Definition AnnotatedMSRun.h:341
std::ptrdiff_t difference_type
Definition AnnotatedMSRun.h:277
T2 m_ptr2
Definition AnnotatedMSRun.h:342
PairIterator & operator++()
Pre-increment operator.
Definition AnnotatedMSRun.h:291
std::size_t operator()(const OpenMS::AnnotatedMSRun &run) const noexcept
Definition AnnotatedMSRun.h:372