Welcome to treemaker’s documentation!

class treemaker.Tree(node=None, children=None, show_nodelabels=False)[source]

Bases: object

Tree object to represent the classification taxonomy.

Parameters:
  • node (str) – Label for this node.
  • children (list) – Optional list of children nodes
  • show_nodelabels (boolean) – A flag to show nodelabels or not (default=False)
add(node, children=None)[source]

Adds a node (with optional children) to the tree.

Parameters:
  • node (str) – Label for this node.
  • children (list) – Optional list of children nodes
Returns:

the created node matching label.

Return type:

treemaker.Tree

get(label, node=None)[source]

Searches tree for node label.

Parameters:
  • label (str) – Label for this node.
  • node (treemaker.Tree) – (optional) parent node. Default is current node.
Returns:

the found or created node matching label.

Return type:

treemaker.Tree

get_or_create(label)[source]

Helper function to get or create a node.

Parameters:label (str) – Label for this node.
Returns:the found or created node matching label.
Return type:treemaker.Tree
is_node

Returns True if node is a node (i.e. has children)

is_tip

Returns True if node is a tip

tips(node=None)[source]

Returns a list of the tips in the tree

Parameters:node (treemaker.Tree) – (optional) parent node. Default is current node.
Returns:the tip nodes from the given node.
Return type:List[treemaker.Tree]
class treemaker.TreeMaker(label='root', nodelabels=False)[source]

Bases: object

add(leaf, classification)[source]

Adds leaf to the tree in the location specified by classification

Parameters:
  • leaf (str) – Leaf label
  • classification (str) – A classification string of a format handled by parse_classification.
Returns:

the tree with the new node added.

Return type:

treemaker.Tree

Raises:

ValueError – If a duplicate leaf label or classification is given.

add_from(iterable)[source]

Adds all entries from an iterable. iterable should be a list of lists or a list of tuples (etc) with 2 values - the first one the taxon name, the second the classification string, e.g.

>>> iterable = [
>>>     ['taxon1', 'a, a'],
>>>     ['taxon2', 'a, b'],
>>> ]
>>> tree = TreeMaker().add_from(iterable)
Parameters:iterable (iter) – an iterable (e.g. a list).
Returns:the tree with the new nodes added.
Return type:treemaker.Tree
Raises:ValueError – If each member of the iterable does not contain two entries (leaf name, and classification).
parse_classification(classification)[source]

Parses a classification string into nodes.

>>> Tree().parse_classification("Indo-European, Germanic, English")
>>> ["Indo-European", "Germanic", "English"]
Parameters:classification (str) – a classification string e.g. “clade 1, clade 2, clade 3”
Returns:a list of the classification nodes.
Return type:List
read(filename)[source]

Reads data from filename and constructs a tree.

filename should be formatted as follows:

Taxon1   FamilyA, GroupA, SubgroupA
Taxon2   FamilyA, GroupA, SubgroupB
Taxon3   FamilyA, GroupB
... etc
Parameters:filename (str) – a filename containing the classification.
Returns:a Tree with the specified classification.
Return type:treemaker.Tree
Raises:ValueError – if a line in the file is not able to be parsed.
write(mode='newick')[source]

Writes the output form of the tree.

Parameters:
  • mode (str) – An output mode. One of: * “nexus” = a nexus file is generated * “newick” = a newick file (bare tree) is generated
  • nodelabels (bool) – show nodelabels or not (default False)
Returns:

a string containing the formatted content.

Return type:

str

Raises:

ValueError – if mode is not “nexus” or “newick”.

write_to_file(filename, mode='nexus')[source]

Writes the tree to filename.

Parameters:
  • mode (str) – An output mode. One of: * “nexus” = a nexus file is generated * “newick” = a newick file (bare tree) is generated
  • nodelabels (bool) – show nodelabels or not (default False)
Returns:

None

Raises:
  • IOError – if filename already exists.
  • ValueError – if mode is not “nexus” or “newick”.

Indices and tables