mdshw5 / pyfaidx

Efficient pythonic random access to fasta subsequences
https://pypi.python.org/pypi/pyfaidx
Other
449 stars 75 forks source link

fetch a chromosome on a FastaVariant which is not part of the vcf raises an error #179

Closed lldelisle closed 2 years ago

lldelisle commented 2 years ago

I noticed that when I try to fetch a chromosome which is not part of the vcf on a FastaVariant I get an error:

~/.venvPyfaidx/lib/python3.7/site-packages/pyfaidx/__init__.py in __iter__(self)
    837             end = start + line_len
    838             if end < len(self):
--> 839                 yield self[start:end]
    840             else:
    841                 yield self[start:]

~/.venvPyfaidx/lib/python3.7/site-packages/pyfaidx/__init__.py in __getitem__(self, n)
    821                 if start < 0:
    822                     start = len(self) + start
--> 823                 return self._fa.get_seq(self.name, start + 1, stop)[::step]
    824 
    825             elif isinstance(n, integer_types):

~/.venvPyfaidx/lib/python3.7/site-packages/pyfaidx/__init__.py in get_seq(self, name, start, end)
   1164             seq_mut = list(seq.seq)
   1165             del seq.seq
-> 1166         var = self.vcf.fetch(name, start - 1, end)
   1167         for record in var:
   1168             if record.is_snp:  # skip indels

~/.venvPyfaidx/lib/python3.7/site-packages/vcf/parser.py in fetch(self, chrom, start, end)
    629             chrom = chrom[3:]
    630 
--> 631         self.reader = self._tabix.fetch(chrom, start, end)
    632         return self
    633 

pysam/libctabix.pyx in pysam.libctabix.TabixFile.fetch()

ValueError: could not create iterator for region 'MT:1-60'

I propose a fix in a PR.

lldelisle commented 2 years ago

Versions used:

(.venvPyfaidx) [ldelisle@helvetios X]$ python --version
Python 3.7.7
(.venvPyfaidx) [ldelisle@helvetios X]$ pip list | grep pyfaidx
pyfaidx           0.6.2 

Also obtained with python 3.9.7. A minimal test to see it is in the PR #180

mdshw5 commented 2 years ago

Fixed in #180