ProteoWizard
FrequencyDataTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: FrequencyDataTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2006 Louis Warschaw Prostate Cancer Center
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 "FrequencyData.hpp"
30 #include <boost/filesystem/operations.hpp>
31 
32 
33 using namespace pwiz::util;
34 using namespace pwiz::data;
35 
36 
37 ostream* os_;
38 
39 
40 void diff(const string& filename1, const string& filename2)
41 {
42  ifstream file1(filename1.c_str()), file2(filename2.c_str());
43  string line1, line2;
44  while (getline(file1, line1) && getline(file2, line2))
45  unit_assert(line1 == line2);
46  if (os_) *os_ << "diff " << filename1 << " " << filename2 << ": success\n";
47 }
48 
49 
50 string filename1 = "FrequencyDataTest.output1.txt";
51 
52 
53 void test()
54 {
55  // create some data, f(x) = abs(5-(x-2))
56  FrequencyData fd;
57  FrequencyData::container& data = fd.data();
58  for (int i=-5; i<=5; i++)
59  data.push_back(FrequencyDatum(i+2, 5-abs(i)));
60  fd.analyze(); // recache after changing data
61 
62  // verify peak()
64  unit_assert(max->x == 2);
65  unit_assert(max->y == 5.);
66 
67  // verify stats
68  unit_assert(fd.mean() == 25./11);
69  unit_assert(fd.meanSquare() == 85./11);
70  unit_assert(fd.sumSquares() == 85.);
71  unit_assert_equal(fd.variance(), 85./11 - 25.*25/11/11, 1e-12);
72 
73  // write out data
74  if (os_) *os_ << "Writing " << filename1 << endl;
76 
77  // read into const FrequencyData
78  string filename2 = "FrequencyDataTest.output2.txt";
80 
81  // verify normalize()
82  fd2.normalize();
83  unit_assert(fd2.shift() == -2);
84  unit_assert(fd2.scale() == 1./5);
85  max = fd2.max();
86  unit_assert(max->x == 0);
87  unit_assert(max->y == 1.);
88 
89  // verify transform(shift, scale)
90  fd2.transform(-fd2.shift(), 1./fd2.scale());
91 
92  // verify read/write
93  if (os_) *os_ << "Writing " << filename2 << endl;
94  fd2.write(filename2, FrequencyData::Text);
95  diff(filename1, filename2);
96 
97  // test subrange
98  string filename3 = "FrequencyDataTest.output3.txt";
99  FrequencyData fd3(fd2, fd2.data().begin(), fd2.max()); // copy first half
100  if (os_) *os_ << "Writing " << filename3 << endl;
101  fd3.write(filename3, FrequencyData::Text);
102  FrequencyData fd4(fd2, fd2.max(), fd2.data().end()); // copy second half
103  ofstream os(filename3.c_str(), ios::app);
104  fd4.write(os, FrequencyData::Text);
105  os.close();
106  diff(filename1, filename3);
107 
108  // read/write binary, and metadata
109  fd.scanNumber(555);
110  fd.retentionTime(444);
112  fd.observationDuration(666);
113  fd.noiseFloor(777);
114  string filename4a = "FrequencyDataTest.output4a.txt";
115  if (os_) *os_ << "Writing " << filename4a << endl;
116  fd.write(filename4a, FrequencyData::Text);
117  string filenameBinary1 = "FrequencyDataTest.output1.cfd";
118  if (os_) *os_ << "Writing " << filenameBinary1 << endl;
119  fd.write(filenameBinary1);
120 
121  FrequencyData fd5(filenameBinary1);
122  unit_assert(fd5.observationDuration() == 666);
124  unit_assert(fd5.scanNumber() == 555);
125  unit_assert(fd5.retentionTime() == 444);
126  unit_assert(fd5.observationDuration() == 1);
127  unit_assert(fd5.noiseFloor() == 777);
128  if (os_) *os_ << "Calibration: " << fd5.calibrationParameters().A << " " << fd5.calibrationParameters().B << endl;
129 
130  string filename4b = "FrequencyDataTest.output4b.txt";
131  if (os_) *os_ << "Writing " << filename4b << endl;
132  fd5.write(filename4b, FrequencyData::Text);
133  diff(filename4a, filename4b);
135 
136  // test window
137  FrequencyData window1(fd, data.begin()+1, 2);
138  FrequencyData window2(fd, fd.max(), 1);
139  FrequencyData window3(fd, data.end()-2, 2);
140  string filename5 = "FrequencyDataTest.output5.txt";
141  if (os_) *os_ << "Writing " << filename5 << endl;
142  ofstream os5(filename5.c_str());
143  window1.write(os5, FrequencyData::Text);
144  window2.write(os5, FrequencyData::Text);
145  window3.write(os5, FrequencyData::Text);
146  os5.close();
147  diff(filename1, filename5);
148 }
149 
150 
151 void testFind()
152 {
153  const FrequencyData fd(filename1);
154 
156  unit_assert(it!=fd.data().end() && it->x==0);
157 
158  it = fd.findNearest(.2);
159  unit_assert(it!=fd.data().end() && it->x==0);
160 
161  it = fd.findNearest(6.1);
162  unit_assert(it!=fd.data().end() && it->x==6);
163 
164  it = fd.findNearest(7.1);
165  unit_assert(it!=fd.data().end() && it->x==7);
166 
167  it = fd.findNearest(666);
168  unit_assert(it!=fd.data().end() && it->x==7);
169 
170  it = fd.findNearest(-666);
171  unit_assert(it==fd.data().begin());
172 }
173 
174 
176 {
179 
180  fd += fd;
181 
182  for (FrequencyData::const_iterator it=fd.data().begin(), jt=fd2.data().begin();
183  it!=fd.data().end();
184  ++it, ++jt)
185  unit_assert(it->y == 2.*jt->y);
186 
187  fd2.transform(0, -2.);
188  fd += fd2;
189 
190  for (FrequencyData::const_iterator it=fd.data().begin(); it!=fd.data().end(); ++it)
191  unit_assert(it->y == 0.);
192 }
193 
194 
196 {
198  if (os_) *os_ << "variance: " << fd.variance() << endl;
199  if (os_) *os_ << "noiseFloor: " << fd.noiseFloor() << endl;
200 }
201 
202 
204 {
205  if (os_) *os_ << "Deleting FrequencyDataTest.output*.txt\n";
206  vector<bfs::path> filepaths;
207  expand_pathmask("FrequencyDataTest.output*.*", filepaths);
208  for (size_t i=0; i < filepaths.size(); ++i)
209  boost::filesystem::remove(filepaths[i]);
210 }
211 
212 
214 {
215  if (os_) *os_ << "testNoiseFloorVarianceCalculation()\n";
216  if (os_) *os_ << setprecision(10);
217 
218  // test noise floor calculation on sample frequency data
219 
220  string filename = "FrequencyDataTest.cfd.temp.txt";
221  ofstream temp(filename.c_str());
222  temp << sampleFrequencyData_;
223  temp.close();
224 
225  FrequencyData fd(filename);
226  boost::filesystem::remove(filename);
227 
228  double result = fd.cutoffNoiseFloor();
229  if (os_) *os_ << "result: " << result << endl;
230  unit_assert_equal(result, 29000, 1000);
231 
232  // test noise floor calculation on sample mass data
233 
234  FrequencyData fdMasses;
235  CalibrationParameters cp = CalibrationParameters::thermo_FT();
236 
238  fdMasses.data().push_back(FrequencyDatum(cp.frequency(p->mz), p->intensity));
239  fdMasses.analyze();
240 
241  double result2 = fdMasses.cutoffNoiseFloor();
242  if (os_) *os_ << "result2: " << result2 << endl;
243  unit_assert_equal(result2, 6000, 1000);
244 }
245 
246 
247 int main(int argc, char* argv[])
248 {
249  TEST_PROLOG(argc, argv)
250 
251  try
252  {
253  if (argc>1 && !strcmp(argv[1],"-v")) // verbose
254  os_ = &cout;
255 
256  if (os_) *os_ << "FrequencyDataTest\n";
257 
258  test();
259  testFind();
260  testAddition();
261  testNoiseFloor();
262  cleanTests();
264 
265  if (os_) *os_ << "success\n";
266  }
267  catch (exception& e)
268  {
269  TEST_FAILED(e.what())
270  }
271  catch (...)
272  {
273  TEST_FAILED("Caught unknown exception.")
274  }
275 
277 }
278