<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/generating-reasonable-starting-trees-for-complex-phylogenetic-analyses/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/12/11/treestartr/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I never really thought I would write an R package. I use R pretty casually. Then, this year, I was invited to participate during the last week of the <a rel="nofollow" href="http://www.analytical.palaeobiology.de/" target="_blank">Analytical Paleobiology short course</a>, an intensive month-long experience in quantitative paleontology. I was thrilled to be invited. But I got a slight sinking feeling in my stomach when I realized all the materials were in R.</p>
<p>And so I, a Pythonista, decided I would spend some of my maternity leave writing R packages to try to blend in with students who had spent the month living and breathing R.</p>
<p><img alt="" src="https://i1.wp.com/media.giphy.com/media/ifxLK48cnyDDi/giphy.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The best way to learn something is to apply it. And in phylogenetics, we have a lot of challenges to which we could apply a little R. We work with datasets that can be hard to visualize. The outputs of Bayesian analyses can be voluminous and tricky to summarize. Models themselves can be complex and incomprehensible. I had a particular task that had been vexing me: generating starting trees for large datasets and complex models of evolution. Many modern methods for estimating phylogenetic trees do so using Markov Chain Monte Carlo simulation, a procedure by which phylogenetic trees are sampled and evaluated under a particular statistical model of evolution (for a gentle, but rigorous, summary of MCMC in phylogenetics see <a rel="nofollow" href="http://www.people.ku.edu/~mtholder/pdf/HolderLewis2003.pdf" target="_blank">this paper by Holder and Lewis</a>). A reasonable starting tree (a tree from which an MCMC tree search is initialized) can improve the efficiency of MCMC-based phylogenetic analyses greatly, particularly in situations where we have lots of parameters and lots of missing data. A starting tree is used to generate initial values for an MCMC search. The starting tree is then modified throughout the course of the tree searching process. Each modification is evaluated to see if it is a better descriptor of the data.</p>
<p>Starting trees are often gleaned from the literature. Usually, a previously published phylogeny will be input as a starting point. In my research we combine fossil and molecular data, and we usually don’t have a published phylogeny for a set of taxa. I can’t simply pluck a tree from the internet and pop it into my analysis. I need a reproducible way to automate the process of generating starting trees. I am faculty at a primarily undergraduate institution, meaning many of my employees are just gaining their skills as computational biologists. Therefore, I need a reasonable way to get starting trees that is simple enough to be executed and debugged by undergraduate students who might not have their legs all the way under them. Tall order!</p>
<p>This is where <a rel="nofollow" href="https://ropensci.github.io/treeStartR/" target="_blank"><code>treestartr</code></a> came from.</p>
<p>From the user side, <code>treestartr</code> has four main utilities: to add taxa to a tree at random, to add taxa to a tree by guessing at taxonomy, to add taxa by hand, or to add taxa via a spreadsheet. All of these facilitate different aspects of the research, and I’ll talk about each in the context of how my students are using them.</p>
<h3>Adding tips via dataframe</h3>
<p>I’m presently working on a dated phylogeny of ants. Two of my <a rel="nofollow" href="https://paleantology.com/the-wright-lab/" target="_blank">lab members</a>, Christina Kolbmann and Tyler Tran, are interested in fossil taxon sampling and its relationship to divergence time estimation accuracy and precision. Ants have tons of fossils, including multiple fossils in some genera or subfamilies. Many of them are preserved in amber, leading to a high degree of precision in placing the ants on the tree (For some stunning images of fossil ants, have a look at <a rel="nofollow" href="http://www.antwiki.org/wiki/Extinct_Ants" target="_blank">AntWiki</a>, a community maintained Wiki devoted to all things ants). This is great – it means we have lots of data. This wealth of data allows us to ask big picture questions about what happens if we exclude parts of the data. One question we’re interested in asking is if subsampling fossils has deleterious effects on accuracy and precision. This is an interesting question because most fossil groups are <em>much</em> more sparsely sampled than ants. We can subsample our wealth of data to simulate different biases that are present in the fossil record of other groups.</p>
<p><img alt="" src="https://i2.wp.com/ropensci.org/img/blog-images/2018-12-11-treestartr/tree.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>In these cases, we know roughly where on the tree might be a reasonable starting place to try attaching the taxon. In this case, we can use the function <a rel="nofollow" href="https://ropensci.github.io/treeStartR/#adding-tips-via-csv" target="_blank"><code>text_placr</code></a> to read in a dataframe of taxonomic relationships. We can then use those relationships to generate a starting tree. An example taxonomy dataframe is below. In this data frame the two tips (if you’ve forgotten your phylogenetic terms, check out the above figure) to be added, <em>Kretzoiartcos beatrix</em> and <em>Ursus abstrusus</em>, are listed in the <code>taxon</code> column. The <code>clade</code> column contains tips that are currently on the tree. The two tips associated with <em>Kretzoiartcos beatrix</em>, <em>Indarctos arctoides</em> and <em>Indarctos vireti</em>, will form a monophyletic group with <em>Kretzoiartcos beatrix</em> on the generated starting tree. The three taxa associated with <em>Ursus abstrusus</em> will form a monophyletic group with <em>Ursus abstrusus</em>.</p>
<table>
<thead>
<tr>
<th>taxon</th>
<th>clade</th>
</tr>
</thead>
<tbody>
<tr>
<td>Kretzoiarctos_beatrix</td>
<td>Indarctos_arctoides</td>
</tr>
<tr>
<td>Kretzoiarctos_beatrix</td>
<td>Indarctos_vireti</td>
</tr>
<tr>
<td>Ursus_abstrusus</td>
<td>Ursus_arctos</td>
</tr>
<tr>
<td>Ursus_abstrusus</td>
<td>Ursus_spelaeus</td>
</tr>
<tr>
<td>Ursus_abstrusus</td>
<td>Ursus_americanus</td>
</tr>
</tbody>
</table>
<p>`</p>
<h3>Adding tips via taxonomy</h3>
<p>Tips can also be added to the tree <a rel="nofollow" href="https://ropensci.github.io/treeStartR/#adding-tips-with-congeners" target="_blank">automatically if there are other members</a> of the same genus on the tree. The function <a rel="nofollow" href="https://ropensci.github.io/treeStartR/#adding-tips-with-congeners" target="_blank"><code>present_tippr</code></a>` checks the genus of a tip to be added to the tree against the genera present on the tree. If there is one match, the tip is added as a sister to its congener. If there are multiple members of the same genus, the tip is added subtending the most recent common ancestor of all members of the genus. This is a fairly handy function for adding taxa that are closely related to other tips on the tree – there’s no need to make a dataframe of species relationships.</p>
<h3>Adding tips at random</h3>
<p>For when you well and truly have no idea how the taxa you want to add to your tree should be added. <a rel="nofollow" href="https://ropensci.github.io/treeStartR/#adding-tips-at-random" target="_blank"><code>rand_absent_tippr</code></a> will add your tips to the tree … somewhere. In the datasets my lab works with, there are always a few fossil taxa with unclear relationships to the rest of the group. This function allows us to develop a distribution of starting positions for mysterious taxa.</p>
<h3>Adding tips by hand</h3>
<p>For the visual among us, <a rel="nofollow" href="https://ropensci.github.io/treeStartR/#adding-tips-at-random" target="_blank"><code>absent_tippr</code></a> opens a graphical user interface. Users can then see the computer-generated node label for every node in the tree, and choose the one they would like the added tip to subtend.</p>
<h3>Mix and Match</h3>
<p>For some of our workflows in the lab, we use multiple functions. For example, we might first place all the tips that have close relatives using <code>present_tippr</code>. Then, tips that represent more distantly related taxa can be placed via <code>text_placr</code>, and the leftover fossils added via <code>rand_absent_tippr</code>.</p>
<h2>Why rOpenSci</h2>
<p>I was a little unsure at first about submitting a package to rOpenSci. I’m a biologist, and while I’m computational, the idea that I might be a solo author on an R package seemed like I was, I don’t know, misrepresenting myself? I’m not some sort of big, serious software person. But the more I thought about it, I realized that that makes rOpenSci more attractive – that some serious people could help guide me in making this package. I want stable software for my students to learn from and use. And so I should ask for review. It’s appropriate and it’s good to ask for expert opinion.</p>
<p>When I was thinking of writing up this blog, I posed the question to Twitter:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">are there other venues (outside or within traditional scientific journals) for R code review? My packages don't fall under scope of <a rel="nofollow" href="https://twitter.com/rOpenSci?ref_src=twsrc%5Etfw" target="_blank">@rOpenSci</a> and would probably need domain experts (plant biologists in this case)</p>
<p>— Chris Muir (@thegoodphyte) <a rel="nofollow" href="https://twitter.com/thegoodphyte/status/1067269265810698240?ref_src=twsrc%5Etfw" target="_blank">November 27, 2018</a></p></blockquote>
<p></p>
<p>And I hope I can answer this question adequately. I picked ROpenSci because it was explicitly R (so the editor and reviewers would know best practices) and the guidelines and how review would proceed were very clear. And for a totally new experience of in-depth code review, being able to see where the effort was collaborative was helpful. I looked at <a rel="nofollow" href="https://joss.readthedocs.io/en/latest/submitting.html" target="_blank">other journals</a>, but I liked that rOpenSci had biologists as editors. Because the handling editor (<a rel="nofollow" href="https://scottchamberlain.info/" target="_blank">Scott Chamberlain</a>) is a biologist, he had the ability to work with me to find reviewers (<a rel="nofollow" href="https://geogeo.tamu.edu/people/faculty/bapstdavidwilliam" target="_blank">David Bapst</a> and <a rel="nofollow" href="https://www.bsse.ethz.ch/cevo/the-group/people/person-detail.html?persid=215787" target="_blank">Rachel Warnock</a>) who are serious experts in both R and the particular type of phylogenetics that I do. I suggested Dave &amp; Rachel because I know I can collaborate with them, and they will be constructive and kind. But they will also really try to break the package and dig in to the meat of what software functions are required to support the science we do, and tell me honestly if my package is providing those functions. <a rel="nofollow" href="https://github.com/ropensci/onboarding/issues/239" target="_blank">You can see</a> they weren’t easy on me!</p>
<p>rOpenSci also has a <a rel="nofollow" href="https://ropensci.org/blog/2017/11/29/review-collaboration-mee/" target="_blank">collaboration</a> with <a rel="nofollow" href="https://besjournals.onlinelibrary.wiley.com/hub/journal/2041210X/author-guidelines" target="_blank">Methods in Ecology and Evolution</a>. I plan to send a short software note there. It’s very nice to have the flexibility to send the paper to a traditional outlet like Methods in Ecology and Evolution, or to send it to <a rel="nofollow" href="https://joss.theoj.org/" target="_blank">JOSS</a>, or to do nothing at all, if I didn’t want to.</p>
<h3>tl;dr</h3>
<p>Come on in, biologists. The water’s fine.</p>
<h2>Future</h2>
<p>I’ll likely add in more utility functions for parsing biological data for use with divergence time estimation. Particularly, I often work with <a rel="nofollow" href="https://revbayes.github.io/" target="_blank">RevBayes</a>, which is a sandbox-like framework for Bayesian analysis of phylogeny and comparative methods. Our analyses integrate across fossil taxa from databases, molecular data from the literature and stratigraphic ranges. Each data source has special and unique quirks, and I’m always looking for ways to lower the bar for undergraduate researchers to be able to access these sorts of complex modeling frameworks. Please feel free to make a pull request or file an issue if there’s something you’d like to see added. I have a <a rel="nofollow" href="https://github.com/ropensci/treeStartR/blob/master/.github/CONTRIBUTING.md" target="_blank">Contributing</a> guide here outlining how to contribute.</p>
<h2>Acknowledgements</h2>
<p>David Bapst and Rachel Warnock reviewed this package, and provided immensely helpful comments. Scott Chamberlain handled the review. I’d also like to thank Jack Henry Wright Stinnett and Alice Wright Stinnett for being such good sleepers that I got to make the skeleton of this package while they napped:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">Wrote a small workflow for generating molecular + morphological starting trees in python, but realized for upcoming use, it actually makes more sense to be an R package. Both kids are asleep, here we go. <a rel="nofollow" href="https://t.co/ckouxLHUV7" target="_blank">pic.twitter.com/ckouxLHUV7</a></p>
<p>— April Wright (@WrightingApril) <a rel="nofollow" href="https://twitter.com/WrightingApril/status/1014973461481938944?ref_src=twsrc%5Etfw" target="_blank">July 5, 2018</a></p></blockquote>
<p></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/blog/2018/12/11/treestartr/"> rOpenSci - open tools for open science</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Pv6eJAUV8uQ" height="1"></a></div>