High-level description
This file provides functions for calculating tree metrics, specifically parsimony and likelihood, for a givenCassiopeiaTree. It includes functions for calculating the number of mutations on a tree, the log likelihood of a character on a tree, and wrapper functions for calculating the overall likelihood of a tree under both discrete and continuous models of lineage tracing.
References
This code references theCassiopeiaTree class from cassiopeia.data and the parameter_estimators module from cassiopeia.tools.
Symbols
calculate_parsimony
Description
Calculates the parsimony score of a tree, defined as the number of character/state mutations that occur on the edges of the tree.Inputs
Outputs
Internal Logic
- If
infer_ancestral_charactersis True, the ancestral character states of the tree are inferred using Camin-Sokal Parsimony. - The function iterates over all edges in the tree.
- For each edge, it retrieves the character states of the parent and child nodes.
- It then calculates the number of mutations along the edge using the
get_mutations_along_edgemethod of theCassiopeiaTreeobject. - The total number of mutations is summed over all edges and returned.
log_transition_probability
Description
Calculates the log transition probability between two given states for a specific character.Inputs
Outputs
Internal Logic
The function calculates the log transition probability based on the following assumptions:- State
0represents the uncut state. - Only state
0can transition to non-zero states. - Any non-missing state can mutate to the missing state, specified by
tree.missing_state_indicator. - The probability of acquiring a mutation is given by the time
tand themutation_probability_function_of_time. - The probability of acquiring heritable missing data is given by the time
tand themissing_probability_function_of_time. - The priors on the tree (
tree.priors) are used to determine the probability of acquiring a non-missing, non-zero state.
s and s_ and returns the corresponding log transition probability.
log_likelihood_of_character
Description
Calculates the log likelihood of a given character on the tree using Felsenstein’s Pruning Algorithm.Inputs
Outputs
Internal Logic
- The function performs a depth-first traversal of the tree in postorder.
- For each node, it calculates the likelihood of each possible state at that node based on the likelihoods of its children and the transition probabilities between states.
- If
use_internal_character_statesis False, the function assumes an implicit root with the uncut state (0) at each character and marginalizes over the transition from the implicit root to its child. - If
use_internal_character_statesis True, the function returns the likelihood of the state annotated at the root for the given character.
get_lineage_tracing_parameters
Description
Retrieves or estimates the lineage tracing parameters (mutation rate, heritable missing rate, and stochastic missing probability) from aCassiopeiaTree.
Inputs
Outputs
Internal Logic
- The function attempts to retrieve the parameters from
tree.parameters. - If any of the parameters are not found, they are estimated using the
estimate_mutation_rateandestimate_missing_data_ratesfunctions. - The function checks if the estimated parameters have valid values and raises a
TreeMetricErrorif they are invalid.
calculate_likelihood_discrete
Description
Calculates the log likelihood of a tree under a discrete model of lineage tracing.Inputs
Outputs
Internal Logic
- The function retrieves the lineage tracing parameters using
get_lineage_tracing_parameters. - It calculates the log likelihood for each character using
log_likelihood_of_characterwith discrete rate functions. - The log likelihoods of all characters are summed to obtain the overall log likelihood of the tree.
calculate_likelihood_continuous
Description
Calculates the log likelihood of a tree under a continuous model of lineage tracing.Inputs
Outputs
Internal Logic
- The function retrieves the lineage tracing parameters using
get_lineage_tracing_parameters. - It calculates the log likelihood for each character using
log_likelihood_of_characterwith continuous rate functions. - The log likelihoods of all characters are summed to obtain the overall log likelihood of the tree.
Error Handling
The functions in this file raiseTreeMetricError exceptions if the input tree is not initialized or if the character states are not properly initialized. They also raise TreeMetricError exceptions if the lineage tracing parameters have invalid values.