Skip to content

Commit

Permalink
Merge pull request #1994 from Gilead-BioStats/fix-1976
Browse files Browse the repository at this point in the history
Closes #1976 wrap all our stop-esque calls into the log4r setup
  • Loading branch information
zdz2101 authored Dec 18, 2024
2 parents 9332851 + 4045ddf commit 5cc2056
Show file tree
Hide file tree
Showing 29 changed files with 170 additions and 214 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,as_label)
importFrom(rlang,as_name)
importFrom(rlang,caller_env)
importFrom(rlang,check_installed)
importFrom(rlang,enquo)
importFrom(rlang,enquos)
Expand Down
12 changes: 5 additions & 7 deletions R/Analyze_Fisher.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ Analyze_Fisher <- function(
dfTransformed,
strOutcome = "Numerator"
) {
stopifnot(
"dfTransformed is not a data.frame" = is.data.frame(dfTransformed),
"GroupID or the value in strOutcome not found in dfTransformed" = all(c("GroupID", strOutcome) %in% names(dfTransformed)),
"NA value(s) found in GroupID" = all(!is.na(dfTransformed[["GroupID"]])),
"strOutcome must be length 1" = length(strOutcome) == 1,
"strOutcome is not character" = is.character(strOutcome)
)
stop_if(cnd = !is.data.frame(dfTransformed), message = "dfTransformed is not a data.frame")
stop_if(cnd = !all(c("GroupID", strOutcome) %in% names(dfTransformed)), message = "GroupID or the value in strOutcome not found in dfTransformed")
stop_if(cnd = !all(!is.na(dfTransformed[["GroupID"]])), message = "NA value(s) found in GroupID")
stop_if(cnd = length(strOutcome) != 1, message = "strOutcome must be length 1")
stop_if(cnd = !is.character(strOutcome), message = "strOutcome is not character")

fisher_model <- function(site) {
SiteTable <- dfTransformed %>%
Expand Down
6 changes: 2 additions & 4 deletions R/Analyze_Identity.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
#' @export

Analyze_Identity <- function(dfTransformed, strValueCol = "Metric") {
stopifnot(
"dfTransformed is not a data.frame" = is.data.frame(dfTransformed),
"strValueCol not found in dfTransformed" = strValueCol %in% names(dfTransformed)
)
stop_if(!is.data.frame(dfTransformed), message = "dfTransformed is not a data.frame")
stop_if(!(strValueCol %in% names(dfTransformed)), message = "strValueCol not found in dfTransformed")

dfAnalyzed <- dfTransformed %>%
mutate(
Expand Down
13 changes: 7 additions & 6 deletions R/Analyze_NormalApprox.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ Analyze_NormalApprox <- function(
dfTransformed,
strType = "binary"
) {
stopifnot(
"dfTransformed is not a data.frame" = is.data.frame(dfTransformed),
"One or more of these columns not found: GroupID, GroupLevel, Denominator, Numerator, Metric" =
all(c("GroupID", "GroupLevel", "Denominator", "Numerator", "Metric") %in% names(dfTransformed)),
"NA value(s) found in GroupID" = all(!is.na(dfTransformed[["GroupID"]])),
"strType is not 'binary' or 'rate'" = strType %in% c("binary", "rate")

stop_if(cnd = !is.data.frame(dfTransformed), message ="dfTransformed is not a data.frame")
stop_if(
cnd = !all(c("GroupID", "GroupLevel", "Denominator", "Numerator", "Metric") %in% names(dfTransformed)),
message = "One or more of these columns not found: GroupID, GroupLevel, Denominator, Numerator, Metric"
)
stop_if(cnd = !all(!is.na(dfTransformed[["GroupID"]])), message = "NA value(s) found in GroupID")
stop_if(cnd = !strType %in% c("binary", "rate"), message = "strType is not 'binary' or 'rate'")

# Caclulate Z-score with overdispersion --------------------------------------
if (strType == "binary") {
Expand Down
10 changes: 5 additions & 5 deletions R/Analyze_Poisson.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#' @export

Analyze_Poisson <- function(dfTransformed) {
stopifnot(
"dfTransformed is not a data.frame" = is.data.frame(dfTransformed),
"One or more of these columns not found: GroupID, GroupLevel, Denominator, Numerator, Metric" =
all(c("GroupID", "GroupLevel", "Denominator", "Numerator", "Metric") %in% names(dfTransformed)),
"NA value(s) found in GroupID" = all(!is.na(dfTransformed[["GroupID"]]))
stop_if(cnd = !is.data.frame(dfTransformed), message = "dfTransformed is not a data.frame")
stop_if(
cnd = !all(c("GroupID", "GroupLevel", "Denominator", "Numerator", "Metric") %in% names(dfTransformed)),
message = "One or more of these columns not found: GroupID, GroupLevel, Denominator, Numerator, Metric"
)
stop_if(cnd = !all(!is.na(dfTransformed[["GroupID"]])), message = "NA value(s) found in GroupID")

dfModel <- dfTransformed %>%
mutate(LogDenominator = log(.data$Denominator))
Expand Down
25 changes: 11 additions & 14 deletions R/Flag.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,17 @@ Flag <- function(
vThreshold = NULL,
strValueColumn = NULL
) {
stopifnot(
"dfAnalyzed is not a data frame" = is.data.frame(dfAnalyzed),
"strColumn is not character" = is.character(strColumn),
"vThreshold is not numeric" = is.numeric(vThreshold),
"vThreshold must be length of 2" = length(vThreshold) == 2,
"vThreshold cannot be NULL" = !is.null(vThreshold),
"strColumn must be length of 1" = length(strColumn) == 1,
"strColumn not found in dfAnalyzed" = strColumn %in% names(dfAnalyzed),
"strValueColumn not found in dfAnalyzed" = strValueColumn %in% names(dfAnalyzed),
"GroupID not found in dfAnalyzed" = "GroupID" %in% names(dfAnalyzed)
)
stop_if(cnd = !is.data.frame(dfAnalyzed), message = "dfAnalyzed is not a data frame")
stop_if(cnd = !is.character(strColumn), message = "strColumn is not character")
stop_if(cnd = !is.numeric(vThreshold), message = "vThreshold is not numeric")
stop_if(cnd = length(vThreshold) != 2, message = "vThreshold must be length of 2")
stop_if(cnd = is.null(vThreshold), message = "vThreshold cannot be NULL")
stop_if(cnd = length(strColumn) != 1, message = "strColumn must be length of 1")
stop_if(cnd = !(strColumn %in% names(dfAnalyzed)), message = "strColumn not found in dfAnalyzed")
stop_if(cnd = !("GroupID" %in% names(dfAnalyzed)), message = "GroupID not found in dfAnalyzed")

if (all(!is.na(vThreshold))) {
stopifnot(
"vThreshold must contain a minimum and maximum value (i.e., vThreshold = c(1, 2))" = vThreshold[2] > vThreshold[1]
)
stop_if(cnd = vThreshold[2] <= vThreshold[1], "vThreshold must contain a minimum and maximum value (i.e., vThreshold = c(1, 2))")
}

# Flag values outside the specified threshold.
Expand All @@ -86,6 +81,8 @@ Flag <- function(

# If strValueColumn is supplied, it can only affect sign of Flag (1 or -1).
if (!is.null(strValueColumn)) {
stop_if(cnd = !(strValueColumn %in% names(dfAnalyzed)), message = "strValueColumn not found in dfAnalyzed")

nMedian <- dfFlagged %>%
pull(strValueColumn) %>%
stats::median(na.rm = TRUE)
Expand Down
16 changes: 6 additions & 10 deletions R/Flag_Fisher.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ Flag_Fisher <- function(
dfAnalyzed,
vThreshold = NULL
) {
stopifnot(
"dfAnalyzed is not a data frame" = is.data.frame(dfAnalyzed),
"vThreshold is not numeric" = is.numeric(vThreshold),
"vThreshold must be length of 2" = length(vThreshold) == 2,
"vThreshold cannot be NULL" = !is.null(vThreshold),
"GroupID not found in dfAnalyzed" = "GroupID" %in% names(dfAnalyzed)
)
stop_if(cnd = !is.data.frame(dfAnalyzed), message = "dfAnalyzed is not a data frame")
stop_if(cnd = !is.numeric(vThreshold), message = "vThreshold is not numeric")
stop_if(cnd = length(vThreshold) != 2, message = "vThreshold must be length of 2")
stop_if(cnd = is.null(vThreshold), message = "vThreshold cannot be NULL")
stop_if(cnd = !("GroupID" %in% names(dfAnalyzed)), message = "GroupID not found in dfAnalyzed")

if (all(!is.na(vThreshold))) {
stopifnot(
"vThreshold must contain a minimum and maximum value (i.e., vThreshold = c(1, 2))" = vThreshold[2] > vThreshold[1]
)
stop_if(cnd = vThreshold[2] <= vThreshold[1], "vThreshold must contain a minimum and maximum value (i.e., vThreshold = c(1, 2))")
}

dfFlagged <- dfAnalyzed %>%
Expand Down
28 changes: 15 additions & 13 deletions R/Flag_NormalApprox.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ Flag_NormalApprox <- function(
dfAnalyzed,
vThreshold = NULL
) {
stopifnot(
"dfAnalyzed is not a data frame" = is.data.frame(dfAnalyzed),
"vThreshold is not numeric" = is.numeric(vThreshold),
"vThreshold must be length of 4" = length(vThreshold) == 4,
"vThreshold cannot be NULL" = !is.null(vThreshold)
)
stop_if(cnd = !is.data.frame(dfAnalyzed), message = "dfAnalyzed is not a data frame")
stop_if(cnd = !is.numeric(vThreshold), message = "vThreshold is not numeric")
stop_if(cnd = !(length(vThreshold) == 4), message = "vThreshold must be length of 4")
stop_if(cnd = is.null(vThreshold), message = "vThreshold cannot be NULL")


if (all(!is.na(vThreshold))) {
stopifnot(
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))" =
vThreshold[1] < vThreshold[2],
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))" =
vThreshold[2] < vThreshold[3],
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))" =
vThreshold[3] < vThreshold[4]
stop_if(
cnd = vThreshold[2] <= vThreshold[1],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))"
)
stop_if(
cnd = vThreshold[3] <= vThreshold[2],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))"
)
stop_if(
cnd = vThreshold[4] <= vThreshold[3],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-3, -2, 2, 3))"
)
}

Expand Down
29 changes: 15 additions & 14 deletions R/Flag_Poisson.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@ Flag_Poisson <- function(
dfAnalyzed,
vThreshold = NULL
) {
stopifnot(
"dfAnalyzed is not a data frame" = is.data.frame(dfAnalyzed),
"vThreshold is not numeric" = is.numeric(vThreshold),
"vThreshold must be length of 4" = length(vThreshold) == 4,
"vThreshold cannot be NULL" = !is.null(vThreshold)
)
stop_if(cnd = !is.data.frame(dfAnalyzed), message = "dfAnalyzed is not a data frame")
stop_if(cnd = !is.numeric(vThreshold), message = "vThreshold is not numeric")
stop_if(cnd = !(length(vThreshold) == 4), message = "vThreshold must be length of 4")
stop_if(cnd = is.null(vThreshold), message = "vThreshold cannot be NULL")


if (all(!is.na(vThreshold))) {
stopifnot(
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-7, -5, 5, 7))" =
vThreshold[1] < vThreshold[2],
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-7, -5, 5, 7))" =
vThreshold[2] < vThreshold[3],
"vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-7, -5, 5, 7))" =
vThreshold[3] < vThreshold[4]
stop_if(
cnd = vThreshold[2] <= vThreshold[1],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = c(-7, -5, 5, 7))"
)
stop_if(
cnd = vThreshold[3] <= vThreshold[2],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = = c(-7, -5, 5, 7))"
)
stop_if(
cnd = vThreshold[4] <= vThreshold[3],
message = "vThreshold must contain cutoff for moderate/high risks in two directions (i.e., vThreshold = = c(-7, -5, 5, 7))"
)
}


# Flag values outside the specified threshold.
dfFlagged <- dfAnalyzed %>%
mutate(
Expand Down
45 changes: 12 additions & 33 deletions R/Input_Rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,33 @@ Input_Rate <- function(
strDenominatorCol = NULL
) {
# Check if data frames are NULL
if (is.null(dfSubjects)) {
stop("dfSubjects must be provided")
}
if (is.null(dfDenominator)) {
stop("dfDenominator, must be provided")
}
if (is.null(dfNumerator)) {
stop("dfNumerator, must be provided")
}
stop_if(cnd = is.null(dfSubjects), message = "dfSubjects must be provided")
stop_if(cnd = is.null(dfDenominator), message = "dfDenominator, must be provided")
stop_if(cnd = is.null(dfNumerator), message = "dfNumerator, must be provided")

# must be eit
strNumeratorMethod <- match.arg(strNumeratorMethod)
strDenominatorMethod <- match.arg(strDenominatorMethod)

# Check if strNumeratorCol is Null when strNumeratorMethod is 'Sum'
if (strNumeratorMethod == "Sum") {
if (is.null(strNumeratorCol)) {
stop("strNumeratorCol must be provided when strNumeratorMethod is 'Sum'")
}
if (!is.numeric(dfNumerator[[strNumeratorCol]])) {
stop("strNumeratorCol must be numeric when strNumeratorMethod is `Sum`")
}
stop_if(cnd = is.null(strNumeratorCol), message = "strNumeratorCol must be provided when strNumeratorMethod is 'Sum'")
stop_if(cnd = !is.numeric(dfNumerator[[strNumeratorCol]]), message = "strNumeratorCol must be numeric when strNumeratorMethod is `Sum`")
}

# Check if strDenominatorCol is Null when strDenominatorMethod is 'Sum'
if (strDenominatorMethod == "Sum") {
if (is.null(strDenominatorCol)) {
stop("strDenominatorCol must be provided when strDenominatorMethod is 'Sum'")
}
if (!is.numeric(dfDenominator[[strDenominatorCol]])) {
stop("strDenominatorCol must be numeric when strDenominatorMethod is `Sum`")
}
stop_if(cnd = is.null(strDenominatorCol), message = "strDenominatorCol must be provided when strDenominatorMethod is 'Sum'")
stop_if(cnd = !is.numeric(dfDenominator[[strDenominatorCol]]), message = "strDenominatorCol must be numeric when strDenominatorMethod is `Sum`")
}

# check that "strSubjectCol" is in all dfs
stopifnot(
strSubjectCol %in% colnames(dfSubjects),
strSubjectCol %in% colnames(dfNumerator),
strSubjectCol %in% colnames(dfDenominator)
)
stop_if(cnd = !(strSubjectCol %in% colnames(dfSubjects)), message = "`strSubjectCol` must be a column name of `dfSubjects`")
stop_if(cnd = !(strSubjectCol %in% colnames(dfNumerator)), message = "`strSubjectCol` must be a column name of `dfNumerator`")
stop_if(cnd = !(strSubjectCol %in% colnames(dfDenominator)), message = "`strSubjectCol` must be a column name of `dfDenominator`")

# check that "strGroupCol" is in dfSubjects
stopifnot(strGroupCol %in% colnames(dfSubjects))
stop_if(cnd = !(strGroupCol %in% colnames(dfSubjects)), message = "`strGroupcol` must be a column name of `dfSubjects`")

# if `strGroupLevel` is null, use `strGroupCol`
if (is.null(strGroupLevel)) {
Expand Down Expand Up @@ -147,12 +131,7 @@ Input_Rate <- function(
group_by(.data$SubjectID) %>%
summarise(Denominator = sum(.data$Denominator, na.rm = TRUE))

if (all(dfDenominator_subj$Denominator == 0)) {
LogMessage(
level = "fatal",
message = "All denominator values are 0, please check `dfDenominator`"
)
}
stop_if(cnd = all(dfDenominator_subj$Denominator == 0), message = "All denominator values are 0, please check `dfDenominator`")

# Merge Numerator and Denominator with Subject Data. Keep all data in Subject. Fill in missing numerator/denominators with 0
dfInput <- dfSubjects %>%
Expand Down
13 changes: 6 additions & 7 deletions R/RunQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@
#'
#' @export
RunQuery <- function(strQuery, df, bUseSchema = FALSE, lColumnMapping = NULL) {
stopifnot(is.character(strQuery))
stop_if(cnd = !is.character(strQuery), message = "strQuery must be a query")

# Check that strQuery contains "FROM df"
if (!stringr::str_detect(strQuery, "FROM df")) {
stop("strQuery must contain 'FROM df'")
}
stop_if(cnd = !stringr::str_detect(strQuery, "FROM df"), message = "strQuery must contain 'FROM df'")

# Check that columnMapping exists if use_schema == TRUE

if (bUseSchema && is.null(lColumnMapping)) {
stop("if use_schema = TRUE, you must provide lColumnMapping spec")
}
stop_if(
cnd = (bUseSchema && is.null(lColumnMapping)),
message = "if use_schema = TRUE, you must provide lColumnMapping spec"
)

# Enforce data structure of schema.
if (bUseSchema) {
Expand Down
7 changes: 3 additions & 4 deletions R/Summarize.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ Summarize <- function(
dfFlagged,
nMinDenominator = NULL
) {
stopifnot(
"dfFlagged is not a data frame" = is.data.frame(dfFlagged),
"One or more of these columns: GroupID, GroupLevel, Flag, Score, not found in dfFlagged" = all(c("GroupID", "GroupLevel", "Flag", "Score") %in% names(dfFlagged))
)
stop_if(cnd = !is.data.frame(dfFlagged), message = "dfFlagged is not a data frame")
stop_if(cnd = !all(c("GroupID", "GroupLevel", "Flag", "Score") %in% names(dfFlagged)),
message = "One or more of these columns: GroupID, GroupLevel, Flag, Score, not found in dfFlagged" )

if (!("Numerator" %in% colnames(dfFlagged))) {
dfFlagged$Numerator <- NA
Expand Down
10 changes: 4 additions & 6 deletions R/Transform_Count.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ Transform_Count <- function(
strCountCol,
strGroupCol = "GroupID"
) {
stopifnot(
"dfInput is not a data frame" = is.data.frame(dfInput),
"strCountCol not found in input data" = strCountCol %in% names(dfInput),
"strCountCol is not numeric or logical" = is.numeric(dfInput[[strCountCol]]) | is.logical(dfInput[[strCountCol]]),
"NA's found in strCountCol" = !anyNA(dfInput[[strCountCol]])
)
stop_if(cnd = !is.data.frame(dfInput), message = "dfInput is not a data frame")
stop_if(cnd = !(strCountCol %in% names(dfInput)), message = "strCountCol not found in input data")
stop_if(cnd = !(is.numeric(dfInput[[strCountCol]]) | is.logical(dfInput[[strCountCol]])), message = "strCountCol is not numeric or logical")
stop_if(cnd = anyNA(dfInput[[strCountCol]]), message = "NA's found in strCountCol")

dfTransformed <- dfInput %>%
group_by(GroupID = .data[[strGroupCol]]) %>%
Expand Down
15 changes: 7 additions & 8 deletions R/Transform_Rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ Transform_Rate <- function(
strNumeratorCol = "Numerator",
strDenominatorCol = "Denominator"
) {
stopifnot(
"dfInput is not a data frame" = is.data.frame(dfInput),
"strNumeratorColumn is not numeric" = is.numeric(dfInput[[strNumeratorCol]]),
"strDenominatorColumn is not numeric" = is.numeric(dfInput[[strDenominatorCol]]),
"NA's found in numerator" = !anyNA(dfInput[[strNumeratorCol]]),
"NA's found in denominator" = !anyNA(dfInput[[strDenominatorCol]]),
"Required columns not found in input data" = c(strNumeratorCol, strDenominatorCol, "GroupID", "GroupLevel") %in% names(dfInput)
)
stop_if(cnd = !is.data.frame(dfInput), message = "dfInput is not a data frame")
stop_if(cnd = !is.numeric(dfInput[[strNumeratorCol]]), message = "strNumeratorColumn is not numeric")
stop_if(cnd = !is.numeric(dfInput[[strDenominatorCol]]), message = "strDenominatorColumn is not numeric")
stop_if(cnd = anyNA(dfInput[[strNumeratorCol]]), message = "NA's found in numerator")
stop_if(cnd = anyNA(dfInput[[strDenominatorCol]]), message = "NA's found in denominator")
stop_if(cnd = !(all(c(strNumeratorCol, strDenominatorCol, "GroupID", "GroupLevel") %in% names(dfInput))), message = "Required columns not found in input data")


dfTransformed <- dfInput %>%
group_by(.data$GroupID, .data$GroupLevel) %>%
Expand Down
12 changes: 5 additions & 7 deletions R/Visualize_Score.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ Visualize_Score <- function(
bFlagFilter = FALSE,
strTitle = ""
) {
stopifnot(
"strTitle must be character" = is.character(strTitle),
"bFlagFilter must be logical" = is.logical(bFlagFilter),
"dfResults must be a data.frame" = is.data.frame(dfResults),
"strType must be 'Metric' or 'Score'" = strType %in% c("Metric", "Score"),
"strType must be length 1" = length(strType) == 1
)
stop_if(cnd = !is.character(strTitle), message = "strTitle must be character" )
stop_if(cnd = !is.logical(bFlagFilter), message = "bFlagFilter must be logical")
stop_if(cnd = !is.data.frame(dfResults), message = "dfResults must be a data.frame")
stop_if(cnd = !(length(strType) == 1), message = "strType must be length 1" )
stop_if(cnd = !(strType %in% c("Metric", "Score")), message = "strType must be 'Metric' or 'Score'" )

dfResults$FlagAbs <- abs(dfResults$Flag)
flagBreaks <- as.character(unique(sort(dfResults$FlagAbs)))
Expand Down
Loading

0 comments on commit 5cc2056

Please sign in to comment.