From 0dfb5a4612c0f6021883931f36155c03225ecf2b Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:48:58 -0400 Subject: [PATCH 1/7] needs improvement --- NAMESPACE | 2 +- R/provoc_methods.R | 19 +++++++++++++++++-- man/autoplot.provoc.Rd | 2 +- man/predict.provoc.Rd | 2 ++ man/variants_similarity.Rd | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a69c754..6164f57 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,12 @@ # Generated by roxygen2: do not edit by hand -S3method(autoplot,provoc) S3method(plot,provoc) S3method(predict,provoc) S3method(print,provoc) S3method(summary,provoc) export(add_coverage) export(astronomize) +export(autoplot.provoc) export(coverage_at_aa) export(filter_varmat) export(fuse) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index bfddfc5..4e175c3 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -17,20 +17,35 @@ #' @examples #' predicted_results <- predict(provoc_obj) predict.provoc <- function(provoc_obj, + b1, b2, newdata = NULL, type = NULL, dispersion = NULL, terms = NULL) { + # TEMP example usage + # library(provoc) + # data("Baaijens") + # Baaijens$mutation <- parse_mutations(Baaijens$label) + # res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) + # print(get_convergence(res)) + # predicted_values <- predict.provoc(res) + if (!"provoc" %in% class(provoc_obj)) { stop("Object must be of class 'provoc'") } + b1_index <- match("b1", colnames(provoc_obj$variant)) + b2_index <- match("b2", colnames(provoc_obj$variant)) + proportions <- as.numeric(provoc_obj$rho) - variant_matrix <- get_varmat(provoc_obj) + variant_matrix <- attr(provoc_obj, "variant_matrix") + #variant_matrix <- get_varmat(provoc_obj) + if (any(!rownames(variant_matrix) %in% provoc_obj$variant)) { stop("Variant matrix does not match variants in results") } - results <- proportions %*% variant_matrix[provoc_obj$variant, ] + results <- proportions[b1_index] %*% variant_matrix[b2_index, ] + #results <- proportions %*% variant_matrix[provoc_obj$variant, ] return(results) } diff --git a/man/autoplot.provoc.Rd b/man/autoplot.provoc.Rd index 6293010..40ac897 100644 --- a/man/autoplot.provoc.Rd +++ b/man/autoplot.provoc.Rd @@ -4,7 +4,7 @@ \alias{autoplot.provoc} \title{Plot a provoc object using ggplot2} \usage{ -\method{autoplot}{provoc}(provoc_obj, date_col = NULL) +autoplot.provoc(provoc_obj, date_col = NULL) } \arguments{ \item{provoc_obj}{The result of provoc(), or an object coerced via as.provoc().} diff --git a/man/predict.provoc.Rd b/man/predict.provoc.Rd index b502d47..29bad91 100644 --- a/man/predict.provoc.Rd +++ b/man/predict.provoc.Rd @@ -6,6 +6,8 @@ \usage{ \method{predict}{provoc}( provoc_obj, + b1, + b2, newdata = NULL, type = NULL, dispersion = NULL, diff --git a/man/variants_similarity.Rd b/man/variants_similarity.Rd index 937247f..b9fdeea 100644 --- a/man/variants_similarity.Rd +++ b/man/variants_similarity.Rd @@ -9,7 +9,7 @@ variants_similarity(data, is_varmat) \arguments{ \item{data}{A dataframe either before or after it has been fused with varmat} -\item{is_varmat}{TRUE if data is a varaint matrix, FALSE if data is a fused dataframe} +\item{is_varmat}{TRUE if data is a variant matrix, FALSE if data is a fused dataframe} } \value{ none From 0f11dfe1164e41c8cade88d0bda8252022afe7a5 Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:51:21 -0400 Subject: [PATCH 2/7] complete temp example usage --- R/provoc_methods.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index 4e175c3..cbdd59b 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -22,12 +22,23 @@ predict.provoc <- function(provoc_obj, dispersion = NULL, terms = NULL) { # TEMP example usage + # ------- # library(provoc) # data("Baaijens") + # Baaijens$mutation <- parse_mutations(Baaijens$label) + # res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) + # print(get_convergence(res)) + + # b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] + # b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] + # predicted_values <- predict.provoc(res) + # print(predicted_values) + # ------- + # END OF TEMP if (!"provoc" %in% class(provoc_obj)) { stop("Object must be of class 'provoc'") From 13d532a24bd702fe11cbe489e0a6ed731dc34363 Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:55:57 -0400 Subject: [PATCH 3/7] still shows NAs --- R/provoc_methods.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index cbdd59b..973b5a6 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -35,7 +35,7 @@ predict.provoc <- function(provoc_obj, # b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] # b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] - # predicted_values <- predict.provoc(res) + # predicted_values <- predict.provoc(res, b1, b2) # print(predicted_values) # ------- # END OF TEMP @@ -44,8 +44,11 @@ predict.provoc <- function(provoc_obj, stop("Object must be of class 'provoc'") } - b1_index <- match("b1", colnames(provoc_obj$variant)) - b2_index <- match("b2", colnames(provoc_obj$variant)) + # b1_index <- match("b1", colnames(provoc_obj$variant)) + # b2_index <- match("b2", colnames(provoc_obj$variant)) + + b1_index <- match(b1, provoc_obj$group) + b2_index <- match(b2, provoc_obj$group) proportions <- as.numeric(provoc_obj$rho) variant_matrix <- attr(provoc_obj, "variant_matrix") From 002d45131342abee36490122e946d64295beb609 Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:48:58 -0400 Subject: [PATCH 4/7] needs improvement --- NAMESPACE | 2 +- R/provoc_methods.R | 19 +++++++++++++++++-- man/autoplot.provoc.Rd | 2 +- man/predict.provoc.Rd | 2 ++ man/variants_similarity.Rd | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index e4b4354..c15fdea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,12 @@ # Generated by roxygen2: do not edit by hand -S3method(autoplot,provoc) S3method(plot,provoc) S3method(predict,provoc) S3method(print,provoc) S3method(summary,provoc) export(add_coverage) export(astronomize) +export(autoplot.provoc) export(coverage_at_aa) export(filter_varmat) export(fuse) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index bfddfc5..4e175c3 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -17,20 +17,35 @@ #' @examples #' predicted_results <- predict(provoc_obj) predict.provoc <- function(provoc_obj, + b1, b2, newdata = NULL, type = NULL, dispersion = NULL, terms = NULL) { + # TEMP example usage + # library(provoc) + # data("Baaijens") + # Baaijens$mutation <- parse_mutations(Baaijens$label) + # res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) + # print(get_convergence(res)) + # predicted_values <- predict.provoc(res) + if (!"provoc" %in% class(provoc_obj)) { stop("Object must be of class 'provoc'") } + b1_index <- match("b1", colnames(provoc_obj$variant)) + b2_index <- match("b2", colnames(provoc_obj$variant)) + proportions <- as.numeric(provoc_obj$rho) - variant_matrix <- get_varmat(provoc_obj) + variant_matrix <- attr(provoc_obj, "variant_matrix") + #variant_matrix <- get_varmat(provoc_obj) + if (any(!rownames(variant_matrix) %in% provoc_obj$variant)) { stop("Variant matrix does not match variants in results") } - results <- proportions %*% variant_matrix[provoc_obj$variant, ] + results <- proportions[b1_index] %*% variant_matrix[b2_index, ] + #results <- proportions %*% variant_matrix[provoc_obj$variant, ] return(results) } diff --git a/man/autoplot.provoc.Rd b/man/autoplot.provoc.Rd index 6293010..40ac897 100644 --- a/man/autoplot.provoc.Rd +++ b/man/autoplot.provoc.Rd @@ -4,7 +4,7 @@ \alias{autoplot.provoc} \title{Plot a provoc object using ggplot2} \usage{ -\method{autoplot}{provoc}(provoc_obj, date_col = NULL) +autoplot.provoc(provoc_obj, date_col = NULL) } \arguments{ \item{provoc_obj}{The result of provoc(), or an object coerced via as.provoc().} diff --git a/man/predict.provoc.Rd b/man/predict.provoc.Rd index b502d47..29bad91 100644 --- a/man/predict.provoc.Rd +++ b/man/predict.provoc.Rd @@ -6,6 +6,8 @@ \usage{ \method{predict}{provoc}( provoc_obj, + b1, + b2, newdata = NULL, type = NULL, dispersion = NULL, diff --git a/man/variants_similarity.Rd b/man/variants_similarity.Rd index 937247f..b9fdeea 100644 --- a/man/variants_similarity.Rd +++ b/man/variants_similarity.Rd @@ -9,7 +9,7 @@ variants_similarity(data, is_varmat) \arguments{ \item{data}{A dataframe either before or after it has been fused with varmat} -\item{is_varmat}{TRUE if data is a varaint matrix, FALSE if data is a fused dataframe} +\item{is_varmat}{TRUE if data is a variant matrix, FALSE if data is a fused dataframe} } \value{ none From 846d035d553a1b284c021b64088485bcc9e9c933 Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:51:21 -0400 Subject: [PATCH 5/7] complete temp example usage --- R/provoc_methods.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index 4e175c3..cbdd59b 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -22,12 +22,23 @@ predict.provoc <- function(provoc_obj, dispersion = NULL, terms = NULL) { # TEMP example usage + # ------- # library(provoc) # data("Baaijens") + # Baaijens$mutation <- parse_mutations(Baaijens$label) + # res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) + # print(get_convergence(res)) + + # b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] + # b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] + # predicted_values <- predict.provoc(res) + # print(predicted_values) + # ------- + # END OF TEMP if (!"provoc" %in% class(provoc_obj)) { stop("Object must be of class 'provoc'") From 81afd2dd9db6f4a5ce321cd5536097e241cd70a4 Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:55:57 -0400 Subject: [PATCH 6/7] still shows NAs --- R/provoc_methods.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index cbdd59b..973b5a6 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -35,7 +35,7 @@ predict.provoc <- function(provoc_obj, # b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] # b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] - # predicted_values <- predict.provoc(res) + # predicted_values <- predict.provoc(res, b1, b2) # print(predicted_values) # ------- # END OF TEMP @@ -44,8 +44,11 @@ predict.provoc <- function(provoc_obj, stop("Object must be of class 'provoc'") } - b1_index <- match("b1", colnames(provoc_obj$variant)) - b2_index <- match("b2", colnames(provoc_obj$variant)) + # b1_index <- match("b1", colnames(provoc_obj$variant)) + # b2_index <- match("b2", colnames(provoc_obj$variant)) + + b1_index <- match(b1, provoc_obj$group) + b2_index <- match(b2, provoc_obj$group) proportions <- as.numeric(provoc_obj$rho) variant_matrix <- attr(provoc_obj, "variant_matrix") From d16132fe6ef65f1bf50f81edbefc70e8d12f299f Mon Sep 17 00:00:00 2001 From: Daner Yasin <77366878+danerkestey@users.noreply.github.com> Date: Tue, 26 Mar 2024 03:13:05 -0400 Subject: [PATCH 7/7] Rebase and clean up --- NAMESPACE | 3 ++- R/provoc_methods.R | 42 ++++++++++++------------------------- man/autoplot.provoc.Rd | 2 +- man/find_mutation_column.Rd | 21 +++++++++++++++++++ man/parse_ivar.Rd | 14 +++++++++++++ man/parse_one_ivar.Rd | 16 ++++++++++++++ man/predict.provoc.Rd | 13 +++++++++++- man/provoc.Rd | 2 +- 8 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 man/find_mutation_column.Rd create mode 100644 man/parse_ivar.Rd create mode 100644 man/parse_one_ivar.Rd diff --git a/NAMESPACE b/NAMESPACE index c15fdea..e56cca0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,12 @@ # Generated by roxygen2: do not edit by hand +S3method(autoplot,provoc) S3method(plot,provoc) S3method(predict,provoc) S3method(print,provoc) S3method(summary,provoc) export(add_coverage) export(astronomize) -export(autoplot.provoc) export(coverage_at_aa) export(filter_varmat) export(fuse) @@ -15,6 +15,7 @@ export(get_convergence) export(get_lineage_info) export(get_mutation_defs) export(get_res) +export(parse_ivar) export(parse_mutations) export(pos_from_aa) export(provoc) diff --git a/R/provoc_methods.R b/R/provoc_methods.R index 973b5a6..a74da86 100644 --- a/R/provoc_methods.R +++ b/R/provoc_methods.R @@ -1,4 +1,3 @@ - #' Predict using Proportions of Variants of Concern #' #' Takes a named list with an estimate of the proportions and the @@ -6,6 +5,8 @@ #' predict outcomes, and returns results in the same order as the original data. #' #' @param provoc_obj Named list with `proportions` and `variant_matrix`. +#' @param b1 Name of the first sra sample group. +#' @param b2 Name of the second sra sample group. #' @param newdata Not yet implemented. #' @param type Not yet implemented. #' @param se.fit Not yet implemented. @@ -13,53 +14,36 @@ #' @param terms Not yet implemented. #' @param na.action Not yet implemented. #' @return Predicted values in the same order as the input data. -#' @export #' @examples -#' predicted_results <- predict(provoc_obj) -predict.provoc <- function(provoc_obj, +#' library(provoc) +#' data("Baaijens") +#' Baaijens$mutation <- parse_mutations(Baaijens$label) +#' res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) +#' b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] +#' b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] +#' predicted_values <- predict.provoc(res, b1, b2) +#' print(predicted_values) +#' @export +predict.provoc <- function( + provoc_obj, b1, b2, newdata = NULL, type = NULL, dispersion = NULL, terms = NULL) { - - # TEMP example usage - # ------- - # library(provoc) - # data("Baaijens") - - # Baaijens$mutation <- parse_mutations(Baaijens$label) - - # res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) - - # print(get_convergence(res)) - - # b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] - # b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] - - # predicted_values <- predict.provoc(res, b1, b2) - # print(predicted_values) - # ------- - # END OF TEMP - if (!"provoc" %in% class(provoc_obj)) { stop("Object must be of class 'provoc'") } - # b1_index <- match("b1", colnames(provoc_obj$variant)) - # b2_index <- match("b2", colnames(provoc_obj$variant)) - b1_index <- match(b1, provoc_obj$group) b2_index <- match(b2, provoc_obj$group) proportions <- as.numeric(provoc_obj$rho) variant_matrix <- attr(provoc_obj, "variant_matrix") - #variant_matrix <- get_varmat(provoc_obj) if (any(!rownames(variant_matrix) %in% provoc_obj$variant)) { stop("Variant matrix does not match variants in results") } results <- proportions[b1_index] %*% variant_matrix[b2_index, ] - #results <- proportions %*% variant_matrix[provoc_obj$variant, ] return(results) } diff --git a/man/autoplot.provoc.Rd b/man/autoplot.provoc.Rd index 40ac897..6293010 100644 --- a/man/autoplot.provoc.Rd +++ b/man/autoplot.provoc.Rd @@ -4,7 +4,7 @@ \alias{autoplot.provoc} \title{Plot a provoc object using ggplot2} \usage{ -autoplot.provoc(provoc_obj, date_col = NULL) +\method{autoplot}{provoc}(provoc_obj, date_col = NULL) } \arguments{ \item{provoc_obj}{The result of provoc(), or an object coerced via as.provoc().} diff --git a/man/find_mutation_column.Rd b/man/find_mutation_column.Rd new file mode 100644 index 0000000..2a3e090 --- /dev/null +++ b/man/find_mutation_column.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/provoc.R +\name{find_mutation_column} +\alias{find_mutation_column} +\title{Finds the right mutation column in mutation_defs.} +\usage{ +find_mutation_column(data, mutation_defs) +} +\arguments{ +\item{data}{Data frame containing count, coverage, and lineage columns} + +\item{mutation_defs}{Optional mutation definitions} +} +\value{ +A list of length 2 containing the possibly transposed mutation_defs and +the right mutation column. +} +\description{ +Checks the columns of mutation_defs and if not found in columns will check the rows +and transpose mutation_defs. +} diff --git a/man/parse_ivar.Rd b/man/parse_ivar.Rd new file mode 100644 index 0000000..cabb349 --- /dev/null +++ b/man/parse_ivar.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/parse_mutations.R +\name{parse_ivar} +\alias{parse_ivar} +\title{Parse Mutations from the ivar pipeline} +\usage{ +parse_ivar(ivar_output) +} +\arguments{ +\item{ivar_output}{A data frame imported from the ivar output. Must have columns POS and ALT} +} +\description{ +Parse Mutations from the ivar pipeline +} diff --git a/man/parse_one_ivar.Rd b/man/parse_one_ivar.Rd new file mode 100644 index 0000000..4313460 --- /dev/null +++ b/man/parse_one_ivar.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/parse_mutations.R +\name{parse_one_ivar} +\alias{parse_one_ivar} +\title{Parse mutations from ivar pipeline} +\usage{ +parse_one_ivar(pos, alt) +} +\arguments{ +\item{pos}{The position on the genome} + +\item{alt}{The alternate genotype} +} +\description{ +Parse mutations from ivar pipeline +} diff --git a/man/predict.provoc.Rd b/man/predict.provoc.Rd index 29bad91..dcf752d 100644 --- a/man/predict.provoc.Rd +++ b/man/predict.provoc.Rd @@ -17,6 +17,10 @@ \arguments{ \item{provoc_obj}{Named list with \code{proportions} and \code{variant_matrix}.} +\item{b1}{Name of the first sra sample group.} + +\item{b2}{Name of the second sra sample group.} + \item{newdata}{Not yet implemented.} \item{type}{Not yet implemented.} @@ -38,5 +42,12 @@ associated variant matrix, performs matrix multiplication to predict outcomes, and returns results in the same order as the original data. } \examples{ -predicted_results <- predict(provoc_obj) +library(provoc) +data("Baaijens") +Baaijens$mutation <- parse_mutations(Baaijens$label) +res <- provoc(formula = cbind(count, coverage) ~ B.1.1.7 + B.1.617.2, data = Baaijens) +b1 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[1], ] +b2 <- Baaijens[Baaijens$sra == unique(Baaijens$sra)[2], ] +predicted_values <- predict.provoc(res, b1, b2) +print(predicted_values) } diff --git a/man/provoc.Rd b/man/provoc.Rd index d540fe6..595dd16 100644 --- a/man/provoc.Rd +++ b/man/provoc.Rd @@ -10,7 +10,7 @@ provoc( mutation_defs = NULL, by = NULL, update_interval = 20, - verbose = TRUE, + verbose = FALSE, annihilate = FALSE ) }