python-nexus – a generic nexus (.nex, .trees) reader for python

© Simon J. Greenhill, 2009

Contact me if you have any problems.

Download:

You can download python-nexus from the bitbucket repository.

Usage:

Reading a Nexus

  1. >>> from nexus import NexusReader
  2. >>> n = NexusReader()
  3. >>> n.read_file('example.nex')
  4.  
  5. # or
  6. >>> n = NexusReader('example.nex')
  7. ...
  8.  
  9. # display blocks found in data file
  10. >>> n.blocks
  11. {'data': <nexusDataBlock: 2 characters from 4 taxa>}
  12.  
  13. #`data` blocks
  14. # get the number of characters
  15. >>> n.data.nchar
  16. 2
  17.  
  18. # get the number of taxa
  19. >>> n.data.ntaxa
  20. 4
  21.  
  22. # get the format string
  23. >>> n.data.format
  24. {'datatype': 'standard', 'symbols': '01', 'gap': '-'}
  25.  
  26. # access the data matrix
  27. >>> n.data.matrix
  28. {'Simon': ['01'], 'Louise': ['11'], 'Betty': ['10'], 'Harry': ['00']}
  29.  
  30. # access by taxon:
  31. >>> n.data.matrix['Simon']
  32. ['01']
  33.  
  34. # get list of taxa
  35. >>> n.data.taxa
  36. ['Betty', 'Harry', 'Louise', 'Simon']
  37.  
  38. # loop over taxa and data
  39. >>> for taxon, characters in n.data:
  40. ...
  41.  
  42. # `tree` blocks
  43.  
  44. >>> n = NexusReader('example.trees')
  45. # get the number of trees
  46. >>> n.trees.ntrees
  47. 3
  48.  
  49. # get a tree
  50. >>> n.trees.trees[0]
  51. 'tree tree.0.1065.603220 = (((((((Chris:0.0668822155,Bruce:0.0173144449):0.0062091603,Tom:0.0523825242):0.0206190840,(Henry:0.0482653647,Timothy:0.0744964092):0.0183093750):0.0401805957,(Mark:0.0066961591,Simon:0.0755275882):0.0264078188):0.0536464636,((Fred:0.0428499135,Kevin:0.0734738565):0.0937536292,Roger:0.0538708492):0.0438297939):0.0453008384,(Michael:0.0953237112,Andrew:0.0654710419):0.0803079594):0.0630363263,David:0.0855948485);'
  52.  
  53. # loop over trees
  54. >>> for tree in n.trees:
  55. ...

Writing a Nexus File

  1. >>> from nexus import NexusWriter
  2. >>> n = NexusWriter()
  3.  
  4. # Add a comment to appear in the header of the file
  5. >>> n.add_comment("I am a comment")
  6.  
  7. #data are added by using the "add" function -
  8. #which takes 3 arguments, a taxon, a character name, and a value
  9. >>> n.add('taxon1', 'Character1', 'A')
  10. >>> n.data
  11. {'Character1': {'taxon1': 'A'}}
  12.  
  13. >>> n.add('taxon2', 'Character1', 'C')
  14. >>> n.add('taxon3', 'Character1', 'A')
  15.  
  16. #Characters and values can be strings or integers
  17. >>> n.add('taxon1', 2, 1)
  18. >>> n.add('taxon2', 2, 2)
  19. >>> n.add('taxon3', 2, 3)
  20.  
  21. #NexusWriter will interpolate missing entries (i.e. taxon2 in this case)
  22. >>> n.add('taxon1', "Char3", '4')
  23. >>> n.add('taxon3', "Char3", '4')
  24.  
  25. # ... when you're ready, you can generate the nexus using `make_nexus` or `write_to_file`:
  26. >>> n.make_nexus(interleave=True, charblock=True)
  27. >>> n.write_to_file(filename="output.nex", interleave=True, charblock=True)