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

Last change on this file since 3048 was 3048, checked in by Peter, 8 years ago

avoid cassert in header file

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