{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ipyrad-analysis toolkit: mrBayes\n", "\n", "[View as notebook](https://nbviewer.jupyter.org/github/dereneaton/ipyrad/blob/master/newdocs/API-analysis/cookbook-mrbayes.ipynb)\n", "\n", "Bayesian phylogenetic inference can provide several advantages over ML approaches, particularly with regards to inferring dated trees (separating rate and time), and because they assess support differently, using a posterior sample of trees inferred from the full data set as opposed to bootstrap resampling of the data set. This may be particularly relevant to RAD-seq data that contains missing data. \n", "\n", "Bayesian inference programs often contain waaay too many options, which make them difficult to use. But it's kind of necessary in order to make informed decisions when setting priors on parameters, as opposed to treating the analysis like a black box. That being said, I've written a sort of blackbox tool for running mrbayes analyses. Once you've established a set of parameters that are best for your analysis this tool is useful to then automate it across many loci (e.g., see [`ipa.treeslider()`](https://ipyrad.readthedocs.io/en/latest/API-analysis/cookbook-treeslider.html)). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Required software" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# conda install ipyrad -c bioconda\n", "# conda install toytree -c eaton-lab\n", "# conda install mrbayes -c bioconda -c conda-forge" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import ipyrad.analysis as ipa\n", "import toytree" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "toytree 1.0.0\n", "MrBayes, Bayesian Analysis of Phylogeny\r\n", "\r\n", "Version: 3.2.7a\r\n", "Features: SSE AVX FMA Beagle readline\r\n", "Host type: x86_64-unknown-linux-gnu (CPU: x86_64)\r\n", "Compiler: gnu 7.3.0\r\n" ] } ], "source": [ "# print current versions\n", "print(\"toytree\", toytree.__version__)\n", "! mb -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting mb param settings\n", "\n", "The purpose of the ipyrad wrapper tool for mrbayes is merely for convenience. You should still take care to understand the priors and parameters that you are selecting by reading the mrbayes documentation. We only support two relatively simple models, a clock and non-clock model with a number of parameters listed in the `.params` attribute of the analysis object. Here I demonstrate the clock model with uncorrelated lognormal distributed rate variation and a birth-death tree prior. This run, which samples 5M iterations takes about 20 hours on 4 cores. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# the path to your NEXUS formatted file\n", "nexfile = \"/home/deren/Documents/ipyrad/sandbox/pedicularis/analysis-ipyrad/min10_outfiles/min10.nex\"\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# init mrbayes object with input data and (optional) parameter options\n", "mb = ipa.mrbayes(\n", " name=\"pedicularis-min10-5M\",\n", " data=nexfile,\n", " clock=True, \n", " ngen=5000000,\n", " samplefreq=5000,\n", " nruns=1,\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "brlenspr clock:birthdeath \n", "clockratepr lognorm(-7,0.6) \n", "clockvarpr tk02 \n", "extinctionpr beta(2, 200) \n", "nchains 4 \n", "ngen 5000000 \n", "nruns 1 \n", "samplefreq 10000 \n", "sampleprob 0.1 \n", "samplestrat diversity \n", "speciationpr exp(10) \n", "tk02varpr exp(1.0) \n", "treeagepr offsetexp(1, 5) " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# modify a param and show all params\n", "mb.params.samplefreq = 10000\n", "mb.params" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#NEXUS\n", "execute /home/deren/Documents/ipyrad/sandbox/pedicularis/analysis-ipyrad/min10_outfiles/min10.nex;\n", "\n", "begin mrbayes;\n", "set autoclose=yes nowarn=yes;\n", "\n", "lset nst=6 rates=gamma;\n", "\n", "prset clockratepr=lognorm(-7,0.6);\n", "prset clockvarpr=tk02;\n", "prset tk02varpr=exp(1.0);\n", "prset brlenspr=clock:birthdeath;\n", "prset samplestrat=diversity;\n", "prset sampleprob=0.1;\n", "prset speciationpr=exp(10);\n", "prset extinctionpr=beta(2, 200);\n", "prset treeagepr=offsetexp(1,5);\n", "\n", "mcmcp ngen=5000000 nrun=1 nchains=4;\n", "mcmcp relburnin=yes burninfrac=0.25;\n", "mcmcp samplefreq=10000;\n", "mcmcp printfreq=10000 diagnfr=5000;\n", "mcmcp filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;\n", "mcmc;\n", "\n", "sump filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;\n", "sumt filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;\n", "end;\n", "\n" ] } ], "source": [ "# print the mb nexus string; this can be modified by changing .params\n", "print(mb.nexus_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run mrbayes\n", "\n", "This takes about 20 minutes to run on my laptop for a data set with 13 samples and ~1.2M SNPs and about 14% missing data. For a publication quality analyses you will likely want to run it quite a bit longer and to test for convergence of your runs using a tool like `tracer` and `treeannotator`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "job pedicularis-min10-5M finished successfully\n" ] } ], "source": [ "# run the command, (options: block until finishes; overwrite existing)\n", "mb.run(block=True, force=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing results\n", "\n", "You can access the results files of the mrbayes analysis from the mb analysis object from the `.trees` attribute, or of course you can also write out the full path to the result files which will be located in your working directory which can be set as a parameter option, otherwise is defaulted to `\"./analysis-mb/\".`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "constre ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.con.tre\n", "info ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.lstat\n", "posttre ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.t" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# you can access the tree results from the mb analysis object\n", "mb.trees" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.con.tre'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# for example to select the consensus tree path\n", "mb.trees.constre" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check the parameter values and convergence\n", "\n", "On this very large data set (in terms of number of sites) we can see that the posterior has not explored the parameter space very well by the end of this run. We would hope to see the ESS score for all parameters above 100. Here the TH, TL, net_speciation, and clockrate parameters have low convergence scores. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Mean | \n", "Variance | \n", "Lower | \n", "Upper | \n", "Median | \n", "ESS | \n", "
---|---|---|---|---|---|---|
Parameter | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
TH | \n", "0.055 | \n", "0.000 | \n", "0.045 | \n", "0.067 | \n", "0.054 | \n", "4.014 | \n", "
TL | \n", "0.443 | \n", "0.005 | \n", "0.328 | \n", "0.564 | \n", "0.438 | \n", "4.031 | \n", "
r(A<->C) | \n", "0.102 | \n", "0.000 | \n", "0.100 | \n", "0.105 | \n", "0.102 | \n", "325.813 | \n", "
r(A<->G) | \n", "0.286 | \n", "0.000 | \n", "0.283 | \n", "0.289 | \n", "0.286 | \n", "267.213 | \n", "
r(A<->T) | \n", "0.112 | \n", "0.000 | \n", "0.109 | \n", "0.114 | \n", "0.112 | \n", "341.615 | \n", "
r(C<->G) | \n", "0.102 | \n", "0.000 | \n", "0.100 | \n", "0.105 | \n", "0.102 | \n", "376.000 | \n", "
r(C<->T) | \n", "0.296 | \n", "0.000 | \n", "0.293 | \n", "0.299 | \n", "0.296 | \n", "376.000 | \n", "
r(G<->T) | \n", "0.102 | \n", "0.000 | \n", "0.100 | \n", "0.104 | \n", "0.102 | \n", "376.000 | \n", "
pi(A) | \n", "0.297 | \n", "0.000 | \n", "0.296 | \n", "0.298 | \n", "0.297 | \n", "238.441 | \n", "
pi(C) | \n", "0.207 | \n", "0.000 | \n", "0.206 | \n", "0.207 | \n", "0.207 | \n", "331.586 | \n", "
pi(G) | \n", "0.208 | \n", "0.000 | \n", "0.207 | \n", "0.209 | \n", "0.208 | \n", "288.870 | \n", "
pi(T) | \n", "0.288 | \n", "0.000 | \n", "0.288 | \n", "0.289 | \n", "0.288 | \n", "239.621 | \n", "
alpha | \n", "0.018 | \n", "0.000 | \n", "0.000 | \n", "0.038 | \n", "0.018 | \n", "182.125 | \n", "
net_speciation | \n", "0.406 | \n", "0.016 | \n", "0.189 | \n", "0.621 | \n", "0.400 | \n", "10.388 | \n", "
relative_extinction | \n", "0.010 | \n", "0.000 | \n", "0.000 | \n", "0.024 | \n", "0.009 | \n", "367.141 | \n", "
tk02var | \n", "13.475 | \n", "7.837 | \n", "8.495 | \n", "18.632 | \n", "13.420 | \n", "220.202 | \n", "
clockrate | \n", "0.003 | \n", "0.000 | \n", "0.002 | \n", "0.005 | \n", "0.003 | \n", "14.291 | \n", "