yat  0.10.4pre
BamFile.h
1 #ifndef theplu_yat_omic_bam_file
2 #define theplu_yat_omic_bam_file
3 
4 // $Id: BamFile.h 2928 2012-12-25 23:20:10Z peter $
5 //
6 // Copyright (C) 2012 Peter Johansson
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 #include "BamHeader.h"
22 #include "BamRead.h"
23 #include "config_bam.h"
24 
25 #include "yat/utility/Exception.h"
26 
27 #include YAT_SAM_HEADER
28 
29 #include <boost/utility.hpp>
30 
31 #include <cassert>
32 #include <cstdio>
33 #include <sstream>
34 #include <stdexcept>
35 #include <string>
36 
37 namespace theplu {
38 namespace yat {
39 namespace omic {
40 
46  template<typename Derived>
47  class BamFile : boost::noncopyable
48  {
49  typedef Derived derived_type;
50  public:
54  BamFile(void);
55 
61  virtual ~BamFile(void);
62 
66  void close(void);
67 
71  bool is_open(void) const;
72  protected:
78  void open_base(const std::string& fn, const std::string& mode,
79  const void* aux);
83  samfile_t* sf_;
84 
90  const std::string& filename(void) const { return filename_; }
91  private:
92  std::string filename_;
93  };
94 
95 
101  class InBamFile : public BamFile<InBamFile>
102  {
103  typedef BamFile<InBamFile> super_t;
104  public:
108  InBamFile(void);
109 
117  explicit InBamFile(const std::string& fn);
118 
122  virtual ~InBamFile(void);
123 
127  const BamHeader& header(void) const;
128 
137  const bam_index_t* index(void) const;
138 
144  void open(const std::string& fn);
145 
151  bool read(BamRead& read);
152 
158  bool read(BamRead& read, bam_iter_t iter);
159  private:
160  BamHeader header_;
161  mutable bam_index_t* index_;
162  };
163 
164 
170  class OutBamFile : public BamFile<OutBamFile>
171  {
173  public:
177  OutBamFile(void);
178 
186  OutBamFile(const std::string&, const BamHeader& header);
187 
197  void open(const std::string& fn, const BamHeader& hdr);
198 
202  void write(const BamRead& read);
203  private:
204  };
205 
206 
207  // template implementations
208  template<class Derived>
210  : sf_(NULL) {}
211 
212 
213  template<class Derived>
215  {
216  close();
217  }
218 
219 
220  template<class Derived>
222  {
223  samclose(sf_);
224  sf_ = NULL;
225  }
226 
227 
228  template<class Derived>
229  bool BamFile<Derived>::is_open(void) const
230  {
231  return sf_;
232  }
233 
234 
235  template<class Derived>
236  void BamFile<Derived>::open_base(const std::string& fn,
237  const std::string& mode,
238  const void* aux)
239  {
240  filename_ = fn;
241  assert(!sf_);
242  sf_ = samopen(fn.c_str(), mode.c_str(), aux);
243  if (!sf_) {
244  std::ostringstream ss;
245  ss << "failed open " << fn;
246  throw utility::runtime_error(ss.str());
247  }
248  }
249 
250 }}}
251 #endif

Generated on Mon Nov 11 2013 09:41:44 for yat by  doxygen 1.8.1