{ "cells": [ { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "# Evolutionary Analysis Using Likelihood" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": false }, "source": [ "## Specifying substitution models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The available pre-defined substitution models\n", "\n", "In cases where code takes a substitution model as an argument, you can use the value under \"Abbreviation\" as a string." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Model Type | \n", "Abbreviation | \n", "Description | \n", "\n", "\n", "
---|---|---|
nucleotide | \n", "JC69 | \n", "Jukes and Cantor's 1969 model | \n", "
nucleotide | \n", "K80 | \n", "Kimura 1980 | \n", "
nucleotide | \n", "F81 | \n", "Felsenstein's 1981 model | \n", "
nucleotide | \n", "HKY85 | \n", "Hasegawa, Kishino and Yanamo 1985 model | \n", "
nucleotide | \n", "TN93 | \n", "Tamura and Nei 1993 model | \n", "
nucleotide | \n", "GTR | \n", "General Time Reversible nucleotide substitution model. | \n", "
nucleotide | \n", "ssGN | \n", "strand-symmetric general Markov nucleotide (non-stationary, non-reversible). Kaehler, 2017, Journal of Theoretical Biology 420: 144–51 | \n", "
nucleotide | \n", "GN | \n", "General Markov Nucleotide (non-stationary, non-reversible). Kaehler, Yap, Zhang, Huttley, 2015, Sys Biol 64 (2): 281–93 | \n", "
nucleotide | \n", "BH | \n", "Barry and Hartigan Discrete Time substitution model Barry and Hartigan 1987. Biometrics 43: 261–76. | \n", "
nucleotide | \n", "DT | \n", "Discrete Time substitution model (non-stationary, non-reversible). motif_length=2 makes this a dinucleotide model, motif_length=3 a trinucleotide model. | \n", "
codon | \n", "CNFGTR | \n", "Conditional nucleotide frequency codon substitution model, GTR variant (with params analagous to the nucleotide GTR model). Yap, Lindsay, Easteal and Huttley, 2010, Mol Biol Evol 27: 726-734 | \n", "
codon | \n", "CNFHKY | \n", "Conditional nucleotide frequency codon substitution model, HKY variant (with kappa, the ratio of transitions to transversions) Yap, Lindsay, Easteal and Huttley, 2010, Mol Biol Evol 27: 726-734 | \n", "
codon | \n", "MG94HKY | \n", "Muse and Gaut 1994 codon substitution model, HKY variant (with kappa, the ratio of transitions to transversions) Muse and Gaut, 1994, Mol Biol Evol, 11, 715-24 | \n", "
codon | \n", "MG94GTR | \n", "Muse and Gaut 1994 codon substitution model, GTR variant (with params analagous to the nucleotide GTR model) Muse and Gaut, 1994, Mol Biol Evol, 11, 715-24 | \n", "
codon | \n", "GY94 | \n", "Goldman and Yang 1994 codon substitution model. N Goldman and Z Yang, 1994, Mol Biol Evol, 11(5):725-36. | \n", "
codon | \n", "Y98 | \n", "Yang's 1998 substitution model, a derivative of the GY94. Z Yang, 1998, Mol Biol Evol, 15(5):568-73 | \n", "
codon | \n", "H04G | \n", "Huttley 2004 CpG substitution model. Includes a term for substitutions to or from CpG's. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8 | \n", "
codon | \n", "H04GK | \n", "Huttley 2004 CpG substitution model. Includes a term for transition substitutions to or from CpG's. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8 | \n", "
codon | \n", "H04GGK | \n", "Huttley 2004 CpG substitution model. Includes a general term for substitutions to or from CpG's and an adjustment for CpG transitions. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8 | \n", "
codon | \n", "GNC | \n", "General Nucleotide Codon, a non-reversible codon model. Kaehler, Yap, Huttley, 2017, Gen Biol Evol 9(1): 134–49 | \n", "
protein | \n", "DSO78 | \n", "Dayhoff et al 1978 empirical protein model Dayhoff, MO, Schwartz RM, and Orcutt, BC. 1978 A model of evolutionary change in proteins. Pp. 345-352. Atlas of protein sequence and structure, Vol 5, Suppl. 3. National Biomedical Research Foundation, Washington D. C Matrix imported from PAML dayhoff.dat file | \n", "
protein | \n", "AH96 | \n", "Adachi and Hasegawa 1996 empirical model for mitochondrial proteins. Adachi J, Hasegawa M. Model of amino acid substitution in proteins encoded by mitochondrial DNA. J Mol Evol. 1996 Apr;42(4):459-68. Matrix imported from PAML mtREV24.dat file | \n", "
protein | \n", "AH96_mtmammals | \n", "Adachi and Hasegawa 1996 empirical model for mammalian mitochondrial proteins. Adachi J, Hasegawa M. Model of amino acid substitution in proteins encoded by mitochondrial DNA. J Mol Evol. 1996 Apr;42(4):459-68. Matrix imported from PAML mtmam.dat file | \n", "
protein | \n", "JTT92 | \n", "Jones, Taylor and Thornton 1992 empirical protein model Jones DT, Taylor WR, Thornton JM. The rapid generation of mutation data matrices from protein sequences. Comput Appl Biosci. 1992 Jun;8(3):275-82. Matrix imported from PAML jones.dat file | \n", "
protein | \n", "WG01 | \n", "Whelan and Goldman 2001 empirical model for globular proteins. Whelan S, Goldman N. A general empirical model of protein evolution derived from multiple protein families using a maximum-likelihood approach. Mol Biol Evol. 2001 May;18(5):691-9. Matrix imported from PAML wag.dat file | \n", "
\n", "25 rows x 3 columns
" ], "text/plain": [ "Specify a model using 'Abbreviation' (case sensitive).\n", "================================================================================================================================================================================================================================================================================================================================================\n", "Model Type Abbreviation Description\n", "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "nucleotide JC69 Jukes and Cantor's 1969 model\n", "nucleotide K80 Kimura 1980\n", "nucleotide F81 Felsenstein's 1981 model\n", "nucleotide HKY85 Hasegawa, Kishino and Yanamo 1985 model\n", "nucleotide TN93 Tamura and Nei 1993 model\n", "nucleotide GTR General Time Reversible nucleotide substitution model.\n", "nucleotide ssGN strand-symmetric general Markov nucleotide (non-stationary, non-reversible). Kaehler, 2017, Journal of Theoretical Biology 420: 144–51\n", "nucleotide GN General Markov Nucleotide (non-stationary, non-reversible). Kaehler, Yap, Zhang, Huttley, 2015, Sys Biol 64 (2): 281–93\n", "nucleotide BH Barry and Hartigan Discrete Time substitution model Barry and Hartigan 1987. Biometrics 43: 261–76.\n", "nucleotide DT Discrete Time substitution model (non-stationary, non-reversible). motif_length=2 makes this a dinucleotide model, motif_length=3 a trinucleotide model.\n", " codon CNFGTR Conditional nucleotide frequency codon substitution model, GTR variant (with params analagous to the nucleotide GTR model). Yap, Lindsay, Easteal and Huttley, 2010, Mol Biol Evol 27: 726-734\n", " codon CNFHKY Conditional nucleotide frequency codon substitution model, HKY variant (with kappa, the ratio of transitions to transversions) Yap, Lindsay, Easteal and Huttley, 2010, Mol Biol Evol 27: 726-734\n", " codon MG94HKY Muse and Gaut 1994 codon substitution model, HKY variant (with kappa, the ratio of transitions to transversions) Muse and Gaut, 1994, Mol Biol Evol, 11, 715-24\n", " codon MG94GTR Muse and Gaut 1994 codon substitution model, GTR variant (with params analagous to the nucleotide GTR model) Muse and Gaut, 1994, Mol Biol Evol, 11, 715-24\n", " codon GY94 Goldman and Yang 1994 codon substitution model. N Goldman and Z Yang, 1994, Mol Biol Evol, 11(5):725-36.\n", " codon Y98 Yang's 1998 substitution model, a derivative of the GY94. Z Yang, 1998, Mol Biol Evol, 15(5):568-73\n", " codon H04G Huttley 2004 CpG substitution model. Includes a term for substitutions to or from CpG's. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8\n", " codon H04GK Huttley 2004 CpG substitution model. Includes a term for transition substitutions to or from CpG's. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8\n", " codon H04GGK Huttley 2004 CpG substitution model. Includes a general term for substitutions to or from CpG's and an adjustment for CpG transitions. GA Huttley, 2004, Mol Biol Evol, 21(9):1760-8\n", " codon GNC General Nucleotide Codon, a non-reversible codon model. Kaehler, Yap, Huttley, 2017, Gen Biol Evol 9(1): 134–49\n", " protein DSO78 Dayhoff et al 1978 empirical protein model Dayhoff, MO, Schwartz RM, and Orcutt, BC. 1978 A model of evolutionary change in proteins. Pp. 345-352. Atlas of protein sequence and structure, Vol 5, Suppl. 3. National Biomedical Research Foundation, Washington D. C Matrix imported from PAML dayhoff.dat file\n", " protein AH96 Adachi and Hasegawa 1996 empirical model for mitochondrial proteins. Adachi J, Hasegawa M. Model of amino acid substitution in proteins encoded by mitochondrial DNA. J Mol Evol. 1996 Apr;42(4):459-68. Matrix imported from PAML mtREV24.dat file\n", " protein AH96_mtmammals Adachi and Hasegawa 1996 empirical model for mammalian mitochondrial proteins. Adachi J, Hasegawa M. Model of amino acid substitution in proteins encoded by mitochondrial DNA. J Mol Evol. 1996 Apr;42(4):459-68. Matrix imported from PAML mtmam.dat file\n", " protein JTT92 Jones, Taylor and Thornton 1992 empirical protein model Jones DT, Taylor WR, Thornton JM. The rapid generation of mutation data matrices from protein sequences. Comput Appl Biosci. 1992 Jun;8(3):275-82. Matrix imported from PAML jones.dat file\n", " protein WG01 Whelan and Goldman 2001 empirical model for globular proteins. Whelan S, Goldman N. A general empirical model of protein evolution derived from multiple protein families using a maximum-likelihood approach. Mol Biol Evol. 2001 May;18(5):691-9. Matrix imported from PAML wag.dat file\n", "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "25 rows x 3 columns" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cogent3 import available_models\n", "\n", "available_models()" ] }, { "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "### Getting a substitution model with ``get_model()``" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "TimeReversibleNucleotide ( name = 'HKY85'; type = 'None'; params = ['kappa']; number of motifs = 4; motifs = ['T', 'C', 'A', 'G'])\n", "\n" ] } ], "source": [ "from cogent3.evolve.models import get_model\n", "\n", "hky = get_model(\"HKY85\")\n", "print(hky)" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true }, "source": [ "### Rate heterogeneity models\n", "\n", "We illustrate this for the gamma distributed case using examples of the canned models displayed above. Creating rate heterogeneity variants of the canned models can be done by using optional arguments that get passed to the substitution model class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For nucleotide\n", "\n", "We specify a general time reversible nucleotide model with gamma distributed rate heterogeneity." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "TimeReversibleNucleotide ( name = 'GTR'; type = 'None'; params = ['A/C', 'A/G', 'A/T', 'C/G', 'C/T']; number of motifs = 4; motifs = ['T', 'C', 'A', 'G'])\n", "\n" ] } ], "source": [ "from cogent3.evolve.models import get_model\n", "\n", "sub_mod = get_model(\"GTR\", with_rate=True, distribution='gamma')\n", "\n", "print(sub_mod)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For codon\n", "\n", "We specify a conditional nucleotide frequency codon model with nucleotide general time reversible parameters and a parameter for the ratio of nonsynonymous to synonymous substitutions (omega) with gamma distributed rate heterogeneity." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "TimeReversibleCodon ( name = 'CNFGTR'; type = 'None'; params = ['A/C', 'A/G', 'A/T', 'C/G', 'C/T', 'omega']; number of motifs = 61; motifs = ['TTT', 'TTC', 'TTA', 'TTG', 'TCT', 'TCC', 'TCA', 'TCG', 'TAT', 'TAC', 'TGT', 'TGC', 'TGG', 'CTT', 'CTC', 'CTA', 'CTG', 'CCT', 'CCC', 'CCA', 'CCG', 'CAT', 'CAC', 'CAA', 'CAG', 'CGT', 'CGC', 'CGA', 'CGG', 'ATT', 'ATC', 'ATA', 'ATG', 'ACT', 'ACC', 'ACA', 'ACG', 'AAT', 'AAC', 'AAA', 'AAG', 'AGT', 'AGC', 'AGA', 'AGG', 'GTT', 'GTC', 'GTA', 'GTG', 'GCT', 'GCC', 'GCA', 'GCG', 'GAT', 'GAC', 'GAA', 'GAG', 'GGT', 'GGC', 'GGA', 'GGG'])\n", "\n" ] } ], "source": [ "from cogent3.evolve.models import get_model\n", "\n", "sub_mod = get_model(\"CNFGTR\", with_rate=True, distribution='gamma')\n", "\n", "print(sub_mod)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For protein\n", "\n", "We specify a Jones, Taylor and Thornton 1992 empirical protein substitution model with gamma distributed rate heterogeneity." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Empirical ( name = 'JTT92'; type = 'None'; number of motifs = 20; motifs = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'])\n", "\n" ] } ], "source": [ "from cogent3.evolve.models import get_model\n", "\n", "sub_mod = get_model(\"JTT92\", with_rate=True, distribution='gamma')\n", "\n", "print(sub_mod)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making a likelihood function\n", "\n", "You start by specifying a substitution model and use that to construct a likelihood function for a specific tree." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from cogent3 import make_tree\n", "from cogent3.evolve.models import get_model\n", "\n", "sub_mod = get_model(\"F81\")\n", "tree = make_tree('(a,b,(c,d))')\n", "\n", "lf = sub_mod.make_likelihood_function(tree)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Providing an alignment to a likelihood function\n", "\n", "You need to load an alignment and then provide it a likelihood function. I construct very simple trees and alignments for this example." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from cogent3 import make_tree, make_aligned_seqs\n", "from cogent3.evolve.models import get_model\n", "sub_mod = get_model(\"F81\")\n", "tree = make_tree('(a,b,(c,d))')\n", "lf = sub_mod.make_likelihood_function(tree)\n", "aln = make_aligned_seqs([('a', 'ACGT'), ('b', 'AC-T'), ('c', 'ACGT'),\n", " ('d', 'AC-T')])\n", "lf.set_alignment(aln)" ] }, { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": true }, "source": [ "### Scoping parameters on trees -- time heterogeneous models\n", "\n", "For many evolutionary analyses, it's desirable to allow different branches on a tree to have different values of a parameter. We show this for a simple codon model case here where we want the great apes (the clade that includes human and orangutan) to have a different value of the ratio of nonsynonymous to synonymous substitutions. This parameter is identified in the precanned ``CNFGTR`` model as ``omega``." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /-Galago\n", " |\n", "-root----|--HowlerMon\n", " |\n", " | /-Rhesus\n", " \\edge.3--|\n", " | /-Orangutan\n", " \\edge.2--|\n", " | /-Gorilla\n", " \\edge.1--|\n", " | /-Human\n", " \\edge.0--|\n", " \\-Chimpanzee\n" ] } ], "source": [ "from cogent3 import load_tree\n", "from cogent3.evolve.models import get_model\n", "\n", "tree = load_tree('../data/primate_brca1.tree')\n", "\n", "print(tree.ascii_art())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "sm = get_model(\"CNFGTR\")\n", "lf = sm.make_likelihood_function(tree, digits=2)\n", "lf.set_param_rule('omega', tip_names=['Human', 'Orangutan'], outgroup_name='Galago', clade=True, init=0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We've set an *initial* value for this clade so that the edges affected by this rule are evident below." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "number of free parameters = 78
\n", "A/C | \n", "A/G | \n", "A/T | \n", "C/G | \n", "C/T | \n", "\n", "\n", "
---|---|---|---|---|
1.00 | \n", "1.00 | \n", "1.00 | \n", "1.00 | \n", "1.00 | \n", "
edge | \n", "parent | \n", "length | \n", "omega | \n", "\n", "\n", "
---|---|---|---|
Galago | \n", "root | \n", "1.00 | \n", "1.00 | \n", "
HowlerMon | \n", "root | \n", "1.00 | \n", "1.00 | \n", "
Rhesus | \n", "edge.3 | \n", "1.00 | \n", "1.00 | \n", "
Orangutan | \n", "edge.2 | \n", "1.00 | \n", "0.50 | \n", "
Gorilla | \n", "edge.1 | \n", "1.00 | \n", "0.50 | \n", "
Human | \n", "edge.0 | \n", "1.00 | \n", "0.50 | \n", "
Chimpanzee | \n", "edge.0 | \n", "1.00 | \n", "0.50 | \n", "
edge.0 | \n", "edge.1 | \n", "1.00 | \n", "0.50 | \n", "
edge.1 | \n", "edge.2 | \n", "1.00 | \n", "0.50 | \n", "
edge.2 | \n", "edge.3 | \n", "1.00 | \n", "1.00 | \n", "
edge.3 | \n", "root | \n", "1.00 | \n", "1.00 | \n", "
AAA | \n", "AAC | \n", "AAG | \n", "AAT | \n", "ACA | \n", "ACC | \n", "ACG | \n", "ACT | \n", "AGA | \n", "AGC | \n", "AGG | \n", "AGT | \n", "ATA | \n", "\n", "\n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "
ATC | \n", "ATG | \n", "ATT | \n", "CAA | \n", "CAC | \n", "CAG | \n", "CAT | \n", "CCA | \n", "CCC | \n", "CCG | \n", "CCT | \n", "CGA | \n", "CGC | \n", "\n", "\n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "
CGG | \n", "CGT | \n", "CTA | \n", "CTC | \n", "CTG | \n", "CTT | \n", "GAA | \n", "GAC | \n", "GAG | \n", "GAT | \n", "GCA | \n", "GCC | \n", "GCG | \n", "\n", "\n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "
GCT | \n", "GGA | \n", "GGC | \n", "GGG | \n", "GGT | \n", "GTA | \n", "GTC | \n", "GTG | \n", "GTT | \n", "TAC | \n", "TAT | \n", "TCA | \n", "TCC | \n", "\n", "\n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "
TCG | \n", "TCT | \n", "TGC | \n", "TGG | \n", "TGT | \n", "TTA | \n", "TTC | \n", "TTG | \n", "TTT | \n", "\n", "\n", "
---|---|---|---|---|---|---|---|---|
0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "0.02 | \n", "
log-likelihood = -6992.7690
\n", "number of free parameters = 16
\n", "A/C | \n", "A/G | \n", "A/T | \n", "C/G | \n", "C/T | \n", "\n", "\n", "
---|---|---|---|---|
1.2316 | \n", "5.2534 | \n", "0.9585 | \n", "2.3159 | \n", "5.9700 | \n", "
edge | \n", "parent | \n", "length | \n", "\n", "\n", "
---|---|---|
Galago | \n", "root | \n", "0.1731 | \n", "
HowlerMon | \n", "root | \n", "0.0449 | \n", "
Rhesus | \n", "edge.3 | \n", "0.0216 | \n", "
Orangutan | \n", "edge.2 | \n", "0.0077 | \n", "
Gorilla | \n", "edge.1 | \n", "0.0025 | \n", "
Human | \n", "edge.0 | \n", "0.0061 | \n", "
Chimpanzee | \n", "edge.0 | \n", "0.0028 | \n", "
edge.0 | \n", "edge.1 | \n", "0.0000 | \n", "
edge.1 | \n", "edge.2 | \n", "0.0034 | \n", "
edge.2 | \n", "edge.3 | \n", "0.0120 | \n", "
edge.3 | \n", "root | \n", "0.0076 | \n", "
A | \n", "C | \n", "G | \n", "T | \n", "\n", "\n", "
---|---|---|---|
0.3757 | \n", "0.1742 | \n", "0.2095 | \n", "0.2406 | \n", "
T | \n", "C | \n", "A | \n", "G | \n", "\n", "\n", "
---|---|---|---|
0.241 | \n", "0.174 | \n", "0.376 | \n", "0.209 | \n", "
A/C | \n", "A/G | \n", "A/T | \n", "C/G | \n", "C/T | \n", "\n", "\n", "
---|---|---|---|---|
1.2316 | \n", "5.2534 | \n", "0.9585 | \n", "2.3159 | \n", "5.9700 | \n", "
\n", "1 rows x 5 columns
" ], "text/plain": [ "global params\n", "==============================================\n", " A/C A/G A/T C/G C/T\n", "----------------------------------------------\n", "1.2316 5.2534 0.9585 2.3159 5.9700\n", "----------------------------------------------\n", "\n", "1 rows x 5 columns" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tables = lf.get_statistics(with_motif_probs=True, with_titles=True)\n", "tables[0] # just displaying the first" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Testing Hypotheses - Using Likelihood Ratio Tests\n", "\n", "We test the molecular clock hypothesis for human and chimpanzee lineages. The null has these two branches constrained to be equal." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "log-likelihood = -7177.4403
\n", "number of free parameters = 10
\n", "edge | \n", "parent | \n", "length | \n", "\n", "\n", "
---|---|---|
Galago | \n", "root | \n", "0.167 | \n", "
HowlerMon | \n", "root | \n", "0.044 | \n", "
Rhesus | \n", "edge.3 | \n", "0.021 | \n", "
Orangutan | \n", "edge.2 | \n", "0.008 | \n", "
Gorilla | \n", "edge.1 | \n", "0.002 | \n", "
Human | \n", "edge.0 | \n", "0.004 | \n", "
Chimpanzee | \n", "edge.0 | \n", "0.004 | \n", "
edge.0 | \n", "edge.1 | \n", "0.000 | \n", "
edge.1 | \n", "edge.2 | \n", "0.003 | \n", "
edge.2 | \n", "edge.3 | \n", "0.012 | \n", "
edge.3 | \n", "root | \n", "0.009 | \n", "
A | \n", "C | \n", "G | \n", "T | \n", "\n", "\n", "
---|---|---|---|
0.376 | \n", "0.174 | \n", "0.209 | \n", "0.241 | \n", "
log-likelihood = -7175.7756
\n", "number of free parameters = 11
\n", "edge | \n", "parent | \n", "length | \n", "\n", "\n", "
---|---|---|
Galago | \n", "root | \n", "0.167 | \n", "
HowlerMon | \n", "root | \n", "0.044 | \n", "
Rhesus | \n", "edge.3 | \n", "0.021 | \n", "
Orangutan | \n", "edge.2 | \n", "0.008 | \n", "
Gorilla | \n", "edge.1 | \n", "0.002 | \n", "
Human | \n", "edge.0 | \n", "0.006 | \n", "
Chimpanzee | \n", "edge.0 | \n", "0.003 | \n", "
edge.0 | \n", "edge.1 | \n", "0.000 | \n", "
edge.1 | \n", "edge.2 | \n", "0.003 | \n", "
edge.2 | \n", "edge.3 | \n", "0.012 | \n", "
edge.3 | \n", "root | \n", "0.009 | \n", "
A | \n", "C | \n", "G | \n", "T | \n", "\n", "\n", "
---|---|---|---|
0.376 | \n", "0.174 | \n", "0.209 | \n", "0.241 | \n", "
0 | |
Rhesus | ACATGGCAGCGTCGACAAGGATTCATTATAGTTCTTAACTTAGAGACATGAACAGAGATG |
Chimpanzee | ...................................A........................ |
Galago | ..CA....A........T.............G..C.T..C.....GAC....G....... |
Gorilla | ............................................................ |
HowlerMon | .........................................................T.. |
Human | .....................................................T...... |
Orangutan | ............................................................ |
7 x 60 dna alignment
\n", "" ], "text/plain": [ "7 x 60 dna alignment: Chimpanzee[ACATGGCAGCG...], Galago[ACCAGGCAACG...], Gorilla[ACATGGCAGCG...], ..." ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cogent3 import load_tree, load_aligned_seqs\n", "from cogent3.evolve.models import get_model\n", "\n", "tree = load_tree('../data/primate_brca1.tree')\n", "aln = load_aligned_seqs('../data/primate_brca1.fasta')\n", "\n", "sm = get_model(\"F81\")\n", "lf = sm.make_likelihood_function(tree, digits=3, space=2)\n", "lf.set_alignment(aln)\n", "lf.set_param_rule('length', tip_names=['Human', 'Chimpanzee'],\n", " outgroup_name='Galago', clade=True, is_independent=False)\n", "lf.set_name('Null Hypothesis')\n", "lf.optimise(local=True, show_progress=False)\n", "sim_aln = lf.simulate_alignment()\n", "sim_aln[:60]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Determining confidence intervals on MLEs\n", "\n", "The profile method is used to calculate a confidence interval for a named parameter. We show it here for a global substitution model exchangeability parameter (*kappa*, the ratio of transition to transversion rates) and for an edge specific parameter (just the human branch length)." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lo=3.78 ; mle=4.44 ; hi=5.22\n", "lo=0.00 ; mle=0.01 ; hi=0.01\n" ] } ], "source": [ "from cogent3 import load_tree, load_aligned_seqs\n", "from cogent3.evolve.models import get_model\n", "\n", "tree = load_tree('../data/primate_brca1.tree')\n", "aln = load_aligned_seqs('../data/primate_brca1.fasta')\n", "sm = get_model(\"HKY85\")\n", "lf = sm.make_likelihood_function(tree)\n", "lf.set_alignment(aln)\n", "lf.optimise(local=True, show_progress=False)\n", "kappa_lo, kappa_mle, kappa_hi = lf.get_param_interval('kappa')\n", "print(f\"lo={kappa_lo:.2f} ; mle={kappa_mle:.2f} ; hi={kappa_hi:.2f}\")\n", "human_lo, human_mle, human_hi = lf.get_param_interval('length', 'Human')\n", "print(f\"lo={human_lo:.2f} ; mle={human_mle:.2f} ; hi={human_hi:.2f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Saving results\n", "\n", "The best approach is to use the json string from the ``to_json()`` method. The saved data can be later reloaded using ``cogent3.util.deserialise.deserialise_object()``. The ``json`` data contains the alignment, tree topology, substitution model, parameter values, etc..\n", "\n", "To illustrate this, I create a very simple likelihood function. The ``json`` variable below is just a string that can be saved to disk." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"model\": {\"alphabet\": {\"motifset\": [\"T\", \"C\", \"A\", \"G\"], \"g'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cogent3 import load_tree, load_aligned_seqs\n", "from cogent3.evolve.models import get_model\n", "\n", "aln = make_aligned_seqs(data=dict(a=\"ACGG\", b=\"ATAG\", c=\"ATGG\"))\n", "tree = make_tree(tip_names=aln.names)\n", "sm = get_model(\"F81\")\n", "lf = sm.make_likelihood_function(tree)\n", "lf.set_alignment(aln)\n", "json = lf.to_json()\n", "json[:60] # just truncating the displayed string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We deserialise the object from the string." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "log-likelihood = -14.2727
\n", "number of free parameters = 3
\n", "edge | \n", "parent | \n", "length | \n", "\n", "\n", "
---|---|---|
a | \n", "root | \n", "1.0000 | \n", "
b | \n", "root | \n", "1.0000 | \n", "
c | \n", "root | \n", "1.0000 | \n", "
A | \n", "C | \n", "G | \n", "T | \n", "\n", "\n", "
---|---|---|---|
0.3333 | \n", "0.0833 | \n", "0.4167 | \n", "0.1667 | \n", "
0 | |
root | TGTGGCACAAATACTCATGCCAGCTCATTACAGCATGAGAACAGTTTGTTACTCACTAAA |
edge.0 | ...............................................A............ |
edge.1 | ...............................................A............ |
edge.2 | ...............................................A............ |
edge.3 | ............................................................ |
5 x 60 dna alignment
\n", "" ], "text/plain": [ "5 x 60 dna alignment: edge.0[TGTGGCACAAA...], edge.1[TGTGGCACAAA...], edge.2[TGTGGCACAAA...], ..." ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ancestors = lf.likely_ancestral_seqs()\n", "ancestors[:60]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or we can get the posterior probabilities (returned as a ``DictArray``) of sequence states at each node." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | T | \n", "C | \n", "A | \n", "G | \n", "\n", "\n", "
---|---|---|---|---|
0 | \n", "0.182 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "
1 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "0.156 | \n", "
2 | \n", "0.182 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "
3 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "0.156 | \n", "
4 | \n", "0.000 | \n", "0.000 | \n", "0.000 | \n", "0.156 | \n", "