ProteoWizard
Classes | Functions | Variables
MSDataFileTest.cpp File Reference
#include "MSDataFile.hpp"
#include "Diff.hpp"
#include "IO.hpp"
#include "SpectrumListBase.hpp"
#include "ChromatogramListBase.hpp"
#include "examples.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Filesystem.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/copy.hpp>

Go to the source code of this file.

Classes

class  TestReader
 

Functions

void hackInMemoryMSData (MSData &msd)
 
void validateWriteRead (const MSDataFile::WriteConfig &writeConfig, const DiffConfig diffConfig)
 
void test ()
 
void demo ()
 
void testReader ()
 
void testSHA1 ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
string filenameBase_ = "temp.MSDataFileTest"
 
const char rawHeader_ []
 

Function Documentation

void hackInMemoryMSData ( MSData msd)

Definition at line 51 of file MSDataFileTest.cpp.

References pwiz::msdata::Run::chromatogramListPtr, pwiz::msdata::MSData::fileDescription, pwiz::msdata::MSData::run, pwiz::msdata::ChromatogramListBase::setDataProcessingPtr(), pwiz::msdata::SpectrumListBase::setDataProcessingPtr(), pwiz::msdata::MSData::softwarePtrs, pwiz::msdata::FileDescription::sourceFilePtrs, and pwiz::msdata::Run::spectrumListPtr.

Referenced by validateWriteRead().

52 {
53  // remove metadata ptrs appended on read
54  vector<SourceFilePtr>& sfs = msd.fileDescription.sourceFilePtrs;
55  if (!sfs.empty()) sfs.erase(sfs.end()-1);
56  vector<SoftwarePtr>& sws = msd.softwarePtrs;
57  if (!sws.empty()) sws.erase(sws.end()-1);
58 
59  // remove current DataProcessing created on read
60  SpectrumListBase* sl = dynamic_cast<SpectrumListBase*>(msd.run.spectrumListPtr.get());
61  ChromatogramListBase* cl = dynamic_cast<ChromatogramListBase*>(msd.run.chromatogramListPtr.get());
64 }
common functionality for base SpectrumList implementations
common functionality for base ChromatogramList implementations
std::vector< SourceFilePtr > sourceFilePtrs
list and descriptions of the source files this mzML document was generated or derived from...
Definition: MSData.hpp:89
virtual void setDataProcessingPtr(DataProcessingPtr dp)
set DataProcessing
ChromatogramListPtr chromatogramListPtr
all chromatograms for this run.
Definition: MSData.hpp:831
virtual void setDataProcessingPtr(DataProcessingPtr dp)
set DataProcessing
boost::shared_ptr< DataProcessing > DataProcessingPtr
Definition: MSData.hpp:287
FileDescription fileDescription
information pertaining to the entire mzML file (i.e. not specific to any part of the data set) is sto...
Definition: MSData.hpp:863
Run run
a run in mzML should correspond to a single, consecutive and coherent set of scans on an instrument...
Definition: MSData.hpp:887
SpectrumListPtr spectrumListPtr
all mass spectra and the acquisitions underlying them are described and attached here. Subsidiary data arrays are also both described and attached here.
Definition: MSData.hpp:828
std::vector< SoftwarePtr > softwarePtrs
list and descriptions of software used to acquire and/or process the data in this mzML file...
Definition: MSData.hpp:872
void validateWriteRead ( const MSDataFile::WriteConfig writeConfig,
const DiffConfig  diffConfig 
)

Definition at line 67 of file MSDataFileTest.cpp.

References diff(), filename1, filenameBase_, pwiz::msdata::MSDataFile::WriteConfig::format, Format_mzXML, hackInMemoryMSData(), pwiz::identdata::examples::initializeTiny(), os_, pwiz::msdata::MSData::run, pwiz::msdata::Run::spectrumListPtr, unit_assert, pwiz::identdata::IO::write(), and pwiz::msdata::MSDataFile::write().

Referenced by test().

69 {
70  if (os_) *os_ << "validateWriteRead()\n " << writeConfig << endl;
71 
72  string filename1 = filenameBase_ + ".1";
73  string filename2 = filenameBase_ + ".2";
74  string filename3 = filenameBase_ + ".3";
75 
76  {
77  // create MSData object in memory
78  MSData tiny;
80 
81  if (writeConfig.format == MSDataFile::Format_mzXML)
82  {
83  // remove s22 since it is not written to mzXML
84  static_cast<SpectrumListSimple&>(*tiny.run.spectrumListPtr).spectra.pop_back();
85  }
86 
87  // write to file #1 (static)
88  MSDataFile::write(tiny, filename1, writeConfig);
89 
90  // simulate CLI garbage collect behavior, wherein delayed deletes stress
91  // memory and file handle usage
92  {
93  std::vector< boost::shared_ptr< MSDataFile > > msds;
94  for (int i=0;i<100;i++)
95  {
96  boost::shared_ptr<MSDataFile> msd1(new MSDataFile(filename1));
97  msds.push_back(msd1);
98  hackInMemoryMSData(*msd1);
99  Diff<MSData, DiffConfig> diff(tiny, *msd1, diffConfig);
100  }
101  }
102 
103  // read back into an MSDataFile object
104  MSDataFile msd1(filename1);
105  hackInMemoryMSData(msd1);
106 
107  // compare
108  Diff<MSData, DiffConfig> diff(tiny, msd1, diffConfig);
109  if (diff && os_) *os_ << diff << endl;
110  unit_assert(!diff);
111 
112  // write to file #2 (member)
113  msd1.write(filename2, writeConfig);
114 
115  // read back into another MSDataFile object
116  MSDataFile msd2(filename2);
117  hackInMemoryMSData(msd2);
118 
119  // compare
120  diff(tiny, msd2);
121  if (diff && os_) *os_ << diff << endl;
122  unit_assert(!diff);
123 
124  // now give the gzip read a workout
125  bio::filtering_istream tinyGZ(bio::gzip_compressor() | bio::file_descriptor_source(filename1));
126  bio::copy(tinyGZ, bio::file_descriptor_sink(filename1+".gz", ios::out|ios::binary));
127 
128  MSDataFile msd3(filename1+".gz");
129  hackInMemoryMSData(msd3);
130 
131  // compare
132  diff(tiny, msd3);
133  if (diff && os_) *os_ << diff << endl;
134  unit_assert(!diff);
135 
136  // test writing to a stream
137  ostringstream oss;
138  msd1.write(oss, writeConfig);
139  string ossStr = oss.str();
140  ofstream ofs(filename3.c_str());
141  ofs.write(ossStr.c_str(), ossStr.length());
142  ofs.close();
143 
144  // read back into another MSDataFile object
145  MSDataFile msd4(filename3);
146  hackInMemoryMSData(msd4);
147 
148  // compare
149  diff(tiny, msd4);
150  if (diff && os_) *os_ << diff << endl;
151  unit_assert(!diff);
152  }
153 
154  // remove temp files
155  boost::filesystem::remove(filename1);
156  boost::filesystem::remove(filename2);
157  boost::filesystem::remove(filename1 + ".gz");
158  boost::filesystem::remove(filename3);
159 }
Calculate diffs of objects in a ProteoWizard data model hierarchy.
Definition: diff_std.hpp:142
string filename1
void diff(const string &filename1, const string &filename2)
Run run
a run in mzML should correspond to a single, consecutive and coherent set of scans on an instrument...
Definition: MSData.hpp:887
PWIZ_API_DECL void write(minimxml::XMLWriter &writer, const CV &cv)
Format_mzXML
Definition: MSDataFile.hpp:49
void hackInMemoryMSData(MSData &msd)
MSData object plus file I/O.
Definition: MSDataFile.hpp:40
SpectrumListPtr spectrumListPtr
all mass spectra and the acquisitions underlying them are described and attached here. Subsidiary data arrays are also both described and attached here.
Definition: MSData.hpp:828
PWIZ_API_DECL void initializeTiny(IdentData &mzid)
string filenameBase_
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition: MSData.hpp:850
ostream * os_
Simple writeable in-memory implementation of SpectrumList.
Definition: MSData.hpp:717
#define unit_assert(x)
Definition: unit.hpp:82
void test ( )

Definition at line 161 of file MSDataFileTest.cpp.

References pwiz::msdata::MSDataFile::WriteConfig::binaryDataEncoderConfig, pwiz::msdata::MSDataFile::WriteConfig::format, Format_mzXML, pwiz::msdata::DiffConfig::ignoreChromatograms, pwiz::msdata::DiffConfig::ignoreMetadata, pwiz::msdata::MSDataFile::WriteConfig::indexed, pwiz::msdata::BinaryDataEncoder::Config::precision, and validateWriteRead().

Referenced by main().

162 {
163  MSDataFile::WriteConfig writeConfig;
164  DiffConfig diffConfig;
165 
166  // mzML 64-bit, full diff
167  validateWriteRead(writeConfig, diffConfig);
168 
169  writeConfig.indexed = false;
170  validateWriteRead(writeConfig, diffConfig); // no index
171  writeConfig.indexed = true;
172 
173  // mzML 32-bit, full diff
174  writeConfig.binaryDataEncoderConfig.precision = BinaryDataEncoder::Precision_32;
175  validateWriteRead(writeConfig, diffConfig);
176 
177  // mzXML 32-bit, diff ignoring metadata and chromatograms
178  writeConfig.format = MSDataFile::Format_mzXML;
179  diffConfig.ignoreMetadata = true;
180  diffConfig.ignoreChromatograms = true;
181  validateWriteRead(writeConfig, diffConfig);
182 
183  // mzXML 64-bit, diff ignoring metadata and chromatograms
184  writeConfig.binaryDataEncoderConfig.precision = BinaryDataEncoder::Precision_64;
185  validateWriteRead(writeConfig, diffConfig);
186 
187  writeConfig.indexed = false;
188  validateWriteRead(writeConfig, diffConfig); // no index
189  writeConfig.indexed = true;
190 }
bool ignoreMetadata
ignore all file level metadata, and most scan level metadata, i.e.
Definition: Diff.hpp:214
configuration for write()
Definition: MSDataFile.hpp:52
Format_mzXML
Definition: MSDataFile.hpp:49
configuration struct for diffing MSData types
Definition: Diff.hpp:205
void validateWriteRead(const MSDataFile::WriteConfig &writeConfig, const DiffConfig diffConfig)
BinaryDataEncoder::Config binaryDataEncoderConfig
Definition: MSDataFile.hpp:55
void demo ( )

Definition at line 193 of file MSDataFileTest.cpp.

References pwiz::msdata::MSDataFile::WriteConfig::binaryDataEncoderConfig, filenameBase_, pwiz::msdata::MSDataFile::WriteConfig::format, Format_mzXML, Format_Text, pwiz::identdata::examples::initializeTiny(), pwiz::msdata::BinaryDataEncoder::Config::precision, and pwiz::identdata::IO::write().

194 {
195  MSData tiny;
197 
199  MSDataFile::write(tiny, filenameBase_ + ".64.mzML", config);
200 
201  config.binaryDataEncoderConfig.precision = BinaryDataEncoder::Precision_32;
202  MSDataFile::write(tiny, filenameBase_ + ".32.mzML", config);
203 
205  MSDataFile::write(tiny, filenameBase_ + ".txt", config);
206 
208  MSDataFile::write(tiny, filenameBase_ + ".32.mzXML", config);
209 
210  config.binaryDataEncoderConfig.precision = BinaryDataEncoder::Precision_64;
211  MSDataFile::write(tiny, filenameBase_ + ".64.mzXML", config);
212 }
Format_Text
configuration for write()
Definition: MSDataFile.hpp:52
PWIZ_API_DECL void write(minimxml::XMLWriter &writer, const CV &cv)
Format_mzXML
Definition: MSDataFile.hpp:49
PWIZ_API_DECL void initializeTiny(IdentData &mzid)
string filenameBase_
BinaryDataEncoder::Config binaryDataEncoderConfig
Definition: MSDataFile.hpp:55
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition: MSData.hpp:850
void testReader ( )

Definition at line 260 of file MSDataFileTest.cpp.

References TestReader::count, filenameBase_, os_, rawHeader_, and unit_assert.

261 {
262  // create a file
263  string filename = filenameBase_ + ".RAW";
264  ofstream os(filename.c_str());
265  os.write(rawHeader_, 18);
266  os.close();
267 
268  // open the file with our Reader
269  TestReader reader;
270  MSDataFile msd(filename, &reader);
271 
272  // verify that our reader got called properly
273  unit_assert(reader.count == 2);
274 
275  // remove temp file
276  boost::filesystem::remove(filename);
277 
278  if (os_) *os_ << endl;
279 }
MSData object plus file I/O.
Definition: MSDataFile.hpp:40
string filenameBase_
const char rawHeader_[]
ostream * os_
#define unit_assert(x)
Definition: unit.hpp:82
void testSHA1 ( )

Definition at line 282 of file MSDataFileTest.cpp.

References pwiz::msdata::MSData::fileDescription, filenameBase_, pwiz::identdata::examples::initializeTiny(), MS_SHA_1, os_, pwiz::msdata::FileDescription::sourceFilePtrs, unit_assert, and pwiz::identdata::IO::write().

Referenced by main().

283 {
284  if (os_) *os_ << "testSHA1()\n";
285 
286  // write out a test file
287 
288  string filename = filenameBase_ + ".SHA1Test";
289  MSData tiny;
291  MSDataFile::write(tiny, filename);
292 
293  {
294  // read in without SHA-1 calculation
295  MSDataFile msd(filename);
296 
297  if (os_)
298  {
299  *os_ << "no SHA-1:\n";
301  IO::write(writer, *msd.fileDescription.sourceFilePtrs.back());
302  }
303 
304  unit_assert(!msd.fileDescription.sourceFilePtrs.empty());
305  unit_assert(!msd.fileDescription.sourceFilePtrs.back()->hasCVParam(MS_SHA_1));
306 
307  // read in with SHA-1 calculation
308 
309  MSDataFile msd_sha1(filename, 0, true);
310 
311  if (os_)
312  {
313  *os_ << "with SHA-1:\n";
315  IO::write(writer, *msd_sha1.fileDescription.sourceFilePtrs.back());
316  }
317 
318  unit_assert(!msd_sha1.fileDescription.sourceFilePtrs.empty());
319  unit_assert(msd_sha1.fileDescription.sourceFilePtrs.back()->hasCVParam(MS_SHA_1));
320  }
321 
322  // clean up
323 
324  boost::filesystem::remove(filename);
325  if (os_) *os_ << endl;
326 }
The XMLWriter class provides simple, tag-level XML syntax writing.
Definition: XMLWriter.hpp:47
MS_SHA_1
SHA-1: SHA-1 (Secure Hash Algorithm-1) is a cryptographic hash function designed by the National Secu...
Definition: cv.hpp:2156
PWIZ_API_DECL void write(minimxml::XMLWriter &writer, const CV &cv)
MSData object plus file I/O.
Definition: MSDataFile.hpp:40
PWIZ_API_DECL void initializeTiny(IdentData &mzid)
string filenameBase_
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition: MSData.hpp:850
ostream * os_
#define unit_assert(x)
Definition: unit.hpp:82
int main ( int  argc,
char *  argv[] 
)

Definition at line 329 of file MSDataFileTest.cpp.

References os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, pwiz::util::testReader(), and testSHA1().

330 {
331  TEST_PROLOG(argc, argv)
332 
333  try
334  {
335  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
336  test();
337  //demo();
338  testReader();
339  testSHA1();
340  }
341  catch (exception& e)
342  {
343  TEST_FAILED(e.what())
344  }
345  catch (...)
346  {
347  TEST_FAILED("Caught unknown exception.")
348  }
349 
351 }
#define TEST_EPILOG
Definition: unit.hpp:166
void testSHA1()
#define TEST_FAILED(x)
Definition: unit.hpp:160
void test()
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:158
ostream * os_
void testReader()

Variable Documentation

ostream* os_ = 0

Definition at line 45 of file MSDataFileTest.cpp.

Referenced by main(), testReader(), testSHA1(), and validateWriteRead().

string filenameBase_ = "temp.MSDataFileTest"

Definition at line 48 of file MSDataFileTest.cpp.

Referenced by demo(), testReader(), testSHA1(), and validateWriteRead().

const char rawHeader_[]
Initial value:
= {'\x01', '\xA1',
'F', '\0', 'i', '\0', 'n', '\0', 'n', '\0',
'i', '\0', 'g', '\0', 'a', '\0', 'n', '\0'}

Definition at line 215 of file MSDataFileTest.cpp.

Referenced by TestReader::identify(), and testReader().