ProteoWizard
SpectrumList_MGF_Test.cpp
Go to the documentation of this file.
1 //
2 // $Id: SpectrumList_MGF_Test.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 "SpectrumList_MGF.hpp"
25 #include "TextWriter.hpp"
29 
30 using namespace pwiz::cv;
31 using namespace pwiz::msdata;
32 using namespace pwiz::util;
33 using namespace pwiz::minimxml;
34 
35 
36 ostream* os_ = 0;
37 
38 const char* testMGF =
39 "BEGIN IONS\n"
40 "PEPMASS=810.790000\n"
41 "TITLE=small.pwiz.0003.0003.2\n"
42 "231.388840 26.545113\n"
43 "233.339828 20.447954\n"
44 "239.396149 17.999159\n"
45 "END IONS\n"
46 "BEGIN IONS\n"
47 "PEPMASS=837.340000\n"
48 "TITLE=small.pwiz.0004.0004.2\n"
49 "RTINSECONDS=123.456\n"
50 "CHARGE=2+\n"
51 "236.047043 11.674493\n"
52 "237.237091 24.431984\n"
53 "238.824036 10.019409\n"
54 "239.531403 6.842983\n"
55 "243.128693 89.586212\n"
56 "END IONS\n";
57 
58 void test()
59 {
60  if (os_) *os_ << "test()\n";
61 
62  if (os_) *os_ << "mgf:\n" << testMGF << endl;
63 
64  shared_ptr<istream> is(new istringstream(testMGF));
65 
66  // dummy would normally be read in from file
67 
68  MSData dummy;
70  dummy.instrumentConfigurationPtrs.back()->cvParams.push_back(MS_LCQ_Deca);
71  dummy.instrumentConfigurationPtrs.back()->userParams.push_back(UserParam("doobie", "420"));
72 
73  SpectrumListPtr sl = SpectrumList_MGF::create(is, dummy);
74 
75  if (os_)
76  {
78  write(*sl);
79  *os_ << endl;
80  }
81 
82  // check easy functions
83 
84  unit_assert(sl.get());
85  unit_assert(sl->size() == 2);
86  unit_assert(sl->find("index=0") == 0);
87  unit_assert(sl->find("index=1") == 1);
88 
89  // find the second spectrum by TITLE field
90  IndexList list = sl->findSpotID("small.pwiz.0004.0004.2");
91  unit_assert(list.size() == 1);
92  unit_assert(list[0] == 1);
93 
94  // look for a non-existent TITLE field
95  list.clear();
96  list = sl->findSpotID("fake title string");
97  unit_assert(list.size() == 0);
98 
99  // check scan 0
100 
101  unit_assert(sl->spectrumIdentity(0).index == 0);
102  unit_assert(sl->spectrumIdentity(0).id == "index=0");
103  unit_assert(sl->spectrumIdentity(0).sourceFilePosition != -1);
104 
105  SpectrumPtr s = sl->spectrum(0, false);
106 
107  unit_assert(s.get());
108  unit_assert(s->id == "index=0");
109  unit_assert(s->index == 0);
110  unit_assert(s->sourceFilePosition != -1);
111  unit_assert(s->cvParam(MS_spectrum_title).value == "small.pwiz.0003.0003.2");
112  unit_assert(s->cvParam(MS_ms_level).valueAs<int>() == 2);
113  unit_assert_equal(s->cvParam(MS_total_ion_current).valueAs<double>(), 64.992226, 1e-5);
114  unit_assert_equal(s->cvParam(MS_base_peak_m_z).valueAs<double>(), 231.38884, 1e-5);
115  unit_assert_equal(s->cvParam(MS_base_peak_intensity).valueAs<double>(), 26.545113, 1e-5);
116 
117  unit_assert(s->precursors.size() == 1);
118  Precursor& precursor0 = s->precursors[0];
119  unit_assert(precursor0.selectedIons.size() == 1);
120  unit_assert_equal(precursor0.selectedIons[0].cvParam(MS_selected_ion_m_z).valueAs<double>(), 810.79, 1e-5);
121 
122  unit_assert(s->defaultArrayLength == 3);
123  unit_assert(s->binaryDataArrayPtrs.empty());
124 
125  s = sl->spectrum(0, true);
126  unit_assert(s->defaultArrayLength == 3);
127  unit_assert(s->binaryDataArrayPtrs.size() == 2);
128  unit_assert(!s->binaryDataArrayPtrs[0]->data.empty() && !s->binaryDataArrayPtrs[1]->data.empty());
129 
130  vector<MZIntensityPair> pairs;
131  s->getMZIntensityPairs(pairs);
132 
133  if (os_)
134  {
135  *os_ << "scan 0:\n";
136  copy(pairs.begin(), pairs.end(), ostream_iterator<MZIntensityPair>(*os_, "\n"));
137  *os_ << endl;
138  }
139 
140 
141  // check scan 1
142 
143  unit_assert(sl->spectrumIdentity(1).index == 1);
144  unit_assert(sl->spectrumIdentity(1).id == "index=1");
145 
146  s = sl->spectrum(1, true);
147  unit_assert(s.get());
148  unit_assert(s->id == "index=1");
149  unit_assert(s->index == 1);
150  unit_assert(s->sourceFilePosition != -1);
151  unit_assert(s->cvParam(MS_spectrum_title).value == "small.pwiz.0004.0004.2");
152  unit_assert(s->cvParam(MS_ms_level).valueAs<int>() == 2);
153  unit_assert(s->scanList.scans.size() == 1);
154  unit_assert_equal(s->scanList.scans[0].cvParam(MS_scan_start_time).timeInSeconds(), 123.456, 1e-5);
155 
156  unit_assert(s->precursors.size() == 1);
157  Precursor& precursor1 = s->precursors[0];
158  unit_assert(precursor1.selectedIons.size() == 1);
159  unit_assert_equal(precursor1.selectedIons[0].cvParam(MS_selected_ion_m_z).valueAs<double>(), 837.34, 1e-5);
160  unit_assert(precursor1.selectedIons[0].cvParam(MS_charge_state).value == "2");
161 
162  unit_assert(s->defaultArrayLength == 5);
163 
164  pairs.clear();
165  s->getMZIntensityPairs(pairs);
166 
167  unit_assert(s->defaultArrayLength == pairs.size());
168 
169  if (os_)
170  {
171  *os_ << "scan 1:\n";
172  copy(pairs.begin(), pairs.end(), ostream_iterator<MZIntensityPair>(*os_, "\n"));
173  *os_ << endl;
174  }
175 }
176 
177 
178 int main(int argc, char* argv[])
179 {
180  TEST_PROLOG(argc, argv)
181 
182  try
183  {
184  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
185  test();
186  }
187  catch (exception& e)
188  {
189  TEST_FAILED(e.what())
190  }
191  catch (...)
192  {
193  TEST_FAILED("Caught unknown exception.")
194  }
195 
197 }
198 
199 
MS_charge_state
charge state: The charge state of the ion, single or multiple and positive or negatively charged...
Definition: cv.hpp:235
MS_base_peak_intensity
base peak intensity: The intensity of the greatest peak in the mass spectrum.
Definition: cv.hpp:1969
std::vector< InstrumentConfigurationPtr > instrumentConfigurationPtrs
list and descriptions of instrument configurations.
Definition: MSData.hpp:873
The method of precursor ion selection and activation.
Definition: MSData.hpp:310
boost::shared_ptr< Spectrum > SpectrumPtr
Definition: MSData.hpp:569
MS_total_ion_current
total ion current: The sum of all the separate ion currents carried by the ions of different m/z cont...
Definition: cv.hpp:1255
MS_scan_start_time
scan start time: The time that an analyzer started a scan, relative to the start of the MS run...
Definition: cv.hpp:148
#define TEST_EPILOG
Definition: unit.hpp:182
const char * testMGF
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
MS_LCQ_Deca
LCQ Deca: ThermoFinnigan LCQ Deca.
Definition: cv.hpp:2119
MS_ms_level
ms level: Stages of ms achieved in a multi stage mass spectrometry experiment.
Definition: cv.hpp:1987
MS_selected_ion_m_z
selected ion m/z: Mass-to-charge ratio of an selected ion.
Definition: cv.hpp:2749
Uncontrolled user parameters (essentially allowing free text). Before using these, one should verify whether there is an appropriate CV term available, and if so, use the CV term instead.
Definition: ParamTypes.hpp:185
MS_base_peak_m_z
base peak m/z: M/z value of the signal of highest intensity in the mass spectrum. ...
Definition: cv.hpp:1966
PWIZ_API_DECL void write(minimxml::XMLWriter &writer, const CV &cv)
boost::shared_ptr< SpectrumList > SpectrumListPtr
Definition: MSData.hpp:707
int main(int argc, char *argv[])
Description of a particular hardware configuration of a mass spectrometer. Each configuration MUST ha...
Definition: MSData.hpp:228
boost::shared_ptr< InstrumentConfiguration > InstrumentConfigurationPtr
Definition: MSData.hpp:249
MS_spectrum_title
spectrum title: A free-form text title describing a spectrum.
Definition: cv.hpp:2923
#define TEST_FAILED(x)
Definition: unit.hpp:176
void test()
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174
ostream * os_
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition: MSData.hpp:845
#define unit_assert(x)
Definition: unit.hpp:85
Definition: cv.hpp:91