1 | // $Id: pileup.cc 3432 2015-10-26 23:34:25Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2014, 2015 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "Suite.h" |
---|
25 | |
---|
26 | #include <yat/utility/config_public.h> |
---|
27 | |
---|
28 | #ifdef YAT_HAVE_LIBBAM |
---|
29 | #include <yat/omic/BamFile.h> |
---|
30 | #include <yat/omic/BamReadIterator.h> |
---|
31 | #include "yat/omic/Pileup.h" |
---|
32 | #endif |
---|
33 | |
---|
34 | #include <yat/utility/Aligner.h> |
---|
35 | |
---|
36 | #include <algorithm> |
---|
37 | #include <cassert> |
---|
38 | #include <string> |
---|
39 | #include <vector> |
---|
40 | |
---|
41 | using namespace theplu::yat; |
---|
42 | |
---|
43 | // The simplest of genotypers; reporting the first and second "thirdiles" |
---|
44 | std::string genotype(std::string str) |
---|
45 | { |
---|
46 | std::string res(" "); |
---|
47 | std::sort(str.begin(), str.end()); |
---|
48 | res[0] = str[str.size()/3]; |
---|
49 | res[1] = str[2*str.size()/3]; |
---|
50 | return res; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | char seq_nt16(size_t x) |
---|
55 | { |
---|
56 | #if YAT_HAVE_HTSLIB |
---|
57 | return seq_nt16_str[x]; |
---|
58 | #elif HAVE_BAM_NT16_REV_TABLE |
---|
59 | return bam_nt16_rev_table[x]; |
---|
60 | #else |
---|
61 | std::string table = "=ACMGRSVTWYHKDBN"; |
---|
62 | return table[x]; |
---|
63 | #endif |
---|
64 | } |
---|
65 | |
---|
66 | #ifdef YAT_HAVE_LIBBAM |
---|
67 | void test1(test::Suite& suite, const std::string& fn) |
---|
68 | { |
---|
69 | omic::InBamFile in; |
---|
70 | in.open(fn); |
---|
71 | |
---|
72 | using omic::BamReadIterator; |
---|
73 | BamReadIterator first(in); |
---|
74 | BamReadIterator last; |
---|
75 | |
---|
76 | size_t count = 0; |
---|
77 | std::vector<std::string> correct_gt; |
---|
78 | correct_gt.push_back("AA"); |
---|
79 | correct_gt.push_back("CC"); |
---|
80 | correct_gt.push_back("CC"); |
---|
81 | correct_gt.push_back("TT"); |
---|
82 | correct_gt.push_back("=A"); |
---|
83 | correct_gt.push_back("AA"); |
---|
84 | correct_gt.push_back("AA"); |
---|
85 | correct_gt.push_back("GG"); |
---|
86 | correct_gt.push_back("AA"); |
---|
87 | correct_gt.push_back("AA"); |
---|
88 | correct_gt.push_back("AA"); |
---|
89 | |
---|
90 | correct_gt.push_back("GG"); |
---|
91 | correct_gt.push_back("GG"); |
---|
92 | correct_gt.push_back("CC"); |
---|
93 | correct_gt.push_back("CC"); |
---|
94 | correct_gt.push_back("CC"); |
---|
95 | |
---|
96 | correct_gt.push_back("TT"); |
---|
97 | correct_gt.push_back("TT"); |
---|
98 | correct_gt.push_back("=G"); |
---|
99 | correct_gt.push_back("=C"); |
---|
100 | |
---|
101 | using omic::Pileup; |
---|
102 | Pileup<BamReadIterator> pileup(first, last); |
---|
103 | while (pileup.good()) { |
---|
104 | if (pileup.pos()+1 < 24341) { |
---|
105 | pileup.increment(); |
---|
106 | continue; |
---|
107 | } |
---|
108 | Pileup<BamReadIterator>::const_iterator iter = pileup.begin(); |
---|
109 | std::string str; |
---|
110 | for (; iter!=pileup.end(); ++iter) { |
---|
111 | if (iter->bam().end() <= pileup.pos()) { |
---|
112 | suite.err() << "error: " << pileup.pos() << " " |
---|
113 | << iter->bam().pos() << " " |
---|
114 | << iter->bam().end() << " " |
---|
115 | << iter->bam().cigar_str() |
---|
116 | << "\n"; |
---|
117 | suite.add(false); |
---|
118 | continue; |
---|
119 | } |
---|
120 | str += seq_nt16(iter->sequence()); |
---|
121 | } |
---|
122 | |
---|
123 | if (pileup.pos()+1 == 24341 && count!=0) { |
---|
124 | suite.err() << "incorrect count\n"; |
---|
125 | suite.add(false); |
---|
126 | } |
---|
127 | |
---|
128 | if (count < correct_gt.size()) |
---|
129 | if (genotype(str) != correct_gt[count]) { |
---|
130 | suite.err() << "incorrect genotype: " << genotype(str) << "\n"; |
---|
131 | suite.err() << "expected: " << correct_gt[count] << "\n"; |
---|
132 | suite.add(false); |
---|
133 | } |
---|
134 | |
---|
135 | if (count <= 3) { |
---|
136 | if (pileup.pos()+1 != static_cast<int>(count+24341)) { |
---|
137 | suite.err() << "incorrect pos or count\n"; |
---|
138 | suite.err() << "pos: " << pileup.pos()+1 << "\n"; |
---|
139 | suite.err() << "count: " << count << "\n"; |
---|
140 | suite.add(false); |
---|
141 | } |
---|
142 | } |
---|
143 | else if (count == 4 && !pileup.skip_ref() ) { |
---|
144 | suite.out() << "expected skip ref\n"; |
---|
145 | suite.add(false); |
---|
146 | } |
---|
147 | else if (count==5) { |
---|
148 | if (pileup.pos()+1 != static_cast<int>(count+24340)) { |
---|
149 | suite.err() << "incorrect pos or count\n"; |
---|
150 | suite.err() << "pos: " << pileup.pos()+1 << "\n"; |
---|
151 | suite.err() << "count: " << count << "\n"; |
---|
152 | suite.add(false); |
---|
153 | } |
---|
154 | } |
---|
155 | pileup.increment(); |
---|
156 | ++count; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void test2(test::Suite& suite, const std::string& fn) |
---|
162 | { |
---|
163 | std::ostringstream error; |
---|
164 | theplu::yat::omic::InBamFile in(fn); |
---|
165 | using omic::BamRead; |
---|
166 | using omic::BamReadIterator; |
---|
167 | using omic::Pileup; |
---|
168 | BamReadIterator first(in, 1, 24150, 25000); |
---|
169 | BamReadIterator last; |
---|
170 | |
---|
171 | std::vector<BamRead> reads(2); |
---|
172 | reads[0] = *first; |
---|
173 | ++first; |
---|
174 | reads[1] = *first; |
---|
175 | in.close(); |
---|
176 | std::string ref = reads[0].sequence(); |
---|
177 | |
---|
178 | suite.out() << "no modifications\n"; |
---|
179 | // no modification |
---|
180 | typedef std::vector<BamRead>::iterator iterator; |
---|
181 | typedef Pileup<iterator>::const_iterator plp_iterator; |
---|
182 | for (Pileup<iterator> plp(reads.begin(), reads.end());plp.pos()<24070; |
---|
183 | plp.increment()) { |
---|
184 | for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) { |
---|
185 | if (i->bam().pos()!=reads[1].pos()) |
---|
186 | continue; |
---|
187 | char nt = seq_nt16(i->sequence()); |
---|
188 | if (nt != ref[plp.pos()-reads[0].pos()]) { |
---|
189 | error << "'" << nt << "' not equal '" |
---|
190 | << ref[plp.pos()-reads[0].pos()] |
---|
191 | << "' at pos " << reads[0].pos() << " for bam with start pos: " |
---|
192 | << i->bam().pos(); |
---|
193 | throw std::runtime_error(error.str()); |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | // insertion |
---|
199 | suite.out() << "insertion\n"; |
---|
200 | assert(reads[1].sequence_length()==100); |
---|
201 | utility::Aligner::Cigar cig; |
---|
202 | cig.push_back(BAM_CMATCH, 10); |
---|
203 | cig.push_back(BAM_CINS, 1); |
---|
204 | cig.push_back(BAM_CMATCH, 89); |
---|
205 | reads[1].cigar(cig); |
---|
206 | int count = 0; |
---|
207 | for (Pileup<iterator> plp(reads.begin(), reads.end());plp.good(); |
---|
208 | plp.increment()) { |
---|
209 | for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) { |
---|
210 | if (!same_query_name(i->bam(), reads[1])) |
---|
211 | continue; |
---|
212 | ++count; |
---|
213 | suite.err() << count << "\n"; |
---|
214 | char nt = seq_nt16(i->sequence()); |
---|
215 | if (nt != reads[1].sequence()[count-1]) |
---|
216 | error << "nt: " << nt << " not as expected. count: " << count << "\n"; |
---|
217 | // first 10 matches |
---|
218 | if (count <= 10) { |
---|
219 | if (plp.skip_ref()) |
---|
220 | error << count << " unexpected skip_ref\n"; |
---|
221 | if (plp.pos() != reads[1].pos()+count-1) |
---|
222 | error << "count: " << count << " with pos: " << plp.pos() << "\n"; |
---|
223 | } |
---|
224 | else if (count==11) { // insertion |
---|
225 | if (!plp.skip_ref()) |
---|
226 | error << count << " unexpected skip_ref\n"; |
---|
227 | } |
---|
228 | else { |
---|
229 | if (plp.skip_ref()) |
---|
230 | error << count << " unexpected skip_ref\n"; |
---|
231 | if (plp.pos() != reads[1].pos()+count-2) |
---|
232 | error << "count: " << count << " with pos: " << plp.pos() << "\n"; |
---|
233 | } |
---|
234 | |
---|
235 | if (error.str().size()) |
---|
236 | throw std::runtime_error(error.str()); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | // deletion |
---|
242 | suite.out() << "deletion\n"; |
---|
243 | assert(reads[1].sequence_length()==100); |
---|
244 | cig.clear(); |
---|
245 | cig.push_back(BAM_CMATCH, 10); |
---|
246 | cig.push_back(BAM_CDEL, 1); |
---|
247 | cig.push_back(BAM_CMATCH, 90); |
---|
248 | assert(cig.size()==3); |
---|
249 | reads[1].cigar(cig); |
---|
250 | count = 0; |
---|
251 | for (Pileup<iterator> plp(reads.begin(), reads.end()); plp.good(); |
---|
252 | plp.increment()) { |
---|
253 | for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) { |
---|
254 | if (!same_query_name(i->bam(), reads[1])) |
---|
255 | continue; |
---|
256 | ++count; |
---|
257 | char nt = seq_nt16(i->sequence()); |
---|
258 | if (plp.pos() != reads[1].pos()+count-1) |
---|
259 | error << "count: " << count << " with pos: " << plp.pos() << "\n"; |
---|
260 | // first 10 matches |
---|
261 | if (count <= 10) { |
---|
262 | if (plp.skip_ref()) |
---|
263 | error << count << " unexpected skip_ref\n"; |
---|
264 | if (nt != reads[1].sequence()[count-1]) |
---|
265 | error << "nt: " << nt << " not as expected\n"; |
---|
266 | } |
---|
267 | else if (count==11) { |
---|
268 | if (plp.skip_ref()) |
---|
269 | error << count << " unexpected skip_ref\n"; |
---|
270 | if (!i->is_del()) |
---|
271 | error << count << " expected deletion\n"; |
---|
272 | } |
---|
273 | else { |
---|
274 | if (plp.skip_ref()) |
---|
275 | error << count << " unexpected skip_ref\n"; |
---|
276 | if (nt != reads[1].sequence()[count-2]) |
---|
277 | error << "nt: " << nt << " not as expected\n"; |
---|
278 | } |
---|
279 | |
---|
280 | if (error.str().size()) |
---|
281 | throw std::runtime_error(error.str()); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | // soft clipped |
---|
286 | suite.out() << "soft clipped\n"; |
---|
287 | assert(reads[1].sequence_length()==100); |
---|
288 | cig.clear(); |
---|
289 | cig.push_back(BAM_CSOFT_CLIP, 10); |
---|
290 | cig.push_back(BAM_CMATCH, 90); |
---|
291 | reads[1].cigar(cig); |
---|
292 | count = 0; |
---|
293 | for (Pileup<iterator> plp(reads.begin(), reads.end()); plp.good(); |
---|
294 | plp.increment()) { |
---|
295 | for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) { |
---|
296 | if (!same_query_name(i->bam(), reads[1])) |
---|
297 | continue; |
---|
298 | ++count; |
---|
299 | char nt = seq_nt16(i->sequence()); |
---|
300 | if (plp.skip_ref()) |
---|
301 | error << count << " unexpected skip_ref\n"; |
---|
302 | if (plp.pos() != reads[1].pos()+count-1) |
---|
303 | error << "count: " << count << " with pos: " << plp.pos() << "\n"; |
---|
304 | if (nt != reads[1].sequence()[count+9]) |
---|
305 | error << "count: " << count << " nt: " << nt << " not as expected\n"; |
---|
306 | |
---|
307 | if (error.str().size()) |
---|
308 | throw std::runtime_error(error.str()); |
---|
309 | } |
---|
310 | } |
---|
311 | } |
---|
312 | #else |
---|
313 | // tests if libbam is not available, should never be run |
---|
314 | void test1(test::Suite& suite, const std::string& fn) {assert(0);} |
---|
315 | void test2(test::Suite& suite, const std::string& fn) {assert(0);} |
---|
316 | #endif |
---|
317 | |
---|
318 | int main(int argc, char* argv[]) |
---|
319 | { |
---|
320 | test::Suite suite(argc, argv, true); |
---|
321 | std::string fn = "../../data/foo.sorted.bam"; |
---|
322 | try { |
---|
323 | test1(suite, fn); |
---|
324 | test2(suite, fn); |
---|
325 | } |
---|
326 | catch (std::runtime_error& e) { |
---|
327 | suite.err() << "test failed: " << e.what() << "\n"; |
---|
328 | suite.add(false); |
---|
329 | } |
---|
330 | return suite.return_value(); |
---|
331 | } |
---|