ProteoWizard
Serializer_MSn_Test.cpp
Go to the documentation of this file.
1 //
2 // $Id: Serializer_MSn_Test.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Barbara Frewen <frewen@u.washington.edu>
6 //
7 // Copyright 2008 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 #include "Serializer_MSn.hpp"
23 #include "Serializer_mzML.hpp"
24 #include "Diff.hpp"
25 #include "TextWriter.hpp"
27 #include "boost/iostreams/positioning.hpp"
29 #include <cstring>
30 
31 
32 using namespace pwiz::util;
33 using namespace pwiz::msdata;
34 
35 
36 ostream* os_ = 0;
37 
38 
40 {
42  fc.set(MS_MSn_spectrum);
44 
45  SourceFilePtr sourceFile(new SourceFile);
46  sourceFile->set(MS_scan_number_only_nativeID_format);
47  sourceFile->set(MS_MS2_file);
48  msd.fileDescription.sourceFilePtrs.push_back(sourceFile);
49 
50  shared_ptr<SpectrumListSimple> spectrumList(new SpectrumListSimple);
51  msd.run.spectrumListPtr = spectrumList;
52  spectrumList->spectra.push_back(SpectrumPtr(new Spectrum));
53 
54  Spectrum& s20 = *spectrumList->spectra[0];
55  s20.id = "scan=1";
56  s20.index = 0;
57 
58  s20.set(MS_MSn_spectrum);
59  s20.set(MS_ms_level, 1);
60 
64  s20.set(MS_base_peak_m_z, 0);
65  s20.set(MS_base_peak_intensity, 20);
66  s20.set(MS_total_ion_current, 110);
67 
68  s20.scanList.scans.push_back(Scan());
69  Scan& s20scan = s20.scanList.scans.back();
70  s20scan.set(MS_scan_start_time, 4, UO_second);
71 
72  s20.setMZIntensityArrays(vector<double>(), vector<double>(), MS_number_of_counts);
73  vector<double>& s20_mz = s20.getMZArray()->data;
74  vector<double>& s20_intensity = s20.getIntensityArray()->data;
75 
76  for (int i=0; i<10; i++)
77  s20_mz.push_back(i*2);
78 
79  for (int i=0; i<10; i++)
80  s20_intensity.push_back((10-i)*2);
81 
82  s20.defaultArrayLength = s20_mz.size();
83 
84 } // initializeTinyMS1()
85 
87 {
89  fc.set(MS_MSn_spectrum);
91 
92  SourceFilePtr sourceFile(new SourceFile);
93  sourceFile->set(MS_scan_number_only_nativeID_format);
94  sourceFile->set(MS_MS2_file);
95  msd.fileDescription.sourceFilePtrs.push_back(sourceFile);
96 
97  shared_ptr<SpectrumListSimple> spectrumList(new SpectrumListSimple);
98  msd.run.spectrumListPtr = spectrumList;
99  spectrumList->spectra.push_back(SpectrumPtr(new Spectrum));
100 
101  Spectrum& s20 = *spectrumList->spectra[0];
102  s20.id = "scan=1";
103  s20.index = 0;
104 
105  s20.set(MS_MSn_spectrum);
106  s20.set(MS_ms_level, 2);
107 
109  s20.set(MS_lowest_observed_m_z, 0);
110  s20.set(MS_highest_observed_m_z, 18);
111  s20.set(MS_base_peak_m_z, 0);
112  s20.set(MS_base_peak_intensity, 20);
113  s20.set(MS_total_ion_current, 110);
114 
115  s20.precursors.resize(1);
116  Precursor& precursor = s20.precursors.front();
117  precursor.selectedIons.resize(1);
118  precursor.selectedIons[0].set(MS_selected_ion_m_z, 445.34, MS_m_z);
119  precursor.selectedIons[0].set(MS_possible_charge_state, 2);
121 
122  s20.scanList.scans.push_back(Scan());
123  Scan& s20scan = s20.scanList.scans.back();
124  s20scan.set(MS_scan_start_time, 4, UO_second);
125 
126  s20.setMZIntensityArrays(vector<double>(), vector<double>(), MS_number_of_counts);
127  vector<double>& s20_mz = s20.getMZArray()->data;
128  vector<double>& s20_intensity = s20.getIntensityArray()->data;
129 
130  for (int i=0; i<10; i++)
131  s20_mz.push_back(i*2);
132 
133  for (int i=0; i<10; i++)
134  s20_intensity.push_back((10-i)*2);
135 
136  s20.defaultArrayLength = s20_mz.size();
137 
138 } // initializeTinyMS2()
139 
140 
141 void testWriteReadMS1(const MSData& msd)
142 {
143  Serializer_MSn serializer(MSn_Type_MS1);
144 
145  ostringstream oss;
146  serializer.write(oss, msd);
147 
148  if (os_) *os_ << "oss:\n" << oss.str() << endl;
149 
150  MSData msd2;
151  shared_ptr<istream> iss(new istringstream(oss.str()));
152  serializer.read(iss, msd2);
153 
154  DiffConfig config;
155  config.ignoreMetadata = true;
156  Diff<MSData, DiffConfig> diff(msd, msd2, config);
157 
158  if (os_ && diff) *os_ << diff << endl;
159  unit_assert(!diff);
160 }
161 
162 void testWriteReadBMS1(const MSData& msd)
163 {
164  Serializer_MSn serializer(MSn_Type_BMS1);
165 
166  ostringstream oss;
167  serializer.write(oss, msd);
168 
169  if (os_) *os_ << "oss:\n" << oss.str() << endl;
170 
171  MSData msd2;
172  shared_ptr<istream> iss(new istringstream(oss.str()));
173  serializer.read(iss, msd2);
174 
175  DiffConfig config;
176  config.ignoreMetadata = true;
177  config.ignoreDataProcessing = true;
178  Diff<MSData, DiffConfig> diff(msd, msd2, config);
179  if (os_ && diff) *os_ << diff << endl;
180  unit_assert(!diff);
181 }
182 
183 void testWriteReadCMS1(const MSData& msd)
184 {
185  Serializer_MSn serializer(MSn_Type_CMS1);
186 
187  ostringstream oss;
188  serializer.write(oss, msd);
189 
190  if (os_) *os_ << "oss:\n" << oss.str() << endl;
191 
192  MSData msd2;
193  shared_ptr<istream> iss(new istringstream(oss.str()));
194  serializer.read(iss, msd2);
195 
196  DiffConfig config;
197  config.ignoreMetadata = true;
198  config.ignoreDataProcessing = true;
199  Diff<MSData, DiffConfig> diff(msd, msd2, config);
200  if (os_ && diff) *os_ << diff << endl;
201  unit_assert(!diff);
202 }
203 
204 void testWriteReadMS2(const MSData& msd)
205 {
206  Serializer_MSn serializer(MSn_Type_MS2);
207 
208  ostringstream oss;
209  serializer.write(oss, msd);
210 
211  if (os_) *os_ << "oss:\n" << oss.str() << endl;
212 
213  MSData msd2;
214  shared_ptr<istream> iss(new istringstream(oss.str()));
215  serializer.read(iss, msd2);
216 
217  DiffConfig config;
218  config.ignoreMetadata = true;
219  Diff<MSData, DiffConfig> diff(msd, msd2, config);
220  if (os_ && diff) *os_ << diff << endl;
221  unit_assert(!diff);
222 }
223 
224 void testWriteReadBMS2(const MSData& msd)
225 {
226  Serializer_MSn serializer(MSn_Type_BMS2);
227 
228  ostringstream oss;
229  serializer.write(oss, msd);
230 
231  if (os_) *os_ << "oss:\n" << oss.str() << endl;
232 
233  MSData msd2;
234  shared_ptr<istream> iss(new istringstream(oss.str()));
235  serializer.read(iss, msd2);
236 
237  DiffConfig config;
238  config.ignoreMetadata = true;
239  config.ignoreDataProcessing = true;
240  Diff<MSData, DiffConfig> diff(msd, msd2, config);
241  if (os_ && diff) *os_ << diff << endl;
242  unit_assert(!diff);
243 }
244 
245 void testWriteReadCMS2(const MSData& msd)
246 {
247  Serializer_MSn serializer(MSn_Type_CMS2);
248 
249  ostringstream oss;
250  serializer.write(oss, msd);
251 
252  if (os_) *os_ << "oss:\n" << oss.str() << endl;
253 
254  MSData msd2;
255  shared_ptr<istream> iss(new istringstream(oss.str()));
256  serializer.read(iss, msd2);
257 
258  DiffConfig config;
259  config.ignoreMetadata = true;
260  config.ignoreDataProcessing = true;
261  Diff<MSData, DiffConfig> diff(msd, msd2, config);
262  if (os_ && diff) *os_ << diff << endl;
263  unit_assert(!diff);
264 }
265 
267 {
268  MSData msd1, msd2;
269 
270  initializeTinyMS1(msd1);
271  initializeTinyMS2(msd2);
272 
273  testWriteReadMS1(msd1);
274  testWriteReadBMS1(msd1);
275  testWriteReadCMS1(msd1);
276  testWriteReadMS2(msd2);
277  testWriteReadBMS2(msd2);
278  testWriteReadCMS2(msd2);
279 }
280 
281 
282 int main(int argc, char* argv[])
283 {
284  TEST_PROLOG(argc, argv)
285 
286  try
287  {
288  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
289  testWriteRead();
290  }
291  catch (exception& e)
292  {
293  TEST_FAILED(e.what())
294  }
295  catch (...)
296  {
297  TEST_FAILED("Caught unknown exception.")
298  }
299 
301 }
302