From b2b15e25963fd14e3f31981b317e95b02bff1b9d Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Mon, 19 Aug 2024 09:32:01 -0400 Subject: [PATCH 1/2] Make negative control outcomes optional - fixes #153 --- R/Module-StrategusModule.R | 58 ++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/R/Module-StrategusModule.R b/R/Module-StrategusModule.R index a9d783cd..4f206999 100644 --- a/R/Module-StrategusModule.R +++ b/R/Module-StrategusModule.R @@ -241,45 +241,41 @@ StrategusModule <- R6::R6Class( return(cohortDefinitionSet) }, - .jobContextHasNegativeControlOutcomeSharedResource = function() { - jobContext <- private$jobContext - ncSharedResource <- private$.getSharedResourceByClassName( - sharedResources = jobContext$sharedResources, - className = "NegativeControlOutcomeSharedResources" - ) - hasNegativeControlOutcomeSharedResource <- !is.null(ncSharedResource) - invisible(hasNegativeControlOutcomeSharedResource) - }, .createNegativeControlOutcomeSettingsFromJobContext = function() { jobContext <- private$jobContext negativeControlSharedResource <- private$.getSharedResourceByClassName( sharedResources = jobContext$sharedResources, className = "NegativeControlOutcomeSharedResources" ) - if (is.null(negativeControlSharedResource)) { - stop("Negative control outcome shared resource not found!") - } - negativeControlOutcomes <- negativeControlSharedResource$negativeControlOutcomes$negativeControlOutcomeCohortSet - if (length(negativeControlOutcomes) <= 0) { - stop("No negative control outcomes found") - } - negativeControlOutcomeCohortSet <- CohortGenerator::createEmptyNegativeControlOutcomeCohortSet() - for (i in 1:length(negativeControlOutcomes)) { - nc <- negativeControlOutcomes[[i]] - negativeControlOutcomeCohortSet <- rbind( - negativeControlOutcomeCohortSet, - data.frame( - cohortId = as.numeric(nc$cohortId), - cohortName = nc$cohortName, - outcomeConceptId = as.numeric(nc$outcomeConceptId) + if (!is.null(negativeControlSharedResource)) { + negativeControlOutcomes <- negativeControlSharedResource$negativeControlOutcomes$negativeControlOutcomeCohortSet + if (length(negativeControlOutcomes) <= 0) { + stop("Negative control outcome shared resource found but no negative control outcomes were provided.") + } + negativeControlOutcomeCohortSet <- CohortGenerator::createEmptyNegativeControlOutcomeCohortSet() + for (i in 1:length(negativeControlOutcomes)) { + nc <- negativeControlOutcomes[[i]] + negativeControlOutcomeCohortSet <- rbind( + negativeControlOutcomeCohortSet, + data.frame( + cohortId = as.numeric(nc$cohortId), + cohortName = nc$cohortName, + outcomeConceptId = as.numeric(nc$outcomeConceptId) + ) ) - ) + } + invisible(list( + cohortSet = negativeControlOutcomeCohortSet, + occurrenceType = negativeControlSharedResource$negativeControlOutcomes$occurrenceType, + detectOnDescendants = negativeControlSharedResource$negativeControlOutcomes$detectOnDescendants + )) + } else { + invisible(list( + cohortSet = NULL, + occurrenceType = "all", + detectOnDescendants = FALSE + )) } - invisible(list( - cohortSet = negativeControlOutcomeCohortSet, - occurrenceType = negativeControlSharedResource$negativeControlOutcomes$occurrenceType, - detectOnDescendants = negativeControlSharedResource$negativeControlOutcomes$detectOnDescendants - )) } ) ) From 81e2d9c543b5b112d2e2f497db670f693913c352 Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Mon, 19 Aug 2024 09:52:06 -0400 Subject: [PATCH 2/2] Add test case --- tests/testthat/test-Execution.R | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/testthat/test-Execution.R b/tests/testthat/test-Execution.R index 2404b711..64da5c65 100644 --- a/tests/testthat/test-Execution.R +++ b/tests/testthat/test-Execution.R @@ -179,3 +179,35 @@ test_that("Execute on Oracle stops if table names exceed length limit", { ) ) }) + +test_that("Negative control outcomes are optional", { + analysisSpecifications <- ParallelLogger::loadSettingsFromJson( + fileName = system.file("testdata/cdmModulesAnalysisSpecifications.json", + package = "Strategus" + ) + ) + + # Remove the nco section + analysisSpecifications$sharedResources <- list(analysisSpecifications$sharedResources[[1]]) + + # Remove all but CG + analysisSpecifications$moduleSpecifications <- list(analysisSpecifications$moduleSpecifications[[3]]) + + executionSettings <- createCdmExecutionSettings( + workDatabaseSchema = workDatabaseSchema, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortTableNames = CohortGenerator::getCohortTableNames(cohortTable = "unit_test"), + workFolder = file.path(tempDir, "work_folder"), + resultsFolder = file.path(tempDir, "results_folder") + ) + + expect_output( + Strategus::execute( + connectionDetails = connectionDetails, + analysisSpecifications = analysisSpecifications, + executionSettings = executionSettings + ), + "Generating cohort set", + ignore.case = TRUE + ) +})