ProteoWizard
DiffTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: DiffTest.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 #include "Diff.hpp"
24 #include "TextWriter.hpp"
25 #include "examples.hpp"
28 #include <cstring>
29 
30 
31 using namespace pwiz::util;
32 using namespace pwiz::cv;
33 using namespace pwiz::data;
34 using namespace pwiz::data::diff_impl;
35 using namespace pwiz::msdata;
36 
37 
38 ostream* os_ = 0;
39 
40 
42 {
43  if (os_) *os_ << "testFileContent()\n";
44 
45  FileContent a, b;
46  a.userParams.push_back(UserParam("common"));
47  b.userParams.push_back(UserParam("common"));
48 
50  unit_assert(!diff);
51 
52  a.userParams.push_back(UserParam("different", "1"));
53  b.userParams.push_back(UserParam("different", "2"));
54 
55  diff(a, b);
56  if (os_) *os_ << diff << endl;
57  unit_assert(diff);
58 
59  unit_assert(diff.a_b.userParams.size() == 1);
60  unit_assert(diff.a_b.userParams[0] == UserParam("different","1"));
61  unit_assert(diff.b_a.userParams.size() == 1);
62  unit_assert(diff.b_a.userParams[0] == UserParam("different","2"));
63 }
64 
65 
67 {
68  if (os_) *os_ << "testSourceFile()\n";
69 
70  SourceFile a("id1","name1","location1"), b("id1","name1","location1");
71  a.userParams.push_back(UserParam("common"));
72  b.userParams.push_back(UserParam("common"));
73 
75  unit_assert(!diff);
76 
77  b.location = "location2";
78  diff(a, b);
79  if (os_) *os_ << diff << endl;
80  unit_assert(diff);
81 }
82 
83 
85 {
86  if (os_) *os_ << "testFileDescription()\n";
87 
88  FileDescription a, b;
89 
90  a.fileContent.userParams.push_back(UserParam("user param 1"));
91  b.fileContent.userParams.push_back(UserParam("user param 1"));
92 
93  Contact contact1, contact2, contact3, contact4;
94  contact1.cvParams.push_back(CVParam(MS_contact_name, "Darren"));
95  contact2.cvParams.push_back(CVParam(MS_contact_name, "Laura Jane"));
96  contact3.cvParams.push_back(CVParam(MS_contact_name, "Emma Lee"));
97  contact4.cvParams.push_back(CVParam(MS_contact_name, "Isabelle Lynn"));
98 
99  // verify vector_diff_diff with differently ordered vectors
100  a.contacts.push_back(contact2);
101  a.contacts.push_back(contact1);
102  b.contacts.push_back(contact1);
103  b.contacts.push_back(contact2);
104 
105  SourceFilePtr source1(new SourceFile("id1"));
106  SourceFilePtr source2a(new SourceFile("id2"));
107  SourceFilePtr source2b(new SourceFile("id2"));
108  source2a->cvParams.push_back(MS_Thermo_RAW_file);
109 
110  a.sourceFilePtrs.push_back(source1);
111  b.sourceFilePtrs.push_back(source1);
112 
114  unit_assert(!diff);
115 
116  a.contacts.push_back(contact3);
117  b.contacts.push_back(contact4);
118 
119  a.sourceFilePtrs.push_back(source2a);
120  b.sourceFilePtrs.push_back(source2b);
121 
122  diff(a, b);
123  if (os_) *os_ << diff << endl;
124 
125  unit_assert(diff);
126  unit_assert(diff.a_b.contacts.size() == 1);
127  unit_assert(diff.a_b.contacts[0].cvParam(MS_contact_name).value == "Emma Lee");
128  unit_assert(diff.b_a.contacts.size() == 1);
129  unit_assert(diff.b_a.contacts[0].cvParam(MS_contact_name).value == "Isabelle Lynn");
130 
131  unit_assert(diff.a_b.sourceFilePtrs.size() == 1);
132  unit_assert(diff.a_b.sourceFilePtrs[0]->hasCVParam(MS_Thermo_RAW_file));
133  unit_assert(diff.b_a.sourceFilePtrs.size() == 1);
134  unit_assert(!diff.b_a.sourceFilePtrs[0]->hasCVParam(MS_Thermo_RAW_file));
135 }
136 
137 
139 {
140  if (os_) *os_ << "testSample()\n";
141 
142  Sample a("id1","name1"), b("id1","name1");
143  a.userParams.push_back(UserParam("common"));
144  b.userParams.push_back(UserParam("common"));
145 
147  unit_assert(!diff);
148 
149  a.cvParams.push_back(MS_peak_intensity);
150  diff(a, b);
151  if (os_) *os_ << diff << endl;
152  unit_assert(diff);
153 }
154 
155 
157 {
158  if (os_) *os_ << "testComponent()\n";
159 
160  Component a, b;
161  a.userParams.push_back(UserParam("common"));
162  b.userParams.push_back(UserParam("common"));
163 
165  unit_assert(!diff);
166 
167  a.order = 420;
168  b.order = 421;
169  diff(a, b);
170  if (os_) *os_ << diff << endl;
171  unit_assert(diff);
172 }
173 
174 
176 {
177  if (os_) *os_ << "testSource()\n";
178 
179  Component a, b;
180  a.userParams.push_back(UserParam("common"));
181  b.userParams.push_back(UserParam("common"));
182 
184  unit_assert(!diff);
185 
186  a.order = 420;
187  b.order = 421;
188  diff(a, b);
189  if (os_) *os_ << diff << endl;
190  unit_assert(diff);
191 }
192 
193 
195 {
196  if (os_) *os_ << "testComponentList()\n";
197 
198  ComponentList a, b;
199 
200  a.push_back(Component(ComponentType_Source, 1));
201  b.push_back(Component(ComponentType_Source, 1));
202  a.push_back(Component(ComponentType_Analyzer, 2));
203  b.push_back(Component(ComponentType_Analyzer, 2));
204  a.push_back(Component(ComponentType_Detector, 3));
205  b.push_back(Component(ComponentType_Detector, 3));
206 
207  a[0].userParams.push_back(UserParam("common"));
208  b[0].userParams.push_back(UserParam("common"));
209 
211  unit_assert(!diff);
212 
213  a[1].userParams.push_back(UserParam("common"));
214  b[1].userParams.push_back(UserParam("common"));
215  a[1].userParams.push_back(UserParam("a only"));
216  b[1].userParams.push_back(UserParam("b only"));
217 
218  a[2].userParams.push_back(UserParam("a only"));
219  b[2].userParams.push_back(UserParam("b only"));
220 
221  diff(a, b);
222  if (os_) *os_ << diff << endl;
223  unit_assert(diff);
224 }
225 
226 
228 {
229  if (os_) *os_ << "testSoftware()\n";
230 
231  Software a, b;
232 
233  a.id = "msdata";
234  a.version = "4.20";
236  b = a;
237 
239  unit_assert(!diff);
240 
241  b.version = "4.21";
242 
243  diff(a, b);
244  if (os_) *os_ << diff << endl;
245  unit_assert(diff);
246 }
247 
248 
250 {
252 
253  a.id = "LCQ Deca";
254  a.cvParams.push_back(MS_LCQ_Deca);
255  a.cvParams.push_back(CVParam(MS_instrument_serial_number, 23433));
259 
260  b = a;
261 
262  a.softwarePtr = SoftwarePtr(new Software("XCalibur"));
263  a.softwarePtr->version = "4.20";
264 
265  b.softwarePtr = SoftwarePtr(new Software("XCalibur"));
266  b.softwarePtr->version = "4.20";
267 
269  unit_assert(!diff);
270 
272  b.componentList.source(0).order = 2;
273  b.componentList.detector(0).order = 1;
274 
275  diff(a, b);
276  if (os_) *os_ << diff << endl;
277  unit_assert(diff);
278 }
279 
280 
282 {
283  if (os_) *os_ << "testProcessingMethod()\n";
284 
285  ProcessingMethod a, b;
286  a.userParams.push_back(UserParam("common"));
287  b.userParams.push_back(UserParam("common"));
288 
290  unit_assert(!diff);
291 
292  a.order = 420;
293  b.order = 421;
294  diff(a, b);
295  if (os_) *os_ << diff << endl;
296  unit_assert(diff);
297 
298  b.order = 420;
299  diff(a, b);
300  if (os_) *os_ << diff << endl;
301  unit_assert(!diff);
302 
303  a.softwarePtr = SoftwarePtr(new Software("pwiz"));
304  b.softwarePtr = SoftwarePtr(new Software("pwiz2"));
305  diff(a, b);
306  if (os_) *os_ << diff << endl;
307  unit_assert(diff);
308 }
309 
310 
312 {
313  if (os_) *os_ << "testDataProcessing()\n";
314 
315  DataProcessing a, b;
316  a.id = "dp1";
317 
318  b = a;
319 
320  ProcessingMethod pm1, pm2, pm3;
321  pm1.userParams.push_back(UserParam("abc"));
322  pm2.userParams.push_back(UserParam("def"));
323  pm3.userParams.push_back(UserParam("ghi"));
324 
325  pm1.softwarePtr = SoftwarePtr(new Software("msdata"));
326  pm1.softwarePtr->version = "4.20";
327 
328  pm2.softwarePtr = SoftwarePtr(new Software("msdata"));
329  pm2.softwarePtr->version = "4.20";
330 
331  a.processingMethods.push_back(pm1);
332  a.processingMethods.push_back(pm2);
333  b.processingMethods.push_back(pm2);
334  b.processingMethods.push_back(pm1);
335 
337  unit_assert(!diff);
338 
339  pm2.softwarePtr = SoftwarePtr(new Software("Xcalibur"));
340  a.processingMethods.push_back(pm3);
341 
342  diff(a, b);
343 
344  if (os_) *os_ << diff << endl;
345  unit_assert(diff);
346 }
347 
348 
350 {
351  if (os_) *os_ << "testScanSettings()\n";
352 
353  ScanSettings a, b;
354  a.id = "as1";
355 
356  b = a;
357 
359  unit_assert(!diff);
360 
361  b.sourceFilePtrs.push_back(SourceFilePtr(new SourceFile("source file")));
362  a.targets.resize(2);
363 
364  diff(a, b);
365 
366  if (os_) *os_ << diff << endl;
367  unit_assert(diff);
368  unit_assert(diff.a_b.sourceFilePtrs.empty());
369  unit_assert(diff.b_a.sourceFilePtrs.size() == 1);
370  unit_assert(diff.a_b.targets.size() == 2);
371  unit_assert(diff.b_a.targets.empty());
372 }
373 
374 
376 {
377  if (os_) *os_ << "testPrecursor()\n";
378 
379  Precursor a, b;
380 
381  a.spectrumID = "1234";
382  a.activation.cvParams.push_back(CVParam(MS_ionization_type, 420));
383  a.selectedIons.resize(1);
384  a.selectedIons[0].cvParams.push_back(MS_reflectron_on);
385  a.cvParams.push_back(MS_reflectron_off);
386  b = a;
387 
389  unit_assert(!diff);
390 
391  a.cvParams.push_back(MS_reflectron_on);
392  a.selectedIons[0].userParams.push_back(UserParam("aaaa"));
393  b.activation.userParams.push_back(UserParam("bbbb"));
394  b.isolationWindow.set(MS_m_z, 200);
395 
396  diff(a, b);
397 
398  if (os_) *os_ << diff << endl;
399  unit_assert(diff);
400  unit_assert(!diff.a_b.selectedIons.empty());
401  unit_assert(!diff.a_b.selectedIons[0].userParams.empty());
402  unit_assert(!diff.b_a.selectedIons.empty());
403  unit_assert(diff.b_a.isolationWindow.cvParam(MS_m_z).valueAs<int>() == 200);
404 }
405 
406 
408 {
409  if (os_) *os_ << "testProduct()\n";
410 
411  Product a, b;
412 
414  b = a;
415 
417  unit_assert(!diff);
418 
419  b.isolationWindow.set(MS_m_z, 200);
420 
421  diff(a, b);
422 
423  if (os_) *os_ << diff << endl;
424  unit_assert(diff);
425  unit_assert(diff.a_b.isolationWindow.cvParams.empty());
426  unit_assert(diff.b_a.isolationWindow.cvParams.size() == 1);
427 }
428 
429 
430 void testScan()
431 {
432  if (os_) *os_ << "testScan()\n";
433 
434  Scan a, b;
435 
437  ip->id = "LTQ FT";
438 
439  a.cvParams.push_back(CVParam(MS_ionization_type, 420));
441  a.scanWindows.push_back(ScanWindow());
442  b = a;
443 
445  unit_assert(!diff);
446 
447  b.scanWindows.push_back(ScanWindow(250.0, 2000.0, MS_m_z));
448  diff(a, b);
449 
450  if (os_) *os_ << diff << endl;
451  unit_assert(diff);
452  unit_assert(diff.b_a.scanWindows.size() == 1);
453 }
454 
455 
457 {
458  if (os_) *os_ << "testScanList()\n";
459 
460  ScanList a, b;
461 
462  Scan a1;
463  a1.set(MS_filter_string, "booger");
464  a1.set(MS_scan_start_time, "4.20", UO_minute);
465 
466  Scan a2;
467  a1.set(MS_filter_string, "goober");
468  a1.set(MS_scan_start_time, "6.66", UO_minute);
469 
470  a.scans.push_back(a1);
471  a.scans.push_back(a2);
472  b.scans.push_back(a2);
473  b.scans.push_back(a1);
474 
476  unit_assert(!diff);
477 
478  a.cvParams.push_back(MS_reflectron_on);
479 
480  diff(a, b);
481 
482  if (os_) *os_ << diff << endl;
483  unit_assert(diff);
484 }
485 
486 
488 {
489  if (os_) *os_ << "testBinaryDataArray()\n";
490 
491  vector<double> data;
492  for (int i=0; i<10; i++) data.push_back(i);
493 
494  BinaryDataArray a, b;
495  a.data = data;
497  b = a;
498 
499  DiffConfig config;
500  config.precision = 1e-4;
501 
502  a.data[9] = 1.00001e10;
503  b.data[9] = 1.00000e10;
504 
505  // we want to verify relative precision diff (1e-5),
506  // not absolute diff (1e5)
507 
509  if (diff && os_) *os_ << diff << endl;
510  unit_assert(!diff);
511 
512  b.data[9] = 1.0002e10;
513 
514  diff(a, b);
515 
516  if (diff && os_) *os_ << diff << endl;
517  unit_assert(diff);
518 }
519 
520 
522 {
523  if (os_) *os_ << "testSpectrum()\n";
524 
525  Spectrum a, b;
526 
527  a.id = "goober";
528  a.index = 1;
530  a.scanList.scans.push_back(Scan());
531  a.scanList.scans.back().instrumentConfigurationPtr = InstrumentConfigurationPtr(new InstrumentConfiguration("LTQ FT"));
532  a.scanList.scans.back().paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("CommonMS1SpectrumParams")));
533  a.scanList.scans.back().cvParams.push_back(CVParam(MS_scan_start_time, 5.890500, UO_minute));
534  a.scanList.scans.back().cvParams.push_back(CVParam(MS_filter_string, "+ c NSI Full ms [ 400.00-1800.00]"));
535  a.scanList.scans.back().scanWindows.push_back(ScanWindow(400.0, 1800.0, MS_m_z));
536 
537  b = a;
538 
539  DiffConfig config;
540  config.precision = 1e-6;
541  Diff<Spectrum, DiffConfig> diff(a, b, config);
542  if (diff) cout << diff;
543  unit_assert(!diff);
544 
545  b.index = 4;
546  b.defaultArrayLength = 22;
548  b.sourceFilePtr = SourceFilePtr(new SourceFile("test.raw"));
549  a.precursors.push_back(Precursor());
550  a.precursors.back().spectrumID = "666";
551  b.products.push_back(Product());
552  b.products.back().isolationWindow.set(MS_ionization_type, 420);
554  a.binaryDataArrayPtrs.back()->data.resize(6);
556  b.binaryDataArrayPtrs.back()->data.resize(7);
557  b.binaryDataArrayPtrs.push_back(a.binaryDataArrayPtrs[0]);
558 
559  diff(a, b);
560 
561  if (os_) *os_ << diff << endl;
562  unit_assert(diff);
563 
564  unit_assert(diff.a_b.index == 1);
565  unit_assert(diff.a_b.id == "goober");
566  unit_assert(diff.a_b.defaultArrayLength == 0);
567  unit_assert(diff.a_b.dataProcessingPtr->id == "msdata 2");
568  unit_assert(diff.a_b.precursors.size() == 1);
569  unit_assert(diff.a_b.products.empty());
570  unit_assert(diff.a_b.binaryDataArrayPtrs.empty());
571 
572  unit_assert(diff.b_a.index == 4);
573  unit_assert(diff.b_a.id == "goober");
574  unit_assert(diff.b_a.defaultArrayLength == 22);
575  unit_assert(diff.b_a.dataProcessingPtr->id == "msdata");
576  unit_assert(diff.b_a.precursors.empty());
577  unit_assert(diff.b_a.products.size() == 1);
578  unit_assert(diff.b_a.binaryDataArrayPtrs.empty());
579 
580  b = a;
581 
582  unit_assert(a.binaryDataArrayPtrs.size() == 1);
583  b.binaryDataArrayPtrs[0] = BinaryDataArrayPtr(new BinaryDataArray);
584  b.binaryDataArrayPtrs[0]->data.resize(6);
585 
586  a.binaryDataArrayPtrs[0]->data[0] = 420;
587  b.binaryDataArrayPtrs[0]->data[0] = 420 + 1e-12;
588 
589  diff(a,b);
590  if (os_ && diff) *os_ << diff << endl;
591  unit_assert(!diff);
592 
593  b.binaryDataArrayPtrs[0]->data[0] += 1e-3;
594  diff(a,b);
595  if (os_ && diff) *os_ << diff << endl;
596  unit_assert(diff);
597 }
598 
599 
601 {
602  if (os_) *os_ << "testChromatogram()\n";
603 
604  Chromatogram a, b;
605 
606  a.id = "goober";
607  a.index = 1;
610  a.binaryDataArrayPtrs.back()->data.resize(6);
611 
612  b = a;
613 
614  DiffConfig config;
615  config.precision = 1e-6;
616  Diff<Chromatogram, DiffConfig> diff(a, b, config);
617  if (diff) cout << diff;
618  unit_assert(!diff);
619 
621  b.binaryDataArrayPtrs[0]->data.resize(6);
622 
623  a.binaryDataArrayPtrs[0]->data[0] = 420;
624  b.binaryDataArrayPtrs[0]->data[0] = 420 + 1e-12;
625 
626  diff(a,b);
627  if (os_ && diff) *os_ << diff << endl;
628  unit_assert(!diff);
629 
630  b.binaryDataArrayPtrs[0]->data[0] += 1e-3;
631  diff(a,b);
632  if (os_ && diff) *os_ << diff << endl;
633  unit_assert(diff);
634 }
635 
636 
638 {
639  if (os_) *os_ << "testSpectrumList()\n";
640 
641  SpectrumListSimple aSimple, bSimple;
642 
643  SpectrumPtr spectrum1a = SpectrumPtr(new Spectrum);
644  spectrum1a->id = "420";
645 
646  SpectrumPtr spectrum1b = SpectrumPtr(new Spectrum);
647  spectrum1b->id = "420";
648 
649  aSimple.spectra.push_back(spectrum1a);
650  bSimple.spectra.push_back(spectrum1b);
651 
652  SpectrumList& a = aSimple;
653  SpectrumList& b = bSimple;
654 
656  unit_assert(!diff);
657 
658  // check: dataProcessingPtr
659 
660  aSimple.dp = DataProcessingPtr(new DataProcessing("dp"));
661  diff(a, b);
662  unit_assert(diff);
663 
664  DiffConfig config_ignore;
665  config_ignore.ignoreDataProcessing = true;
666  Diff<SpectrumList, DiffConfig, SpectrumListSimple> diff_ignore(a, b, config_ignore);
667  unit_assert(!diff_ignore);
668 
669  aSimple.dp = DataProcessingPtr();
670  diff(a, b);
671  unit_assert(!diff);
672 
673  // check: different SpectrumList::size()
674 
675  SpectrumPtr spectrum2 = SpectrumPtr(new Spectrum);
676  spectrum2->id = "421";
677  aSimple.spectra.push_back(spectrum2);
678 
679  diff(a, b);
680  if (os_) *os_ << diff << endl;
681  unit_assert(diff);
682  unit_assert(diff.a_b.spectra.size() == 1);
683  unit_assert(diff.a_b.spectra[0]->userParams.size() == 1);
684 
685  // check: same SpectrumList::size(), different last scan number
686 
687  SpectrumPtr spectrum3 = SpectrumPtr(new Spectrum);
688  spectrum3->id = "422";
689  bSimple.spectra.push_back(spectrum3);
690 
691  diff(a, b);
692  if (os_) *os_ << diff << endl;
693  unit_assert(diff);
694  unit_assert(diff.a_b.spectra.size() == 1);
695  unit_assert(diff.a_b.spectra[0]->id == "421");
696  unit_assert(diff.b_a.spectra.size() == 1);
697  unit_assert(diff.b_a.spectra[0]->id == "422");
698 
699  // check: scan numbers match, binary data slightly different
700 
701  spectrum3->id = "421";
704  b1->data.resize(10);
705  b2->data.resize(10);
706  for (int i=0; i<10; i++)
707  b1->data[i] = b2->data[i] = i;
708  b2->data[2] += 1e-7;
709  spectrum2->binaryDataArrayPtrs.push_back(b1);
710  spectrum3->binaryDataArrayPtrs.push_back(b2);
711 
712  DiffConfig config;
713  config.precision = 1e-6;
714 
716  unit_assert(!diffWide);
717 
718  config.precision = 1e-12;
720  if (os_) *os_ << diffNarrow << endl;
721  unit_assert(diffNarrow);
722 }
723 
724 
726 {
727  if (os_) *os_ << "testChromatogramList()\n";
728 
729  ChromatogramListSimple aSimple, bSimple;
730 
731  ChromatogramPtr chromatogram1a = ChromatogramPtr(new Chromatogram);
732  chromatogram1a->id = "420";
733 
734  ChromatogramPtr chromatogram1b = ChromatogramPtr(new Chromatogram);
735  chromatogram1b->id = "420";
736 
737  aSimple.chromatograms.push_back(chromatogram1a);
738  bSimple.chromatograms.push_back(chromatogram1b);
739 
740  ChromatogramList& a = aSimple;
741  ChromatogramList& b = bSimple;
742 
744  DiffConfig config_ignore;
745  config_ignore.ignoreChromatograms = true;
746 
747  Diff<ChromatogramList, DiffConfig, ChromatogramListSimple> diffIgnore(a, b, config_ignore);
748  unit_assert(!diff);
749  unit_assert(!diffIgnore);
750 
751  // check: dataProcessingPtr
752 
753  aSimple.dp = DataProcessingPtr(new DataProcessing("dp"));
754  diff(a, b);
755  unit_assert(diff);
756 
757  DiffConfig config_ignore_dp;
758  config_ignore_dp.ignoreDataProcessing = true;
759  Diff<ChromatogramList, DiffConfig, ChromatogramListSimple> diff_ignore_dp(a, b, config_ignore_dp);
760  unit_assert(!diff_ignore_dp);
761 
762  aSimple.dp = DataProcessingPtr();
763  diff(a, b);
764  unit_assert(!diff);
765 
766  // check: different ChromatogramList::size()
767 
768  ChromatogramPtr chromatogram2 = ChromatogramPtr(new Chromatogram);
769  chromatogram2->id = "421";
770  aSimple.chromatograms.push_back(chromatogram2);
771 
772  diff(a, b);
773  if (os_) *os_ << diff << endl;
774  unit_assert(diff);
775  unit_assert(diff.a_b.chromatograms.size() == 1);
776  unit_assert(diff.a_b.chromatograms[0]->userParams.size() == 1);
777 
778  diffIgnore(a,b);
779  if (os_) *os_ << diffIgnore << endl;
780  unit_assert(!diffIgnore);
781 
782  // check: same ChromatogramList::size(), different last scan number
783 
784  ChromatogramPtr chromatogram3 = ChromatogramPtr(new Chromatogram);
785  chromatogram3->id = "422";
786  bSimple.chromatograms.push_back(chromatogram3);
787 
788  diff(a, b);
789  if (os_) *os_ << diff << endl;
790  unit_assert(diff);
791  unit_assert(diff.a_b.chromatograms.size() == 1);
792  unit_assert(diff.a_b.chromatograms[0]->id == "421");
793  unit_assert(diff.b_a.chromatograms.size() == 1);
794  unit_assert(diff.b_a.chromatograms[0]->id == "422");
795 
796  diffIgnore(a,b);
797  unit_assert(!diffIgnore);
798 
799  // check: scan numbers match, binary data slightly different
800 
801  chromatogram3->id = "421";
804  b1->data.resize(10);
805  b2->data.resize(10);
806  for (int i=0; i<10; i++)
807  b1->data[i] = b2->data[i] = i;
808  b2->data[2] += 1e-7;
809  chromatogram2->binaryDataArrayPtrs.push_back(b1);
810  chromatogram3->binaryDataArrayPtrs.push_back(b2);
811 
812  DiffConfig config;
813  config.precision = 1e-6;
814 
816  unit_assert(!diffWide);
817 
818  config.precision = 1e-12;
820  if (os_) *os_ << diffNarrow << endl;
821  unit_assert(diffNarrow);
822 
823  diffIgnore(a,b);
824  unit_assert(!diffIgnore);
825 }
826 
827 
828 void testRun()
829 {
830  if (os_) *os_ << "testRun()\n";
831 
832  Run a, b;
833 
834  a.id = "goober";
835  a.startTimeStamp = "20 April 2004 4:20pm";
836  b.id = "goober";
837  b.startTimeStamp = "20 April 2004 4:20pm";
838 
840  unit_assert(!diff);
841 
842  b.id = "raisinet";
843 
844  shared_ptr<SpectrumListSimple> spectrumList1(new SpectrumListSimple);
845  spectrumList1->spectra.push_back(SpectrumPtr(new Spectrum));
846  spectrumList1->spectra.back()->id = "spectrum1";
847  a.spectrumListPtr = spectrumList1;
848 
849  shared_ptr<ChromatogramListSimple> chromatogramList1(new ChromatogramListSimple);
850  chromatogramList1->chromatograms.push_back(ChromatogramPtr(new Chromatogram));
851  chromatogramList1->chromatograms.back()->id = "chromatogram1";
852  b.chromatogramListPtr = chromatogramList1;
853 
854  // same ref id
857 
858  b.samplePtr = SamplePtr(new Sample("sample"));
859  a.defaultSourceFilePtr = SourceFilePtr(new SourceFile("source file"));
860 
861  diff(a, b);
862  if (os_) *os_ << diff << endl;
863  unit_assert(diff);
864 
865  unit_assert(diff.a_b.spectrumListPtr->size() == 1);
866  unit_assert(diff.a_b.spectrumListPtr->spectrum(0)->userParams.size() == 1);
867 
868  unit_assert(diff.a_b.chromatogramListPtr.get());
869  unit_assert(diff.a_b.chromatogramListPtr->size() == 1);
870  unit_assert(diff.a_b.chromatogramListPtr->chromatogram(0)->userParams.size() == 1);
871 
874 
875  unit_assert(!diff.a_b.samplePtr.get());
876  unit_assert(!diff.b_a.samplePtr->empty());
877 
879  unit_assert(!diff.b_a.defaultSourceFilePtr.get());
880 
881  unit_assert(diff.a_b.startTimeStamp.empty());
882  unit_assert(diff.b_a.startTimeStamp.empty());
883 }
884 
885 
886 struct MSDataWithSettableVersion : public MSData {using MSData::version; void version(const string& v) {version_ = v;}};
887 
889 {
890  if (os_) *os_ << "testMSData()\n";
891 
893 
894  a.id = "goober";
895  b.id = "goober";
896 
898  unit_assert(!diff);
899 
900  a.accession = "different";
901  b.version("version");
902  a.cvs.push_back(CV());
904  a.paramGroupPtrs.push_back(ParamGroupPtr(new ParamGroup("pg")));
905  b.samplePtrs.push_back(SamplePtr(new Sample("sample")));
906  a.instrumentConfigurationPtrs.push_back(InstrumentConfigurationPtr(new InstrumentConfiguration("instrumentConfiguration")));
907  b.softwarePtrs.push_back(SoftwarePtr(new Software("software")));
908  a.dataProcessingPtrs.push_back(DataProcessingPtr(new DataProcessing("dataProcessing")));
909  b.run.id = "run";
910  b.scanSettingsPtrs.push_back(ScanSettingsPtr(new ScanSettings("scanSettings")));
911 
912  diff(a, b);
913  if (os_) *os_ << diff << endl;
914  unit_assert(diff);
915 
916  unit_assert(diff.a_b.accession == "different");
917  unit_assert(diff.b_a.accession.empty());
918 
919  unit_assert(diff.a_b.id == (a.id + " (" + a.version() + ")"));
920  unit_assert(diff.b_a.id == (b.id + " (" + b.version() + ")"));
921 
922  unit_assert(diff.a_b.cvs.size() == 1);
923  unit_assert(diff.b_a.cvs.empty());
924 
927 
928  unit_assert(!diff.a_b.paramGroupPtrs.empty());
929  unit_assert(diff.b_a.paramGroupPtrs.empty());
930 
931  unit_assert(diff.a_b.samplePtrs.empty());
932  unit_assert(!diff.b_a.samplePtrs.empty());
933 
936 
937  unit_assert(diff.a_b.softwarePtrs.empty());
938  unit_assert(!diff.b_a.softwarePtrs.empty());
939 
940  unit_assert(!diff.a_b.dataProcessingPtrs.empty());
941  unit_assert(diff.b_a.dataProcessingPtrs.empty());
942 
943  unit_assert(diff.a_b.run.empty());
944  unit_assert(!diff.b_a.run.empty());
945 
946  unit_assert(diff.a_b.scanSettingsPtrs.empty());
947  unit_assert(!diff.b_a.scanSettingsPtrs.empty());
948 }
949 
950 
952 {
953  MSData tiny;
955 
956  MSData tinier;
959  tinier.run.spectrumListPtr = sl;
960  tinier.run.chromatogramListPtr = cl;
961 
962  for (unsigned int i=0; i<tiny.run.spectrumListPtr->size(); i++)
963  {
964  SpectrumPtr from = tiny.run.spectrumListPtr->spectrum(i, true);
965  sl->spectra.push_back(SpectrumPtr(new Spectrum));
966  SpectrumPtr& to = sl->spectra.back();
967 
968  for (vector<BinaryDataArrayPtr>::const_iterator it=from->binaryDataArrayPtrs.begin();
969  it!=from->binaryDataArrayPtrs.end(); ++it)
970  {
971  // copy BinaryDataArray::data from tiny to tinier
972  to->binaryDataArrayPtrs.push_back(BinaryDataArrayPtr(new BinaryDataArray));
973  to->binaryDataArrayPtrs.back()->data = (*it)->data;
974  }
975 
976  // copy "important" scan metadata
977 
978  to->defaultArrayLength = from->defaultArrayLength;
979  to->scanList = from->scanList;
980 
981  to->precursors.resize(from->precursors.size());
982  for (size_t precursorIndex=0; precursorIndex<from->precursors.size(); ++precursorIndex)
983  {
984  Precursor& precursorTo = to->precursors[precursorIndex];
985  Precursor& precursorFrom = from->precursors[precursorIndex];
986  precursorTo.selectedIons = precursorFrom.selectedIons;
987  }
988  }
989 
990  for (unsigned int i=0; i<tiny.run.chromatogramListPtr->size(); i++)
991  {
992  ChromatogramPtr from = tiny.run.chromatogramListPtr->chromatogram(i, true);
993  cl->chromatograms.push_back(ChromatogramPtr(new Chromatogram));
994  ChromatogramPtr& to = cl->chromatograms.back();
995 
996  for (vector<BinaryDataArrayPtr>::const_iterator it=from->binaryDataArrayPtrs.begin();
997  it!=from->binaryDataArrayPtrs.end(); ++it)
998  {
999  // copy BinaryDataArray::data from tiny to tinier
1000  to->binaryDataArrayPtrs.push_back(BinaryDataArrayPtr(new BinaryDataArray));
1001  to->binaryDataArrayPtrs.back()->data = (*it)->data;
1002  }
1003 
1004  // copy "important" scan metadata
1005 
1006  to->defaultArrayLength = from->defaultArrayLength;
1007  }
1008 
1009  if (os_)
1010  {
1011  *os_ << "tinier::";
1012  TextWriter(*os_,0)(tinier);
1013  }
1014 
1015  Diff<MSData, DiffConfig> diff_full(tiny, tinier);
1016  unit_assert(diff_full);
1017 
1018  DiffConfig config;
1019  config.ignoreMetadata = true;
1020  config.ignoreIdentity = true;
1021 
1022  Diff<MSData, DiffConfig> diff_data(tiny, tinier, config);
1023  if (os_ && diff_data) *os_ << diff_data << endl;
1024  unit_assert(!diff_data);
1025 }
1026 
1027 
1028 static const char* userParamName_MaxBinaryDataArrayDifference_ = "Maximum binary data array difference";
1029 
1030 // gets value of MaxBinaryDataArrayDifference userParam if present, else 0
1031 template <typename list_type>
1032 double getMaxPrecisionDiff(const list_type& list)
1033 {
1034  if (list.dp.get() &&
1035  !list.dp->processingMethods.empty() &&
1036  !list.dp->processingMethods.back().userParam(userParamName_MaxBinaryDataArrayDifference_).empty())
1037  return lexical_cast<double>(list.dp->processingMethods.back().userParam(userParamName_MaxBinaryDataArrayDifference_).value);
1038  return 0;
1039 }
1040 
1041 
1043 
1044 {
1045  if (os_)
1046  {
1047  *os_ <<"testMaxPrecisionDiff()\n";
1048  }
1049 
1051 
1058 
1059  std::vector<double> data1;
1060  std::vector<double> data2;
1061 
1062  data1.push_back(3.0);
1063  data2.push_back(3.0000001);
1064 
1065  e->data = data1;
1066  f->data = data2;
1067 
1068  DiffConfig config;
1069  config.precision=1e-6;
1070 
1071  Diff<BinaryDataArray, DiffConfig> diff_toosmall(*e,*f,config);
1072 
1073  //not diff for diff of 1e-7
1074  unit_assert(!diff_toosmall);
1075 
1076  data1.push_back(2.0);
1077  data2.push_back(2.0001);
1078 
1079  c->data = data1;
1080  d->data = data2;
1081 
1082  data1.push_back(1.0);
1083  data2.push_back(1.001);
1084 
1085  a->data = data1;
1086  b->data = data2;
1087 
1089 
1090  //diff
1091  unit_assert(diff);
1092 
1093  if(os_) *os_<<diff<<endl;
1094 
1095 
1096  Diff<BinaryDataArray, DiffConfig> diff2(*c,*d,config);
1097 
1098  //diff
1099  unit_assert(diff2);
1100 
1101  if(os_) *os_<<diff2<<endl;
1102 
1103  // BinaryDataArray UserParam is set
1104  unit_assert(!diff.a_b.userParams.empty());
1105  unit_assert(!diff.b_a.userParams.empty());
1106 
1107  // and correctly
1108  double maxBin_a_b=boost::lexical_cast<double>(diff.a_b.userParam("Binary data array difference").value);
1109  double maxBin_b_a=boost::lexical_cast<double>(diff.a_b.userParam("Binary data array difference").value);
1110 
1111  unit_assert_equal(maxBin_a_b,.001,epsilon);
1112  unit_assert_equal(maxBin_b_a,.001,epsilon);
1113 
1114  Run run_a, run_b;
1115 
1116  shared_ptr<SpectrumListSimple> sls_a(new SpectrumListSimple);
1117  shared_ptr<SpectrumListSimple> sls_b(new SpectrumListSimple);
1118 
1119  SpectrumPtr spa(new Spectrum);
1120  SpectrumPtr spb(new Spectrum);
1121  SpectrumPtr spc(new Spectrum);
1122  SpectrumPtr spd(new Spectrum);
1123 
1124  spa->binaryDataArrayPtrs.push_back(a);
1125  spb->binaryDataArrayPtrs.push_back(b);
1126  spc->binaryDataArrayPtrs.push_back(c);
1127  spd->binaryDataArrayPtrs.push_back(d);
1128 
1129  sls_a->spectra.push_back(spa);
1130  sls_a->spectra.push_back(spc);
1131  sls_b->spectra.push_back(spb);
1132  sls_b->spectra.push_back(spc);
1133 
1134  shared_ptr<ChromatogramListSimple> cls_a(new ChromatogramListSimple);
1135  shared_ptr<ChromatogramListSimple> cls_b(new ChromatogramListSimple);
1136 
1137  ChromatogramPtr cpa(new Chromatogram);
1138  ChromatogramPtr cpb(new Chromatogram);
1139  ChromatogramPtr cpc(new Chromatogram);
1140  ChromatogramPtr cpd(new Chromatogram);
1141 
1142  cpa->binaryDataArrayPtrs.push_back(a);
1143  cpb->binaryDataArrayPtrs.push_back(b);
1144  cpc->binaryDataArrayPtrs.push_back(c);
1145  cpd->binaryDataArrayPtrs.push_back(d);
1146 
1147  cls_a->chromatograms.push_back(cpa);
1148  cls_a->chromatograms.push_back(cpc);
1149  cls_b->chromatograms.push_back(cpb);
1150  cls_b->chromatograms.push_back(cpd);
1151 
1152  run_a.spectrumListPtr = sls_a;
1153  run_b.spectrumListPtr = sls_b;
1154 
1155  run_a.chromatogramListPtr = cls_a;
1156  run_b.chromatogramListPtr = cls_b;
1157 
1158  // Run user param is written for both Spectrum and Chromatogram binary data array difference user params, if present, with the correct value (max of the Spectrum and Chromatogram user params over the SpectrumList/ ChromatogramList respectively)
1159 
1160  Diff<Run, DiffConfig> diff_run(run_a,run_b,config);
1161 
1162  // diff
1163 
1164  unit_assert(diff_run);
1165 
1166 
1167  // Run user params are set
1168 
1169  unit_assert(!diff_run.a_b.userParams.empty());
1170  unit_assert(!diff_run.b_a.userParams.empty());
1171 
1172 
1173  // and correctly
1174 
1175  double maxSpecList_a_b=boost::lexical_cast<double>(diff_run.a_b.userParam("Spectrum binary data array difference").value);
1176  double maxSpecList_b_a=boost::lexical_cast<double>(diff_run.b_a.userParam("Spectrum binary data array difference").value);
1177 
1178  double maxChrList_a_b=boost::lexical_cast<double>(diff_run.a_b.userParam("Chromatogram binary data array difference").value);
1179  double maxChrList_b_a=boost::lexical_cast<double>(diff_run.b_a.userParam("Chromatogram binary data array difference").value);
1180 
1181  unit_assert_equal(maxSpecList_a_b,.001,epsilon);
1182  unit_assert_equal(maxSpecList_b_a,.001,epsilon);
1183  unit_assert_equal(maxChrList_a_b,.001,epsilon);
1184  unit_assert_equal(maxChrList_b_a,.001,epsilon);
1185 
1186  // test that Spectrum UserParam is written upon finding a binary data diff, with the correct value
1187 
1188  // user params are set
1189  unit_assert(!diff_run.a_b.spectrumListPtr->spectrum(0)->userParams.empty());
1190  unit_assert(!diff_run.b_a.spectrumListPtr->spectrum(0)->userParams.empty()); //user params are set
1191 
1192  // and correctly
1193 
1194  double maxSpec_a_b=boost::lexical_cast<double>(diff_run.a_b.spectrumListPtr->spectrum(0)->userParam("Binary data array difference").value);
1195  double maxSpec_b_a=boost::lexical_cast<double>(diff_run.b_a.spectrumListPtr->spectrum(0)->userParam("Binary data array difference").value);
1196 
1197  unit_assert_equal(maxSpec_a_b,.001,epsilon);
1198  unit_assert_equal(maxSpec_b_a,.001,epsilon);
1199 
1200 
1201  // test that Chromatogram UserParam is written upon finding a binary data diff, with the correct value
1202 
1203  // user params are set
1204  unit_assert(!diff_run.a_b.chromatogramListPtr->chromatogram(0)->userParams.empty());
1205  unit_assert(!diff_run.b_a.chromatogramListPtr->chromatogram(0)->userParams.empty());
1206 
1207  // and correctly
1208 
1209  double maxChr_a_b=boost::lexical_cast<double>(diff_run.a_b.chromatogramListPtr->chromatogram(0)->userParam("Binary data array difference").value);
1210  double maxChr_b_a=boost::lexical_cast<double>(diff_run.b_a.chromatogramListPtr->chromatogram(0)->userParam("Binary data array difference").value);
1211 
1212  unit_assert_equal(maxChr_a_b,.001,epsilon);
1213  unit_assert_equal(maxChr_b_a,.001,epsilon);
1214 
1215  if(os_) *os_<<diff_run<<endl;
1216 
1217 
1218 
1219  // test that maxPrecisionDiff is being returned correctly for a zero diff within diff_impl::diff(SpectrumList, SpectrumList, SpectrumList, SpectrumList, DiffConfig)
1220 
1221  shared_ptr<SpectrumListSimple> sls_a_a(new SpectrumListSimple);
1222  shared_ptr<SpectrumListSimple> sls_A_A(new SpectrumListSimple);
1223 
1224  pwiz::data::diff_impl::diff(*sls_a, *sls_a,*sls_a_a,*sls_A_A,config);
1225  double maxPrecisionNonDiffSpec = getMaxPrecisionDiff(*sls_a_a);
1226  unit_assert_equal(maxPrecisionNonDiffSpec,0,epsilon);
1227 
1228 
1229  // test that maxPrecisionDiff is being returned correctly for a non-zero diff within diff_impl::diff(SpectrumList, SpectrumList, SpectrumList, SpectrumList, DiffConfig)
1230 
1231  shared_ptr<SpectrumListSimple> sls_a_b(new SpectrumListSimple);
1232  shared_ptr<SpectrumListSimple> sls_b_a(new SpectrumListSimple);
1233 
1234  pwiz::data::diff_impl::diff(*sls_a, *sls_b,*sls_a_b,*sls_b_a,config);
1235  double maxPrecisionDiffSpec = getMaxPrecisionDiff(*sls_a_b);
1236  unit_assert_equal(maxPrecisionDiffSpec,.001,epsilon);
1237 
1238 
1239  // test that maxPrecisionDiff is being returned correctly for a zero diff within diff_impl::diff(ChromatogramList, ChromatogramList, ChromatogramList, ChromatogramList, DiffConfig)
1240 
1241  shared_ptr<ChromatogramListSimple> cls_a_a(new ChromatogramListSimple);
1242  shared_ptr<ChromatogramListSimple> cls_A_A(new ChromatogramListSimple);
1243 
1244  pwiz::data::diff_impl::diff(*cls_a, *cls_a,*cls_a_a,*cls_A_A,config);
1245  double maxPrecisionNonDiffChr = getMaxPrecisionDiff(*cls_a_a);
1246  unit_assert_equal(maxPrecisionNonDiffChr,0,epsilon);
1247 
1248  // test that maxPrecisionDiff is being returned correctly for a non-zero diff within diff_impl::diff(ChromatogramList, ChromatogramList, ChromatogramList, ChromatogramList, DiffConfig)
1249 
1250  shared_ptr<ChromatogramListSimple> cls_a_b(new ChromatogramListSimple);
1251  shared_ptr<ChromatogramListSimple> cls_b_a(new ChromatogramListSimple);
1252 
1253  pwiz::data::diff_impl::diff(*cls_a,*cls_b,*cls_a_b,*cls_b_a,config);
1254  double maxPrecisionDiffChr = getMaxPrecisionDiff(*cls_a_b);
1255  unit_assert_equal(maxPrecisionDiffChr,.001,epsilon);
1256 
1257 
1258 }
1259 
1260 
1262 {
1263  if(os_) *os_<<"testMSDiffUpdate()"<<endl;
1264 
1265  MSData tiny1;
1266  MSData tiny2;
1267 
1268  examples::initializeTiny(tiny1);
1269  examples::initializeTiny(tiny2);
1270 
1271  Diff<MSData, DiffConfig> diff_initial(tiny1,tiny2);
1272  unit_assert(!diff_initial);
1273 
1274  //inflict metadata differences
1275 
1276  tiny1.id="ego";
1277  tiny1.run.id="superego";
1278 
1279  //inflict spectral differences
1280 
1281  SpectrumPtr tiny1_s0 = tiny1.run.spectrumListPtr->spectrum(0);
1282  SpectrumPtr tiny2_s1 = tiny2.run.spectrumListPtr->spectrum(1);
1283 
1284  tiny1_s0->id = "tiny1";
1285  tiny2_s1->id = "tiny2";
1286 
1287  //inflict chromatogram differences
1288 
1289  ChromatogramPtr tiny1_c0=tiny1.run.chromatogramListPtr->chromatogram(0);
1290 
1291  tiny1_c0->id="zumas";
1292 
1293  //test metadata, spectral, chromatogram differences
1294 
1295  Diff<MSData, DiffConfig> diff_changed(tiny1,tiny2);
1296  unit_assert(diff_changed);
1297 
1298  if(os_) *os_<<diff_changed<<endl;
1299 
1300  tiny1.run.spectrumListPtr.reset();
1301 
1302  Diff<MSData, DiffConfig> diff_changed_changed(tiny1,tiny2);
1303  unit_assert(diff_changed_changed);
1304 
1305  if(os_) *os_<<diff_changed_changed<<endl;
1306 }
1307 
1308 
1309 void test()
1310 {
1311  testFileContent();
1312  testSourceFile();
1314  testSample();
1315  testComponent();
1316  testSource();
1318  testSoftware();
1322  testScanSettings();
1323  testPrecursor();
1324  testProduct();
1325  testScan();
1326  testScanList();
1328  testSpectrum();
1329  testChromatogram();
1330  testSpectrumList();
1332  testRun();
1333  testMSData();
1336  testMSDiffUpdate();
1337 }
1338 
1339 
1340 int main(int argc, char* argv[])
1341 {
1342  TEST_PROLOG_EX(argc, argv, "_MSData")
1343 
1344  try
1345  {
1346  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
1347  test();
1348  }
1349  catch (exception& e)
1350  {
1351  TEST_FAILED(e.what())
1352  }
1353  catch (...)
1354  {
1355  TEST_FAILED("Caught unknown exception.")
1356  }
1357 
1358  TEST_EPILOG
1359 }
1360