| OpenMS 2.5.0

SpectrumLookup.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 $
33 // --------------------------------------------------------------------------
34
35 #pragma once
36
37 #include <OpenMS/CONCEPT/LogStream.h>
38 #include <OpenMS/KERNEL/MSSpectrum.h>
39
40 #include <boost/regex.hpp>
41
42 namespace OpenMS
43 {
67 class OPENMS_DLLAPI SpectrumLookup
68 {
69 public:
70
72 static const String& default_scan_regexp;
73
75 std::vector<boost::regex> reference_formats;
76
78 double rt_tolerance;
79
81 SpectrumLookup();
82
84 virtual ~SpectrumLookup();
85
87 bool empty() const;
88
102 template <typename SpectrumContainer>
103 void readSpectra(const SpectrumContainer& spectra,
104 const String& scan_regexp = default_scan_regexp)
105 {
106 rts_.clear();
107 ids_.clear();
108 scans_.clear();
109 n_spectra_ = spectra.size();
110 setScanRegExp_(scan_regexp);
111 for (Size i = 0; i < n_spectra_; ++i)
112 {
113 const MSSpectrum& spectrum = spectra[i];
114 String native_id = spectrum.getNativeID();
115 Int scan_no = -1;
116 if (!scan_regexp.empty())
117 {
118 scan_no = extractScanNumber(native_id, scan_regexp_, true);
119 if (scan_no < 0)
120 {
121 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;
122 }
123 }
124 addEntry_(i, spectrum.getRT(), scan_no, native_id);
125 }
126 }
127
139 Size findByRT(double rt) const;
140
150 Size findByNativeID(const String& native_id) const;
151
162 Size findByIndex(Size index, bool count_from_one = false) const;
163
173 Size findByScanNumber(Size scan_number) const;
174
187 Size findByReference(const String& spectrum_ref) const;
188
198 void addReferenceFormat(const String& regexp);
199
211 static Int extractScanNumber(const String& native_id,
212 const boost::regex& scan_regexp,
213 bool no_error = false);
214
215 static Int extractScanNumber(const String& native_id,
216 const String& native_id_type_accession);
217
218 protected:
219
221 static const String& regexp_names_;
222
223 Size n_spectra_;
224
225 boost::regex scan_regexp_;
226
227 std::vector<String> regexp_name_list_;
228
229 std::map<double, Size> rts_;
230 std::map<String, Size> ids_;
231 std::map<Size, Size> scans_;
232
241 void addEntry_(Size index, double rt, Int scan_number,
242 const String& native_id);
243
255 Size findByRegExpMatch_(const String& spectrum_ref, const String& regexp,
256 const boost::smatch& match) const;
257
263 void setScanRegExp_(const String& scan_regexp);
264
265 private:
266
268 SpectrumLookup(const SpectrumLookup&);
269
271 SpectrumLookup& operator=(const SpectrumLookup&);
272
273 };
274
275 } //namespace OpenMS
276
OpenMS::SpectrumLookup::scan_regexp_
boost::regex scan_regexp_
Regular expression to extract scan numbers.
Definition: SpectrumLookup.h:225
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::MSSpectrum::getRT
double getRT() const
OpenMS::SpectrumLookup::rts_
std::map< double, Size > rts_
Mapping: RT -> spectrum index.
Definition: SpectrumLookup.h:229
OpenMS::SpectrumLookup::rt_tolerance
double rt_tolerance
Tolerance for look-up by retention time.
Definition: SpectrumLookup.h:78
OpenMS::SpectrumSettings::getNativeID
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software. ...
OpenMS::SpectrumLookup
Helper class for looking up spectra based on different attributes.
Definition: SpectrumLookup.h:67
OpenMS::SpectrumLookup::default_scan_regexp
static const String & default_scan_regexp
Default regular expression for extracting scan numbers from spectrum native IDs.
Definition: SpectrumLookup.h:72
OpenMS::SpectrumLookup::scans_
std::map< Size, Size > scans_
Mapping: scan number -> spectrum index.
Definition: SpectrumLookup.h:231
OPENMS_LOG_WARN
#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:460
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
OpenMS::SpectrumLookup::readSpectra
void readSpectra(const SpectrumContainer &spectra, const String &scan_regexp=default_scan_regexp)
Read and index spectra for later look-up.
Definition: SpectrumLookup.h:103
OpenMS::SpectrumLookup::reference_formats
std::vector< boost::regex > reference_formats
Possible formats of spectrum references, defined as regular expressions.
Definition: SpectrumLookup.h:75
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::SpectrumLookup::n_spectra_
Size n_spectra_
Number of spectra.
Definition: SpectrumLookup.h:223
LogStream.h
MSSpectrum.h
OpenMS::SpectrumLookup::regexp_name_list_
std::vector< String > regexp_name_list_
Named groups in vector format.
Definition: SpectrumLookup.h:227
int
OpenMS::SpectrumLookup::ids_
std::map< String, Size > ids_
Mapping: native ID -> spectrum index.
Definition: SpectrumLookup.h:230
OpenMS::SpectrumLookup::regexp_names_
static const String & regexp_names_
Named groups recognized in regular expression.
Definition: SpectrumLookup.h:221
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127