newick Module

Read/parse a newick tree.

class newick.Node

Defines a Node object, a list of nested Node objects. Recursive phylogeny subunit.


Parse a newick phylogeny, provided either via a file or a string. The tree does not need to be bifurcating, and may be rooted or unrooted. Returns a Node object along which sequences may be evolved.

Trees can either read from a file or given directly to read_tree as a string. One of these two keyword arguments is required.

  1. file, the name of the file containing a newick tree for parsing. If this argument is provided in addition to tstring, the tree in the file will be used and tstring will be ignored.
  2. tree, a newick tree string. If a file is additionally provided, the tstring argument will be ignored.

To implement branch (temporal) heterogeneity, place “model flags” at particular nodes within the tree. Model flags must be in the format _flagname_ (i.e. with both a leading and a trailing underscore), and they should be placed after the branch lengths. Model flags may be repeated throughout the tree, but the model associated with each model flag will always be the same. Note that these model flag names must have correspondingly named model objects.

tree = read_tree(file = "/path/to/tree/file.tre")
tree = read_tree(tree = "(t4:0.785,(t3:0.380,(t2:0.806,(t5:0.612,t1:0.660):0.762):0.921):0.207);")

# Tree containing model flags named m1 and m2
tree = read_tree(tree = "(t4:0.785,(t3:0.380,(t2:0.806,(t5:0.612,t1:0.660):0.762_m1_):0.921)_m2_:0.207);"
newick.print_tree(tree, level=0)

Prints a Node object in graphical, nested format. This function takes two arguments:

  1. tree is a Node object to print
  2. level is used internally for printing. DO NOT PROVIDE THIS ARGUMENT.

Each node in the tree is represented by a string in the format, “name branch.length model.flag”, and levels are represented by indentation. Names for tree tips are taken directly from the provided tree, and internal node names are assigned automatically by the read_tree function. The node with a branch length of None will be the root node where sequence evolution will begin. Note that the model.flag field will be None under cases of branch homogeneity.

For example,
>>> my_tree = newick.read_tree(tree = "(t4:0.785,(t3:0.380,(t2:0.806,(t5:0.612,t1:0.660):0.762):0.921):0.207);")
>>> print_tree(my_tree)
     root None None
         t4 0.785 None
             internal_node3 0.207 None
                 t3 0.38 None
                 internal_node2 0.921 None
                     t2 0.806 None
                     internal_node1 0.762 None
                         t5 0.612 None
                         t1 0.66 None

>>> flagged_tree = newick.read_tree(tree = "(t4:0.785,(t3:0.380,(t2:0.806,(t5:0.612,t1:0.660):0.762_m1_):0.921_m2_):0.207);")
>>> newick.print_tree(flagged_tree)  
      root None None
         t4 0.785 None
         internal_node3 0.207 None
             t3 0.38 None
             internal_node2 0.921 m2
                 t2 0.806 m2
                 internal_node1 0.762 m1
                     t5 0.612 m1
                     t1 0.66 m1