| 1 |
#include <iostream> |
| 2 |
|
| 3 |
#include "../src/trim_primer_quality.h" |
| 4 |
#include "htslib/sam.h" |
| 5 |
|
| 6 |
/* |
| 7 |
typedef struct { |
| 8 |
uint8_t cigar_flag[3]; |
| 9 |
uint32_t cigar_flag[3]; |
| 10 |
} result_t; |
| 11 |
*/ |
| 12 |
|
| 13 |
int test_isize_trim(uint8_t min_qual, uint8_t sliding_window, |
| 14 |
bool no_write_flag, bool keep_for_reanalysis, |
| 15 |
int min_length, std::string testname, |
| 16 |
uint8_t cigar_flag[14][3], uint32_t cigar_len[14][3]) { |
| 17 |
int success = 0; |
| 18 |
int res; |
| 19 |
uint32_t *cigar; |
| 20 |
|
| 21 |
bam1_t *aln = bam_init1(); |
| 22 |
|
| 23 |
std::string bam = "../data/test_isize.sorted.bam"; |
| 24 |
std::string bed = "../data/test_isize.bed"; |
| 25 |
std::string pair_info = ""; |
| 26 |
int32_t primer_offset = 0; |
| 27 |
std::string prefix = "../data/trim_isize"; |
| 28 |
std::string bam_out = "../data/trim_isize.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 |
|
| 38 |
if (res) { |
| 39 |
success = -1; |
| 40 |
std::cerr << testname << " failed: trim_bam_qual_primer() returned " << res |
| 41 |
<< std::endl; |
| 42 |
} else { |
| 43 |
samFile *in = hts_open(bam_out.c_str(), "r"); |
| 44 |
if (!in) { |
| 45 |
success = -1; |
| 46 |
std::cerr << testname << " failed: Can't open " << bam_out << std::endl; |
| 47 |
} else { |
| 48 |
sam_hdr_t *hdr = sam_hdr_read(in); |
| 49 |
if (!hdr) { |
| 50 |
success = -1; |
| 51 |
std::cerr << testname << " failed: Can't read header from " << bam_out |
| 52 |
<< std::endl; |
| 53 |
} else { |
| 54 |
int n = 0; |
| 55 |
while (sam_read1(in, hdr, aln) >= 0) { |
| 56 |
/* |
| 57 |
if (aln->core.isize != expected[n].isize) { |
| 58 |
success = -1; |
| 59 |
std::cerr << testname << " test failed: found isize " << |
| 60 |
aln->core.isize << " at record " << n << ": expected " << |
| 61 |
expected[n].isize << std::endl; |
| 62 |
} |
| 63 |
*/ |
| 64 |
cigar = bam_get_cigar(aln); |
| 65 |
for (uint i = 0; i < aln->core.n_cigar; ++i) { |
| 66 |
if (((cigar[i]) & BAM_CIGAR_MASK) != cigar_flag[n][i]) { |
| 67 |
success = -1; |
| 68 |
std::cout << "Cigar flag didn't match for " << bam_get_qname(aln) |
| 69 |
<< " on " << i + 1 << " op" |
| 70 |
<< "! Expected " << (uint)cigar_flag[n][i] << " " |
| 71 |
<< "Got " << ((cigar[i]) & BAM_CIGAR_MASK) << std::endl; |
| 72 |
} |
| 73 |
if ((((cigar[i]) >> BAM_CIGAR_SHIFT)) != cigar_len[n][i]) { |
| 74 |
success = -1; |
| 75 |
std::cout << "Cigar length didn't match for " |
| 76 |
<< bam_get_qname(aln) << " on " << i + 1 << " op" |
| 77 |
<< "! Expected " << (uint)cigar_len[n][i] << " " |
| 78 |
<< "Got " << ((cigar[i]) >> BAM_CIGAR_SHIFT) |
| 79 |
<< std::endl; |
| 80 |
} |
| 81 |
} |
| 82 |
n++; |
| 83 |
} |
| 84 |
sam_hdr_destroy(hdr); |
| 85 |
} |
| 86 |
sam_close(in); |
| 87 |
} |
| 88 |
} |
| 89 |
return success; |
| 90 |
} |
| 91 |
|
| 92 |
int main() { |
| 93 |
int success = 0; |
| 94 |
int test_res = 0; |
| 95 |
|
| 96 |
uint8_t cigar_flag[14][3] = {{BAM_CSOFT_CLIP, BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 97 |
{BAM_CSOFT_CLIP, BAM_CMATCH}, |
| 98 |
{BAM_CSOFT_CLIP, BAM_CMATCH}, |
| 99 |
{BAM_CSOFT_CLIP, BAM_CMATCH}, |
| 100 |
{BAM_CSOFT_CLIP, BAM_CMATCH}, |
| 101 |
{BAM_CSOFT_CLIP, BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 102 |
{BAM_CSOFT_CLIP, BAM_CMATCH}, |
| 103 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 104 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 105 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 106 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 107 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 108 |
{BAM_CMATCH, BAM_CSOFT_CLIP}, |
| 109 |
{BAM_CMATCH, BAM_CSOFT_CLIP}}; |
| 110 |
|
| 111 |
uint32_t cigar_len[14][3] = { |
| 112 |
{24, 363, 24}, {24, 155}, {24, 155}, {24, 64}, {24, 64}, |
| 113 |
{24, 363, 24}, {24, 155}, {156, 24}, {156, 24}, {156, 24}, |
| 114 |
{64, 24}, {64, 24}, {137, 10}, {137, 11}}; |
| 115 |
|
| 116 |
test_res = test_isize_trim(20, 4, false, false, 30, "default paramaters", |
| 117 |
cigar_flag, cigar_len); |
| 118 |
if (test_res) success = -1; |
| 119 |
|
| 120 |
return success; |
| 121 |
} |
| 122 |
/* |
| 123 |
#define BAM_CMATCH 0 |
| 124 |
#define BAM_CINS 1 |
| 125 |
#define BAM_CDEL 2 |
| 126 |
#define BAM_CREF_SKIP 3 |
| 127 |
#define BAM_CSOFT_CLIP 4 |
| 128 |
#define BAM_CHARD_CLIP 5 |
| 129 |
#define BAM_CPAD 6 |
| 130 |
#define BAM_CEQUAL 7 |
| 131 |
#define BAM_CDIFF 8 |
| 132 |
#define BAM_CBACK 9 |
| 133 |
|
| 134 |
#define BAM_CIGAR_STR "MIDNSHP=XB" |
| 135 |
#define BAM_CIGAR_SHIFT 4 |
| 136 |
#define BAM_CIGAR_MASK 0xf |
| 137 |
#define BAM_CIGAR_TYPE 0x3C1A7 |
| 138 |
*/ |