Skip to content

BGZF RecursionError #1701

Open
Open

Description

@peterjc

There appears to be an issue in the .read() and/or .readline() methods as per these issues using Bio/bgzf.py within pyfaidx:

mdshw5/pyfaidx#125
mdshw5/pyfaidx#131

Steps to reproduce from @mdshw5

$ wget http://ftp.ensembl.org/pub/current_fasta/homo_sapiens/dna/Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz
--2018-01-06 20:21:07--  http://ftp.ensembl.org/pub/current_fasta/homo_sapiens/dna/Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz
Resolving ftp.ensembl.org... 193.62.193.8
Connecting to ftp.ensembl.org|193.62.193.8|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 881214396 (840M) [application/x-gzip]
Saving to: 'Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz'

Homo_sapiens.GRCh38.dna.primary_assemb 100%[============================================================================>] 840.39M   393KB/s    in 24m 34s 

2018-01-06 20:45:41 (584 KB/s) - 'Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz' saved [881214396/881214396]

$ gunzip Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz 
$ bgzip Homo_sapiens.GRCh38.dna.primary_assembly.fa 
$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyfaidx import Fasta
>>> pyfaidx_handle = Fasta('Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz')
>>> pyfaidx_handle['12'][111803961:111803962]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Development/pyfaidx/pyfaidx/__init__.py", line 698, in __getitem__
    return self._fa.get_seq(self.name, start + 1, stop)[::step]
  File "/Development/pyfaidx/pyfaidx/__init__.py", line 887, in get_seq
    seq = self.faidx.fetch(name, start, end)
  File "/Development/pyfaidx/pyfaidx/__init__.py", line 532, in fetch
    seq = self.from_file(name, start, end)
  File "/Development/pyfaidx/pyfaidx/__init__.py", line 574, in from_file
    chunk_seq = self.file.read(chunk).decode()
  File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 651, in read
    return data + self.read(size)
  File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 651, in read
    return data + self.read(size)
...
 File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 651, in read
    return data + self.read(size)
  File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 644, in read
    self._load_block()  # will reset offsets
  File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 576, in _load_block
    block_size, self._buffer = _load_bgzf_block(handle, self._text)
  File "/Development/pyfaidx/venv/lib/python2.7/site-packages/Bio/bgzf.py", line 411, in _load_bgzf_block
    if magic != _bgzf_magic:
RuntimeError: maximum recursion depth exceeded in cmp
>>> 

Quoting mdshw5/pyfaidx#125 (comment) his analysis:

Ah, I think I know what's happening here. I'm calling BgzfReader.read() and asking for the data between chromosome 12 locations [1: 111803962]. This is due to the way I've decided to avoid potentially adding/subtracting virtual offsets in my Faidx.fetch() method. I think BgzfReader will hit the interpreter recursion limit any time you ask it to read a large string from a file with a large number of newline characters, such as this example.

It's not trivial, but I could try to come up with a way to be more efficient with my BGZF reads. @peterjc: maybe I'm missing something but it seems impossible to seek/read a specific substring efficiently that may span multiple BGZF blocks unless you store the offset of each block indexed to the string.

I think the next step would be inserting some debugging in order to make a tiny test-case using this human FASTA file and just Bio/bgzf.py

Note I suspect both .readline() and .read() may be affected by this recursion issue (I left a TODO comment in both methods).

Activity

  1. mdshw5 commented on Jul 3, 2018

    @mdshw5
    Contributor

    I think the issue with recursion limit being reached can be mitigated by indexing each BGZF block and storing the compressed/uncompressed offset in the same was as the ".gzi" format produced by bgzip (http://www.htslib.org/doc/bgzip.html#GZI_FORMAT). I plan to support creating and reading this format in mdshw5/pyfaidx#126, but so far haven't started the implementation. It may make sense for Bio/bgzf.py to support this index format, so that readline() and read() can determine the number of BGZF blocks the read will span, and the methods would lose their recursive implementation (I think).

  2. peterjc commented on Jul 3, 2018

    @peterjc
    MemberAuthor

    If you can make a self contained test, that would be enormously helpful.

    GZI might be sensible, which looks like caching the information my BgzfBlocks code reports, but I don't immediately see why this is needed. I am likely missing something having not worked with this code for some time.

    Alternatively I was thinking readline() and read() can likely be re-factored to loop over blocks as needed rather than recurring.

  3. mdshw5 commented on Jul 3, 2018

    @mdshw5
    Contributor

    Alternatively I was thinking readline() and read() can likely be re-factored to loop over blocks as needed rather than recurring.

    I think this is what I'm proposing, and you're right that GZI support isn't necessary but might be sensible.

    If you can make a self contained test, that would be enormously helpful.

    Do you mean a test that triggers the RuntimeError? Self-contained as in just using Bio.bgzf directly and factoring out any of the pyfaidx code involved in the above example?

  4. peterjc commented on Jul 3, 2018

    @peterjc
    MemberAuthor

    Yes, a test case calling Bio.bgzf directly without pyfaidx would be ideal for me. Thanks.

  5. peterjc commented on Aug 23, 2018

    @peterjc
    MemberAuthor

    This should be fixed via #1767 by @rtf-const, and assuming this does solve the pyfaidx issues, it would be good to get another Biopython release out fairly soon.

  6. mdshw5 commented on Aug 23, 2018

    @mdshw5
    Contributor

    This fixes the issue in mdshw5/pyfaidx#125 and can be marked as closed. Thanks @rtf-const and @peterjc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions