ProteoWizard
LocalMaximumPeakDetectorTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: LocalMaximumPeakDetectorTest.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 2008 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 
27 
28 using namespace pwiz::util;
29 using namespace pwiz::analysis;
30 
31 
32 ostream* os_ = 0;
33 
34 
35 struct TestData
36 {
37  size_t windowSize;
38  const char* xRaw;
39  const char* yRaw;
40  const char* xPeakValues;
41  const char* yPeakValues;
42 };
43 
44 const TestData testData[] =
45 {
46  {
47  3,
48  "1 2 3 4 5 6 7 8 9",
49  "0 1 0 1 2 3 0 0 4",
50  "2 6 9",
51  "1 3 4"
52  },
53 
54  {
55  5,
56  "1 2 3 4 5 6 7 8 9",
57  "0 1 0 1 2 3 0 0 4",
58  "2 6 9",
59  "1 3 4"
60  },
61 
62  {
63  7,
64  "1 2 3 4 5 6 7 8 9",
65  "0 1 0 1 2 3 0 0 4",
66  "9",
67  "4"
68  }
69 };
70 
71 const size_t testDataSize = sizeof(testData) / sizeof(TestData);
72 
73 
74 vector<double> parseDoubleArray(const string& doubleArray)
75 {
76  vector<double> doubleVector;
77  vector<string> tokens;
78  bal::split(tokens, doubleArray, bal::is_space(), bal::token_compress_on);
79  if (!tokens.empty() && !tokens[0].empty())
80  for (size_t i=0; i < tokens.size(); ++i)
81  doubleVector.push_back(lexical_cast<double>(tokens[i]));
82  return doubleVector;
83 }
84 
85 
86 void test()
87 {
88  for (size_t i=0; i < testDataSize; ++i)
89  {
90  const TestData& data = testData[i];
91 
92  vector<double> xRaw = parseDoubleArray(data.xRaw);
93  vector<double> yRaw = parseDoubleArray(data.yRaw);
94  vector<double> target_xPeakValues = parseDoubleArray(data.xPeakValues);
95  vector<double> target_yPeakValues = parseDoubleArray(data.yPeakValues);
96 
97  // sanity checks
98  unit_assert(xRaw.size() == yRaw.size());
99  unit_assert(target_xPeakValues.size() == target_yPeakValues.size());
100 
101  LocalMaximumPeakDetector peakDetector(data.windowSize);
102  vector<double> xPeakValues, yPeakValues;
103  peakDetector.detect(xRaw, yRaw, xPeakValues, yPeakValues);
104 
105  unit_assert(xPeakValues.size() == target_xPeakValues.size());
106 
107  for (size_t j=0; j < xPeakValues.size(); ++j)
108  {
109  unit_assert_equal(xPeakValues[j], target_xPeakValues[j], 1e-5);
110  unit_assert_equal(yPeakValues[j], target_yPeakValues[j], 1e-5);
111  }
112  }
113 }
114 
115 
116 int main(int argc, char* argv[])
117 {
118  TEST_PROLOG(argc, argv)
119 
120  try
121  {
122  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
123  test();
124  }
125  catch (exception& e)
126  {
127  TEST_FAILED(e.what())
128  }
129  catch (...)
130  {
131  TEST_FAILED("Caught unknown exception.")
132  }
133 
135 }