yat  0.13.2pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 3363 2014-11-25 10:42:54Z 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:
84  void open_base(const std::string& fn, const std::string& mode,
85  const void* aux);
86 
87 #ifndef YAT_HAVE_HTSLIB
88 
94  typedef samfile_t samFile;
95 #endif
96 
101 
107  const std::string& filename(void) const { return filename_; }
108  private:
109  std::string filename_;
110  };
111 
112 
118  class InBamFile : public BamFile<InBamFile>
119  {
120  typedef BamFile<InBamFile> super_t;
121  public:
132 #if YAT_HAVE_HTSLIB
133  typedef hts_idx_t index_type;
134 #else
135  typedef bam_index_t index_type;
136 #endif
137 
141  InBamFile(void);
142 
150  explicit InBamFile(const std::string& fn);
151 
155  virtual ~InBamFile(void);
156 
160  const BamHeader& header(void) const;
161 
170  const index_type* index(void) const;
171 
179  void open(const std::string& fn);
180 
186  bool read(BamRead& read);
187 
197 #if YAT_HAVE_HTSLIB
198  bool read(BamRead& read, hts_itr_t* iter);
199 #else
200  bool read(BamRead& read, bam_iter_t iter);
201 #endif
202  private:
203  BamHeader header_;
204  mutable index_type* index_;
205  };
206 
207 
213  class OutBamFile : public BamFile<OutBamFile>
214  {
216  public:
220  OutBamFile(void);
221 
229  OutBamFile(const std::string&, const BamHeader& header);
230 
240  OutBamFile(const std::string&, const BamHeader& header,
241  unsigned int compression);
242 
252  void open(const std::string& fn, const BamHeader& hdr);
253 
268  void open(const std::string& fn, const BamHeader& hdr,
269  unsigned int compression);
270 
278  void write(const BamRead& read);
279 
283  class error : public utility::IO_error
284  {
285  public:
287  error(const BamRead&);
289  // has to be throw() since base class destructor is
290  virtual ~error(void) throw();
294  const BamRead& read(void) const;
295  private:
296  BamRead read_;
297  }; // end of class error
298 
299  private:
300  };
301 
302 
303  // template implementations
304  template<class Derived>
306  : sf_(NULL) {}
307 
308 
309  template<class Derived>
311  {
312  close();
313  }
314 
315 
316  template<class Derived>
318  {
319 #if YAT_HAVE_HTSLIB
320  if (sf_==NULL)
321  return;
322  if (sam_close(sf_))
323  throw utility::IO_error("BamFile::close() failed");
324 #else
325  samclose(sf_);
326 #endif
327  sf_ = NULL;
328  }
329 
330 
331  template<class Derived>
332  bool BamFile<Derived>::is_open(void) const
333  {
334  return sf_;
335  }
336 
337 
338  template<class Derived>
339  void BamFile<Derived>::open_base(const std::string& fn,
340  const std::string& mode,
341  const void* aux)
342  {
343  filename_ = fn;
344  YAT_ASSERT(!sf_);
345 #if YAT_HAVE_HTSLIB
346  YAT_ASSERT(aux==NULL); // aux is ignored in htslib mode
347  sf_ = sam_open(fn.c_str(), mode.c_str());
348 #else
349  sf_ = samopen(fn.c_str(), mode.c_str(), aux);
350 #endif
351  if (!sf_) {
352  std::ostringstream ss;
353  ss << "failed open '" << fn << "'";
354  throw utility::runtime_error(ss.str());
355  }
356  }
357 
358 }}}
359 #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:339
Wrapper around bam_hdr_t struct.
Definition: BamHeader.h:56
const std::string & filename(void) const
filename of bam file
Definition: BamFile.h:107
Error thrown from OutBamFile::write(const BamRead&amp;) at failure.
Definition: BamFile.h:283
BamFile(void)
Definition: BamFile.h:305
Class holding a bam query.
Definition: BamRead.h:53
void open(const std::string &fn)
Open an input bam file.
bam_index_t index_type
Definition: BamFile.h:135
bool is_open(void) const
Definition: BamFile.h:332
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
samFile * sf_
Definition: BamFile.h:100
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:317
virtual ~BamFile(void)
Destructor.
Definition: BamFile.h:310
Definition: BamFile.h:213
void open(const std::string &fn, const BamHeader &hdr)
Open an output bam file.
const index_type * index(void) const
Definition: BamFile.h:118
error(const BamRead &)
Constructor.
samfile_t samFile
only defined when compiling against old bamlib
Definition: BamFile.h:94
bool read(BamRead &read)
read the next BamRead

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5