Skip to content

Commit

Permalink
Version 0.0.7
Browse files Browse the repository at this point in the history
* Update CodeToRunOhdsi.R
* Add doNotExportCohortData
* Update CreateCohortExplorerApp.R
  • Loading branch information
gowthamrao authored Dec 7, 2022
1 parent d46c1a6 commit 04589fb
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 31 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: CohortExplorer
Type: Package
Title: An R package with a Shiny viewer to explore profiles of patients in a cohort
Version: 0.0.6
Date: 2022-11-21
Version: 0.0.8
Date: 2022-12-07
Authors@R: c(
person("Gowtham", "Rao", email = "[email protected]", role = c("aut", "cre")),
person("Observational Health Data Science and Informatics", role = c("cph"))
Expand Down
78 changes: 55 additions & 23 deletions R/CreateCohortExplorerApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#' @param cohortDefinitionId The cohort id to extract records.
#'
#' @param cohortName (optional) Cohort Name
#'
#' @param doNotExportCohortData (Optional) Do you want to not export cohort data? If set to true, parameters cohortDefinitionId,
#' cohort, cohortDatabaseSchema, cohortName will be ignored. The persons entire
#' observation period would be considered the cohort. Cohort Name will be 'Observation Period', cohort
#' id will be set to 0.
#'
#' @param sampleSize (Optional, default = 20) The number of persons to randomly sample. Ignored, if personId is given.
#'
Expand Down Expand Up @@ -77,6 +82,7 @@ createCohortExplorerApp <- function(connectionDetails = NULL,
cohortTable = "cohort",
cohortDefinitionId,
cohortName = NULL,
doNotExportCohortData = FALSE,
sampleSize = 25,
personIds = NULL,
exportFolder,
Expand All @@ -87,6 +93,23 @@ createCohortExplorerApp <- function(connectionDetails = NULL,

errorMessage <- checkmate::makeAssertCollection()

checkmate::assertLogical(
x = doNotExportCohortData,
any.missing = FALSE,
len = 1,
min.len = 1,
null.ok = FALSE,
add = errorMessage
)
checkmate::reportAssertions(collection = errorMessage)

if (doNotExportCohortData) {
cohortDatabaseSchema <- cdmDatabaseSchema
cohortDefinitionId <- 0
cohortName = "Observation Period"
cohortTable = "observation_period"
}

checkmate::assertCharacter(
x = cohortDatabaseSchema,
min.len = 1,
Expand Down Expand Up @@ -236,47 +259,56 @@ createCohortExplorerApp <- function(connectionDetails = NULL,
} else {
# take a random sample
sql <- "DROP TABLE IF EXISTS #persons_filter;
SELECT *
INTO #persons_filter
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY NEWID()) AS new_id, person_id
FROM (
SELECT DISTINCT subject_id person_id
FROM @cohort_database_schema.@cohort_table
WHERE cohort_definition_id = @cohort_definition_id
) all_ids
) f
WHERE new_id <= @sample_size;"

writeLines("Attempting to find subjects in cohort table.")
SELECT *
INTO #persons_filter
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY NEWID()) AS new_id, person_id
FROM (
{!@do_not_export_cohort_data} ? {SELECT DISTINCT subject_id person_id
FROM @cohort_database_schema.@cohort_table
WHERE cohort_definition_id = @cohort_definition_id} : {
SELECT DISTINCT person_id
FROM @cohort_database_schema.@cohort_table
}
) all_ids
) f
WHERE new_id <= @sample_size;"

writeLines("Attempting to find random subjects.")
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = sql,
tempEmulationSchema = tempEmulationSchema,
sample_size = sampleSize,
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
cohort_definition_id = cohortDefinitionId
cohort_definition_id = cohortDefinitionId,
do_not_export_cohort_data = doNotExportCohortData
)
}

writeLines("Getting cohort table.")
cohort <- DatabaseConnector::renderTranslateQuerySql(
connection = connection,
sql = "SELECT c.subject_id,
p.new_id,
cohort_start_date AS start_date,
cohort_end_date AS end_date
sql = "SELECT {!@do_not_export_cohort_data} ? {c.subject_id} : {c.person_id subject_id},
p.new_id,
{!@do_not_export_cohort_data} ? {cohort_start_date AS start_date,
cohort_end_date AS end_date} : {
observation_period_start_date AS start_date,
observation_period_end_date AS end_date
}
FROM @cohort_database_schema.@cohort_table c
INNER JOIN #persons_filter p
ON c.subject_id = p.person_id
WHERE cohort_definition_id = @cohort_definition_id
ORDER BY c.subject_id, cohort_start_date;",
ON {!@do_not_export_cohort_data} ? {c.subject_id} : {c.person_id} = p.person_id
{!@do_not_export_cohort_data} ? {WHERE cohort_definition_id = @cohort_definition_id}
ORDER BY {!@do_not_export_cohort_data} ? {c.subject_id} : {c.person_id},
{!@do_not_export_cohort_data} ? {cohort_start_date} : {observation_period_start_date};",
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
tempEmulationSchema = tempEmulationSchema,
cohort_definition_id = cohortDefinitionId,
do_not_export_cohort_data = doNotExportCohortData,
snakeCaseToCamelCase = TRUE
) %>%
dplyr::tibble()
Expand Down
4 changes: 1 addition & 3 deletions extras/exampleCodeToRun/CodeToRun.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ library(magrittr)
# Pre-requisites ----
remotes::install_github('OHDSI/CohortExplorer')

cohortDefinitionIds <- c(10393

)
cohortDefinitionIds <- c(10393)

ROhdsiWebApi::authorizeWebApi(baseUrl = Sys.getenv("BaseUrl"), authMethod = "windows")
cohortDefinitionSet <-
Expand Down
6 changes: 3 additions & 3 deletions extras/exampleCodeToRun/CodeToRunOhdsi.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library(magrittr)
# Pre-requisites ----
# remotes::install_github('OHDSI/CohortExplorer')

cohortDefinitionIds <- c(63)
cohortDefinitionIds <- c(256)

ROhdsiWebApi::authorizeWebApi(
baseUrl = Sys.getenv("ohdsiAtlasPhenotype"),
Expand All @@ -19,7 +19,7 @@ cohortDefinitionSet <-
dplyr::rename(cohortId = id, cohortName = name) %>%
dplyr::arrange(cohortId)

exportFolder <- "c:/temp/CohortExplorer"
exportFolder <- "d:/temp/CohortExplorer"
projectCode <- "pl_"


Expand Down Expand Up @@ -59,7 +59,7 @@ for (i in (1:length(databaseIds))) {
)

cohortTableName <- paste0(stringr::str_squish(projectCode),
stringr::str_squish(cdmSource$database))
stringr::str_squish(cdmSource$sourceKey))

# EXECUTE --------------------------------------------------------------------
tryCatch(
Expand Down
Binary file removed inst/doc/HowToUseCohortExplorer.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions man/createCohortExplorerApp.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-createCohortExplorerApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,20 @@ test_that("Extract person level data", {
assignNewId = TRUE,
shiftDates = TRUE
)

createCohortExplorerApp(
connection = connection,
cohortDatabaseSchema = cohortDatabaseSchema,
cdmDatabaseSchema = cdmDatabaseSchema,
vocabularyDatabaseSchema = vocabularyDatabaseSchema,
cohortTable = cohortTable,
cohortDefinitionId = c(1),
sampleSize = 100,
doNotExportCohortData = TRUE,
databaseId = "databaseData",
exportFolder = outputDir
)

testthat::expect_true(file.exists(file.path(outputDir, "CohortExplorerShiny", "data", "CohortExplorer_0_databaseData.RData")))

})

0 comments on commit 04589fb

Please sign in to comment.