1 | // $Id: bam.cc 3028 2013-04-21 00:37:31Z peter $ |
---|
2 | // |
---|
3 | // Copyright (C) 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 HAVE_LIBBAM |
---|
23 | #include "yat/omic/BamFile.h" |
---|
24 | #include "yat/omic/BamRead.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <cassert> |
---|
28 | #include <string> |
---|
29 | |
---|
30 | using namespace theplu::yat; |
---|
31 | |
---|
32 | void test1(test::Suite& suite); |
---|
33 | |
---|
34 | int main(int argc, char* argv[]) |
---|
35 | { |
---|
36 | test::Suite suite(argc, argv); |
---|
37 | #ifndef HAVE_LIBBAM |
---|
38 | suite.out() << "no libbam\n"; |
---|
39 | return EXIT_SKIP; |
---|
40 | #endif |
---|
41 | #ifndef HAVE_SAMTOOLS |
---|
42 | suite.out() << "no samtools\n"; |
---|
43 | return EXIT_SKIP; |
---|
44 | #endif |
---|
45 | #ifdef HAVE_LIBBAM |
---|
46 | test1(suite); |
---|
47 | #endif |
---|
48 | return suite.return_value(); |
---|
49 | } |
---|
50 | |
---|
51 | #ifdef HAVE_LIBBAM |
---|
52 | using namespace omic; |
---|
53 | |
---|
54 | void test_aux(test::Suite& suite, const BamRead& b) |
---|
55 | { |
---|
56 | suite.out() << "test aux\n"; |
---|
57 | BamRead bam(b); |
---|
58 | |
---|
59 | char c = 'h'; |
---|
60 | int size = bam.aux_size(); |
---|
61 | bam.aux_append("ZZ", 'A', 1, (uint8_t*)(&c)); |
---|
62 | suite.out() << "size: " << size << " " << bam.aux_size() << "\n"; |
---|
63 | suite.add(bam.aux_size() == size+4); |
---|
64 | bam.aux("ZZ"); |
---|
65 | char c1 = bam_aux2A(bam.aux("ZZ")); |
---|
66 | suite.out() << c << "==" << c1 << "\n"; |
---|
67 | suite.add(c==c1); |
---|
68 | bam.aux_del("ZZ"); |
---|
69 | if (!suite.add(bam.aux_size() == size)) |
---|
70 | suite.err() << "error: incorrect size: " << bam.aux_size() << "\n"; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | void test_cigar(test::Suite& suite, const BamRead& b, const BamHeader& hdr) |
---|
75 | { |
---|
76 | BamRead bam(b); |
---|
77 | |
---|
78 | suite.out() << "test cigar:\n"; |
---|
79 | suite.out() << bam.cigar_str() << "\n"; |
---|
80 | OutBamFile os("cigar_test.bam", hdr); |
---|
81 | std::vector<uint32_t> cig; |
---|
82 | |
---|
83 | bam.cigar(cig); |
---|
84 | suite.out() << bam.cigar_str() << "\n"; |
---|
85 | suite.add(bam.cigar_str()==""); |
---|
86 | os.write(bam); |
---|
87 | |
---|
88 | cig.resize(1); |
---|
89 | cig[0] = bam_cigar_gen(bam.sequence_length(), BAM_CMATCH); |
---|
90 | bam.cigar(cig); |
---|
91 | suite.out() << bam.cigar_str() << "\n"; |
---|
92 | suite.add(bam.cigar_str()=="100M"); |
---|
93 | os.write(bam); |
---|
94 | |
---|
95 | cig.resize(3); |
---|
96 | cig[0] = bam_cigar_gen(50, BAM_CMATCH); |
---|
97 | cig[1] = bam_cigar_gen(2, BAM_CDEL); |
---|
98 | cig[2] = bam_cigar_gen(50, BAM_CMATCH); |
---|
99 | bam.cigar(cig); |
---|
100 | suite.out() << bam.cigar_str() << "\n"; |
---|
101 | suite.add(bam.cigar_str()=="50M2D50M"); |
---|
102 | os.write(bam); |
---|
103 | |
---|
104 | // check that other elements in data* is preserved |
---|
105 | if (!suite.add(same_query_name(b, bam))) |
---|
106 | suite.err() << "error: \n'" << b.name() << "'\n'" << bam.name() << "'\n"; |
---|
107 | |
---|
108 | if (!suite.add(b.sequence()==bam.sequence())) |
---|
109 | suite.err() << "error: \n'" << b.sequence() << "'\n'" |
---|
110 | << bam.sequence() << "'\n"; |
---|
111 | |
---|
112 | for (int32_t i=0; i<b.core().l_qseq; ++i) |
---|
113 | if (bam.qual(i) != b.qual(i)) { |
---|
114 | suite.err() << "error: qual: " << i << " " << bam.qual(i) |
---|
115 | << " not euqal " << b.qual(i) << "\n"; |
---|
116 | suite.add(false); |
---|
117 | } |
---|
118 | |
---|
119 | os.close(); |
---|
120 | } |
---|
121 | |
---|
122 | void test1(test::Suite& suite) |
---|
123 | { |
---|
124 | std::string file = "../../data/foo.sorted.bam"; |
---|
125 | |
---|
126 | InBamFile in; |
---|
127 | in.open(file); |
---|
128 | BamRead bam; |
---|
129 | in.read(bam); |
---|
130 | std::string str = "AGCTTTGTTATTATTTGAGGGTGTGGTTCAGTTGTAAACAGTGTATG" |
---|
131 | "TTTTAGAATTTGTGTTATTGTGATGGCGATGACCAAGTACAACATATTTCCCA"; |
---|
132 | std::string seq = bam.sequence(); |
---|
133 | if (str!=seq) { |
---|
134 | suite.err() << "incorrect sequence: '" << bam.sequence() << "'\n"; |
---|
135 | suite.err() << "expected: '" << str << "'\n"; |
---|
136 | suite.add(false); |
---|
137 | } |
---|
138 | uint8_t c = bam.sequence(24); |
---|
139 | if (c != 4) { |
---|
140 | suite.add(false); |
---|
141 | suite.err() << "error: sequence: " << static_cast<int>(c) |
---|
142 | << " expected " << 4 << "\n"; |
---|
143 | } |
---|
144 | bam.sequence(24, 8); |
---|
145 | c = bam.sequence(24); |
---|
146 | if (c != 8) { |
---|
147 | suite.add(false); |
---|
148 | suite.err() << "error: sequence: " << static_cast<int>(c) |
---|
149 | << " expected " << 8 << "\n"; |
---|
150 | } |
---|
151 | uint8_t q = bam.qual(24); |
---|
152 | if (q != 72) { |
---|
153 | suite.add(false); |
---|
154 | suite.err() << "error: quality: " << static_cast<int>(q) |
---|
155 | << " expected " << 72 << "\n"; |
---|
156 | } |
---|
157 | uint8_t new_q = 60; |
---|
158 | bam.qual(24, new_q); |
---|
159 | q = bam.qual(24); |
---|
160 | if (q != new_q) { |
---|
161 | suite.add(false); |
---|
162 | suite.err() << "error: quality: " << static_cast<int>(q) |
---|
163 | << " expected " << new_q << "\n"; |
---|
164 | } |
---|
165 | test_cigar(suite, bam, in.header()); |
---|
166 | test_aux(suite, bam); |
---|
167 | } |
---|
168 | #endif |
---|