source: trunk/test/pileup.cc @ 3317

Last change on this file since 3317 was 3317, checked in by Peter, 7 years ago

refs #806 add test for Pileup class

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