skbio.tree.TreeNode.to_taxonomy#

TreeNode.to_taxonomy(allow_empty=False, filter_f=None)[source]#

Return a taxonomy representation of self.

Parameters:
allow_emptybool, optional

Allow gaps the taxonomy (e.g., internal nodes without names).

filter_ffunction, optional

Specify a filtering function that returns True if the lineage is to be returned. This function must accept a TreeNode as its first parameter, and a list that represents the lineage as the second parameter.

Yields:
tuple

(tip, [lineage]) where tip corresponds to a tip in the tree and [lineage] is the expanded names from root to tip. None and empty strings are omitted from the lineage.

Notes

If allow_empty is True and the root node does not have a name, then that name will not be included. This is because it is common to have multiple domains represented in the taxonomy, which would result in a root node that does not have a name and does not make sense to represent in the output.

Examples

>>> from skbio.tree import TreeNode
>>> lineages = {'1': ['Bacteria', 'Firmicutes', 'Clostridia'],
...             '2': ['Bacteria', 'Firmicutes', 'Bacilli'],
...             '3': ['Bacteria', 'Bacteroidetes', 'Sphingobacteria'],
...             '4': ['Archaea', 'Euryarchaeota', 'Thermoplasmata'],
...             '5': ['Archaea', 'Euryarchaeota', 'Thermoplasmata'],
...             '6': ['Archaea', 'Euryarchaeota', 'Halobacteria'],
...             '7': ['Archaea', 'Euryarchaeota', 'Halobacteria'],
...             '8': ['Bacteria', 'Bacteroidetes', 'Sphingobacteria'],
...             '9': ['Bacteria', 'Bacteroidetes', 'Cytophagia']}
>>> tree = TreeNode.from_taxonomy(lineages.items())
>>> lineages = sorted([(n.name, l) for n, l in tree.to_taxonomy()])
>>> for name, lineage in lineages:
...     print(name, '; '.join(lineage))
1 Bacteria; Firmicutes; Clostridia
2 Bacteria; Firmicutes; Bacilli
3 Bacteria; Bacteroidetes; Sphingobacteria
4 Archaea; Euryarchaeota; Thermoplasmata
5 Archaea; Euryarchaeota; Thermoplasmata
6 Archaea; Euryarchaeota; Halobacteria
7 Archaea; Euryarchaeota; Halobacteria
8 Bacteria; Bacteroidetes; Sphingobacteria
9 Bacteria; Bacteroidetes; Cytophagia