Question: BioPython write combined nexus files
0
gravatar for nmkn
3 days ago by
nmkn0
nmkn0 wrote:

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 • 85 views
ADD COMMENTlink modified 1 day ago • written 3 days ago by nmkn0
1

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 REPLYlink written 3 days ago by RamRS27k

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 REPLYlink written 2 days ago by Asaf8.0k

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

ADD REPLYlink written 2 days ago by nmkn0

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 REPLYlink written 2 days ago by Joe17k

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 REPLYlink written 2 days ago by nmkn0
0
gravatar for Joe
2 days ago by
Joe17k
United Kingdom
Joe17k wrote:

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 COMMENTlink modified 2 days ago • written 2 days ago by Joe17k

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 REPLYlink modified 2 days ago • written 2 days ago by nmkn0

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 REPLYlink modified 1 day ago • written 1 day ago by Joe17k

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 REPLYlink written 14 hours ago by nmkn0
0
gravatar for nmkn
1 day ago by
nmkn0
nmkn0 wrote:

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

ADD COMMENTlink written 1 day ago by nmkn0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1077 users visited in the last hour