You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SearchIO fails to parse HMMER 3 results with additional lines #1126
I've encountered today a possible bug while parsing HMMER3 text output. In some rare cases HMMER appends additional lines below line indicating the end of results section ('//'). In such case parser will fail:
Traceback (most recent call last):
File "test_hmmer.py", line 6, in
for qres in SearchIO.parse(ifile, 'hmmer3-text'):
File "/usr/lib/python2.7/dist-packages/Bio/SearchIO/init.py", line 314, in parse
for qresult in generator:
File "/usr/lib/python2.7/dist-packages/Bio/SearchIO/HmmerIO/hmmer3_text.py", line 47, in iter
for qresult in self._parse_qresult():
File "/usr/lib/python2.7/dist-packages/Bio/SearchIO/HmmerIO/hmmer3_text.py", line 109, in _parse_qresult
qid = regx.group(1).strip()
AttributeError: 'NoneType' object has no attribute 'group'
Please see attached file for example of HMMER output files and code that raises the error. example1 runs just fine, while example2 raises above mentioned exception. It's the same for Python 2.7 and Python 3.5.
Removing lines inserted after '//' line solves the issue.
If it is indeed a bug, not a feature, I would like to tackle the fix (seems kind of trivial?) since I wanted to contribute to the project since a while. Maybe this could be possible first step?
This sounds to me like a clear bug in HMMER3, so please (also) report it to them. If there is any pattern to these rare outputs that would help track down the cause.
As to coping with this in the Biopython SearchIO parser (and indexing code), @bow is the best person to comment. One solution might be to ignore these extra lines with a warning message...
Thanks for reporting the bug and including a test case! I can reproduce it on Python 3.5 on the latest Biopython master indeed. If you want to try fix the error, go ahead. The place to start would be the hmmer3_text.py file inside the Bio.SearchIO.HmmerIO module. Feel free to ping me if you have any questions about anything.
I am sorry for not giving any updates on the issue for a while. I was simply swamped with other things (day job) and couldn't tackle this. Now I think I have a working solution.
I've contacted HMMER maintainer and his response was:
"The end of the output from each query is "^//". The end of the output from the entire search is "^[ok]". The [ok] tag at the end of the output allows a parser to know that the file is complete, not truncated."
Based on that one shall allow for multiple qresult sections in a given file. For the better understanding of what I was trying with prepped please find attached example files:
example1.out - standard single query output
example2.out - single query output with comment ('^#') lines after line terminating query section ('^//')
example3.out - double query output with no comment lines after line terminating query section
example4.out - double query output with comment lines after line terminating query section (kind of like second preamble but representing also possible messages like in example2).
test_hmmer.py - my test script
hmmer3_text.py - original file from the Bio.SearchIO.HmmerIO module with code changes I mention below
Thirst thing that has to be changed in order to parse output files containing additional comment lines is how qid of a given qresult is read. If comment line occurs after '//' line, it will raise AttributeError since regx is None because preamble is already parsed.
def_parse_qresult(self):
"""Parses a HMMER3 query block."""self._read_until(lambdaline: line.startswith('Query:'))
whileself.line:
# get query id and lengthregx=re.search(_QRE_ID_LEN, self.line)
qid=regx.group(1).strip()
Code below would solve the issue by omitting any comment lines and identifying qresult section below them.
def_parse_qresult(self):
"""Parses a HMMER3 query block."""self._read_until(lambdaline: line.startswith('Query:'))
whileself.line:
regx=re.search(_QRE_ID_LEN, self.line)
whilenotregx:
# Catch if block contains proper qresultself.line=read_forward(self.handle)
regx=re.search(_QRE_ID_LEN, self.line)
# Catch if block contains proper qresultqid=regx.group(1).strip()
Second small change is in original line 149 where one has to accept any kind of comment line since it seems that HMMER might print here all sorts of information (like '# No hits satisfy inclusion thresholds; no alignment saved' in example2.out). So this code:
No worries & thanks for the update + the work! This looks ok already, but I'm curious if you have implemented these changes in a public branch somewhere? That would make it easier to pull your proposed changes :).
Also, have you tested your changes against the existing HmmerIO test cases? Formats like this (semi-readable without any hard specs) often feels quite fickle to me and so testing against known cases is quite helpful. If you don't mind, you could also add your test cases to our test suite to prevent any future regressions.
Thank you for reply! I am glad it looks ok. ;-) I can push these changes to the local fork in my repository. Would that be sufficient?
Also, have you tested your changes against the existing HmmerIO test cases? Formats like this (semi-readable without any hard specs) often feels quite fickle to me and so testing against known cases is quite helpful. If you don't mind, you could also add your test cases to our test suite to prevent any future regressions.
That's actually a good point, I didn't run HmmerIO test cases. I will do that and see if I can include my test cases in there for future. I'll do that probably during the day.
I can push these changes to the local fork in my repository. Would that be sufficient?
That would be perfect ~ you can make your own branch and later on when we're sure all the changes pass the test we can either rebase the changes on the main branch (with/without squashing).
$ python3 test_SearchIO_hmmer3_text.py
test_30_hmmsearch_001 (__main__.HmmersearchCases)
Test parsing hmmersearch 3.0 (text_30_hmmsearch_001) ... ok
test_30_hmmsearch_002 (__main__.HmmersearchCases)
Test parsing hmmersearch 3.0 (text_30_hmmsearch_002) ... ok
test_30_hmmsearch_003 (__main__.HmmersearchCases)
Test parsing hmmersearch 3.0 (text_30_hmmsearch_003) ... ok
test_30_hmmsearch_004 (__main__.HmmersearchCases)
Test parsing hmmersearch 3.0 (text_30_hmmsearch_004) ... ok
test_30_hmmsearch_005 (__main__.HmmersearchCases)
Test parsing hmmersearch 3.0 (text_30_hmmsearch_005) ... ok
test_31b1_hmmsearch_001 (__main__.HmmersearchCases)
Test parsing hmmsearch 3.1b1 (text_31b1_hmmsearch_001) ... ok
test_31b2_hmmscan_001 (__main__.HmmersearchCases)
Test parsing hmmscan 3.1b2 (text_31b2_hmmscan_001) ... ok
test_31b2_hmmsearch_001 (__main__.HmmersearchCases)
Test parsing hmmsearch 3.1b2 (text_31b2_hmmsearch_001) ... ok
test_31b2_hmmsearch_002 (__main__.HmmersearchCases)
Test parsing hmmsearch 3.1b2 (text_31b2_hmmsearch_002) ... ok
test_30_hmmscan_001 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_001) ... ok
test_30_hmmscan_002 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_002) ... ok
test_30_hmmscan_003 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_003) ... ok
test_30_hmmscan_004 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_004) ... ok
test_30_hmmscan_005 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_005) ... ok
test_30_hmmscan_006 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_006) ... ok
test_30_hmmscan_007 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_007) ... ok
test_30_hmmscan_008 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_008) ... ok
test_30_hmmscan_009 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_009) ... ok
test_30_hmmscan_010 (__main__.HmmscanCases)
Test parsing hmmscan 3.0 (text_30_hmmscan_010) ... ok
test_31b1_hmmscan_001 (__main__.HmmscanCases)
Test parsing hmmscan 3.1b1 (text_31b1_hmmscan_001) ... ok
test_31b2_phmmer_001 (__main__.PhmmerCases)
Test parsing phmmer 3.1b2 (text_31b2_phmmer_001) ... ok
----------------------------------------------------------------------
Ran 21 tests in 0.108s
OK
I've added two simple tests checking whether proper amount of queries, proper queries id and proper number of hits for queries are read. Example:
deftest_31b2_hmmsearch_002(self):
"""Test parsing hmmsearch 3.1b2 (text_31b2_hmmsearch_002)"""txt_file=get_file('text_31b2_hmmsearch_002.out')
qresults=list(parse(txt_file, FMT))
# Assert we deal with 2 queriesself.assertEqual(len(qresults), 2)
# Test whether proper query id is readself.assertListEqual([x.idforxinqresults], ['infile_sto',
'infile_sto2'])
# Test if proper number of hits is readself.assertEqual([len(x.hits) forxinqresults], [10,10])
All changes are pushed with 2 commits to my fork of biopython mkorycinski/biopython: bb1a46c - deals with changes in hmmer3_text module d9dd142 - adding tests
Great, this looks ok for a merge. Would you like to open a pull request with your changes rebased against the latest master? This should keep the commit history linear & makes it easier to merge (it may be helpful to take a look at the short guide we've written).
I hope it looks ok that way. Turned out I made a mistake to make changes to the main branch of my fork. In order to fix that I would need to delete fork and fork biopython again.
Would pull request from a main branch in my fork be sufficient for now? For future contributions (hopefully there will be) I will set separate brunch and set biopython/biopython as an upstream.
@mkorycinski , you can always branch off to a new branch (i.e. git checkout -b <new_branch_name>) and keep the changes intact on it and then hard-resetting your own copy of the master branch.
But for now, no worries. I can cherry-pick your changes onto master :). It should come up soon.
Activity
peterjc commentedon Mar 28, 2017
This sounds to me like a clear bug in HMMER3, so please (also) report it to them. If there is any pattern to these rare outputs that would help track down the cause.
As to coping with this in the Biopython SearchIO parser (and indexing code), @bow is the best person to comment. One solution might be to ignore these extra lines with a warning message...
bow commentedon Mar 28, 2017
Hi @mkorycinski,
Thanks for reporting the bug and including a test case! I can reproduce it on Python 3.5 on the latest Biopython master indeed. If you want to try fix the error, go ahead. The place to start would be the
hmmer3_text.pyfile inside theBio.SearchIO.HmmerIOmodule. Feel free to ping me if you have any questions about anything.mkorycinski commentedon Mar 29, 2017
@peterjc
Thank you for the reply. I will indeed report this to HMMER maintainer, given such messages looks kind of like debugging 'left-overs'.
@bow
Thank you! I will look into it then and contact you in case I have any questions.
mkorycinski commentedon Apr 8, 2017
Hi,
I am sorry for not giving any updates on the issue for a while. I was simply swamped with other things (day job) and couldn't tackle this. Now I think I have a working solution.
I've contacted HMMER maintainer and his response was:
"The end of the output from each query is "^//". The end of the output from the entire search is "^[ok]". The [ok] tag at the end of the output allows a parser to know that the file is complete, not truncated."
Based on that one shall allow for multiple qresult sections in a given file. For the better understanding of what I was trying with prepped please find attached example files:
hmmer_bug.zip
Thirst thing that has to be changed in order to parse output files containing additional comment lines is how
qidof a givenqresultis read. If comment line occurs after '//' line, it will raiseAttributeErrorsinceregxisNonebecause preamble is already parsed.this code gives:
Code below would solve the issue by omitting any comment lines and identifying qresult section below them.
Second small change is in original
line 149where one has to accept any kind of comment line since it seems that HMMER might print here all sorts of information (like '# No hits satisfy inclusion thresholds; no alignment saved' in example2.out). So this code:would simply become:
With above mentioned changes all examples are parsed correctly. Would that be acceptable solution? Turned out to be rather simple.
bow commentedon Apr 8, 2017
Hey @mkorycinski,
No worries & thanks for the update + the work! This looks ok already, but I'm curious if you have implemented these changes in a public branch somewhere? That would make it easier to pull your proposed changes :).
Also, have you tested your changes against the existing HmmerIO test cases? Formats like this (semi-readable without any hard specs) often feels quite fickle to me and so testing against known cases is quite helpful. If you don't mind, you could also add your test cases to our test suite to prevent any future regressions.
mkorycinski commentedon Apr 8, 2017
Hi @bow,
Thank you for reply! I am glad it looks ok. ;-) I can push these changes to the local fork in my repository. Would that be sufficient?
That's actually a good point, I didn't run HmmerIO test cases. I will do that and see if I can include my test cases in there for future. I'll do that probably during the day.
Fix to issue biopython#1126 from biopython/biopython regarding HMMER3…
bow commentedon Apr 8, 2017
That would be perfect ~ you can make your own branch and later on when we're sure all the changes pass the test we can either rebase the changes on the main branch (with/without squashing).
Adding tests to the issue fix biopython#1126
mkorycinski commentedon Apr 9, 2017
All tests run smoothly.
I've added two simple tests checking whether proper amount of queries, proper queries id and proper number of hits for queries are read. Example:
All changes are pushed with 2 commits to my fork of biopython mkorycinski/biopython:
bb1a46c - deals with changes in hmmer3_text module
d9dd142 - adding tests
bow commentedon Apr 9, 2017
Great, this looks ok for a merge. Would you like to open a pull request with your changes rebased against the latest master? This should keep the commit history linear & makes it easier to merge (it may be helpful to take a look at the short guide we've written).
mkorycinski commentedon Apr 9, 2017
Sure. I'll fix my local repository according to the guide (thanks for the link!) and create pull request with changes.
mkorycinski commentedon Apr 9, 2017
I hope it looks ok that way. Turned out I made a mistake to make changes to the main branch of my fork. In order to fix that I would need to delete fork and fork biopython again.
Would pull request from a main branch in my fork be sufficient for now? For future contributions (hopefully there will be) I will set separate brunch and set biopython/biopython as an upstream.
Fix to issue #1126 from biopython/biopython regarding HMMER3 text parser
bow commentedon Apr 9, 2017
@mkorycinski , you can always branch off to a new branch (i.e.
git checkout -b <new_branch_name>) and keep the changes intact on it and then hard-resetting your own copy of the master branch.But for now, no worries. I can cherry-pick your changes onto master :). It should come up soon.
mkorycinski commentedon Apr 10, 2017
@bow I forgot about hard-reset... I'll do it for the future. Thanks for help with everything! ;-)
Fix to issue biopython#1126 from biopython/biopython regarding HMMER3…
Adding tests to the issue fix biopython#1126