Hi, I've been trying to concatenate multiple genes into a single nexus file to be used in MrBayes.
Apparently, Biopython has this script in their website, but it's not working for me (ref: http://biopython.org/wiki/Concatenate_nexus):
from Bio.Nexus import Nexus
# the combine function takes a list [(name, nexus instance)...], if we provide the
# file handles in a list we can use a list comprehension to such a list easily
handles = [open('btCOI.nex', 'r'), open('btCOII.nex', 'r'), open('btITS.nex', 'r')]
nexi = [handle.name, Nexus.Nexus(handle)) for handle in file_list]
combined = Nexus.combine(nexi)
combined.write_nexus_data(filename='btCOMBINED.nex')
It keeps spitting me this error:
nexi = [handle.name, Nexus.Nexus(handle)) for handle in file_list]
NameError: name 'file_list' is not defined
If it worked it would be great, because it would automatically create partitions for each gene, etc, which is exactly what I need. Any help in this direction is welcome. :-)
I think Frédéric's answer is right - I actually wrote the example on the Biopython wiki, so sorry for letting the bug slip in.
The BioPython Wiki helped me so many times! Thank you for work.
Yep - the encoding thing was reported (but not on the mailing list or bug tracker) and fixed ready for our next release: https://github.com/biopython/biopython/commit/f152fa2c2a7f71adaaf51b746cfd0a4308ea2edb - Sorry about that.
David, could this be simplified a little by giving Nexus filenames instead of handles?
Sounds great, but now I'm hitting this error message: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Nexus/Nexus.py", line 1275, in write_nexus_data fh=open(filename,'w',encoding="utf-8") TypeError: 'encoding' is an invalid keyword argument for this function PS: Thanks for all the hard work in the wiki, it's very useful indeed! :-)
I changed, as suggested by david w, the last line to combined.write_nexus_data(open("combined.nex", "w")) and it works now. Thank you all for the help! :D
@Peter - yep, the example was written when
SeqIO
took handles, so I guess that was just the style.Nexus.combine()
takes either handles or filenames@Andre, thanks for alerting us to these bugs, I gather the 'new' error you found came from file handles changing in python 2/3. I'll update the wiki with the workaround, but the problem is already fixed in the github version of Biopython - so you will be able to use a filename in the next release