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

Last change on this file since 2943 was 2943, checked in by Peter, 9 years ago

merge release 0.10 into trunk

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