Repository navigation
Description
Spin out from #855 which was specifically for test_NCBIXML.py but has a similar root cause.
One of the test NEXUS files contains a non-ASCII accented character:
$ grep $'\xc3\xb1' Nexus/*.nex
Nexus/bats.nex:Agnarsson I., Zambrana-torrelio C.M., Flores saldaña N.P., & May-collado L.M. 2011.
Does the NEXUS format as anything about locales, unicode, ASCII?
Testing with Biopython 1.70 with my default locale, everything is fine as a UTF8 encoding is the default. However, under some systems (including the multibuild systems for compiling wheels), you can get a default encoding of ascii.
The failure can be recreated under Python 3 as follows, here on Mac OS X using Python 3.6:
$ LANG=C python3 test_Nexus.py
test_NexusComments (__main__.NexusTest1)
Test the ability to parse nexus comments at internal and leaf nodes ... ok
test_NexusTest1 (__main__.NexusTest1)
Test Nexus module ... ok
test_TreeTest1 (__main__.NexusTest1)
Test Tree module. ... ok
test_WriteToFileName (__main__.NexusTest1)
Test writing to a given filename. ... ok
test_internal_node_labels (__main__.NexusTest1)
Handle text labels on internal nodes. ... ok
test_large_newick (__main__.NexusTest1) ... ok
test_write_with_dups (__main__.NexusTest1) ... ok
test_data_and_codons_block (__main__.OldSelfTests)
Simple sequence data file with DATA and CODONS block ... ok
test_data_sets_trees_unknown_block (__main__.OldSelfTests)
Sequence data file with DATA, SETS, TREES and an unknown block ... ok
test_taxa_and_characters_block (__main__.OldSelfTests)
Taxa and characters multi-state block ... ok
test_taxa_and_characters_with_many_codings_one_without_state (__main__.OldSelfTests)
Taxa and chr blocks, over 9 codings, 1 character without states ... ok
test_taxa_and_characters_with_many_codings_two_without_state (__main__.OldSelfTests)
Taxa and chr blocks, over 9 codings, 2 character without states ... ok
test_trees_and_taxa_block (__main__.OldSelfTests)
Basic tree file with TREES and TAXA block ... ERROR
test_empty_file_read (__main__.TestSelf) ... ok
test_multiple_output (__main__.TestSelf) ... ok
test_repeated_names_no_taxa (__main__.TestSelf) ... Repeated names without a TAXA block
IUPACProtein() alignment with 4 rows and 50 columns
-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---- CYS1_DICDI
MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLG ALEU_HORVU
------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK---- CATH_HUMAN
-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---X CYS1_DICDI.copy
Seq('-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ----', IUPACProtein()) CYS1_DICDI CYS1_DICDI
Seq('MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLG', IUPACProtein()) ALEU_HORVU ALEU_HORVU
Seq('------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK----', IUPACProtein()) CATH_HUMAN CATH_HUMAN
Seq('-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---X', IUPACProtein()) CYS1_DICDI CYS1_DICDI.copy
Done
ok
test_repeated_names_with_taxa (__main__.TestSelf) ... Repeated names with a TAXA block
IUPACProtein() alignment with 4 rows and 50 columns
-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---- CYS1_DICDI
MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLG ALEU_HORVU
------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK---- CATH_HUMAN
-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---X CYS1_DICDI.copy
Seq('-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ----', IUPACProtein()) CYS1_DICDI CYS1_DICDI
Seq('MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLG', IUPACProtein()) ALEU_HORVU ALEU_HORVU
Seq('------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK----', IUPACProtein()) CATH_HUMAN CATH_HUMAN
Seq('-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ---X', IUPACProtein()) CYS1_DICDI CYS1_DICDI.copy
Done
ok
======================================================================
ERROR: test_trees_and_taxa_block (__main__.OldSelfTests)
Basic tree file with TREES and TAXA block
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_Nexus.py", line 32, in test_trees_and_taxa_block
nexus1.read('Nexus/bats.nex')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/Nexus/Nexus.py", line 635, in read
file_contents = fp.read()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 398: ordinal not in range(128)
----------------------------------------------------------------------
Ran 17 tests in 0.112s
FAILED (errors=1)
I'm note sure what the best way to fix this is without looking into the NEXUS details... should we switch to parsing it in binary mode? Should we open them in text mode but with encoding='utf-8' by default on Python 3?
CC @etal
Activity
mdehoon commentedon Jun 3, 2021
It's probably the safest choice. The worst that could happen is that the comments at the top of the file will be malformed, but since the actual data is in ASCII, the encoding doesn't matter otherwise.