source: trunk/test/bam_pair_buffer.cc @ 4057

Last change on this file since 4057 was 4057, checked in by Peter, 7 months ago

closes #798; new classes BamPairBuffer? and a more general SortedBuffer?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: bam_pair_buffer.cc 4057 2021-03-31 05:46:00Z peter $
2//
3// Copyright (C) 2021 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#if YAT_HAVE_HTSLIB
23#include "yat/omic/algorithm.h"
24#include "yat/omic/BamFile.h"
25#include "yat/omic/BamPairBuffer.h"
26#include "yat/omic/BamReadFilter.h"
27#include "yat/omic/BamPairIterator2.h"
28#include "yat/omic/BamReadIterator.h"
29#endif
30
31#include <boost/iterator/filter_iterator.hpp>
32
33#include <iostream>
34
35using namespace theplu::yat;
36
37void test1(test::Suite& suite);
38
39int main(int argc, char* argv[])
40{
41  test::Suite suite(argc, argv, true);
42
43#ifdef YAT_HAVE_HTSLIB
44  test1(suite);
45#endif
46  return suite.return_value();
47}
48
49
50#ifdef YAT_HAVE_HTSLIB
51using namespace omic;
52
53
54template<typename InputIterator, typename OutputIterator>
55void process(InputIterator first, InputIterator last,
56             OutputIterator out)
57{
58  BamPairBuffer<OutputIterator> buffer(out);
59  BamPairIterator2<InputIterator> end(last, last);
60  for (BamPairIterator2<InputIterator> iter(first, last); iter!=end; ++iter)
61    buffer.push(iter->first(), iter->second());
62}
63
64
65void test1(test::Suite& suite)
66{
67  std::string file = "../../data/foo.sorted.bam";
68
69  InBamFile is(file);
70  BamReadIterator iter(is);
71  BamReadIterator end;
72  std::vector<BamRead> result;
73  BamReadFilter filter(0, 0, 2);
74  process(boost::make_filter_iterator(filter, iter, end),
75          boost::make_filter_iterator(filter, end),
76          std::back_inserter(result));
77  is.close();
78
79  if (result.empty()) {
80    suite.add(false);
81    suite.err() << "error: empty result\n";
82  }
83
84  for (size_t i=0; i<result.size(); ++i) {
85    if ((result[i].flag() & 2) == 0) {
86      suite.add(false);
87      suite.err() << "error: bit x02 not set in flag\n";
88    }
89  }
90
91  if (!std::is_sorted(result.begin(), result.end(), BamLessPos())) {
92    suite.add(false);
93    suite.err() << "error: result is not sorted\n";
94    BamLessPos less;
95    for (size_t i=0; i+1<result.size(); ++i)
96      if (less(result[i+1], result[i]))
97        suite.err() << "position of " << result[i].name() << ", "
98                    << result[i].pos() << ", is greater than "
99                    << "position of " << result[i+1].name() << ", "
100                    << result[i+1].pos() << ".\n";
101  }
102
103}
104#endif
Note: See TracBrowser for help on using the repository browser.