In these analyses our interest is primarily in inferring accurate branch lengths under a relaxed molecular clock model. This means that tips are forced to line up at the present (time) but that rates of substitutions are allowed to vary among branches to best explain the variation in the sequence data.
There is a huge range of models that can be employed using mrbayes by employing different combinations of parameter settings, model definitions, and prior settings. The ipyrad-analysis tool here is intended to make it easy to run such jobs many times (e.g., distributed in parallel) once you have decided on your settings. In addition, we provide a number of pre-set models (e.g., clock_model=2) that may be useful for simple scenarios.
Here we use simulations to demonstrate the accuracy of branch length estimation when sequences come from a single versus multiple distinct genealogies (e.g., gene tree vs species tree), and show an option to fix the topology to speed up analyses when your only interest is to estimate branch lengths.
# conda install ipyrad -c conda-forge -c bioconda
# conda install mrbayes -c conda-forge -c bioconda
# conda install ipcoal -c conda-forge
import toytree
import ipcoal
import ipyrad.analysis as ipa
TREE = toytree.rtree.bdtree(ntips=8, b=0.8, d=0.2, seed=123)
TREE = TREE.mod.node_scale_root_height(1e6)
TREE.draw(ts='o', layout='d', scalebar=True);
When Ne is greater the gene tree is more likely to deviate from the species tree topology and branch lengths. By setting recombination rate to 0 there will only be one true underlying genealogy for the gene tree. We set nsamples=2 because we want to simulate diploid individuals.
# init simulator
model = ipcoal.Model(TREE, Ne=2e4, nsamples=2, recomb=0)
# simulate sequence data on coalescent genealogies
model.sim_loci(nloci=1, nsites=20000)
# write results to database file
model.write_concat_to_nexus(name="mbtest-1", outdir='/tmp', diploid=True)
wrote concat locus (8 x 20000bp) to /tmp/mbtest-1.nex
# the simulated genealogy of haploid alleles
gene = model.df.genealogy[0]
# draw the genealogy
toytree.tree(gene).draw(ts='o', layout='d', scalebar=True);
This shows the 2 haploid samples simulated for each tip in the species tree.
model.draw_seqview(idx=0, start=0, end=50);
# init the mb object
mb = ipa.mrbayes(
data="/tmp/mbtest-1.nex",
name="itest-1",
workdir="/tmp",
clock_model=2,
constraints=TREE,
ngen=int(1e6),
nruns=2,
)
# modify a parameter
mb.params.clockratepr = "normal(0.01,0.005)"
mb.params.samplefreq = 5000
# summary of priors/params
print(mb.params)
brlenspr clock:uniform clockratepr normal(0.01,0.005) clockvarpr igr igrvarpr exp(10.0) nchains 4 ngen 1000000 nruns 2 samplefreq 5000 topologypr fixed(fixedtree)
# start the run
mb.run(force=True)
job itest-1 finished successfully
# load the inferred tree
mbtre = toytree.tree("/tmp/itest-1.nex.con.tre", 10)
# scale root node to 1e6
mbtre = mbtre.mod.node_scale_root_height(1e6)
# draw inferred tree
c, a, m = mbtre.draw(ts='o', layout='d', scalebar=True);
# draw TRUE tree in orange on the same axes
TREE.draw(
axes=a,
ts='o',
layout='d',
scalebar=True,
edge_colors="darkorange",
node_sizes=0,
fixed_order=mbtre.get_tip_labels(),
);
# check convergence statistics
mb.convergence_stats
Mean | Variance | Lower | Upper | Median | minESS | avgESS | PSRF | |
---|---|---|---|---|---|---|---|---|
Parameter | ||||||||
TH | 1.057e-02 | 1.270e-06 | 8.548e-03 | 1.280e-02 | 1.058e-02 | 96.759 | 123.880 | 0.997 |
TL | 3.975e-02 | 1.433e-05 | 3.405e-02 | 4.663e-02 | 3.930e-02 | 93.967 | 122.484 | 0.997 |
r(A<->C) | 1.711e-01 | 1.907e-04 | 1.438e-01 | 1.966e-01 | 1.694e-01 | 46.516 | 98.758 | 0.998 |
r(A<->G) | 1.618e-01 | 1.587e-04 | 1.335e-01 | 1.835e-01 | 1.633e-01 | 82.391 | 116.695 | 0.999 |
r(A<->T) | 1.646e-01 | 1.955e-04 | 1.436e-01 | 1.969e-01 | 1.626e-01 | 21.969 | 86.484 | 1.022 |
r(C<->G) | 1.522e-01 | 1.635e-04 | 1.286e-01 | 1.748e-01 | 1.528e-01 | 7.775 | 79.388 | 1.012 |
r(C<->T) | 1.920e-01 | 1.813e-04 | 1.660e-01 | 2.139e-01 | 1.928e-01 | 21.517 | 73.623 | 1.023 |
r(G<->T) | 1.582e-01 | 1.961e-04 | 1.355e-01 | 1.874e-01 | 1.568e-01 | 23.112 | 87.056 | 1.012 |
pi(A) | 2.473e-01 | 6.721e-06 | 2.422e-01 | 2.526e-01 | 2.477e-01 | 137.517 | 144.259 | 0.998 |
pi(C) | 2.490e-01 | 9.690e-06 | 2.448e-01 | 2.545e-01 | 2.491e-01 | 8.147 | 79.573 | 1.083 |
pi(G) | 2.538e-01 | 1.512e-05 | 2.473e-01 | 2.600e-01 | 2.536e-01 | 5.873 | 47.914 | 1.175 |
pi(T) | 2.499e-01 | 8.684e-06 | 2.456e-01 | 2.562e-01 | 2.495e-01 | 16.049 | 54.003 | 1.030 |
alpha | 1.736e+00 | 9.001e-01 | 3.765e-01 | 3.552e+00 | 1.649e+00 | 68.018 | 109.509 | 0.997 |
igrvar | 2.744e-04 | 1.582e-07 | 1.800e-06 | 8.916e-04 | 1.563e-04 | 85.329 | 98.034 | 0.997 |
clockrate | 1.036e-02 | 2.030e-05 | 3.860e-03 | 1.825e-02 | 9.978e-03 | 8.984 | 28.529 | 1.056 |
Here we use concatenated sequence data from 100 loci where each represents one or more distinct genealogies. In addition, Ne is increased to 1e5, allowing for more genealogical variation. We expect the accuracy of estimated edge lengths will decrease since we are not adequately modeling the genealogical variation when using concatenation. Here I set the recombination rate within loci to be zero. There is free recombination among loci, however, since they are unlinked.
# init simulator
model = ipcoal.Model(TREE, Ne=1e5, nsamples=2, recomb=0)
# simulate sequence data on coalescent genealogies
model.sim_loci(nloci=100, nsites=200)
# write results to database file
model.write_concat_to_nexus(name="mbtest-2", outdir='/tmp', diploid=True)
wrote concat locus (8 x 20000bp) to /tmp/mbtest-2.nex
# the simulated genealogies of haploid alleles
genes = model.df.genealogy[:4]
# draw the genealogies of the first four loci
toytree.mtree(genes).draw(ts='o', layout='r', height=250);
# init the mb object
mb = ipa.mrbayes(
data="/tmp/mbtest-2.nex",
workdir="/tmp",
name="itest-2",
clock_model=2,
constraints=TREE,
ngen=int(1e6),
nruns=2,
)
# summary of priors/params
print(mb.params)
# start the run
mb.run(force=True)
brlenspr clock:uniform clockratepr normal(0.01,0.005) clockvarpr igr igrvarpr exp(10.0) nchains 4 ngen 1000000 nruns 2 samplefreq 1000 topologypr fixed(fixedtree) job itest-2 finished successfully
# load the inferred tree
mbtre = toytree.tree("/tmp/itest-2.nex.con.tre", 10)
# scale root node from unitless to 1e6
mbtre = mbtre.mod.node_scale_root_height(1e6)
# draw inferred tree
c, a, m = mbtre.draw(ts='o', layout='d', scalebar=True);
# draw true tree in orange on the same axes
TREE.draw(
axes=a,
ts='o',
layout='d',
scalebar=True,
edge_colors="darkorange",
node_sizes=0,
fixed_order=mbtre.get_tip_labels(),
);
mb.convergence_stats
Mean | Variance | Lower | Upper | Median | minESS | avgESS | PSRF | |
---|---|---|---|---|---|---|---|---|
Parameter | ||||||||
TH | 1.371e-02 | 9.286e-07 | 1.189e-02 | 1.567e-02 | 1.371e-02 | 536.255 | 571.212 | 0.999 |
TL | 5.898e-02 | 1.207e-05 | 5.240e-02 | 6.553e-02 | 5.870e-02 | 490.961 | 540.829 | 0.999 |
r(A<->C) | 1.496e-01 | 1.543e-04 | 1.236e-01 | 1.715e-01 | 1.498e-01 | 510.420 | 543.507 | 1.000 |
r(A<->G) | 1.325e-01 | 1.460e-04 | 1.105e-01 | 1.569e-01 | 1.317e-01 | 588.878 | 616.804 | 1.000 |
r(A<->T) | 2.180e-01 | 2.215e-04 | 1.871e-01 | 2.453e-01 | 2.173e-01 | 592.980 | 618.963 | 1.000 |
r(C<->G) | 2.347e-01 | 2.366e-04 | 2.023e-01 | 2.616e-01 | 2.346e-01 | 549.871 | 584.678 | 1.001 |
r(C<->T) | 1.403e-01 | 1.450e-04 | 1.191e-01 | 1.653e-01 | 1.403e-01 | 543.051 | 607.157 | 1.001 |
r(G<->T) | 1.248e-01 | 1.276e-04 | 1.047e-01 | 1.485e-01 | 1.246e-01 | 535.020 | 643.010 | 1.000 |
pi(A) | 2.498e-01 | 9.551e-06 | 2.432e-01 | 2.555e-01 | 2.498e-01 | 396.019 | 481.191 | 0.999 |
pi(C) | 2.500e-01 | 8.635e-06 | 2.445e-01 | 2.558e-01 | 2.499e-01 | 531.979 | 532.053 | 0.999 |
pi(G) | 2.502e-01 | 8.753e-06 | 2.440e-01 | 2.555e-01 | 2.503e-01 | 525.809 | 526.485 | 1.000 |
pi(T) | 2.500e-01 | 8.477e-06 | 2.444e-01 | 2.556e-01 | 2.501e-01 | 491.522 | 495.196 | 1.000 |
alpha | 4.149e-02 | 7.388e-04 | 6.260e-05 | 9.082e-02 | 3.843e-02 | 650.513 | 652.001 | 1.000 |
igrvar | 1.155e-04 | 3.241e-08 | 1.220e-06 | 3.748e-04 | 6.512e-05 | 331.231 | 371.282 | 1.000 |
clockrate | 1.196e-02 | 1.587e-05 | 4.939e-03 | 1.966e-02 | 1.190e-02 | 133.216 | 165.319 | 0.999 |
mb.print_nexus_string()
#NEXUS [log block] log start filename=/tmp/itest-2.nex.log replace; [data block] execute /tmp/mbtest-2.nex; [tree block] begin trees; tree fixedtree = ((r7,r6),(r5,(r4,(r3,(r2,(r1,r0)))))); end; [mb block] begin mrbayes; set autoclose=yes nowarn=yes; lset nst=6 rates=gamma; prset brlenspr=clock:uniform; prset clockvarpr=igr; prset igrvarpr=exp(10.0); prset clockratepr=normal(0.01,0.005); prset topologypr=fixed(fixedtree); mcmcp ngen=1000000 nrun=2 nchains=4; mcmcp relburnin=yes burninfrac=0.25; mcmcp samplefreq=1000; mcmcp printfreq=10000 diagnfr=5000; mcmcp filename=/tmp/itest-2.nex; mcmc; sump filename=/tmp/itest-2.nex; sumt filename=/tmp/itest-2.nex contype=allcompat; end; [log block] log stop filename=/tmp/itest-2.nex.log append;
Here we will try to infer the topology from a concatenated data set (i.e., not set a constraint on the topology). I increased the ngen setting since the MCMC chain takes longer to converge when searching over topology space. Take note that the support values from mrbayes newick files are available in the "prob{percent}" feature, as shown below.
# init the mb object
mb = ipa.mrbayes(
data="/tmp/mbtest-2.nex",
name="itest-3",
workdir="/tmp",
clock_model=2,
ngen=int(2e6),
nruns=2,
)
# summary of priors/params
print(mb.params)
# start run
mb.run(force=True)
brlenspr clock:uniform clockratepr normal(0.01,0.005) clockvarpr igr igrvarpr exp(10.0) nchains 4 ngen 2000000 nruns 2 samplefreq 1000 topologypr uniform job itest-3 finished successfully
The tree topology was correctly inferred
# load the inferred tree
mbtre = toytree.tree("/tmp/itest-3.nex.con.tre", 10)
# scale root node from unitless to 1e6
mbtre = mbtre.mod.node_scale_root_height(1e6)
# draw inferred tree
c, a, m = mbtre.draw(
layout='d',
scalebar=True,
node_sizes=18,
node_labels="prob{percent}",
);
The branch lengths are not very accurate in this case:
# load the inferred tree
mbtre = toytree.tree("/tmp/itest-3.nex.con.tre", 10)
# scale root node from unitless to 1e6
mbtre = mbtre.mod.node_scale_root_height(1e6)
# draw inferred tree
c, a, m = mbtre.draw(ts='o', layout='d', scalebar=True);
# draw true tree in orange on the same axes
TREE.draw(
axes=a,
ts='o',
layout='d',
scalebar=True,
edge_colors="darkorange",
node_sizes=0,
fixed_order=mbtre.get_tip_labels(),
);