| 1 | #include "remove_reads_from_amplicon.h" |
| 2 | |
| 3 | int rmv_reads_from_amplicon(std::string bam, std::string region_, |
| 4 | std::string bam_out, std::vector<std::string> amp, |
| 5 | std::string bed, std::string cmd) { |
| 6 | std::vector<primer> primers = populate_from_file(bed); |
| 7 | if (primers.size() == 0) { |
| 8 | return 0; |
| 9 | } |
| 10 | bam_out += ".bam"; |
| 11 | std::cout << "Writing to " << bam_out << std::endl; |
| 12 | if (bam.empty()) { |
| 13 | std::cout << "Bam in empty" << std::endl; |
| 14 | return 0; |
| 15 | } |
| 16 | // open BAM for reading |
| 17 | samFile *in = hts_open(bam.c_str(), "r"); |
| 18 | BGZF *out = bgzf_open(bam_out.c_str(), "w"); |
| 19 | if (in == NULL) { |
| 20 | std::cout << ("Unable to open BAM/SAM file.") << std::endl; |
| 21 | return -1; |
| 22 | } |
| 23 | // Load the index |
| 24 | hts_idx_t *idx = sam_index_load(in, bam.c_str()); |
| 25 | if (idx == NULL) { |
| 26 | if (sam_index_build2(bam.c_str(), 0, 0) < 0) { |
| 27 | std::cout << ("Unable to open BAM/SAM index.") << std::endl; |
| 28 | return -1; |
| 29 | } else { |
| 30 | idx = sam_index_load(in, bam.c_str()); |
| 31 | } |
| 32 | } |
| 33 | // Get the header |
| 34 | bam_hdr_t *header = sam_hdr_read(in); |
| 35 | add_pg_line_to_header(&header, const_cast<char *>(cmd.c_str())); |
| 36 | if (bam_hdr_write(out, header) < 0) { |
| 37 | std::cout << "Unable to write BAM header to path." << std::endl; |
| 38 | sam_close(in); |
| 39 | return -1; |
| 40 | } |
| 41 | if (header == NULL) { |
| 42 | sam_close(in); |
| 43 | std::cout << "Unable to open BAM/SAM header." << std::endl; |
| 44 | } |
| 45 | if (region_.empty()) { |
| 46 | std::cout << "Number of references: " << header->n_targets << std::endl; |
| 47 | for (int i = 0; i < header->n_targets; ++i) { |
| 48 | std::cout << "Reference Name: " << header->target_name[i] << std::endl; |
| 49 | std::cout << "Reference Length: " << header->target_len[i] << std::endl; |
| 50 | if (i == 0) { |
| 51 | region_.assign(header->target_name[i]); |
| 52 | } |
| 53 | } |
| 54 | std::cout << "Using Region: " << region_ << std::endl; |
| 55 | } |
| 56 | std::string temp(header->text); |
| 57 | std::string sortFlag("SO:coordinate"); |
| 58 | if (temp.find(sortFlag)) { |
| 59 | std::cout << "Sorted By Coordinate" << std::endl; // Sort by coordinate |
| 60 | } else { |
| 61 | std::cout << "Not sorted" << std::endl; |
| 62 | } |
| 63 | // Initialize iterator |
| 64 | hts_itr_t *iter = NULL; |
| 65 | // Move the iterator to the region we are interested in |
| 66 | iter = sam_itr_querys(idx, header, region_.c_str()); |
| 67 | if (header == NULL || iter == NULL) { |
| 68 | sam_close(in); |
| 69 | std::cout << "Unable to iterate to region within BAM/SAM." << std::endl; |
| 70 | return -1; |
| 71 | } |
| 72 | // Initiate the alignment record |
| 73 | bam1_t *aln = bam_init1(); |
| 74 | int ctr = 0, rmv_ctr = 0; |
| 75 | bool w; |
| 76 | while (sam_itr_next(in, iter, aln) >= 0) { |
| 77 | uint8_t *a = bam_aux_get(aln, "XA"); |
| 78 | w = true; |
| 79 | if (a != 0) { |
| 80 | for (std::vector<std::string>::iterator it = amp.begin(); it != amp.end(); |
| 81 | ++it) { |
| 82 | if (bam_aux2i(a) == get_primer_indice(primers, *it)) { |
| 83 | w = false; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if (w) { |
| 88 | if (bam_write1(out, aln) < 0) { |
| 89 | std::cout << "Not able to write to BAM" << std::endl; |
| 90 | hts_itr_destroy(iter); |
| 91 | hts_idx_destroy(idx); |
| 92 | bam_destroy1(aln); |
| 93 | bam_hdr_destroy(header); |
| 94 | sam_close(in); |
| 95 | bgzf_close(out); |
| 96 | return -1; |
| 97 | }; |
| 98 | } else { |
| 99 | rmv_ctr++; |
| 100 | } |
| 101 | ctr++; |
| 102 | if (ctr % 1000000 == 0) { |
| 103 | std::cout << "Processed " << ctr << " reads" << std::endl; |
| 104 | } |
| 105 | } |
| 106 | std::cout << "Results:" << std::endl; |
| 107 | std::cout << rmv_ctr << " reads were removed." << std::endl; |
| 108 | hts_itr_destroy(iter); |
| 109 | hts_idx_destroy(idx); |
| 110 | bam_destroy1(aln); |
| 111 | bam_hdr_destroy(header); |
| 112 | sam_close(in); |
| 113 | bgzf_close(out); |
| 114 | return 0; |
| 115 | } |