Skip to content

Commit

Permalink
introducing tests for factorial designs
Browse files Browse the repository at this point in the history
  • Loading branch information
wolski committed Apr 25, 2024
1 parent 728829e commit d109af2
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 36 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export(summarise_QValues)
export(summarize_hierarchy)
export(summarize_stats)
export(summarize_stats_all)
export(summarize_stats_factors)
export(summarize_stats_quantiles)
export(summary_ROPECA_median_p.scaled)
export(table_facade)
Expand Down
24 changes: 23 additions & 1 deletion R/ContrastsSimpleImpute.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,38 @@
#' lProt$rename_response("transformedIntensity")
#'
#' Contr <- c("dil.b_vs_a" = "group_A - group_Ctrl")
#' #ContrastsMissing$debug("get_contrasts")
#' csi <- ContrastsMissing$new(lProt, contrasts = Contr)
#' csi$get_contrast_sides()
#'
#' res <- csi$get_contrasts()
#'
#' stopifnot(nrow(res) == (protIntensity$protein_Id |> unique() |> length()))
#' res$contrast |> table()
#' stopifnot((res$p.value |> is.na() |> sum()) == 0)
#' plot(res$diff, -log10(res$p.value), pch = ".")
#' csi$column_description()
#' x<- csi$get_Plotter()
#' p <- x$volcano()
#' pdf(file = NULL)
#' print(p)
#' dev.off()
#'
#' dd <- prolfqua::sim_lfq_data_protein_2Factor_config(Nprot = 100,weight_missing = 0.1)
#'
#' Contrasts <- c("c1" = "TreatmentA - TreatmentB",
#' "C2" = "BackgroundX- BackgroundZ",
#' "c3" = "`TreatmentA:BackgroundX` - `TreatmentA:BackgroundZ`",
#' "c4" = "`TreatmentB:BackgroundX` - `TreatmentB:BackgroundZ`"
#' )
#' lProt <- LFQData$new(dd$data, dd$config)
#' lProt$rename_response("transformedIntensity")
#'
#' csi <- ContrastsMissing$new(lProt, contrasts = Contrasts)
#' res <- csi$get_contrasts()
#' pl <- csi$get_Plotter()
#' pdf(file = NULL)
#' pl$volcano()
#' dev.off()
ContrastsMissing <- R6::R6Class(
"ContrastsMissing",
inherit = ContrastsInterface,
Expand Down
6 changes: 4 additions & 2 deletions R/simulate_LFQ_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ sim_lfq_data_protein_config <- function(Nprot = 10, with_missing = TRUE, weight_
#' @param seed seed for reproducibility, if NULL no seed is set.
#' @export
#' @examples
#' undebug(sim_lfq_data_protein_2Factor_config)
#' x <- sim_lfq_data_protein_2Factor_config()
#' stopifnot("data.frame" %in% class(x$data))
#' stopifnot("AnalysisConfiguration" %in% class(x$config))
Expand All @@ -228,11 +229,11 @@ sim_lfq_data_protein_2Factor_config <- function(Nprot = 10,
if (!is.null(seed)) {
set.seed(seed)
}
res <- sim_lfq_data(Nprot = 10, PEPTIDE = FALSE,
res <- sim_lfq_data(Nprot = Nprot, PEPTIDE = FALSE,
fc = list(A = c(D = -2, U = 2, N = 0), B = c(D = 1, U = -4), C = c(D = -1, U = -4)),
prop = list(A = c(D = 10, U = 10), B = c(D = 5, U = 20), C = c(D = 15, U = 25)))
res <- res |> mutate(Treatment = case_when(group %in% c("Ctrl", "A") ~ "A", TRUE ~ "B"))
res <- res |> mutate(Background = case_when(group %in% c("Ctrl", "C") ~ "Z", TRUE ~ "X"))
data <- res |> mutate(Background = case_when(group %in% c("Ctrl", "C") ~ "Z", TRUE ~ "X"))
if (with_missing) {
data <- data[!which_missing(data$abundance,weight_missing = weight_missing),]
}
Expand All @@ -244,6 +245,7 @@ sim_lfq_data_protein_2Factor_config <- function(Nprot = 10,
atable$nr_children = "nr_peptides"
atable$factors["Treatment"] = "Treatment"
atable$factors["Background"] = "Background"
atable$factorDepth <- 2
atable$hierarchy[["protein_Id"]] = c("proteinID", "idtype2")
atable$set_response("abundance")

Expand Down
20 changes: 17 additions & 3 deletions R/tidyMS_missigness_V2.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@
#' mh$impute_weighted_lod()
#' mh$impute_lod()
#' mh$get_poolvar()
#' mh$get_contrast_estimates(Contrasts)
#' bb <- mh$get_contrast_estimates(Contrasts)
#' mh$get_contrasts(Contrasts)
#'
#' dd <- prolfqua::sim_lfq_data_protein_2Factor_config(Nprot = 100,weight_missing = 0.1)
#'
#' Contrasts <- c("c1" = "TreatmentA - TreatmentB",
#' "C2" = "BackgroundX- BackgroundZ",
#' "c3" = "`TreatmentA:BackgroundX` - `TreatmentA:BackgroundZ`",
#' "c4" = "`TreatmentB:BackgroundX` - `TreatmentB:BackgroundZ`"
#' )
#' mh <- prolfqua::MissingHelpers$new(dd$data, dd$config, prob = 0.8,weighted = TRUE)
#' mh$get_stats()$interaction |> table()
#' mh$get_contrast_estimates(Contrasts)
#'
MissingHelpers <- R6::R6Class(
"MissingHelpers",

Expand Down Expand Up @@ -44,8 +56,7 @@ MissingHelpers <- R6::R6Class(
},
get_stats = function(){
if (is.null(self$stats)) {
self$stats = prolfqua::summarize_stats(self$data, self$config)
self$stats = prolfqua::make_interaction_column(self$stats, columns = self$config$table$factor_keys_depth(), sep = ":")
self$stats = prolfqua::summarize_stats_factors(self$data, self$config)
}
return(self$stats)
},
Expand Down Expand Up @@ -90,6 +101,9 @@ MissingHelpers <- R6::R6Class(
pooled <- pooled |> mutate(df = ifelse(df == 0, 1, df))
return(pooled)
},
#' @description
#' get contrast estimates
#' @param Contrasts named array with contrasts
get_contrast_estimates = function(
Contrasts
){
Expand Down
84 changes: 59 additions & 25 deletions R/tidyMS_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,28 @@ poolvar <- function(res1, config, method = c("V1","V2")){
#'
#' res1 <- summarize_stats(data, config)
#'
summarize_stats <- function(pdata, config){
#' res2 <- prolfqua::sim_lfq_data_protein_2Factor_config()
#' res2$config$table$factorDepth <- 2
#' stats <- summarize_stats(res2$data, res2$config)
#' stats <- prolfqua::make_interaction_column(stats, columns = res2$config$table$factor_keys_depth(), sep = ":")
#' stopifnot(nrow(stats) == 40)
#'
#' stats <- summarize_stats(res2$data, res2$config, factor_key = res2$config$table$factor_keys()[1])
#' stats <- prolfqua::make_interaction_column(stats, columns = res2$config$table$factor_keys()[1], sep = ":")
#' stopifnot(nrow(stats) == 20)
#' stats <- summarize_stats(res2$data, res2$config, factor_key = res2$config$table$factor_keys()[2])
#' stats <- prolfqua::make_interaction_column(stats, columns = res2$config$table$factor_keys()[2], sep = ":")
#' stopifnot(nrow(stats) == 20)
#'
#' stats <- summarize_stats(res2$data, res2$config, factor_key = NULL)
#' stopifnot(nrow(stats) == 10)
#'
summarize_stats <- function(pdata, config, factor_key = config$table$factor_keys_depth()){
print(factor_key)
pdata <- complete_cases(pdata, config)
intsym <- sym(config$table$get_response())
hierarchyFactor <- pdata |>
dplyr::group_by(!!!syms( c(config$table$hierarchy_keys(), config$table$factor_keys_depth()) )) |>
dplyr::group_by(!!!syms( c(config$table$hierarchy_keys(), factor_key) )) |>
dplyr::summarize(nrReplicates = dplyr::n(),
nrMeasured = sum(!is.na(!!intsym)),
nrNAs = sum(is.na(!!intsym)),
Expand All @@ -177,13 +194,46 @@ summarize_stats <- function(pdata, config){
.groups = "drop_last") |> dplyr::ungroup()

hierarchyFactor <- hierarchyFactor |>
dplyr::mutate(dplyr::across(config$table$factor_keys_depth(), as.character))
dplyr::mutate(dplyr::across(all_of(factor_key), as.character))
if (config$table$is_response_transformed == FALSE) {
hierarchyFactor |> dplyr::mutate(CV = sd/meanAbundance * 100) -> hierarchyFactor
hierarchyFactor <- hierarchyFactor |> dplyr::mutate(CV = sd/meanAbundance * 100)
}
if (is.null(factor_key)) {
hierarchyFactor <- dplyr::mutate(hierarchyFactor, !!config$table$factor_keys()[1] := "All")
}
return(ungroup(hierarchyFactor))
}


#' compute var sd etc for all factor levels
#'
#' @export
#' @examples
#' # example code
#' res2 <- prolfqua::sim_lfq_data_protein_2Factor_config()
#' xx <- summarize_stats_factors(res2$data, res2$config)
#' stopifnot(nrow(xx) == 80)
#'
summarize_stats_factors <- function(pdata, config){
fac_res <- list()
stats <- summarize_stats(
pdata,
config)
fac_res[["interaction"]] <- prolfqua::make_interaction_column(stats, columns = config$table$factor_keys_depth(),sep = ":")

if (config$table$factorDepth > 1 ) { # if 1 only then done
for (factor in config$table$factor_keys_depth()) {
stats <- summarize_stats(
pdata,
config,factor_key = factor)
fac_res[[factor]] <- prolfqua::make_interaction_column(stats, columns = factor, sep = ":")
}
}
intfact <- dplyr::bind_rows(fac_res)
return(intfact)
}


#' Compute mean, sd, and CV for e.g. Peptides, or proteins, for all samples.
#'
#' @param pdata data.frame
Expand All @@ -201,28 +251,13 @@ summarize_stats <- function(pdata, config){
#' res1 <- summarize_stats_all(bb$data, bb$config)
#'
#' stopifnot((res1 |> dplyr::filter(group_ == "All") |> nrow()) == (res1 |> nrow()))
#'
summarize_stats_all <- function(pdata, config){
pdata <- complete_cases(pdata, config)
intsym <- sym(config$table$get_response())
hierarchy <- pdata |>
dplyr::group_by(!!!syms( config$table$hierarchy_keys() )) |>
dplyr::summarize(nrReplicates = dplyr::n(),
nrMeasured = sum(!is.na(!!intsym)),
sd = sd(!!intsym,na.rm = TRUE),
var = sd(!!intsym,na.rm = TRUE),
meanAbundance = mean(!!intsym,na.rm = TRUE),
medianAbundance = median(!!intsym, na.rm = TRUE),
.groups = "drop_last") |> dplyr::ungroup()

hierarchy <- dplyr::mutate(hierarchy, !!config$table$factor_keys()[1] := "All")
hierarchyFactor <- hierarchy
if (config$table$is_response_transformed == FALSE) {
hierarchyFactor |> dplyr::mutate(CV = sd/meanAbundance * 100) -> hierarchyFactor
}
return(ungroup(hierarchyFactor))
#' res2 <- prolfqua::sim_lfq_data_protein_2Factor_config()
#' resSt <- summarize_stats_all(res2$data, res2$config)
summarize_stats_all <- function(pdata, config) {
summarize_stats(pdata, config, factor_key = NULL)
}


#' summarize stats output (compute quantiles)
#' @param stats_res result of running `summarize_stats`
#' @param config AnalysisConfiguration
Expand All @@ -247,7 +282,6 @@ summarize_stats_all <- function(pdata, config){
#' stats_res <- summarize_stats(data, config)
#' sq <- summarize_stats_quantiles(stats_res, config)
#' sq <- summarize_stats_quantiles(stats_res, config, stats = "sd")
#'
#' stats_res <- summarize_stats(data, config)
#' xx <- summarize_stats_quantiles(stats_res, config, probs = seq(0,1,by = 0.1))
#' ggplot2::ggplot(xx$long, aes(x = probs, y = quantiles, color = group_)) + geom_line() + geom_point()
Expand Down
24 changes: 23 additions & 1 deletion man/ContrastsMissing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion man/MissingHelpers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/sim_lfq_data_protein_2Factor_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions man/summarize_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/summarize_stats_factors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d109af2

Please sign in to comment.