2525#include " smithlab_utils.hpp"
2626#include " MappedRead.hpp"
2727
28+ // extern "C" {
29+ // #include <htslib/thread_pool.h>
30+ // }
31+
2832using std::string;
2933using std::vector;
3034using std::cerr;
@@ -33,8 +37,21 @@ using std::runtime_error;
3337
3438char check_htslib_wrapper () {return 1 ;}
3539
40+ /*
41+ void
42+ SAMReader::add_threads(const size_t n_threads) {
43+ // ADS: should probably check that `hts_tpool` succeeded
44+ tp = new htsThreadPool{hts_tpool_init(n_threads), 0};
45+ // tp->pool = hts_tpool_init(n_threads);
46+ // tp->qsize = 0;
47+ const int err_code = hts_set_thread_pool(hts, tp);
48+ if (err_code < 0) throw runtime_error("error setting threads");
49+ }
50+ */
51+
3652SAMReader::SAMReader (const string &fn) :
37- filename(fn), good(true ) {
53+ filename(fn), good(true ), hts(nullptr ),
54+ hdr(nullptr ), b(nullptr ) { // , tp(nullptr) {
3855
3956 if (!(hts = hts_open (filename.c_str (), " r" )))
4057 throw runtime_error (" cannot open file: " + filename);
@@ -52,16 +69,22 @@ SAMReader::SAMReader(const string &fn) :
5269SAMReader::~SAMReader () {
5370 if (hdr) {
5471 bam_hdr_destroy (hdr);
55- hdr = 0 ;
72+ hdr = nullptr ;
5673 }
5774 if (b) {
5875 bam_destroy1 (b);
59- b = 0 ;
76+ b = nullptr ;
6077 }
6178 if (hts) {
6279 assert (hts_close (hts) >= 0 );
63- hts = 0 ;
80+ hts = nullptr ;
6481 }
82+ // if (tp) {
83+ // assert(tp->pool);
84+ // hts_tpool_destroy(tp->pool);
85+ // tp->pool = nullptr;
86+ // tp = nullptr;
87+ // }
6588 good = false ;
6689}
6790
80103SAMReader::get_sam_record (sam_rec &sr) {
81104 int rd_ret = 0 ;
82105 if ((rd_ret = sam_read1 (hts, hdr, b)) >= 0 ) {
106+ // ADS: the line below implicitly converts the 0-based leftmost
107+ // coordinate in the bam1_core_t struct into a 1-based value,
108+ // which corresponds to the conversion of a BAM record to a SAM
109+ // record. Remember to convert it back for 0-based coordinates.
83110 int fmt_ret = 0 ;
84111 if ((fmt_ret = sam_format1 (hdr, b, &hts->line )) <= 0 )
85112 throw runtime_error (" failed reading record from: " + filename);
@@ -90,6 +117,8 @@ SAMReader::get_sam_record(sam_rec &sr) {
90117 // sam_read1 only get called when "(fp->line.l == 0)". For BAM
91118 // format, it does not seem to matter.
92119 hts->line .l = 0 ;
120+ // ADS: possibly this should be:
121+ // hts->line.l = ks_clear(hts->line.l);
93122 }
94123 else if (rd_ret == -1 )
95124 good = false ;
0 commit comments