Repository navigation
Description
Setup
I am reporting a problem with Biopython version, Python version, and operating
system as follows:
import sys; print(sys.version)
import platform; print platform.python_implementation(); print(platform.platform())
import Bio; print(Bio.__version__)import sys; print(sys.version)
3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
import platform; print platform.python_implementation(); print(platform.platform())
import Bio; print(Bio.version)>>> import platform; print platform.python_implementation(); print(platform.platform())
File "", line 1
import platform; print platform.python_implementation(); print(platform.platform())
^
import Bio; print(Bio.version)SyntaxError: invalid syntax
import Bio; print(Bio.version)
1.69
Expected behaviour
print ("Converted 1674 records")
with no Error
Actual behaviour
Traceback (most recent call last):
File "test.py", line 11, in <module>
count = SeqIO.convert("data/SB1200000043.gb", "genbank", "test.fasta", "fasta")
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 1019, in convert
alphabet)
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/_convert.py", line 372, in _handle_convert
return f(in_handle, out_handle, alphabet)
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/_convert.py", line 38, in _genbank_convert_fasta
return SeqIO.write(records, out_handle, "fasta")
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 490, in write
count = writer_class(fp).write_file(sequences)
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/Interfaces.py", line 208, in write_file
count = self.write_records(records)
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/SeqIO/Interfaces.py", line 192, in write_records
for record in records:
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/GenBank/Scanner.py", line 480, in parse_records
record = self.parse(handle, do_features)
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/GenBank/Scanner.py", line 464, in parse
if self.feed(handle, consumer, do_features):
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/GenBank/Scanner.py", line 438, in feed
self.parse_features(skip=True) # ignore the data
File "/Users/brandon/miniconda3/lib/python3.6/site-packages/Bio/GenBank/Scanner.py", line 187, in parse_features
line = self.handle.readline()
File "/Users/brandon/miniconda3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 7634: invalid start byte
Steps to reproduce
$ python2 test.py OK
$ python3 test.py ERROR
I think below code is not compatible with my python3 on this genbank file.
Test CODE:
from Bio import SeqIO
gb_file = 'data/SB1200000043.gb'
from Bio import SeqIO
count = SeqIO.convert("data/SB1200000043.gb", "genbank", "test.fasta", "fasta")
genbank file is
https://www.dropbox.com/s/lwwbdyijhyyrjkz/SB1200000043.gb?dl=0
Activity
peterjc commentedon Aug 10, 2017
It seems our suggested Python commands to report the Python version and platform failed somehow, but it looks like Biopython 1.69 under Python 3.6 via miniconda under Mac OS X?
(Update: That's been fixed in f0a8d67)
Note we've fixed several encoding specific bugs since Biopython 1.70 was released in XML files.
Thank you for the test case, I can reproduce this with the latest Biopython code:
peterjc commentedon Aug 10, 2017
I think these are the problem lines:
Those "question marks" after Lasergene seem to be 0xAE, or in decimal character 174:
On my computer in emacs they show up as R in a circle (
®) which is a symbol for registered trade mark https://en.wikipedia.org/wiki/Registered_trademark_symbolWhere did you get this GenBank format file from? The NCBI directly? If so was it via FTP, Entrez, ... and can you share the original URL?
peterjc commentedon Aug 10, 2017
According to https://www.compart.com/en/unicode/U+00AE and https://www.compart.com/en/unicode/charsets/containing/U+00AE under UTF-8 the registered trademark should be two bytes (0xC2 0xAE), but the single byte encoding 0xAE (as used in your file) did get used in some legacy encodings. Explicitly using one of those encodings might be a workaround for Python 3 without changing Biopython.
We need to find out if the NCBI has an explicit policy on the encoding used in GenBank files...
peterjc commentedon Aug 10, 2017
Quoting ftp://ftp.ncbi.nlm.nih.gov/genbank/gbrel.txt
Spotted by @idoerg - thanks.
This is not quite as strong as saying all GenBank format files should be ASCII only, but it strongly suggests your files are invalid due to the non-ASCII registered trade mark symbol in some of the COMMENT entries. If the files are from the NCBI, we ought to contact them for clarification.
peterjc commentedon Aug 11, 2017
I've checked an example directly from the NCBI website, https://www.ncbi.nlm.nih.gov/nuccore/KU996377
As per this screenshot, the problem character in the comment is shown there as a question-mark-in-black-diamond:
I will email the NCBI about this.
[-]UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 7634: invalid start byte[/-][+]Non-ASCII non-UTF-8 characters in GenBank files break on Python 3[/+]peterjc commentedon Aug 11, 2017
Logged via https://support.ncbi.nlm.nih.gov using the link on https://www.ncbi.nlm.nih.gov/nuccore/KU996377 - their reference #28045-244780
peterjc commentedon Aug 14, 2017
This identifies all the accessions in the shared GenBank file with a unicode problem:
[Update: Example now sorts and reports as a list along with counts, 19 out of 1674 are problematic]
peterjc commentedon Aug 16, 2017
It sounds like the NCBI will curate these records to remove the problem character.
To do that yourself, the following command will remove these characters under Mac OS X or Linux. This works because the problem character is 0xAE in hex, 174 in decimal, and thus 256 in octal.
Note this deliberately temporarily changes the locale settings, it wasn't essential on the Linux machine I tested, but it was on my Mac where the default is UTF-8:
This should confirm the problem characters have been removed:
hmkim commentedon Aug 17, 2017
Thank you so much.
For your close investigation of this.
peterjc commentedon Aug 17, 2017
Thanks for raising the issue - the NCBI should be fixing those 19 records, and it has given us a better awareness of some of the encoding things to watch out for in future.
wiebepo commentedon Feb 16, 2022
For anyone else who might come across this post while searching for a solution. I encountered this issue recently. In my case, I was using compressed Genbank files. In Python 3.8, I used gzip module to avoid encountering the error by ignoring any bytes that did not conform to utf-8.
for r in SeqIO.parse(gzip.open(file, 'rt', encoding='utf8', errors='ignore'), "genbank"):bucongfan commentedon Mar 27, 2023
Please let more people see this reply
hagen-wende commentedon Aug 2, 2024
I also ran into a similar issue. I got this error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1960: ordinal not in range(128)I tried to resolve it by detecting the encoding and converting to utf-8, which unfortunately did not solve the issue. I also checked that there where no non-utf-8 characters in the file. but as it was stating ASCII I then checked for non-ascii characters and found some, removing those solved the issue. This was a user Vector NTI file that hat "5´" instead of "5'" in one of the features.
I solved the issue by parsing the file and removing all non-ASCII characters.