π¦ Coreο
Core data structures for phylogenetic modeling and simulation.
- class phyloom.core.MSA(sequences: Iterable[SequenceRecord])[source]ο
Multiple sequence alignment.
- Parameters:
sequences β Sequence records in alignment order.
Examples
Maintain a multiple sequence alignment of three sequences.
>>> from phyloom.core import MSA, SequenceRecord >>> sequences = [ ... SequenceRecord("human", "ACGT"), ... SequenceRecord("mouse", "A-GT"), ... SequenceRecord("avian", "AC-T"), ... ] >>> msa = MSA(sequences) >>> [sequence.name for sequence in msa] ['human', 'mouse', 'avian'] >>> msa.n_sequences, msa.n_sites, msa.shape, msa.n_unique_sequences (3, 4, (3, 4), 3) >>> len(msa) 3
Empty and ragged alignments are rejected.
>>> MSA([]) Traceback (most recent call last): ... ValueError: MSA must contain at least one sequence. >>> MSA([SequenceRecord("a", "AC"), SequenceRecord("b", "A")]) Traceback (most recent call last): ... ValueError: All sequences in the alignment must have the same length (got lengths: {1, 2}).
- property sequences: tuple[SequenceRecord, ...]ο
Sequence records in alignment order.
- dump(filepath: PathLike, header_from_sequence: Callable[[SequenceRecord], str] | None)[source]ο
Write the alignment to a FASTA file.
- Parameters:
filepath β Path to the output FASTA file.
header_from_sequence β Optional function to generate a header from a sequence. If not provided, the sequence name is used as the header.
- __iter__() Iterator[SequenceRecord][source]ο
Iterate over sequence records in alignment order.
- class phyloom.core.Edge(length: LengthT_co | None = None, **kwargs: Any)[source]ο
Bases:
MetadataMixin,Generic[LengthT_co]A directed edge in a phylogenetic tree or network.
- Parameters:
length β Optional edge length.
kwargs β Additional metadata associated with the edge.
- property length: LengthT_coο
Edge length.
- class phyloom.core.Net(nodes: Mapping[NodeID, Node], edges: Mapping[tuple[NodeID, NodeID], Edge[LengthT_co]], root: NodeID, stem: Edge[LengthT_co] | None = None)[source]ο
Bases:
Generic[LengthT_co]Phylogenetic network.
A
Netrepresents a rooted directed acyclic graph (DAG) in which every node is reachable from a distinguished root and edges are directed away from that root. Unlike a tree, a network may contain reticulation nodes with multiple parents while remaining acyclic. An optional stem edge can represent the branch leading into the network above the root.- Parameters:
nodes β Mapping from node ids to node records.
edges β Mapping from directed
(parent, child)node-id pairs to edge records.root β Root node id.
stem β Optional stem edge attached above the root.
Examples
Maintain a phylogenetic network with four nodes.
>>> from phyloom.core import Net, Node, Edge >>> names = ["root", "left", "right", "reticulate"] >>> nodes = {i: Node(name) for i, name in enumerate(names)} >>> links = [(0, 1), (0, 2), (1, 3), (2, 3)] >>> edges = {link: Edge() for link in links} >>> net = Net(nodes, edges, root=0) >>> net.root, net.stem (0, None) >>> net.n_nodes, net.n_edges (4, 4) >>> [nodes[node].name for node in net] ['root', 'right', 'left', 'reticulate'] >>> [nodes[parent].name for parent in net.parents_of(3)] ['left', 'right'] >>> net.children_of(0) (1, 2) >>> net.is_leaf(0), net.is_leaf(3) (False, True) >>> net.get_leaves(), net.count_leaves() ((3,), 1) >>> net.is_internal(0), net.is_internal(3) (True, False) >>> net.get_internals(), net.count_internals() ((0, 2, 1), 3) >>> net.get_node_id("reticulate"), net.get_node("reticulate") (3, Node(name='reticulate', metadata={})) >>> net.pretty_print() root |-- left | `-- reticulate `-- right `-- ref reticulate
Cycles are rejected.
>>> cyclic_edges = {**edges, (3, 1): Edge()} >>> Net(nodes, cyclic_edges, root=0) Traceback (most recent call last): ... ValueError: The network is not a DAG or is not fully connected from the root.
Every node must be reachable from the root.
>>> Net({**nodes, 4: Node("unreachable")}, edges, root=0) Traceback (most recent call last): ... ValueError: The network is not a DAG or is not fully connected from the root.
- class phyloom.core.Node(name: str = '', **kwargs: Any)[source]ο
Bases:
MetadataMixinA node in a phylogenetic tree or network.
- Parameters:
name β Node name.
kwargs β Additional metadata associated with the node.
- class phyloom.core.SequenceRecord(name: str, chars: str, **kwargs: Any)[source]ο
Bases:
MetadataMixinA sequence record in a multiple sequence alignment (MSA).
- Parameters:
name β Sequence name.
chars β Sequence characters. Must contain at least one character.
**kwargs β Additional metadata associated with the sequence.
- class phyloom.core.Tree(nodes: Mapping[NodeID, Node], edges: Mapping[tuple[NodeID, NodeID], Edge[LengthT_co]], root: NodeID, stem: Edge[LengthT_co] | None = None)[source]ο
Bases:
Net[LengthT_co]Phylogenetic tree.
A
Treerepresents a rooted directed tree in which every node is reachable from a distinguished root, and edges are directed away from that root. Unlike a network, a tree contains no reticulation nodes with multiple parents: every node except the root has exactly one parent. An optional stem edge can represent the branch leading into the tree above the root.- Parameters:
nodes β Mapping from node ids to node records.
edges β Mapping from directed
(parent, child)node-id pairs to edge records.root β Root node id.
stem β Optional stem edge attached above the root.
Examples
Maintain a phylogenetic tree with five nodes.
>>> from phyloom.core import Edge, Node, Tree >>> names = ["root", "left", "right", "left-left", "left-right"] >>> nodes = {i: Node(name) for i, name in enumerate(names)} >>> links = [(0, 1), (0, 2), (1, 3), (1, 4)] >>> lengths = [0.2, 0.3, 0.4, 0.5] >>> edges = {link: Edge(length) for link, length in zip(links, lengths)} >>> tree = Tree(nodes, edges, root=0, stem=Edge(length=0.1)) >>> tree.root 0 >>> tree.nodes[tree.parent_of(4)].name 'left' >>> tree.children_of(1) (3, 4) >>> tree.get_leaves(), tree.count_leaves() ((2, 4, 3), 3) >>> tree.get_internals(), tree.count_internals() ((0, 1), 2) >>> tree.edge_of(0).length, tree.edge_of(4).length (0.1, 0.5) >>> tree.is_binary() True >>> tree.get_mrca([3, 4]) 1 >>> tree.get_path(3, 2) [3, 1, 0, 2]
Reticulation nodes are rejected.
>>> nodes = {i: Node(str(i)) for i in range(4)} >>> links = [(0, 1), (0, 2), (1, 3), (2, 3)] >>> edges = {link: Edge() for link in links} >>> Tree(nodes, edges, root=0) Traceback (most recent call last): ... ValueError: The network is not a tree: some nodes have more than one parent.
- get_mrca(nodes: Iterable[NodeID]) NodeID[source]ο
Get the most recent common ancestor of two nodes.
- get_path(from_node: NodeID, to_node: NodeID) list[NodeID][source]ο
Get the path between two nodes, including both endpoints.
- ladderize()[source]ο
Sort each nodeβs children by the number of descendant leaves.
The tree is modified in place. Children with fewer descendant leaves are placed first.
Examples
>>> from phyloom.core import Edge, Node, Tree >>> nodes = {i: Node(str(i)) for i in range(5)} >>> links = [(0, 2), (0, 1), (2, 3), (2, 4)] >>> edges = {link: Edge() for link in links} >>> tree = Tree(nodes, edges, root=0) >>> tree.pretty_print() 0 |-- 2 | |-- 3 | `-- 4 `-- 1 >>> tree.ladderize() >>> tree.pretty_print() 0 |-- 1 `-- 2 |-- 3 `-- 4