biopython icon indicating copy to clipboard operation
biopython copied to clipboard

Genbank to Fasta conversion problem in Python3

Open TBWarrington opened this issue 8 years ago • 8 comments

Setup

I am reporting a problem with Biopython version 1.71, Python version 3.5, and operating system Ubuntu (windows linux subsystem) as follows:

>>> import sys; print(sys.version)
3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
>>> import platform; print(platform.python_implementation()); print(platform.platform())
CPython
Linux-4.4.0-17134-Microsoft-x86_64-with-Ubuntu-16.04-xenial
>>> import Bio; print(Bio.__version__)
1.71
>>>

(Please copy and run the above in your Python, and copy-and-paste the output)

Expected behaviour

Script to convert genbank files to fasta

Actual behaviour

Works fine in Python2.7 but not Python3.5

Gives this error:

$ python3 gb2muli-line-fa.py C150A.gb test.fa
/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented bidirectional feature?
  BiopythonParserWarning)
/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented Origin feature?
  BiopythonParserWarning)
/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented tf_interacting_site feature?
  BiopythonParserWarning)
/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/__init__.py:1143: BiopythonParserWarning: Couldn't parse feature location: 'Terminator5068..5113'
  % location_line))
Traceback (most recent call last):
  File "gb2muli-line-fa.py", line 12, in <module>
    for seq_record in SeqIO.parse(input_handle, "genbank") :
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/SeqIO/__init__.py", line 617, in parse
    for r in i:
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py", line 489, in parse_records
    record = self.parse(handle, do_features)
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py", line 473, in parse
    if self.feed(handle, consumer, do_features):
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py", line 445, in feed
    self._feed_feature_table(consumer, self.parse_features(skip=False))
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/Scanner.py", line 395, in _feed_feature_table
    consumer.location(location_string)
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/GenBank/__init__.py", line 1040, in location
    strand)
  File "/home/Tim/.local/lib/python3.5/site-packages/Bio/SeqFeature.py", line 676, in __init__
    self.start))
ValueError: End location (716) must be greater than or equal to start location (8571)

Steps to reproduce

run attached script on attached genbank file.

$ python3 gb2muli-line-fa.py C150A.gb test.fa

gb2muli-line-fa.zip

TBWarrington avatar Jun 06 '18 21:06 TBWarrington

There are at least two problems with your GenBank file from the log above.

The warning Couldn't parse feature location: 'Terminator5068..5113' shows there is an invalid location in the file. This is being ignored, so that feature will get a location value of None.

     bidirectional Terminator 5068..5113
                              /label="B011"
                              /ApEinfo_revcolor=#b1ff67
                              /ApEinfo_fwdcolor=#b1ff67

The feature type should not have any spaces in it, so bidirectional_terminator might work - although you would be better following the actual standards here. This happens multiple times in the file, e.g.:

     Origin of Replication    complement(7813..8401)
                              /label="ori (pMB1 derivative)"
                              /ApEinfo_revcolor=#84b0dc
                              /ApEinfo_fwdcolor=#84b0dc

The second problem, ValueError: End location (716) must be greater than or equal to start location (8571) is a result of Biopython 1.71 being much stricter about out-of-order coordinates. Given this caused multiple people problems, our plan for Biopython 1.72 is this will in future just give another warning, and the feature will get a missing location value of None. See #1497 and linked issues like #1608 for discussion.

The problem feature here is:

     Gene                     complement(8572..716)
                              /label="AmpR (bla)"
                              /ApEinfo_revcolor=#ffef86
                              /ApEinfo_fwdcolor=#ffef86

If that is mean to span the origin, since your genome is 8716bp, I think that ought to be something like this (untested):

     Gene                     complement(join(8572..8716,1..716))
                              /label="AmpR (bla)"
                              /ApEinfo_revcolor=#ffef86
                              /ApEinfo_fwdcolor=#ffef86

If you say it worked with Biopython under Python 2.7? In that case you are almost certainly using an older Biopython than Biopython 1.71.

If all you want is the FASTA output, you can simple delete all these features from the GenBank file. Or avoid Biopython 1.71 as a workaround.

Note that the NCBI use the extension .faa for FASTA Amino Acids - and your script makes a nucleotide FASTA file.

Also, unless you really really want 70 character line wrapping, your script can be much shorter - this will give 60 character wrapping of the sequence:

from Bio import SeqIO
import textwrap

gbk_filename = sys.argv[1]
fasta_filename = sys.argv[2]

SeqIO.convert(gbk_filename, "gb", fasta_filename, "fasta")

Finally, this does suggest a problem in the tool producing the GenBank file which is worth following up.

peterjc avatar Jun 07 '18 09:06 peterjc

If you are comfortable updating to the latest code from GitHub, as of pull request #1657, this is what your script would now give:

$ python3 gb2muli-line-fa.py C150A.gb C150A.fasta
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented bidirectional feature?
  BiopythonParserWarning)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented Origin feature?
  BiopythonParserWarning)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Overindented tf_interacting_site feature?
  BiopythonParserWarning)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/__init__.py:1151: BiopythonParserWarning: Couldn't parse feature location: 'Terminator5068..5113'
  % location_line))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/__init__.py:1047: BiopythonParserWarning: Ignoring invalid location: '8572..716'
  BiopythonParserWarning)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/__init__.py:1151: BiopythonParserWarning: Couldn't parse feature location: 'ofReplicationcomplement(7813..8401)'
  % location_line))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/__init__.py:1151: BiopythonParserWarning: Couldn't parse feature location: 'Terminator7286..7331'
  % location_line))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/GenBank/__init__.py:1047: BiopythonParserWarning: Ignoring invalid location: '7382..1316'
  BiopythonParserWarning)
Dealing with GenBank record pC150A

i.e. It should work again under Biopython 1.72 but with more warnings than under Biopython 1.70

  • Over indented bidirectional feature?
  • Over indented Origin feature?
  • Couldn't parse feature location: 'Terminator5068..5113'
  • Couldn't parse feature location: 'ofReplicationcomplement(7813..8401)'
  • Couldn't parse feature location: 'Terminator7286..7331'
  • Ignoring invalid location: '7382..1316'

All these warnings are saying there are multiple problems with your GenBank file - it seems to have been produced by an editor of some kind - Which tool? Are you using their latest version?

peterjc avatar Jun 07 '18 10:06 peterjc

Great! I definitely prefer warnings to crashing. I realized after I posted that my python2 is using biopython 1.6. Yes, the GenBank file was produced by the export function of Benchling. I'll get ahold of them and let them know about the improperly formatted gb file. Thanks for your help.

TBWarrington avatar Jun 08 '18 00:06 TBWarrington

@peterjc I wounder if it's worth to have a module for reading "Genbank" formats from third party programs? Or as extensions to SeqIO? SnapGene, Benchling and ApE (the above file has seen ApE at least once, e.g. /ApE_info...) are popular programs and is kind of unrealistic always to get their programmers to follow to the standard...

MarkusPiotrowski avatar Jun 08 '18 08:06 MarkusPiotrowski

@MarkusPiotrowski we do have EMBL and an explicit sub-format IMGT which has a few consistent deviations like longer indentation of the feature table to allow for longer feature types. However, I do not think any of the GenBank variants are consistent enough to merit their own format sub-type in Biopython. Rather I think we should continue to try to do the best job we can with semi-broken GenBank files, issuing warnings as appropriate, and urge the problem tool authors to comply with the formal standard.

@TBWarrington thanks for getting in touch with the Benchling team - the closer they can match the NCBI GenBank formatting, the more success their customers will have importing the data into other packages.

Closing this issue as resolved (pending the release of Biopython 1.72)

peterjc avatar Jun 12 '18 10:06 peterjc

Biopython 1.72 is now out, this should give a warning here rather than an error.

Updated with examples:

$ python -c "from Bio import SeqIO; print(SeqIO.read('C150A.gb', 'gb').format('fasta'))" 
/Library/Python/2.7/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Over indented bidirectional feature?
  BiopythonParserWarning)
/Library/Python/2.7/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Over indented Origin feature?
  BiopythonParserWarning)
/Library/Python/2.7/site-packages/Bio/GenBank/Scanner.py:202: BiopythonParserWarning: Over indented tf_interacting_site feature?
  BiopythonParserWarning)
/Library/Python/2.7/site-packages/Bio/GenBank/__init__.py:1163: BiopythonParserWarning: Couldn't parse feature location: 'Terminator5068..5113'
  % location_line))
/Library/Python/2.7/site-packages/Bio/GenBank/__init__.py:1059: BiopythonParserWarning: Ignoring invalid location: '8572..716'
  BiopythonParserWarning)
/Library/Python/2.7/site-packages/Bio/GenBank/__init__.py:1163: BiopythonParserWarning: Couldn't parse feature location: 'ofReplicationcomplement(7813..8401)'
  % location_line))
/Library/Python/2.7/site-packages/Bio/GenBank/__init__.py:1163: BiopythonParserWarning: Couldn't parse feature location: 'Terminator7286..7331'
  % location_line))
/Library/Python/2.7/site-packages/Bio/GenBank/__init__.py:1059: BiopythonParserWarning: Ignoring invalid location: '7382..1316'
  BiopythonParserWarning)
>pC150A
CCACGCTCACCGGCTCCAGATTTATCAGCAATAAACCAGCCAGCCGGAAGGGCCGAGCGC
AGAAGTGGTCCTGCAACTTTATCCGCCTCCATCCAGTCTATTAATTGTTGCCGGGAAGCT
...

Or, the optimised route which ignores all the features (and thus does not trigger their warnings):

$ python -c "from Bio import SeqIO; import sys; SeqIO.convert(sys.stdin, 'gb', sys.stdout, 'fasta')" < C150A.gb 
>pC150A
CCACGCTCACCGGCTCCAGATTTATCAGCAATAAACCAGCCAGCCGGAAGGGCCGAGCGC
AGAAGTGGTCCTGCAACTTTATCCGCCTCCATCCAGTCTATTAATTGTTGCCGGGAAGCT
...

peterjc avatar Jul 04 '18 12:07 peterjc

Hi Peter, I'm one of the developers from Benchling. Thanks for the detailed notes here - we'll be adding proper support for wrapping annotations using join.

Unfortunately, we are not able to strictly stick to the standard for feature types (assuming you are referring to something like this list: http://www.insdc.org/documents/feature_table.html#7.3). We take in data from a lot of different sources, and ideally output the same data when they export it out of Benchling. There are many sources that don't follow the exact genbank spec, and it ended up being better for us to preserve their data. That obviously doesn't mean you need to support that as well, but just to give you a sense of why we allow e.g. spaces in the feature names right now during export.

vineetgopal avatar Jul 05 '18 04:07 vineetgopal

Hi @vineetgopal - thanks for commenting. I appreciate the design pressures when dealing with multiple input sources, especially if free format rather than restricted to a controlled vocabulary or ontology.

The INSDC feature key list is at http://www.insdc.org/documents/feature_table.html#7.2 (e.g. gene) with the qualifiers (key-value entries) listed at http://www.insdc.org/documents/feature_table.html#7.3 (e.g. translation, note).

Regarding spaces in feature keys, that does break the Biopython parser (but the trade off is we can cope with unusual indentation). I would urge you follow the specification examples and convert spaces to underscores here, e.g. bidirectional Terminator --> bidirectional_Terminator and Origin of Replication --> Origin_of_Replication from the examples earlier.

However, in many cases it ought to be possible to map to an official entry, for example rep_origin is defined for the origin of replication, and gene (rather than Gene) is defined too.

Perhaps in your user interface you could offer a drop down menu of the INSDC defined feature types, and give some visual feedback when a non-standard entry is used?

peterjc avatar Jul 05 '18 09:07 peterjc