Repository navigation
Speed up simple FASTA parsers - #1808
Conversation
| lines.append(line.rstrip()) | ||
|
|
||
| assert False, "Should not reach this line" | ||
| if lines: |
There was a problem hiding this comment.
I think this if statement should not be conditional - consider empty sequences (zero length), e.g.
>empty with a blank line afterwards
and:
>empty with no line afterwards
These are both valid but corner case FASTA files.
There was a problem hiding this comment.
Thanks, I did't know, is this tested in the suite? I'm not sure why the tests are failing?
|
It looks like the empty sequence edge case was not directly being picked up on in our test suite (that would be worth adding explicitly), rather they are failing here: Have you been able to run the test suite locally? See the testing chapter in the Tutorial... |
|
I can add a few the empty sequence edge cases, I assume Yes I ran the full test suite locally but wanted to check them here also; sometimes I get additional test failures locally that are not replicated here, this is one of those cases: I also get some Anyway, I will look more into this and get back to you here |
|
Think I fixed it (at least locally the rogue test passed). But is this valid FASTA? It was an example like this that was tripping up my previous code |
Codecov Report
@@ Coverage Diff @@
## master #1808 +/- ##
==========================================
- Coverage 85.33% 85.33% -0.01%
==========================================
Files 301 301
Lines 49205 49197 -8
==========================================
- Hits 41991 41984 -7
+ Misses 7214 7213 -1
Continue to review full report at Codecov.
|
|
With the methods now implemented it's much easier to do the timings by comparing the master branch to this new-unmerged one. It shows a clear speed increase for the new implementation, using I think all that remains now probably is adding more unit tests? Original branch: New branch: Code: * |
|
RE: Yes, I consider that to be a pathological but valid FASTA file containing a zero length sequence followed by a single letter sequence. The |
|
Can you check if your unicode problem has been reported already, and if so can you add anything useful to one of these open issues? If it is a new issue, please log it. |
|
My initial testing - note that 60 or 80 bases per line is more common so I've used that - but this was based on your example: import os
SEQ_LINE = 'ATCGN'* 80 + '\n'
# Make test files
if not os.path.isfile('one_big.fa'):
with open('one_big.fa', 'w') as out_f:
out_f.write('>1\n')
for _ in range(5000000):
out_f.write(SEQ_LINE)
if not os.path.isfile('many_medium.fa'):
with open('many_medium.fa', 'w') as out_f:
for i in range(1000000):
out_f.write('>{}\n'.format(i))
for _ in range(5):
out_f.write(SEQ_LINE)
if not os.path.isfile('lots_small.fa'):
with open('lots_small.fa', 'w') as out_f:
for i in range(5000000-1):
out_f.write('>{}\n'.format(i))
out_f.write(SEQ_LINE)
out_f.write('>Last\n')
# avoid new-line at end-of-file - corner case to test
out_f.write(SEQ_LINE.rstrip()) Then the test script, from timeit import timeit
def use_parser(function_name, filename):
with open(filename) as handle:
return sum(1 for _ in function_name(handle))
print('Timings: 1 large sequence')
print(timeit('sum(1 for _ in SimpleFastaParser(open("chr1.fa")))',
setup='from Bio.SeqIO.FastaIO import SimpleFastaParser',
number = 20))
print('Timings: 1,000,000 medium sequences')
print(timeit('sum(1 for _ in SimpleFastaParser(open("many_medium.fa")))',
setup='from Bio.SeqIO.FastaIO import SimpleFastaParser',
number = 20))
print('Timings: 5,000,000 small sequences')
print(timeit('sum(1 for _ in SimpleFastaParser(open("lots_small.fa")))',
setup='from Bio.SeqIO.FastaIO import SimpleFastaParser',
number = 20))Using the master branch on macOS Python 3.7.0rc1, Using your branch on macOS Python 3.7.0rc1, definitely faster in all three: TODO: At least Python 2.7 testing, ideally also PyPy testing, hopefully tomorrow... |
|
Thank you Peter! I ran your script on python 3 and python 2. My timings are very different but the new implementation still performs better. I ran the scripts several times (and in different orders) because I found it hard to believe the python 2 timings, but they remained fairly consistent (not caching I guess). Actually I found that just iterating over the file handle was a lot faster in 2 than 3, which is surprising, and likely something specific to the server I'm running on I guess. Master branch on python 3.6.6, Arch-Linux: Unmerged branch on python 3.6.6, Arch-Linux: Master branch on python 2.7.15, Arch-Linux: Unmerged branch on python 2.7.15, Arch-Linux: |
|
RE the unicode problem, it's not obvious to me, I'll look into it separately and respond in the relevant issue (or create a new one) |
|
My timings on macOS, Python 2.7.10, master branch: My timings on macOS, Python 2.7.10, your branch: Again, consistently faster. Next, a PyPy comparison... |
|
My timings on macOS, using https://bitbucket.org/pypy/pypy/downloads/pypy2-v6.0.0-osx64.tar.bz2 and the master branch: And your branch, It would seem thanks to PyPy's optimisations on the fly, the two FASTA parsers come out about even. |
|
Merged, thank you! |
|
I did some timings previously of
Timings: and... Code: |
This pull request addresses issue #1805
Basically, the PR replaces
.readline()calls by iterating over file handles for FASTA parsing with theSimpleFastaParserandFastaTwoLineParser. This speeds up the parsers, at least on my system, maybe someone else can test this independently also?Here are timings before the PR, suggesting a speed improvement for the new implementation:
Here are timings after the PR, old and new timings should be the same here since the new methods are now implemented (I am not sure why they are not for the 2-line parser case, but there is still a speed improvement):
The code is on pastebin for reproducibility:
https://pastebin.com/MmhBVfFu
I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.
I have read the
CONTRIBUTING.rstfile and understand that AppVeyor andTravisCI will be used to confirm the Biopython unit tests and
flake8stylechecks pass with these changes.
I have added my name to the alphabetical contributors listings in the files
NEWS.rstandCONTRIB.rstas part of this pull request, am listedalready, or do not wish to be listed. (This acknowledgement is optional.)