yat  0.14.5pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 3550 2017-01-03 05:41:02Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2016 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 
100  samFile* sf_;
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 
175  uint64_t n_mapped(int tid) const;
176 
182  uint64_t n_unmapped(int tid) const;
183 
187  uint64_t n_no_coordinate(void) const;
188 
196  void open(const std::string& fn);
197 
203  bool read(BamRead& read);
204 
214 #if YAT_HAVE_HTSLIB
215  bool read(BamRead& read, hts_itr_t* iter);
216 #else
217  bool read(BamRead& read, bam_iter_t iter);
218 #endif
219  private:
220  uint64_t get_idx_stat(int tid, bool return_mapped) const;
221  BamHeader header_;
222  // always access index_ via function index(), so index is loaded
223  // if needed
224  mutable index_type* index_;
225  };
226 
227 
233  class OutBamFile : public BamFile<OutBamFile>
234  {
236  public:
240  OutBamFile(void);
241 
249  OutBamFile(const std::string&, const BamHeader& header);
250 
260  OutBamFile(const std::string&, const BamHeader& header,
261  unsigned int compression);
262 
272  void open(const std::string& fn, const BamHeader& hdr);
273 
288  void open(const std::string& fn, const BamHeader& hdr,
289  unsigned int compression);
290 
298  void write(const BamRead& read);
299 
303  class error : public utility::IO_error
304  {
305  public:
307  error(const BamRead&);
309  // has to be throw() since base class destructor is
310  virtual ~error(void) throw();
314  const BamRead& read(void) const;
315  private:
316  BamRead read_;
317  }; // end of class error
318 
319  private:
320  };
321 
322 
323  // template implementations
324  template<class Derived>
326  : sf_(NULL) {}
327 
328 
329  template<class Derived>
331  {
332  close();
333  }
334 
335 
336  template<class Derived>
338  {
339 #if YAT_HAVE_HTSLIB
340  if (sf_==NULL)
341  return;
342  if (sam_close(sf_))
343  throw utility::IO_error("BamFile::close() failed");
344 #else
345  samclose(sf_);
346 #endif
347  sf_ = NULL;
348  }
349 
350 
351  template<class Derived>
352  bool BamFile<Derived>::is_open(void) const
353  {
354  return sf_;
355  }
356 
357 
358  template<class Derived>
359  void BamFile<Derived>::open_base(const std::string& fn,
360  const std::string& mode,
361  const void* aux)
362  {
363  filename_ = fn;
364  YAT_ASSERT(!sf_);
365 #if YAT_HAVE_HTSLIB
366  YAT_ASSERT(aux==NULL); // aux is ignored in htslib mode
367  sf_ = sam_open(fn.c_str(), mode.c_str());
368 #else
369  sf_ = samopen(fn.c_str(), mode.c_str(), aux);
370 #endif
371  if (!sf_) {
372  std::ostringstream ss;
373  ss << "failed open '" << fn << "'";
374  throw utility::runtime_error(ss.str());
375  }
376  }
377 
378 }}}
379 #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
uint64_t n_no_coordinate(void) const
void open_base(const std::string &fn, const std::string &mode, const void *aux)
Definition: BamFile.h:359
Wrapper around bam_hdr_t struct.
Definition: BamHeader.h:60
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:303
BamFile(void)
Definition: BamFile.h:325
Class holding a bam query.
Definition: BamRead.h:53
void open(const std::string &fn)
Open an input bam file.
bool is_open(void) const
Definition: BamFile.h:352
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
uint64_t n_unmapped(int tid) const
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:337
virtual ~BamFile(void)
Destructor.
Definition: BamFile.h:330
Definition: BamFile.h:233
void open(const std::string &fn, const BamHeader &hdr)
Open an output bam file.
const index_type * index(void) const
Definition: BamFile.h:118
hts_idx_t index_type
Definition: BamFile.h:133
error(const BamRead &)
Constructor.
uint64_t n_mapped(int tid) const
bool read(BamRead &read)
read the next BamRead

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5