ipyrad-analysis toolkit: mrBayes

View as notebook

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.

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>`__).

Required software

# conda install ipyrad -c bioconda
# conda install toytree -c eaton-lab
# conda install mrbayes -c bioconda -c conda-forge
import ipyrad.analysis as ipa
import toytree
# print current versions
print("toytree", toytree.__version__)
! mb -v
toytree 1.0.0
MrBayes, Bayesian Analysis of Phylogeny

Version:   3.2.7a
Features:  SSE AVX FMA Beagle readline
Host type: x86_64-unknown-linux-gnu (CPU: x86_64)
Compiler:  gnu 7.3.0

Setting mb param settings

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.

# the path to your NEXUS formatted file
nexfile = "/home/deren/Documents/ipyrad/sandbox/pedicularis/analysis-ipyrad/min10_outfiles/min10.nex"

# init mrbayes object with input data and (optional) parameter options
mb = ipa.mrbayes(
# modify a param and show all params
mb.params.samplefreq = 10000
brlenspr       clock:birthdeath
clockratepr    lognorm(-7,0.6)
clockvarpr     tk02
extinctionpr   beta(2, 200)
nchains        4
ngen           5000000
nruns          1
samplefreq     10000
sampleprob     0.1
samplestrat    diversity
speciationpr   exp(10)
tk02varpr      exp(1.0)
treeagepr      offsetexp(1, 5)
# print the mb nexus string; this can be modified by changing .params
execute /home/deren/Documents/ipyrad/sandbox/pedicularis/analysis-ipyrad/min10_outfiles/min10.nex;

begin mrbayes;
set autoclose=yes nowarn=yes;

lset nst=6 rates=gamma;

prset clockratepr=lognorm(-7,0.6);
prset clockvarpr=tk02;
prset tk02varpr=exp(1.0);
prset brlenspr=clock:birthdeath;
prset samplestrat=diversity;
prset sampleprob=0.1;
prset speciationpr=exp(10);
prset extinctionpr=beta(2, 200);
prset treeagepr=offsetexp(1,5);

mcmcp ngen=5000000 nrun=1 nchains=4;
mcmcp relburnin=yes burninfrac=0.25;
mcmcp samplefreq=10000;
mcmcp printfreq=10000 diagnfr=5000;
mcmcp filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;

sump filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;
sumt filename=/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex;

Run mrbayes

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.

# run the command, (options: block until finishes; overwrite existing)
mb.run(block=True, force=True)
job pedicularis-min10-5M finished successfully

Accessing results

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/".

# you can access the tree results from the mb analysis object
constre   ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.con.tre
info      ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.lstat
posttre   ~/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.t
# for example to select the consensus tree path

Check the parameter values and convergence

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.

# show the convergence statistics (from the .pstat mb output file)
Mean Variance Lower Upper Median ESS
TH 0.055 0.000 0.045 0.067 0.054 4.014
TL 0.443 0.005 0.328 0.564 0.438 4.031
r(A<->C) 0.102 0.000 0.100 0.105 0.102 325.813
r(A<->G) 0.286 0.000 0.283 0.289 0.286 267.213
r(A<->T) 0.112 0.000 0.109 0.114 0.112 341.615
r(C<->G) 0.102 0.000 0.100 0.105 0.102 376.000
r(C<->T) 0.296 0.000 0.293 0.299 0.296 376.000
r(G<->T) 0.102 0.000 0.100 0.104 0.102 376.000
pi(A) 0.297 0.000 0.296 0.298 0.297 238.441
pi(C) 0.207 0.000 0.206 0.207 0.207 331.586
pi(G) 0.208 0.000 0.207 0.209 0.208 288.870
pi(T) 0.288 0.000 0.288 0.289 0.288 239.621
alpha 0.018 0.000 0.000 0.038 0.018 182.125
net_speciation 0.406 0.016 0.189 0.621 0.400 10.388
relative_extinction 0.010 0.000 0.000 0.024 0.009 367.141
tk02var 13.475 7.837 8.495 18.632 13.420 220.202
clockrate 0.003 0.000 0.002 0.005 0.003 14.291

Plotting mb consesus tree

You can plot either the consensus tree (.nex.cons.tre) or the posterior distribution of trees (.nex.t) produced by a mrbayes run by loading the single tree or list of trees with toytree as demonstrated below.

# load from the .trees attribute or from the saved tree file
tre = toytree.tree(mb.trees.constre, tree_format=10)

# draw the tree with styling

Plotting mb posterior distribution of trees

# load from the .trees attribute or from the saved tree file
mtre = toytree.mtree(mb.trees.posttre)

# remove the burnin manually
mtre.treelist = mtre.treelist[10:]

# draw a few trees on a shared axis
    nrows=1, ncols=4, start=20, shared_axis=True,

The command below plots a cloud tree diagram across the ~90 trees in the posterior distribution. Because we have no fossil calibrations for this data the root age is not of great interest so I use the .mod fuction below to scale the root height of each tree to 1.0. This will highlight variation among trees in relative node heights. Below you can see that in the posterior set of trees there some variation around node heights in the middle of the tree, and there does not appear to be any variation in the topology recovered, which is not uncommon for large concatenated data sets.

# set root height of all trees to 1.0
mtre.treelist = [i.mod.node_scale_root_height(1.0) for i in mtre.treelist]
# draw the posterior of trees overlapping
    width=400, height=300,
    edge_style={"stroke-opacity": 0.02},
    tip_labels={i: i.rsplit("_", 1)[0] for i in mtre.treelist[0].get_tip_labels()},