\n",
"\n",
"The program [TreeMix](https://bitbucket.org/nygcresearch/treemix/wiki/Home) by [Pickrell & Pritchard (2012)](http://journals.plos.org/plosgenetics/article?id=10.1371/journal.pgen.1002967) is used to infer population splits and admixture from allele frequency data. From the TreeMix documentation: \"In the underlying model, the modern-day populations in a species are related to a common ancestor via a graph of ancestral populations. We use the allele frequencies in the modern populations to infer the structure of this graph.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Required software"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# conda install treemix ipyrad ipcoal -c conda-forge -c bioconda"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import ipyrad.analysis as ipa\n",
"import toytree\n",
"import toyplot\n",
"import ipcoal"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ipyrad 0.9.54\n",
"toytree 2.0.1\n",
"TreeMix v. 1.12\r\n"
]
}
],
"source": [
"print('ipyrad', ipa.__version__)\n",
"print('toytree', toytree.__version__)\n",
"! treemix --version | grep 'TreeMix v. '"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simulate example data"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"canvas1, axes1 = tmx.draw_tree();\n",
"canvas2, axes2 = tmx.draw_cov();"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
"# save your plots\n",
"import toyplot.svg\n",
"toyplot.svg.render(canvas1, \"/tmp/treemix-m1.svg\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Finding the best value for `m`\n",
"\n",
"As with structure plots there is no True best value, but you can use model selection methods to decide whether one is a statistically better fit to your data than another. Adding additional admixture edges will always improve the likelihood score, but with diminishing returns as you add additional edges that explain little variation in the data. You can look at the log likelihood score of each model fit by running a for-loop like below. You may want to run this within another for-loop that iterates over different subsampled SNPs. "
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"tests = {}\n",
"nadmix = [0, 1, 2, 3, 4, 5]\n",
"\n",
"# iterate over n admixture edges and store results in a dictionary\n",
"for adm in nadmix:\n",
" tmx.params.m = adm\n",
" tmx.run()\n",
" tests[adm] = tmx.results.llik"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"