sjspielman / pyvolve

Python library to simulate evolutionary sequence data
Other
78 stars 23 forks source link

Unable to Load Single-Node Trees #11

Closed niemasd closed 6 years ago

niemasd commented 6 years ago

Currently, it seems as though the Newick parser in Pyvolve does not allow single-node trees, e.g. N13|4|3.0:3;. For example, the following chunk of code:

treestr = "N13|4|3.0:3;"
import pyvolve
tree = pyvolve.read_tree(tree=treestr)

results in the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/pyvolve/newick.py", line 119, in read_tree
    (tree, flags, internalNode_count, index) = _parse_tree(tstring, flags, internalNode_count, scale_tree, 0) 
  File "/usr/local/lib/python3.6/site-packages/pyvolve/newick.py", line 316, in _parse_tree
    assert(tstring[index]=='(')
AssertionError
niemasd commented 6 years ago

I wrote a fairly simple Newick parser in my own package, TreeSwift, that handles trees like this. Feel free to reuse the logic if you wish to implement this ability:

sjspielman commented 6 years ago

Hi @niemasd,

pyvolve will read in this tree fine if it has parentheses, which is a required component of newick format:

>>> from pyvolve import *
>>> t = "(N13|4|3.0:3);"
>>> tree = read_tree(tree = t)
>>> tree
<pyvolve.newick.Node instance at 0x10e250368>
>>> print_tree(tree)
root None None
    N13|4|3.0 3.0 None

This will set up a simulation for a single root sequence along a branch with length of 3.0

Best, Stephanie

niemasd commented 6 years ago

Ah, excellent, thank you for the help!