Skip to content

Commit

Permalink
add child computation
Browse files Browse the repository at this point in the history
  • Loading branch information
wolski committed Apr 8, 2024
1 parent c1150db commit b99b95d
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 64 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export(names_to_matrix)
export(normalize_log2_robscale)
export(nr_B_in_A)
export(nr_B_in_A_per_sample)
export(nr_obs_hierarchy)
export(nr_obs_experiment)
export(nr_obs_sample)
export(old2new)
export(pairs_smooth)
Expand Down
22 changes: 15 additions & 7 deletions R/AnalysisConfiguration.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,16 @@ hierarchy_counts <- function(pdata, config){
#'
#' config <- bb$config
#' data <- bb$data
#' res <- hierarchy_counts_sample(data, config)
#' res <- hierarchy_counts_sample(data, config, nr_children = 1)
#' res()
#' x <- res("long")
#' res("plot")
#'
#' # filters on peptide level
#' res <- hierarchy_counts_sample(data, config, nr_children = 2)
#' res()
#' x2 <- res("long")
#' res("plot")
#' # filters on protein level based on peptide count
#' bb <- prolfqua::sim_lfq_data_protein_config()
#' res <- hierarchy_counts_sample(bb$data, bb$config, nr_children = 2)
#' x1 <- res()
Expand All @@ -414,15 +415,23 @@ hierarchy_counts <- function(pdata, config){
#' x2 <- res()
#' res("long")
#' res("plot")
#' dplyr::inner_join(x1,
#' x2,
#' by = c("isotopeLabel","sampleName"),
#' suffix =c("2","1"))
#' x1$nr_children <- 2
#' x2$nr_children <- 1
#' xl <- dplyr::bind_rows(x1, x2)
#'
#'
#'
#'
#' xl$nr_children |> table()
#' nudgeval <- -mean(xl$protein_Id) * 0.05
#' ggplot(xl, aes(x = sampleName, y = protein_Id, fill = as.character(nr_children)) ) +
#' geom_bar(stat = "identity", position = position_dodge())
#'
hierarchy_counts_sample <- function(
pdata,
configuration,
nr_children = 1) {

hierarchy <- configuration$table$hierarchy_keys()
summary <- pdata |> dplyr::filter(!is.na(!!rlang::sym(configuration$table$get_response() )),
!!rlang::sym(configuration$table$nr_children ) >= .env$nr_children) |>
Expand All @@ -440,7 +449,6 @@ hierarchy_counts_sample <- function(
if (value == "long") {
return(long)
}else if (value == "plot" & nrow(long) > 0) {

nudgeval <- -mean(long$nr) * 0.05
# TODO(WEW) check potential problem with sampleName
ggplot2::ggplot(long, ggplot2::aes(x = !!rlang::sym(configuration$table$sampleName), y = .data$nr)) +
Expand Down
2 changes: 2 additions & 0 deletions R/Contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Contrasts <- R6::R6Class(
contrast_result = NULL,
#' @field global use a global linear function (determined by get_linfct)
global = TRUE,
#' @field protein_annot holds protein annotation
protein_annot = NULL,
#' @description
#' initialize
#' create Contrast
Expand Down
6 changes: 5 additions & 1 deletion R/ContrastsPlotter.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ ContrastsPlotter <- R6::R6Class(
fcthresh = 1,
#' @field avg.abundance name of column containing avg abundance values.
avg.abundance = character(),
#' @field avg.abundance name of column containing avg abundance values.
protein_annot = NULL,
#' @description
#' create Crontrast_Plotter
#' @param contrastDF frame with contrast data
Expand All @@ -98,7 +100,8 @@ ContrastsPlotter <- R6::R6Class(
modelName = "modelName",
diff = "diff",
contrast = "contrast",
avg.abundance = "avgAbd"
avg.abundance = "avgAbd",
protein_annot = NULL
){
self$contrastDF <- tidyr::unite(
contrastDF,
Expand All @@ -113,6 +116,7 @@ ContrastsPlotter <- R6::R6Class(
self$fcthresh = fcthresh
self$contrast = contrast
self$avg.abundance = avg.abundance
self$protein_annot = protein_annot
},
#' @description
#' plot histogram of selected scores (e.g. p-value, FDR, t-statistics)
Expand Down
15 changes: 8 additions & 7 deletions R/ProteinAnnotation.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
#' @examples
#'
#' istar <-prolfqua::sim_lfq_data_peptide_config()
#' data <- istar$data
#'
#'
#' lfqdata <- LFQData$new(data, istar$config)
#' lfqdata <- LFQData$new(istar$data, istar$config)
#' pannot <- ProteinAnnotation$new( lfqdata )
#' pannot$annotate_decoys()
#' pannot$annotate_contaminants()
#' dd <- pannot$clean()
#' tmp <- lfqdata$get_subset(dd)
#'
#' pannot$row_annot
#'
ProteinAnnotation <-
R6::R6Class("ProteinAnnotation",
Expand Down Expand Up @@ -49,8 +46,12 @@ ProteinAnnotation <-
} else {
self$row_annot <- distinct(select(lfqdata$data, self$pID))
}


if (!self$nr_peptides %in% colnames(row_annot) ) {
self$row_annot <- inner_join(
self$row_annot,
nr_obs_experiment(lfqdata$data, lfqdata$config, name_nr_child = self$nr_peptides),
by = self$pID)
}
},
#' @description
#' annotate rev sequences
Expand Down
25 changes: 17 additions & 8 deletions R/tidyMS_R6_TransitionCorrelations.R
Original file line number Diff line number Diff line change
Expand Up @@ -709,28 +709,37 @@ nr_obs_sample <- function(data, config, new_child = config$table$nr_children){
#' Aggregates e.g. protein abundances from peptide abundances
#'
#' @export
#' @param data tidy data
#' @param config prolfqua config
#' @param from_children compute from existing child stats
#' @param name_nr_child how to name column
#' @examples
#' dd <- prolfqua::sim_lfq_data_peptide_config()
#'
#' xd <- nr_obs_hierarchy(dd$data, dd$config)
#' xd <- nr_obs_experiment(dd$data, dd$config)
#' xd
#' xd <- nr_obs_experiment(dd$data, dd$config, from_children = FALSE)
#' xd
#' dp <- prolfqua::sim_lfq_data_protein_config()
#' debug(nr_obs_hierarchy)
#' nr_obs_sample(dp$data, dp$config)
#' xd <- nr_obs_hierarchy(dp$data, dp$config)
#'
#' dp <- prolfqua::sim_lfq_data_protein_config()
#' undebug(nr_obs_experiment)
#' nr_obs_experiment(dp$data, dp$config)
#' nr_obs_experiment(dp$data, dp$config, from_children = FALSE)
#'
nr_obs_hierarchy <- function(data, config, from_children = TRUE , name_nr_child = "nr_child_exp"){
nr_obs_experiment <- function(data, config, from_children = TRUE,
name_nr_child = "nr_child_exp"){
tb <- config$table
if (!from_children & (tb$hierarchyDepth < length(tb$hierarchy_keys())) ) {
xq <- data |> tidyr::select(tb$hierarchy_keys()) |>
xq <- data |> dplyr::select(tb$hierarchy_keys()) |>
distinct() |>
dplyr::group_by(!!sym(tb$hierarchy_keys_depth())) |>
dplyr::summarize(!!name_nr_child := dplyr::n(), .groups = "drop")
return(xq)
} else {
xz <- nr_obs_sample(data,config)
xz <- x |> group_by(!!sym(tb$hierarchy_keys_depth())) |>
xz <- xz |> group_by(!!sym(tb$hierarchy_keys_depth())) |>
summarize(!!name_nr_child := max(!!sym(tb$nr_children)), .groups = "drop")
return(xz)
}
}

Expand Down
2 changes: 2 additions & 0 deletions man/Contrasts.Rd

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

5 changes: 4 additions & 1 deletion man/ContrastsPlotter.Rd

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

7 changes: 2 additions & 5 deletions man/ProteinAnnotation.Rd

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

20 changes: 14 additions & 6 deletions man/hierarchy_counts_sample.Rd

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

39 changes: 39 additions & 0 deletions man/nr_obs_experiment.Rd

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

28 changes: 0 additions & 28 deletions man/nr_obs_hierarchy.Rd

This file was deleted.

0 comments on commit b99b95d

Please sign in to comment.