Skip to content

SearchIO fails to parse HMMER 3 results with additional lines #1126

Closed

Description

@mkorycinski

Hi,

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.

hmmer_bug.zip

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?

Best regards,
Mateusz

Activity

  1. peterjc commented on Mar 28, 2017

    @peterjc
    Member

    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...

  2. bow commented on Mar 28, 2017

    @bow
    Member

    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.py file inside the Bio.SearchIO.HmmerIO module. Feel free to ping me if you have any questions about anything.

  3. mkorycinski commented on Mar 29, 2017

    @mkorycinski
    ContributorAuthor

    @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.

  4. mkorycinski commented on Apr 8, 2017

    @mkorycinski
    ContributorAuthor

    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:

    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
    
    

    hmmer_bug.zip

    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(lambda line: line.startswith('Query:'))
    
        while self.line:
    
            # get query id and length
            regx = re.search(_QRE_ID_LEN, self.line)
            qid = regx.group(1).strip()

    this code gives:

    Traceback (most recent call last):
      File "test_hmmer.py", line 6, in <module>
        for qres in SearchIO.parse(ifile, 'hmmer3-text'):
      File "/usr/local/lib/python3.5/dist-packages/Bio/SearchIO/__init__.py", line 314, in parse
        for qresult in generator:
      File "/usr/local/lib/python3.5/dist-packages/Bio/SearchIO/HmmerIO/hmmer3_text.py", line 47, in __iter__
        for qresult in self._parse_qresult():
      File "/usr/local/lib/python3.5/dist-packages/Bio/SearchIO/HmmerIO/hmmer3_text.py", line 113, in _parse_qresult
        qid = regx.group(1).strip()
    AttributeError: 'NoneType' object has no attribute 'group'

    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(lambda line: line.startswith('Query:'))
    
        while self.line:
            regx = re.search(_QRE_ID_LEN, self.line)
    
            while not regx:
                # Catch if block contains proper qresult
                self.line = read_forward(self.handle)
                regx = re.search(_QRE_ID_LEN, self.line)
    
        # Catch if block contains proper qresult
        qid = 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:

    if self.line.startswith('# Alignment of'):
        self.line = self.handle.readline()

    would simply become:

    if self.line.startswith('#'):
        self.line = self.handle.readline()

    With above mentioned changes all examples are parsed correctly. Would that be acceptable solution? Turned out to be rather simple.

  5. bow commented on Apr 8, 2017

    @bow
    Member

    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.

  6. mkorycinski commented on Apr 8, 2017

    @mkorycinski
    ContributorAuthor

    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?

    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.

  7. added a commit that references this issue on Apr 8, 2017
  8. bow commented on Apr 8, 2017

    @bow
    Member

    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).

  9. added a commit that references this issue on Apr 9, 2017
    d9dd142
  10. mkorycinski commented on Apr 9, 2017

    @mkorycinski
    ContributorAuthor

    All tests run smoothly.

    $ 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:

    def test_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 queries
            self.assertEqual(len(qresults), 2)
    
            # Test whether proper query id is read
            self.assertListEqual([x.id for x in qresults], ['infile_sto',
                                                            'infile_sto2'])
    
            # Test if proper number of hits is read
            self.assertEqual([len(x.hits) for x in qresults], [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

  11. bow commented on Apr 9, 2017

    @bow
    Member

    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).

  12. mkorycinski commented on Apr 9, 2017

    @mkorycinski
    ContributorAuthor

    Sure. I'll fix my local repository according to the guide (thanks for the link!) and create pull request with changes.

  13. mkorycinski commented on Apr 9, 2017

    @mkorycinski
    ContributorAuthor

    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.

  14. added a commit that references this issue on Apr 9, 2017
  15. bow commented on Apr 9, 2017

    @bow
    Member

    @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.

  16. mkorycinski commented on Apr 10, 2017

    @mkorycinski
    ContributorAuthor

    @bow I forgot about hard-reset... I'll do it for the future. Thanks for help with everything! ;-)

  17. added 2 commits that reference this issue on Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions