source: trunk/test/bam_region_iterator.cc @ 3056

Last change on this file since 3056 was 3056, checked in by Peter, 10 years ago

fixes #764: throw if region in BamReadIterator? is invalid

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: bam_region_iterator.cc 3056 2013-06-24 06:37:21Z peter $
2//
3// Copyright (C) 2012, 2013 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 HAVE_LIBBAM
23#include "yat/omic/BamReadIterator.h"
24#include "yat/omic/BamWriteIterator.h"
25#include "yat/omic/config_bam.h"
26#endif
27
28#ifdef YAT_BAM_HEADER
29#include YAT_BAM_HEADER
30#endif
31
32#include <yat/utility/Exception.h>
33
34#include <algorithm>
35#include <cassert>
36#include <fstream>
37#include <iostream>
38#include <string>
39
40using namespace theplu::yat;
41
42void test1(test::Suite& suite);
43int main(int argc, char* argv[])
44{
45  test::Suite suite(argc, argv);
46#ifndef HAVE_LIBBAM
47  suite.out() << "no libbam\n";
48  return EXIT_SKIP;
49#endif
50#ifndef HAVE_SAMTOOLS
51  suite.out() << "no samtools\n";
52  return EXIT_SKIP;
53#endif
54
55  try {
56    test1(suite);
57  }
58  catch (std::runtime_error& e) {
59    suite.err() << "what: " << e.what() << "\n";
60    suite.add(false);
61  }
62
63  return suite.return_value();
64}
65
66void test1(test::Suite& suite)
67{
68#ifdef HAVE_LIBBAM
69  using namespace omic;
70  std::string file = "../../data/foo.sorted.bam";
71
72  InBamFile bam_stream(file);
73  if (!bam_stream.is_open()) {
74    suite.err() << "cannot read " << file << "\n";
75    suite.add(false);
76    return;
77  }
78  int begin = 24500;
79  int endpos = 24600;
80  BamReadIterator first(bam_stream, 1, begin, endpos);
81  BamReadIterator end;
82
83  int count = 0;
84  while (first != end) {
85    if (!suite.add(first->pos() < endpos) || !suite.add(first->end() > begin))
86      suite.err() << "error: " << first->pos() << "-" << first->end() << "\n";
87    ++count;
88    ++first;
89  }
90  suite.out() << count << std::endl;
91
92  if (count!=185) {
93    suite.err() << "count: " << count << "\nexpected: 185\n";
94    suite.add(false);
95  }
96
97  // test negative coordinates trigger exception
98  suite.out() << "test negative coordinates\n";
99  try {
100    BamReadIterator iter(bam_stream, 1, -2, -1);
101    suite.add(false);
102    suite.err() << "error: no exception thrown\n";
103  }
104  catch (utility::runtime_error& e) {
105    suite.out() << "expected exception caught with what(): '" << e.what()
106                << "'\n";
107  }
108
109#endif
110}
Note: See TracBrowser for help on using the repository browser.