Skip to content

Instantly share code, notes, and snippets.

@brantfaircloth
Created June 26, 2012 22:01
Show Gist options
  • Save brantfaircloth/2999578 to your computer and use it in GitHub Desktop.
Save brantfaircloth/2999578 to your computer and use it in GitHub Desktop.
Splitting a concatenated nexus file with biopython
from Bio.Nexus import Nexus
aln = Nexus.Nexus()
aln.read('my-properly-formatted-nexus-file.nex')
# assuming your partitions are defined in a charset block like:
#
# begin sets;
# charset bag2 = 1-186;
# charset bag3 = 187-483;
# charset bche = 484-990;
# end;
# get count of charsets:
len(aln.charsets.keys())
# take a gander at the charsets:
aln.charsets()
# split the concatenated file into charsets files (prepending whatever text you place after filename='')
aln.write_nexus_data_partitions(filename='my', charpartition=aln.charsets)
# this will output in the os.getcwd():
#
# test_bag2
# test_bag3
# test_bche
@n8upham
Copy link

n8upham commented Apr 19, 2015

Hi Brant
I keep getting this error:

Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Nexus/Nexus.py", line 640, in read
self._parse_nexus_block(title, contents)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Nexus/Nexus.py", line 683, in _parse_nexus_block
raise NexusError('Unknown command: %s ' % line.command)
Bio.Nexus.Nexus.NexusError: Unknown command: format

Seems to be something with my nexus format? Or possibly its not finding the correct file? When I include the full directory before the filename, there is no error, but then "len(aln.charsets.keys())" returns length 0 even though I have 30 charsets in the file. Can you point me in the right direction..? Thanks, --nate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment