Compound %RSDs

Relative standard deviations per compound or compound class are calculated and visualized for different sample groupings, either by sample type or for biological replicates (i.e. samples from the same group within variables indicated with parameter replicate_variables) as well as for all batches or each batch separately.

This analysis is based on the status-preprocessed dataset 1.

Red RSD% threshold lines in bar plots are currently only based on parameter filter_compound_qc_pool_max_rsd.

# Select dataset
biocrates <- datasets$discarded

# Gather replicate variables to use them as one variable for the calculations of separate
# group variables %RSDs (in comparison to interacting group variables %RSDs)
if (length(REPLICATE_VARIABLES) > 0) {
  biocrates_regrouped <- biocrates %>%
    filter(Sample.Type == SAMPLE_TYPE_BIOLOGICAL) %>%
    tidyr::gather(key = "Study.Variable", "Study.Group", REPLICATE_VARIABLES)
}
# Figure size calculations
calc_fig_sizes_rep_rsd <- function(bcdata, rep_variables, sample_types, unit, legends = 0,
                                   num_combinations = NULL, unit_factor = 1){
  num_variables <- length(rep_variables)
  num_units <- bcdata %>% pull(UQ(sym(unit))) %>% unique() %>% length()
  if (is.null(num_combinations)){
    num_combinations <- bcdata %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables)) %>% distinct() %>% nrow()
  }

  facet_parts <- max(unit_factor * num_units + 3.5 * num_variables,
                     3.5 * num_variables * 2.5)
  facet_parts_max <- 90

  num_columns <- min(max(floor(facet_parts_max / facet_parts), 1), num_combinations)
  num_rows <- ceiling(num_combinations/num_columns)

  fig_width_rep_rsd <- 7
  if (facet_parts_max / facet_parts < 1){
    fig_width_rep_rsd <- (1/7) + (6/7) * 7 / facet_parts_max * facet_parts
  }
  fig_height_rep_rsd <- max((num_rows + 1) * 1.2, 3.5) + legends

  return(list(h = fig_height_rep_rsd, w = fig_width_rep_rsd, c = num_columns))
}

Compounds {.tabset}

All batches {.tabset}

if (ENOUGH_REFERENCE_QC) {
  print(
    plot_rsd_versus(
  data = biocrates,
  threshold_bs = params$filter_compound_bs_min_rsd,
  threshold_qc = params$filter_compound_qc_ref_max_rsd,
  qc_type = ENV$SAMPLE_TYPE_REFERENCE_QC
    )
  )
}
if (ENOUGH_POOLED_QC) {
  print(
    plot_rsd_versus(
      data = biocrates,
      threshold_bs = params$filter_compound_bs_min_rsd,
      threshold_qc = params$filter_compound_qc_pool_max_rsd,
      qc_type = SAMPLE_TYPE_POOLED_QC
    )
  )
}
Sample types {.tabset}
rep_variables <- c("Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Compound")
Concentration
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c,
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables, sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per sample type"),
  show_type = "statistics"
)
Area
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c,
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd,
                       target = ENV$AREA)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables, sample_types = sample_types, target = ENV$AREA
  ),
  caption = paste0("Compound %RSDs per sample type"),
  show_type = "statistics"
)
### Biological replicates
if (length(REPLICATE_VARIABLES) > 0) {
  rep_variables <- REPLICATE_VARIABLES
  sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
  # Calculate figure size
  fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
    biocrates, rep_variables, sample_types, "Compound")
}
cat(paste0("\n\n##### Study groups (separate)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates_regrouped, rep_variables = "Study.Group",
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c,
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates_regrouped, rep_variables = "Study.Group", sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

cat(paste0("\n\n##### Study groups (interacting)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c,
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables, sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

if (all(c("BR", "TR") %in% names(biocrates))){
  cat(paste0("\n##### BRs (mean TRs)\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "mean",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "mean"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )

  cat(paste0("\n##### BRs (median TRs)\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "median",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "median"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )
}
### Technical replicates
if (length(REPLICATE_VARIABLES) > 0){
  if (all(c("BR", "TR") %in% names(biocrates))){
    rep_variables <- REPLICATE_VARIABLES
    sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
    num_combinations <- biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% ungroup() %>%
      select(one_of(rep_variables)) %>% distinct() %>%  nrow()
    max_tr_number <-  biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% summarize(Number = n())
    max_tr_number <- max(max_tr_number$Number)
    unit_factor <- max(1, max_tr_number/2)
    # Calculate figure size
    fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
      biocrates, c(rep_variables), sample_types, "Compound", legends = 1,
      num_combinations = num_combinations, unit_factor = unit_factor)
  }
}
if (all(c("BR", "TR") %in% names(biocrates))){
  cat(paste0("\n##### TRs only\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "only",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "only"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )
}

Per batch {.tabset}

if (ENOUGH_REFERENCE_QC) {
  print(
    plot_rsd_versus(
      data = biocrates,
      threshold_bs = params$filter_compound_bs_min_rsd,
      threshold_qc = params$filter_compound_qc_ref_max_rsd,
      qc_type = ENV$SAMPLE_TYPE_REFERENCE_QC,
      shape = "Batch"
    )
  )
}
if (ENOUGH_POOLED_QC) {
  print(
    plot_rsd_versus(
      data = biocrates,
      threshold_bs = params$filter_compound_bs_min_rsd,
      threshold_qc = params$filter_compound_qc_pool_max_rsd,
      qc_type = SAMPLE_TYPE_POOLED_QC,
      shape = "Batch"
    )
  )
}
Sample types
rep_variables <- c("Batch", "Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Compound")
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c,
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables, sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per sample type"),
  show_type = "statistics"
)
### Biological replicates
if (length(REPLICATE_VARIABLES) > 0){
  rep_variables <- c("Batch", REPLICATE_VARIABLES)
  sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
  # Calculate figure size
  fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
    biocrates, rep_variables, sample_types, "Compound")
}
cat(paste0("\n\n##### Study groups (separate)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates_regrouped,
                             rep_variables = c("Batch", "Study.Group"),
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c,
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates_regrouped,
    rep_variables = c("Batch", "Study.Group"),
    sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

cat(paste0("\n\n##### Study groups (interacting)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c,
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables, sample_types = sample_types
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

if (all(c("BR", "TR") %in% names(biocrates))){
  cat(paste0("\n##### BRs (mean TRs)\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "mean",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "mean"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )

  cat(paste0("\n##### BRs (median TRs)\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "median",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "median"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )
}
### Technical replicates
if (length(REPLICATE_VARIABLES) > 0){
  if (all(c("BR", "TR") %in% names(biocrates))){
    rep_variables <- REPLICATE_VARIABLES
    sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
    num_combinations <- biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% ungroup() %>%
      select(one_of(rep_variables)) %>% distinct() %>%  nrow()
    max_tr_number <-  biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% summarize(Number = n())
    max_tr_number <- max(max_tr_number$Number)
    unit_factor <- max(1, max_tr_number/2)
    # Calculate figure size
    fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
      biocrates, c(rep_variables), sample_types, "Compound", legends = 1,
      num_combinations = num_combinations, unit_factor = unit_factor)
  }
}
if (all(c("BR", "TR") %in% names(biocrates))){
  cat(paste0("\n##### TRs only\n"))
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "only",
                               facet_cols = fig_sizes_rep_rsd$c,
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "only"
    ),
    caption = paste0("Compound %RSDs per study groups"),
    show_type = "statistics"
  )
}

Class medians {.tabset}

All batches {.tabset}

Sample types
rep_variables <- c("Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Class")
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Class"
  ),
  caption = paste0("Median class %RSDs per sample type"),
  show_type = "statistics"
)
##### Biological replicates
if (length(REPLICATE_VARIABLES) > 0){
  rep_variables <- REPLICATE_VARIABLES
  sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
  # Calculate figure size
  fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
    biocrates, rep_variables, sample_types, "Class")
}
cat(paste0("\n\n##### Study groups (separate)\n"))
print(plot_bio_replicate_rsd(
  bcdata = biocrates_regrouped,
  rep_variables = c("Study.Group"),
  sample_types = sample_types,
  summarize = "Class",
  facet_cols = fig_sizes_rep_rsd$c,
  rsd_threshold = params$filter_compound_bs_min_rsd)
)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates_regrouped,
    rep_variables = c("Study.Group"),
    sample_types = sample_types,
    summarize = "Class"
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

cat(paste0("\n\n##### Study groups (interacting)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Class"
  ),
  caption = paste0("Median class %RSDs per study groups"),
  show_type = "statistics"
)

if (all(c("BR", "TR") %in% names(biocrates))){
  cat("\n##### BRs (mean TRs)\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "mean",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "mean", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )

  cat("\n##### BRs (median TRs)\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "median",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "median", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )
}
### Technical replicates
if (length(REPLICATE_VARIABLES) > 0){
  if (all(c("BR", "TR") %in% names(biocrates))){
    rep_variables <- REPLICATE_VARIABLES
    sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
    num_combinations <- biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% ungroup() %>%
      select(one_of(rep_variables)) %>% distinct() %>%  nrow()
    max_tr_number <-  biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% summarize(Number = n())
    max_tr_number <- max(max_tr_number$Number)
    unit_factor <- max(1, max_tr_number/2)
    # Calculate figure size
    fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
      biocrates, c(rep_variables), sample_types, "Class", legends = 1,
      num_combinations = num_combinations, unit_factor = unit_factor)
  }
}
if (all(c("BR", "TR") %in% names(biocrates))){
  cat("\n##### TRs only\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "only",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "only", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )
}

Per batch {.tabset}

Sample types
rep_variables <- c("Batch", "Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Class")
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Class"
  ),
  caption = paste0("Median class %RSDs per sample type"),
  show_type = "statistics"
)
##### Biological replicates
if (length(REPLICATE_VARIABLES) > 0){
  rep_variables <- c("Batch", REPLICATE_VARIABLES)
  sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
  # Calculate figure size
  fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
    biocrates, rep_variables, sample_types, "Class")
}
cat(paste0("\n\n##### Study groups (separate)\n"))
print(plot_bio_replicate_rsd(
  bcdata = biocrates_regrouped,
  rep_variables = c("Batch", "Study.Group"),
  sample_types = sample_types,
  summarize = "Class",
  facet_cols = fig_sizes_rep_rsd$c,
  rsd_threshold = params$filter_compound_bs_min_rsd)
)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates_regrouped,
    rep_variables = c("Batch", "Study.Group"),
    sample_types = sample_types,
    summarize = "Class"
  ),
  caption = paste0("Compound %RSDs per study groups"),
  show_type = "statistics"
)

cat(paste0("\n\n##### Study groups (interacting)\n"))
print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                             sample_types = sample_types,
                             facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                             rsd_threshold = params$filter_compound_bs_min_rsd))
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Class"
  ),
  caption = paste0("Median class %RSDs per study groups"),
  show_type = "statistics"
)

if (all(c("BR", "TR") %in% names(biocrates))){
  cat("\n##### BRs (mean TRs)\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "mean",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "mean", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )

  cat("\n##### BRs (median TRs)\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "median",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "median", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )
}
### Technical replicates
if (length(REPLICATE_VARIABLES) > 0){
  if (all(c("BR", "TR") %in% names(biocrates))){
    rep_variables <- REPLICATE_VARIABLES
    sample_types <- c(SAMPLE_TYPE_BIOLOGICAL)
    num_combinations <- biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% ungroup() %>%
      select(one_of(rep_variables)) %>% distinct() %>%  nrow()
    max_tr_number <-  biocrates %>%
      filter(Sample.Type %in% sample_types) %>%
      select(one_of(rep_variables), BR, TR) %>% distinct() %>%
      group_by_at(vars(one_of(rep_variables), BR)) %>%
      filter(all(n() > 1)) %>% summarize(Number = n())
    max_tr_number <- max(max_tr_number$Number)
    unit_factor <- max(1, max_tr_number/2)
    # Calculate figure size
    fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
      biocrates, c(rep_variables), sample_types, "Class", legends = 1,
      num_combinations = num_combinations, unit_factor = unit_factor)
  }
}
if (all(c("BR", "TR") %in% names(biocrates))){
  cat("\n##### TRs only\n")
  print(plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                               sample_types = sample_types, tec_reps_use = "only",
                               facet_cols = fig_sizes_rep_rsd$c, summarize = "Class",
                               rsd_threshold = params$filter_compound_bs_min_rsd))
  easy_datatable(
    data = table_bio_replicate_rsd(
      bcdata = biocrates, rep_variables = rep_variables,
      sample_types = sample_types, tec_reps_use = "only", summarize = "Class"
    ),
    caption = paste0("Median class %RSDs per study groups"),
    show_type = "statistics"
  )
}

Sample type medians {.tabset}

All batches {.tabset}

rep_variables <- c("Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Sample.Type")
Concentration
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c, summarize = "Sample.Type",
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Sample.Type"
  ),
  caption = paste0("Median sample type %RSDs"),
  show_type = "statistics"
)
Area
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c, summarize = "Sample.Type",
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd,
                       target = ENV$AREA)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Sample.Type",
    target = ENV$AREA
  ),
  caption = paste0("Median sample type %RSDs"),
  show_type = "statistics"
)

Per batch

rep_variables <- c("Batch", "Sample.Type")
sample_types <- c(SAMPLE_TYPE_POOLED_QC, ENV$SAMPLE_TYPE_REFERENCE_QC, SAMPLE_TYPE_BIOLOGICAL)
# Calculate figure size
fig_sizes_rep_rsd <- calc_fig_sizes_rep_rsd(
  biocrates, rep_variables, sample_types, "Sample.Type")
plot_bio_replicate_rsd(bcdata = biocrates, rep_variables = rep_variables,
                       sample_types = sample_types,
                       facet_cols = fig_sizes_rep_rsd$c, summarize = "Sample.Type",
                       rsd_threshold = params$filter_compound_qc_pool_max_rsd)
easy_datatable(
  data = table_bio_replicate_rsd(
    bcdata = biocrates, rep_variables = rep_variables,
    sample_types = sample_types, summarize = "Sample.Type"
  ),
  caption = paste0("Median sample type %RSDs"),
  show_type = "statistics"
)
# Remove "biocrates" dataset to ensure following sections select the dataset they need
rm(biocrates)


bihealth/metaquac documentation built on Aug. 7, 2021, 8:40 a.m.