1 | // $Id: bam_header.cc 3408 2015-04-16 00:22:24Z peter $ |
---|
2 | // |
---|
3 | // Copyright (C) 2013, 2014 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 | |
---|
27 | using namespace theplu::yat; |
---|
28 | |
---|
29 | void test1(test::Suite& suite); |
---|
30 | |
---|
31 | int 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 | |
---|
45 | void 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 | #endif |
---|
103 | } |
---|