source: trunk/test/bam_iterator.cc @ 2943

Last change on this file since 2943 was 2943, checked in by Peter, 9 years ago

merge release 0.10 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: bam_iterator.cc 2943 2013-01-04 06:38:38Z peter $
2//
3// Copyright (C) 2012 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/BamFile.h"
24#include "yat/omic/BamRead.h"
25#include "yat/omic/BamReadIterator.h"
26#include "yat/omic/BamWriteIterator.h"
27#endif
28
29#include <algorithm>
30#include <cassert>
31#include <fstream>
32#include <string>
33
34using namespace theplu::yat;
35
36void test1(test::Suite& suite);
37
38int main(int argc, char* argv[])
39{
40  test::Suite suite(argc, argv);
41#ifndef HAVE_LIBBAM
42  suite.out() << "no libbam\n";
43  return EXIT_SKIP;
44#endif
45#ifndef HAVE_SAMTOOLS
46  suite.out() << "no samtools\n";
47  return EXIT_SKIP;
48#endif
49  test1(suite);
50  return suite.return_value();
51}
52
53#ifdef HAVE_LIBBAM
54using namespace omic;
55
56struct BamReadEqual
57{
58  bool operator()(const BamRead& lhs, const BamRead& rhs) const
59  {
60    // FIXME
61    /*
62    if (lhs.l_aux() != rhs.l_aux())
63      return false;
64    if (lhs.data_len() != rhs.data_len())
65      return false;
66    if (lhs.m_data() != rhs.m_data())
67      return false;
68    for (int i=0; i<lhs.data_len(); ++i)
69      if (lhs.data()[i] != rhs.data()[i])
70        return false;
71    */
72
73    // comparing core
74    if (lhs.tid() != rhs.tid())
75      return false;
76    if (lhs.pos() != rhs.pos())
77      return false;
78    if (lhs.core().bin != rhs.core().bin)
79      return false;
80    if (lhs.core().qual != rhs.core().qual)
81      return false;
82    if (lhs.core().l_qname != rhs.core().l_qname)
83      return false;
84    if (lhs.core().flag != rhs.core().flag)
85      return false;
86    if (lhs.core().n_cigar != rhs.core().n_cigar)
87      return false;
88    if (lhs.core().l_qseq != rhs.core().l_qseq)
89      return false;
90    if (lhs.core().mtid != rhs.core().mtid)
91      return false;
92    if (lhs.core().mpos != rhs.core().mpos)
93      return false;
94    if (lhs.core().isize != rhs.core().isize)
95      return false;
96
97    return true;
98  }
99};
100
101
102void test1(test::Suite& suite)
103{
104  std::string file = "../../data/foo.sorted.bam";
105
106  InBamFile in;
107  in.open(file);
108  std::string outname("bam_iterator.bam");
109  OutBamFile out(outname, in.header());
110  std::copy(BamReadIterator(in), BamReadIterator(), BamWriteIterator(out));
111  out.close();
112
113  // compare that copy has equally many elements
114  in.close();
115  in.open(file);
116  size_t n1 = std::distance(BamReadIterator(in), BamReadIterator());
117  suite.out() << "entries in " << file << ": " << n1 << "\n";
118  in.close();
119
120  InBamFile copy(outname);
121  size_t n2 = std::distance(BamReadIterator(copy), BamReadIterator());
122  copy.close();
123  suite.out() << "entries in " << outname << ": " << n2 << "\n";
124  if (n1!=n2) {
125    suite.out() << "error: not same number of entries in copy\n";
126    suite.add(false);
127  }
128
129  copy.open(outname);
130  in.open(file);
131  if (!std::equal(BamReadIterator(in), BamReadIterator(),
132                  BamReadIterator(copy), BamReadEqual())) {
133    suite.out() << "error: " << file << " and " << outname << " not equal\n";
134    suite.add(false);
135  }
136  copy.close();
137  in.close();
138  unlink(outname.c_str());
139}
140#endif
Note: See TracBrowser for help on using the repository browser.