OpenMS
CachedMzMLHandler.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-2023.
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 
38 
40 #include <OpenMS/CONCEPT/Types.h>
42 #include <OpenMS/CONCEPT/Macros.h>
43 
46 
47 #include <fstream>
48 
49 #define CACHED_MZML_FILE_IDENTIFIER 8094
50 
51 namespace OpenMS
52 {
53 
54 namespace Internal
55 {
56 
66  class OPENMS_DLLAPI CachedMzMLHandler :
67  public ProgressLogger
68  {
69  typedef int IntType;
70  typedef double DoubleType;
71 
72 public:
73 
74  typedef PeakMap MapType;
77 
78  // using double precision to store all data (has to agree with type of BinaryDataArrayPtr)
79  typedef double DatumSingleton;
80 
81  typedef std::vector<DatumSingleton> Datavector;
82 
88 
90  ~CachedMzMLHandler() override;
91 
95 
99 
101  void writeMemdump(const MapType& exp, const String& out) const;
102 
104  void writeMetadata(MapType exp, const String& out_meta, bool addCacheMetaValue=false);
105 
107  void writeMetadata_x(const MapType& exp, const String& out_meta, bool addCacheMetaValue=false);
108 
110  void readMemdump(MapType& exp_reading, const String& filename) const;
112 
117  void createMemdumpIndex(const String& filename);
118 
120  const std::vector<std::streampos>& getSpectraIndex() const;
121 
123  const std::vector<std::streampos>& getChromatogramIndex() const;
125 
129 
142  std::ifstream& ifs,
143  int& ms_level,
144  double& rt)
145  {
146  std::vector<OpenSwath::BinaryDataArrayPtr> data = readSpectrumFast(ifs, ms_level, rt);
147  data1 = data[0];
148  data2 = data[1];
149  }
150 
160  static std::vector<OpenSwath::BinaryDataArrayPtr> readSpectrumFast(std::ifstream& ifs, int& ms_level, double& rt);
161 
171  OpenSwath::BinaryDataArrayPtr& data2, std::ifstream& ifs)
172  {
173  std::vector<OpenSwath::BinaryDataArrayPtr> data = readChromatogramFast(ifs);
174  data1 = data[0];
175  data2 = data[1];
176  }
177 
185  static std::vector<OpenSwath::BinaryDataArrayPtr> readChromatogramFast(std::ifstream& ifs);
187 
196  static void readSpectrum(SpectrumType& spectrum, std::ifstream& ifs);
197 
206  static void readChromatogram(ChromatogramType& chromatogram, std::ifstream& ifs);
207 
208 protected:
209 
211  void writeSpectrum_(const SpectrumType& spectrum, std::ofstream& ofs) const;
212 
214  void writeChromatogram_(const ChromatogramType& chromatogram, std::ofstream& ofs) const;
215 
217  static inline void readDataFast_(std::ifstream& ifs, std::vector<OpenSwath::BinaryDataArrayPtr>& data, const Size& data_size,
218  const Size& nr_float_arrays);
219 
221  std::vector<std::streampos> spectra_index_;
222  std::vector<std::streampos> chrom_index_;
223 
224  };
225 }
226 }
227 
An class that uses on-disk caching to read and write spectra and chromatograms.
Definition: CachedMzMLHandler.h:68
static void readDataFast_(std::ifstream &ifs, std::vector< OpenSwath::BinaryDataArrayPtr > &data, const Size &data_size, const Size &nr_float_arrays)
helper method for fast reading of spectra and chromatograms
void writeSpectrum_(const SpectrumType &spectrum, std::ofstream &ofs) const
write a single spectrum to filestream
std::vector< DatumSingleton > Datavector
Definition: CachedMzMLHandler.h:81
const std::vector< std::streampos > & getChromatogramIndex() const
Access to a constant copy of the binary chromatogram index.
MSChromatogram ChromatogramType
Definition: CachedMzMLHandler.h:76
void writeChromatogram_(const ChromatogramType &chromatogram, std::ofstream &ofs) const
write a single chromatogram to filestream
static std::vector< OpenSwath::BinaryDataArrayPtr > readSpectrumFast(std::ifstream &ifs, int &ms_level, double &rt)
Fast access to a spectrum.
static void readSpectrum(SpectrumType &spectrum, std::ifstream &ifs)
Read a single spectrum directly into an OpenMS MSSpectrum (assuming file is already at the correct po...
double DoubleType
Definition: CachedMzMLHandler.h:70
MSSpectrum SpectrumType
Definition: CachedMzMLHandler.h:75
std::vector< std::streampos > chrom_index_
Definition: CachedMzMLHandler.h:222
void readMemdump(MapType &exp_reading, const String &filename) const
Read all spectra from a dump from the disk.
static void readSpectrumFast(OpenSwath::BinaryDataArrayPtr &data1, OpenSwath::BinaryDataArrayPtr &data2, std::ifstream &ifs, int &ms_level, double &rt)
fast access to a spectrum (a direct copy of the data into the provided arrays)
Definition: CachedMzMLHandler.h:140
std::vector< std::streampos > spectra_index_
Members.
Definition: CachedMzMLHandler.h:221
int IntType
Definition: CachedMzMLHandler.h:69
CachedMzMLHandler()
Default constructor.
void writeMetadata_x(const MapType &exp, const String &out_meta, bool addCacheMetaValue=false)
Write only the meta data of an MSExperiment.
void createMemdumpIndex(const String &filename)
Create an index on the location of all the spectra and chromatograms.
static void readChromatogram(ChromatogramType &chromatogram, std::ifstream &ifs)
Read a single chromatogram directly into an OpenMS MSChromatogram (assuming file is already at the co...
const std::vector< std::streampos > & getSpectraIndex() const
Access to a constant copy of the binary spectra index.
static void readChromatogramFast(OpenSwath::BinaryDataArrayPtr &data1, OpenSwath::BinaryDataArrayPtr &data2, std::ifstream &ifs)
Fast access to a chromatogram.
Definition: CachedMzMLHandler.h:170
PeakMap MapType
Definition: CachedMzMLHandler.h:74
CachedMzMLHandler & operator=(const CachedMzMLHandler &rhs)
Assignment operator.
void writeMetadata(MapType exp, const String &out_meta, bool addCacheMetaValue=false)
Write only the meta data of an MSExperiment.
static std::vector< OpenSwath::BinaryDataArrayPtr > readChromatogramFast(std::ifstream &ifs)
Fast access to a chromatogram.
double DatumSingleton
Definition: CachedMzMLHandler.h:79
~CachedMzMLHandler() override
Default destructor.
void writeMemdump(const MapType &exp, const String &out) const
Write complete spectra as a dump to the disk.
The representation of a chromatogram.
Definition: MSChromatogram.h:57
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:72
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
boost::shared_ptr< BinaryDataArray > BinaryDataArrayPtr
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/DataStructures.h:79