ProteoWizard
MSDataTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: MSDataTest.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 
24 #include "MSData.hpp"
27 
28 using namespace pwiz::util;
29 using namespace pwiz::data;
30 using namespace pwiz::msdata;
31 
32 
34 {
35  // fill in SpectrumListSimple
36 
37  shared_ptr<SpectrumListSimple> spectrumListSimple(new SpectrumListSimple);
38 
39  unit_assert(spectrumListSimple->empty());
40  spectrumListSimple->dp = DataProcessingPtr(new DataProcessing("dp"));
41  unit_assert(!spectrumListSimple->empty());
42 
43  SpectrumPtr spectrum0(new Spectrum);
44  spectrum0->index = 0;
45  spectrum0->id = "scan=1";
46 
47  // add m/z values 0,...,9
49  for (unsigned int i=0; i<10; i++) bd_mz->data.push_back(i);
50  bd_mz->cvParams.push_back(MS_m_z_array);
51  double* buffer = &bd_mz->data[0];
52 
53  // add intensity values 10,...,1
54  BinaryDataArrayPtr bd_intensity(new BinaryDataArray);
55  for (unsigned int i=0; i<10; i++) bd_intensity->data.push_back(10-i);
56  bd_intensity->cvParams.push_back(MS_intensity_array);
57 
58  spectrum0->binaryDataArrayPtrs.push_back(bd_mz);
59  spectrum0->binaryDataArrayPtrs.push_back(bd_intensity);
60  spectrum0->defaultArrayLength = 10;
61 
62  SpectrumPtr spectrum1(new Spectrum);
63  spectrum1->index = 1;
64  spectrum1->id = "scan=2";
65  spectrum1->cvParams.push_back(MS_MSn_spectrum);
66  spectrum1->cvParams.push_back(CVParam(MS_ionization_type, 420));
67 
68  spectrumListSimple->spectra.push_back(spectrum0);
69  spectrumListSimple->spectra.push_back(spectrum1);
70 
71  // let an MSData object hold onto it as a SpectrumListPtr
72 
73  MSData data;
74  data.run.spectrumListPtr = spectrumListSimple;
75 
76  // test SpectrumList interface
77 
78  // verify index()
79  const SpectrumList& spectrumList = *data.run.spectrumListPtr;
80  unit_assert(spectrumList.size() == 2);
81  unit_assert(spectrumList.find("scan=1") == 0);
82  unit_assert(spectrumList.find("scan=2") == 1);
83 
84  // verify findNameValue
85 
86  IndexList result = spectrumList.findNameValue("scan", "1");
87  unit_assert(result.size()==1 && result[0]==0);
88 
89  result = spectrumList.findNameValue("scan", "2");
90  unit_assert(result.size()==1 && result[0]==1);
91 
92  // verify spectrumIdentity()
93 
94  const SpectrumIdentity& identity0 = spectrumList.spectrumIdentity(0);
95  unit_assert(identity0.index == spectrum0->index);
96  unit_assert(identity0.id == spectrum0->id);
97 
98  const SpectrumIdentity& identity1 = spectrumList.spectrumIdentity(1);
99  unit_assert(identity1.index == spectrum1->index);
100  unit_assert(identity1.id == spectrum1->id);
101 
102  // verify spectrum 0
103  SpectrumPtr spectrum = spectrumList.spectrum(0);
104  unit_assert(spectrum->index == spectrum0->index);
105  unit_assert(spectrum->id == spectrum0->id);
106 
107  // verify no extra copying of binary data arrays
108  unit_assert(spectrum->binaryDataArrayPtrs.size() == 2);
109  unit_assert(&(spectrum->binaryDataArrayPtrs[0]->data[0]) == buffer);
110 
111  // verify getMZIntensityPairs()
112 
113  unit_assert(spectrum->binaryDataArrayPtrs[0]->hasCVParam(MS_m_z_array) == true);
114  unit_assert(spectrum->binaryDataArrayPtrs[1]->hasCVParam(MS_intensity_array) == true);
115 
116  vector<MZIntensityPair> mziPairs;
117  spectrum->getMZIntensityPairs(mziPairs);
118  unit_assert(mziPairs.size() == 10);
119 
120  vector<double> doubleArray;
121  unit_assert(spectrum->defaultArrayLength == 10);
122  doubleArray.resize(spectrum->defaultArrayLength*2);
123  spectrum->getMZIntensityPairs(reinterpret_cast<MZIntensityPair*>(&doubleArray[0]),
124  spectrum->defaultArrayLength);
125 
126  for (unsigned int i=0; i<10; i++)
127  {
128  const MZIntensityPair& p = mziPairs[i];
129  unit_assert(p.mz == i);
130  unit_assert(p.intensity == 10-i);
131  unit_assert(doubleArray[2*i] == i);
132  unit_assert(doubleArray[2*i+1] == 10-i);
133  }
134 
135  // verify setMZIntensityPairs()
136  spectrum->binaryDataArrayPtrs.clear();
137  unit_assert(spectrum->binaryDataArrayPtrs.empty());
138  vector<MZIntensityPair> mziPairs2;
139  for (unsigned int i=0; i<10; i++)
140  mziPairs2.push_back(MZIntensityPair(2*i, 3*i));
141  spectrum->setMZIntensityPairs(mziPairs2, MS_number_of_counts);
142  unit_assert(spectrum->binaryDataArrayPtrs.size() == 2);
143  unit_assert(spectrum->binaryDataArrayPtrs[0]->hasCVParam(MS_m_z_array) == true);
144  unit_assert(spectrum->binaryDataArrayPtrs[1]->hasCVParam(MS_intensity_array) == true);
145  unit_assert(spectrum->binaryDataArrayPtrs[1]->cvParam(MS_intensity_array).units == MS_number_of_counts);
146  unit_assert(spectrum->binaryDataArrayPtrs[0]->data.size() == 10);
147  unit_assert(spectrum->binaryDataArrayPtrs[1]->data.size() == 10);
148  for (unsigned int i=0; i<10; i++)
149  unit_assert(spectrum->binaryDataArrayPtrs[0]->data[i] == 2*i &&
150  spectrum->binaryDataArrayPtrs[1]->data[i] == 3*i);
151 
152  // verify spectrum 1
153  spectrum = spectrumList.spectrum(1);
154  unit_assert(spectrum->index == spectrum1->index);
155  unit_assert(spectrum->id == spectrum1->id);
156 
157  // verify DataProcessingPtr
158 
159  unit_assert(spectrumList.dataProcessingPtr().get() &&
160  spectrumList.dataProcessingPtr()->id == "dp");
161 }
162 
163 
165 {
167 
168  for (int i=0; i<3; i++)
169  {
170  vector<TimeIntensityPair> pairs;
171  for (int j=0; j<10; j++) pairs.push_back(TimeIntensityPair(j, 10*i+j));
172  cls.chromatograms.push_back(ChromatogramPtr(new Chromatogram));
173  cls.chromatograms.back()->setTimeIntensityPairs(pairs, UO_second, MS_number_of_counts);
174  }
175 
176  DataProcessingPtr dp(new DataProcessing("dp"));
177  cls.dp = dp;
178 
179  ChromatogramList& cl = cls;
180 
181  unit_assert(cl.size() == 3);
182 
183  for (size_t i=0; i<3; i++)
184  {
185  vector<TimeIntensityPair> result;
186  cl.chromatogram(i)->getTimeIntensityPairs(result);
187  unit_assert(result.size() == 10);
188  for (size_t j=0; j<10; j++)
189  unit_assert(result[j].time==j && result[j].intensity==10*i+j);
190  }
191 
192  unit_assert(cl.dataProcessingPtr().get() &&
193  cl.dataProcessingPtr()->id == "dp");
194 }
195 
196 
198 {
199  string id = "hair=blue favorite=420 age=36.175 upsideDown=1";
200 
201  map<string,string> parsedID = id::parse(id);
202  unit_assert(parsedID.size() == 4);
203 
204  unit_assert(id::value(id, "hair") == "blue");
205  unit_assert(id::valueAs<int>(id, "favorite") == 420);
206  unit_assert_equal(id::valueAs<double>(id, "age"), 36.175, 1e-6);
207  unit_assert(id::valueAs<bool>(id, "upsideDown") == true);
208 
209  unit_assert(id::abbreviate(id) == "blue.420.36.175.1");
210  unit_assert(id::abbreviate(id, ',') == "blue,420,36.175,1");
211 
212  id = "controllerType=0 controllerNumber=1 scan=123";
217  unit_assert(id::abbreviate(id) == "0.1.123");
218  unit_assert(id::abbreviate(id, ',') == "0,1,123");
219 
221 
222  id = "spectrum=123";
226 
227  id = "scan=123";
236  unit_assert(id::abbreviate(id) == "123");
237  unit_assert(id::abbreviate(id, ',') == "123");
238 
239  id = "sample=1 period=2 cycle=123 experiment=3";
242  unit_assert(id::abbreviate(id) == "1.2.123.3");
243  unit_assert(id::abbreviate(id, ',') == "1,2,123,3");
244 }
245 
246 
248 {
249  MSData msd;
251  msd.run.spectrumListPtr = sl;
252 
253  DataProcessingPtr realDeal(new DataProcessing("dp"));
254  DataProcessingPtr poser(new DataProcessing("dp"));
255 
256  msd.dataProcessingPtrs.push_back(realDeal);
257  sl->dp = poser;
258 
259  // test allDataProcessingPtrs()
260  vector<DataProcessingPtr> all = msd.allDataProcessingPtrs();
261  unit_assert_operator_equal(1, all.size());
262  unit_assert_operator_equal(realDeal, all[0]);
263 }
264 
265 
266 int main(int argc, const char* argv[])
267 {
268  TEST_PROLOG(argc, argv)
269 
270  try
271  {
274  testIDParsing();
276  }
277  catch (exception& e)
278  {
279  TEST_FAILED(e.what())
280  }
281  catch (...)
282  {
283  TEST_FAILED("Caught unknown exception.")
284  }
285 
287 }
288 
289