diff --git a/DESCRIPTION b/DESCRIPTION index 837be75..f91bf03 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,10 +34,10 @@ Imports: purrr, rlang, ggplot2 -URL: https://github.com/EPPIcenter/moire, https://eppicenter.github.io/moire/, https://eppicenter.ucsf.edu/resources +URL: https://github.com/EPPIcenter/moire, https://EPPIcenter.github.io/moire/, https://eppicenter.ucsf.edu/resources BugReports: https://github.com/EPPIcenter/moire/issues Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index b8aec00..eb3ba5c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(calculate_eda) export(calculate_he) export(calculate_med_allele_freqs) export(calculate_naive_allele_frequencies) @@ -19,6 +20,7 @@ export(simulate_sample_genotype) export(summarize_allele_freq_fn) export(summarize_allele_freqs) export(summarize_coi) +export(summarize_eda) export(summarize_effective_coi) export(summarize_epsilon_neg) export(summarize_epsilon_pos) @@ -28,6 +30,8 @@ import(RcppProgress) import(purrr) importFrom(Rcpp,sourceCpp) importFrom(RcppParallel,RcppParallelLibs) +importFrom(dplyr,arrange) +importFrom(dplyr,bind_rows) importFrom(ggplot2,aes) importFrom(ggplot2,coord_cartesian) importFrom(ggplot2,geom_point) diff --git a/R/summary.R b/R/summary.R index 51e030f..0eacf94 100644 --- a/R/summary.R +++ b/R/summary.R @@ -128,6 +128,17 @@ calculate_he <- function(allele_freqs) { return(1 - sum(allele_freqs**2)) } +#' Calcuate the expected number of distinct alleles under a given multiplicity of infection (n) +#' +#' @export +#' +#' @param allele_freqs Simplex of allele frequencies +#' @param n multiplicity of infection +calculate_eda <- function(allele_freqs, n) { + sum(1 - (1 - allele_freqs)^n) +} + + #' Summarize COI #' #' @details Summarize complexity of infection results from MCMC. Returns @@ -443,6 +454,42 @@ summarize_he <- function(mcmc_results, return(res) } +#' Summarize expected number of distinct alleles +#' +#' @details Summarize expected number of distinct alleles results from MCMC estimates of allele frequencies. +#' Returns a dataframe that contains summaries of the posterior distribution of expected number of distinct alleles +#' +#' @export +#' +#' @importFrom dplyr bind_rows +#' @importFrom dplyr arrange +#' @importFrom rlang .data +#' +#' @param mcmc_results Result of calling run_mcmc() +#' @param n values of multiplicity of infection to evaluate the expected number of distinct alleles +#' @param lower_quantile The lower quantile of the posterior distribution +#' to return +#' @param upper_quantile The upper quantile of the posterior distribution +#' to return +#' @param merge_chains boolean indicating that all chain results should be merged +summarize_eda <- function(mcmc_results, n, lower_quantile = .025, upper_quantile = .975, merge_chains = TRUE) { + res <- lapply(n, function(n_) { + out <- summarize_allele_freq_fn( + mcmc_results, + fn = function(x) calculate_eda(x, n_), + lower_quantile = lower_quantile, + upper_quantile = upper_quantile, + merge_chains = merge_chains + ) + out$n <- n_ + return(out) + }) |> + dplyr::bind_rows() |> + dplyr::arrange(.data$locus, n) + + return(res) +} + names_or_idxs <- function(vec) { if (is.null(names(vec))) { return(sapply(seq(1, length(vec)), as.character)) @@ -451,7 +498,6 @@ names_or_idxs <- function(vec) { } } - #' Summarize allele frequencies #' #' @details Summarize individual allele frequencies from the posterior diff --git a/man/calculate_eda.Rd b/man/calculate_eda.Rd new file mode 100644 index 0000000..447dbe4 --- /dev/null +++ b/man/calculate_eda.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/summary.R +\name{calculate_eda} +\alias{calculate_eda} +\title{Calcuate the expected number of distinct alleles under a given multiplicity of infection (n)} +\usage{ +calculate_eda(allele_freqs, n) +} +\arguments{ +\item{allele_freqs}{Simplex of allele frequencies} + +\item{n}{multiplicity of infection} +} +\description{ +Calcuate the expected number of distinct alleles under a given multiplicity of infection (n) +} diff --git a/man/summarize_eda.Rd b/man/summarize_eda.Rd new file mode 100644 index 0000000..dc5abb2 --- /dev/null +++ b/man/summarize_eda.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/summary.R +\name{summarize_eda} +\alias{summarize_eda} +\title{Summarize expected number of distinct alleles} +\usage{ +summarize_eda( + mcmc_results, + n, + lower_quantile = 0.025, + upper_quantile = 0.975, + merge_chains = TRUE +) +} +\arguments{ +\item{mcmc_results}{Result of calling run_mcmc()} + +\item{n}{values of multiplicity of infection to evaluate the expected number of distinct alleles} + +\item{lower_quantile}{The lower quantile of the posterior distribution +to return} + +\item{upper_quantile}{The upper quantile of the posterior distribution +to return} + +\item{merge_chains}{boolean indicating that all chain results should be merged} +} +\description{ +Summarize expected number of distinct alleles +} +\details{ +Summarize expected number of distinct alleles results from MCMC estimates of allele frequencies. +Returns a dataframe that contains summaries of the posterior distribution of expected number of distinct alleles +}