πΎ Input/outputο
Input/output utilities for phylogenetic data.
π³ Newickο
Read and write networks in Newick format.
Newick is a compact, text-based format for representing evolutionary trees
and networks. Nested parentheses describe the branching structure, commas
separate sibling nodes, labels name nodes, and values following :
specify edge lengths. Each tree or network ends with a semicolon, and a
Newick file stores one per line. Labels and edge lengths may be omitted.
Metadata are encoded as bracketed comments of the form [&key=value,...].
Metadata immediately following a node label are stored on that node, while
metadata immediately following an edge length, or a bare :, are stored
on the corresponding edge.
Nodes with more than one parent are represented by labels prefixed with
#: every occurrence of the same # label refers to the same network
node rather than to a new one. When reading, metadata attached to any
occurrence are merged into that node, and conflicting values raise errors.
When writing, the nodeβs subtree and metadata are written at its first
occurrence only.
from_newick() and to_newick() convert between a single Newick
string and a Net or Tree,
while load_newick() and dump_newick() read and write Newick
files.
The supported Newick grammar is:
newick_file ::= newick_line+
newick_line ::= newick "\n"
newick ::= node ";"
node ::= children? reference? label metadata? edge?
children ::= "(" node ("," node)* ")"
reference ::= "#"
label ::= <possibly empty string without ":", "[", ",", ")" or ";">
edge ::= ":" edge_length? metadata?
edge_length ::= <float>
metadata ::= "[&" feature ("," feature)* "]"
feature ::= key "=" value
key ::= <string without ",", "=" or "]">
value ::= <string without "=" or "]">
- phyloom.io.newick.from_newick(newick: str, *, parse_as: Literal['tree']) Tree[float | None][source]ο
- phyloom.io.newick.from_newick(newick: str, *, parse_as: Literal['net'] = 'net') Net[float | None]
Parse a Newick string into a network or tree.
- Parameters:
newick β Newick string to parse.
parse_as β Whether to parse the Newick string as a tree or a network.
Examples
Parse a network with node metadata, edge metadata, and a repeated node reference.
>>> from phyloom.io import from_newick >>> newick = ( ... '((A[&host="human",date=2020.1]:0.1,' ... '#H1[&host="avian",date=2020.2]:0.2)' ... 'AB[&kind="clade"]:0.3[&support=0.95],' ... '(#H1[&status="resampled"]:[&route="reference"],' ... 'C[&host="swine",date=2020.3]:0.5)' ... 'BC[&kind="reticulate"]:[&support=0.85])' ... 'Root[&location="Zurich"]:0.0[&clock="days"];' ... ) >>> from_newick(newick).pretty_print() Root (location=Zurich) [0.0, clock=days] |-- AB (kind=clade) [0.3, support=0.95] | |-- A (host=human, date=2020.1) [0.1] | `-- H1 (host=avian, date=2020.2, status=resampled) [0.2] `-- BC (kind=reticulate) [support=0.85] |-- ref H1 [route=reference] `-- C (host=swine, date=2020.3) [0.5]
- phyloom.io.newick.load_newick(filepath: PathLike, *, load_as: Literal['tree']) Iterator[Tree[float | None]][source]ο
- phyloom.io.newick.load_newick(filepath: PathLike, *, load_as: Literal['net'] = 'net') Iterator[Net[float | None]]
Load networks or trees from a Newick file.
- Parameters:
filepath β Path to the Newick file.
load_as β Whether to load the Newick file as trees or networks.
- phyloom.io.newick.to_newick(net: Net[float | None]) str[source]ο
Serialize a network into a Newick string.
- Parameters:
net β Network to serialize.
Examples
Serialize a network with two inner nodes and a shared leaf.
>>> from phyloom.core import Edge, Net, Node >>> from phyloom.io import to_newick >>> nodes = { ... 0: Node("root"), ... 1: Node("left"), ... 2: Node("right"), ... 3: Node("H1", host="human"), ... } >>> edges = { ... (0, 1): Edge(length=0.1), ... (0, 2): Edge(length=0.2), ... (1, 3): Edge(), ... (2, 3): Edge(length=0.4, route="reference"), ... } >>> net = Net(nodes, edges, root=0, stem=Edge(length=0.0)) >>> to_newick(net) '((#H1[&host="human"])left:0.1,(#H1:0.4[&route="reference"])right:0.2)root:0.0;'
𧬠FASTAο
Read and write multiple sequence alignments (MSA) in FASTA format.
FASTA is a text-based format for storing biological sequences. Each sequence
record begins with a header line starting with >, followed by one or more
lines containing the sequence characters. When reading, the character lines
of a record are concatenated into a single sequence, leading and trailing
whitespace is stripped, and blank lines are ignored. When writing, each
sequence is written on a single line. Since the records form an alignment,
every sequence must contain at least one character and all sequences must
have the same length.
Headers often encode sequence metadata alongside the name, separated by
delimiters such as |; for example, name|date|country. By default,
the full header is used as the sequence name and no metadata are extracted.
load_fasta() accepts functions that extract the name and the metadata
of a SequenceRecord from its header, and
dump_fasta() accepts a function that builds the header from a record,
so that metadata can be preserved in both directions.
The supported FASTA grammar is:
fasta_file ::= sequence_record+
sequence_record ::= header_line chars_line+
header_line ::= ">" header "\n"
header ::= <string>
chars_line ::= chars "\n"
chars ::= <non-empty string not starting with ">">
- phyloom.io.fasta.load_fasta(filepath: PathLike, *, sequence_name_from_header: Callable[[str], str] | None = None, sequence_metadata_from_header: Callable[[str], Metadata] | None = None) MSA[source]ο
Load a multiple sequence alignment from a FASTA file.
- Parameters:
filepath β Path to the FASTA file.
sequence_name_from_header β Optional function that extracts a sequence name from a header. If not provided, the full header is used as the sequence name.
sequence_metadata_from_header β Optional function that extracts sequence metadata from a header. If not provided, no metadata is extracted.
- phyloom.io.fasta.dump_fasta(msa: MSA, filepath: PathLike, *, header_from_sequence: Callable[[SequenceRecord], str] | None = None)[source]ο
Write a multiple sequence alignment to a FASTA file.
- Parameters:
msa β Multiple sequence alignment to write.
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.