Skip to content

Commit c198ca7

Browse files
Adding code in comments that would allow for threads in decoding the BAM input. This is meaningless as long as we take so much time and space for converting BAM records into a large SAM record class
1 parent 5d59aad commit c198ca7

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

htslib_wrapper.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include "smithlab_utils.hpp"
2626
#include "MappedRead.hpp"
2727

28+
// extern "C" {
29+
// #include <htslib/thread_pool.h>
30+
// }
31+
2832
using std::string;
2933
using std::vector;
3034
using std::cerr;
@@ -33,8 +37,21 @@ using std::runtime_error;
3337

3438
char 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+
3652
SAMReader::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) :
5269
SAMReader::~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

@@ -80,6 +103,10 @@ bool
80103
SAMReader::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;

htslib_wrapper.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class SAMReader {
4242

4343
bool get_sam_record(sam_rec &sr);
4444

45+
void add_threads(const size_t n_threads);
46+
4547
std::string get_header() const;
4648

4749
private:
@@ -50,9 +52,10 @@ class SAMReader {
5052
std::string filename;
5153
bool good;
5254

53-
htsFile* hts;
54-
bam_hdr_t *hdr;
55-
bam1_t *b;
55+
htsFile* hts{};
56+
bam_hdr_t *hdr{};
57+
bam1_t *b{};
58+
// htsThreadPool *tp{};
5659
};
5760

5861
SAMReader &

0 commit comments

Comments
 (0)