OpenMS
SpectrumLookup.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- 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 
13 
14 #include <boost/regex.hpp>
15 
16 namespace OpenMS
17 {
41  class OPENMS_DLLAPI SpectrumLookup
42  {
43  public:
44 
46  static const String& default_scan_regexp;
47 
49  std::vector<boost::regex> reference_formats;
50 
52  double rt_tolerance;
53 
56 
58  virtual ~SpectrumLookup();
59 
61  bool empty() const;
62 
76  template <typename SpectrumContainer>
77  void readSpectra(const SpectrumContainer& spectra,
78  const String& scan_regexp = default_scan_regexp)
79  {
80  rts_.clear();
81  ids_.clear();
82  scans_.clear();
83  n_spectra_ = spectra.size();
84  setScanRegExp_(scan_regexp);
85  for (Size i = 0; i < n_spectra_; ++i)
86  {
87  const MSSpectrum& spectrum = spectra[i];
88  const String& native_id = spectrum.getNativeID();
89  Int scan_no = -1;
90  if (!scan_regexp.empty())
91  {
92  scan_no = extractScanNumber(native_id, scan_regexp_, true);
93  if (scan_no < 0)
94  {
95  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;
96  }
97  }
98  addEntry_(i, spectrum.getRT(), scan_no, native_id);
99  }
100  }
101 
113  Size findByRT(double rt) const;
114 
124  Size findByNativeID(const String& native_id) const;
125 
136  Size findByIndex(Size index, bool count_from_one = false) const;
137 
147  Size findByScanNumber(Size scan_number) const;
148 
161  Size findByReference(const String& spectrum_ref) const;
162 
172  void addReferenceFormat(const String& regexp);
173 
185  static Int extractScanNumber(const String& native_id,
186  const boost::regex& scan_regexp,
187  bool no_error = false);
188 
189  static Int extractScanNumber(const String& native_id,
190  const String& native_id_type_accession);
196  static std::string getRegExFromNativeID(const String& id);
197 
201  static bool isNativeID(const String& id);
202 
203  protected:
204 
206  static const String& regexp_names_;
207 
209 
210  boost::regex scan_regexp_;
211 
212  std::vector<String> regexp_name_list_;
213 
214  std::map<double, Size> rts_;
215  std::map<String, Size> ids_;
216  std::map<Size, Size> scans_;
217 
226  void addEntry_(Size index, double rt, Int scan_number,
227  const String& native_id);
228 
240  Size findByRegExpMatch_(const String& spectrum_ref, const String& regexp,
241  const boost::smatch& match) const;
242 
248  void setScanRegExp_(const String& scan_regexp);
249 
250  private:
251 
254 
257 
258  };
259 
260 } //namespace OpenMS
261 
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:444
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:42
virtual ~SpectrumLookup()
Destructor.
std::map< Size, Size > scans_
Mapping: scan number -> spectrum index.
Definition: SpectrumLookup.h:216
void setScanRegExp_(const String &scan_regexp)
Set the regular expression for extracting scan numbers from spectrum native IDs.
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:214
Size n_spectra_
Number of spectra.
Definition: SpectrumLookup.h:208
bool empty() const
Check if any spectra were set.
boost::regex scan_regexp_
Regular expression to extract scan numbers.
Definition: SpectrumLookup.h:210
Size findByRT(double rt) const
Look up spectrum by retention time (RT).
static const String & default_scan_regexp
Default regular expression for extracting scan numbers from spectrum native IDs.
Definition: SpectrumLookup.h:46
SpectrumLookup(const SpectrumLookup &)
Copy constructor (not implemented)
Size findByScanNumber(Size scan_number) const
Look up spectrum by scan number (extracted from the native ID).
void addReferenceFormat(const String &regexp)
Register a possible format for a spectrum reference.
Size findByReference(const 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:49
Size findByRegExpMatch_(const String &spectrum_ref, const String &regexp, const boost::smatch &match) const
Look up spectrum by regular expression match.
SpectrumLookup()
Constructor.
Size findByNativeID(const String &native_id) const
Look up spectrum by native ID.
double rt_tolerance
Tolerance for look-up by retention time.
Definition: SpectrumLookup.h:52
void addEntry_(Size index, double rt, Int scan_number, const String &native_id)
Add a look-up entry for a spectrum.
std::vector< String > regexp_name_list_
Named groups in vector format.
Definition: SpectrumLookup.h:212
static Int extractScanNumber(const String &native_id, const boost::regex &scan_regexp, bool no_error=false)
Extract the scan number from the native ID of a spectrum.
static Int extractScanNumber(const String &native_id, const String &native_id_type_accession)
static std::string getRegExFromNativeID(const String &id)
Determine the RegEx string to extract scan/index number from native IDs. Can be used for extractScanN...
SpectrumLookup & operator=(const SpectrumLookup &)
Assignment operator (not implemented).
static bool isNativeID(const String &id)
Simple prefix check if a spectrum identifier id is a nativeID from a vendor file.
void readSpectra(const SpectrumContainer &spectra, const String &scan_regexp=default_scan_regexp)
Read and index spectra for later look-up.
Definition: SpectrumLookup.h:77
std::map< String, Size > ids_
Mapping: native ID -> spectrum index.
Definition: SpectrumLookup.h:215
static const String & regexp_names_
Named groups recognized in regular expression.
Definition: SpectrumLookup.h:206
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software.
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:76
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22