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