Skip to content

Commit 094584a

Browse files
src/load_data_for_complexity.cpp: fixed the priority queue which was sorting in the wrong direction
1 parent 99edc6b commit 094584a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/load_data_for_complexity.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
#include <utility> // std::swap
4343
#include <vector>
4444

45-
using std::cerr;
46-
using std::endl;
47-
using std::max;
4845
using std::min;
4946
using std::mt19937;
5047
using std::priority_queue;
@@ -520,6 +517,13 @@ load_coverage_counts_GR(const string &input_file_name, const uint64_t seed,
520517
#ifdef HAVE_HTSLIB
521518
// Deal with SAM/BAM format only if we have htslib
522519

520+
static inline void
521+
reset_bam_rec(bamxx::bam_rec &b) {
522+
if (b.b)
523+
bam_destroy1(b.b);
524+
b.b = nullptr;
525+
}
526+
523527
static inline bool
524528
not_mapped(const bamxx::bam_rec &aln) {
525529
return get_tid(aln) == -1;
@@ -540,6 +544,9 @@ struct aln_pos {
540544
bool operator<(const aln_pos &rhs) const {
541545
return tid < rhs.tid || (tid == rhs.tid && pos < rhs.pos);
542546
}
547+
bool operator>(const aln_pos &rhs) const {
548+
return tid > rhs.tid || (tid == rhs.tid && pos > rhs.pos);
549+
}
543550
bool operator!=(const aln_pos &rhs) const {
544551
// ADS: ordered to check pos first
545552
return pos != rhs.pos || tid != rhs.tid;
@@ -704,8 +711,8 @@ split_genomic_interval(const genomic_interval &gi, mt19937 &generator,
704711

705712
template <class T, class U>
706713
static inline bool
707-
can_pop(const T &pq, const U &u, const hts_pos_t max_dist) {
708-
return pq.top().tid != u.tid || pq.top().pos + max_dist < u.pos;
714+
can_pop(const T &pq, const U &last, const hts_pos_t max_dist) {
715+
return pq.top().tid != last.tid || pq.top().pos + max_dist < last.pos;
709716
}
710717

711718
template <class T>
@@ -757,7 +764,7 @@ load_coverage_counts_BAM(const uint32_t n_threads, const string &inputfile,
757764
size_t current_count = 1;
758765

759766
// initialize prioirty queue to reorder the split reads
760-
priority_queue<aln_pos> pq;
767+
priority_queue<aln_pos, vector<aln_pos>, std::greater<aln_pos>> pq;
761768
vector<aln_pos> parts; // reuse allocated space
762769
aln_pos prev_part;
763770
genomic_interval prev;
@@ -787,7 +794,7 @@ load_coverage_counts_BAM(const uint32_t n_threads, const string &inputfile,
787794

788795
// add split intervals to the priority queue
789796
const auto last = parts.back(); // keep a copy for test below
790-
for (auto &&i : parts)
797+
for (const auto &i : parts)
791798
pq.push(i);
792799

793800
// remove genomic interval parts from the priority queue
@@ -798,6 +805,7 @@ load_coverage_counts_BAM(const uint32_t n_threads, const string &inputfile,
798805
update_coverage_hist(curr_part, prev_part, coverage_hist, current_count);
799806
prev_part = curr_part;
800807
}
808+
reset_bam_rec(aln);
801809
prev = curr;
802810
++n_reads;
803811
}

0 commit comments

Comments
 (0)