diff --git a/pysam/libctabix.pyx b/pysam/libctabix.pyx index 54a2006a2..88db89d5f 100644 --- a/pysam/libctabix.pyx +++ b/pysam/libctabix.pyx @@ -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` @@ -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") @@ -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: diff --git a/tests/tabix_test.py b/tests/tabix_test.py index d82379d12..483ee23dc 100644 --- a/tests/tabix_test.py +++ b/tests/tabix_test.py @@ -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