OpenMS  2.4.0
MzMLSqliteHandler.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-2018.
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: Hannes Roest $
32 // $Authors: Hannes Roest
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 
41 
42 // forward declarations
43 struct sqlite3;
44 struct sqlite3_stmt;
45 
46 namespace OpenMS
47 {
48  class ProgressLogger;
49 
50  namespace Internal
51  {
52 
77  class OPENMS_DLLAPI MzMLSqliteHandler
78  {
79 
80 public:
81 
87  MzMLSqliteHandler(String filename);
88 
96 
103  void readExperiment(MSExperiment & exp, bool meta_only = false) const;
104 
112  void readSpectra(std::vector<MSSpectrum> & exp, const std::vector<int> & indices, bool meta_only = false) const;
113 
121  void readChromatograms(std::vector<MSChromatogram> & exp, const std::vector<int> & indices, bool meta_only = false) const;
122 
128  Size getNrSpectra() const;
129 
135  Size getNrChromatograms() const;
136 
145  void setConfig(bool write_full_meta, bool use_lossy_compression, double linear_abs_mass_acc, int sql_batch_size = 500)
146  {
147  write_full_meta_ = write_full_meta;
148  use_lossy_compression_ = use_lossy_compression;
149  linear_abs_mass_acc_ = linear_abs_mass_acc;
150  sql_batch_size_ = sql_batch_size;
151  }
152 
161  std::vector<size_t> getSpectraIndicesbyRT(double RT, double deltaRT, const std::vector<int> & indices) const;
162 
163 protected:
164 
165  void populateChromatogramsWithData_(sqlite3 *db, std::vector<MSChromatogram>& chromatograms) const;
166 
167  void populateChromatogramsWithData_(sqlite3 *db, std::vector<MSChromatogram>& chromatograms, const std::vector<int> & indices) const;
168 
169  void populateSpectraWithData_(sqlite3 *db, std::vector<MSSpectrum>& spectra) const;
170 
171  void populateSpectraWithData_(sqlite3 *db, std::vector<MSSpectrum>& spectra, const std::vector<int> & indices) const;
172 
173  void prepareChroms_(sqlite3 *db, std::vector<MSChromatogram>& chromatograms) const;
174 
175  void prepareSpectra_(sqlite3 *db, std::vector<MSSpectrum>& spectra) const;
177 
178 public:
179 
187 
193  void writeExperiment(const MSExperiment & exp);
194 
201  void createTables();
202  void createIndices();
203 
209  void writeSpectra(const std::vector<MSSpectrum>& spectra);
210 
216  void writeChromatograms(const std::vector<MSChromatogram>& chroms);
217 
226  void writeRunLevelInformation(const MSExperiment & exp, bool write_full_meta, int run_id);
227 
228 protected:
229 
230  void executeBlobBind_(sqlite3 *db, String& prepare_statement, std::vector<String>& data);
231 
232  void executeSql_(sqlite3 *db, const std::stringstream& statement);
233 
234  sqlite3* openDB() const;
236 
238 
239  /*
240  * These are spectra and chromatogram ids that are global for a specific
241  * database file. Keeping track of them allows us to append spectra and
242  * chromatograms multiple times to a database.
243  *
244  * However, currently they are initialized to zero when opening a new
245  * file, so appending to an existing file won't work.
246  */
250 
255  };
256 
257 
258  } // namespace Internal
259 } // namespace OpenMS
260 
261 
void setConfig(bool write_full_meta, bool use_lossy_compression, double linear_abs_mass_acc, int sql_batch_size=500)
Set file configuration.
Definition: MzMLSqliteHandler.h:145
A more convenient string class.
Definition: String.h:57
Int run_id_
Definition: MzMLSqliteHandler.h:249
bool use_lossy_compression_
Definition: MzMLSqliteHandler.h:251
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
String filename_
Definition: MzMLSqliteHandler.h:237
Int chrom_id_
Definition: MzMLSqliteHandler.h:248
double write_full_meta_
Definition: MzMLSqliteHandler.h:253
Int spec_id_
Definition: MzMLSqliteHandler.h:247
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
double linear_abs_mass_acc_
Definition: MzMLSqliteHandler.h:252
int sql_batch_size_
Definition: MzMLSqliteHandler.h:254
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Sqlite handler for storing spectra and chromatograms in sqMass format.
Definition: MzMLSqliteHandler.h:77
int Int
Signed integer type.
Definition: Types.h:102