---
title: "Reading a BEAST posterior"
author: "Richèl J.C. Bilderbeek"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Reading a BEAST posterior}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

This vignette shows how to parse a BEAST2 posterior file using `tracerer`.

Load the `tracerer` package:

```{r message = FALSE}
library(tracerer)
```


## Extracting the phylogenies from the .trees file

Check if the example file can be found:

```{r}
trees_file <- get_tracerer_path("beast2_example_output.trees")
testit::assert(file.exists(trees_file))
```

Parse the posterior:

```{r}
posterior_trees <- parse_beast_trees(trees_file)
```

Investigating the posterior:

```{r}
names(posterior_trees)
testit::assert(length(posterior_trees) == 11)
```

We can see that the posterior has multiple states.

Every state is a phylogeny:

```{r}
testit::assert(class(posterior_trees[[1]]) == "phylo")
```

We can plot these all separately:

```{r}
for (p in posterior_trees) {
  graphics::plot(p)
}
```

Or we plot all at once:

```{r fig.width = 7, fig.height = 7}
phangorn::densiTree(
  posterior_trees, 
  type = "cladogram", 
  alpha = 1
)
```
