ProteoWizard
Reader.hpp
Go to the documentation of this file.
1 //
2 // $Id: Reader.hpp 6239 2014-05-24 03:37:45Z 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 #ifndef _READER_HPP_
25 #define _READER_HPP_
26 
28 #include "MSData.hpp"
29 #include <string>
30 #include <stdexcept>
31 
32 
33 namespace pwiz {
34 namespace msdata {
35 
36 /// interface for file readers
38 {
39  public:
40 
41 
42  /// Reader configuration
44  {
45  /// when true, sets certain vendor readers to produce SIM/SRM transitions as spectra instead of chromatograms
48 
49  /// when true, allows for skipping 0 length checks (and thus skip re-reading data for ABI)
51 
52  /// when true, all drift bins/scans in a frame/block are written in combined form instead of as individual spectra
54 
55  Config();
56  Config(const Config& rhs);
57  };
58 
59 
60  /// return true iff Reader recognizes the file as one it should handle
61  /// that's not to say one it CAN handle, necessarily, as in Thermo on linux,
62  /// see comment for identify() below
63  bool accept(const std::string& filename,
64  const std::string& head) const
65  {
66  return (identify(filename,head).length() != 0);
67  }
68 
69  /// return file type iff Reader recognizes the file, else empty;
70  /// note: for formats requiring a 3rd party DLL identify() should
71  /// return non-empty if it recognized the format, even though reading
72  /// may fail if the 3rd party DLL isn't actually present
73  /// Reader may filter based on filename and/or head of the file
74  virtual std::string identify(const std::string& filename,
75  const std::string& head) const = 0;
76 
77  /// fill in the MSData structure from the first (or only) sample
78  virtual void read(const std::string& filename,
79  const std::string& head,
80  MSData& result,
81  int runIndex = 0,
82  const Config& config = Config()) const = 0;
83 
84  /// fill in a vector of MSData structures; provides support for multi-run input files
85  virtual void read(const std::string& filename,
86  const std::string& head,
87  std::vector<MSDataPtr>& results,
88  const Config& config = Config()) const = 0;
89 
90  /// fill in a vector of MSData.Id values; provides support for multi-run input files
91  virtual void readIds(const std::string& filename,
92  const std::string& head,
93  std::vector<std::string>& dataIds,
94  const Config& config = Config()) const;
95 
96  /// returns a unique string identifying the reader type
97  virtual const char* getType() const = 0;
98 
99  virtual ~Reader(){}
100 };
101 
102 class PWIZ_API_DECL ReaderFail : public std::runtime_error // reader failure exception
103 {
104  public:
105 
106  ReaderFail(const std::string& error)
107  : std::runtime_error(("[ReaderFail] " + error).c_str()),
108  error_(error)
109  {}
110 
111  virtual const std::string& error() const {return error_;}
112  virtual ~ReaderFail() throw() {}
113 
114  private:
115  std::string error_;
116 };
117 
118 typedef boost::shared_ptr<Reader> ReaderPtr;
119 
120 
121 ///
122 /// Reader container (composite pattern).
123 ///
124 /// The template get<reader_type>() gives access to child Readers by type, to facilitate
125 /// Reader-specific configuration at runtime.
126 ///
128  public std::vector<ReaderPtr>
129 {
130  public:
131 
132  /// returns child name iff some child identifies, else empty string
133  virtual std::string identify(const std::string& filename) const;
134 
135  /// returns child name iff some child identifies, else empty string
136  virtual std::string identify(const std::string& filename,
137  const std::string& head) const;
138 
139  /// delegates to first child that identifies
140  virtual void read(const std::string& filename,
141  MSData& result,
142  int runIndex = 0,
143  const Config& config = Config()) const;
144 
145  /// delegates to first child that identifies
146  virtual void read(const std::string& filename,
147  const std::string& head,
148  MSData& result,
149  int runIndex = 0,
150  const Config& config = Config()) const;
151 
152  /// delegates to first child that identifies;
153  /// provides support for multi-run input files
154  virtual void read(const std::string& filename,
155  std::vector<MSDataPtr>& results,
156  const Config& config = Config()) const;
157 
158  /// delegates to first child that identifies;
159  /// provides support for multi-run input files
160  virtual void read(const std::string& filename,
161  const std::string& head,
162  std::vector<MSDataPtr>& results,
163  const Config& config = Config()) const;
164 
165  /// delegates to first child that identifies;
166  /// provides support for multi-run input files
167  virtual void readIds(const std::string& filename,
168  std::vector<std::string>& results,
169  const Config& config = Config()) const;
170 
171  /// delegates to first child that identifies;
172  /// provides support for multi-run input files
173  virtual void readIds(const std::string& filename,
174  const std::string& head,
175  std::vector<std::string>& results,
176  const Config& config = Config()) const;
177 
178  /// appends all of the rhs operand's Readers to the list
179  ReaderList& operator +=(const ReaderList& rhs);
180 
181  /// appends the rhs Reader to the list
182  ReaderList& operator +=(const ReaderPtr& rhs);
183 
184  /// returns a concatenated list of all the Readers from the lhs and rhs operands
185  ReaderList operator +(const ReaderList& rhs) const;
186 
187  /// returns a concatenated list of all the Readers from the lhs and rhs operands
188  ReaderList operator +(const ReaderPtr& rhs) const;
189 
190  /// returns pointer to Reader of the specified type
191  template <typename reader_type>
192  reader_type* get()
193  {
194  for (iterator it=begin(); it!=end(); ++it)
195  {
196  reader_type* p = dynamic_cast<reader_type*>(it->get());
197  if (p) return p;
198  }
199 
200  return 0;
201  }
202 
203  /// returns const pointer to Reader of the specified type
204  template <typename reader_type>
205  const reader_type* get() const
206  {
207  return const_cast<ReaderList*>(this)->get<reader_type>();
208  }
209 
210  virtual const char* getType() const {return "ReaderList";} // satisfy inheritance
211 };
212 
213 
214 /// returns a list containing the lhs and rhs as readers
215 PWIZ_API_DECL ReaderList operator +(const ReaderPtr& lhs, const ReaderPtr& rhs);
216 
217 
218 /// tries to identify a filepath using the provided Reader or ReaderList;
219 /// returns the CVID file format of the specified filepath,
220 /// or CVID_Unknown if the file format has no CV term or the filepath doesn't exist
221 PWIZ_API_DECL CVID identifyFileFormat(const ReaderPtr& reader, const std::string& filepath);
222 
223 
224 } // namespace msdata
225 } // namespace pwiz
226 
227 
228 #endif // _READER_HPP_
229 
ReaderFail(const std::string &error)
Definition: Reader.hpp:106
PWIZ_API_DECL double & operator+=(double &d, const MZTolerance &tolerance)
virtual const char * getType() const
returns a unique string identifying the reader type
Definition: Reader.hpp:210
STL namespace.
Reader container (composite pattern).
Definition: Reader.hpp:127
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:63
boost::shared_ptr< Reader > ReaderPtr
Definition: Reader.hpp:118
bool acceptZeroLengthSpectra
when true, allows for skipping 0 length checks (and thus skip re-reading data for ABI) ...
Definition: Reader.hpp:50
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...
#define PWIZ_API_DECL
Definition: Export.hpp:32
interface for file readers
Definition: Reader.hpp:37
virtual ~Reader()
Definition: Reader.hpp:99
bool simAsSpectra
when true, sets certain vendor readers to produce SIM/SRM transitions as spectra instead of chromatog...
Definition: Reader.hpp:46
PWIZ_API_DECL void read(std::istream &is, CV &cv)
PWIZ_API_DECL ReaderList operator+(const ReaderPtr &lhs, const ReaderPtr &rhs)
returns a list containing the lhs and rhs as readers
bool combineIonMobilitySpectra
when true, all drift bins/scans in a frame/block are written in combined form instead of as individua...
Definition: Reader.hpp:53
Reader configuration.
Definition: Reader.hpp:43
virtual const std::string & error() const
Definition: Reader.hpp:111
This is the root element of ProteoWizard; it represents the mzML element, defined as: intended to cap...
Definition: MSData.hpp:845