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()).

Required software


In [1]:
# conda install ipyrad -c bioconda
# conda install toytree -c eaton-lab
# conda install mrbayes -c bioconda -c conda-forge

In [2]:
import ipyrad.analysis as ipa
import toytree

In [3]:
# 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.


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

In [5]:
# init mrbayes object with input data and (optional) parameter options
mb = ipa.mrbayes(
    name="pedicularis-min10-5M",
    data=nexfile,
    clock=True, 
    ngen=5000000,
    samplefreq=5000,
    nruns=1,
)

In [6]:
# modify a param and show all params
mb.params.samplefreq = 10000
mb.params


Out[6]:
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)     

In [7]:
# print the mb nexus string; this can be modified by changing .params
print(mb.nexus_string)


#NEXUS
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;
mcmc;

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;
end;

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.


In [8]:
# 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/".


In [9]:
# you can access the tree results from the mb analysis object
mb.trees


Out[9]:
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

In [10]:
# for example to select the consensus tree path
mb.trees.constre


Out[10]:
'/home/deren/Documents/ipyrad/newdocs/API-analysis/analysis-mb/pedicularis-min10-5M.nex.con.tre'

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.


In [12]:
# show the convergence statistics (from the .pstat mb output file)
mb.convergence_stats.round(3)


Out[12]:
Mean Variance Lower Upper Median ESS
Parameter
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.


In [16]:
# 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
tre.draw(
    tip_labels_align=True, 
    scalebar=True,
    #node_labels="support",
);


39618_rex_SRR175472338362_rex_SRR175472535236_rex_SRR175473140578_rex_SRR175472435855_rex_SRR175472630556_thamno_SRR175472033413_thamno_SRR175472841954_cyathophylloides_SRR175472141478_cyathophylloides_SRR175472230686_cyathophylla_SRR175473029154_superba_SRR175471533588_przewalskii_SRR175472732082_przewalskii_SRR1754729061218

Plotting mb posterior distribution of trees


In [27]:
# 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
mtre.draw_tree_grid(
    nrows=1, ncols=4, start=20, shared_axis=True,
    height=400, 
);


38362_rex_SRR175472539618_rex_SRR175472335236_rex_SRR175473140578_rex_SRR175472435855_rex_SRR175472630556_thamno_SRR175472033413_thamno_SRR175472841478_cyathophylloides_SRR175472241954_cyathophylloides_SRR175472129154_superba_SRR175471530686_cyathophylla_SRR175473032082_przewalskii_SRR175472933588_przewalskii_SRR175472738362_rex_SRR175472539618_rex_SRR175472335236_rex_SRR175473135855_rex_SRR175472640578_rex_SRR175472430556_thamno_SRR175472033413_thamno_SRR175472829154_superba_SRR175471530686_cyathophylla_SRR175473041478_cyathophylloides_SRR175472241954_cyathophylloides_SRR175472133588_przewalskii_SRR175472732082_przewalskii_SRR175472938362_rex_SRR175472539618_rex_SRR175472335236_rex_SRR175473140578_rex_SRR175472435855_rex_SRR175472630556_thamno_SRR175472033413_thamno_SRR175472830686_cyathophylla_SRR175473029154_superba_SRR175471541954_cyathophylloides_SRR175472141478_cyathophylloides_SRR175472233588_przewalskii_SRR175472732082_przewalskii_SRR175472938362_rex_SRR175472539618_rex_SRR175472335236_rex_SRR175473135855_rex_SRR175472640578_rex_SRR175472430556_thamno_SRR175472033413_thamno_SRR175472829154_superba_SRR175471530686_cyathophylla_SRR175473041478_cyathophylloides_SRR175472241954_cyathophylloides_SRR175472133588_przewalskii_SRR175472732082_przewalskii_SRR17547290.000.020.03

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.


In [38]:
# set root height of all trees to 1.0
mtre.treelist = [i.mod.node_scale_root_height(1.0) for i in mtre.treelist]

In [39]:
# draw the posterior of trees overlapping
mtre.draw_cloud_tree(
    width=400, height=300,
    html=True,
    edge_colors="red",
    edge_style={"stroke-opacity": 0.02},
    tip_labels={i: i.rsplit("_", 1)[0] for i in mtre.treelist[0].get_tip_labels()},
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno29154_superba30686_cyathophylla41478_cyathophylloides41954_cyathophylloides33588_przewalskii32082_przewalskii

Save figures


In [14]:
import toyplot.pdf
canvas, axes = mtre.draw_cloud_tree(
    width=400, height=300,
    html=True,
    edge_colors="red",
    tip_labels={i: i.rsplit("_", 1)[0] for i in mtre.treelist[0].get_tip_labels()}
);
toyplot.pdf.render(canvas, "./pedicularis-min10-5M-mb-tree.pdf")

What else?

If you have reference mapped data then you should see the .treeslider() tool to infer trees in sliding windows along scaffolds; or the .extractor() tool to extract, filter, and concatenate RAD loci within a given window (e.g., near some known gene).