ProteoWizard
MSDataAnalyzerTest.cpp
Go to the documentation of this file.
1//
2// $Id$
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 "MSDataAnalyzer.hpp"
27#include <cstring>
28
29
30using namespace pwiz::util;
31using namespace pwiz::analysis;
32
33
34ostream* os_ = 0;
35
36
38{
39 size_t index;
40 bool opened;
42 bool closed;
43
44 SimpleAnalyzer(size_t _index) : index(_index), opened(false), updateCount(0), closed(false) {}
45
46 virtual void open(const DataInfo& dataInfo)
47 {
48 // initialize everything, since Analyzers may be reused
49 opened = false;
50 updateCount = 0;
51 closed=false;
52
53 // do something
54 opened = true;
55 }
56
57 virtual UpdateRequest updateRequested(const DataInfo& dataInfo,
58 const SpectrumIdentity& entry) const
59 {
60 // only request this->index
62 }
63
64 virtual void update(const DataInfo& dataInfo,
65 const Spectrum& spectrum)
66 {
67 if (os_) *os_ << "[" << index << "]" << " update: " << spectrum.index << endl;
69 }
70
71 virtual void close(const DataInfo& dataInfo) {closed = true;}
72};
73
74
76{
77 size_t count;
78
80
81 virtual size_t iterationsPerCallback() const {return 5;}
82
83 virtual MSDataAnalyzerDriver::Status progress(size_t index, size_t size)
84 {
85 if (os_) *os_ << "progress: " << index << "/" << size << endl;
86 count++;
87 return MSDataAnalyzerDriver::Status_Ok;
88 }
89};
90
91
93{
94 size_t count;
95
97
98 virtual size_t iterationsPerCallback() const {return 5;}
99
100 virtual MSDataAnalyzerDriver::Status progress(size_t index, size_t size)
101 {
102 if (os_) *os_ << "progress: " << index << "/" << size << endl;
103 count++;
104 return index<5 ? MSDataAnalyzerDriver::Status_Ok : MSDataAnalyzerDriver::Status_Cancel;
105 }
106};
107
108
109void test()
110{
111 if (os_) *os_ << "test()\n";
112
113 // set up analyzers
114
115 MSDataAnalyzerContainer analyzers;
116 analyzers.push_back(MSDataAnalyzerPtr(new SimpleAnalyzer(23))); // request index 23
117 analyzers.push_back(MSDataAnalyzerPtr(new SimpleAnalyzer(17))); // request index 17
118
119 unit_assert(analyzers.size() == 2);
120 for (MSDataAnalyzerContainer::const_iterator it=analyzers.begin(); it!=analyzers.end(); ++it)
121 {
122 const SimpleAnalyzer& anal = dynamic_cast<const SimpleAnalyzer&>(**it);
123 unit_assert(!anal.opened);
124 unit_assert(anal.updateCount == 0);
125 unit_assert(!anal.closed);
126 }
127
128 // instantiate MSData object
129
130 MSData dummy;
132 const int spectrumCount = 30;
133 for (int i=0; i<spectrumCount; i++)
134 {
135 sl->spectra.push_back(SpectrumPtr(new Spectrum));
136 sl->spectra.back()->index = i;
137 }
138 dummy.run.spectrumListPtr = sl;
139
140 // run driver
141
142 MSDataAnalyzerDriver driver(analyzers);
143 SimpleProgressCallback callback;
144 MSDataAnalyzerDriver::Status status = driver.analyze(dummy, &callback);
145
146 unit_assert(status == MSDataAnalyzerDriver::Status_Ok);
147
148 for (MSDataAnalyzerContainer::const_iterator it=analyzers.begin(); it!=analyzers.end(); ++it)
149 {
150 const SimpleAnalyzer& anal = dynamic_cast<const SimpleAnalyzer&>(**it);
151 unit_assert(anal.opened);
152 unit_assert(anal.updateCount == 1);
153 unit_assert(anal.closed);
154 }
155
156 unit_assert(callback.count == spectrumCount/callback.iterationsPerCallback() + 1);
157
158 // run driver again with cancel callback
159
160 if (os_) *os_ << "testing cancel callback:\n";
161
162 CancelProgressCallback cancelCallback;
163 status = driver.analyze(dummy, &cancelCallback);
164
165 unit_assert(status == MSDataAnalyzerDriver::Status_Cancel);
166
167 if (os_) *os_ << "cancelled!\n";
168
169 for (MSDataAnalyzerContainer::const_iterator it=analyzers.begin(); it!=analyzers.end(); ++it)
170 {
171 const SimpleAnalyzer& anal = dynamic_cast<const SimpleAnalyzer&>(**it);
172 unit_assert(anal.opened);
173 unit_assert(anal.updateCount == 0);
174 unit_assert(!anal.closed);
175 }
176
177 unit_assert(cancelCallback.count == 2);
178}
179
180
181int main(int argc, char* argv[])
182{
183 TEST_PROLOG(argc, argv)
184
185 try
186 {
187 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
188 test();
189 }
190 catch (exception& e)
191 {
192 TEST_FAILED(e.what())
193 }
194 catch (...)
195 {
196 TEST_FAILED("Caught unknown exception.")
197 }
198
200}
201
UpdateRequest_NoBinary
UpdateRequest_None
int main(int argc, char *argv[])
ostream * os_
void test()
const char * anal(const CVParam &cvParam)
container of MSDataAnalyzer (composite pattern)
event generator for MSDataAnalyzer
Status analyze(const MSDataAnalyzer::DataInfo &dataInfo, ProgressCallback *progressCallback=0) const
analyze a single MSData object, calling back to client if requested
Interface for MSData analyzers.
boost::shared_ptr< MSDataAnalyzer > MSDataAnalyzerPtr
boost::shared_ptr< Spectrum > SpectrumPtr
Definition MSData.hpp:573
boost::shared_ptr< SpectrumListSimple > SpectrumListSimplePtr
Definition MSData.hpp:731
virtual size_t iterationsPerCallback() const
virtual MSDataAnalyzerDriver::Status progress(size_t index, size_t size)
virtual void update(const DataInfo &dataInfo, const Spectrum &spectrum)
analyze a single spectrum
SimpleAnalyzer(size_t _index)
virtual void close(const DataInfo &dataInfo)
end analysis of the data
virtual void open(const DataInfo &dataInfo)
start analysis of the data
virtual UpdateRequest updateRequested(const DataInfo &dataInfo, const SpectrumIdentity &entry) const
ask analyzer if it wants an update
virtual MSDataAnalyzerDriver::Status progress(size_t index, size_t size)
virtual size_t iterationsPerCallback() const
information about the data to be analyzed
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition MSData.hpp:850
Run run
a run in mzML should correspond to a single, consecutive and coherent set of scans on an instrument.
Definition MSData.hpp:886
SpectrumListPtr spectrumListPtr
all mass spectra and the acquisitions underlying them are described and attached here....
Definition MSData.hpp:827
The structure that captures the generation of a peak list (including the underlying acquisitions)
Definition MSData.hpp:506
Identifying information for a spectrum.
Definition MSData.hpp:471
size_t index
the zero-based, consecutive index of the spectrum in the SpectrumList.
Definition MSData.hpp:473
Simple writeable in-memory implementation of SpectrumList.
Definition MSData.hpp:717
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175