yat  0.20.3pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 3999 2020-10-08 23:22:32Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2016, 2017, 2018, 2020 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 yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "BamHeader.h"
26 #include "BamRead.h"
27 
28 #include "yat/utility/deprecate.h"
29 #include "yat/utility/Exception.h"
30 #include "yat/utility/FileUtil.h"
31 #include "yat/utility/yat_assert.h"
32 
33 #include <htslib/sam.h>
34 
35 #include <boost/utility.hpp>
36 
37 #include <cstdio>
38 #include <sstream>
39 #include <stdexcept>
40 #include <string>
41 
42 namespace theplu {
43 namespace yat {
44 namespace omic {
45 
51  template<typename Derived>
52  class BamFile : boost::noncopyable
53  {
54  typedef Derived derived_type;
55  public:
59  BamFile(void);
60 
66  virtual ~BamFile(void);
67 
71  void close(void);
72 
78  bool have_index(void) const;
79 
83  bool is_open(void) const;
84  protected:
88  void build_index_base(void) const;
89 
100  void open_base(const std::string& fn, const std::string& mode,
101  const void* aux) YAT_DEPRECATE;
102 
110  void open_base(const std::string& fn, const std::string& mode);
111 
115  samFile* sf_;
116 
122  const std::string& filename(void) const { return filename_; }
123 
124  private:
125  std::string filename_;
126  };
127 
128 
134  class InBamFile : public BamFile<InBamFile>
135  {
136  typedef BamFile<InBamFile> super_t;
137  public:
147  typedef hts_idx_t index_type;
148 
152  InBamFile(void);
153 
161  explicit InBamFile(const std::string& fn);
162 
166  virtual ~InBamFile(void);
167 
171  void build_index(void) const;
172 
176  const BamHeader& header(void) const;
177 
186  const index_type* index(void) const;
187 
191  uint64_t n_mapped(int tid) const;
192 
198  uint64_t n_unmapped(int tid) const;
199 
203  uint64_t n_no_coordinate(void) const;
204 
212  void open(const std::string& fn);
213 
219  bool read(BamRead& read);
220 
230  bool read(BamRead& read, hts_itr_t* iter);
231  private:
232  uint64_t get_idx_stat(int tid, bool return_mapped) const;
233  BamHeader header_;
234  // always access index_ via function index(), so index is loaded
235  // if needed
236  mutable index_type* index_;
237  };
238 
239 
245  class OutBamFile : public BamFile<OutBamFile>
246  {
248  public:
252  OutBamFile(void);
253 
261  OutBamFile(const std::string&, const BamHeader& header);
262 
272  OutBamFile(const std::string&, const BamHeader& header,
273  unsigned int compression);
274 
280  void build_index(void) const;
281 
291  void open(const std::string& fn, const BamHeader& hdr);
292 
307  void open(const std::string& fn, const BamHeader& hdr,
308  unsigned int compression);
309 
317  void write(const BamRead& read);
318 
322  class error : public utility::IO_error
323  {
324  public:
326  error(const BamRead&);
328  // has to be throw() since base class destructor is
329  virtual ~error(void) throw();
333  const BamRead& read(void) const;
334  private:
335  BamRead read_;
336  }; // end of class error
337 
338  private:
339  void open(const std::string& fn, const std::string& mode,
340  const BamHeader& h);
341 
342  };
343 
344 
345  // template implementations
346  template<class Derived>
348  : sf_(NULL) {}
349 
350 
351  template<class Derived>
353  {
354  close();
355  }
356 
357 
358  template<class Derived>
360  {
361  int res = bam_index_build(filename_.c_str(), 0);
362  if (res) {
363  std::ostringstream msg;
364  msg << "failed building index file '" << filename_ << ".bai': ";
365  if (res == -1)
366  msg << "failed to build index";
367  else if (res == -2)
368  msg << "failed to open file '" << filename_ << "'";
369  else if (res == -4)
370  msg << "failed to save file";
371  throw utility::runtime_error(msg.str());
372  }
373  }
374 
375 
376  template<class Derived>
378  {
379  if (sf_==NULL)
380  return;
381  if (sam_close(sf_))
382  throw utility::IO_error("BamFile::close() failed");
383  sf_ = NULL;
384  }
385 
386 
387  template<class Derived>
388  bool BamFile<Derived>::is_open(void) const
389  {
390  return sf_;
391  }
392 
393 
394  template<class Derived>
396  {
397  return utility::FileUtil(filename() + ".bai").exists();
398  }
399 
400 
401  template<class Derived>
402  void BamFile<Derived>::open_base(const std::string& fn,
403  const std::string& mode,
404  const void* aux)
405  {
406  YAT_ASSERT(aux==NULL); // aux is ignored in htslib mode
407  open_base(fn, mode);
408  }
409 
410 
411  template<class Derived>
412  void BamFile<Derived>::open_base(const std::string& fn,
413  const std::string& mode)
414  {
415  filename_ = fn;
416  YAT_ASSERT(!sf_);
417  sf_ = sam_open(fn.c_str(), mode.c_str());
418  if (!sf_) {
419  std::ostringstream ss;
420  ss << "failed open '" << fn << "'";
421  throw utility::runtime_error(ss.str());
422  }
423  }
424 
425 }}}
426 #endif
void write(const BamRead &read)
write a read to output file
virtual ~InBamFile(void)
destructor
Definition: BamFile.h:52
void build_index(void) const
uint64_t n_no_coordinate(void) const
The Department of Theoretical Physics namespace as we define it.
void open_base(const std::string &fn, const std::string &mode, const void *aux)
Definition: BamFile.h:402
Wrapper around bam_hdr_t struct.
Definition: BamHeader.h:51
Error thrown from OutBamFile::write(const BamRead&) at failure.
Definition: BamFile.h:322
BamFile(void)
Definition: BamFile.h:347
Class holding a bam query.
Definition: BamRead.h:51
const index_type * index(void) const
const std::string & filename(void) const
filename of bam file
Definition: BamFile.h:122
bool is_open(void) const
Definition: BamFile.h:388
void open(const std::string &fn)
Open an input bam file.
bool have_index(void) const
Definition: BamFile.h:395
const BamRead & read(void) const
void build_index(void) const
Class used for all runtime error detected within yat library.
Definition: Exception.h:53
samFile * sf_
Definition: BamFile.h:115
uint64_t n_unmapped(int tid) const
void build_index_base(void) const
Definition: BamFile.h:359
virtual ~error(void)
Destructor.
InBamFile(void)
Default constructor.
Class to report errors associated with IO operations.
Definition: Exception.h:124
bool exists(void) const
Check whether file exists.
void close(void)
close file
Definition: BamFile.h:377
virtual ~BamFile(void)
Destructor.
Definition: BamFile.h:352
Definition: BamFile.h:245
void open(const std::string &fn, const BamHeader &hdr)
Open an output bam file.
Definition: BamFile.h:134
hts_idx_t index_type
Definition: BamFile.h:147
error(const BamRead &)
Constructor.
const BamHeader & header(void) const
uint64_t n_mapped(int tid) const
Checking file/directory existence and access permissions.
Definition: FileUtil.h:43
bool read(BamRead &read)
read the next BamRead

Generated on Sat Feb 18 2023 03:31:44 for yat by  doxygen 1.8.14