Useful functions

class scHPL.utils.TreeNode(name=None, **kw)[source]

Bases: Node

This class is an extension of Node, but with the possibility to add a classifier

Attributes:
is_binary
is_leaf
length
newick

The representation of the Node in Newick format.

Methods

ascii_art([strict, show_internal])

Return a unicode string representing a tree in ASCII art fashion.

create([name, descendants])

Create a new Node object.

get_leaf_names()

Get the names of all the leaf nodes of the subtree descending from this node.

get_leaf_names_first()

Get the names of all the leaf nodes of the subtree descending from this node.

get_leaves()

Get all the leaf nodes of the subtree descending from this node.

get_node(label)

Gets the specified node by name.

prune(leaves[, inverse])

Remove all those nodes in the specified list, or if inverse=True, remove all those nodes not in the specified list.

prune_by_names(leaf_names[, inverse])

Perform an (inverse) prune, with leaves specified by name.

remove_internal_names()

Set the name of all non-leaf nodes in the subtree to None.

remove_leaf_names()

Set the name of all leaf nodes in the subtree to None.

remove_lengths()

Set the length of all nodes in the subtree to None.

remove_names()

Set the name of all nodes in the subtree to None.

remove_redundant_nodes([preserve_lengths])

Remove all nodes which have only a single child, and attach their grandchildren to their parent.

resolve_polytomies()

Insert additional nodes with length=0 into the subtree in such a way that all non-leaf nodes have only 2 descendants, i.e. the tree becomes a fully resolved binary tree.

set_classifier(classifier)

Add a classifier to the node.

visit(visitor[, predicate])

Apply a function to matching nodes in the (sub)tree rooted at self.

walk([mode])

Traverses the (sub)tree rooted at self, yielding each visited Node.

add_descendant

get_RE

get_classifier

get_dimred

get_maxDist

get_pca

set_RE

set_dimred

set_maxDist

set_pca

classmethod create(name=None, descendants=None, **kw)[source]

Create a new Node object.

Parameters:
  • name – Node label.

  • descendants – list of descendants or None.

  • kw – Additonal keyword arguments are passed through to Node.__init__.

Returns:

Node instance.

get_RE()[source]
get_classifier()[source]
get_dimred()[source]
get_leaf_names_first()[source]

Get the names of all the leaf nodes of the subtree descending from this node. :return: List of names of Nodes with no descendants.

get_maxDist()[source]
get_pca()[source]
set_RE(maxRE)[source]
set_classifier(classifier)[source]

Add a classifier to the node.

set_dimred(value)[source]
set_maxDist(maxDist)[source]
set_pca(pca, explainingpc)[source]
scHPL.utils.add_node(name: str, tree: TreeNode, parent: str, children: list | None = None)[source]

Add a node to the tree manually.

Parameters:

name: String

Name of the cell population to add

tree: TreeNode

Tree the node should be added to

parent: String

Parent node the cell population should be added to. If the node should be added to the root, this should be ‘root’

children: List

If the new node is an internal node, the child nodes should also be defined. Format should be a list with strings e.g. [‘child1’, ‘child2’]

scHPL.utils.create_tree(s, strip_comments=False, **kw)[source]

Load a list of trees from a Newick formatted string.

Parameters:
  • s – Newick formatted string.

  • strip_comments – Flag signaling whether to strip comments enclosed in square brackets.

  • kw – Keyword arguments are passed through to Node.create.

Returns:

List of Node objects.

scHPL.utils.load(fp, strip_comments=False, **kw)[source]

Load a list of trees from an open Newick formatted file.

Parameters:
  • fp – open file handle.

  • strip_comments – Flag signaling whether to strip comments enclosed in square brackets.

  • kw – Keyword arguments are passed through to Node.create.

Returns:

List of Node objects.

scHPL.utils.print_tree(tree: TreeNode, new_nodes: list = [])[source]

Print the tree

Parameters:
  • tree (TreeNode) – Tree to print

  • new_nodes (List = []) – Nodes recently added to the tree, these are printed in bold

Return type:

None.

scHPL.utils.read_tree(fname, encoding='utf8', strip_comments=False, **kw)[source]

Load a list of trees from a Newick formatted file.

Parameters:
  • fname – file path.

  • strip_comments – Flag signaling whether to strip comments enclosed in square brackets.

  • kw – Keyword arguments are passed through to Node.create.

Returns:

List of Node objects.

scHPL.utils.remove_node(name, tree: TreeNode, children: bool = True)[source]

Remove a node from the tree

Parameters:
  • name (String or List) – name of the node to remove

  • tree (TreeNode) – tree containing this node

  • children (Boolean = True) – if True, children are also removed. If False: children are rewired to parent node

Returns:

tree

Return type:

updated tree

scHPL.utils.rename_node(old_name, new_name: str, tree: TreeNode)[source]

Rename a node in the tree manually.

Parameters:

old_name:

old name of the node (either a string of a list)

new_name: String

new_name of the node

tree: TreeNode

tree containing the node