Debian Codegraph @

tests/test_removereads.cpp

raw
1 #include <fstream>
2 #include <iostream>
3 #include <vector>
4
5 #include "../src/remove_reads_from_amplicon.h"
6 #include "htslib/sam.h"
7
8 int main() {
9 int num_success = 0, num_tests = 1;
10 std::vector<std::string> amp;
11 std::ifstream fin("../data/test.masked_primer_indices.txt");
12 std::string s, region = "Consensus_ZI-27_threshold_0_quality_20";
13 while (getline(fin, s, '\t')) {
14 amp.push_back(s);
15 }
16 rmv_reads_from_amplicon(
17 "../data/test.trimmed.sorted.bam", "", "../data/test.trimmed.masked", amp,
18 "../data/test.bed",
19 "@PG\tID:ivar-removereads\tPN:ivar\tVN:1.0.0\tCL:ivar removereads -i "
20 "../data/test.trimmed.sorted.bam -p ../data/test.trimmed.masked -t "
21 "../data/test.masked_primer_indices.txt -b ../data/test.bed\n\0");
22 std::string out_file = "../data/test.trimmed.masked.bam";
23 samFile *in = hts_open(out_file.c_str(), "r");
24 bam_hdr_t *header = sam_hdr_read(in);
25 hts_itr_t *iter = NULL;
26 if (sam_index_build2(out_file.c_str(), 0, 0) < 0) {
27 std::cerr << "Failed to build index" << std::endl;
28 return 1;
29 }
30 hts_idx_t *idx = sam_index_load(in, out_file.c_str());
31 iter = sam_itr_querys(idx, header, region.c_str());
32 bam1_t *aln = bam_init1();
33 bool w;
34 while (sam_itr_next(in, iter, aln) >= 0) {
35 uint8_t *a = bam_aux_get(aln, "XA");
36 w = true;
37 if (a != 0) {
38 if (bam_aux2i(a) == 2 || bam_aux2i(a) == 4 || bam_aux2i(a) == 6 ||
39 bam_aux2i(a) == 1 || bam_aux2i(a) == 3 || bam_aux2i(a) == 5) {
40 w = false;
41 }
42 }
43 if (!w) break;
44 }
45 if (w) num_success += 1;
46 hts_itr_destroy(iter);
47 hts_idx_destroy(idx);
48 bam_destroy1(aln);
49 bam_hdr_destroy(header);
50 sam_close(in);
51 return (num_success == num_tests) ? 0 : 1;
52 }