Overview of the treepplr R package
evolnets for host repertoire evolution models.website·Indexed 32 days ago
https://treeppl.orgAn R interface for TreePPL, a probabilistic programming language for trees. The package provides tools to compile TreePPL models, run programs via tprun(), and process outputs including MCMC and SMC convergence analysis. It includes utilities for converting between TreePPL JSON strings and R phylo/multiPhylo objects, as well as functions for importing data and calculating tree ages.
evolnets for host repertoire evolution models.evolnets (used for host repertoire evolution models).TreePPL is a probabilistic programming language designed for flexible modeling of tree-structured data. It provides the following core capabilities:
optmodel.tppl model is a probabilistic model used to infer parameter values from the read numbers of spike-in species in metabarcoding samples. These inferred distributions are then used to predict the abundances of other insect species within the sample set. This implementation is based on the methodology described by Iwaszkiewicz-Eggebrecht et al. (2023).Model file: models/metabarcoding/optmodel.tppl
Example input data: models/metabarcoding/data/testdata_optmodel.json
tp_data function converts common input formats (such as phylogenetic trees from ape or FASTA sequence files) into this required format and writes them to a file.# For models requiring a phylogenetic tree
phylo <- ape::read.tree(file = "path/to/your/file.tre")
data_path <- tp_data(data_input = phylo)
# For models requiring sequence data
fasta_file <- "path/to/your/file.fasta"
data_path <- tp_data(data_input = fasta_file){} are mandatory for if statements and for loops. The only permitted side-effects within a function are changing the likelihood, printing, and returning an output.// Function without a return
function flip(datapoint: Bool, probability: Real) {
observe datapoint ~ Bernoulli(probability);
}
// Function with a return value
function flip(datapoint: Bool, probability: Real) => Real {
observe datapoint ~ Bernoulli(probability);
return probability;
}
// Conditional logic
if (condition) {
// execute when true
} else {
// execute when false
}
// Looping
for i in 1 to n {
// loop body
}After compiling the model binary, use Pigeons.tppl_construct_target to create a TreePPLTarget struct. This struct is then passed to the pigeons function to execute the sampling process.
Important execution parameters:
n_rounds: Determines the number of iterations; each successive round doubles the iterations of the previous round.n_chains: The number of parallel chains. A rule of thumb is to use $2\Lambda + 1$ chains, where $\Lambda$ is the global communication barrier.multithreaded: Boolean to enable multithreading.Always call Pigeons.kill_child_processes(pt) after the run to terminate the TreePPL processes.
using Pigeons
# Construct the target
data_path = "./treeppl/models/diversification/data/testdata_crbd.json"
output_path = "pigeons_output_dir"
tppl_target = Pigeons.tppl_construct_target(tppl_bin, data_path, output_path)
# Launch Pigeons
pt = pigeons(target = tppl_target, n_rounds = 12, n_chains = 10, multithreaded=true)
# Cleanup
Pigeons.kill_child_processes(pt)treeppl package. You can define a model using either inline source code or a separate .tppl file. Creating a treeppl.Model instance automatically compiles the source code. If no errors are raised during instantiation, compilation was successful.import treeppl
# Option 1: Using inline source code
coin_source = """
model function coin(outcomes: Bool[]) => Real {
assume p ~ Beta(2.0, 2.0);
for i in 1 to (length(outcomes)) {
observe outcomes[i] ~ Bernoulli(p);
}
return(p);
}
"""
coin = treeppl.Model(source=coin_source)
# Option 2: Using a model file
coin = treeppl.Model(filename="coin.tppl")Pigeons.tppl_compile_samples function.# pt is the Pigeons object
Pigeons.tppl_compile_samples(pt, "compiled_samples.json")tp_parse_smc or tp_parse_mcmc to convert the raw output into a data frame. For SMC runs, tp_smc_convergence can be used to evaluate convergence.# For SMC output
output_df_smc <- tp_parse_smc(output)
tp_smc_convergence(output_df_smc)
# For MCMC output
output_df_mcmc <- tp_parse_mcmc(output)treepplr package can be installed using the pak manager. Note that installing the R package does not immediately install the TreePPL compiler; the compiler is downloaded automatically the first time an analysis is run, or it can be installed manually using treepplr::tp_installing_treeppl().if(!require("pak", quietly = TRUE)) {
install.packages("pak")
library(pak)
} else {
library(pak)
}
pak::pak("treeppl/treepplr")