-
Notifications
You must be signed in to change notification settings - Fork 5
139 feat/blq options #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
139 feat/blq options #491
Changes from all commits
556e9a7
15b357b
9884b46
8dab1e1
2a7f54a
5b33ac1
61b87d0
a643f94
39356c1
f6cf248
f6b2c8a
5e3eee3
b745304
9c898ec
88ccadb
668b377
b5816f3
6fdd4bc
430265c
ab3ccdc
7f72c3c
18b8f33
897a84e
a99999e
a90bc68
9ae60bc
05641d6
3fc8acd
f8e97de
d626406
7a17abd
197db3b
1c20017
cfcfea1
1c5d37d
fbc290a
0e75210
22b4425
040f106
6c1b9f0
07f1a79
ddd9f60
049ac6c
5f2dbf0
1e08be0
2e46694
9940083
5c49d11
b3ba671
7e6ec73
8716335
7cab948
14c392a
b3edf4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,8 +53,10 @@ format_pkncadata_intervals <- function(pknca_conc, | |
| dose_groups <- unname(unlist(pknca_dose$columns$groups)) | ||
| time_column <- pknca_dose$columns$time | ||
| # Obtain all possible pknca parameters | ||
| params <- setdiff(names(PKNCA::get.interval.cols()), | ||
| c("start", "end")) | ||
| params <- setdiff( | ||
| names(PKNCA::get.interval.cols()), | ||
| c("start", "end") | ||
| ) | ||
|
|
||
| # Select conc data and for time column give priority to non-predose samples | ||
| sub_pknca_conc <- pknca_conc$data %>% | ||
|
|
@@ -67,29 +69,32 @@ format_pkncadata_intervals <- function(pknca_conc, | |
| group_by(!!!syms(dose_groups)) %>% | ||
| mutate(is_one_dose = length(unique(DOSNOA)) == 1) %>% | ||
| ungroup() %>% | ||
| select(any_of(c(dose_groups, | ||
| time_column, "DOSNOA", "is_one_dose"))) | ||
| select(any_of(c( | ||
| dose_groups, | ||
| time_column, "DOSNOA", "is_one_dose" | ||
| ))) | ||
|
|
||
| # Based on dose times create a data frame with start and end times | ||
| dose_intervals <- left_join(sub_pknca_dose, | ||
| sub_pknca_conc, | ||
| by = intersect(names(sub_pknca_dose), c(conc_groups, "DOSNOA")), | ||
| relationship = "many-to-many") %>% | ||
|
|
||
| sub_pknca_conc, | ||
| by = intersect(names(sub_pknca_dose), c(conc_groups, "DOSNOA")), | ||
| relationship = "many-to-many" | ||
| ) %>% | ||
| # Pick 1 per concentration group and dose number | ||
| group_by(!!!syms(dose_groups), DOSNOA) %>% | ||
| mutate(max_end = max(ARRLT, na.rm = TRUE)) %>% # calculate max end time for Dose group | ||
| filter(ARRLT >= 0) %>% # filter out negative ARRLT values | ||
| group_by(!!!syms(c(conc_groups, "DOSNOA"))) %>% | ||
| slice(1) %>% # slice one row per conc group | ||
| ungroup() %>% | ||
|
|
||
| # Make start from last dose (pknca_dose) or first concentration (pknca_conc) | ||
| mutate(start = if (start_from_last_dose) !!sym(time_column) | ||
| else !!sym(time_column) + !!sym("ARRLT")) %>% | ||
| mutate(start = if (start_from_last_dose) { | ||
| !!sym(time_column) | ||
| } else { | ||
| !!sym(time_column) + !!sym("ARRLT") | ||
| }) %>% | ||
| group_by(!!!syms(conc_groups)) %>% | ||
| arrange(start) %>% | ||
|
|
||
| # Make end based on next dose time (if no more, TRTRINT or last NFRLT) | ||
| mutate(end = if (has_trtrint) { | ||
| case_when( | ||
|
|
@@ -104,17 +109,16 @@ format_pkncadata_intervals <- function(pknca_conc, | |
| is_one_dose ~ Inf, | ||
| TRUE ~ start + max_end | ||
| ) | ||
| } | ||
| ) %>% | ||
| }) %>% | ||
| ungroup() %>% | ||
| select(any_of(c("start", "end", conc_groups, | ||
| "ATPTREF", "DOSNOA", "VOLUME"))) %>% | ||
|
|
||
| select(any_of(c( | ||
| "start", "end", conc_groups, | ||
| "ATPTREF", "DOSNOA", "VOLUME" | ||
| ))) %>% | ||
| # Create logical columns with only TRUE for the NCA parameters requested by the user | ||
| mutate(!!!setNames(rep(FALSE, length(params)), params)) %>% | ||
| # Identify the intervals as the base ones for the NCA analysis | ||
| mutate(type_interval = "main") | ||
|
|
||
| } | ||
|
|
||
| #' Update an intervals data frame with user-selected parameters by study type | ||
|
|
@@ -124,12 +128,22 @@ format_pkncadata_intervals <- function(pknca_conc, | |
| #' @param study_types_df A data frame mapping analysis profiles to their study type. | ||
| #' @param auc_data A data frame containing partial AUC ranges. | ||
| #' @param impute Logical indicating whether to impute start values for parameters. | ||
| #' @param blq_imputation_rule A list defining the Below Limit of Quantification (BLQ) | ||
| #' imputation rule using PKNCA format. The list should either contain three elements named: | ||
| #' `first`, `middle`, and `last` or two elements named `before.tmax` and `after.tmax`. | ||
| #' Each element can be a numeric value (substituting the BLQ value), or a string such as | ||
| #' `"drop"` (ignores the value) or `"keep"` (keeps the value as 0). Default is NULL, | ||
| #' which does not specify any BLQ imputation in any interval. | ||
| #' | ||
| #' @returns An updated PKNCAdata object with parameter intervals based on user selections. | ||
| #' | ||
| update_main_intervals <- function(data, parameter_selections, | ||
| study_types_df, auc_data, impute = TRUE) { | ||
|
|
||
| update_main_intervals <- function( | ||
| data, | ||
| parameter_selections, | ||
| study_types_df, auc_data, | ||
| impute = TRUE, | ||
| blq_imputation_rule = NULL | ||
| ) { | ||
| all_pknca_params <- setdiff(names(PKNCA::get.interval.cols()), c("start", "end")) | ||
|
|
||
| # Determine the grouping columns from the study_types_df | ||
|
|
@@ -189,9 +203,32 @@ update_main_intervals <- function(data, parameter_selections, | |
|
|
||
| # Impute start values if requested | ||
| if (impute) { | ||
| data <- apply_imputation(data) | ||
| data <- create_start_impute(data) | ||
| } | ||
|
|
||
| ############################################ | ||
| # Define a BLQ imputation method for PKNCA | ||
| # and apply it only for non-observational parameters | ||
|
|
||
| if (!is.null(blq_imputation_rule)) { | ||
| PKNCA_impute_method_blq <<- function(conc.group, time.group, ...) { # nolint | ||
| PKNCA::clean.conc.blq(conc = conc.group, time = time.group, conc.blq = blq_imputation_rule) | ||
| } | ||
Gero1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| data$intervals <- data$intervals %>% | ||
| mutate( | ||
| impute = ifelse( | ||
| is.na(impute) | impute == "", | ||
Gero1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "blq", | ||
| paste0("blq, ", impute) | ||
| ) | ||
| ) | ||
| } | ||
Gero1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ############################################ | ||
| # Remove any imputation from the observational parameters | ||
| data <- rm_impute_obs_params(data, metadata_nca_parameters) | ||
|
|
||
| data | ||
| } | ||
|
|
||
|
|
@@ -201,25 +238,34 @@ update_main_intervals <- function(data, parameter_selections, | |
| #' selectively removing imputation for parameters that are not dependent on AUC. | ||
|
||
| #' | ||
| #' @param data A PKNCAdata object. | ||
| #' @param nca_parameters A dataset containing the mapping between PKNCA terms and CDISC terms. | ||
| #' @param metadata_nca_parameters A data frame mapping PKNCA parameters (`PKNCA`) | ||
| #' and information on their parameter dependencies ('Depends'). | ||
| #' @returns A PKNCAdata object with imputation rules applied. | ||
| #' @import dplyr | ||
| #' | ||
| apply_imputation <- function(data, nca_parameters = metadata_nca_parameters) { | ||
| data <- create_start_impute(data) | ||
|
|
||
| rm_impute_obs_params <- function(data, metadata_nca_parameters = metadata_nca_parameters) { | ||
| # Don't impute parameters that are not AUC dependent | ||
| params_auc_dep <- nca_parameters %>% | ||
| params_auc_dep <- metadata_nca_parameters %>% | ||
| filter(grepl("auc|aumc", PKNCA) | grepl("auc", Depends)) %>% | ||
| pull(PKNCA) | ||
|
|
||
| params_not_to_impute <- nca_parameters %>% | ||
| filter(!grepl("auc|aumc", PKNCA), | ||
| !grepl(paste0(params_auc_dep, collapse = "|"), Depends)) %>% | ||
| params_not_to_impute <- metadata_nca_parameters %>% | ||
| filter( | ||
| !grepl("auc|aumc", PKNCA), | ||
| !grepl(paste0(params_auc_dep, collapse = "|"), Depends) | ||
| ) %>% | ||
| pull(PKNCA) %>% | ||
| intersect(names(PKNCA::get.interval.cols())) | ||
|
|
||
| all_impute_methods <- na.omit(unique(data$intervals$impute)) | ||
| if (length(all_impute_methods) == 0) { | ||
| return(data) | ||
| } | ||
| all_impute_methods <- all_impute_methods %>% | ||
| strsplit(split = ",") %>% | ||
| unlist() %>% | ||
| trimws() %>% | ||
| unique() | ||
|
|
||
| data$intervals <- Reduce(function(d, ti_arg) { | ||
| interval_remove_impute( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the global assignment operator
<<-creates a function in the global environment, which can lead to namespace pollution and testing difficulties. Consider passing this function as a parameter or using a more controlled scoping mechanism.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m-kolomanski will this be good enough?