ProteoWizard
MSDataCacheTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: MSDataCacheTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
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 
24 #include "MSDataCache.hpp"
29 #include <cstring>
30 
31 
32 using namespace pwiz;
33 using namespace pwiz::util;
34 using namespace pwiz::analysis;
35 
36 
37 ostream* os_ = 0;
38 const double epsilon_ = 1e-6;
39 
40 
42 {
43  if (os_) *os_ << "testMetadata()\n";
44 
45  if (os_) *os_ << "spectrumCount: " << cache.size() << endl;
46  unit_assert(cache.size() == 5);
47 
48  unit_assert(cache[0].index == 0);
49  unit_assert(cache[0].id == "scan=19");
50  unit_assert(cache[0].scanNumber == 19); // TODO: change to nativeID
51  unit_assert(cache[0].massAnalyzerType == MS_QIT);
52  unit_assert(cache[0].msLevel == 1);
53  unit_assert_equal(cache[0].retentionTime, 353.43, epsilon_);
54  unit_assert_equal(cache[0].mzLow, 400.39, epsilon_);
55  unit_assert_equal(cache[0].mzHigh, 1795.56, epsilon_);
56  unit_assert(cache[0].precursors.empty());
57 
58  unit_assert(cache[1].index == 1);
59  unit_assert(cache[1].id == "scan=20");
60  unit_assert(cache[1].scanNumber == 20); // TODO: change to nativeID
61  unit_assert(cache[1].massAnalyzerType == MS_QIT);
62  unit_assert(cache[1].msLevel == 2);
63  unit_assert_equal(cache[1].retentionTime, 359.43, epsilon_);
64  unit_assert_equal(cache[1].mzLow, 320.39, epsilon_);
65  unit_assert_equal(cache[1].mzHigh, 1003.56, epsilon_);
66  unit_assert(cache[1].precursors.size() == 1);
67  unit_assert(cache[1].precursors[0].index == 0);
68  unit_assert_equal(cache[1].precursors[0].mz, 445.34, epsilon_);
69  unit_assert_equal(cache[1].precursors[0].intensity, 120053, epsilon_);
70  unit_assert(cache[1].precursors[0].charge == 2);
71 
72  if (os_) *os_ << endl;
73 }
74 
75 
77 {
78  MSData tiny;
80 
81  MSDataCache cache;
82 
83  cache.open(tiny);
84 
85  cache.update(tiny, *tiny.run.spectrumListPtr->spectrum(0));
86  unit_assert(!cache[0].data.empty());
87  unit_assert(cache[1].data.empty());
88 
89  cache.update(tiny, *tiny.run.spectrumListPtr->spectrum(1));
90  unit_assert(cache[0].data.empty());
91  unit_assert(!cache[1].data.empty());
92 
93  testMetadata(cache);
94 }
95 
96 
97 void printCache(ostream& os, const MSDataCache& cache)
98 {
99  os << "cached binary data:\n";
100  for (vector<SpectrumInfo>::const_iterator it=cache.begin(); it!=cache.end(); ++it)
101  {
102  os << it->index << " "
103  << it->data.size() << "/"
104  << it->data.capacity() << endl;
105  }
106  os << endl;
107 }
108 
109 
110 void testMRU()
111 {
112  if (os_) *os_ << "testMRU()\n";
113 
114  vector<MZIntensityPair> pairs(100);
115 
117  for (size_t i=0; i<10; i++)
118  {
119  SpectrumPtr spectrum(new Spectrum);
120  spectrum->setMZIntensityPairs(pairs, MS_number_of_counts);
121  spectrum->index = i;
122  spectrum->id = "scan=" + lexical_cast<string>(i);
123  sl->spectra.push_back(spectrum);
124  }
125 
126  MSData msd;
127  msd.run.spectrumListPtr = sl;
128 
129  MSDataCache::Config config;
130  config.binaryDataCacheSize = 3;
131  MSDataCache cache(config);
132 
133  cache.open(msd);
134 
135  if (os_) *os_ << "update: 0 1 2\n";
136  cache.update(msd, *sl->spectrum(0, true));
137  cache.update(msd, *sl->spectrum(1, true));
138  cache.update(msd, *sl->spectrum(2, true));
139  if (os_) printCache(*os_, cache); // mru: 2 1 0
140 
141  unit_assert(cache[0].data.size() == 100);
142  unit_assert(cache[1].data.size() == 100);
143  unit_assert(cache[2].data.size() == 100);
144  unit_assert(cache[3].data.size() == 0);
145 
146  if (os_) *os_ << "update: 3\n";
147  cache.update(msd, *sl->spectrum(3, true));
148  if (os_) printCache(*os_, cache); // mru: 3 2 1
149 
150  unit_assert(cache[0].data.capacity() == 0);
151  unit_assert(cache[1].data.size() == 100);
152  unit_assert(cache[2].data.size() == 100);
153  unit_assert(cache[3].data.size() == 100);
154 
155  if (os_) *os_ << "update: 1\n";
156  cache.update(msd, *sl->spectrum(1, true));
157  if (os_) printCache(*os_, cache); // mru: 1 3 2
158 
159  unit_assert(cache[0].data.capacity() == 0);
160  unit_assert(cache[1].data.size() == 100);
161  unit_assert(cache[2].data.size() == 100);
162  unit_assert(cache[3].data.size() == 100);
163 
164  if (os_) *os_ << "update: 4\n";
165  cache.update(msd, *sl->spectrum(4, true));
166  if (os_) printCache(*os_, cache); // mru: 4 1 3
167 
168  unit_assert(cache[0].data.capacity() == 0);
169  unit_assert(cache[1].data.size() == 100);
170  unit_assert(cache[2].data.capacity() == 0);
171  unit_assert(cache[3].data.size() == 100);
172  unit_assert(cache[3].data.size() == 100);
173 
174  if (os_) *os_ << endl;
175 }
176 
177 
179 {
180  virtual UpdateRequest updateRequested(const DataInfo& dataInfo,
181  const SpectrumIdentity& spectrumIdentity) const
182  {
183  return (spectrumIdentity.index%2==0) ? UpdateRequest_NoBinary : UpdateRequest_None;
184  }
185 };
186 
187 
189 {
190  if (os_) *os_ << "testUpdateRequest()\n";
191 
192  vector<MZIntensityPair> pairs(100);
193 
195  for (size_t i=0; i<10; i++)
196  {
197  SpectrumPtr spectrum(new Spectrum);
198  spectrum->setMZIntensityPairs(pairs, MS_number_of_counts);
199  spectrum->index = i;
200  spectrum->id = "scan=" + lexical_cast<string>(i);
201  sl->spectra.push_back(spectrum);
202  }
203 
204  MSData msd;
205  msd.run.spectrumListPtr = sl;
206 
207  MSDataAnalyzerContainer analyzers;
208  shared_ptr<MSDataCache> cache(new MSDataCache);
209  analyzers.push_back(cache);
210  analyzers.push_back(MSDataAnalyzerPtr(new EvenRequester));
211 
212  MSDataAnalyzerDriver driver(analyzers);
213  driver.analyze(msd);
214 
215  for (size_t i=0, end=cache->size(); i<end; i++)
216  {
217  const SpectrumInfo& info = cache->at(i);
218  if (os_) *os_ << info.index << " " << info.id << endl;
219 
220  // cache has only been updated with the spectra requested by EvenRequester
221 
222  unit_assert(i%2==0 && info.index==i && info.id=="scan="+lexical_cast<string>(i) ||
223  i%2==1 && info.index==(size_t)-1&& info.id.empty());
224  }
225 
226  if (os_) *os_ << endl;
227 }
228 
229 
231 {
232  if (os_) *os_ << "testAutomaticUpdate()\n";
233 
234  vector<MZIntensityPair> pairs(100);
235 
237  for (size_t i=0; i<10; i++)
238  {
239  SpectrumPtr spectrum(new Spectrum);
240  spectrum->setMZIntensityPairs(pairs, MS_number_of_counts);
241  spectrum->index = i;
242  spectrum->id = "scan=" + lexical_cast<string>(i);
243  sl->spectra.push_back(spectrum);
244  }
245 
246  MSData msd;
247  msd.run.spectrumListPtr = sl;
248 
249  MSDataCache cache;
250  cache.open(msd);
251 
252  unit_assert(cache.size() == sl->size());
253  for (size_t i=0; i<cache.size(); i++)
254  unit_assert(cache[i].index == (size_t)-1);
255 
256  const SpectrumInfo& info5 = cache.spectrumInfo(5, true);
257  unit_assert(cache[5].data.size() == 100);
258 
259  cache.spectrumInfo(7); // getBinaryData==false -> doesn't change cached binary data
260  unit_assert(cache[5].data.size() == 100);
261  unit_assert(cache[7].data.size() == 0);
262 
263  const SpectrumInfo& info7 = cache.spectrumInfo(7, true);
264 
265  if (os_)
266  {
267  for (size_t i=0; i<cache.size(); i++)
268  *os_ << i << " " << cache[i].index << " " << cache[i].id << " "
269  << cache[i].data.size() << endl;
270  }
271 
272  unit_assert(info7.data.size() == 100);
273  unit_assert(info5.data.size() == 0);
274 
275  unit_assert(info5.index==5 && info5.id=="scan=5");
276  unit_assert(cache[5].index==5 && cache[5].id=="scan=5");
277  unit_assert(info7.index==7 && info7.id=="scan=7");
278  unit_assert(cache[7].index==7 && cache[7].id=="scan=7");
279 
280  for (size_t i=0; i<cache.size(); i++)
281  if (i!=5 && i!=7)
282  unit_assert(cache[i].index == (size_t)-1);
283 }
284 
285 
286 int main(int argc, char* argv[])
287 {
288  TEST_PROLOG(argc, argv)
289 
290  try
291  {
292  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
293  testDefault();
294  testMRU();
297  }
298  catch (exception& e)
299  {
300  TEST_FAILED(e.what())
301  }
302  catch (...)
303  {
304  TEST_FAILED("Caught unknown exception.")
305  }
306 
308 }
309