1 | // $Id: bam.cc 3295 2014-07-25 04:24:39Z peter $ |
---|
2 | // |
---|
3 | // Copyright (C) 2013, 2014 Peter Johansson |
---|
4 | // |
---|
5 | // This program is free software; you can redistribute it and/or modify |
---|
6 | // it under the terms of the GNU General Public License as published by |
---|
7 | // the Free Software Foundation; either version 3 of the License, or |
---|
8 | // (at your option) any later version. |
---|
9 | // |
---|
10 | // This program is distributed in the hope that it will be useful, but |
---|
11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | // General Public License for more details. |
---|
14 | // |
---|
15 | // You should have received a copy of the GNU General Public License |
---|
16 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | #include <config.h> |
---|
19 | |
---|
20 | #include "Suite.h" |
---|
21 | |
---|
22 | #ifdef YAT_HAVE_LIBBAM |
---|
23 | #include "yat/omic/BamFile.h" |
---|
24 | #include "yat/omic/BamRead.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "yat/utility/Aligner.h" |
---|
28 | |
---|
29 | #include <cassert> |
---|
30 | #include <string> |
---|
31 | |
---|
32 | using namespace theplu::yat; |
---|
33 | |
---|
34 | void test1(test::Suite& suite); |
---|
35 | |
---|
36 | int main(int argc, char* argv[]) |
---|
37 | { |
---|
38 | test::Suite suite(argc, argv, true); |
---|
39 | #ifdef YAT_HAVE_LIBBAM |
---|
40 | test1(suite); |
---|
41 | #endif |
---|
42 | return suite.return_value(); |
---|
43 | } |
---|
44 | |
---|
45 | #ifdef YAT_HAVE_LIBBAM |
---|
46 | using namespace omic; |
---|
47 | |
---|
48 | void test_aux(test::Suite& suite, const BamRead& b) |
---|
49 | { |
---|
50 | suite.out() << "test aux\n"; |
---|
51 | BamRead bam(b); |
---|
52 | |
---|
53 | char c = 'h'; |
---|
54 | int size = bam.aux_size(); |
---|
55 | bam.aux_append("ZZ", 'A', 1, (uint8_t*)(&c)); |
---|
56 | suite.out() << "size: " << size << " " << bam.aux_size() << "\n"; |
---|
57 | suite.add(bam.aux_size() == size+4); |
---|
58 | bam.aux(); |
---|
59 | bam.aux("ZZ"); |
---|
60 | char c1 = bam_aux2A(bam.aux("ZZ")); |
---|
61 | suite.out() << c << "==" << c1 << "\n"; |
---|
62 | suite.add(c==c1); |
---|
63 | bam.aux_del("ZZ"); |
---|
64 | if (!suite.add(bam.aux_size() == size)) |
---|
65 | suite.err() << "error: incorrect size: " << bam.aux_size() << "\n"; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | void test_cigar(test::Suite& suite, const BamRead& b, const BamHeader& hdr) |
---|
70 | { |
---|
71 | BamRead bam(b); |
---|
72 | |
---|
73 | bam.cigar(); |
---|
74 | bam.cigar(0); |
---|
75 | bam.cigar_op(0); |
---|
76 | bam.cigar_oplen(0); |
---|
77 | |
---|
78 | suite.out() << "test cigar:\n"; |
---|
79 | suite.out() << bam.cigar_str() << "\n"; |
---|
80 | OutBamFile os("cigar_test.bam", hdr); |
---|
81 | utility::Aligner::Cigar cig; |
---|
82 | |
---|
83 | bam.cigar(cig); |
---|
84 | suite.out() << bam.cigar_str() << "\n"; |
---|
85 | suite.add(bam.cigar_str()==""); |
---|
86 | os.write(bam); |
---|
87 | |
---|
88 | cig.push_back(BAM_CMATCH, bam.sequence_length()); |
---|
89 | bam.cigar(cig); |
---|
90 | suite.out() << bam.cigar_str() << "\n"; |
---|
91 | suite.add(bam.cigar_str()=="100M"); |
---|
92 | os.write(bam); |
---|
93 | |
---|
94 | cig.clear(); |
---|
95 | cig.push_back(BAM_CMATCH, 50); |
---|
96 | cig.push_back(BAM_CDEL, 2); |
---|
97 | cig.push_back(BAM_CMATCH, 50); |
---|
98 | bam.cigar(cig); |
---|
99 | suite.out() << bam.cigar_str() << "\n"; |
---|
100 | suite.add(bam.cigar_str()=="50M2D50M"); |
---|
101 | os.write(bam); |
---|
102 | |
---|
103 | // check that other elements in data* is preserved |
---|
104 | if (!suite.add(same_query_name(b, bam))) |
---|
105 | suite.err() << "error: \n'" << b.name() << "'\n'" << bam.name() << "'\n"; |
---|
106 | |
---|
107 | if (!suite.add(b.sequence()==bam.sequence())) |
---|
108 | suite.err() << "error: \n'" << b.sequence() << "'\n'" |
---|
109 | << bam.sequence() << "'\n"; |
---|
110 | |
---|
111 | for (int32_t i=0; i<b.core().l_qseq; ++i) |
---|
112 | if (bam.qual(i) != b.qual(i)) { |
---|
113 | suite.err() << "error: qual: " << i << " " << bam.qual(i) |
---|
114 | << " not euqal " << b.qual(i) << "\n"; |
---|
115 | suite.add(false); |
---|
116 | } |
---|
117 | |
---|
118 | os.close(); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | void test_sequence(test::Suite& suite, const BamRead& b) |
---|
123 | { |
---|
124 | suite.out() << "test sequence\n"; |
---|
125 | BamRead bam(b); |
---|
126 | std::string seq = b.sequence(); |
---|
127 | std::vector<uint8_t> qual; |
---|
128 | qual.reserve(seq.size()); |
---|
129 | for (size_t i=0; i<seq.size(); ++i) |
---|
130 | qual.push_back(bam.qual(i)); |
---|
131 | assert(seq.size()==100); |
---|
132 | bam.sequence(seq, qual); |
---|
133 | |
---|
134 | if (bam.sequence()!=seq) { |
---|
135 | suite.add(false); |
---|
136 | suite.err() << "incorrect sequence:" << bam.sequence() << "\n"; |
---|
137 | suite.err() << "expected: " << seq << "\n"; |
---|
138 | } |
---|
139 | |
---|
140 | // just for consistency |
---|
141 | utility::Aligner::Cigar cig; |
---|
142 | bam.cigar(cig); |
---|
143 | bam.core().flag &= ~BAM_FUNMAP; |
---|
144 | |
---|
145 | // trim off one base |
---|
146 | seq.resize(seq.size()-1); |
---|
147 | qual.resize(qual.size()-1); |
---|
148 | bam.sequence(seq, qual); |
---|
149 | |
---|
150 | if (bam.sequence()!=seq) { |
---|
151 | suite.add(false); |
---|
152 | suite.err() << "incorrect sequence:" << bam.sequence() << "\n"; |
---|
153 | suite.err() << "expected: " << seq << "\n"; |
---|
154 | } |
---|
155 | |
---|
156 | // extend sequence |
---|
157 | seq.resize(120, 'A'); |
---|
158 | qual.resize(120, 0); |
---|
159 | bam.sequence(seq, qual); |
---|
160 | if (bam.sequence()!=seq) { |
---|
161 | suite.add(false); |
---|
162 | suite.err() << "incorrect sequence:" << bam.sequence() << "\n"; |
---|
163 | suite.err() << "expected: " << seq << "\n"; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void test_name(test::Suite& suite, const BamRead& b) |
---|
169 | { |
---|
170 | suite.out() << "test name\n"; |
---|
171 | BamRead bam(b); |
---|
172 | std::string name = "my-new-name"; |
---|
173 | bam.name(name); |
---|
174 | if (bam.name() != name) { |
---|
175 | suite.add(false); |
---|
176 | suite.err() << "error: name: '" << bam.name() |
---|
177 | << "' expected: '" << name << "'\n"; |
---|
178 | } |
---|
179 | // check that other fields are not changed |
---|
180 | if (bam.cigar_str()!=b.cigar_str()) { |
---|
181 | suite.add(false); |
---|
182 | suite.err() << "error: cigar str:" << bam.cigar_str() << " != " |
---|
183 | << b.cigar_str() << "\n"; |
---|
184 | } |
---|
185 | if (bam.sequence()!=b.sequence()) { |
---|
186 | suite.add(false); |
---|
187 | suite.err() << "error: sequence:" << bam.sequence() << " != " |
---|
188 | << b.sequence() << "\n"; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | void test_open3(test::Suite& suite, const BamHeader& header) |
---|
194 | { |
---|
195 | OutBamFile out("yaya.bam", header, 0); |
---|
196 | out.close(); |
---|
197 | out.open("yaya.bam", header, 9); |
---|
198 | out.close(); |
---|
199 | try { |
---|
200 | out.open("yaya.bam", header, 10); |
---|
201 | suite.add(false); |
---|
202 | suite.err() << "open(\"yaya.bam\", header, 10): did not throw\n"; |
---|
203 | } |
---|
204 | catch (std::invalid_argument& e) { |
---|
205 | suite.err() << "expected exception: " << e.what() << "\n"; |
---|
206 | } |
---|
207 | out.close(); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | void test_write(test::Suite& suite, const BamHeader& header) |
---|
212 | { |
---|
213 | OutBamFile out; |
---|
214 | BamRead read; |
---|
215 | try { |
---|
216 | // trying to write to a file not open |
---|
217 | out.write(read); |
---|
218 | suite.add(false); |
---|
219 | suite.err() << "write(BamRead): did not throw\n"; |
---|
220 | } |
---|
221 | catch (utility::runtime_error& e) { |
---|
222 | suite.err() << "expected exception: " << e.what() << "\n"; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | void test1(test::Suite& suite) |
---|
228 | { |
---|
229 | std::string file = "../../data/foo.sorted.bam"; |
---|
230 | |
---|
231 | InBamFile in; |
---|
232 | in.open(file); |
---|
233 | BamRead bam; |
---|
234 | in.read(bam); |
---|
235 | // test that functions are implemented |
---|
236 | bam.end(); |
---|
237 | bam.flag(); |
---|
238 | bam.mpos(); |
---|
239 | bam.mtid(); |
---|
240 | bam.pos(); |
---|
241 | bam.tid(); |
---|
242 | bam.swap(bam); |
---|
243 | std::string str = "AGCTTTGTTATTATTTGAGGGTGTGGTTCAGTTGTAAACAGTGTATG" |
---|
244 | "TTTTAGAATTTGTGTTATTGTGATGGCGATGACCAAGTACAACATATTTCCCA"; |
---|
245 | std::string seq = bam.sequence(); |
---|
246 | if (str!=seq) { |
---|
247 | suite.err() << "incorrect sequence: '" << bam.sequence() << "'\n"; |
---|
248 | suite.err() << "expected: '" << str << "'\n"; |
---|
249 | suite.add(false); |
---|
250 | } |
---|
251 | uint8_t c = bam.sequence(24); |
---|
252 | if (c != 4) { |
---|
253 | suite.add(false); |
---|
254 | suite.err() << "error: sequence: " << static_cast<int>(c) |
---|
255 | << " expected " << 4 << "\n"; |
---|
256 | } |
---|
257 | bam.sequence(24, 8); |
---|
258 | c = bam.sequence(24); |
---|
259 | if (c != 8) { |
---|
260 | suite.add(false); |
---|
261 | suite.err() << "error: sequence: " << static_cast<int>(c) |
---|
262 | << " expected " << 8 << "\n"; |
---|
263 | } |
---|
264 | uint8_t q = bam.qual(24); |
---|
265 | if (q != 72) { |
---|
266 | suite.add(false); |
---|
267 | suite.err() << "error: quality: " << static_cast<int>(q) |
---|
268 | << " expected " << 72 << "\n"; |
---|
269 | } |
---|
270 | uint8_t new_q = 60; |
---|
271 | bam.qual(24, new_q); |
---|
272 | q = bam.qual(24); |
---|
273 | if (q != new_q) { |
---|
274 | suite.add(false); |
---|
275 | suite.err() << "error: quality: " << static_cast<int>(q) |
---|
276 | << " expected " << new_q << "\n"; |
---|
277 | } |
---|
278 | test_cigar(suite, bam, in.header()); |
---|
279 | test_aux(suite, bam); |
---|
280 | test_sequence(suite, bam); |
---|
281 | test_name(suite, bam); |
---|
282 | test_open3(suite, in.header()); |
---|
283 | test_write(suite, in.header()); |
---|
284 | } |
---|
285 | #endif |
---|