BioPython write combined nexus files
2
1
Entering edit mode
4.4 years ago
nmkn ▴ 10

I'm trying to concatenate nexus files using BioPython as documented in this page here.

It seems to create the objects just fine but fails to write the actual file - without any error.

from Bio.Nexus import Nexus

file_list = ['T591_1.nex','T591_2.nex']
nexi =  [(fname, Nexus.Nexus(fname)) for fname in file_list]

combined = Nexus.combine(nexi)
combined.write_nexus_data(filename=open('btCOMBINED.nex', 'w'))

combined

<Bio.Nexus.Nexus.Nexus object at 0x103a49b20>

I searched for the combined.write_nexus_data() function but it appears to not exist anymore? When I try using

write_nexus_data()

instead I get the error: NameError: name 'write_nexus_data' is not defined.

Any help would be greatly appreciated. I don't know python and this is my first attempt to use BioPython.

biopython nexus • 2.1k views
ADD COMMENT
1
Entering edit mode

Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or select a chunk of text and use the highlighted button to format it as a code block. I've done it for you this time.
code_formatting

ADD REPLY
0
Entering edit mode

Did combined.write_nexis_data() threw an error? Was the file created? It might be in a directory you don't expect it to be in, try giving it an absolute path maybe.

ADD REPLY
0
Entering edit mode

No file created in the working directory and no error thrown.

ADD REPLY
0
Entering edit mode

I've not used this bit of the module before, but if I had to guess, I'd say the documentation is probably just out of date relative to the codebase.

Looking at https://biopython.org/DIST/docs/api/Bio.Nexus.Nexus-pysrc.html#Nexus.write_nexus_data

There appears to be a 'append' flag to that method, so perhaps they've refactored the code to have one write method which serves all purposes (again, total guess).

ADD REPLY
0
Entering edit mode

Okay, I tried also using write_nexus_data(filename=open('btCOMBINED.nex', 'w')) but then got that error which I am unsure how I am using the command wrong.

ADD REPLY
0
Entering edit mode
4.4 years ago
Joe 21k

Can you specify what versions of python and biopython you are using?

I've just attempted to run the documented examples, with their example files as at: https://biopython.org/wiki/Concatenate_nexus

It works fine for me on Python 3.7.3 and BioPython 1.74:

Downloading test data:

$ curl https://biopython.org/examples/btCOI.nex > btCOI.nex
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   151  100   151    0     0   2745      0 --:--:-- --:--:-- --:--:--  2745

$ curl https://biopython.org/examples/btCOII.nex > btCOII.nex
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   151  100   151    0     0    766      0 --:--:-- --:--:-- --:--:--   762

$ curl https://biopython.org/examples/btITS.nex > btITS.nex
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   151  100   151    0     0   1078      0 --:--:-- --:--:-- --:--:--  1086

In python:

$ python
Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> from Bio.Nexus import Nexus
>>> file_list = ['btCOI.nex', 'btCOII.nex', 'btITS.nex']
>>> nexi =  [(fname, Nexus.Nexus(fname)) for fname in file_list]
>>> combined = Nexus.combine(nexi)
>>> combined.write_nexus_data(filename=open('btCOMBINED.nex', 'w'))
<_io.TextIOWrapper name='btCOMBINED.nex' mode='w' encoding='UTF-8'>

>>> [x for x in os.listdir() if x.endswith("nex")]
[''btCOMBINED.nex', 'btCOI.nex', 'btCOII.nex', 'btITS.nex']
>>> exit()

Check file:

$ cat btCOMBINED.nex
#NEXUS
begin data;
    dimensions ntax=4 nchar=32;
    format datatype=dna missing=? gap=-;
matrix
bt1 GGGGGGGGGGGGAAAAAAAAAAAA-TTTTTTT
bt2 GGGGGGGGGGGGAAAAAAAAAAAA-TTTTTTT
bt3 GGGGGGGGGGGGAAAAAAAAAAAA-TTTTTTT
bt4 ????????????????????????-TTTTTTT
;
end;

begin sets;
charset btCOI.nex = 1-12;
charset btCOII.nex = 13-24;
charset btITS.nex = 25-32;
charpartition combined = btCOI.nex: 1-12, btCOII.nex: 13-24, btITS.nex: 25-32;
end;
ADD COMMENT
0
Entering edit mode

Okay, thanks. I'm using Python 3.8.3 (called by python3 since I also have a legacy version) and Biopython 1.77. I don't get a file to write using the sample data either. Do I need to use <_io.TextIOWrapper name='btCOMBINED.nex' mode='w' encoding='UTF-8'>? It errors when I try to add that.

ADD REPLY
0
Entering edit mode

No, that's not a code line, it's just the printed output of the previous command showing a description of the Nexus object.

Do you have all the necessary permissions to create and modify files in the directory you're working in?

Have you tried their example files? Perhaps there's some issue combining your specific data.

ADD REPLY
0
Entering edit mode

I stated above - don't get a file to write using the sample data either. I do have permissions to write to my directory. Thanks anyway.

ADD REPLY
0
Entering edit mode

If this is a genuine bug, it must have come in in the subsequent releases. You could try either downgrading your biopython to 1.74 and give that a go, or post a bug report at the biopython github issues page (they have a template for providing all the necessary information).

I'm afraid I'm out of ideas!

ADD REPLY
0
Entering edit mode
4.4 years ago
nmkn ▴ 10

I couldn't figure out how to get this to work so I used another program for the same task instead: Sequence Matrix .

ADD COMMENT

Login before adding your answer.

Traffic: 1670 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6