yat  0.12.3pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 3166 2014-01-17 02:23:05Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "BamHeader.h"
26 #include "BamRead.h"
27 #include "config_bam.h"
28 
29 #include "yat/utility/Exception.h"
30 #include "yat/utility/yat_assert.h"
31 
32 #include YAT_SAM_HEADER
33 
34 #include <boost/utility.hpp>
35 
36 #include <cstdio>
37 #include <sstream>
38 #include <stdexcept>
39 #include <string>
40 
41 namespace theplu {
42 namespace yat {
43 namespace omic {
44 
50  template<typename Derived>
51  class BamFile : boost::noncopyable
52  {
53  typedef Derived derived_type;
54  public:
58  BamFile(void);
59 
65  virtual ~BamFile(void);
66 
70  void close(void);
71 
75  bool is_open(void) const;
76  protected:
82  void open_base(const std::string& fn, const std::string& mode,
83  const void* aux);
87  samfile_t* sf_;
88 
94  const std::string& filename(void) const { return filename_; }
95  private:
96  std::string filename_;
97  };
98 
99 
105  class InBamFile : public BamFile<InBamFile>
106  {
107  typedef BamFile<InBamFile> super_t;
108  public:
112  InBamFile(void);
113 
121  explicit InBamFile(const std::string& fn);
122 
126  virtual ~InBamFile(void);
127 
131  const BamHeader& header(void) const;
132 
141  const bam_index_t* index(void) const;
142 
150  void open(const std::string& fn);
151 
157  bool read(BamRead& read);
158 
164  bool read(BamRead& read, bam_iter_t iter);
165  private:
166  BamHeader header_;
167  mutable bam_index_t* index_;
168  };
169 
170 
176  class OutBamFile : public BamFile<OutBamFile>
177  {
179  public:
183  OutBamFile(void);
184 
192  OutBamFile(const std::string&, const BamHeader& header);
193 
203  OutBamFile(const std::string&, const BamHeader& header,
204  unsigned int compression);
205 
215  void open(const std::string& fn, const BamHeader& hdr);
216 
231  void open(const std::string& fn, const BamHeader& hdr,
232  unsigned int compression);
233 
241  void write(const BamRead& read);
242 
246  class error : public utility::IO_error
247  {
248  public:
250  error(const BamRead&);
252  // has to be throw() since base class destructor is
253  virtual ~error(void) throw();
257  const BamRead& read(void) const;
258  private:
259  BamRead read_;
260  }; // end of class error
261 
262  private:
263  };
264 
265 
266  // template implementations
267  template<class Derived>
269  : sf_(NULL) {}
270 
271 
272  template<class Derived>
274  {
275  close();
276  }
277 
278 
279  template<class Derived>
281  {
282  samclose(sf_);
283  sf_ = NULL;
284  }
285 
286 
287  template<class Derived>
288  bool BamFile<Derived>::is_open(void) const
289  {
290  return sf_;
291  }
292 
293 
294  template<class Derived>
295  void BamFile<Derived>::open_base(const std::string& fn,
296  const std::string& mode,
297  const void* aux)
298  {
299  filename_ = fn;
300  YAT_ASSERT(!sf_);
301  sf_ = samopen(fn.c_str(), mode.c_str(), aux);
302  if (!sf_) {
303  std::ostringstream ss;
304  ss << "failed open '" << fn << "'";
305  throw utility::runtime_error(ss.str());
306  }
307  }
308 
309 }}}
310 #endif
void write(const BamRead &read)
write a read to output file
virtual ~InBamFile(void)
destructor
Definition: BamFile.h:51
const BamRead & read(void) const
void open_base(const std::string &fn, const std::string &mode, const void *aux)
Definition: BamFile.h:295
Wrapper around bam_header_t struct.
Definition: BamHeader.h:47
const std::string & filename(void) const
filename of bam file
Definition: BamFile.h:94
Error thrown from OutBamFile::write(const BamRead&amp;) at failure.
Definition: BamFile.h:246
BamFile(void)
Definition: BamFile.h:268
Class holding a bam query.
Definition: BamRead.h:53
void open(const std::string &fn)
Open an input bam file.
const bam_index_t * index(void) const
bool is_open(void) const
Definition: BamFile.h:288
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
virtual ~error(void)
Destructor.
InBamFile(void)
Default constructor.
Class to report errors associated with IO operations.
Definition: Exception.h:109
const BamHeader & header(void) const
void close(void)
close file
Definition: BamFile.h:280
virtual ~BamFile(void)
Destructor.
Definition: BamFile.h:273
Definition: BamFile.h:176
void open(const std::string &fn, const BamHeader &hdr)
Open an output bam file.
samfile_t * sf_
Definition: BamFile.h:87
Definition: BamFile.h:105
error(const BamRead &)
Constructor.
bool read(BamRead &read)
read the next BamRead

Generated on Mon Jun 1 2015 12:29:51 for yat by  doxygen 1.8.5