source: trunk/yat/omic/BamFile.h @ 2884

Last change on this file since 2884 was 2884, checked in by Peter, 10 years ago

add missing since tags. refs #729

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1#ifndef theplu_yat_omic_bam_file
2#define theplu_yat_omic_bam_file
3
4// $Id: BamFile.h 2884 2012-12-04 02:17:22Z 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
24#include <sam.h>
25
26#include <boost/utility.hpp>
27
28#include <cassert>
29#include <cstdio>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33
34namespace theplu {
35namespace yat {
36namespace omic {
37
38  /**
39     Base class for bam files
40
41     \since New in yat 0.10
42   */
43  template<typename Derived>
44  class BamFile : boost::noncopyable
45  {
46    typedef Derived derived_type;
47  public:
48    /**
49       Default Constructor
50     */
51    BamFile(void);
52
53    /**
54       \brief Destructor
55
56       Closes file if it is open.
57     */
58    virtual ~BamFile(void);
59
60    /**
61       \brief close file
62     */
63    void close(void);
64
65    /**
66       \return \c true iff open
67     */
68    bool is_open(void) const;
69  protected:
70    /**
71       open a bam file named \a fn with mode \a mode
72
73       \see samopen
74     */
75    void open_base(const std::string& fn, const std::string& mode,
76                   const void* aux);
77    /**
78       bam file handler
79     */
80    samfile_t* sf_;
81
82    /**
83       \brief filename of bam file
84
85       The filename is set in open_base().
86     */
87    const std::string& filename(void) const { return filename_; }
88  private:
89    std::string filename_;
90  };
91
92
93  /**
94     This class supports reading from a bam file.
95
96     \since New in yat 0.10
97   */
98  class InBamFile : public BamFile<InBamFile>
99  {
100    typedef BamFile<InBamFile> super_t;
101  public:
102    /**
103       \brief Default constructor
104     */
105    InBamFile(void);
106
107    /**
108       Create an input file
109
110       \param fn string specifying the filename
111
112       \see open(const std::string&)
113     */
114    explicit InBamFile(const std::string& fn);
115
116    /**
117       \brief destructor
118     */
119    virtual ~InBamFile(void);
120
121    /**
122       \return header
123     */
124    const BamHeader& header(void) const;
125
126    /**
127       \return index associated with BamFile
128
129       First time this function is called an index file is loaded from
130       disk. If bam file is named 'foo.bam', the index file should be
131       named 'foo.bam.bai'. If no such file exists or this bam file
132       reads from stdin, thsi function throws.
133     */
134    const bam_index_t* index(void) const;
135
136    /**
137       \brief Open an input bam file.
138
139       \param fn string specifying the filename
140     */
141    void open(const std::string& fn);
142
143    /**
144       \brief read the next BamRead
145
146       \return true on success
147     */
148    bool read(BamRead& read);
149
150    /**
151       \brief read the next BamRead
152
153       \return true on success
154     */
155    bool read(BamRead& read, bam_iter_t iter);
156  private:
157    BamHeader header_;
158    mutable bam_index_t* index_;
159  };
160
161
162  /**
163     This class supports writing to a bam file.
164
165     \since New in yat 0.10
166   */
167  class OutBamFile : public BamFile<OutBamFile>
168  {
169    typedef BamFile<OutBamFile> super_t;
170  public:
171    /**
172       Create an output bam file
173     */
174    OutBamFile(void);
175
176    /**
177       \brief Create an output bam file.
178
179       Equivalent to default constructor followed by a call to open(2).
180
181       \see open(const std::string&, const BamHeader&)
182     */
183    OutBamFile(const std::string&, const BamHeader& header);
184
185    /**
186       \brief Open an output bam file.
187
188       Opens an output bam file and writes the header contained in \a
189       hdr.
190
191       \param fn string specifying the filename
192       \param hdr header
193     */
194    void open(const std::string& fn, const BamHeader& hdr);
195
196    /**
197       \brief write a read to output file
198     */
199    void write(const BamRead& read);
200  private:
201  };
202
203
204  // template implementations
205  template<class Derived>
206  BamFile<Derived>::BamFile(void)
207  : sf_(NULL) {}
208
209
210  template<class Derived>
211  BamFile<Derived>::~BamFile(void)
212  {
213    close();
214  }
215
216
217  template<class Derived>
218  void BamFile<Derived>::close(void)
219  {
220    samclose(sf_);
221    sf_ = NULL;
222  }
223
224
225  template<class Derived>
226  bool BamFile<Derived>::is_open(void) const
227  {
228    return sf_;
229  }
230
231
232  template<class Derived>
233  void BamFile<Derived>::open_base(const std::string& fn,
234                                   const std::string& mode,
235                                   const void* aux)
236  {
237    filename_ = fn;
238    assert(!sf_);
239    sf_ = samopen(fn.c_str(), mode.c_str(), aux);
240    if (!sf_) {
241      std::ostringstream ss;
242      ss << "failed open " << fn;
243      throw std::runtime_error(ss.str());
244    }
245  }
246
247}}}
248#endif
Note: See TracBrowser for help on using the repository browser.