source: trunk/test/bam_iterator.cc @ 2895

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

add two tests for bam functionality

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