ProteoWizard
ReaderTest.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
27#include "Reader.hpp"
28#include "examples.hpp"
29#include "MSDataFile.hpp"
31#include <cstring>
32
33
34using namespace pwiz::util;
35using namespace pwiz::cv;
36using namespace pwiz::msdata;
37
38
39ostream* os_ = 0;
40
41
42class Reader1 : public Reader
43{
44 public:
45
47 {
48 string name;
49 mutable bool done;
50 ReaderConfig() : name("default"), done(false) {}
51 };
52
54
55 virtual std::string identify(const std::string& filename, const std::string& head) const
56 {
57 bool result = (filename == "1");
58 if (os_) *os_ << "Reader1::identify(): " << boolalpha << result << endl;
59 return std::string (result?filename:std::string(""));
60 }
61
62 virtual void read(const std::string& filename,
63 const std::string& head,
64 MSData& result,
65 int runIndex = 0,
66 const Config& config = Config()) const
67 {
68 if (os_) *os_ << "Reader1::read()\n";
69 readerConfig.done = true;
70 }
71
72 virtual void read(const std::string& filename,
73 const std::string& head,
74 std::vector<MSDataPtr>& results,
75 const Config& config = Config()) const
76 {
77 results.push_back(MSDataPtr(new MSData));
78 read(filename, head, *results.back(), 0, config);
79 }
80
81 virtual const char *getType() const {return "Reader1";} // satisfy inheritance
82};
83
84
85class Reader2 : public Reader
86{
87 public:
88
90 {
91 string color;
92 mutable bool done;
93 ReaderConfig() : color("orange"), done(false) {}
94 };
95
97
98 virtual std::string identify(const std::string& filename, const std::string& head) const
99 {
100 bool result = (filename == "2");
101 if (os_) *os_ << "Reader2::identify(): " << boolalpha << result << endl;
102 return std::string (result?filename:std::string(""));
103 }
104
105 virtual void read(const std::string& filename,
106 const std::string& head,
107 MSData& result,
108 int runIndex = 0,
109 const Config& config = Config()) const
110 {
111 if (os_) *os_ << "Reader2::read()\n";
112 readerConfig.done = true;
113 }
114
115 virtual void read(const std::string& filename,
116 const std::string& head,
117 std::vector<MSDataPtr>& results,
118 const Config& config = Config()) const
119 {
120 results.push_back(MSDataPtr(new MSData));
121 read(filename, head, *results.back(), 0, config);
122 }
123
124 const char *getType() const {return "Reader2";} // satisfy inheritance
125};
126
127
129{
130 if (os_) *os_ << "testGet()\n";
131
132 ReaderList readers;
133 readers.push_back(ReaderPtr(new Reader1));
134 readers.push_back(ReaderPtr(new Reader2));
135
136 unit_assert(readers.size() == 2);
137
138 Reader1* reader1 = readers.get<Reader1>();
139 unit_assert(reader1);
140 if (os_) *os_ << "reader1 config: " << reader1->readerConfig.name << endl;
141 unit_assert(reader1->readerConfig.name == "default");
142 reader1->readerConfig.name = "raw";
143 if (os_) *os_ << "reader1 config: " << reader1->readerConfig.name << endl;
144 unit_assert(reader1->readerConfig.name == "raw");
145
146 Reader2* reader2 = readers.get<Reader2>();
147 unit_assert(reader2);
148 if (os_) *os_ << "reader2 config: " << reader2->readerConfig.color << endl;
149 unit_assert(reader2->readerConfig.color == "orange");
150 reader2->readerConfig.color = "purple";
151 if (os_) *os_ << "reader2 config: " << reader2->readerConfig.color << endl;
152 unit_assert(reader2->readerConfig.color == "purple");
153
154 const ReaderList& const_readers = readers;
155 const Reader2* constReader2 = const_readers.get<Reader2>();
156 unit_assert(constReader2);
157 if (os_) *os_ << "constReader2 config: " << constReader2->readerConfig.color << endl;
158
159 if (os_) *os_ << endl;
160}
161
162
164{
165 if (os_) *os_ << "testAccept()\n";
166
167 ReaderList readers;
168 readers.push_back(ReaderPtr(new Reader1));
169 readers.push_back(ReaderPtr(new Reader2));
170
171 if (os_) *os_ << "accept 1:\n";
172 unit_assert(readers.accept("1", "head"));
173 if (os_) *os_ << "accept 2:\n";
174 unit_assert(readers.accept("2", "head"));
175 if (os_) *os_ << "accept 3:\n";
176 unit_assert(!readers.accept("3", "head"));
177
178 if (os_) *os_ << endl;
179}
180
181
183{
184 if (os_) *os_ << "testRead()\n";
185
186 ReaderList readers;
187 readers.push_back(ReaderPtr(new Reader1));
188 readers.push_back(ReaderPtr(new Reader2));
189
190 MSData msd;
191
192 // note: composite pattern with accept/read will cause two calls
193 // to accept(); the alternative is to maintain state between accept()
194 // and read(), which opens possibility for misuse.
195
196 unit_assert(readers.get<Reader1>()->readerConfig.done == false);
197 if (readers.accept("1", "head"))
198 readers.read("1", "head", msd);
199 unit_assert(readers.get<Reader1>()->readerConfig.done == true);
200
201 readers.get<Reader1>()->readerConfig.done = false;
202 unit_assert(readers.get<Reader2>()->readerConfig.done == false);
203 if (readers.accept("2", "head"))
204 readers.read("2", "head", msd);
205 unit_assert(readers.get<Reader1>()->readerConfig.done == false);
206 unit_assert(readers.get<Reader2>()->readerConfig.done == true);
207
208 if (os_) *os_ << endl;
209}
210
211
213{
214 ReaderPtr readers(new ExtendedReaderList);
215
216 {ofstream fs("testSpectraDataFile.mzedML"); fs << "<?xml?><mzML>";}
217 unit_assert_operator_equal(MS_mzML_format, identifyFileFormat(readers, "testSpectraDataFile.mzedML"));
218 bfs::remove("testSpectraDataFile.mzedML");
219
220 {ofstream fs("testSpectraDataFile.mzedXML"); fs << "<?xml?><mzXML>";}
221 unit_assert_operator_equal(MS_ISB_mzXML_format, identifyFileFormat(readers, "testSpectraDataFile.mzedXML"));
222 bfs::remove("testSpectraDataFile.mzedXML");
223
224
225 {
226 MSData msd;
229 config.format = MSDataFile::Format_MZ5;
230#ifndef WITHOUT_MZ5
231 MSDataFile::write(msd, "testSpectraDataFile.Mz5", config);
232 unit_assert_operator_equal(MS_mz5_format, identifyFileFormat(readers, "testSpectraDataFile.Mz5"));
233#endif
234 }
235 bfs::remove("testSpectraDataFile.Mz5");
236
237 {ofstream fs("testSpectraDataFile.mGF"); fs << "MGF";}
238 unit_assert_operator_equal(MS_Mascot_MGF_format, identifyFileFormat(readers, "testSpectraDataFile.mGF"));
239 bfs::remove("testSpectraDataFile.mGF");
240
241 {ofstream fs("testSpectraDataFile.Ms2"); fs << "MS2";}
242 unit_assert_operator_equal(MS_MS2_format, identifyFileFormat(readers, "testSpectraDataFile.Ms2"));
243 bfs::remove("testSpectraDataFile.Ms2");
244
245 {ofstream fs("testSpectraDataFile.wiFF"); fs << "WIFF";}
246 unit_assert_operator_equal(MS_ABI_WIFF_format, identifyFileFormat(readers, "testSpectraDataFile.wiFF"));
247 bfs::remove("testSpectraDataFile.wiFF");
248
249 {ofstream fs("_FUNC42.DAT"); fs << "Life, the Universe, and Everything";}
251 bfs::remove("_FUNC42.DAT");
252}
253
254
255void test()
256{
257 testGet();
258 testAccept();
259 testRead();
261}
262
263
264int main(int argc, char* argv[])
265{
266 TEST_PROLOG_EX(argc, argv, "_MSData")
267
268 try
269 {
270 if (argc==2 && !strcmp(argv[1],"-v")) os_ = &cout;
271 test();
272 }
273 catch (exception& e)
274 {
275 TEST_FAILED(e.what())
276 }
277 catch (...)
278 {
279 TEST_FAILED("Caught unknown exception.")
280 }
281
283}
284
ReaderConfig readerConfig
virtual const char * getType() const
virtual void read(const std::string &filename, const std::string &head, std::vector< MSDataPtr > &results, const Config &config=Config()) const
virtual std::string identify(const std::string &filename, const std::string &head) const
virtual void read(const std::string &filename, const std::string &head, MSData &result, int runIndex=0, const Config &config=Config()) const
virtual void read(const std::string &filename, const std::string &head, TraData &result, int runIndex=0) const
Config config
ReaderConfig readerConfig
virtual void read(const std::string &filename, const std::string &head, TraData &result, int runIndex=0) const
virtual std::string identify(const std::string &filename, const std::string &head) const
Config config
virtual void read(const std::string &filename, const std::string &head, std::vector< MSDataPtr > &results, const Config &config=Config()) const
const char * getType() const
virtual void read(const std::string &filename, const std::string &head, MSData &result, int runIndex=0, const Config &config=Config()) const
default ReaderList, extended to include vendor readers
interface for file readers
Definition Reader.hpp:39
bool accept(const std::string &filename, const std::string &head) const
return true iff Reader recognizes the file as one it should handle
Definition Reader.hpp:84
Reader container (composite pattern).
Definition Reader.hpp:150
virtual void read(const std::string &filename, MSData &result, int runIndex=0, const Config &config=Config()) const
delegates to first child that identifies
reader_type * get()
returns pointer to Reader of the specified type
Definition Reader.hpp:213
MS_ABI_WIFF_format
ABI WIFF format: Applied Biosystems WIFF file format.
Definition cv.hpp:2295
MS_mzML_format
mzML format: Proteomics Standards Inititative mzML file format.
Definition cv.hpp:2403
MS_mz5_format
mz5 format: mz5 file format, modelled after mzML.
Definition cv.hpp:6057
MS_MS2_format
MS2 format: MS2 file format for MS2 spectral data.
Definition cv.hpp:4794
MS_Mascot_MGF_format
Mascot MGF format: Mascot MGF file format.
Definition cv.hpp:3669
MS_Waters_raw_format
Waters raw format: Waters data file format found in a Waters RAW directory, generated from an MS acqu...
Definition cv.hpp:2184
MS_ISB_mzXML_format
ISB mzXML format: Institute of Systems Biology mzXML file format.
Definition cv.hpp:2307
int main(int argc, char *argv[])
void testRead()
void testIdentifyFileFormat()
ostream * os_
void testAccept()
void test()
void testGet()
PWIZ_API_DECL void initializeTiny(MSData &msd)
PWIZ_API_DECL CVID identifyFileFormat(const ReaderPtr &reader, const std::string &filepath)
tries to identify a filepath using the provided Reader or ReaderList; returns the CVID file format of...
boost::shared_ptr< MSData > MSDataPtr
Definition MSData.hpp:913
boost::shared_ptr< Reader > ReaderPtr
Definition Reader.hpp:139
configuration for write()
static void write(const MSData &msd, const std::string &filename, const WriteConfig &config=WriteConfig(), const pwiz::util::IterationListenerRegistry *iterationListenerRegistry=0)
static write function for any MSData object; iterationListenerRegistry may be used for progress updat...
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition MSData.hpp:850
ostream * os_
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_PROLOG_EX(argc, argv, suffix)
Definition unit.hpp:157
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92