source: trunk/test/pileup.cc @ 3801

Last change on this file since 3801 was 3801, checked in by Peter, 4 years ago

implement reverse iterator in Pileup. closes #919

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1// $Id: pileup.cc 3801 2019-05-04 07:47:28Z 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
41using namespace theplu::yat;
42
43// The simplest of genotypers; reporting the first and second "thirdiles"
44std::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
54char 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
67void 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  Pileup<BamReadIterator>::const_reverse_iterator rend = pileup.rend();
159  std::distance(pileup.rbegin(), rend);
160}
161
162
163void test2(test::Suite& suite, const std::string& fn)
164{
165  std::ostringstream error;
166  theplu::yat::omic::InBamFile in(fn);
167  using omic::BamRead;
168  using omic::BamReadIterator;
169  using omic::Pileup;
170  BamReadIterator first(in, 1, 24150, 25000);
171  BamReadIterator last;
172
173  std::vector<BamRead> reads(2);
174  reads[0] = *first;
175  ++first;
176  reads[1] = *first;
177  in.close();
178  std::string ref = reads[0].sequence();
179
180  suite.out() << "no modifications\n";
181  // no modification
182  typedef std::vector<BamRead>::iterator iterator;
183  typedef Pileup<iterator>::const_iterator plp_iterator;
184  for (Pileup<iterator> plp(reads.begin(), reads.end());plp.pos()<24070;
185       plp.increment()) {
186    for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) {
187      if (i->bam().pos()!=reads[1].pos())
188        continue;
189      char nt = seq_nt16(i->sequence());
190      if (nt != ref[plp.pos()-reads[0].pos()]) {
191        error << "'" << nt << "' not equal '"
192              << ref[plp.pos()-reads[0].pos()]
193              << "' at pos " << reads[0].pos() << " for bam with start pos: "
194              << i->bam().pos();
195        throw std::runtime_error(error.str());
196      }
197    }
198  }
199
200  // insertion
201  suite.out() << "insertion\n";
202  assert(reads[1].sequence_length()==100);
203  utility::Aligner::Cigar cig;
204  cig.push_back(BAM_CMATCH, 10);
205  cig.push_back(BAM_CINS, 1);
206  cig.push_back(BAM_CMATCH, 89);
207  reads[1].cigar(cig);
208  int count = 0;
209  for (Pileup<iterator> plp(reads.begin(), reads.end());plp.good();
210       plp.increment()) {
211    for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) {
212      if (!same_query_name(i->bam(), reads[1]))
213        continue;
214      ++count;
215      suite.err() << count << "\n";
216      char nt = seq_nt16(i->sequence());
217      if (nt != reads[1].sequence()[count-1])
218        error << "nt: " << nt << " not as expected. count: " << count << "\n";
219      // first 10 matches
220      if (count <= 10) {
221        if (plp.skip_ref())
222          error << count << " unexpected skip_ref\n";
223        if (plp.pos() != reads[1].pos()+count-1)
224          error << "count: " << count << " with pos: " << plp.pos() << "\n";
225      }
226      else if (count==11) { // insertion
227        if (!plp.skip_ref())
228          error << count << " unexpected skip_ref\n";
229      }
230      else {
231        if (plp.skip_ref())
232          error << count << " unexpected skip_ref\n";
233        if (plp.pos() != reads[1].pos()+count-2)
234          error << "count: " << count << " with pos: " << plp.pos() << "\n";
235      }
236
237      if (error.str().size())
238        throw std::runtime_error(error.str());
239    }
240  }
241
242
243  // deletion
244  suite.out() << "deletion\n";
245  assert(reads[1].sequence_length()==100);
246  cig.clear();
247  cig.push_back(BAM_CMATCH, 10);
248  cig.push_back(BAM_CDEL, 1);
249  cig.push_back(BAM_CMATCH, 90);
250  assert(cig.size()==3);
251  reads[1].cigar(cig);
252  count = 0;
253  for (Pileup<iterator> plp(reads.begin(), reads.end()); plp.good();
254       plp.increment()) {
255    for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) {
256      if (!same_query_name(i->bam(), reads[1]))
257        continue;
258      ++count;
259      char nt = seq_nt16(i->sequence());
260      if (plp.pos() != reads[1].pos()+count-1)
261        error << "count: " << count << " with pos: " << plp.pos() << "\n";
262      // first 10 matches
263      if (count <= 10) {
264        if (plp.skip_ref())
265          error << count << " unexpected skip_ref\n";
266        if (nt != reads[1].sequence()[count-1])
267          error << "nt: " << nt << " not as expected\n";
268      }
269      else if (count==11) {
270        if (plp.skip_ref())
271          error << count << " unexpected skip_ref\n";
272        if (!i->is_del())
273          error << count << " expected deletion\n";
274      }
275      else {
276        if (plp.skip_ref())
277          error << count << " unexpected skip_ref\n";
278        if (nt != reads[1].sequence()[count-2])
279          error << "nt: " << nt << " not as expected\n";
280      }
281
282      if (error.str().size())
283        throw std::runtime_error(error.str());
284    }
285  }
286
287  // soft clipped
288  suite.out() << "soft clipped\n";
289  assert(reads[1].sequence_length()==100);
290  cig.clear();
291  cig.push_back(BAM_CSOFT_CLIP, 10);
292  cig.push_back(BAM_CMATCH, 90);
293  reads[1].cigar(cig);
294  count = 0;
295  for (Pileup<iterator> plp(reads.begin(), reads.end()); plp.good();
296       plp.increment()) {
297    for (plp_iterator i = plp.begin(); i!=plp.end(); ++i) {
298      if (!same_query_name(i->bam(), reads[1]))
299        continue;
300      ++count;
301      char nt = seq_nt16(i->sequence());
302      if (plp.skip_ref())
303        error << count << " unexpected skip_ref\n";
304      if (plp.pos() != reads[1].pos()+count-1)
305        error << "count: " << count << " with pos: " << plp.pos() << "\n";
306      if (nt != reads[1].sequence()[count+9])
307        error << "count: " << count << " nt: " << nt << " not as expected\n";
308
309      if (error.str().size())
310        throw std::runtime_error(error.str());
311    }
312  }
313}
314#else
315// tests if libbam is not available, should never be run
316void test1(test::Suite& suite, const std::string& fn) {assert(0);}
317void test2(test::Suite& suite, const std::string& fn) {assert(0);}
318#endif
319
320int main(int argc, char* argv[])
321{
322  test::Suite suite(argc, argv, true);
323  std::string fn = "../../data/foo.sorted.bam";
324  try {
325    test1(suite, fn);
326    test2(suite, fn);
327  }
328  catch (std::runtime_error& e) {
329    suite.err() << "test failed: " << e.what() << "\n";
330    suite.add(false);
331  }
332  return suite.return_value();
333}
Note: See TracBrowser for help on using the repository browser.