ProteoWizard
Reader.hpp
Go to the documentation of this file.
1 //
2 // $Id: Reader.hpp 2820 2011-06-27 22:51:16Z chambm $
3 //
4 //
5 // Origional author: Robert Burke <robert.burke@proteowizard.org>
6 //
7 // Copyright 2009 Spielberg Family Center for Applied Proteomics
8 // University of Southern California, Los Angeles, California 90033
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 _MZID_READER_HPP_
25 #define _MZID_READER_HPP_
26 
28 #include "IdentData.hpp"
29 #include <string>
30 #include <stdexcept>
32 
33 
34 namespace pwiz {
35 namespace identdata {
36 
37 /// interface for file readers
39 {
40  public:
41 
42 
43  /// HACK: provide an option to read only file-level metadata;
44  /// once we have an enumerable ResultList implementation
45  /// this will be deprecated
47  {
50 
51  Config(bool ignoreSequenceCollectionAndAnalysisData = false)
52  : ignoreSequenceCollectionAndAnalysisData(ignoreSequenceCollectionAndAnalysisData),
53  iterationListenerRegistry(NULL)
54  {}
55  };
56 
57 
58  /// return true iff Reader recognizes the file as one it should handle
59  /// that's not to say one it CAN handle, necessarily, as in Thermo on linux,
60  /// see comment for identify() below
61  bool accept(const std::string& filename,
62  const std::string& head) const
63  {
64  return (identify(filename,head).length() != 0);
65  }
66 
67  /// return file type iff Reader recognizes the file, else empty;
68  /// note: for formats requiring a 3rd party DLL identify() should
69  /// return true if it recognized the format, even though reading
70  /// may fail if the 3rd party DLL isn't actually present
71  /// Reader may filter based on filename and/or head of the file
72  virtual std::string identify(const std::string& filename,
73  const std::string& head) const = 0;
74 
75  /// fill in a vector of IdentData structures; provides support for multi-run input files
76  virtual void read(const std::string& filename,
77  IdentData& results,
78  const Config& config = Config()) const;
79 
80  /// fill in a vector of IdentData structures; provides support for multi-run input files
81  virtual void read(const std::string& filename,
82  const std::string& head,
83  IdentData& results,
84  const Config& config = Config()) const = 0;
85 
86  /// fill in a vector of IdentData structures; provides support for multi-run input files
87  virtual void read(const std::string& filename,
88  IdentDataPtr& results,
89  const Config& config = Config()) const;
90 
91  /// fill in a vector of IdentData structures; provides support for multi-run input files
92  virtual void read(const std::string& filename,
93  const std::string& head,
94  IdentDataPtr& results,
95  const Config& config = Config()) const = 0;
96 
97  /// fill in a vector of IdentData structures; provides support for multi-run input files
98  virtual void read(const std::string& filename,
99  const std::string& head,
100  std::vector<IdentDataPtr>& results,
101  const Config& config = Config()) const = 0;
102 
103  virtual const char *getType() const = 0; // what kind of reader are you?
104 
105  virtual ~Reader(){}
106 };
107 
108 typedef boost::shared_ptr<Reader> ReaderPtr;
109 
110 class PWIZ_API_DECL ReaderFail : public std::runtime_error // reader failure exception
111 {
112  public:
113 
114  ReaderFail(const std::string& error)
115  : std::runtime_error(("[ReaderFail] " + error).c_str()),
116  error_(error)
117  {}
118 
119  virtual const std::string& error() const {return error_;}
120  virtual ~ReaderFail() throw() {}
121 
122  private:
123  std::string error_;
124 };
125 
126 typedef boost::shared_ptr<Reader> ReaderPtr;
127 
128 
129 ///
130 /// Reader container (composite pattern).
131 ///
132 /// The template get<reader_type>() gives access to child Readers by type, to facilitate
133 /// Reader-specific configuration at runtime.
134 ///
136  public std::vector<ReaderPtr>
137 {
138  public:
139 
140  /// returns child name iff some child identifies, else empty string
141  virtual std::string identify(const std::string& filename) const;
142 
143  /// returns child name iff some child identifies, else empty string
144  virtual std::string identify(const std::string& filename,
145  const std::string& head) const;
146 
147  /// delegates to first child that identifies
148  virtual void read(const std::string& filename,
149  IdentData& result,
150  const Config& config = Config()) const;
151 
152  /// delegates to first child that identifies
153  virtual void read(const std::string& filename,
154  const std::string& head,
155  IdentData& result,
156  const Config& config = Config()) const;
157 
158  /// delegates to first child that identifies
159  virtual void read(const std::string& filename,
160  IdentDataPtr& result,
161  const Config& config = Config()) const;
162 
163  /// delegates to first child that identifies
164  virtual void read(const std::string& filename,
165  const std::string& head,
166  IdentDataPtr& result,
167  const Config& config = Config()) const;
168 
169  /// delegates to first child that identifies;
170  /// provides support for multi-run input files
171  virtual void read(const std::string& filename,
172  std::vector<IdentDataPtr>& results,
173  const Config& config = Config()) const;
174 
175  /// delegates to first child that identifies;
176  /// provides support for multi-run input files
177  virtual void read(const std::string& filename,
178  const std::string& head,
179  std::vector<IdentDataPtr>& results,
180  const Config& config = Config()) const;
181 
182  /// appends all of the rhs operand's Readers to the list
183  ReaderList& operator +=(const ReaderList& rhs);
184 
185  /// appends the rhs Reader to the list
186  ReaderList& operator +=(const ReaderPtr& rhs);
187 
188  /// returns a concatenated list of all the Readers from the lhs and rhs operands
189  ReaderList operator +(const ReaderList& rhs) const;
190 
191  /// returns a concatenated list of all the Readers from the lhs and rhs operands
192  ReaderList operator +(const ReaderPtr& rhs) const;
193 
194  /// returns pointer to Reader of the specified type
195  template <typename reader_type>
196  reader_type* get()
197  {
198  for (iterator it=begin(); it!=end(); ++it)
199  {
200  reader_type* p = dynamic_cast<reader_type*>(it->get());
201  if (p) return p;
202  }
203 
204  return 0;
205  }
206 
207  /// returns const pointer to Reader of the specified type
208  template <typename reader_type>
209  const reader_type* get() const
210  {
211  return const_cast<ReaderList*>(this)->get<reader_type>();
212  }
213 
214  virtual const char *getType() const {return "ReaderList";} // satisfy inheritance
215 
216 };
217 
218 
219 /// returns a list containing the lhs and rhs as readers
220 PWIZ_API_DECL ReaderList operator +(const ReaderPtr& lhs, const ReaderPtr& rhs);
221 
222 
223 } // namespace identdata
224 } // namespace pwiz
225 
226 
227 #endif // _MZID_READER_HPP_