1 #include "primer_bed.h" 2 #include "htslib/sam.h" 3 #include "htslib/bgzf.h" 7 int rmv_reads_from_amplicon(std::string bam, std::string region_, std::string bam_out, uint16_t* amplicon,
int amp_n){
8 for (
int i = 0; i < amp_n; ++i){
9 std::cout <<
"Amplicon: " << amplicon[i] <<
" ";
12 std::cout << std::endl;
13 std::cout << bam_out << std::endl;
15 std::cout <<
"Bam in empty" << std::endl;
19 samFile *in = hts_open(bam.c_str(),
"r");
20 BGZF *out = bgzf_open(bam_out.c_str(),
"w");
22 std::cout << (
"Unable to open BAM/SAM file.") << std::endl;
26 hts_idx_t *idx = sam_index_load(in, bam.c_str());
28 std::cout << (
"Unable to open BAM/SAM index.") << std::endl;
32 bam_hdr_t *header = sam_hdr_read(in);
33 if(bam_hdr_write(out, header) < 0){
34 std::cout <<
"Unable to write BAM header to path." << std::endl;
40 std::cout <<
"Unable to open BAM/SAM header." << std::endl;
43 std::cout <<
"Number of references: " << header->n_targets << std::endl;
44 for (
int i = 0; i < header->n_targets; ++i){
45 std::cout <<
"Reference Name: " << header->target_name[i] << std::endl;
46 std::cout <<
"Reference Length: " << header->target_len[i] << std::endl;
48 region_.assign(header->target_name[i]);
51 std::cout <<
"Using Region: " << region_ << std::endl;
53 std::string temp(header->text);
54 std::string sortFlag (
"SO:coordinate");
55 if (temp.find(sortFlag)) {
56 std::cout <<
"Sorted By Coordinate" << std::endl;
58 std::cout <<
"Not sorted" << std::endl;
61 hts_itr_t *iter = NULL;
63 iter = sam_itr_querys(idx, header, region_.c_str());
64 if(header == NULL || iter == NULL) {
66 std::cout <<
"Unable to iterate to region within BAM/SAM." << std::endl;
70 bam1_t *aln = bam_init1();
73 while(sam_itr_next(in, iter, aln) >= 0) {
74 uint8_t* a = bam_aux_get(aln,
"xa");
77 for (
int i = 0; i < amp_n; ++i){
78 if(bam_aux2i(a) == amplicon[i]){
84 if(bam_write1(out, aln) < 0){
85 std::cout <<
"Not able to write to BAM" << std::endl;
86 hts_itr_destroy(iter);
89 bam_hdr_destroy(header);
96 if(ctr % 100000 == 0){
97 std::cout << ctr << std::endl;
100 hts_itr_destroy(iter);
101 hts_idx_destroy(idx);
103 bam_hdr_destroy(header);