Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pysam/libctabix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ cdef class TabixFile:
end=None,
region=None,
parser=None,
multiple_iterators=False):
multiple_iterators=False,
skip_invalid_references=False):
'''fetch one or more rows in a :term:`region` using 0-based
indexing. The region is specified by :term:`reference`,
*start* and *end*. Alternatively, a samtools :term:`region`
Expand All @@ -440,6 +441,10 @@ cdef class TabixFile:
effectively re-opening the file. Re-opening a file creates
some overhead, so beware.

Set *skip_invalid_references* if you want to gracefully ignore
cases where a reference name is not valid. By default,
a *ValueError* will be raised in such cases.

'''
if not self.is_open():
raise ValueError("I/O operation on closed file")
Expand Down Expand Up @@ -505,9 +510,12 @@ cdef class TabixFile:
# return an empty iterator
return EmptyIterator()
else:
raise ValueError(
"could not create iterator for region '%s'" %
region)
if skip_invalid_references:
return EmptyIterator()
else:
raise ValueError(
"could not create iterator for region '%s'" %
region)

# use default parser if no parser is specified
if parser is None:
Expand Down
5 changes: 5 additions & 0 deletions tests/tabix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ def testInvalidIntervals(self):

# unknown chromosome
self.assertRaises(ValueError, self.tabix.fetch, "chrUn")
# an empty iterator should be returned
# if skip_invalid_references is set to True
self.assertEqual(
0,
len(list(self.tabix.fetch("chrUn", skip_invalid_references=True))))

# out of range access
# to be implemented
Expand Down