source: trunk/test/bam_iterator.cc @ 2986

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

merge 0.10-stable branch into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1// $Id: bam_iterator.cc 2986 2013-02-18 08:07:44Z 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/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  try {
50    test1(suite);
51  }
52  catch (std::runtime_error& e) {
53    suite.add(false);
54    suite.err() << "error: what: " << e.what() << "\n";
55  }
56  return suite.return_value();
57}
58
59#ifdef HAVE_LIBBAM
60using namespace omic;
61
62struct BamReadEqual
63{
64  bool operator()(const BamRead& lhs, const BamRead& rhs) const
65  {
66    // FIXME
67    /*
68    if (lhs.l_aux() != rhs.l_aux())
69      return false;
70    if (lhs.data_len() != rhs.data_len())
71      return false;
72    if (lhs.m_data() != rhs.m_data())
73      return false;
74    for (int i=0; i<lhs.data_len(); ++i)
75      if (lhs.data()[i] != rhs.data()[i])
76        return false;
77    */
78
79    // comparing core
80    if (lhs.tid() != rhs.tid())
81      return false;
82    if (lhs.pos() != rhs.pos())
83      return false;
84    if (lhs.mtid() != rhs.mtid())
85      return false;
86    if (lhs.mpos() != rhs.mpos())
87      return false;
88    if (lhs.core().bin != rhs.core().bin)
89      return false;
90    if (lhs.core().qual != rhs.core().qual)
91      return false;
92    if (lhs.core().l_qname != rhs.core().l_qname)
93      return false;
94    if (lhs.core().flag != rhs.core().flag)
95      return false;
96    if (lhs.core().n_cigar != rhs.core().n_cigar)
97      return false;
98    if (lhs.core().l_qseq != rhs.core().l_qseq)
99      return false;
100    if (lhs.core().mtid != rhs.core().mtid)
101      return false;
102    if (lhs.core().mpos != rhs.core().mpos)
103      return false;
104    if (lhs.core().isize != rhs.core().isize)
105      return false;
106
107    return true;
108  }
109};
110
111
112void test1(test::Suite& suite)
113{
114  std::string file = "../../data/foo.sorted.bam";
115
116  InBamFile in;
117  in.open(file);
118  std::string outname("bam_iterator.bam");
119  OutBamFile out(outname, in.header());
120  std::copy(BamReadIterator(in), BamReadIterator(), BamWriteIterator(out));
121  out.close();
122
123  // compare that copy has equally many elements
124  in.close();
125  in.open(file);
126  size_t n1 = std::distance(BamReadIterator(in), BamReadIterator());
127  suite.out() << "entries in " << file << ": " << n1 << "\n";
128  in.close();
129
130  InBamFile copy(outname);
131  size_t n2 = std::distance(BamReadIterator(copy), BamReadIterator());
132  copy.close();
133  suite.out() << "entries in " << outname << ": " << n2 << "\n";
134  if (n1!=n2) {
135    suite.out() << "error: not same number of entries in copy\n";
136    suite.add(false);
137  }
138
139  copy.open(outname);
140  in.open(file);
141  if (!std::equal(BamReadIterator(in), BamReadIterator(),
142                  BamReadIterator(copy), BamReadEqual())) {
143    suite.out() << "error: " << file << " and " << outname << " not equal\n";
144    suite.add(false);
145  }
146  copy.close();
147  in.close();
148  unlink(outname.c_str());
149}
150#endif
Note: See TracBrowser for help on using the repository browser.