Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
MSDataWritingConsumer.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-2015.
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 #ifndef OPENMS_FORMAT_DATAACCESS_MSDATAWRITINGCONSUMER_H
36 #define OPENMS_FORMAT_DATAACCESS_MSDATAWRITINGCONSUMER_H
37 
39 
40 #include <OpenMS/FORMAT/MzMLFile.h>
43 
44 #include <vector>
45 #include <string>
46 #include <fstream>
47 #include <boost/shared_ptr.hpp>
48 
49 namespace OpenMS
50 {
89  class OPENMS_DLLAPI MSDataWritingConsumer :
90  public Internal::MzMLHandler< MSExperiment<> >,
91  public Interfaces::IMSDataConsumer< MSExperiment<> >
92  {
93 
94  public:
98 
104  explicit MSDataWritingConsumer(String filename) :
105  Internal::MzMLHandler<MapType>(MapType(), filename, MzMLFile().getVersion(), ProgressLogger()),
106  started_writing_(false),
107  writing_spectra_(false),
108  writing_chromatograms_(false),
109  spectra_written_(0),
110  chromatograms_written_(0),
111  spectra_expected_(0),
112  chromatograms_expected_(0),
113  add_dataprocessing_(false)
114  {
115  validator_ = new Internal::MzMLValidator(this->mapping_, this->cv_);
116 
117  // open file in binary mode to avoid any line ending conversions
118  ofs_.open(filename.c_str(), std::ios::out | std::ios::binary);
119  ofs_.precision(writtenDigits(double()));
120  }
121 
124  {
125  doCleanup_();
126  }
127 
129 
130 
138  {
139  settings_ = exp;
140  }
141 
152  virtual void setExpectedSize(Size expectedSpectra, Size expectedChromatograms)
153  {
154  spectra_expected_ = expectedSpectra;
155  chromatograms_expected_ = expectedChromatograms;
156  }
157 
166  virtual void consumeSpectrum(SpectrumType & s)
167  {
168  if (writing_chromatograms_)
169  {
170  throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__,
171  "Cannot write spectra after writing chromatograms.");
172  }
173 
174  // Process the spectrum
175  SpectrumType scpy = s;
176  processSpectrum_(scpy);
177 
178  // Add dataprocessing if required
179  if (add_dataprocessing_)
180  {
181  scpy.getDataProcessing().push_back(additional_dataprocessing_);
182  }
183 
184  if (!started_writing_)
185  {
186  // This is the first data to be written -> start writing the header
187  // We also need to modify the map and add this dummy spectrum in
188  // order to write the header correctly
189  MapType dummy;
190  dummy = settings_;
191  dummy.addSpectrum(scpy);
192 
193  //--------------------------------------------------------------------
194  //header
195  //--------------------------------------------------------------------
196  Internal::MzMLHandler<MapType>::writeHeader_(ofs_, dummy, dps_, *validator_);
197  started_writing_ = true;
198  }
199  if (!writing_spectra_)
200  {
201  // This is the first spectrum, thus write the spectrumList header
202  ofs_ << "\t\t<spectrumList count=\"" << spectra_expected_ << "\" defaultDataProcessingRef=\"dp_sp_0\">\n";
203  writing_spectra_ = true;
204  }
205  bool renew_native_ids = false;
206  // TODO writeSpectrum assumes that dps_ has at least one value -> assert
207  // this here ...
209  spectra_written_++, *validator_, renew_native_ids, dps_);
210  }
211 
220  virtual void consumeChromatogram(ChromatogramType & c)
221  {
222  // make sure to close an open List tag
223  if (writing_spectra_)
224  {
225  ofs_ << "\t\t</spectrumList>\n";
226  }
227 
228  // Create copy and add dataprocessing if required
229  ChromatogramType ccpy = c;
230  processChromatogram_(ccpy);
231 
232  if (add_dataprocessing_)
233  {
234  ccpy.getDataProcessing().push_back(additional_dataprocessing_);
235  }
236 
237  if (!started_writing_)
238  {
239  // this is the first data to be written -> start writing the header
240  // We also need to modify the map and add this dummy chromatogram in
241  // order to write the header correctly
242  MapType dummy;
243  dummy = settings_;
244  dummy.addChromatogram(ccpy);
245 
246  //--------------------------------------------------------------------
247  //header (fill also dps_ variable)
248  //--------------------------------------------------------------------
249  Internal::MzMLHandler<MapType>::writeHeader_(ofs_, dummy, dps_, *validator_);
250  started_writing_ = true;
251  }
252  if (!writing_chromatograms_)
253  {
254  ofs_ << "\t\t<chromatogramList count=\"" << chromatograms_expected_ << "\" defaultDataProcessingRef=\"dp_sp_0\">\n";
255  writing_chromatograms_ = true;
256  writing_spectra_ = false;
257  }
259  chromatograms_written_++, *validator_);
260  }
262 
272  {
273  additional_dataprocessing_ = d;
274  add_dataprocessing_ = true;
275  }
276 
280  virtual Size getNrSpectraWritten() {return spectra_written_;}
284  virtual Size getNrChromatogramsWritten() {return chromatograms_written_;}
285 
286  private:
287 
289 
290 
295  virtual void processSpectrum_(SpectrumType & s) = 0;
296 
302  virtual void processChromatogram_(ChromatogramType & c) = 0;
304 
310  virtual void doCleanup_()
311  {
312  //--------------------------------------------------------------------------------------------
313  //cleanup
314  //--------------------------------------------------------------------------------------------
315  // make sure to close an open List tag
316  if (writing_spectra_)
317  {
318  ofs_ << "\t\t</spectrumList>\n";
319  }
320  else if (writing_chromatograms_)
321  {
322  ofs_ << "\t\t</chromatogramList>\n";
323  }
324 
325  // Only write the footer if we actually did start writing ...
326  if (started_writing_)
327  Internal::MzMLHandlerHelper::writeFooter_(ofs_, options_, spectra_offsets, chromatograms_offsets);
328 
329  delete validator_;
330  ofs_.close();
331  }
332 
333  protected:
334 
336  std::ofstream ofs_;
337 
354 
357 
361  std::vector<std::vector<DataProcessing> > dps_;
364  };
365 
375  class OPENMS_DLLAPI PlainMSDataWritingConsumer :
376  public MSDataWritingConsumer
377  {
380 
381  public:
382 
383  explicit PlainMSDataWritingConsumer(String filename) : MSDataWritingConsumer(filename) {}
384  };
385 
393  class OPENMS_DLLAPI NoopMSDataWritingConsumer :
394  public MSDataWritingConsumer
395  {
396  public:
397 
398  explicit NoopMSDataWritingConsumer(String filename) : MSDataWritingConsumer(filename) {}
400  void consumeSpectrum(SpectrumType & /* s */) {}
402 
403  private:
404 
405  void doCleanup_() {}
408  };
409 
410 
411 } //end namespace OpenMS
412 
413 #endif
Description of the applied preprocessing steps.
Definition: DataProcessing.h:51
const std::vector< DataProcessing > & getDataProcessing() const
returns a const reference to the description of the applied processing
virtual Size getNrSpectraWritten()
Return the number of spectra written.
Definition: MSDataWritingConsumer.h:280
A more convenient string class.
Definition: String.h:57
virtual void setExperimentalSettings(const ExperimentalSettings &exp)
Set experimental settings for the whole file.
Definition: MSDataWritingConsumer.h:137
bool writing_spectra_
Stores whether we are currently writing spectra.
Definition: MSDataWritingConsumer.h:341
Consumer class that writes MS data to disk using the mzML format.
Definition: MSDataWritingConsumer.h:375
void processChromatogram_(MapType::ChromatogramType &)
Process a chromatogram before storing to disk.
Definition: MSDataWritingConsumer.h:379
Semantically validates MzXML files.
Definition: MzMLValidator.h:52
Consumer class that perform no operation.
Definition: MSDataWritingConsumer.h:393
std::ofstream ofs_
File stream (to write mzML)
Definition: MSDataWritingConsumer.h:336
Int writtenDigits(const FloatingPointType &=FloatingPointType())
Number of digits commonly used for writing a floating point type (a.k.a. precision). Specializations are defined for float, double, long double.
Definition: Types.h:288
Size spectra_expected_
Number of spectra expected.
Definition: MSDataWritingConsumer.h:349
const double c
ExperimentalSettings settings_
Experimental settings to use for the whole file.
Definition: MSDataWritingConsumer.h:359
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void addChromatogram(const MSChromatogram< ChromatogramPeakType > &chromatogram)
adds a chromatogram to the list
Definition: MSExperiment.h:782
bool writing_chromatograms_
Stores whether we are currently writing chromatograms.
Definition: MSDataWritingConsumer.h:343
void writeHeader_(std::ostream &os, const MapType &exp, std::vector< std::vector< DataProcessing > > &dps, Internal::MzMLValidator &validator)
Definition: MzMLHandler.h:3958
const std::vector< DataProcessing > & getDataProcessing() const
returns a const reference to the description of the applied processing
bool add_dataprocessing_
Whether to add dataprocessing term to the data before writing.
Definition: MSDataWritingConsumer.h:353
File adapter for MzML files.
Definition: MzMLFile.h:55
virtual void addDataProcessing(DataProcessing d)
Optionally add a data processing method to each chromatogram and spectrum.
Definition: MSDataWritingConsumer.h:271
XML handler for MzMLFile.
Definition: MzMLHandler.h:108
void setExperimentalSettings(const ExperimentalSettings &)
Set experimental settings for the whole file.
Definition: MSDataWritingConsumer.h:399
A method or algorithm argument contains illegal values.
Definition: Exception.h:634
PlainMSDataWritingConsumer(String filename)
Definition: MSDataWritingConsumer.h:383
void processSpectrum_(MapType::SpectrumType &)
Process a spectrum before storing to disk.
Definition: MSDataWritingConsumer.h:378
std::vector< std::vector< DataProcessing > > dps_
Vector of data processing objects -> will be filled by writeHeader_.
Definition: MSDataWritingConsumer.h:361
MapType::SpectrumType SpectrumType
Definition: MSDataWritingConsumer.h:96
void consumeSpectrum(SpectrumType &)
Consume a spectrum.
Definition: MSDataWritingConsumer.h:400
void consumeChromatogram(ChromatogramType &)
Consume a chromatogram.
Definition: MSDataWritingConsumer.h:401
static void writeFooter_(std::ostream &os, const PeakFileOptions &options_, std::vector< std::pair< std::string, long > > &spectra_offsets, std::vector< std::pair< std::string, long > > &chromatograms_offsets)
Write the mzML footer the appropriate compression term given the PeakFileOptions and the NumpressConf...
virtual ~MSDataWritingConsumer()
Destructor.
Definition: MSDataWritingConsumer.h:123
void processSpectrum_(MapType::SpectrumType &)
Process a spectrum before storing to disk.
Definition: MSDataWritingConsumer.h:406
MapType::ChromatogramType ChromatogramType
Definition: MSDataWritingConsumer.h:97
Internal::MzMLValidator * validator_
Validator that knows about CV terms.
Definition: MSDataWritingConsumer.h:356
MSExperiment MapType
Definition: MSDataWritingConsumer.h:95
bool started_writing_
Stores whether we have already started writing any data.
Definition: MSDataWritingConsumer.h:339
void addSpectrum(const MSSpectrum< PeakT > &spectrum)
adds a spectra to the list
Definition: MSExperiment.h:758
void doCleanup_()
Cleanup function called by the destructor.
Definition: MSDataWritingConsumer.h:405
void processChromatogram_(MapType::ChromatogramType &)
Process a chromatogram before storing to disk.
Definition: MSDataWritingConsumer.h:407
Consumer class that writes MS data to disk using the mzML format.
Definition: MSDataWritingConsumer.h:89
The interface of a consumer of spectra and chromatograms.
Definition: IMSDataConsumer.h:68
Size chromatograms_expected_
Number of chromatograms expected.
Definition: MSDataWritingConsumer.h:351
virtual void doCleanup_()
Cleanup function called by the destructor.
Definition: MSDataWritingConsumer.h:310
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
NoopMSDataWritingConsumer(String filename)
Definition: MSDataWritingConsumer.h:398
virtual Size getNrChromatogramsWritten()
Return the number of chromatograms written.
Definition: MSDataWritingConsumer.h:284
void writeSpectrum_(std::ostream &os, const SpectrumType &spec, Size s, Internal::MzMLValidator &validator, bool renew_native_ids, std::vector< std::vector< DataProcessing > > &dps)
Definition: MzMLHandler.h:4868
virtual void consumeChromatogram(ChromatogramType &c)
Consume a chromatogram.
Definition: MSDataWritingConsumer.h:220
MSDataWritingConsumer(String filename)
Constructor.
Definition: MSDataWritingConsumer.h:104
void writeChromatogram_(std::ostream &os, const ChromatogramType &chromatogram, Size c, Internal::MzMLValidator &validator)
Definition: MzMLHandler.h:5201
Size chromatograms_written_
Number of chromatograms written.
Definition: MSDataWritingConsumer.h:347
Description of the experimental settings.
Definition: ExperimentalSettings.h:59
virtual void setExpectedSize(Size expectedSpectra, Size expectedChromatograms)
Set expected size of spectra and chromatograms to be written.
Definition: MSDataWritingConsumer.h:152
Size spectra_written_
Number of spectra written.
Definition: MSDataWritingConsumer.h:345
virtual void consumeSpectrum(SpectrumType &s)
Consume a spectrum.
Definition: MSDataWritingConsumer.h:166
DataProcessing additional_dataprocessing_
The dataprocessing to be added to each spectrum/chromatogram.
Definition: MSDataWritingConsumer.h:363

OpenMS / TOPP release 2.0.0 Documentation generated on Fri May 29 2015 17:20:26 using doxygen 1.8.9.1