| 1 |
#include <iostream> |
| 2 |
|
| 3 |
#include "../src/interval_tree.h" |
| 4 |
#include "../src/primer_bed.h" |
| 5 |
#include "../src/trim_primer_quality.h" |
| 6 |
#include "htslib/sam.h" |
| 7 |
|
| 8 |
typedef struct { |
| 9 |
uint16_t flag; |
| 10 |
uint32_t n_cigar; |
| 11 |
hts_pos_t isize; |
| 12 |
uint8_t fail; |
| 13 |
} result_t; |
| 14 |
|
| 15 |
int test_trim(uint8_t min_qual, uint8_t sliding_window, bool no_write_flag, |
| 16 |
bool keep_for_reanalysis, int min_length, std::string testname, |
| 17 |
int nexpected, result_t *expected) { |
| 18 |
int success = 0; |
| 19 |
int res; |
| 20 |
|
| 21 |
bam1_t *aln = bam_init1(); |
| 22 |
|
| 23 |
std::string bam = "../data/test.unmapped.sorted.bam"; |
| 24 |
std::string bed = "../data/test.bed"; |
| 25 |
std::string pair_info = ""; |
| 26 |
int32_t primer_offset = 0; |
| 27 |
std::string prefix = "/tmp/trim"; |
| 28 |
std::string bam_out = "/tmp/trim.bam"; |
| 29 |
|
| 30 |
std::string region_ = ""; |
| 31 |
std::string cmd = "@PG\tID:ivar-trim\tPN:ivar\tVN:1.0.0\tCL:ivar trim\n"; |
| 32 |
|
| 33 |
// Test and check result |
| 34 |
res = trim_bam_qual_primer( |
| 35 |
bam, bed, prefix, min_qual, sliding_window, cmd, no_write_flag, |
| 36 |
keep_for_reanalysis, min_length, pair_info, primer_offset); |
| 37 |
if (res) { |
| 38 |
success = -1; |
| 39 |
std::cerr << testname << " failed: trim_bam_qual_primer() returned " << res |
| 40 |
<< std::endl; |
| 41 |
} else { |
| 42 |
samFile *in = hts_open(bam_out.c_str(), "r"); |
| 43 |
if (!in) { |
| 44 |
success = -1; |
| 45 |
std::cerr << testname << " failed: Can't open " << bam_out << std::endl; |
| 46 |
} else { |
| 47 |
sam_hdr_t *hdr = sam_hdr_read(in); |
| 48 |
if (!hdr) { |
| 49 |
success = -1; |
| 50 |
std::cerr << testname << " failed: Can't read header from " << bam_out |
| 51 |
<< std::endl; |
| 52 |
} else { |
| 53 |
int n = 0; |
| 54 |
while (sam_read1(in, hdr, aln) >= 0) { |
| 55 |
std::cerr << (aln->core.flag & BAM_FQCFAIL ? "FAIL" : "Pass") << "\t"; |
| 56 |
std::cerr << aln->core.n_cigar << "\t" << aln->core.l_qseq << "\t" |
| 57 |
<< aln->core.mtid << "\t" << aln->core.mpos << "\t" |
| 58 |
<< aln->core.isize << std::endl; |
| 59 |
if (aln->core.isize != expected[n].isize) { |
| 60 |
success = -1; |
| 61 |
std::cerr << testname << " test failed: found isize " |
| 62 |
<< aln->core.isize << " at record " << n << ": expected " |
| 63 |
<< expected[n].isize << std::endl; |
| 64 |
} |
| 65 |
if (!(aln->core.flag & BAM_FQCFAIL) != !expected[n].fail) { |
| 66 |
success = -1; |
| 67 |
std::cerr << testname << " test failed: found " |
| 68 |
<< (aln->core.flag & BAM_FQCFAIL ? "FAIL" : "Pass") |
| 69 |
<< " at record " << n << ": expected " |
| 70 |
<< (expected[n].fail ? "FAIL" : "Pass") << std::endl; |
| 71 |
} |
| 72 |
n++; |
| 73 |
} |
| 74 |
if (n != nexpected) { |
| 75 |
success = -1; |
| 76 |
std::cerr << testname << " test failed: found " << n |
| 77 |
<< " records: expected " << nexpected << std::endl; |
| 78 |
} |
| 79 |
sam_hdr_destroy(hdr); |
| 80 |
} |
| 81 |
sam_close(in); |
| 82 |
} |
| 83 |
} |
| 84 |
return success; |
| 85 |
} |
| 86 |
|
| 87 |
int main() { |
| 88 |
int success = 0; |
| 89 |
|
| 90 |
// Default paramaters |
| 91 |
{ |
| 92 |
result_t expected[5] = {{163, 2, 364, 0}, |
| 93 |
{163, 2, 382, 0}, |
| 94 |
{83, 2, -356, 0}, |
| 95 |
{83, 2, -382, 0}, |
| 96 |
{67, 2, -398, 0}}; |
| 97 |
if (test_trim(20, 4, false, false, 30, "default paramaters", 5, expected)) |
| 98 |
success = -1; |
| 99 |
} |
| 100 |
|
| 101 |
// -e (write missing primers) |
| 102 |
{ |
| 103 |
result_t expected[9] = { |
| 104 |
{131, 7, 403, 0}, {163, 2, 364, 0}, {163, 2, 382, 0}, |
| 105 |
{163, 2, 242, 0}, {83, 2, -242, 0}, {147, 2, -150, 0}, |
| 106 |
{83, 2, -356, 0}, {83, 2, -382, 0}, {67, 2, -398, 0}}; |
| 107 |
if (test_trim(20, 4, true, false, 30, "write missing primers", 9, expected)) |
| 108 |
success = -1; |
| 109 |
} |
| 110 |
|
| 111 |
// -k (keep for reanalysis) |
| 112 |
{ |
| 113 |
result_t expected[10] = {{643, 7, 403, 1}, {163, 2, 364, 0}, |
| 114 |
{163, 2, 382, 0}, {675, 2, 242, 1}, |
| 115 |
{595, 2, -242, 1}, {611, 2, -150, 1}, |
| 116 |
{659, 2, -150, 1}, {83, 2, -356, 0}, |
| 117 |
{83, 2, -382, 0}, {67, 2, -398, 0}}; |
| 118 |
if (test_trim(20, 4, false, true, 30, "keep for reanalysis", 10, expected)) |
| 119 |
success = -1; |
| 120 |
} |
| 121 |
|
| 122 |
// -e -k (all flags) |
| 123 |
{ |
| 124 |
result_t expected[10] = {{643, 7, 403, 0}, {163, 2, 364, 0}, |
| 125 |
{163, 2, 382, 0}, {675, 2, 242, 0}, |
| 126 |
{595, 2, -242, 0}, {611, 2, -150, 1}, |
| 127 |
{659, 2, -150, 0}, {83, 2, -356, 0}, |
| 128 |
{83, 2, -382, 0}, {67, 2, -398, 0}}; |
| 129 |
if (test_trim(20, 4, true, true, 30, "both flags", 10, expected)) |
| 130 |
success = -1; |
| 131 |
} |
| 132 |
|
| 133 |
return success; |
| 134 |
} |