pirouette
has the option to investigate the error BEAST2 makes on a twin tree. A twin tree is a tree with the same topology as the original phylogeny, yet where the branch lengths follow a birth-death branch length distribution.
library(pirouette)
library(ggplot2)
Twinning is useful to separate the effect of an unknown tree prior (i.e. speciation model) on a phylogeny’s shape from the noise (the minimal error) made by BEAST2.
In this example, the following tree is used:
ape::read.tree(text = "((A:1, B:1):1, (C:1, D:1):1);")
phylogeny <-::plot.phylo(phylogeny, main = "The True Phylogeny") ape
This phylogeny follows a speciation model unknown to BEAST2, as two speciation events happened at exactly the same time. Such a phylogeny will have a likelihood of zero for all of the BEAST2 tree priors.
Twinning:
pirouette::create_twinning_params() twinning_params <-
pirouette::create_alignment_params(
alignment_params <-root_sequence = pirouette::create_blocked_dna(length = 100),
rng_seed = 314
)
We select our inference models in two ways:
type | run_if | measure evidence | inference model |
---|---|---|---|
generative | always | TRUE |
Birth-Death |
candidate | best_candidate | TRUE |
Yule |
1 <- create_test_gen_experiment(
experiment_inference_model = create_test_inference_model(
tree_prior = create_bd_tree_prior()
)
)if (rappdirs::app_dir()$os != "win") {
2 <- create_test_cand_experiment(
experiment_inference_model = create_test_inference_model(
tree_prior = create_yule_tree_prior()
)
)1$inference_conditions$do_measure_evidence <- TRUE
experiment_
list(experiment_1, experiment_2)
experiments <-
$twin_evidence_filename <- get_temp_evidence_filename()
twinning_params
create_pir_params(
pir_params <-alignment_params = alignment_params,
experiments = experiments,
twinning_params = twinning_params,
evidence_filename = get_temp_evidence_filename()
)
else {
} list(experiment_1)
experiments <-
create_pir_params(
pir_params <-alignment_params = alignment_params,
experiments = experiments,
twinning_params = twinning_params
) }
Run:
NULL
df <-if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { pir_run(
df <-phylogeny = phylogeny,
pir_params = pir_params
)else {
} create_test_pir_run_output()
df <- }
Show as a table:
::kable(df) knitr
tree | inference_model | inference_model_weight | site_model | clock_model | tree_prior | error_1 | error_2 | error_3 |
---|---|---|---|---|---|---|---|---|
true | generative | 0.5 | JC69 | relaxed_log_normal | birth_death | 0.1 | 0.11 | 0.12 |
Show as a figure:
::pir_plot(df) pirouette
library(ggplot2)
See true tree again:
::plot.phylo(phylogeny, main = "The True Phylogeny") ape
See the alignment generated from the true tree:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {check_file_exists(pir_params$alignment_params$fasta_filename)
::image.DNAbin(
ape::read.FASTA(file = pir_params$alignment_params$fasta_filename)
ape
) }
See the posterior trees generated from the true alignment, for the generative model:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {# BEAUti offers the '$(tree)' shorthand notation.
# Here, do what BEAUti does...
treelog_filename <- pir_params$experiments[[1]]$inference_model$mcmc$treelog$filename
gsub(
treelog_filename <-x = treelog_filename,
pattern = "\\$\\(tree\\)",
replacement = beautier::get_alignment_id(
$alignment_params$fasta_filename
pir_params
)
)check_file_exists(treelog_filename)
::plot_densitree(tracerer::parse_beast_trees(treelog_filename))
babette }
See the evidence of the true alignments for the models:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {::kable(readr::read_csv(pir_params$evidence_filename))
knitr }
See the posterior trees generated from the true alignment, for the model with the most evidence:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { pir_params$experiments[[2]]
experiment <- experiment$inference_model$mcmc$treelog$filename
trees_filename <-check_file_exists(trees_filename)
::plot_densitree(tracerer::parse_beast_trees(trees_filename))
babette }
See the posterior parameter estimates generated from the true alignment, for the generative model:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {# A tracelog's filename is set to NA by default.
# Here, do what BEAUti does...
tracelog_filename <- pir_params$experiments[[1]]$inference_model$mcmc$tracelog$filename
if (is.na(tracelog_filename)) {
$experiments[[1]]$inference_model$mcmc$tracelog$filename <-
pir_params paste0(
::get_alignment_id(pir_params$alignment_params$fasta_filename),
beautier".log"
)
}
check_file_exists(
$experiments[[1]]$inference_model$mcmc$tracelog$filename
pir_params
) tracerer::parse_beast_tracelog_file(
df <-$experiments[[1]]$inference_model$mcmc$tracelog$filename
pir_params
)ggplot(data = df, aes(x = Sample, y = likelihood)) + geom_line()
}
See the posterior parameter estimates generated from the true alignment, for the model with the most evidence:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { pir_params$experiments[[2]]
experiment <- experiment$inference_model$mcmc$tracelog$filename
log_filename <-check_file_exists(log_filename)
tracerer::parse_beast_tracelog_file(log_filename)
df <-ggplot(data = df, aes(x = Sample, y = likelihood)) + geom_line()
}
See the twin tree:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {::plot.phylo(
ape::read.tree(pir_params$twinning_params$twin_tree_filename),
apemain = "The Twin Tree"
) }
See the alignment generated from the twin tree:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) {check_file_exists(pir_params$twinning_params$twin_alignment_filename)
::image.DNAbin(
ape::read.FASTA(file = pir_params$twinning_params$twin_alignment_filename)
ape
) }
See the posterior trees generated from the twin alignment, for the generative model:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { to_twin_filename(
trees_filename <-$experiments[[1]]$inference_model$mcmc$treelog$filename
pir_params
)check_file_exists(trees_filename)
::plot_densitree(
babette::parse_beast_trees(trees_filename)
tracerer
) }
See the evidence of the true alignments for the models:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { pir_params$twinning_params$twin_evidence_filename
twin_evidence_filename <-check_file_exists(twin_evidence_filename)
::kable(readr::read_csv(twin_evidence_filename))
knitr }
See the posterior trees generated from the twin alignment, for the model with the most evidence:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { to_twin_filename(
trees_filename <-$experiments[[2]]$inference_model$mcmc$treelog$filename
pir_params
)check_file_exists(trees_filename)
::plot_densitree(tracerer::parse_beast_trees(trees_filename))
babette }
See the posterior parameter estimates generated from the true alignment, for the generative model:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() && is_beast2_ns_pkg_installed()) {
to_twin_filename(
log_filename <-$experiments[[2]]$inference_model$mcmc$tracelog$filename
pir_params
)check_file_exists(log_filename)
tracerer::parse_beast_tracelog_file(log_filename)
df <-ggplot(data = df, aes(x = Sample, y = likelihood)) + geom_line()
}
See the posterior parameter estimates generated from the true alignment, for the model with the most evidence:
if (rappdirs::app_dir()$os != "win" &&
is_beast2_installed() &&
is_beast2_ns_pkg_installed()
) { to_twin_filename(
log_filename <-$experiments[[2]]$inference_model$mcmc$tracelog$filename
pir_params
)check_file_exists(log_filename)
tracerer::parse_beast_tracelog_file(log_filename)
df <-ggplot(data = df, aes(x = Sample, y = likelihood)) + geom_line()
}