yat  0.16.4pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 3792 2019-04-12 07:15:09Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2016, 2017, 2018 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 #include "config_bam.h"
28 
29 #include "yat/utility/Exception.h"
30 #include "yat/utility/FileUtil.h"
31 #include "yat/utility/yat_assert.h"
32 
33 #include YAT_SAM_HEADER
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 
97  void open_base(const std::string& fn, const std::string& mode,
98  const void* aux);
99 
100 #ifndef YAT_HAVE_HTSLIB
101 
107  typedef samfile_t samFile;
108 #endif
109 
113  samFile* sf_;
114 
120  const std::string& filename(void) const { return filename_; }
121 
122  private:
123  std::string filename_;
124  };
125 
126 
132  class InBamFile : public BamFile<InBamFile>
133  {
134  typedef BamFile<InBamFile> super_t;
135  public:
146 #if YAT_HAVE_HTSLIB
147  typedef hts_idx_t index_type;
148 #else
149  typedef bam_index_t index_type;
150 #endif
151 
155  InBamFile(void);
156 
164  explicit InBamFile(const std::string& fn);
165 
169  virtual ~InBamFile(void);
170 
174  void build_index(void) const;
175 
179  const BamHeader& header(void) const;
180 
189  const index_type* index(void) const;
190 
194  uint64_t n_mapped(int tid) const;
195 
201  uint64_t n_unmapped(int tid) const;
202 
206  uint64_t n_no_coordinate(void) const;
207 
215  void open(const std::string& fn);
216 
222  bool read(BamRead& read);
223 
233 #if YAT_HAVE_HTSLIB
234  bool read(BamRead& read, hts_itr_t* iter);
235 #else
236  bool read(BamRead& read, bam_iter_t iter);
237 #endif
238  private:
239  uint64_t get_idx_stat(int tid, bool return_mapped) const;
240  BamHeader header_;
241  // always access index_ via function index(), so index is loaded
242  // if needed
243  mutable index_type* index_;
244  };
245 
246 
252  class OutBamFile : public BamFile<OutBamFile>
253  {
255  public:
259  OutBamFile(void);
260 
268  OutBamFile(const std::string&, const BamHeader& header);
269 
279  OutBamFile(const std::string&, const BamHeader& header,
280  unsigned int compression);
281 
287  void build_index(void) const;
288 
298  void open(const std::string& fn, const BamHeader& hdr);
299 
314  void open(const std::string& fn, const BamHeader& hdr,
315  unsigned int compression);
316 
324  void write(const BamRead& read);
325 
329  class error : public utility::IO_error
330  {
331  public:
333  error(const BamRead&);
335  // has to be throw() since base class destructor is
336  virtual ~error(void) throw();
340  const BamRead& read(void) const;
341  private:
342  BamRead read_;
343  }; // end of class error
344 
345  private:
346  void open(const std::string& fn, const std::string& mode,
347  const BamHeader& h);
348 
349  };
350 
351 
352  // template implementations
353  template<class Derived>
355  : sf_(NULL) {}
356 
357 
358  template<class Derived>
360  {
361  close();
362  }
363 
364 
365  template<class Derived>
367  {
368 #ifdef YAT_HAVE_HTSLIB
369  int res = bam_index_build(filename_.c_str(), 0);
370 #else
371  int res = bam_index_build(filename_.c_str());
372 #endif
373  if (res) {
374  std::ostringstream msg;
375  msg << "failed building index file '" << filename_ << ".bai': ";
376  if (res == -1)
377  msg << "failed to build index";
378  else if (res == -2)
379  msg << "failed to open file '" << filename_ << "'";
380  else if (res == -4)
381  msg << "failed to save file";
382  throw utility::runtime_error(msg.str());
383  }
384  }
385 
386 
387  template<class Derived>
389  {
390 #if YAT_HAVE_HTSLIB
391  if (sf_==NULL)
392  return;
393  if (sam_close(sf_))
394  throw utility::IO_error("BamFile::close() failed");
395 #else
396  samclose(sf_);
397 #endif
398  sf_ = NULL;
399  }
400 
401 
402  template<class Derived>
403  bool BamFile<Derived>::is_open(void) const
404  {
405  return sf_;
406  }
407 
408 
409  template<class Derived>
411  {
412  return utility::FileUtil(filename() + ".bai").exists();
413  }
414 
415 
416  template<class Derived>
417  void BamFile<Derived>::open_base(const std::string& fn,
418  const std::string& mode,
419  const void* aux)
420  {
421  filename_ = fn;
422  YAT_ASSERT(!sf_);
423 #if YAT_HAVE_HTSLIB
424  YAT_ASSERT(aux==NULL); // aux is ignored in htslib mode
425  sf_ = sam_open(fn.c_str(), mode.c_str());
426 #else
427  sf_ = samopen(fn.c_str(), mode.c_str(), aux);
428 #endif
429  if (!sf_) {
430  std::ostringstream ss;
431  ss << "failed open '" << fn << "'";
432  throw utility::runtime_error(ss.str());
433  }
434  }
435 
436 }}}
437 #endif
bool exists(void) const
Check whether file exists.
Definition: BamFile.h:52
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:417
Wrapper around bam_hdr_t struct.
Definition: BamHeader.h:60
const std::string & filename(void) const
filename of bam file
Definition: BamFile.h:120
Error thrown from OutBamFile::write(const BamRead&) at failure.
Definition: BamFile.h:329
BamFile(void)
Definition: BamFile.h:354
Class holding a bam query.
Definition: BamRead.h:53
bool is_open(void) const
Definition: BamFile.h:403
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
samFile * sf_
Definition: BamFile.h:113
void build_index_base(void) const
Definition: BamFile.h:366
Class to report errors associated with IO operations.
Definition: Exception.h:109
bool have_index(void) const
Definition: BamFile.h:410
void close(void)
close file
Definition: BamFile.h:388
virtual ~BamFile(void)
Destructor.
Definition: BamFile.h:359
Definition: BamFile.h:252
Definition: BamFile.h:132
hts_idx_t index_type
Definition: BamFile.h:147
Checking file/directory existence and access permissions.
Definition: FileUtil.h:43

Generated on Thu Dec 12 2019 03:12:08 for yat by  doxygen 1.8.11