treepplr: R Interface to TreePPL

website·Indexed 32 days ago

https://treeppl.org

An 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.

Tokens
15.7K
Snippets
114
Records
148
Agent score
66%

What's inside treepplr

  1. Overview of the treepplr R package

    treepplr is the R interface to TreePPL, a probabilistic programming language designed for phylogenetics. It serves as a bridge that converts data into a TreePPL-readable format, parses TreePPL output, and integrates with downstream analysis packages, such as evolnets for host repertoire evolution models.
  2. Overview of the treepplr R package

    treepplr is the R interface for TreePPL, a probabilistic programming language designed for phylogenetics. It serves as a bridge that converts data into a format readable by TreePPL, parses the resulting output, and integrates with downstream analysis packages such as evolnets (used for host repertoire evolution models).
  3. Overview of TreePPL capabilities

    TreePPL is a probabilistic programming language designed for flexible modeling of tree-structured data. It provides the following core capabilities:

    • Flexible Modeling: Allows users to express complex models using a powerful programming language.
    • Language Interfaces: Supports importing data and analyzing results via R and Python interfaces, including Jupyter notebook support.
    • Inference: Offers a variety of built-in inference strategies, with the option for users to develop custom strategies.
    • Libraries: Includes code and model libraries to accelerate model development.
  4. Use the Basic metabarcoding model for abundance prediction

    The 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

  5. Prepare data for TreePPL using tp_data

    TreePPL requires data in a specific JSON format. The 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)
  6. Define functions and control flow in Treeppl

    Functions must specify input parameters and their types. Curly braces {} 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
    }
  7. Run parallel tempering on a TreePPL model

    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)
  8. Create and compile a TreePPL model in Python

    To use TreePPL in Python, import the 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")
  9. Compile Pigeons posterior samples into a single file

    Pigeons produces posterior samples that are spread across multiple files. To analyze these samples, they must first be compiled into a single file using the Pigeons.tppl_compile_samples function.
    # pt is the Pigeons object
    Pigeons.tppl_compile_samples(pt, "compiled_samples.json")
  10. Parse TreePPL output and check convergence

    Depending on the inference method used, use 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)
  11. Install the treepplr R package

    The 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")