Skip to content

Commit

Permalink
BamReader now throws error if sam_read1 value < -1
Browse files Browse the repository at this point in the history
  • Loading branch information
walaj committed Apr 25, 2017
1 parent 770cd10 commit ad8a854
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion SeqLib/BamReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace SeqLib {
private:

// do the read loading
bool load_read(BamRecord& r);
// the return value here is just passed along from sam_read1
int32_t load_read(BamRecord& r);

void reset() {
empty = true;
Expand Down
2 changes: 1 addition & 1 deletion htslib
35 changes: 27 additions & 8 deletions src/BamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,24 @@ bool BamReader::GetNextRecord(BamRecord& r) {

// shortcut if we have only a single bam
if (m_bams.size() == 1) {

if (m_bams.begin()->second.fp.get() == NULL || m_bams.begin()->second.mark_for_closure) // cant read if not opened
return false;
if (m_bams.begin()->second.load_read(r)) { // try to read

// try and get the next read
int32_t status = m_bams.begin()->second.load_read(r);
if (status >= 0)
return true;
if (status == -1) {
// didn't find anything, clear it
m_bams.begin()->second.mark_for_closure = true;
return false;
}
// didn't find anything, clear it
m_bams.begin()->second.mark_for_closure = true;

// run time error
std::stringstream ss;
ss << "sam_read1 return status: " << status << " file: " << m_bams.begin()->first;
throw std::runtime_error(ss.str());
return false;
}

Expand All @@ -253,10 +264,17 @@ bool BamReader::GetNextRecord(BamRecord& r) {
continue;

// load the next read
if (!tb->load_read(r)) { // if cant load, mark for closing
int32_t status = tb->load_read(r);
if (status == -1) {
// can't load, so mark for closing
tb->empty = true;
tb->mark_for_closure = true; // no more reads in this BAM
continue;
} else if (status < 0) { // error sent back from sam_read1
// run time error
std::stringstream ss;
ss << "sam_read1 return status: " << status << " file: " << bam->first;
throw std::runtime_error(ss.str());
}

}
Expand Down Expand Up @@ -302,22 +320,23 @@ std::string BamReader::PrintRegions() const {

}

bool _Bam::load_read(BamRecord& r) {
int32_t _Bam::load_read(BamRecord& r) {

// allocated the memory
bam1_t* b = bam_init1();
int32_t valid;

if (hts_itr.get() == NULL) {
valid = sam_read1(fp.get(), m_hdr.get_(), b);

if (valid < 0) {

#ifdef DEBUG_WALKER
std::cerr << "ended reading on null hts_itr" << std::endl;
#endif
//goto endloop;
bam_destroy1(b);
return false;
return valid;
}
} else {

Expand All @@ -336,7 +355,7 @@ std::string BamReader::PrintRegions() const {
++m_region_idx; // increment to next region
if (m_region_idx >= m_region->size()) {
bam_destroy1(b);
return false;
return valid;
}
//goto endloop;

Expand All @@ -351,7 +370,7 @@ std::string BamReader::PrintRegions() const {
next_read.assign(b); // assign the shared_ptr for the bam1_t
r = next_read;

return true;
return valid;
}

std::ostream& operator<<(std::ostream& out, const BamReader& b)
Expand Down

0 comments on commit ad8a854

Please sign in to comment.