ProteoWizard
IOTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: IOTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 Spielberg Family Center for Applied Proteomics
8 // Cedars-Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #include "IO.hpp"
25 #include "Diff.hpp"
26 #include "References.hpp"
29 
30 using namespace pwiz::util;
31 using namespace pwiz::minimxml;
32 using namespace pwiz::cv;
33 using namespace pwiz::data;
34 using namespace pwiz::msdata;
35 using boost::iostreams::stream_offset;
36 
37 
38 ostream* os_ = 0;
39 
40 
41 template <typename object_type>
42 void testObject(const object_type& a)
43 {
44  if (os_) *os_ << "testObject(): " << typeid(a).name() << endl;
45 
46  // write 'a' out to a stream
47 
48  ostringstream oss;
49  XMLWriter writer(oss);
50  IO::write(writer, a);
51  if (os_) *os_ << oss.str() << endl;
52 
53  // read 'b' in from stream
54 
55  object_type b;
56  istringstream iss(oss.str());
57  IO::read(iss, b);
58 
59  // compare 'a' and 'b'
60 
62  if (diff && os_) *os_ << "diff:\n" << diff << endl;
63  unit_assert(!diff);
64 }
65 
66 
67 template <typename object_type>
68 void testObjectWithMSData(const object_type& a, const MSData& msd)
69 {
70  if (os_) *os_ << "testObject(): " << typeid(a).name() << endl;
71 
72  // write 'a' out to a stream
73 
74  ostringstream oss;
75  XMLWriter writer(oss);
76  IO::write(writer, a, msd);
77  if (os_) *os_ << oss.str() << endl;
78 
79  // read 'b' in from stream
80 
81  object_type b;
82  istringstream iss(oss.str());
83  IO::read(iss, b);
84 
85  // compare 'a' and 'b'
86 
88  if (diff && os_) *os_ << "diff:\n" << diff << endl;
89  unit_assert(!diff);
90 }
91 
92 
94 {
95  if (os_) *os_ << "testObject_SpectrumList(): " << endl;
96 
97  // write 'a' out to a stream
98 
99  ostringstream oss;
100  XMLWriter writer(oss);
101  MSData dummy;
102  IO::write(writer, a, dummy);
103  if (os_) *os_ << oss.str() << endl;
104 
105  // read 'b' in from stream
106 
108  istringstream iss(oss.str());
109  IO::read(iss, b);
110 
111  // compare 'a' and 'b'
112 
114  if (diff && os_) *os_ << "diff:\n" << diff << endl;
115  unit_assert(!diff);
116 
117 
118 }
119 
121 
122 {
123  if (os_) *os_ << "testObject_ChromatogramList(): " << endl;
124 
125  // write 'a' out to a stream
126 
127  ostringstream oss;
128  XMLWriter writer(oss);
129  IO::write(writer, a);
130  if (os_) *os_ << oss.str() << endl;
131 
132  // read 'b' in from stream
133 
135  istringstream iss(oss.str());
136  IO::read(iss, b);
137 
138  // compare 'a' and 'b'
139 
141  if (diff && os_) *os_ << "diff:\n" << diff << endl;
142  unit_assert(!diff);
143 }
144 
145 
146 void testCV()
147 {
148  CV a;
149  a.URI = "abcd";
150  a.id = "efgh";
151  a.fullName = "ijkl";
152  a.version = "mnop";
153 
154  testObject(a);
155 }
156 
157 
159 {
160  UserParam a;
161  a.name = "abcd";
162  a.value = "efgh";
163  a.type = "ijkl";
164  a.units = UO_minute;
165 
166  testObject(a);
167 }
168 
169 
171 {
172  CVParam a(MS_selected_ion_m_z, "810.48", MS_m_z);
173  testObject(a);
174 
175  CVParam b(UO_second, "123.45");
176  testObject(b);
177 }
178 
179 
181 {
182  ParamGroup a("pg");
183  a.userParams.push_back(UserParam("goober", "goo", "peanuts"));
184  a.cvParams.push_back(CVParam(MS_ionization_type, "420"));
185  a.cvParams.push_back(CVParam(MS_selected_ion_m_z, "666", MS_m_z));
186  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pgp")));
187  testObject(a);
188 }
189 
190 
191 template <typename object_type>
193 {
194  object_type a;
195  a.userParams.push_back(UserParam("goober", "goo", "peanuts"));
196  a.cvParams.push_back(CVParam(MS_ionization_type, "420"));
197  a.cvParams.push_back(CVParam(MS_selected_ion_m_z, "666", MS_m_z));
198  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pgp")));
199  testObject(a);
200 }
201 
202 
204 {
205  SourceFile a("id123", "name456", "location789");
206  a.userParams.push_back(UserParam("goober", "goo", "peanuts"));
207  a.cvParams.push_back(CVParam(MS_ionization_type, "420"));
208  a.cvParams.push_back(CVParam(MS_selected_ion_m_z, "666", MS_m_z));
209  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pgp")));
210  testObject(a);
211 }
212 
213 
215 {
216  FileDescription a;
217  a.fileContent.cvParams.push_back(MS_MSn_spectrum);
218 
219  SourceFilePtr sf(new SourceFile("1", "tiny1.RAW", "file://F:/data/Exp01"));
220  sf->cvParams.push_back(MS_Thermo_RAW_file);
221  sf->cvParams.push_back(MS_SHA_1);
222  a.sourceFilePtrs.push_back(sf);
223 
224  Contact contact;
225  contact.cvParams.push_back(CVParam(MS_contact_name, "Darren"));
226  a.contacts.push_back(contact);
227 
228  testObject(a);
229 }
230 
231 
233 {
234  Sample a("id123", "name456");
235  a.userParams.push_back(UserParam("goober", "goo", "peanuts"));
236  a.cvParams.push_back(CVParam(MS_ionization_type, "420"));
237  a.cvParams.push_back(CVParam(MS_selected_ion_m_z, "666", MS_m_z));
238  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pgp")));
239  testObject(a);
240 }
241 
242 
244 {
246  a.userParams.push_back(UserParam("goober", "goo", "peanuts"));
247  a.cvParams.push_back(CVParam(MS_ionization_type, "420"));
248  a.cvParams.push_back(CVParam(MS_selected_ion_m_z, "666", MS_m_z));
249  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pgp")));
250  testObject(a);
251 }
252 
253 
255 {
256  ComponentList a;
257  a.push_back(Component(MS_nanoelectrospray, 1));
258  a.push_back(Component(MS_quadrupole_ion_trap, 2));
259  a.push_back(Component(MS_electron_multiplier, 3));
260  testObject(a);
261 }
262 
263 
265 {
266  Software a;
267  a.id = "goober";
269  a.version = "4.20";
270  testObject(a);
271 }
272 
273 
275 {
277  a.id = "LCQ Deca";
278  a.cvParams.push_back(MS_LCQ_Deca);
279  a.cvParams.push_back(CVParam(MS_instrument_serial_number, 23433));
283  a.softwarePtr = SoftwarePtr(new Software("XCalibur"));
284  testObject(a);
285 }
286 
287 
289 {
291  a.order = 420;
292  a.cvParams.push_back(CVParam(MS_deisotoping, false));
293  a.cvParams.push_back(CVParam(MS_charge_deconvolution, false));
294  a.cvParams.push_back(CVParam(MS_peak_picking, true));
295  a.softwarePtr = SoftwarePtr(new Software("pwiz"));
296  testObject(a);
297 }
298 
299 
301 {
302  DataProcessing a;
303 
304  a.id = "msdata processing";
305 
306  ProcessingMethod pm1, pm2;
307 
308  pm1.order = 420;
309  pm1.cvParams.push_back(CVParam(MS_deisotoping, false));
310  pm1.cvParams.push_back(CVParam(MS_charge_deconvolution, false));
311  pm1.cvParams.push_back(CVParam(MS_peak_picking, true));
312  pm1.softwarePtr = SoftwarePtr(new Software("msdata"));
313 
314  pm2.order = 421;
315  pm2.userParams.push_back(UserParam("testing"));
316 
317  a.processingMethods.push_back(pm1);
318  a.processingMethods.push_back(pm2);
319 
320  testObject(a);
321 }
322 
323 
325 {
326  ScanSettings a;
327 
328  a.id = "as1";
329 
330  Target t1, t2;
331 
332  t1.set(MS_selected_ion_m_z, 200);
333  t2.userParams.push_back(UserParam("testing"));
334 
335  a.targets.push_back(t1);
336  a.targets.push_back(t2);
337 
338  a.sourceFilePtrs.push_back(SourceFilePtr(new SourceFile("sf1")));
339  a.sourceFilePtrs.push_back(SourceFilePtr(new SourceFile("sf2")));
340 
341  testObject(a);
342 }
343 
344 
346 {
347  Precursor a;
348 
349  a.spectrumID = "scan=19";
353  a.selectedIons.resize(2);
354  a.selectedIons[0].set(MS_selected_ion_m_z, 445.34, MS_m_z);
355  a.selectedIons[1].set(MS_charge_state, 2);
358 
359  testObject(a);
360 
361  // TODO: fix this to test mzML 1.0 precursors;
362  // (requires fixing the framework to support testing different schema versions)
363 }
364 
365 
367 {
368  Product a;
369 
373 
374  testObject(a);
375 }
376 
377 
378 void testScan()
379 {
380  Scan a;
381 
383  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("CommonMS1SpectrumParams")));
384  a.cvParams.push_back(CVParam(MS_scan_start_time, 5.890500, UO_minute));
385  a.cvParams.push_back(CVParam(MS_filter_string, "+ c NSI Full ms [ 400.00-1800.00]"));
386  a.scanWindows.push_back(ScanWindow(400.0, 1800.0, MS_m_z));
387 
388  MSData dummy;
389  testObjectWithMSData(a, dummy);
390 }
391 
392 
394 {
395  ScanList a;
396  a.cvParams.push_back(MS_sum_of_spectra);
397 
398  Scan a1;
399  a1.cvParams.push_back(MS_reflectron_on);
400 
401  Scan a2;
402  a1.cvParams.push_back(MS_reflectron_off);
403 
404  a.scans.push_back(a1);
405  a.scans.push_back(a2);
406 
407  MSData dummy;
408  testObjectWithMSData(a, dummy);
409 }
410 
411 
413 {
414  if (os_) *os_ << "testBinaryDataArray():\n";
415 
416  BinaryDataArray a;
417  for (int i=0; i<10; i++) a.data.push_back(i);
419 
420  // write 'a' out to a stream
421 
422  ostringstream oss;
423  XMLWriter writer(oss);
424  IO::write(writer, a, config);
425  if (os_) *os_ << oss.str() << endl;
426 
427  // read 'b' in from stream
428 
429  BinaryDataArray b;
430  istringstream iss(oss.str());
431  IO::read(iss, b);
432 
433  // compare 'a' and 'b'
434 
436  if (diff && os_) *os_ << "diff:\n" << diff << endl;
437  unit_assert(!diff);
438 }
439 
440 
442 {
444 
445  config.precision = BinaryDataEncoder::Precision_32;
446  config.byteOrder = BinaryDataEncoder::ByteOrder_LittleEndian;
447  testBinaryDataArray(config);
448 
449  config.precision = BinaryDataEncoder::Precision_64;
450  config.byteOrder = BinaryDataEncoder::ByteOrder_LittleEndian;
451  testBinaryDataArray(config);
452 
453  //config.precision = BinaryDataEncoder::Precision_64;
454  //config.compression = BinaryDataEncoder::Compression_Zlib;
455  //testBinaryDataArray(config);
456 }
457 
458 
459 const char* bdaWithExternalMetadata = "\
460 <binaryDataArray encodedLength=\"160\" arrayLength=\"15\"> \
461  <referenceableParamGroupRef ref=\"mz_params\"/> \
462  <binary>AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBAAAAAAAAAIkAAAAAAAAAkQAAAAAAAACZAAAAAAAAAKEAAAAAAAAAqQAAAAAAAACxA</binary> \
463 </binaryDataArray>";
464 
465 
467 {
468  // instantiate an MSData object with the binary array metadata held in a ParamGroup
469 
470  MSData msd;
471  ParamGroupPtr pg(new ParamGroup);
472  pg->id = "mz_params";
473  pg->cvParams.push_back(MS_m_z_array);
474  pg->cvParams.push_back(MS_64_bit_float);
475  pg->cvParams.push_back(MS_no_compression);
476  msd.paramGroupPtrs.push_back(pg);
477 
478  istringstream is(bdaWithExternalMetadata);
479  BinaryDataArray bda;
480 
481  // test read with MSData reference
482 
483  IO::read(is, bda, &msd);
484 
485  unit_assert(bda.data.size() == 15);
486  for (size_t i=0; i<15; i++)
487  unit_assert(bda.data[i] == i);
488 }
489 
490 
492 {
493  if (os_) *os_ << "testSpectrum():\n";
494 
495  Spectrum a;
496 
497  a.index = 123;
498  a.id = "goo";
499  a.defaultArrayLength = 666;
501  a.sourceFilePtr = SourceFilePtr(new SourceFile("sf"));
503  for (size_t i=0; i<a.defaultArrayLength; i++)
504  a.binaryDataArrayPtrs.back()->data.push_back(i);
505  a.binaryDataArrayPtrs.back()->set(MS_m_z_array);
507  for (size_t i=0; i<a.defaultArrayLength; i++)
508  a.binaryDataArrayPtrs.back()->data.push_back(i*2);
510  a.cvParams.push_back(MS_reflectron_on);
511  a.cvParams.push_back(MS_MSn_spectrum);
512 
513  a.precursors.push_back(Precursor());
514  a.precursors.back().spectrumID = "19";
515  a.precursors.back().selectedIons.resize(1);
516  a.precursors.back().selectedIons[0].set(MS_selected_ion_m_z, 445.34, MS_m_z);
517  a.precursors.back().selectedIons[0].set(MS_charge_state, 2);
518  a.precursors.back().activation.set(MS_collision_induced_dissociation);
519  a.precursors.back().activation.set(MS_collision_energy, 35.00, UO_electronvolt);
520 
521  a.products.push_back(Product());
522  a.products.back().isolationWindow.set(MS_ionization_type, "420");
523 
524  a.scanList.scans.push_back(Scan());
525  Scan& scan = a.scanList.scans.back();
526  scan.set(MS_scan_start_time, 4.20);
527  scan.set(MS_filter_string, "doobie");
528 
529  a.scanList.scans.push_back(Scan());
530  Scan& scan2 = a.scanList.scans.back();
531  scan2.set(MS_scan_start_time, 4.21);
532  scan2.set(MS_filter_string, "doo");
533 
534  // write 'a' out to a stream
535 
536  ostringstream oss;
537  XMLWriter writer(oss);
538  MSData dummy;
539  IO::write(writer, a, dummy);
540  if (os_) *os_ << oss.str() << endl;
541 
542  // read 'b' in from stream
543 
544  Spectrum b;
545  istringstream iss(oss.str());
546  IO::read(iss, b, IO::ReadBinaryData);
547  unit_assert(b.sourceFilePosition == 0); // not -1
548 
549  // compare 'a' and 'b'
550 
552  if (diff && os_) *os_ << "diff:\n" << diff << endl;
553  unit_assert(!diff);
554 
555  // test IgnoreBinaryData
556 
557  Spectrum c;
558  iss.seekg(0);
559  IO::read(iss, c); // default = IgnoreBinaryData
560  unit_assert(c.binaryDataArrayPtrs.empty());
561  unit_assert(c.sourceFilePosition == 0); // not -1
562 
563  a.binaryDataArrayPtrs.clear();
564  diff(a, c);
565  unit_assert(!diff);
566 }
567 
568 
570 {
571  if (os_) *os_ << "testChromatogram():\n";
572 
573  Chromatogram a;
574 
575  a.index = 123;
576  a.id = "goo";
577  a.defaultArrayLength = 666;
580  for (size_t i=0; i<a.defaultArrayLength; i++)
581  a.binaryDataArrayPtrs.back()->data.push_back(i);
582  a.binaryDataArrayPtrs.back()->set(MS_time_array);
584  for (size_t i=0; i<a.defaultArrayLength; i++)
585  a.binaryDataArrayPtrs.back()->data.push_back(i*2);
587  a.cvParams.push_back(MS_total_ion_current_chromatogram); // TODO: fix when CV has appropriate terms
588 
589  // write 'a' out to a stream
590 
591  ostringstream oss;
592  XMLWriter writer(oss);
593  IO::write(writer, a);
594  if (os_) *os_ << oss.str() << endl;
595 
596  // read 'b' in from stream
597 
598  Chromatogram b;
599  istringstream iss(oss.str());
600  IO::read(iss, b, IO::ReadBinaryData);
601  unit_assert(b.sourceFilePosition == 0); // not -1
602 
603  // compare 'a' and 'b'
604 
606  if (diff && os_) *os_ << "diff:\n" << diff << endl;
607  unit_assert(!diff);
608 
609  // test IgnoreBinaryData
610 
611  Chromatogram c;
612  iss.seekg(0);
613  IO::read(iss, c); // default = IgnoreBinaryData
614  unit_assert(c.binaryDataArrayPtrs.empty());
615  unit_assert(c.sourceFilePosition == 0); // not -1
616 
617  a.binaryDataArrayPtrs.clear();
618  diff(a, c);
619  unit_assert(!diff);
620 }
621 
622 
624 {
626 
627  SpectrumPtr spectrum1(new Spectrum);
628  spectrum1->id = "goober";
629  spectrum1->index = 0;
630  spectrum1->defaultArrayLength = 666;
631  spectrum1->userParams.push_back(UserParam("description1"));
632 
633  SpectrumPtr spectrum2(new Spectrum);
634  spectrum2->id = "raisinet";
635  spectrum2->index = 1;
636  spectrum2->defaultArrayLength = 667;
637  spectrum2->userParams.push_back(UserParam("description2"));
638 
639  a.spectra.push_back(spectrum1);
640  a.spectra.push_back(spectrum2);
641  a.dp = DataProcessingPtr(new DataProcessing("dp"));
642 
644 }
645 
646 
648 {
649  if (os_) *os_ << "testSpectrumListWithPositions()\n ";
650 
652 
653  SpectrumPtr spectrum1(new Spectrum);
654  spectrum1->id = "goober";
655  spectrum1->index = 0;
656  spectrum1->defaultArrayLength = 666;
657  spectrum1->userParams.push_back(UserParam("description1"));
658 
659  SpectrumPtr spectrum2(new Spectrum);
660  spectrum2->id = "raisinet";
661  spectrum2->index = 1;
662  spectrum2->defaultArrayLength = 667;
663  spectrum2->userParams.push_back(UserParam("description2"));
664 
665  a.spectra.push_back(spectrum1);
666  a.spectra.push_back(spectrum2);
667 
668  ostringstream oss;
669  XMLWriter writer(oss);
670  vector<stream_offset> positions;
671  MSData dummy;
672  IO::write(writer, a, dummy, BinaryDataEncoder::Config(), &positions);
673 
674  if (os_)
675  {
676  copy(positions.begin(), positions.end(), ostream_iterator<stream_offset>(*os_, " "));
677  *os_ << endl << oss.str() << endl;
678  *os_ << "\n\n";
679  }
680 
681  unit_assert(positions.size() == 2);
682  unit_assert(positions[0] == 27);
683  unit_assert(positions[1] == 179);
684 }
685 
686 
688 {
689  public:
690 
691  virtual Status update(const UpdateMessage& updateMessage)
692  {
693  indices_.push_back(updateMessage.iterationIndex);
694  return Status_Ok;
695  }
696 
697  const vector<size_t>& indices() const {return indices_;}
698 
699  private:
700  vector<size_t> indices_;
701 };
702 
703 
705 {
706  public:
707 
708  virtual Status update(const UpdateMessage& updateMessage)
709  {
710  if (updateMessage.iterationIndex == 5) return Status_Cancel;
711  indices_.push_back(updateMessage.iterationIndex);
712  return Status_Ok;
713  }
714 
715  const vector<size_t>& indices() const {return indices_;}
716 
717  private:
718  vector<size_t> indices_;
719 };
720 
721 
723 {
724  if (os_) *os_ << "testSpectrumListWriteProgress()\n ";
725 
727 
728  for (size_t i=0; i<11; i++)
729  {
730  SpectrumPtr spectrum(new Spectrum);
731  spectrum->id = "goober_" + lexical_cast<string>(i);
732  spectrum->index = i;
733  spectrum->defaultArrayLength = 666;
734  a.spectra.push_back(spectrum);
735  }
736 
737  ostringstream oss;
738  XMLWriter writer(oss);
739 
741  TestIterationListener& listener = *boost::static_pointer_cast<TestIterationListener>(listenerPtr);
742  IterationListenerRegistry registry;
743  registry.addListener(listenerPtr, 3); // callbacks: 0,2,5,8,10
744 
745  MSData dummy;
746  IO::write(writer, a, dummy, BinaryDataEncoder::Config(), 0, &registry);
747 
748  if (os_)
749  {
750  *os_ << "callback indices: ";
751  copy(listener.indices().begin(), listener.indices().end(),
752  ostream_iterator<size_t>(*os_, " "));
753  *os_ << "\n\n";
754  }
755 
756  unit_assert(listener.indices().size() == 5);
757  unit_assert(listener.indices()[0] == 0);
758  unit_assert(listener.indices()[1] == 2);
759  unit_assert(listener.indices()[2] == 5);
760  unit_assert(listener.indices()[3] == 8);
761  unit_assert(listener.indices()[4] == 10);
762 
763  // test #2, this time with cancel at index 6
764 
766  TestIterationListener_WithCancel& cancelListener = *boost::static_pointer_cast<TestIterationListener_WithCancel>(cancelListenerPtr);
767  IterationListenerRegistry registry2;
768  registry2.addListener(cancelListenerPtr, 3); // callbacks: 0,2, cancel at 5
769 
770  ostringstream oss2;
771  XMLWriter writer2(oss2);
772  IO::write(writer2, a, dummy, BinaryDataEncoder::Config(), 0, &registry2);
773 
774  if (os_)
775  {
776  *os_ << "callback indices: ";
777  copy(cancelListener.indices().begin(), cancelListener.indices().end(),
778  ostream_iterator<size_t>(*os_, " "));
779  *os_ << "\n\n";
780  }
781 
782  unit_assert(cancelListener.indices().size() == 2);
783  unit_assert(cancelListener.indices()[0] == 0);
784  unit_assert(cancelListener.indices()[1] == 2);
785 }
786 
787 
789 {
791 
792  ChromatogramPtr chromatogram1(new Chromatogram);
793  chromatogram1->id = "goober";
794  chromatogram1->index = 0;
795  chromatogram1->defaultArrayLength = 666;
796 
797  ChromatogramPtr chromatogram2(new Chromatogram);
798  chromatogram2->id = "raisinet";
799  chromatogram2->index = 1;
800  chromatogram2->defaultArrayLength = 667;
801 
802  a.chromatograms.push_back(chromatogram1);
803  a.chromatograms.push_back(chromatogram2);
804  a.dp = DataProcessingPtr(new DataProcessing("dp"));
805 
807 }
808 
809 
811 {
812  if (os_) *os_ << "testChromatogramListWithPositions()\n ";
813 
815 
816  ChromatogramPtr chromatogram1(new Chromatogram);
817  chromatogram1->id = "goober";
818  chromatogram1->index = 0;
819  chromatogram1->defaultArrayLength = 666;
820 
821  ChromatogramPtr chromatogram2(new Chromatogram);
822  chromatogram2->id = "raisinet";
823  chromatogram2->index = 1;
824  chromatogram2->defaultArrayLength = 667;
825 
826  a.chromatograms.push_back(chromatogram1);
827  a.chromatograms.push_back(chromatogram2);
828 
829  ostringstream oss;
830  XMLWriter writer(oss);
831  vector<stream_offset> positions;
832  IO::write(writer, a, BinaryDataEncoder::Config(), &positions);
833 
834  if (os_)
835  {
836  copy(positions.begin(), positions.end(), ostream_iterator<stream_offset>(*os_, " "));
837  *os_ << endl << oss.str() << endl;
838  *os_ << "\n\n";
839  }
840 
841  unit_assert(positions.size() == 2);
842  unit_assert(positions[0] == 31);
843  unit_assert(positions[1] == 113);
844 }
845 
846 
847 void testRun()
848 {
849  if (os_) *os_ << "testRun():\n";
850 
851  Run a;
852 
853  a.id = "goober";
855  a.samplePtr = SamplePtr(new Sample("sample"));
856  a.startTimeStamp = "20 April 2004 4:20pm";
858 
859  // spectrumList
860 
861  shared_ptr<SpectrumListSimple> spectrumListSimple(new SpectrumListSimple);
862 
863  SpectrumPtr spectrum1(new Spectrum);
864  spectrum1->id = "goober";
865  spectrum1->index = 0;
866  spectrum1->defaultArrayLength = 666;
867  spectrum1->userParams.push_back(UserParam("description1"));
868 
869  SpectrumPtr spectrum2(new Spectrum);
870  spectrum2->id = "raisinet";
871  spectrum2->index = 1;
872  spectrum2->defaultArrayLength = 667;
873  spectrum2->userParams.push_back(UserParam("description2"));
874 
875  spectrumListSimple->spectra.push_back(spectrum1);
876  spectrumListSimple->spectra.push_back(spectrum2);
877 
878  a.spectrumListPtr = spectrumListSimple;
879 
880  // chromatogramList
881 
882  shared_ptr<ChromatogramListSimple> chromatogramListSimple(new ChromatogramListSimple);
883 
884  ChromatogramPtr chromatogram1(new Chromatogram);
885  chromatogram1->id = "goober";
886  chromatogram1->index = 0;
887  chromatogram1->defaultArrayLength = 666;
888 
889  ChromatogramPtr chromatogram2(new Chromatogram);
890  chromatogram2->id = "raisinet";
891  chromatogram2->index = 1;
892  chromatogram2->defaultArrayLength = 667;
893 
894  chromatogramListSimple->chromatograms.push_back(chromatogram1);
895  chromatogramListSimple->chromatograms.push_back(chromatogram2);
896 
897  a.chromatogramListPtr = chromatogramListSimple;
898 
899  // write 'a' out to a stream
900 
901  MSData dummy;
902 
903  ostringstream oss;
904  XMLWriter writer(oss);
905  IO::write(writer, a, dummy);
906  if (os_) *os_ << oss.str() << endl;
907 
908  // read 'b' in from stream, ignoring SpectrumList (default)
909 
910  Run b;
911  istringstream iss(oss.str());
912  IO::read(iss, b, IO::IgnoreSpectrumList); // IO::IgnoreSpectrumList
913 
914  // compare 'a' and 'b'
915 
917  if (diff && os_) *os_ << "diff:\n" << diff << endl;
918  unit_assert(diff);
919  unit_assert(diff.a_b.spectrumListPtr.get());
920  unit_assert(diff.a_b.spectrumListPtr->size() == 1);
921  unit_assert(diff.a_b.spectrumListPtr->spectrum(0)->userParams.size() == 1);
922 
923  // read 'c' in from stream, reading SpectrumList
924 
925  Run c;
926  iss.seekg(0);
927  IO::read(iss, c, IO::ReadSpectrumList);
928 
929  // compare 'a' and 'c'
930 
931  diff(a,c);
932  if (diff && os_) *os_ << "diff:\n" << diff << endl;
933  unit_assert(!diff);
934 
935  // remove SpectrumList and ChromatogramList from a, and compare to b
936 
937  a.spectrumListPtr.reset();
938  a.chromatogramListPtr.reset();
939  diff(a, b);
940  unit_assert(!diff);
941 }
942 
943 
945 {
946  msd.accession = "test accession";
947  msd.id = "test id";
948 
949  // cvList
950 
951  msd.cvs.resize(1);
952  CV& cv = msd.cvs.front();
953  cv.URI = "http://psidev.sourceforge.net/ms/xml/mzdata/psi-ms.2.0.2.obo";
954  cv.id = "MS";
955  cv.fullName = "Proteomics Standards Initiative Mass Spectrometry Ontology";
956  cv.version = "2.0.2";
957 
958  // fileDescription
959 
961  fc.cvParams.push_back(MS_MSn_spectrum);
962  fc.userParams.push_back(UserParam("number of cats", "4"));
963 
964  SourceFilePtr sfp(new SourceFile);
965  sfp->id = "1";
966  sfp->name = "tiny1.RAW";
967  sfp->location = "file://F:/data/Exp01";
968  sfp->cvParams.push_back(MS_Thermo_RAW_file);
969  sfp->cvParams.push_back(CVParam(MS_SHA_1,"71be39fb2700ab2f3c8b2234b91274968b6899b1"));
970  msd.fileDescription.sourceFilePtrs.push_back(sfp);
971 
972  SourceFilePtr sfp_parameters(new SourceFile("sf_parameters", "parameters.par", "file:///C:/settings/"));
973  msd.fileDescription.sourceFilePtrs.push_back(sfp_parameters);
974 
975  msd.fileDescription.contacts.resize(1);
976  Contact& contact = msd.fileDescription.contacts.front();
977  contact.cvParams.push_back(CVParam(MS_contact_name, "William Pennington"));
978  contact.cvParams.push_back(CVParam(MS_contact_address,
979  "Higglesworth University, 12 Higglesworth Avenue, 12045, HI, USA"));
980  contact.cvParams.push_back(CVParam(MS_contact_URL, "http://www.higglesworth.edu/"));
981  contact.cvParams.push_back(CVParam(MS_contact_email, "wpennington@higglesworth.edu"));
982 
983  // paramGroupList
984 
985  ParamGroupPtr pg1(new ParamGroup);
986  pg1->id = "CommonMS1SpectrumParams";
987  pg1->cvParams.push_back(MS_positive_scan);
988  pg1->cvParams.push_back(MS_full_scan);
989  msd.paramGroupPtrs.push_back(pg1);
990 
991  ParamGroupPtr pg2(new ParamGroup);
992  pg2->id = "CommonMS2SpectrumParams";
993  pg2->cvParams.push_back(MS_positive_scan);
994  pg2->cvParams.push_back(MS_full_scan);
995  msd.paramGroupPtrs.push_back(pg2);
996 
997  // sampleList
998 
999  SamplePtr samplePtr(new Sample);
1000  samplePtr->id = "1";
1001  samplePtr->name = "Sample1";
1002  msd.samplePtrs.push_back(samplePtr);
1003 
1004  // instrumentConfigurationList
1005 
1006  InstrumentConfigurationPtr instrumentConfigurationPtr(new InstrumentConfiguration);
1007  instrumentConfigurationPtr->id = "LCQ Deca";
1008  instrumentConfigurationPtr->cvParams.push_back(MS_LCQ_Deca);
1009  instrumentConfigurationPtr->cvParams.push_back(CVParam(MS_instrument_serial_number,"23433"));
1010  instrumentConfigurationPtr->componentList.push_back(Component(MS_nanoelectrospray, 1));
1011  instrumentConfigurationPtr->componentList.push_back(Component(MS_quadrupole_ion_trap, 2));
1012  instrumentConfigurationPtr->componentList.push_back(Component(MS_electron_multiplier, 3));
1013 
1014  SoftwarePtr softwareXcalibur(new Software);
1015  softwareXcalibur->id = "Xcalibur";
1016  softwareXcalibur->set(MS_Xcalibur);
1017  softwareXcalibur->version = "2.0.5";
1018  instrumentConfigurationPtr->softwarePtr = softwareXcalibur;
1019 
1020  msd.instrumentConfigurationPtrs.push_back(instrumentConfigurationPtr);
1021 
1022  // softwareList
1023 
1024  SoftwarePtr softwareBioworks(new Software);
1025  softwareBioworks->id = "Bioworks";
1026  softwareBioworks->set(MS_Bioworks);
1027  softwareBioworks->version = "3.3.1 sp1";
1028 
1029  SoftwarePtr software_pwiz(new Software);
1030  software_pwiz->id = "pwiz";
1031  software_pwiz->set(MS_pwiz);
1032  software_pwiz->version = "1.0";
1033 
1034  msd.softwarePtrs.push_back(softwareBioworks);
1035  msd.softwarePtrs.push_back(software_pwiz);
1036  msd.softwarePtrs.push_back(softwareXcalibur);
1037 
1038  // dataProcessingList
1039 
1040  DataProcessingPtr dpXcalibur(new DataProcessing);
1041  dpXcalibur->id = "Xcalibur Processing";
1042 
1043  ProcessingMethod procXcal;
1044  procXcal.order = 1;
1045  procXcal.softwarePtr = softwareXcalibur;
1046  procXcal.cvParams.push_back(CVParam(MS_deisotoping, false));
1047  procXcal.cvParams.push_back(CVParam(MS_charge_deconvolution, false));
1048  procXcal.cvParams.push_back(CVParam(MS_peak_picking, true));
1049 
1050  dpXcalibur->processingMethods.push_back(procXcal);
1051 
1052  DataProcessingPtr dp_msconvert(new DataProcessing);
1053  dp_msconvert->id = "pwiz conversion";
1054 
1055  ProcessingMethod proc_msconvert;
1056  proc_msconvert.order = 2;
1057  proc_msconvert.softwarePtr = software_pwiz;
1058  proc_msconvert.cvParams.push_back(MS_Conversion_to_mzML);
1059 
1060  dp_msconvert->processingMethods.push_back(proc_msconvert);
1061 
1062  msd.dataProcessingPtrs.push_back(dpXcalibur);
1063  msd.dataProcessingPtrs.push_back(dp_msconvert);
1064 
1065  ScanSettingsPtr as1(new ScanSettings("as1"));
1066  as1->sourceFilePtrs.push_back(sfp_parameters);
1067  Target t1;
1068  t1.set(MS_m_z, 1000);
1069  Target t2;
1070  t2.set(MS_m_z, 1200);
1071  as1->targets.push_back(t1);
1072  as1->targets.push_back(t2);
1073  msd.scanSettingsPtrs.push_back(as1);
1074 
1075  // run
1076 
1077  msd.run.id = "Exp01";
1078  msd.run.defaultInstrumentConfigurationPtr = instrumentConfigurationPtr;
1079  msd.run.samplePtr = samplePtr;
1080  msd.run.startTimeStamp = "2007-06-27T15:23:45.00035";
1081  msd.run.defaultSourceFilePtr = sfp;
1082 
1083  shared_ptr<SpectrumListSimple> spectrumList(new SpectrumListSimple);
1084  msd.run.spectrumListPtr = spectrumList;
1085 
1086  spectrumList->spectra.push_back(SpectrumPtr(new Spectrum));
1087  spectrumList->spectra.push_back(SpectrumPtr(new Spectrum));
1088 
1089  Spectrum& s19 = *spectrumList->spectra[0];
1090  s19.id = "S19";
1091  s19.index = 0;
1092  s19.defaultArrayLength = 10;
1093  s19.cvParams.push_back(MS_MSn_spectrum);
1094  s19.set(MS_ms_level, 1);
1095  s19.cvParams.push_back(MS_centroid_spectrum);
1096  s19.cvParams.push_back(CVParam(MS_lowest_observed_m_z, 400.39));
1097  s19.cvParams.push_back(CVParam(MS_highest_observed_m_z, 1795.56));
1098  s19.cvParams.push_back(CVParam(MS_base_peak_m_z, 445.347));
1099  s19.cvParams.push_back(CVParam(MS_base_peak_intensity, 120053));
1100  s19.cvParams.push_back(CVParam(MS_total_ion_current, 1.66755e+007));
1101  s19.scanList.scans.push_back(Scan());
1102  Scan& s19scan = s19.scanList.scans.back();
1103  s19scan.instrumentConfigurationPtr = instrumentConfigurationPtr;
1104  s19scan.paramGroupPtrs.push_back(pg1);
1105  s19scan.cvParams.push_back(CVParam(MS_scan_start_time, 5.890500, UO_minute));
1106  s19scan.cvParams.push_back(CVParam(MS_filter_string, "+ c NSI Full ms [ 400.00-1800.00]"));
1107  s19scan.scanWindows.resize(1);
1108  ScanWindow& window = s19scan.scanWindows.front();
1109  window.cvParams.push_back(CVParam(MS_scan_window_lower_limit, 400.000000));
1110  window.cvParams.push_back(CVParam(MS_scan_window_upper_limit, 1800.000000));
1111 
1112  BinaryDataArrayPtr s19_mz(new BinaryDataArray);
1113  s19_mz->dataProcessingPtr = dpXcalibur;
1114  s19_mz->cvParams.push_back(MS_m_z_array);
1115  s19_mz->data.resize(10);
1116  for (int i=0; i<10; i++)
1117  s19_mz->data[i] = i;
1118 
1119  BinaryDataArrayPtr s19_intensity(new BinaryDataArray);
1120  s19_intensity->dataProcessingPtr = dpXcalibur;
1121  s19_intensity->cvParams.push_back(MS_intensity_array);
1122  s19_intensity->data.resize(10);
1123  for (int i=0; i<10; i++)
1124  s19_intensity->data[i] = 10-i;
1125 
1126  s19.binaryDataArrayPtrs.push_back(s19_mz);
1127  s19.binaryDataArrayPtrs.push_back(s19_intensity);
1128 
1129  Spectrum& s20 = *spectrumList->spectra[1];
1130  s20.id = "S20";
1131  s20.index = 1;
1132  s20.defaultArrayLength = 10;
1133 
1134  s20.cvParams.push_back(MS_MSn_spectrum);
1135  s20.set(MS_ms_level, 2);
1136 
1137  s20.cvParams.push_back(MS_centroid_spectrum);
1138  s20.cvParams.push_back(CVParam(MS_lowest_observed_m_z, 320.39));
1139  s20.cvParams.push_back(CVParam(MS_highest_observed_m_z, 1003.56));
1140  s20.cvParams.push_back(CVParam(MS_base_peak_m_z, 456.347));
1141  s20.cvParams.push_back(CVParam(MS_base_peak_intensity, 23433));
1142  s20.cvParams.push_back(CVParam(MS_total_ion_current, 1.66755e+007));
1143 
1144  s20.precursors.resize(1);
1145  Precursor& precursor = s20.precursors.front();
1146  precursor.spectrumID= s19.id;
1147  precursor.selectedIons.resize(1);
1148  precursor.selectedIons[0].cvParams.push_back(CVParam(MS_selected_ion_m_z, 445.34));
1149  precursor.selectedIons[0].cvParams.push_back(CVParam(MS_charge_state, 2));
1151  precursor.activation.cvParams.push_back(CVParam(MS_collision_energy, 35.00, UO_electronvolt));
1152 
1153  s20.scanList.scans.push_back(Scan());
1154  Scan& s20scan = s20.scanList.scans.back();
1155  s20scan.instrumentConfigurationPtr = instrumentConfigurationPtr;
1156  s20scan.paramGroupPtrs.push_back(pg2);
1157  s20scan.cvParams.push_back(CVParam(MS_scan_start_time, 5.990500, UO_minute));
1158  s20scan.cvParams.push_back(CVParam(MS_filter_string, "+ c d Full ms2 445.35@cid35.00 [ 110.00-905.00]"));
1159  s20scan.scanWindows.resize(1);
1160  ScanWindow& window2 = s20scan.scanWindows.front();
1161  window2.cvParams.push_back(CVParam(MS_scan_window_lower_limit, 110.000000));
1162  window2.cvParams.push_back(CVParam(MS_scan_window_upper_limit, 905.000000));
1163 
1164  BinaryDataArrayPtr s20_mz(new BinaryDataArray);
1165  s20_mz->dataProcessingPtr = dpXcalibur;
1166  s20_mz->cvParams.push_back(MS_m_z_array);
1167  s20_mz->data.resize(10);
1168  for (int i=0; i<10; i++)
1169  s20_mz->data[i] = i;
1170 
1171  BinaryDataArrayPtr s20_intensity(new BinaryDataArray);
1172  s20_intensity->dataProcessingPtr = dpXcalibur;
1173  s20_intensity->cvParams.push_back(MS_intensity_array);
1174  s20_intensity->data.resize(10);
1175  for (int i=0; i<10; i++)
1176  s20_intensity->data[i] = 10-i;
1177 
1178  s20.binaryDataArrayPtrs.push_back(s20_mz);
1179  s20.binaryDataArrayPtrs.push_back(s20_intensity);
1180 
1181  // chromatograms
1182 
1183  shared_ptr<ChromatogramListSimple> chromatogramList(new ChromatogramListSimple);
1184  msd.run.chromatogramListPtr = chromatogramList;
1185 
1186  chromatogramList->chromatograms.push_back(ChromatogramPtr(new Chromatogram));
1187 
1188  Chromatogram& tic = *chromatogramList->chromatograms[0];
1189  tic.id = "tic";
1190  tic.index = 0;
1191  tic.defaultArrayLength = 10;
1193 
1194  BinaryDataArrayPtr tic_time(new BinaryDataArray);
1195  tic_time->dataProcessingPtr = dp_msconvert;
1196  tic_time->cvParams.push_back(MS_time_array);
1197  tic_time->data.resize(10);
1198  for (int i=0; i<10; i++)
1199  tic_time->data[i] = i;
1200 
1201  BinaryDataArrayPtr tic_intensity(new BinaryDataArray);
1202  tic_intensity->dataProcessingPtr = dp_msconvert;
1203  tic_intensity->cvParams.push_back(MS_intensity_array);
1204  tic_intensity->data.resize(10);
1205  for (int i=0; i<10; i++)
1206  tic_intensity->data[i] = 10-i;
1207 
1208  tic.binaryDataArrayPtrs.push_back(tic_time);
1209  tic.binaryDataArrayPtrs.push_back(tic_intensity);
1210 }
1211 
1212 
1214 {
1215  if (os_) *os_ << "testMSData():\n";
1216 
1217  MSData a;
1218  initializeTestData(a);
1219 
1220  // write 'a' out to a stream
1221 
1222  ostringstream oss;
1223  XMLWriter writer(oss);
1224  IO::write(writer, a);
1225  if (os_) *os_ << oss.str() << endl;
1226 
1227  // read 'b' in from stream, ignoring SpectrumList (default)
1228 
1229  MSData b;
1230  istringstream iss(oss.str());
1231  IO::read(iss, b); // IO::IgnoreSpectrumList
1232 
1233  // compare 'a' and 'b'
1234 
1236  if (diff && os_) *os_ << "diff:\n" << diff << endl;
1237  unit_assert(diff);
1238  unit_assert(diff.a_b.run.spectrumListPtr.get());
1239  unit_assert(diff.a_b.run.spectrumListPtr->size() == 1);
1240  unit_assert(diff.a_b.run.spectrumListPtr->spectrum(0)->userParams.size() == 1);
1241 
1242  // read 'c' in from stream, reading SpectrumList
1243 
1244  MSData c;
1245  iss.seekg(0);
1246  IO::read(iss, c, IO::ReadSpectrumList);
1247 
1248  // compare 'a' and 'c'
1249 
1250  diff(a,c);
1251  if (diff && os_) *os_ << "diff:\n" << diff << endl;
1252  unit_assert(!diff);
1253 
1254  // remove SpectrumList and ChromatogramList from a, and compare to b
1255 
1256  a.run.spectrumListPtr.reset();
1257  a.run.chromatogramListPtr.reset();
1258  diff(a, b);
1259  unit_assert(!diff);
1260 }
1261 
1262 
1263 void test()
1264 {
1265  testCV();
1266  testUserParam();
1267  testCVParam();
1268  testParamGroup();
1269  testNamedParamContainer<FileContent>();
1270  testSourceFile();
1271  testNamedParamContainer<Contact>();
1273  testSample();
1274  testComponent();
1275  testComponentList();
1276  testSoftware();
1280  testNamedParamContainer<Target>();
1281  testScanSettings();
1282  testNamedParamContainer<IsolationWindow>();
1283  testNamedParamContainer<SelectedIon>();
1284  testNamedParamContainer<Activation>();
1285  testPrecursor();
1286  testProduct();
1287  testNamedParamContainer<ScanWindow>();
1288  testScan();
1289  testScanList();
1292  testSpectrum();
1293  testChromatogram();
1294  testSpectrumList();
1299  testRun();
1300  testMSData();
1301 }
1302 
1303 
1304 int main(int argc, char* argv[])
1305 {
1306  TEST_PROLOG_EX(argc, argv, "_MSData")
1307 
1308  try
1309  {
1310  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
1311  test();
1312  if (os_) *os_ << "ok\n";
1313  }
1314  catch (exception& e)
1315  {
1316  TEST_FAILED(e.what())
1317  }
1318  catch (...)
1319  {
1320  TEST_FAILED("Caught unknown exception.")
1321  }
1322 
1323  TEST_EPILOG
1324 }
1325