1 | // $Id: bam_iterator.cc 3209 2014-05-04 13:52:34Z 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 YAT_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 | |
---|
34 | using namespace theplu::yat; |
---|
35 | |
---|
36 | void test1(test::Suite& suite); |
---|
37 | |
---|
38 | int main(int argc, char* argv[]) |
---|
39 | { |
---|
40 | test::Suite suite(argc, argv, true); |
---|
41 | try { |
---|
42 | test1(suite); |
---|
43 | } |
---|
44 | catch (std::runtime_error& e) { |
---|
45 | suite.add(false); |
---|
46 | suite.err() << "error: what: " << e.what() << "\n"; |
---|
47 | } |
---|
48 | return suite.return_value(); |
---|
49 | } |
---|
50 | |
---|
51 | #ifdef YAT_HAVE_LIBBAM |
---|
52 | using namespace omic; |
---|
53 | |
---|
54 | struct BamReadEqual |
---|
55 | { |
---|
56 | bool operator()(const BamRead& lhs, const BamRead& rhs) const |
---|
57 | { |
---|
58 | // FIXME |
---|
59 | /* |
---|
60 | if (lhs.l_aux() != rhs.l_aux()) |
---|
61 | return false; |
---|
62 | if (lhs.data_len() != rhs.data_len()) |
---|
63 | return false; |
---|
64 | if (lhs.m_data() != rhs.m_data()) |
---|
65 | return false; |
---|
66 | for (int i=0; i<lhs.data_len(); ++i) |
---|
67 | if (lhs.data()[i] != rhs.data()[i]) |
---|
68 | return false; |
---|
69 | */ |
---|
70 | |
---|
71 | // comparing core |
---|
72 | if (lhs.tid() != rhs.tid()) |
---|
73 | return false; |
---|
74 | if (lhs.pos() != rhs.pos()) |
---|
75 | return false; |
---|
76 | if (lhs.mtid() != rhs.mtid()) |
---|
77 | return false; |
---|
78 | if (lhs.mpos() != rhs.mpos()) |
---|
79 | return false; |
---|
80 | if (lhs.core().bin != rhs.core().bin) |
---|
81 | return false; |
---|
82 | if (lhs.core().qual != rhs.core().qual) |
---|
83 | return false; |
---|
84 | if (lhs.core().l_qname != rhs.core().l_qname) |
---|
85 | return false; |
---|
86 | if (lhs.core().flag != rhs.core().flag) |
---|
87 | return false; |
---|
88 | if (lhs.core().n_cigar != rhs.core().n_cigar) |
---|
89 | return false; |
---|
90 | if (lhs.core().l_qseq != rhs.core().l_qseq) |
---|
91 | return false; |
---|
92 | if (lhs.core().mtid != rhs.core().mtid) |
---|
93 | return false; |
---|
94 | if (lhs.core().mpos != rhs.core().mpos) |
---|
95 | return false; |
---|
96 | if (lhs.core().isize != rhs.core().isize) |
---|
97 | return false; |
---|
98 | |
---|
99 | return true; |
---|
100 | } |
---|
101 | }; |
---|
102 | |
---|
103 | |
---|
104 | void test1(test::Suite& suite) |
---|
105 | { |
---|
106 | std::string file = "../../data/foo.sorted.bam"; |
---|
107 | |
---|
108 | InBamFile in; |
---|
109 | in.open(file); |
---|
110 | std::string outname("bam_iterator.bam"); |
---|
111 | OutBamFile out(outname, in.header()); |
---|
112 | BamWriteIterator bam_out(out); |
---|
113 | std::copy(BamReadIterator(in), BamReadIterator(), bam_out); |
---|
114 | if (false) // avoid running; only compile |
---|
115 | suite.test_output_iterator(bam_out); |
---|
116 | out.close(); |
---|
117 | |
---|
118 | // compare that copy has equally many elements |
---|
119 | in.close(); |
---|
120 | in.open(file); |
---|
121 | size_t n1 = std::distance(BamReadIterator(in), BamReadIterator()); |
---|
122 | suite.out() << "entries in " << file << ": " << n1 << "\n"; |
---|
123 | in.close(); |
---|
124 | |
---|
125 | InBamFile copy(outname); |
---|
126 | size_t n2 = std::distance(BamReadIterator(copy), BamReadIterator()); |
---|
127 | copy.close(); |
---|
128 | suite.out() << "entries in " << outname << ": " << n2 << "\n"; |
---|
129 | if (n1!=n2) { |
---|
130 | suite.out() << "error: not same number of entries in copy\n"; |
---|
131 | suite.add(false); |
---|
132 | } |
---|
133 | |
---|
134 | copy.open(outname); |
---|
135 | in.open(file); |
---|
136 | if (!std::equal(BamReadIterator(in), BamReadIterator(), |
---|
137 | BamReadIterator(copy), BamReadEqual())) { |
---|
138 | suite.out() << "error: " << file << " and " << outname << " not equal\n"; |
---|
139 | suite.add(false); |
---|
140 | } |
---|
141 | copy.close(); |
---|
142 | in.close(); |
---|
143 | unlink(outname.c_str()); |
---|
144 | |
---|
145 | } |
---|
146 | #else |
---|
147 | void test1(test::Suite& suite) {} |
---|
148 | #endif |
---|