source: branches/0.10-stable/test/bam_iterator.cc @ 2970

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

surround test1 call with #ifdef HAVE_LIBBAM since test1 is not implemented n that case

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