OpenMS
Loading...
Searching...
No Matches
SpectrumLookup.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: Hendrik Weisser $
6// $Authors: Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14
15#include <boost/regex.hpp>
16
17namespace OpenMS
18{
56 class OPENMS_DLLAPI SpectrumLookup
57 {
58 public:
59
61 static const std::string& default_scan_regexp;
62
64 std::vector<boost::regex> reference_formats;
65
68
71
73 virtual ~SpectrumLookup();
74
76 bool empty() const;
77
91 template <typename SpectrumContainer>
92 void readSpectra(const SpectrumContainer& spectra,
93 const std::string& scan_regexp = default_scan_regexp)
94 {
95 rts_.clear();
96 ids_.clear();
97 scans_.clear();
98 n_spectra_ = spectra.size();
99 setScanRegExp_(scan_regexp);
100 for (Size i = 0; i < n_spectra_; ++i)
101 {
102 const MSSpectrum& spectrum = spectra[i];
103 const std::string& native_id = spectrum.getNativeID();
104 Int scan_no = -1;
105 if (!scan_regexp.empty())
106 {
107 scan_no = extractScanNumber(native_id, scan_regexp_, true);
108 if (scan_no < 0)
109 {
110 OPENMS_LOG_WARN << "Warning: Could not extract scan number from spectrum native ID '" + native_id + "' using regular expression '" + scan_regexp + "'. Look-up by scan number may not work properly." << std::endl;
111 }
112 }
113 addEntry_(i, spectrum.getRT(), scan_no, native_id);
114 }
115 }
116
128 Size findByRT(double rt) const;
129
139 Size findByNativeID(const std::string& native_id) const;
140
151 Size findByIndex(Size index, bool count_from_one = false) const;
152
162 Size findByScanNumber(Size scan_number) const;
163
176 Size findByReference(const std::string& spectrum_ref) const;
177
187 void addReferenceFormat(const std::string& regexp);
188
203 static Int extractScanNumber(const std::string& native_id,
204 const boost::regex& scan_regexp,
205 bool no_error = false);
206
218 static Int extractScanNumber(const std::string& native_id,
219 const std::string& native_id_type_accession);
220
231 static std::string getRegExFromNativeID(const std::string& native_id);
232
243 static bool isNativeID(const std::string& id);
244
245 protected:
246
248 static const std::string& regexp_names_;
249
251
252 boost::regex scan_regexp_;
253
254 std::vector<std::string> regexp_name_list_;
255
256 std::map<double, Size> rts_;
257 std::map<std::string, Size> ids_;
258 std::map<Size, Size> scans_;
259
268 void addEntry_(Size index, double rt, Int scan_number,
269 const std::string& native_id);
270
282 Size findByRegExpMatch_(const std::string& spectrum_ref, const std::string& regexp,
283 const boost::smatch& match) const;
284
290 void setScanRegExp_(const std::string& scan_regexp);
291
292 private:
293
296
299
300 };
301
302} //namespace OpenMS
303
#define OPENMS_LOG_WARN
Macro for warnings.
Definition LogStream.h:583
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
double getRT() const
Helper class for looking up spectra based on different attributes.
Definition SpectrumLookup.h:57
virtual ~SpectrumLookup()
Destructor.
std::map< Size, Size > scans_
Mapping: scan number -> spectrum index.
Definition SpectrumLookup.h:258
std::vector< std::string > regexp_name_list_
Named groups in vector format.
Definition SpectrumLookup.h:254
void setScanRegExp_(const std::string &scan_regexp)
Set the regular expression for extracting scan numbers from spectrum native IDs.
void readSpectra(const SpectrumContainer &spectra, const std::string &scan_regexp=default_scan_regexp)
Read and index spectra for later look-up.
Definition SpectrumLookup.h:92
static const std::string & default_scan_regexp
Default regular expression for extracting scan numbers from spectrum native IDs.
Definition SpectrumLookup.h:61
static Int extractScanNumber(const std::string &native_id, const std::string &native_id_type_accession)
Extract the scan number from the native ID using a CV accession.
Size findByIndex(Size index, bool count_from_one=false) const
Look up spectrum by index (position in the vector of spectra).
std::map< double, Size > rts_
Mapping: RT -> spectrum index.
Definition SpectrumLookup.h:256
static std::string getRegExFromNativeID(const std::string &native_id)
Determine the RegEx string to extract scan/index number from native IDs. Can be used for extractScanN...
Size n_spectra_
Number of spectra.
Definition SpectrumLookup.h:250
bool empty() const
Check if any spectra were set.
boost::regex scan_regexp_
Regular expression to extract scan numbers.
Definition SpectrumLookup.h:252
SpectrumLookup & operator=(const SpectrumLookup &)
Assignment operator (not implemented).
Size findByRT(double rt) const
Look up spectrum by retention time (RT).
Size findByRegExpMatch_(const std::string &spectrum_ref, const std::string &regexp, const boost::smatch &match) const
Look up spectrum by regular expression match.
SpectrumLookup(const SpectrumLookup &)
Copy constructor (not implemented)
Size findByScanNumber(Size scan_number) const
Look up spectrum by scan number (extracted from the native ID).
static bool isNativeID(const std::string &id)
Simple prefix check if a spectrum identifier id is a nativeID from a vendor file.
static Int extractScanNumber(const std::string &native_id, const boost::regex &scan_regexp, bool no_error=false)
Extract the scan number from the native ID of a spectrum.
Size findByNativeID(const std::string &native_id) const
Look up spectrum by native ID.
Size findByReference(const std::string &spectrum_ref) const
Look up spectrum by reference.
std::vector< boost::regex > reference_formats
Possible formats of spectrum references, defined as regular expressions.
Definition SpectrumLookup.h:64
SpectrumLookup()
Constructor.
double rt_tolerance
Tolerance for look-up by retention time.
Definition SpectrumLookup.h:67
void addReferenceFormat(const std::string &regexp)
Register a possible format for a spectrum reference.
std::map< std::string, Size > ids_
Mapping: native ID -> spectrum index.
Definition SpectrumLookup.h:257
void addEntry_(Size index, double rt, Int scan_number, const std::string &native_id)
Add a look-up entry for a spectrum.
static const std::string & regexp_names_
Named groups recognized in regular expression.
Definition SpectrumLookup.h:248
const std::string & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software.
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19