multovl  1.3
Multiple overlaps of genomic regions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
linereader.hh
Go to the documentation of this file.
1 /* <LICENSE>
2 License for the MULTOVL multiple genomic overlap tools
3 
4 Copyright (c) 2007-2012, Dr Andras Aszodi,
5 Campus Science Support Facilities GmbH (CSF),
6 Dr-Bohr-Gasse 3, A-1030 Vienna, Austria, Europe.
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12 
13  * Redistributions of source code must retain the above copyright notice,
14  this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18  * Neither the name of the Campus Science Support Facilities GmbH
19  nor the names of its contributors may be used to endorse
20  or promote products derived from this software without specific prior
21  written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
24 AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 </LICENSE> */
35 #ifndef LINEREADER_HEADER
36 #define LINEREADER_HEADER
37 
38 // == HEADER linereader.hh ==
39 
48 // -- Standard headers --
49 
50 #include <string>
51 #include <map>
52 #include <iostream>
53 #include <stdexcept>
54 
55 // -- Boost headers --
56 
57 #include "boost/regex.hpp"
58 
59 namespace multovl {
60 
61 class Region;
62 
63 namespace io {
64 
76 {
77  public:
78 
80  enum Status
81  {
82  CLEAN = 0,
83  ERROR = 1,
84  EMPTY = 2,
85  COMMENT = 4,
86  DATA = 8
87  };
88 
91  explicit Linereader(const std::string& commentchars="#");
92 
93  virtual ~Linereader() {}
94 
97  Status parse(const std::string& line);
98 
101  virtual bool read_into(Region& region) const = 0;
102 
103  // -- Accessors
104 
106  Status status() const { return _status; }
107 
110  const std::string& comment() const { return _comment; }
111 
114  size_t fieldcnt() const { return _fieldcnt; }
115 
117  const std::string& error_msg() const { return _err; }
118 
119  const std::string& chrom() const { return _chrom; }
120  unsigned int first() const { return _first; }
121  unsigned int last() const { return _last; }
122  char strand() const { return _strand; }
123  const std::string& name() const { return _name; }
124 
125  protected:
126 
128  class Exception: public std::logic_error
129  {
130  public:
131 
135  Exception(const std::string& from, const std::string& problem):
136  logic_error("\"" + from + "\": " + problem) {}
137  };
138 
141  virtual void reset();
142 
146  {
147  _status = static_cast<Status>(_status | flag);
148  }
149 
151  static bool empty_white(const std::string& str);
152 
155  virtual void parse_data(const std::string& line) = 0;
156 
159  static unsigned int str_to_uint(std::string& str) throw (Exception);
160 
161  // these data members can be set during parsing by the derived classes
163  size_t _fieldcnt;
164  std::string _comment;
165  std::string _err;
166 
167  // basically the contents of a Region object
168  std::string _chrom, _name;
169  unsigned int _first, _last;
170  char _strand;
171 
172  private:
173 
174  boost::regex _comment_regex;
175 };
176 
185 {
186  public:
187 
190 
193  virtual bool read_into(Region& region) const;
194 
195  protected:
196 
197  virtual void reset();
198 
200  virtual void parse_data(const std::string& line);
201 
202 };
203 
210 {
211  public:
212 
215 
216  // NOTE: read_into() is not overridden
217 
218  protected:
219 
220  // NOTE: reset() is not overridden
221 
223  virtual void parse_data(const std::string& line);
224 
225 };
226 
227 } // namespace io
228 } // namespace multovl
229 
230 #endif // MULTOVL_LINEREADER_HEADER
static unsigned int str_to_uint(std::string &str)
std::string _err
stores parsing error messages
Definition: linereader.hh:165
virtual void parse_data(const std::string &line)=0
virtual void parse_data(const std::string &line)
Parse a /line/ assuming it contains BED-formatted data.
Status parse(const std::string &line)
Linereader's own exception.
Definition: linereader.hh:128
virtual void reset()
static bool empty_white(const std::string &str)
Returns /true/ if /str/ is empty or contains whitespace characters only.
const std::string & comment() const
Definition: linereader.hh:110
Linereader(const std::string &commentchars="#")
comment line was seen
Definition: linereader.hh:85
virtual bool read_into(Region &region) const
state after reset (all bits off)
Definition: linereader.hh:82
Instances of the Region class represent regions on a sequence. They have first and last coordinates...
Definition: region.hh:65
Linereader for GFF-formatted lines. Only the columns containing coordinate information are parsed...
Definition: linereader.hh:209
size_t _fieldcnt
how many column fields have been seen
Definition: linereader.hh:163
Status status() const
Definition: linereader.hh:106
Base class of parser objects. Linereader objects process one line at a time, and change their interna...
Definition: linereader.hh:75
Status
Symbolic constants to signify the result of a parsing operation.
Definition: linereader.hh:80
void set_statusflag(Status flag)
Definition: linereader.hh:145
Linereader for BED-formatted lines. The BedLinereader class parses columns 1 to 6 of the BED format s...
Definition: linereader.hh:184
empty line (whitespace only) was seen
Definition: linereader.hh:84
BedLinereader()
Sets up a BedLinereader object.
Definition: linereader.hh:189
const std::string & error_msg() const
Definition: linereader.hh:117
Status _status
the status after parsing
Definition: linereader.hh:162
Exception(const std::string &from, const std::string &problem)
Definition: linereader.hh:135
could not parse line
Definition: linereader.hh:83
virtual void parse_data(const std::string &line)
Parse a /line/ assuming it contains GFF-formatted data.
size_t fieldcnt() const
Definition: linereader.hh:114
GffLinereader()
Init to empty.
Definition: linereader.hh:214
data line parsed successfully
Definition: linereader.hh:86
virtual bool read_into(Region &region) const =0
std::string _comment
store a comment line
Definition: linereader.hh:164