ProteoWizard
SpectrumList_MetadataFixerTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: SpectrumList_MetadataFixerTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers <a.t> vanderbilt.edu>
6 //
7 // Copyright 2009 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
27 
28 using namespace pwiz::util;
29 using namespace pwiz::msdata;
30 using namespace pwiz::analysis;
31 
32 ostream* os_ = 0;
33 
34 
36 {
37  // space-delimited doubles
38  const char* inputMZArray;
39  const char* inputIntensityArray;
40 
41  double tic;
42  double bpi;
43  double bpmz;
44 };
45 
47 {
48  { "1 2 3 4 5", "10 20 30 40 50", 150, 50, 5 },
49  { "1 2 3 4 5", "50 40 30 20 10", 150, 50, 1 },
50  { "1 2 3 4 5", "10 20 30 20 10", 90, 30, 3 },
51  { "1", "10", 10, 10, 1 }
52 };
53 
55 
56 vector<double> parseDoubleArray(const string& doubleArray)
57 {
58  vector<double> doubleVector;
59  vector<string> tokens;
60  bal::split(tokens, doubleArray, bal::is_space());
61  if (!tokens.empty() && !tokens[0].empty())
62  for (size_t i=0; i < tokens.size(); ++i)
63  doubleVector.push_back(lexical_cast<double>(tokens[i]));
64  return doubleVector;
65 }
66 
67 void test()
68 {
69  for (size_t i=0; i < testMetadataFixersSize; ++i)
70  {
72  SpectrumListPtr originalList(sl);
73 
74  // test once with no metadata
75  SpectrumPtr s0(new Spectrum);
76  sl->spectra.push_back(s0);
77 
79 
80  // test again with existing metadata to be overwritten
81  SpectrumPtr s1(new Spectrum);
82  s1->set(MS_TIC, t.tic + 1);
83  s1->set(MS_base_peak_intensity, t.bpi + 1);
84  s1->set(MS_base_peak_m_z, t.bpmz + 1);
85  sl->spectra.push_back(s1);
86 
87  vector<double> inputMZArray = parseDoubleArray(t.inputMZArray);
88  vector<double> inputIntensityArray = parseDoubleArray(t.inputIntensityArray);
89  s0->setMZIntensityArrays(inputMZArray, inputIntensityArray, MS_number_of_counts);
90  s1->setMZIntensityArrays(inputMZArray, inputIntensityArray, MS_number_of_counts);
91 
92  SpectrumListPtr fixer(new SpectrumList_MetadataFixer(originalList));
93 
94  SpectrumPtr fixedSpectrum = fixer->spectrum(0, true);
95  unit_assert(fixedSpectrum->cvParam(MS_TIC).valueAs<double>() == t.tic);
96  unit_assert(fixedSpectrum->cvParam(MS_base_peak_intensity).valueAs<double>() == t.bpi);
97  unit_assert(fixedSpectrum->cvParam(MS_base_peak_m_z).valueAs<double>() == t.bpmz);
98 
99  fixedSpectrum = fixer->spectrum(1, true);
100  unit_assert(fixedSpectrum->cvParam(MS_TIC).valueAs<double>() == t.tic);
101  unit_assert(fixedSpectrum->cvParam(MS_base_peak_intensity).valueAs<double>() == t.bpi);
102  unit_assert(fixedSpectrum->cvParam(MS_base_peak_m_z).valueAs<double>() == t.bpmz);
103  }
104 }
105 
106 int main(int argc, char* argv[])
107 {
108  TEST_PROLOG(argc, argv)
109 
110  try
111  {
112  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
113  test();
114  }
115  catch (exception& e)
116  {
117  TEST_FAILED(e.what())
118  }
119  catch (...)
120  {
121  TEST_FAILED("Caught unknown exception.")
122  }
123 
125 }