{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reconstructing ancestral states\n", "\n", "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.\n", "\n", "We first fit a model to the sample data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from cogent3.app import io, evo\n", "\n", "reader = io.load_aligned(format=\"fasta\")\n", "aln = reader(\"../data/primate_brca1.fasta\")\n", "gn = evo.model(\"GN\", tree=\"../data/primate_brca1.tree\")\n", "result = gn(aln)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the `ancestral_states` app" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5x tabular_result('edge.0': DictArray, 'edge.1': DictArray, 'edge.2': DictArray, 'edge.3': DictArray)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reconstuctor = evo.ancestral_states()\n", "states_result = reconstuctor(result)\n", "states_result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | T | \n", "C | \n", "A | \n", "G | \n", "\n", "\n", "
---|---|---|---|---|
0 | \n", "1.000 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "
1 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "
2 | \n", "1.000 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "
3 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "
4 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
2809 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "
2810 | \n", "1.000 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "
2811 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "
2812 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "0.000 | \n", "
2813 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "1.000 | \n", "