treemaker package

Submodules

treemaker.test_treemaker module

class treemaker.test_treemaker.Test_ParseArgs(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_IOError_on_no_file()[source]
test_parse_filename_and_full_output()[source]
test_parse_filename_and_output()[source]
test_parse_filename_only()[source]
test_parse_mode()[source]
test_parse_nodelabels()[source]
class treemaker.test_treemaker.Test_Tree(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_add()[source]
test_conflicts()[source]
test_deep_tree()[source]
test_empty_tree()[source]
test_eq()[source]
test_example()[source]
test_get()[source]
test_get_or_create()[source]
test_gt()[source]
test_is_node()[source]
test_is_tip()[source]
test_lt()[source]
test_real()[source]
test_recursive_add()[source]
test_repr()[source]
test_sanitise()[source]
test_simple()[source]
test_singleton_tree()[source]
test_tips()[source]
class treemaker.test_treemaker.Test_TreeMaker(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_add()[source]
test_add_2()[source]
test_add_from()[source]
test_add_from_exception()[source]
test_error_on_bad_taxon()[source]
test_error_on_duplicate()[source]
test_parse_classification()[source]
class treemaker.test_treemaker.Test_TreeMakerIO(methodName='runTest')[source]

Bases: unittest.case.TestCase

Test the IO functionality of TreeMaker in its own test class as we need some setup/teardown logic.

classmethod setUpClass()[source]

Hook method for setting up class fixture before running tests in the class.

classmethod tearDownClass()[source]

Hook method for deconstructing the class fixture after running all tests in the class.

test_read()[source]
test_read_error_on_malformed()[source]
test_read_skips_empty_lines()[source]
test_read_with_tabs()[source]
test_write_newick()[source]
test_write_nexus()[source]
test_write_nexus_error_on_bad_method()[source]
test_write_to_file_error_on_existing_file()[source]
test_write_to_file_error_on_invalid_mode()[source]
test_write_to_newick()[source]
test_write_to_nexus()[source]
class treemaker.test_treemaker.Test_Tree_Nodelabels(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_deep_tree()[source]
test_example()[source]
test_real()[source]
test_recursive_add()[source]
test_simple()[source]

treemaker.treemaker module

TreeMaker

class treemaker.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.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”.
treemaker.treemaker.main(args=None)[source]
treemaker.treemaker.parse_args(args)[source]

Parses command line arguments

Returns a tuple of (inputfile, method, outputfile)

Module contents