#!/usr/bin/env python # coding: utf-8 # # ipyrad-analysis toolkit: mrBayes # # [View as notebook](https://nbviewer.jupyter.org/github/dereneaton/ipyrad/blob/master/newdocs/API-analysis/cookbook-mrbayes.ipynb) # # 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 # 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__) get_ipython().system(' mb -v') # ### 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 # In[7]: # print the mb nexus string; this can be modified by changing .params print(mb.nexus_string) # ### 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) # ### 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 # In[10]: # for example to select the consensus tree path mb.trees.constre # ### 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) # ### 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", ); # ### 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, ); # 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()}, ); # ### 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).