source: trunk/test/bam_header.cc @ 3409

Last change on this file since 3409 was 3409, checked in by Peter, 8 years ago

refs #817. Fix bug that struct was not updated in text(1) function (in libbam mode); add test to catch bug.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: bam_header.cc 3409 2015-04-17 01:07:04Z peter $
2//
3// Copyright (C) 2013, 2014, 2015 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 YAT_HAVE_LIBBAM
23#include "yat/omic/BamFile.h"
24#include "yat/omic/BamHeader.h"
25#endif
26
27using namespace theplu::yat;
28
29void test1(test::Suite& suite);
30
31int main(int argc, char* argv[])
32{
33  test::Suite suite(argc, argv, true);
34  try {
35    test1(suite);
36  }
37  catch (std::runtime_error& e) {
38    suite.err() << "what: " << e.what() << "\n";
39    suite.add(false);
40  }
41
42  return suite.return_value();
43}
44
45void test1(test::Suite& suite)
46{
47#ifdef YAT_HAVE_LIBBAM
48  using namespace omic;
49  std::string file = "../../data/foo.sorted.bam";
50
51  InBamFile bam_stream(file);
52  BamHeader hdr = bam_stream.header();
53  suite.out() << hdr.n_targets() << "\n";
54  suite.add(hdr.n_targets()==84);
55  suite.out() << hdr.target_name(0) << "\n";
56  if (!suite.add(std::string("1")==hdr.target_name(0)))
57    suite.err() << "error\n";
58  int32_t tid = hdr.tid("1");
59  suite.out() << "tid: " << tid << "\n";
60  if (!suite.add(tid==0))
61    suite.err() << "error\n";
62
63  int begin, end;
64  hdr.parse_region("GL000197.1", tid, begin, end);
65  suite.out() << tid << " " << begin << " " << end << "\n";
66  suite.add(tid==35);
67  hdr.parse_region("GL000197.1:1000-2000", tid, begin, end);
68  suite.out() << tid << " " << begin << " " << end << "\n";
69  if (!suite.add(tid==35))
70    suite.err() << "incorrect tid\n";
71  if (!suite.add(begin==999))
72    suite.err() << "incorrect begin\n";
73  if (!suite.add(end==2000))
74    suite.err() << "incorrect end\n";
75
76  suite.out() << hdr.target_name(35) << "\n";
77  if (!suite.add(std::string("GL000197.1")==hdr.target_name(35)))
78    suite.err() << "error\n";
79  suite.out() << hdr.target_length(0) << "\n";
80  suite.add(hdr.target_length(0)==249250621);
81
82  std::string str = hdr.text();
83  std::string str1(str);
84  str1 += "@PG\tID:prog\tVN:1.0\n";
85  omic::BamHeader hdr2(hdr);
86  hdr.text(str1);
87  std::string str2 = hdr.text();
88  if (str2.substr(0, str.size()) != str) {
89    suite.add(false);
90    suite.err() << "incorrect text:\n" << str2 << "\nexpected:\n"
91                << str1 << "\n";
92  }
93  else if (str2 != str1) {
94    suite.add(false);
95    suite.err() << "error: line was not added to header\n";
96  }
97  if (hdr2.text() != str) {
98    suite.add(false);
99    suite.err() << "function text(1) affects copies\n";
100  }
101
102  int32_t n_chr = hdr.n_targets();
103
104  // check that we can add another chromosomevia a @SG field
105  std::stringstream ss(hdr.text());
106  std::string str3;
107  std::string line;
108  int32_t n_targ = 0;
109  while (getline(ss, line)) {
110    str3 += line + "\n";
111    if (line.substr(0,3) == "@SQ") {
112      ++n_targ;
113      if (n_targ == n_chr)
114        str3 += "@SQ\tSN:Zbababababa\tLN:123\n";
115    }
116  }
117  hdr.text(str3);
118
119  int32_t n = hdr.n_targets();
120  if (n != n_chr+1) {
121    suite.add(false);
122    suite.err() << "error: incorrect number of targets: " << n
123                << " expected " << (n_chr+1) << "\n";
124    suite.err() << "header text:\n" << hdr.text() << "===\n";
125  }
126
127#endif
128}
Note: See TracBrowser for help on using the repository browser.