Skip to content
This repository has been archived by the owner on May 25, 2024. It is now read-only.

Commit

Permalink
Provide more informative error message for failed SAM header parsing.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 315543585
  • Loading branch information
cmclean authored and copybara-github committed Jun 9, 2020
1 parent 213ec93 commit 65b8739
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 10 additions & 9 deletions nucleus/io/python/sam_reader_wrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def test_context_manager(self):
self.assertIsInstance(query_iterable2, clif_postproc.WrappedCppIterable)

def test_from_file_raises_with_missing_bam(self):
with self.assertRaisesRegexp(ValueError,
'Not found: Could not open missing.bam'):
with self.assertRaisesRegex(ValueError,
'Not found: Could not open missing.bam'):
sam_reader.SamReader.from_file(
reads_path='missing.bam', ref_path='', options=self.options)

Expand All @@ -141,9 +141,9 @@ def test_ops_on_closed_reader_raise(self):
with reader:
pass
# At this point the reader is closed.
with self.assertRaisesRegexp(ValueError, 'Cannot Iterate a closed'):
with self.assertRaisesRegex(ValueError, 'Cannot Iterate a closed'):
reader.iterate()
with self.assertRaisesRegexp(ValueError, 'Cannot Query a closed'):
with self.assertRaisesRegex(ValueError, 'Cannot Query a closed'):
reader.query(ranges.parse_literal('chr20:10,000,000-10,000,100'))

@parameterized.parameters('test.sam', 'unindexed.bam')
Expand All @@ -152,15 +152,15 @@ def test_query_without_index_raises(self, unindexed_file_name):
window = ranges.parse_literal('chr20:10,000,000-10,000,100')
with sam_reader.SamReader.from_file(
reads_path=path, ref_path='', options=self.options) as reader:
with self.assertRaisesRegexp(ValueError, 'Cannot query without an index'):
with self.assertRaisesRegex(ValueError, 'Cannot query without an index'):
reader.query(window)

def test_query_raises_with_bad_range(self):
with sam_reader.SamReader.from_file(
reads_path=self.bam, ref_path='', options=self.options) as reader:
with self.assertRaisesRegexp(ValueError, 'Unknown reference_name'):
with self.assertRaisesRegex(ValueError, 'Unknown reference_name'):
reader.query(ranges.parse_literal('XXX:1-10'))
with self.assertRaisesRegexp(ValueError, 'unknown reference interval'):
with self.assertRaisesRegex(ValueError, 'unknown reference interval'):
reader.query(ranges.parse_literal('chr20:10-5'))

def test_sam_iterate_raises_on_malformed_record(self):
Expand All @@ -174,9 +174,10 @@ def test_sam_iterate_raises_on_malformed_record(self):

def test_headless_sam_raises(self):
headerless = test_utils.genomics_core_testdata('headerless.sam')
with self.assertRaisesRegexp(ValueError, 'Could not parse SAM header'):
with self.assertRaisesRegex(ValueError,
'Could not parse file with bad SAM header'):
sam_reader.SamReader.from_file(
reads_path=headerless, ref_path='', options=self.options)
reads_path=headerless, ref_path='', options=self.options)


if __name__ == '__main__':
Expand Down
7 changes: 5 additions & 2 deletions nucleus/io/sam_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

#include <errno.h>
#include <stdint.h>

#include <map>
#include <utility>
#include <vector>

#include "google/protobuf/repeated_field.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "htslib/cram.h"
Expand Down Expand Up @@ -649,12 +651,13 @@ StatusOr<std::unique_ptr<SamReader>> SamReader::FromFile(

bam_hdr_t* header = sam_hdr_read(fp);
if (header == nullptr) {
string errmsg = absl::StrCat("bad SAM header: ", fp->fn);
int retval = hts_close(fp);
fp = nullptr;
if (retval < 0) {
return tf::errors::Internal("hts_close() failed during nonexistent header ");
return tf::errors::Internal("hts_close() failed on file with ", errmsg);
}
return tf::errors::Unknown("Could not parse SAM header ");
return tf::errors::Unknown("Could not parse file with ", errmsg);
}

hts_idx_t* idx = nullptr;
Expand Down

0 comments on commit 65b8739

Please sign in to comment.