ProteoWizard
ParamTypesTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: ParamTypesTest.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 "ParamTypes.hpp"
27 #include <cstring>
28 
29 
30 using namespace pwiz::cv;
31 using namespace pwiz::util;
32 using namespace pwiz::data;
33 
34 
35 ostream* os_ = 0;
36 
37 
39 {
40  public:
41 
42  WriteCVParam(ostream& os) : os_(os) {}
43 
44  void operator()(const CVParam& param)
45  {
46  os_ << "<cvParam "
47  << "cvLabel=\"" << cvTermInfo(param.cvid).id.substr(0,2) << "\" "
48  << "accession=\"" << cvTermInfo(param.cvid).id << "\" "
49  << "name=\"" << cvTermInfo(param.cvid).name << "\" "
50  << "value=\"" << param.value << "\"";
51 
52  if (param.units != CVID_Unknown)
53  {
54  os_ << " unitAccession=\"" << cvTermInfo(param.units).id << "\" "
55  << "unitName=\"" << cvTermInfo(param.units).name << "\"";
56  }
57 
58  os_ << "/>\n";
59  }
60 
61  private:
62  ostream& os_;
63 };
64 
65 
66 const char* mzmlScanTime =
67  "<cvParam cvLabel=\"MS\" accession=\"MS:1000016\" name=\"scan start time\" value=\"5.890500\" "
68  "unitAccession=\"UO:0000031\" unitName=\"minute\"/>\n";
69 
70 const char* mzmlCollisionEnergy =
71  "<cvParam cvLabel=\"MS\" accession=\"MS:1000045\" name=\"collision energy\" value=\"35.00\" "
72  "unitAccession=\"UO:0000266\" unitName=\"electronvolt\"/>\n";
73 
74 
75 void test()
76 {
77  vector<CVParam> params;
78 
79  params.push_back(CVParam(MS_lowest_observed_m_z, 420));
80  params.push_back(CVParam(MS_highest_observed_m_z, 2000.012345));
81  params.push_back(CVParam(MS_m_z, "goober"));
82  params.push_back(CVParam(MS_scan_start_time, 5.890500, UO_minute));
83  params.push_back(CVParam(MS_collision_energy, 35.00, UO_electronvolt));
84  params.push_back(CVParam(MS_deisotoping, true));
85  params.push_back(CVParam(MS_peak_picking, false));
86 
87  if (os_)
88  {
89  *os_ << "params:\n";
90  copy(params.begin(), params.end(), ostream_iterator<CVParam>(*os_, "\n"));
91  *os_ << endl;
92 
93  *os_ << "as mzML <cvParam> elements:\n";
94  for_each(params.begin(), params.end(), WriteCVParam(*os_));
95  *os_ << endl;
96 
97  *os_ << "value casting:\n";
98  int temp = params[0].valueAs<int>();
99  *os_ << temp << endl;
100  float temp2 = params[1].valueAs<float>();
101  *os_ << temp2 << endl;
102  string temp3 = params[2].valueAs<string>();
103  *os_ << temp3 << "\n\n";
104  }
105 
106  // verify simple things
107  unit_assert(420 == params[0].valueAs<int>());
108  unit_assert(2000.012345 == params[1].valueAs<double>());
109  unit_assert("goober" == params[2].value);
110  unit_assert(5.890500 == params[3].valueAs<double>());
111  unit_assert(35.00 == params[4].valueAs<double>());
112  unit_assert(params[0] == CVParam(MS_lowest_observed_m_z, 420));
113  unit_assert(params[1] != CVParam(MS_lowest_observed_m_z, 420));
115  unit_assert(params[5].valueAs<bool>() == true);
116  unit_assert(params[6].valueAs<bool>() == false);
117 
118  // verify manual mzml writing -- this is to verify that we have enough
119  // info to write <cvParam> elements as required by mzML
120 
121  ostringstream ossScanTime;
122  CVParam scanTime(MS_scan_start_time, "5.890500", UO_minute);
123  (WriteCVParam(ossScanTime))(scanTime);
124  if (os_) *os_ << "mzmlScanTime: " << mzmlScanTime << endl
125  << "ossScanTime: " << ossScanTime.str() << endl;
126  unit_assert(ossScanTime.str() == mzmlScanTime);
127  if (os_) *os_ << "scan time in seconds: " << scanTime.timeInSeconds() << endl;
128  unit_assert_equal(scanTime.timeInSeconds(), 5.8905 * 60, 1e-10);
129 
130  ostringstream ossCollisionEnergy;
131  (WriteCVParam(ossCollisionEnergy))(CVParam(MS_collision_energy, "35.00", UO_electronvolt));
132  if (os_) *os_ << "mzmlCollisionEnergy: " << mzmlCollisionEnergy << endl
133  << "ossCollisionEnergy: " << ossCollisionEnergy.str() << endl;
134  unit_assert(ossCollisionEnergy.str() == mzmlCollisionEnergy);
135 }
136 
137 
138 void testIs()
139 {
140  vector<CVParam> params;
141  params.push_back(CVParam(MS_plasma_desorption));
142  params.push_back(CVParam(MS_lowest_observed_m_z, 420));
143  params.push_back(CVParam(MS_collision_induced_dissociation));
144 
145  vector<CVParam>::const_iterator it =
146  find_if(params.begin(), params.end(), CVParamIs(MS_lowest_observed_m_z));
147 
148  unit_assert(it->value == "420");
149 }
150 
151 
153 {
154  // example of how to search through a collection of CVParams
155  // to find the first one whose cvid IsA specified CVID
156 
157  vector<CVParam> params;
158  params.push_back(CVParam(MS_lowest_observed_m_z, 420));
159  params.push_back(CVParam(MS_plasma_desorption));
160  params.push_back(CVParam(MS_collision_induced_dissociation));
161  params.push_back(CVParam(UO_electronvolt));
162  params.push_back(CVParam(MS_highest_observed_m_z, 2400.0));
163 
164  vector<CVParam>::const_iterator itDiss =
165  find_if(params.begin(), params.end(), CVParamIsChildOf(MS_dissociation_method));
166 
167  vector<CVParam>::const_iterator itUnit =
168  find_if(params.begin(), params.end(), CVParamIsChildOf(UO_unit));
169 
170  if (os_)
171  {
172  *os_ << "find dissociation method: "
173  << (itDiss!=params.end() ? cvTermInfo(itDiss->cvid).name : "not found")
174  << endl;
175 
176  *os_ << "find unit: "
177  << (itUnit!=params.end() ? cvTermInfo(itUnit->cvid).name : "not found")
178  << endl;
179 
180  }
181 
182  unit_assert(itDiss!=params.end() && itDiss->cvid==MS_plasma_desorption);
183  unit_assert(itUnit!=params.end() && itUnit->cvid==UO_electronvolt);
184 }
185 
186 
188 {
189  ParamContainer pc;
190  pc.cvParams.push_back(MS_reflectron_on);
191  pc.cvParams.push_back(MS_MSn_spectrum);
192  pc.cvParams.push_back(MS_reflectron_off);
193  pc.cvParams.push_back(CVParam(MS_ionization_type, 420));
194  pc.userParams.push_back(UserParam("name1", "1", "type1", UO_second));
195  pc.userParams.push_back(UserParam("name2", "2", "type2", UO_minute));
196 
197  ParamGroupPtr pg(new ParamGroup);
198  pg->cvParams.push_back(CVParam(UO_dalton, 666));
199  pc.paramGroupPtrs.push_back(pg);
200 
205 
208 
213 
216 
217  string result = "goober";
218  result = pc.cvParam(MS_selected_ion_m_z).value;
219  unit_assert(result == "");
220  result = pc.cvParam(MS_ionization_type).value;
221  unit_assert(result == "420");
222  result = pc.cvParam(UO_dalton).value;
223  unit_assert(result == "666");
224 
225  UserParam userParam = pc.userParam("name");
226  unit_assert(userParam.empty());
227  userParam = pc.userParam("name1");
228  unit_assert(userParam.name == "name1");
229  unit_assert(userParam.valueAs<int>() == 1);
230  unit_assert(userParam.type == "type1");
231  unit_assert(userParam.units == UO_second);
232  userParam = pc.userParam("name2");
233  unit_assert(userParam.name == "name2");
234  unit_assert(userParam.valueAs<double>() == 2);
235  unit_assert(userParam.type == "type2");
236  unit_assert(userParam.units == UO_minute);
237  unit_assert(pc.userParam("goober").valueAs<int>() == 0);
238 
239  pc.set(MS_ms_level, 2);
240  unit_assert(pc.cvParam(MS_ms_level).valueAs<int>() == 2);
241  pc.set(MS_ms_level, 3);
242  unit_assert(pc.cvParam(MS_ms_level).valueAs<int>() == 3);
243 
244  pc.set(MS_deisotoping, true);
245  unit_assert(pc.cvParam(MS_deisotoping).valueAs<bool>() == true);
246  pc.set(MS_deisotoping, false);
247  unit_assert(pc.cvParam(MS_deisotoping).valueAs<bool>() == false);
248 
249  pc.set(MS_CID);
250  pc.set(MS_ETD);
251  pg->set(MS_PQD);
252  vector<CVParam> dissociationMethods = pc.cvParamChildren(MS_dissociation_method);
253  unit_assert(dissociationMethods.size() == 3);
254  unit_assert(dissociationMethods[0] == MS_CID);
255  unit_assert(dissociationMethods[1] == MS_ETD);
256  unit_assert(dissociationMethods[2] == MS_PQD);
257 }
258 
259 
260 int main(int argc, char* argv[])
261 {
262  TEST_PROLOG(argc, argv)
263 
264  try
265  {
266  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
267  test();
268  testIs();
269  testIsChildOf();
271  }
272  catch (exception& e)
273  {
274  TEST_FAILED(e.what())
275  }
276  catch (...)
277  {
278  TEST_FAILED("Caught unknown exception.")
279  }
280 
282 }
283