historydag.dag_node

Functions

empty_node(label, clades[, attr])

Return a HistoryDagNode with the given label and clades, with no children.

Classes

EdgeSet(*args[, weights, probs])

A container class for edge target nodes, and associated probabilities and weights.

HistoryDagNode(label, clades, attr)

A recursive representation of a history DAG object.

UANode(targetnodes)

A universal ancestor node, the root node of a HistoryDag.

class historydag.dag_node.HistoryDagNode(label, clades, attr)[source]

A recursive representation of a history DAG object.

  • a dictionary keyed by clades (frozensets) containing EdgeSet objects

  • a label, which is a namedtuple.

__init__(label, clades, attr)[source]
empty_copy()[source]

Returns a HistoryDagNode object with the same clades, label, and attr dictionary, but no descendant edges.

Return type:

HistoryDagNode

node_self()[source]

Deprecated name for empty_copy()

Return type:

HistoryDagNode

clade_union()[source]

Returns the union of this node’s child clades (or a set containing only the node label, for leaf nodes.)

Return type:

FrozenSet[Union[NamedTuple, UALabel]]

under_clade()[source]

Deprecated name for clade_union()

Return type:

FrozenSet[Union[NamedTuple, UALabel]]

is_leaf()[source]

Returns whether this is a leaf node.

Return type:

bool

is_ua_node()[source]

Returns whether this is the source node in the DAG, from which all others are reachable.

Return type:

bool

is_root()[source]

Deprecated name for is_ua_node()

Return type:

bool

is_history_root()[source]

Return whether node is a root of any histories in the DAG.

Return type:

bool

child_clades()[source]

Returns the node’s child clades, or a frozenset containing a frozenset if this node is a UANode.

Return type:

frozenset

partitions()[source]

Deprecated name for child_clades()

Return type:

frozenset

sorted_child_clades()[source]

Returns the node’s child clades as a sorted tuple containing leaf labels in sorted tuples.

Return type:

tuple

sorted_partitions()[source]

Deprecated name for sorted_child_clades()

Return type:

tuple

children(clade=None)[source]

Returns generator object containing child nodes.

Parameters:

clade (Optional[Set[Union[NamedTuple, UALabel]]]) – If clade is provided, returns generator object of edge targets from that

Return type:

Generator[HistoryDagNode, None, None]

clade. If no clade is provided, generator includes all children of self.

add_edge(target, weight=0, prob=None, prob_norm=True)[source]

Adds edge, if allowed and not already present.

Returns whether edge was added.

Return type:

bool

remove_node(nodedict={})[source]

Recursively removes node self and any orphaned children from dag.

May not work on root. Does not check to make sure that parent clade still has descendant edges.

to_ascii(name_func, show_internal=False, compact=False, sort_method=None)[source]

A convenience function that uses the to_ete() method and ete3’s ASCII drawing tools to render a sub-history below self.

Expects that this node is part of a tree-shaped history DAG. If this is not true, this method may fail silently.

Parameters:
  • name_func – A function taking a HistoryDagNode and returning a string to identify that node in the ascii tree.

  • show_internal – Whether to show internal node names.

  • compact – Whether to show the tree in a more compact format

  • sort_method – Either None, ladderize, leaf-name, or child-name. leaf-name sorts children by the alphabetically first leaf name below each child node. child-name sorts directly by child name.

Returns:

A string including whitespace and newlines (no tabs) which when printed shows the structure of the history.

to_ete(name_func=lambda n: ..., feature_funcs={}, sort_func=lambda seq: ...)[source]

Convert a history DAG node which is part of a history to an ete tree.

Parameters:
Return type:

TreeNode

Returns:

An ete3 Tree with the same topology as the subhistory below self, and node names and attributes as specified.

class historydag.dag_node.UANode(targetnodes)[source]

A universal ancestor node, the root node of a HistoryDag.

__init__(targetnodes)[source]
empty_copy()[source]

Returns a UANode object with the same clades and label, but no descendant edges.

Return type:

UANode

is_ua_node()[source]

Returns whether this is the source node in the DAG, from which all others are reachable.

Return type:

bool

class historydag.dag_node.EdgeSet(*args, weights=None, probs=None)[source]

A container class for edge target nodes, and associated probabilities and weights.

Goal: associate targets (edges) with arbitrary parameters, but support set-like operations like lookup and enforce that elements are unique.

Takes no arguments, or an ordered iterable containing target nodes.

__init__(*args, weights=None, probs=None)[source]

Takes no arguments, or an ordered iterable containing target nodes.

set_targets(targets, weights=None, probs=None)[source]

Set the target nodes of this node.

If no weights or probabilities are provided, then these will be set to 0 and 1/n, respectively.

set_edge_stats(weights=None, probs=None)[source]

Set the edge weights and/or probabilities of this EdgeSet.

shallowcopy()[source]

Return an identical EdgeSet object, which points to the same target nodes.

Return type:

EdgeSet

sample(mask=None, log_probabilities=False)[source]

Returns a randomly sampled child edge, and its corresponding weight.

When possible, only edges with nonzero mask value will be sampled.

Return type:

Tuple[HistoryDagNode, float]

add_to_edgeset(target, weight=0, prob=None, prob_norm=True)[source]

Add a target node to the EdgeSet.

currently does nothing if edge is already present. Also does nothing if the target node has one child clade, and parent node is not the DAG root.

Parameters:
  • target – target node

  • weight – edge weight

  • prob – edge probability. If not provided, edge probability will be 1 / n where n is the number of edges in the edgeset.

  • prob_norm – if True, probability vector will be normalized.

Return type:

bool

Returns:

Whether an edge was added

historydag.dag_node.empty_node(label, clades, attr=None)[source]

Return a HistoryDagNode with the given label and clades, with no children.

Return type:

HistoryDagNode