Reconstructing ancestral states#

This app takes a model_result and returns a tabular_result consisting of the posterior probabilities of ancestral states for each node of a tree. These probabilities are computed using the marginal reconstruction algorithm.

We first fit a model to the sample data.

from cogent3 import get_app

loader = get_app("load_aligned", format="fasta")
aln = loader("data/primate_brca1.fasta")
gn = get_app("model", "GN", tree="data/primate_brca1.tree")
result = gn(aln)

Define the ancestral_states app#

reconstuctor = get_app("ancestral_states")
states_result = reconstuctor(result)
states_result
5x tabular_result('edge.0': DictArray, 'edge.1': DictArray, 'edge.2': DictArray, 'edge.3': DictArray, 'root': DictArray)

The tabular_result is keyed by the node name. Each value is a DictArray, with header corresponding to the states and rows corresponding to alignment position.

states_result["edge.0"]
TCAG
01.00000.00000.00000.0000
10.00000.00000.00001.0000
21.00000.00000.00000.0000
30.00000.00000.00001.0000
40.00000.00000.00001.0000
...............
28090.00000.00000.00001.0000
28101.00000.00000.00000.0000
28110.00000.00000.00001.0000
28120.00000.00001.00000.0000
28130.00000.00000.00001.0000

If not included in the newick tree file, the internal node names are automatically generated open loading. You can establish what those are by interrogating the tree bound to the likelihood function object. (If you move your mouse cursor over the nodes, their names will appear as hover text.)

result.tree.get_figure(contemporaneous=True).show(width=500, height=500)