Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A nexus (phylogenetics) file reader (.nex, .trees)

Project description

Reading a Nexus

>>> import os
>>> EXAMPLE_DIR = os.path.join(os.path.dirname(__file__), 'examples')
>>> from nexus import NexusReader
>>> n = NexusReader()
>>> n.read_file(os.path.join(EXAMPLE_DIR, 'example.nex'))
>>> n = NexusReader(os.path.join(EXAMPLE_DIR, 'example.nex'))

# display blocks found in data file >>> n.blocks {‘data’: <NexusDataBlock: 2 characters from 4 taxa>}

data blocks

{'datatype': 'standard', 'symbols': '01', 'gap': '-'}
['Simon', 'Louise', 'Betty', 'Harry']
['0', '1']
>>> sorted(
['Betty', 'Harry', 'Louise', 'Simon']
>>> sorted(
['Betty', 'Harry', 'Louise', 'Simon']
>>> for taxon, characters in #doctest: +SKIP

tree blocks

>>> n = NexusReader(os.path.join(EXAMPLE_DIR, 'example.trees'))
>>> n.trees.ntrees
>>> n.trees.trees[0][0:60]
'tree tree.0.1065.603220 = (((((((Chris:0.0668822155,Bruce:0.'
>>> for tree in n.trees: #doctest: +SKIP

Writing a Nexus File

>>> from nexus import NexusWriter
>>> n = NexusWriter()

Add a comment to appear in the header of the file >>> n.add_comment(“I am a comment”)

data are added by using the “add” function - which takes 3 arguments, a taxon, a character name, and a value

>>> n.add('taxon1', 'Character1', 'A')
{'Character1': {'taxon1': 'A'}}
>>> n.add('taxon2', 'Character1', 'C')
>>> n.add('taxon3', 'Character1', 'A')

Characters and values can be strings or integers >>> n.add(‘taxon1’, 2, 1) >>> n.add(‘taxon2’, 2, 2) >>> n.add(‘taxon3’, 2, 3)

NexusWriter will interpolate missing entries (i.e. taxon2 in this case) >>> n.add(‘taxon1’, “Char3”, ‘4’) >>> n.add(‘taxon3’, “Char3”, ‘4’)

… when you’re ready, you can generate the nexus using make_nexus or write_to_file: n.make_nexus(interleave=True, charblock=True) n.write_to_file(filename=”output.nex”, interleave=True, charblock=True)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for python-nexus, version 1.63
Filename, size File type Python version Upload date Hashes
Filename, size python_nexus-1.63-py2.py3-none-any.whl (76.1 kB) File type Wheel Python version 3.6 Upload date Hashes View hashes
Filename, size python-nexus-1.63.tar.gz (38.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page