High-level description
Thetree_utils.py file provides a collection of functions for building, manipulating, annotating, and visualizing phylogenetic trees. These functions are primarily used for analyzing and understanding the evolutionary relationships between sequences, particularly in the context of fitness prediction.
Code Structure
This file defines a set of independent utility functions that operate on phylogenetic trees represented using the Biopython library’sPhylo objects. Some functions utilize external tools like fasttree for tree building.
References
This code references the Biopython library (Bio) extensively for handling sequences, alignments, and phylogenetic trees. It also uses the matplotlib library for visualization purposes.
Symbols
calculate_tree
Description
Builds a phylogenetic tree from a given sequence alignment and outgroup sequence using thefasttree program. It infers ancestral sequences and performs basic tree manipulation like rooting and ladderizing.
Inputs
Outputs
Internal Logic
- Writes the alignment and outgroup sequence to a temporary FASTA file.
- Executes
fasttreewith the temporary file as input. - Parses the
fasttreeoutput into a BiopythonPhylo.Treeobject. - Roots the tree using the provided outgroup.
- Ladderizes the tree for visual clarity.
- If
ancestralis True, infers ancestral sequences for internal nodes. - Removes the temporary file.
branch_label
Description
Generates a label for a tree branch based on the mutations occurring along that branch.Inputs
Outputs
Internal Logic
- Determines whether to use amino acid (
node.aa_mutations) or nucleotide (node.mutations) mutations based on theaaflag. - If
display_positionsis provided, filters the mutations to include only those occurring at the specified positions. - Formats the selected mutations into a string representation.
collapse_zero_branches
Description
Collapses branches in the tree where the sequences at the parent and child nodes are identical. This simplifies the tree by merging nodes with no evolutionary changes.Inputs
Internal Logic
- Recursively traverses the tree.
- For each internal node, compares its sequence to its children’s sequences.
- If a child node has the same sequence as its parent, collapses the child into the parent.
annotate_leaf
Description
Adds annotations to a leaf node in the tree.Inputs
Internal Logic
Iterates through theannotation dictionary and sets the attributes of the leaf node using the provided key-value pairs.
translate_sequences_on_tree
Description
Translates nucleotide sequences to amino acid sequences for all nodes in the tree.Inputs
Internal Logic
- Iterates through all nodes (internal and terminal) in the tree.
- Extracts the nucleotide sequence corresponding to the coding region specified by
cds. - Translates the nucleotide sequence to an amino acid sequence, handling potential translation errors by inserting ‘X’ characters.
- Stores the translated amino acid sequence in the
aa_seqattribute of each node.
mutations_on_branches
Description
Identifies and annotates mutations occurring along each branch of the tree.Inputs
Internal Logic
- Initializes an empty list of mutations for the root node.
- Calls the recursive function
mutations_on_branches_subtreeto traverse the tree. - For each branch, compares the sequences of the parent and child nodes to identify mutations.
- Stores the identified mutations in the
mutations(andaa_mutationsifaais True) attribute of the child node.
mutations_on_branches_subtree
Description
Recursive helper function formutations_on_branches that traverses the tree and identifies mutations along each branch.
Inputs
Internal Logic
- Iterates through the child nodes of the current subtree.
- Compares the nucleotide sequences of the parent and child nodes to identify mutations.
- If
aais True, also compares the amino acid sequences. - Stores the identified mutations in the
mutationsandaa_mutationsattributes of the child node. - Recursively calls itself for each child node that is not a terminal node.
find_internal_nodes
Description
Finds corresponding internal nodes between two trees (sourceT and destT) based on the most recent common ancestor (MRCA) of their leaf nodes.
Inputs
Internal Logic
- Creates a lookup table for leaf nodes in
destT. - Calculates the path from the root to each leaf node in
sourceTwithin thedestTtree. - Iterates through internal nodes in
sourceT. - For each internal node, finds the MRCA of its leaf nodes in
destTusing the calculated paths. - Links the internal nodes in
sourceTanddestTby setting theirmirror_nodeattributes.
label_nodes
Description
Sets labels for specific nodes in the tree based on a provided dictionary.Inputs
Internal Logic
- Clears any existing labels for all nodes.
- Iterates through nodes and sets labels based on the provided
seqs_to_labeldictionary.
node_label_func
Description
A simple function that attempts to return thename attribute of a tree node. Used as a default node label function for tree plotting.
Inputs
Outputs
erase_color
Description
Resets the color of all nodes in the tree to None.Inputs
Internal Logic
Iterates through all nodes and sets thecolor attribute to None.
plot_prediction_tree
Description
Visualizes a phylogenetic tree with nodes colored according to a prediction.Inputs
Internal Logic
- Colors the tree using the provided
predictionobject and specifiedmethod. - Draws the tree using the
draw_treefunction.
draw_tree
Description
Draws a phylogenetic tree using matplotlib.Inputs
Internal Logic
- Creates a matplotlib figure and axes if not provided.
- Uses Biopython’s
Phylo.drawfunction to render the tree. - Adds a scale bar and colorbar if enabled.
plot_combined_tree
Description
Plots a combined tree with nodes colored based on predictions from a separate dataset.Inputs
Internal Logic
- Colors the combined tree using predictions from the
predictionobject. - Draws the tree using the
draw_treefunction.
