diff --git a/.gitignore b/.gitignore index 544dd20e..5f2dd0f5 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ output/ *.log # R environment settings file -.Renviron \ No newline at end of file +.Renviron +.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index 31aca523..0a04a07b 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: Achilles Type: Package Title: Achilles Data Source Characterization -Version: 1.8 -Date: 2025-10-10 +Version: 1.8.0 +Date: 2025-12-02 Authors@R: c( person("Frank", "DeFalco", email = "fdefalco@ohdsi.org", role = c("aut","cre")), person("Patrick", "Ryan", email = "ryan@ohdsi.org", role = c("aut")), @@ -24,12 +24,12 @@ Description: Automated Characterization of Health Information at Large-Scale variety of Observational Health Data Sciences and Informatics community applications. Depends: - DatabaseConnector (>= 2.0.0), + DatabaseConnector (>= 7.0.0), R (>= 4.0.0) Imports: - DBI, + DBI (>= 1.0.0), duckdb, - SqlRender (>= 1.6.0), + SqlRender (>= 1.19.2), dplyr, jsonlite, ParallelLogger, @@ -49,7 +49,7 @@ Suggests: VignetteBuilder: knitr License: Apache License Roxygen: list() -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.3 Encoding: UTF-8 URL: https://ohdsi.github.io/Achilles/, https://github.com/OHDSI/Achilles BugReports: https://github.com/OHDSI/Achilles/issues diff --git a/NAMESPACE b/NAMESPACE index 929c06a7..8fc8e256 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,8 +41,7 @@ import(ParallelLogger) import(SqlRender) import(dplyr) importFrom(data.table,fwrite) -importFrom(dplyr,desc) -importFrom(dplyr,ntile) +importFrom(dplyr,rename_with) importFrom(rlang,.data) importFrom(stats,aggregate) importFrom(stats,cycle) diff --git a/NEWS.md b/NEWS.md index 7003c4f7..79ee53c4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,47 @@ +# Achilles 1.8 + +## Improvements and New Features + +- **Export Enhancements** + - Added `unit_concept_id` to ARES export of measurement tables. + - Improved unit concept ID naming and handling. + - Added export of the location table to ARES. + - Added database summary reporting. + +- **Performance Improvements** + - Refactored performance logs to be captured directly (not just through console logs). + - Improved performance of ARES export, especially for DuckDB. + - Optimized analyses for better performance, including analyses 117 and 1815. + +- **Code Quality and Maintenance** + - Fixed ambiguous `dplyr::select` statements and standardized column name casing (e.g., `IS_DEFAULT` → `is_default`). + - Added missing SQL scripts for performance tracking. + - Fixed fromJSON method usage for correctness. + - Fixed errors when creating metadata tables with zero-length vectors. + - Moved repeated subqueries to temp tables in some analyses. + - Trimmed trailing whitespaces in export scripts. + +- **Bug Fixes** + - Fixed crash during `exportToAres` (DuckDB) related to unit concept IDs. + - Handled missing server values in temporal characterization functions. + +- **Documentation** + - Updated and added links in the `DESCRIPTION` file. + +## Notable Commits + +- [Fix column name case: IS_DEFAULT → is_default](https://github.com/OHDSI/Achilles/commit/134bc0a7e0159dde85653ac41c1e73d8f4123fd1) +- [Ambiguous dplyr::select statements](https://github.com/OHDSI/Achilles/commit/889999c7a8b59476b8d4d95ee0d4d4a842db81ff) +- [Add db summary](https://github.com/OHDSI/Achilles/commit/87790da7453f0c8c2aa6d8e5071f5fa7a0a397fe) +- [Location table export to ARES](https://github.com/OHDSI/Achilles/commit/d5199716fca61bf4b45ab38ee8c1c441633743bc) +- [Improve performance of analysis 117](https://github.com/OHDSI/Achilles/commit/f9405e4a3b1a03ba4e4603db5774de96c5d8d3f6) +- [Add links to DESCRIPTION](https://github.com/OHDSI/Achilles/commit/c0f1a934c949a5b989f02eb56e271101306e1ed9) +- [Fix fromJSON correct method usage](https://github.com/OHDSI/Achilles/commit/b6ff65524a34da285e6340236791b00f6e32a39d) + +--- + +For more, visit the [develop branch commit history](https://github.com/OHDSI/Achilles/commits?sha=develop&sort=updated). + # Achilles 1.7.2 1. Improved test setup management diff --git a/R/Achilles.R b/R/Achilles.R index 84b05e78..9c336021 100755 --- a/R/Achilles.R +++ b/R/Achilles.R @@ -1,6 +1,6 @@ # @file Achilles # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -113,14 +113,16 @@ #' @examples #' \dontrun{ #' connectionDetails <- createConnectionDetails(dbms = "sql server", server = "some_server") -#' achillesResults <- achilles(connectionDetails = connectionDetails, +#' achillesResults <- achilles( +#' connectionDetails = connectionDetails, #' cdmDatabaseSchema = "cdm", #' resultsDatabaseSchema = "results", #' scratchDatabaseSchema = "scratch", #' sourceName = "Some Source", #' cdmVersion = "5.3", #' numThreads = 10, -#' outputFolder = "output") +#' outputFolder = "output" +#' ) #' } #' #' @export @@ -147,39 +149,38 @@ achilles <- function(connectionDetails, updateGivenAnalysesOnly = FALSE, excludeAnalysisIds, sqlDialect = NULL) { - totalStart <- Sys.time() achillesSql <- c() - - performanceTable <- data.frame(analysis_id = integer(), elapsed_seconds = numeric(), start_time = numeric(), end_time = numeric ()) - + + performanceTable <- data.frame(analysis_id = integer(), elapsed_seconds = numeric(), start_time = numeric(), end_time = numeric()) + # Check if the correct parameters are supplied when running in sqlOnly mode if (sqlOnly && missing(connectionDetails) && is.null(sqlDialect)) { stop("Error: When specifying sqlOnly = TRUE, sqlDialect or connectionDetails must be supplied.") } - + if (sqlOnly && !missing(connectionDetails)) { print("Running Achilles in SQL ONLY mode. Using connectionDetails, sqlDialect is ignored. Please wait for script generation.") } - + if (sqlOnly && missing(connectionDetails) && !is.null(sqlDialect)) { connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = sqlDialect) print("Running Achilles in SQL ONLY mode. Using dialect supplied by sqlDialect. Please wait for script generation.") } - + if (!dir.exists(outputFolder)) { dir.create(outputFolder) } - + timestamp <- format(Sys.time(), "%Y%m%d_%H%M%S") - logFile <- file.path(outputFolder, paste("log_achilles_", timestamp,".txt", sep = '')) + logFile <- file.path(outputFolder, paste("log_achilles_", timestamp, ".txt", sep = "")) ParallelLogger::clearLoggers() ParallelLogger::addDefaultFileLogger(logFile) ParallelLogger::addDefaultErrorReportLogger(file.path(outputFolder, "errorReportR.txt")) - + ParallelLogger::logInfo("Performing database characterization.") - + # Try to get CDM Version if not provided if (!missing(cdmVersion)) { ParallelLogger::logInfo(paste("CDM Version", cdmVersion, "passed as parameter.")) @@ -187,27 +188,27 @@ achilles <- function(connectionDetails, cdmVersion <- .getCdmVersion(connectionDetails, cdmDatabaseSchema) ParallelLogger::logInfo(paste("CDM Version", cdmVersion, "found in cdm_source table.")) } - + cdmVersion <- as.character(cdmVersion) - + # Check CDM version is valid if (compareVersion(a = as.character(cdmVersion), b = "5") < 0) { stop("Error: Invalid CDM Version number. CDM V5 and greater are supported.") } - + # Establish folder paths if (!dir.exists(outputFolder)) { dir.create(path = outputFolder, recursive = TRUE) } - + # Get source name if none provided if (missing(sourceName) & !sqlOnly) { sourceName <- .getSourceName(connectionDetails, cdmDatabaseSchema) } - + # Obtain analyses to run analysisDetails <- getAnalysisDetails() - + if (!missing(analysisIds)) { # If specific analysis_ids are given, run only those analysisDetails <- analysisDetails[analysisDetails$analysis_id %in% analysisIds, ] @@ -215,13 +216,13 @@ achilles <- function(connectionDetails, # If specific analyses are not given, determine whether or not to run only default analyses analysisDetails <- analysisDetails[analysisDetails$is_default == 1, ] } - + # Remove unwanted analyses that have not already been excluded, if any are specified if (!missing(excludeAnalysisIds) && - any(analysisDetails$analysis_id %in% excludeAnalysisIds)) { + any(analysisDetails$analysis_id %in% excludeAnalysisIds)) { analysisDetails <- analysisDetails[-which(analysisDetails$analysis_id %in% excludeAnalysisIds), ] } - + resultsTables <- list( list( detailType = "results", @@ -230,7 +231,7 @@ achilles <- function(connectionDetails, file = system.file("csv", "schemas", "schema_achilles_results.csv", package = "Achilles"), header = TRUE ), - analysisIds = analysisDetails[analysisDetails$distribution <= 0,]$analysis_id + analysisIds = analysisDetails[analysisDetails$distribution <= 0, ]$analysis_id ), list( detailType = "results_dist", @@ -244,31 +245,31 @@ achilles <- function(connectionDetails, ), header = TRUE ), - analysisIds = analysisDetails[abs(analysisDetails$distribution) == 1,]$analysis_id + analysisIds = analysisDetails[abs(analysisDetails$distribution) == 1, ]$analysis_id ) ) schemaDelim <- "." - + if (sqlOnly) { if (.supportsTempTables(connectionDetails) && - connectionDetails$dbms != "oracle") { + connectionDetails$dbms != "oracle") { scratchDatabaseSchema <- "#" schemaDelim <- "s_" } } else { connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) on.exit(DatabaseConnector::disconnect(connection), add = TRUE) - + if (numThreads == 1 || scratchDatabaseSchema == "#") { numThreads <- 1 - + if (.supportsTempTables(connectionDetails) && - connectionDetails$dbms != "oracle") { + connectionDetails$dbms != "oracle") { scratchDatabaseSchema <- "#" schemaDelim <- "s_" } - + ParallelLogger::logInfo("Beginning single-threaded execution") } else if (!requireNamespace("ParallelLogger", quietly = TRUE)) { stop( @@ -285,59 +286,57 @@ achilles <- function(connectionDetails, ParallelLogger::logInfo("Beginning multi-threaded execution") } } - + # Determine whether or not to create Achilles support tables if (!createTable && missing(analysisIds)) { createTable <- TRUE preserveResults <- FALSE } else if (!createTable && - !missing(analysisIds) && !updateGivenAnalysesOnly) { + !missing(analysisIds) && !updateGivenAnalysesOnly) { createTable <- TRUE preserveResults <- FALSE } else if (!createTable && - !missing(analysisIds) && updateGivenAnalysesOnly) { + !missing(analysisIds) && updateGivenAnalysesOnly) { preserveResults <- TRUE } - + ## If not creating support tables, then either remove ALL prior results or only those results for the given analysisIds if (!sqlOnly) { if (!createTable && !preserveResults) { .deleteExistingResults( connectionDetails = connectionDetails, resultsDatabaseSchema = resultsDatabaseSchema, - analysisDetails = analysisDetails ) } else if (!createTable && preserveResults) { .deleteGivenAnalyses( connectionDetails = connectionDetails, resultsDatabaseSchema = resultsDatabaseSchema, - analysisIds = analysisIds ) } } - + # Create and populate the achilles_analysis table if (createTable) { sql <- SqlRender::loadRenderTranslateSql( sqlFilename = "analyses/achilles_analysis_ddl.sql", packageName = "Achilles", - dbms = connectionDetails$dbms, - resultsDatabaseSchema = resultsDatabaseSchema + dbms = connectionDetails$dbms, + resultsDatabaseSchema = resultsDatabaseSchema ) - - sqlPerformanceTable <- SqlRender::loadRenderTranslateSql( + + sqlPerformanceTable <- SqlRender::loadRenderTranslateSql( sqlFilename = "analyses/achilles_performance_ddl.sql", packageName = "Achilles", - dbms = connectionDetails$dbms, - resultsDatabaseSchema = resultsDatabaseSchema + dbms = connectionDetails$dbms, + resultsDatabaseSchema = resultsDatabaseSchema ) # Populate achilles_analysis without the "distribution" and "distributed_field" # columns from achilles_analysis_details.csv analysisDetailsCsv <- Achilles::getAnalysisDetails() - analysisDetailsCsv <- analysisDetailsCsv[,-c(2, 3)] + analysisDetailsCsv <- analysisDetailsCsv[, -c(2, 3)] if (!sqlOnly) { # Create empty achilles_analysis @@ -361,7 +360,7 @@ achilles <- function(connectionDetails, progressBar = F, reportOverallTime = F ) - + # Populate achilles_analysis with data from achilles_analysis_details.csv from above suppressMessages( DatabaseConnector::insertTable( @@ -377,16 +376,16 @@ achilles <- function(connectionDetails, ) } } - + # Clean up existing scratch tables if ((numThreads > 1 || - !.supportsTempTables(connectionDetails)) && !sqlOnly) { + !.supportsTempTables(connectionDetails)) && !sqlOnly) { # Drop the scratch tables ParallelLogger::logInfo(sprintf( "Dropping any existing scratch Achilles tables from schema %s", scratchDatabaseSchema )) - + dropAllScratchTables( connectionDetails = connectionDetails, scratchDatabaseSchema = scratchDatabaseSchema, @@ -397,7 +396,7 @@ achilles <- function(connectionDetails, defaultAnalysesOnly = defaultAnalysesOnly ) } - + # Generate Main Analyses mainAnalysisIds <- analysisDetails$analysis_id @@ -421,9 +420,9 @@ achilles <- function(connectionDetails, ) ) }) - + achillesSql <- c(achillesSql, lapply(mainSqls, function(s) s$sql)) - + if (!sqlOnly) { analysisCount <- length(mainSqls) if (analysisCount > 1) { @@ -431,8 +430,8 @@ achilles <- function(connectionDetails, } else { analysisMessage <- paste(analysisCount, "analysis.") } - ParallelLogger::logInfo(paste("Running",analysisMessage)) - + ParallelLogger::logInfo(paste("Running", analysisMessage)) + if (numThreads == 1) { for (mainSql in mainSqls) { start <- Sys.time() @@ -457,7 +456,7 @@ achilles <- function(connectionDetails, endTime <- Sys.time() delta <- endTime - start analysisId <- as.integer(mainSql$analysisId) - performanceTable[nrow(performanceTable) + 1,] <- c(analysisId,delta,start,endTime) + performanceTable[nrow(performanceTable) + 1, ] <- c(analysisId, delta, start, endTime) ParallelLogger::logInfo(sprintf( "[Main Analysis] [COMPLETE] %d (%f %s)", as.integer(mainSql$analysisId), @@ -469,16 +468,17 @@ achilles <- function(connectionDetails, } else { cluster <- ParallelLogger::makeCluster(numberOfThreads = numThreads, singleThreadToMain = TRUE) results <- ParallelLogger::clusterApply(cluster = cluster, x = mainSqls, function(mainSql) { - start <- Sys.time() - connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) - ParallelLogger::logInfo( - sprintf( - "[Main Analysis] [START] %d (%s)", - as.integer(mainSql$analysisId), - analysisDetails$analysis_name[analysisDetails$analysis_id == mainSql$analysisId] - ) + start <- Sys.time() + connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) + ParallelLogger::logInfo( + sprintf( + "[Main Analysis] [START] %d (%s)", + as.integer(mainSql$analysisId), + analysisDetails$analysis_name[analysisDetails$analysis_id == mainSql$analysisId] ) - tryCatch({ + ) + tryCatch( + { DatabaseConnector::executeSql( connection = connection, sql = mainSql$sql, @@ -493,20 +493,20 @@ achilles <- function(connectionDetails, ParallelLogger::logInfo(sprintf( "[Main Analysis] [COMPLETE] %d (%f %s)", as.integer(mainSql$analysisId), - delta, attr(delta, "units") )) - }, error = function(e) { + }, + error = function(e) { ParallelLogger::logError(sprintf( "[Main Analysis] [ERROR] %d (%s)", as.integer(mainSql$analysisId), - e )) - }) - }) - + } + ) + }) + ParallelLogger::stopCluster(cluster = cluster) } @@ -521,13 +521,13 @@ achilles <- function(connectionDetails, progressBar = F ) } - + # Merge scratch tables into final analysis tables include <- sapply(resultsTables, function(d) { any(d$analysisIds %in% analysisDetails$analysis_id) }) resultsTablesToMerge <- resultsTables[include] - + mergeSqls <- lapply(resultsTablesToMerge, function(table) { .mergeAchillesScratchTables( resultsTable = table, @@ -548,44 +548,50 @@ achilles <- function(connectionDetails, performanceTable = performanceTable ) }) - + achillesSql <- c(achillesSql, mergeSqls) - + if (!sqlOnly) { ParallelLogger::logInfo("Merging scratch Achilles tables") - + if (numThreads == 1) { - tryCatch({ - for (sql in mergeSqls) { - DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) + tryCatch( + { + for (sql in mergeSqls) { + DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) + } + }, + error = function(e) { + ParallelLogger::logError(sprintf("Merging scratch Achilles tables [ERROR] (%s)", e)) } - }, error = function(e) { - ParallelLogger::logError(sprintf("Merging scratch Achilles tables [ERROR] (%s)", e)) - }) + ) } else { - tryCatch({ - cluster <- ParallelLogger::makeCluster(numberOfThreads = numThreads, singleThreadToMain = TRUE) - connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) - clusterApplyResults <- - ParallelLogger::clusterApply(cluster = cluster, x = mergeSqls, function(sql) { - DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) - }) - }, error = function(e) { - ParallelLogger::logError( - sprintf("Merging scratch Achilles tables (merging scratch Achilles tables) [ERROR] (%s)", e) - ) - DatabaseConnector::disconnect(connection = connection) - ParallelLogger::stopCluster(cluster = cluster) - }) + tryCatch( + { + cluster <- ParallelLogger::makeCluster(numberOfThreads = numThreads, singleThreadToMain = TRUE) + connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) + clusterApplyResults <- + ParallelLogger::clusterApply(cluster = cluster, x = mergeSqls, function(sql) { + DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) + }) + }, + error = function(e) { + ParallelLogger::logError( + sprintf("Merging scratch Achilles tables (merging scratch Achilles tables) [ERROR] (%s)", e) + ) + DatabaseConnector::disconnect(connection = connection) + ParallelLogger::stopCluster(cluster = cluster) + } + ) } } - + if (!sqlOnly) { ParallelLogger::logInfo( - sprintf("Done. Achilles results can now be found in schema %s", resultsDatabaseSchema ) + sprintf("Done. Achilles results can now be found in schema %s", resultsDatabaseSchema) ) } - + # Clean up scratch tables - single threaded, drop and disconnect. For multithreaded, do not disconnect if (numThreads == 1 && dropScratchTables && !sqlOnly) { if (connectionDetails$dbms == "oracle") { @@ -598,16 +604,21 @@ achilles <- function(connectionDetails, # Oracle TEMP tables are created as persistent tables and are given randomly generated string # prefixes preceding tempAchillesPrefix, therefore, they need their own code to drop the # scratch tables. - + allTables <- DatabaseConnector::getTableNames(connection, scratchDatabaseSchema) - + tablesToDrop <- - c(allTables[which(grepl(tempAchillesPrefix, allTables, fixed = TRUE))], + c( + allTables[which(grepl(tempAchillesPrefix, allTables, fixed = TRUE))], allTables[which(grepl(tolower(tempAchillesPrefix), - allTables, fixed = TRUE))], allTables[which(grepl(toupper(tempAchillesPrefix), allTables, - fixed = TRUE))]) - + allTables, + fixed = TRUE + ))], allTables[which(grepl(toupper(tempAchillesPrefix), allTables, + fixed = TRUE + ))] + ) + dropSqls <- lapply(tablesToDrop, function(scratchTable) { sql <- SqlRender::render( @@ -619,10 +630,10 @@ achilles <- function(connectionDetails, sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) }) - + dropSqls <- unlist(dropSqls) for (k in 1:length(dropSqls)) { - DatabaseConnector::executeSql(connection, dropSqls[k],progressBar = F, reportOverallTime = F) + DatabaseConnector::executeSql(connection, dropSqls[k], progressBar = F, reportOverallTime = F) } ParallelLogger::logInfo( sprintf( @@ -630,7 +641,7 @@ achilles <- function(connectionDetails, scratchDatabaseSchema ) ) - + DatabaseConnector::disconnect(connection = connection) } else { ParallelLogger::logInfo( @@ -639,18 +650,17 @@ achilles <- function(connectionDetails, scratchDatabaseSchema ) ) - + dropAllScratchTables( connectionDetails = connectionDetails, scratchDatabaseSchema = scratchDatabaseSchema, - tempAchillesPrefix = tempAchillesPrefix, numThreads = numThreads, tableTypes = c("achilles"), outputFolder = outputFolder, defaultAnalysesOnly = defaultAnalysesOnly ) - + ParallelLogger::logInfo( sprintf( "Temporary Achilles tables removed from schema %s", @@ -664,18 +674,17 @@ achilles <- function(connectionDetails, "Dropping scratch Achilles tables from schema %s", scratchDatabaseSchema )) - + dropAllScratchTables( connectionDetails = connectionDetails, scratchDatabaseSchema = scratchDatabaseSchema, - tempAchillesPrefix = tempAchillesPrefix, numThreads = numThreads, tableTypes = c("achilles"), outputFolder = outputFolder, defaultAnalysesOnly = defaultAnalysesOnly ) - + ParallelLogger::logInfo( sprintf( "Temporary Achilles tables removed from schema %s", @@ -683,10 +692,10 @@ achilles <- function(connectionDetails, ) ) } - + # Create indices indicesSql <- "/* INDEX CREATION SKIPPED PER USER REQUEST */" - + if (createIndices) { achillesTables <- lapply(unique(analysisDetails$distribution), function(a) { @@ -707,20 +716,24 @@ achilles <- function(connectionDetails, ) } achillesSql <- c(achillesSql, indicesSql) - + if (sqlOnly) { SqlRender::writeSql( sql = paste(achillesSql, collapse = "\n\n"), - targetFile = file.path(outputFolder, - "achilles.sql") + targetFile = file.path( + outputFolder, + "achilles.sql" + ) ) ParallelLogger::logInfo(sprintf( "All Achilles SQL scripts can be found in folder: %s", - file.path(outputFolder, - "achilles.sql") + file.path( + outputFolder, + "achilles.sql" + ) )) } - + achillesResults <- list( resultsConnectionDetails = connectionDetails, @@ -733,11 +746,11 @@ achilles <- function(connectionDetails, indicesSql = indicesSql, call = match.call() ) - + class(achillesResults) <- "achillesResults" - + invisible(achillesResults) - + totalDelta <- Sys.time() - totalStart ParallelLogger::logInfo(sprintf("[Total Runtime] %f %s", totalDelta, attr(totalDelta, "units"))) } @@ -773,7 +786,6 @@ createIndices <- function(connectionDetails, sqlOnly = FALSE, verboseMode = TRUE, achillesTables = c("achilles_results", "achilles_results_dist")) { - # Log execution if (verboseMode) { appenders <- list( @@ -791,14 +803,16 @@ createIndices <- function(connectionDetails, ) ) } - logger <- ParallelLogger::createLogger(name = "createIndices", - threshold = "INFO", - appenders = appenders) + logger <- ParallelLogger::createLogger( + name = "createIndices", + threshold = "INFO", + appenders = appenders + ) ParallelLogger::registerLogger(logger) - + dropIndicesSql <- c() indicesSql <- c() - + # dbms specific index operations if (connectionDetails$dbms %in% c("redshift", "netezza", "bigquery", "snowflake", "spark")) { return(sprintf( @@ -806,7 +820,7 @@ createIndices <- function(connectionDetails, toupper(connectionDetails$dbms) )) } - + if (connectionDetails$dbms == "pdw") { indicesSql <- c( indicesSql, @@ -816,57 +830,59 @@ createIndices <- function(connectionDetails, ) ) } - + indices <- read.csv( file = system.file("csv", - "post_processing", - "indices.csv", - package = "Achilles"), + "post_processing", + "indices.csv", + package = "Achilles" + ), header = TRUE, stringsAsFactors = FALSE ) - + # create index SQLs for (i in 1:nrow(indices)) { - if (indices[i,]$table_name %in% achillesTables) { + if (indices[i, ]$table_name %in% achillesTables) { sql <- SqlRender::render( sql = "drop index @resultsDatabaseSchema.@indexName;", resultsDatabaseSchema = resultsDatabaseSchema, - - indexName = indices[i,]$INDEX_NAME + indexName = indices[i, ]$INDEX_NAME ) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) dropIndicesSql <- c(dropIndicesSql, sql) - + sql <- SqlRender::render( sql = "create index @indexName on @resultsDatabaseSchema.@tableName (@fields);", resultsDatabaseSchema = resultsDatabaseSchema, - tableName = indices[i,]$table_name, - indexName = indices[i,]$index_name, - fields = paste(strsplit( - x = indices[i,]$fields, split = "~" - )[[1]], - collapse = ",") + tableName = indices[i, ]$table_name, + indexName = indices[i, ]$index_name, + fields = paste( + strsplit( + x = indices[i, ]$fields, split = "~" + )[[1]], + collapse = "," + ) ) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) indicesSql <- c(indicesSql, sql) } } - + if (!sqlOnly) { connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) on.exit(DatabaseConnector::disconnect(connection = connection), add = TRUE) - - try(DatabaseConnector::executeSql(connection = connection,sql = paste(dropIndicesSql, collapse = "\n\n"), progressBar = F, reportOverallTime = F), silent = TRUE) - DatabaseConnector::executeSql(connection = connection,sql = paste(indicesSql, collapse = "\n\n"), progressBar = F, reportOverallTime = F) + + try(DatabaseConnector::executeSql(connection = connection, sql = paste(dropIndicesSql, collapse = "\n\n"), progressBar = F, reportOverallTime = F), silent = TRUE) + DatabaseConnector::executeSql(connection = connection, sql = paste(indicesSql, collapse = "\n\n"), progressBar = F, reportOverallTime = F) } - + ParallelLogger::unregisterLogger("createIndices") - + invisible(c(dropIndicesSql, indicesSql)) } @@ -923,58 +939,60 @@ dropAllScratchTables <- function(connectionDetails, outputFolder, verboseMode = TRUE, defaultAnalysesOnly = TRUE) { - appenders <- list( ParallelLogger::createFileAppender( layout = ParallelLogger::layoutParallel, fileName = file.path(outputFolder, "log_dropScratchTables.txt") ) ) - - logger <- ParallelLogger::createLogger(name = "dropAllScratchTables", - threshold = "INFO", - appenders = appenders + + logger <- ParallelLogger::createLogger( + name = "dropAllScratchTables", + threshold = "INFO", + appenders = appenders ) ParallelLogger::registerLogger(logger) - + # Initialize thread and scratchDatabaseSchema settings schemaDelim <- "." - + if (numThreads == 1 || scratchDatabaseSchema == "#") { numThreads <- 1 - + if (.supportsTempTables(connectionDetails) && - connectionDetails$dbms != "oracle") { + connectionDetails$dbms != "oracle") { scratchDatabaseSchema <- "#" schemaDelim <- "s_" } } - + if ("achilles" %in% tableTypes) { # Drop Achilles Scratch Tables analysisDetails <- getAnalysisDetails() - + if (defaultAnalysesOnly) { resultsTables <- lapply(analysisDetails$analysis_id[analysisDetails$distribution <= 0 & - analysisDetails$is_default == - 1], function(id) { - sprintf("%s_%d", tempAchillesPrefix, id) - }) + analysisDetails$is_default == + 1], function(id) { + sprintf("%s_%d", tempAchillesPrefix, id) + }) } else { resultsTables <- - lapply(analysisDetails$analysis_id[analysisDetails$distribution <= 0], - function(id) { - sprintf("%s_%d", tempAchillesPrefix, id) - }) + lapply( + analysisDetails$analysis_id[analysisDetails$distribution <= 0], + function(id) { + sprintf("%s_%d", tempAchillesPrefix, id) + } + ) } - + resultsDistTables <- lapply(analysisDetails$analysis_id[abs(analysisDetails$distribution) == - 1], function(id) { - sprintf("%s_dist_%d", tempAchillesPrefix, id) - }) - + 1], function(id) { + sprintf("%s_dist_%d", tempAchillesPrefix, id) + }) + dropSqls <- lapply(c(resultsTables, resultsDistTables), function(scratchTable) { sql <- @@ -987,23 +1005,26 @@ dropAllScratchTables <- function(connectionDetails, sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) }) - + cluster <- ParallelLogger::makeCluster(numberOfThreads = numThreads, singleThreadToMain = TRUE) connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) - on.exit(DatabaseConnector::disconnect(connection), add=TRUE) - + on.exit(DatabaseConnector::disconnect(connection), add = TRUE) + clusterApplyResults <- ParallelLogger::clusterApply(cluster = cluster, x = dropSqls, function(sql) { - tryCatch({ - DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) - }, error = function(e) { - ParallelLogger::logError(sprintf("Drop Achilles Scratch Table -- ERROR (%s)", e)) - }) + tryCatch( + { + DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) + }, + error = function(e) { + ParallelLogger::logError(sprintf("Drop Achilles Scratch Table -- ERROR (%s)", e)) + } + ) }) - + ParallelLogger::stopCluster(cluster = cluster) } - + ParallelLogger::unregisterLogger("dropAllScratchTables") } @@ -1036,7 +1057,6 @@ dropAllScratchTables <- function(connectionDetails, optimizeAtlasCache <- function(connectionDetails, resultsDatabaseSchema, vocabDatabaseSchema = resultsDatabaseSchema, - outputFolder = "output", sqlOnly = FALSE, verboseMode = TRUE, @@ -1064,21 +1084,25 @@ optimizeAtlasCache <- function(connectionDetails, ) } logger <- - ParallelLogger::createLogger(name = "optimizeAtlasCache", - threshold = "INFO", - appenders = appenders) + ParallelLogger::createLogger( + name = "optimizeAtlasCache", + threshold = "INFO", + appenders = appenders + ) ParallelLogger::registerLogger(logger) - - resultsConceptCountTable <- list(tablePrefix = tempAchillesPrefix, - schema = read.csv( - file = system.file( - "csv", - "schemas", - "schema_achilles_results_concept_count.csv", - package = "Achilles" - ), - header = TRUE - )) + + resultsConceptCountTable <- list( + tablePrefix = tempAchillesPrefix, + schema = read.csv( + file = system.file( + "csv", + "schemas", + "schema_achilles_results_concept_count.csv", + package = "Achilles" + ), + header = TRUE + ) + ) optimizeAtlasCacheSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "analyses/create_result_concept_table.sql", @@ -1087,7 +1111,8 @@ optimizeAtlasCache <- function(connectionDetails, resultsDatabaseSchema = resultsDatabaseSchema, vocabDatabaseSchema = vocabDatabaseSchema, fieldNames = paste(resultsConceptCountTable$schema$field_name, - collapse = ", ") + collapse = ", " + ) ) if (!sqlOnly) { connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) @@ -1101,16 +1126,18 @@ optimizeAtlasCache <- function(connectionDetails, DatabaseConnector::disconnect(connection = connection) }) } - + ParallelLogger::unregisterLogger("optimizeAtlasCache") - + invisible(optimizeAtlasCacheSql) } .getCdmVersion <- function(connectionDetails, cdmDatabaseSchema) { sql <- - SqlRender::render(sql = "select cdm_version from @cdmDatabaseSchema.cdm_source", - cdmDatabaseSchema = cdmDatabaseSchema) + SqlRender::render( + sql = "select cdm_version from @cdmDatabaseSchema.cdm_source", + cdmDatabaseSchema = cdmDatabaseSchema + ) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) @@ -1118,17 +1145,19 @@ optimizeAtlasCache <- function(connectionDetails, c <- tolower(( DatabaseConnector::querySql(connection = connection, sql = sql) - )[1,]) - gsub(pattern = "v", - replacement = "", - x = c) + )[1, ]) + gsub( + pattern = "v", + replacement = "", + x = c + ) }, error = function(e) { "" }, finally = { DatabaseConnector::disconnect(connection = connection) rm(connection) }) - + cdmVersion } @@ -1141,7 +1170,6 @@ optimizeAtlasCache <- function(connectionDetails, schemaDelim, scratchDatabaseSchema, cdmDatabaseSchema, - resultsDatabaseSchema, tempEmulationSchema, cdmVersion, @@ -1151,8 +1179,10 @@ optimizeAtlasCache <- function(connectionDetails, numThreads, outputFolder) { SqlRender::loadRenderTranslateSql( - sqlFilename = file.path("analyses", - paste(analysisId, "sql", sep = ".")), + sqlFilename = file.path( + "analyses", + paste(analysisId, "sql", sep = ".") + ), packageName = "Achilles", dbms = connectionDetails$dbms, warnOnMissingParameters = FALSE, @@ -1166,7 +1196,7 @@ optimizeAtlasCache <- function(connectionDetails, achilles_version = packageVersion(pkg = "Achilles"), cdmVersion = cdmVersion, singleThreaded = (scratchDatabaseSchema == - "#") + "#") ) } @@ -1193,49 +1223,55 @@ optimizeAtlasCache <- function(connectionDetails, fieldType = field["field_type"] ) }) - + # obtain the analysis SQLs to union in the merge if (!sqlOnly) { logs <- .parseLogs(logFile) } - detailSqls <- lapply(resultsTable$analysisIds[resultsTable$analysisIds %in% analysisIds], - function(analysisId) { - analysisSql <- SqlRender::render( - sql = "select @castedNames from @scratchDatabaseSchema@schemaDelim@tablePrefix_@analysisId", - scratchDatabaseSchema = scratchDatabaseSchema, - schemaDelim = schemaDelim, - castedNames = paste(castedNames, collapse = ", "), - tablePrefix = resultsTable$tablePrefix, - analysisId = analysisId - ) - - if (!sqlOnly) { - # obtain the runTime for this analysis - runTime <- .getAchillesResultBenchmark(analysisId, logs) - - benchmarkSelects <- - lapply(resultsTable$schema$field_name, function(c) { - if (tolower(c) == "analysis_id") { - sprintf("%d as analysis_id", - .getBenchmarkOffset() + as.integer(analysisId)) - } else if (tolower(c) == "stratum_1") { - sprintf("'%s' as stratum_1", runTime) - } else if (tolower(c) == "count_value") { - sprintf("%d as count_value", smallCellCount + 1) - } else { - sprintf("NULL as %s", c) - } - }) - - benchmarkSql <- SqlRender::render(sql = "select @benchmarkSelect", - benchmarkSelect = paste(benchmarkSelects, collapse = ", ")) - - analysisSql <- - paste(c(analysisSql, benchmarkSql), collapse = " union all ") - } - analysisSql - }) - + detailSqls <- lapply( + resultsTable$analysisIds[resultsTable$analysisIds %in% analysisIds], + function(analysisId) { + analysisSql <- SqlRender::render( + sql = "select @castedNames from @scratchDatabaseSchema@schemaDelim@tablePrefix_@analysisId", + scratchDatabaseSchema = scratchDatabaseSchema, + schemaDelim = schemaDelim, + castedNames = paste(castedNames, collapse = ", "), + tablePrefix = resultsTable$tablePrefix, + analysisId = analysisId + ) + + if (!sqlOnly) { + # obtain the runTime for this analysis + runTime <- .getAchillesResultBenchmark(analysisId, logs) + + benchmarkSelects <- + lapply(resultsTable$schema$field_name, function(c) { + if (tolower(c) == "analysis_id") { + sprintf( + "%d as analysis_id", + .getBenchmarkOffset() + as.integer(analysisId) + ) + } else if (tolower(c) == "stratum_1") { + sprintf("'%s' as stratum_1", runTime) + } else if (tolower(c) == "count_value") { + sprintf("%d as count_value", smallCellCount + 1) + } else { + sprintf("NULL as %s", c) + } + }) + + benchmarkSql <- SqlRender::render( + sql = "select @benchmarkSelect", + benchmarkSelect = paste(benchmarkSelects, collapse = ", ") + ) + + analysisSql <- + paste(c(analysisSql, benchmarkSql), collapse = " union all ") + } + analysisSql + } + ) + SqlRender::loadRenderTranslateSql( sqlFilename = "analyses/merge_achilles_tables.sql", packageName = "Achilles", @@ -1253,15 +1289,17 @@ optimizeAtlasCache <- function(connectionDetails, .getSourceName <- function(connectionDetails, cdmDatabaseSchema) { sql <- - SqlRender::render(sql = "select cdm_source_name from @cdmDatabaseSchema.cdm_source", - cdmDatabaseSchema = cdmDatabaseSchema) + SqlRender::render( + sql = "select cdm_source_name from @cdmDatabaseSchema.cdm_source", + cdmDatabaseSchema = cdmDatabaseSchema + ) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) connection <- DatabaseConnector::connect(connectionDetails = connectionDetails) sourceName <- tryCatch({ s <- DatabaseConnector::querySql(connection = connection, sql = sql) - s[1,] + s[1, ] }, error = function(e) { "" }, finally = { @@ -1279,7 +1317,7 @@ optimizeAtlasCache <- function(connectionDetails, analysisDetails$analysis_id[analysisDetails$distribution == 0] distIds <- analysisDetails$analysis_id[analysisDetails$distribution == 1] - + if (length(resultIds) > 0) { sql <- SqlRender::render( @@ -1289,12 +1327,12 @@ optimizeAtlasCache <- function(connectionDetails, ) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) - + connection <- suppressMessages(DatabaseConnector::connect(connectionDetails = connectionDetails)) on.exit(DatabaseConnector::disconnect(connection = connection)) DatabaseConnector::executeSql(connection = connection, sql = sql, progressBar = F, reportOverallTime = F) } - + if (length(distIds) > 0) { sql <- SqlRender::render( @@ -1316,35 +1354,37 @@ optimizeAtlasCache <- function(connectionDetails, analysisIds) { conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(conn)) - + sql <- "delete from @resultsDatabaseSchema.achilles_results where analysis_id in (@analysisIds);" sql <- SqlRender::render( sql, resultsDatabaseSchema = resultsDatabaseSchema, analysisIds = paste(analysisIds, - collapse = ",") + collapse = "," + ) ) sql <- SqlRender::translate(sql, targetDialect = connectionDetails$dbms) - + DatabaseConnector::executeSql(conn, sql, progressBar = F, reportOverallTime = F) - + sql <- "delete from @resultsDatabaseSchema.achilles_results_dist where analysis_id in (@analysisIds);" - sql <- SqlRender::render( sql, + sql <- SqlRender::render(sql, resultsDatabaseSchema = resultsDatabaseSchema, analysisIds = paste(analysisIds, - collapse = ",") + collapse = "," + ) ) sql <- SqlRender::translate(sql, targetDialect = connectionDetails$dbms) - + DatabaseConnector::executeSql(conn, sql, progressBar = F, reportOverallTime = F) } .getAchillesResultBenchmark <- function(analysisId, logs) { - logs <- logs[logs$analysis_id == analysisId,] + logs <- logs[logs$analysis_id == analysisId, ] if (nrow(logs) == 1) { - runTime <- logs[1,]$elapsed_seconds + runTime <- logs[1, ]$elapsed_seconds runTimeValue <- round(runTime[1], 2) runTimeValue } else { @@ -1359,20 +1399,22 @@ optimizeAtlasCache <- function(connectionDetails, sep = "\t", stringsAsFactors = FALSE ) - + names(logs) <- - c("startTime", + c( + "startTime", "thread", "logType", "package", "packageFunction", - "comment") - logs <- logs[grepl(pattern = "COMPLETE", x = logs$comment),] + "comment" + ) + logs <- logs[grepl(pattern = "COMPLETE", x = logs$comment), ] logs$analysisId <- logs$runTime <- NA - + for (i in 1:nrow(logs)) { - logs[i,]$analysisId <- .parseAnalysisId(logs[i,]$comment) - logs[i,]$runTime <- .parseRunTime(logs[i,]$comment) + logs[i, ]$analysisId <- .parseAnalysisId(logs[i, ]$comment) + logs[i, ]$runTime <- .parseRunTime(logs[i, ]$comment) } logs } diff --git a/R/createTimeSeries.r b/R/createTimeSeries.r index 1e73a313..adf77906 100644 --- a/R/createTimeSeries.r +++ b/R/createTimeSeries.r @@ -1,6 +1,6 @@ # @file createTimeSeries # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -49,9 +49,15 @@ #' @examples #' # Example 1: #' temporalData <- data.frame(START_DATE = seq.Date(as.Date("20210101", "%Y%m%d"), -#' as.Date("20231201", -#' "%Y%m%d"), by = "month"), COUNT_VALUE = round(runif(36, 1, 1000)), PREVALENCE = round(runif(36, -#' 0, 10), 2), PROPORTION_WITHIN_YEAR = round(runif(36, 0, 1), 2), stringsAsFactors = FALSE) +#' as.Date( +#' "20231201", +#' "%Y%m%d" +#' ), +#' by = "month" +#' ), COUNT_VALUE = round(runif(36, 1, 1000)), PREVALENCE = round(runif( +#' 36, +#' 0, 10 +#' ), 2), PROPORTION_WITHIN_YEAR = round(runif(36, 0, 1), 2), stringsAsFactors = FALSE) #' dummyTs <- createTimeSeries(temporalData) #' dummyTs.cv <- dummyTs[, "COUNT_VALUE"] #' dummyTs.pv <- dummyTs[, "PREVALENCE"] @@ -60,8 +66,10 @@ #' \dontrun{ #' # Example 2: #' pneumonia <- 255848 -#' temporalData <- getTemporalData(connectionDetails = connectionDetails, cdmDatabaseSchema = "cdm", -#' resultsDatabaseSchema = "results", conceptId = pneumonia) +#' temporalData <- getTemporalData( +#' connectionDetails = connectionDetails, cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", conceptId = pneumonia +#' ) #' pneumoniaTs <- createTimeSeries(temporalData) #' pneumoniaTs.cv <- pneumoniaTs[, "COUNT_VALUE"] #' pneumoniaTs.pv <- pneumoniaTs[, "PREVALENCE"] @@ -71,13 +79,16 @@ #' @export createTimeSeries <- function(temporalData) { - requiredColumns <- c("START_DATE", "COUNT_VALUE", "PREVALENCE", "PROPORTION_WITHIN_YEAR") - if (sum(colnames(temporalData) %in% requiredColumns) < 4) - stop(paste0("ERROR: INVALID DATA FRAME FORMAT. The data frame must contain columns: ", - paste(requiredColumns, - collapse = ", "))) + if (sum(colnames(temporalData) %in% requiredColumns) < 4) { + stop(paste0( + "ERROR: INVALID DATA FRAME FORMAT. The data frame must contain columns: ", + paste(requiredColumns, + collapse = ", " + ) + )) + } if (nrow(temporalData) == 0) { stop("ERROR: Cannot create time series from an empty data frame") @@ -95,9 +106,13 @@ createTimeSeries <- function(temporalData) { # series lastRow <- nrow(resultSetData) - denseDates <- seq.Date(from = as.Date(resultSetData$START_DATE[1], "%Y%m%d"), - to = as.Date(resultSetData$START_DATE[lastRow], - "%Y%m%d"), by = "month") + denseDates <- seq.Date( + from = as.Date(resultSetData$START_DATE[1], "%Y%m%d"), + to = as.Date( + resultSetData$START_DATE[lastRow], + "%Y%m%d" + ), by = "month" + ) # Find gaps, if any, in data (e.g., dates that have no data, give that date a 0 count and 0 # prevalence) @@ -110,23 +125,33 @@ createTimeSeries <- function(temporalData) { joinResults$PROPORTION_WITHIN_YEAR[which(is.na(joinResults$PROPORTION_WITHIN_YEAR))] <- 0 # Now that we no longer have sparse dates, keep only necessary columns and build the time series - joinResults <- joinResults[, c("START_DATE", - "COUNT_VALUE", - "PREVALENCE", - "PROPORTION_WITHIN_YEAR")] + joinResults <- joinResults[, c( + "START_DATE", + "COUNT_VALUE", + "PREVALENCE", + "PROPORTION_WITHIN_YEAR" + )] # Find the end of the dense results lastRow <- nrow(joinResults) # Create the multivariate time series - tsData <- data.frame(COUNT_VALUE = joinResults$COUNT_VALUE, PREVALENCE = joinResults$PREVALENCE, - PROPORTION_WITHIN_YEAR = joinResults$PROPORTION_WITHIN_YEAR) - - resultSetDataTs <- ts(data = tsData, start = c(as.numeric(substring(joinResults$START_DATE[1], 1, - 4)), as.numeric(substring(joinResults$START_DATE[1], - 6, - 7))), end = c(as.numeric(substring(joinResults$START_DATE[lastRow], - 1, 4)), as.numeric(substring(joinResults$START_DATE[lastRow], 6, 7))), frequency = 12) + tsData <- data.frame( + COUNT_VALUE = joinResults$COUNT_VALUE, PREVALENCE = joinResults$PREVALENCE, + PROPORTION_WITHIN_YEAR = joinResults$PROPORTION_WITHIN_YEAR + ) + + resultSetDataTs <- ts(data = tsData, start = c(as.numeric(substring( + joinResults$START_DATE[1], 1, + 4 + )), as.numeric(substring( + joinResults$START_DATE[1], + 6, + 7 + ))), end = c(as.numeric(substring( + joinResults$START_DATE[lastRow], + 1, 4 + )), as.numeric(substring(joinResults$START_DATE[lastRow], 6, 7))), frequency = 12) return(resultSetDataTs) } diff --git a/R/exportToAres.R b/R/exportToAres.R index 7fec7011..081242bf 100644 --- a/R/exportToAres.R +++ b/R/exportToAres.R @@ -9,17 +9,21 @@ normalizeEmptyValue <- function(x) { } } +querySqlWithUpperCaseColumns <- function(...) { + DatabaseConnector::querySql(...) |> + dplyr::rename_with(toupper) +} + saveConceptsAsJson <- function( - concept_id, - reports, - columnsToNormalize, - columnsToConvertToDataFrame, - dir -) { - report <- reports[reports$CONCEPT_ID == concept_id,] + concept_id, + reports, + columnsToNormalize, + columnsToConvertToDataFrame, + dir) { + report <- reports[reports$CONCEPT_ID == concept_id, ] report <- as.list(report) - #Normalize the specified columns + # Normalize the specified columns for (col in columnsToNormalize) { report[[col]] <- normalizeEmptyValue(report[[col]]) } @@ -37,11 +41,10 @@ saveConceptsAsJson <- function( } saveConceptsAsDuckDb <- function( - duckdbCon, - conceptData, - domain, - schema -) { + duckdbCon, + conceptData, + domain, + schema) { for (tableName in names(conceptData$reports)) { tableData <- conceptData$reports[[tableName]] @@ -68,15 +71,14 @@ saveConceptsAsDuckDb <- function( } processAndExportConceptData <- function( - duckdbCon, - conceptData, - outputPath, - outputFormat, - columnsToNormalize, - columnsToConvertToDataFrame, - domain, - schema -) { + duckdbCon, + conceptData, + outputPath, + outputFormat, + columnsToNormalize, + columnsToConvertToDataFrame, + domain, + schema) { if (is.null(conceptData)) { return() } @@ -109,8 +111,7 @@ processAndExportConceptData <- function( } -generateAOProcedureReports <- function(connectionDetails, proceduresData, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOProcedureReports <- function(connectionDetails, proceduresData, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { if (nrow(proceduresData) == 0) { return(NULL) } @@ -157,25 +158,25 @@ generateAOProcedureReports <- function(connectionDetails, proceduresData, cdmDat conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataProceduresByType <- - DatabaseConnector::querySql(conn, queryProceduresByType) %>% - dplyr::select(c("CONCEPT_ID" = "PROCEDURE_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryProceduresByType) %>% + dplyr::select(c("CONCEPT_ID" = "PROCEDURE_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataAgeAtFirstOccurrence <- - DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstOccurrence) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataProcedureFrequencyDistribution <- - DatabaseConnector::querySql(conn, queryProcedureFrequencyDistribution) %>% - dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) + querySqlWithUpperCaseColumns(conn, queryProcedureFrequencyDistribution) %>% + dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) uniqueConcepts <- data.frame( @@ -184,18 +185,18 @@ generateAOProcedureReports <- function(connectionDetails, proceduresData, cdmDat ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - proceduresData, - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "CDM_TABLE_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) + dplyr::left_join( + proceduresData, + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "CDM_TABLE_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) if (outputFormat == "duckdb") { reports <- list( @@ -209,50 +210,49 @@ generateAOProcedureReports <- function(connectionDetails, proceduresData, cdmDat } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataProcedureFrequencyDistribution %>% - tidyr::nest(PROCEDURE_FREQUENCY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataProceduresByType %>% - tidyr::nest(PROCEDURES_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstOccurrence %>% - tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataProcedureFrequencyDistribution %>% + tidyr::nest(PROCEDURE_FREQUENCY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataProceduresByType %>% + tidyr::nest(PROCEDURES_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstOccurrence %>% + tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) -{ - output = { } +generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) { + output <- { } conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) renderedSql <- SqlRender::loadRenderTranslateSql( @@ -265,8 +265,8 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results vocab_database_schema = vocabDatabaseSchema ) - personSummaryData <- DatabaseConnector::querySql(conn, renderedSql) - output$SUMMARY = personSummaryData + personSummaryData <- querySqlWithUpperCaseColumns(conn, renderedSql) + output$SUMMARY <- personSummaryData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/person/population_age_gender.sql", @@ -277,8 +277,8 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - ageGenderData <- DatabaseConnector::querySql(conn, renderedSql) - output$AGE_GENDER_DATA = ageGenderData + ageGenderData <- querySqlWithUpperCaseColumns(conn, renderedSql) + output$AGE_GENDER_DATA <- ageGenderData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/person/gender.sql", @@ -289,8 +289,8 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - genderData <- DatabaseConnector::querySql(conn, renderedSql) - output$GENDER_DATA = genderData + genderData <- querySqlWithUpperCaseColumns(conn, renderedSql) + output$GENDER_DATA <- genderData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/person/race.sql", @@ -301,8 +301,8 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - raceData <- DatabaseConnector::querySql(conn, renderedSql) - output$RACE_DATA = raceData + raceData <- querySqlWithUpperCaseColumns(conn, renderedSql) + output$RACE_DATA <- raceData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/person/ethnicity.sql", @@ -313,8 +313,8 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - ethnicityData <- DatabaseConnector::querySql(conn, renderedSql) - output$ETHNICITY_DATA = ethnicityData + ethnicityData <- querySqlWithUpperCaseColumns(conn, renderedSql) + output$ETHNICITY_DATA <- ethnicityData renderedSql <- SqlRender::loadRenderTranslateSql( @@ -326,32 +326,29 @@ generateAOPersonReport <- function(connectionDetails, cdmDatabaseSchema, results results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - birthYearData <- DatabaseConnector::querySql(conn, renderedSql) + birthYearData <- querySqlWithUpperCaseColumns(conn, renderedSql) output$BIRTH_YEAR_DATA <- birthYearData return(output) } -generateAOAchillesPerformanceReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) -{ - - queryAchillesPerformance <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/performance/sqlAchillesPerformance.sql", - packageName = "Achilles", - dbms = connection@dbms, - warnOnMissingParameters = FALSE, - cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, - vocab_database_schema = vocabDatabaseSchema +generateAOAchillesPerformanceReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) { + queryAchillesPerformance <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/performance/sqlAchillesPerformance.sql", + packageName = "Achilles", + dbms = connection@dbms, + warnOnMissingParameters = FALSE, + cdm_database_schema = cdmDatabaseSchema, + results_database_schema = resultsDatabaseSchema, + vocab_database_schema = vocabDatabaseSchema ) - dataPerformance <- DatabaseConnector::querySql(connection, queryAchillesPerformance) + dataPerformance <- querySqlWithUpperCaseColumns(connection, queryAchillesPerformance) names(dataPerformance) <- c("analysis_id", "analysis_name", "category", "elapsed_seconds") dataPerformance$elapsed_seconds <- format(round(as.numeric(dataPerformance$elapsed_seconds), digits = 2), nsmall = 2) return(dataPerformance) } -generateAODeathReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) -{ - +generateAODeathReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) { queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/death/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", @@ -382,29 +379,28 @@ generateAODeathReport <- function(connection, cdmDatabaseSchema, resultsDatabase results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - deathByTypeData <- DatabaseConnector::querySql(connection, queryDeathByType) - prevalenceByGenderAgeYearData <- DatabaseConnector::querySql(connection, queryPrevalenceByGenderAgeYear) - prevalenceByMonthData <- DatabaseConnector::querySql(connection, queryPrevalenceByMonth) - ageAtDeathData <- DatabaseConnector::querySql(connection, queryAgeAtDeath) + deathByTypeData <- querySqlWithUpperCaseColumns(connection, queryDeathByType) + prevalenceByGenderAgeYearData <- querySqlWithUpperCaseColumns(connection, queryPrevalenceByGenderAgeYear) + prevalenceByMonthData <- querySqlWithUpperCaseColumns(connection, queryPrevalenceByMonth) + ageAtDeathData <- querySqlWithUpperCaseColumns(connection, queryAgeAtDeath) - output = { } - output$PREVALENCE_BY_GENDER_AGE_YEAR = prevalenceByGenderAgeYearData - output$PREVALENCE_BY_MONTH = prevalenceByMonthData - output$DEATH_BY_TYPE = deathByTypeData - output$AGE_AT_DEATH = ageAtDeathData + output <- { } + output$PREVALENCE_BY_GENDER_AGE_YEAR <- prevalenceByGenderAgeYearData + output$PREVALENCE_BY_MONTH <- prevalenceByMonthData + output$DEATH_BY_TYPE <- deathByTypeData + output$AGE_AT_DEATH <- ageAtDeathData return(output) } -generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) -{ - output = { } +generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputPath) { + output <- { } renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/ageatfirst.sql", packageName = "Achilles", dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - ageAtFirstObservationData <- DatabaseConnector::querySql(connection, renderedSql) + ageAtFirstObservationData <- querySqlWithUpperCaseColumns(connection, renderedSql) output$AGE_AT_FIRST_OBSERVATION <- ageAtFirstObservationData renderedSql <- SqlRender::loadRenderTranslateSql( @@ -414,8 +410,8 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - ageByGenderData <- DatabaseConnector::querySql(connection, renderedSql) - output$AGE_BY_GENDER = ageByGenderData + ageByGenderData <- querySqlWithUpperCaseColumns(connection, renderedSql) + output$AGE_BY_GENDER <- ageByGenderData observationLengthHist <- { } renderedSql <- SqlRender::loadRenderTranslateSql( @@ -424,11 +420,11 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - observationLengthStats <- DatabaseConnector::querySql(connection, renderedSql) - observationLengthHist$MIN = observationLengthStats$MIN_VALUE - observationLengthHist$MAX = observationLengthStats$MAX_VALUE - observationLengthHist$INTERVAL_SIZE = observationLengthStats$INTERVAL_SIZE - observationLengthHist$INTERVALS = (observationLengthStats$MAX_VALUE - observationLengthStats$MIN_VALUE) / observationLengthStats$INTERVAL_SIZE + observationLengthStats <- querySqlWithUpperCaseColumns(connection, renderedSql) + observationLengthHist$MIN <- observationLengthStats$MIN_VALUE + observationLengthHist$MAX <- observationLengthStats$MAX_VALUE + observationLengthHist$INTERVAL_SIZE <- observationLengthStats$INTERVAL_SIZE + observationLengthHist$INTERVALS <- (observationLengthStats$MAX_VALUE - observationLengthStats$MIN_VALUE) / observationLengthStats$INTERVAL_SIZE renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/observationlength_data.sql", @@ -436,8 +432,8 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - observationLengthData <- DatabaseConnector::querySql(connection, renderedSql) - output$OBSERVATION_LENGTH_HISTOGRAM = observationLengthHist + observationLengthData <- querySqlWithUpperCaseColumns(connection, renderedSql) + output$OBSERVATION_LENGTH_HISTOGRAM <- observationLengthHist renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/cumulativeduration.sql", @@ -445,11 +441,11 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - cumulativeDurationData <- DatabaseConnector::querySql(connection, renderedSql) + cumulativeDurationData <- querySqlWithUpperCaseColumns(connection, renderedSql) cumulativeDurationData$X_LENGTH_OF_OBSERVATION <- cumulativeDurationData$X_LENGTH_OF_OBSERVATION / 365.25 cumulativeDurationData$SERIES_NAME <- NULL names(cumulativeDurationData) <- c("YEARS", "PERCENT_PEOPLE") - output$CUMULATIVE_DURATION = cumulativeDurationData + output$CUMULATIVE_DURATION <- cumulativeDurationData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/observationlengthbygender.sql", @@ -458,7 +454,7 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - opLengthByGenderData <- DatabaseConnector::querySql(connection, renderedSql) + opLengthByGenderData <- querySqlWithUpperCaseColumns(connection, renderedSql) opLengthByGenderData$MIN_VALUE <- opLengthByGenderData$MIN_VALUE / 365.25 opLengthByGenderData$P10_VALUE <- opLengthByGenderData$P10_VALUE / 365.25 opLengthByGenderData$P25_VALUE <- opLengthByGenderData$P25_VALUE / 365.25 @@ -467,7 +463,7 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res opLengthByGenderData$P90_VALUE <- opLengthByGenderData$P90_VALUE / 365.25 opLengthByGenderData$MAX_VALUE <- opLengthByGenderData$MAX_VALUE / 365.25 - output$OBSERVATION_PERIOD_LENGTH_BY_GENDER = opLengthByGenderData + output$OBSERVATION_PERIOD_LENGTH_BY_GENDER <- opLengthByGenderData renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/observationlengthbyage.sql", @@ -475,7 +471,7 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - opLengthByAgeData <- DatabaseConnector::querySql(connection, renderedSql) + opLengthByAgeData <- querySqlWithUpperCaseColumns(connection, renderedSql) opLengthByAgeData$MIN_VALUE <- opLengthByAgeData$MIN_VALUE / 365.25 opLengthByAgeData$P10_VALUE <- opLengthByAgeData$P10_VALUE / 365.25 opLengthByAgeData$P25_VALUE <- opLengthByAgeData$P25_VALUE / 365.25 @@ -483,7 +479,7 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res opLengthByAgeData$P75_VALUE <- opLengthByAgeData$P75_VALUE / 365.25 opLengthByAgeData$P90_VALUE <- opLengthByAgeData$P90_VALUE / 365.25 opLengthByAgeData$MAX_VALUE <- opLengthByAgeData$MAX_VALUE / 365.25 - output$OBSERVATION_PERIOD_LENGTH_BY_AGE = opLengthByAgeData + output$OBSERVATION_PERIOD_LENGTH_BY_AGE <- opLengthByAgeData observedByYearHist <- { } renderedSql <- SqlRender::loadRenderTranslateSql( @@ -492,11 +488,11 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - observedByYearStats <- DatabaseConnector::querySql(connection, renderedSql) - observedByYearHist$MIN = observedByYearStats$MIN_VALUE - observedByYearHist$MAX = observedByYearStats$MAX_VALUE - observedByYearHist$INTERVAL_SIZE = observedByYearStats$INTERVAL_SIZE - observedByYearHist$INTERVALS = (observedByYearStats$MAX_VALUE - observedByYearStats$MIN_VALUE) / observedByYearStats$INTERVAL_SIZE + observedByYearStats <- querySqlWithUpperCaseColumns(connection, renderedSql) + observedByYearHist$MIN <- observedByYearStats$MIN_VALUE + observedByYearHist$MAX <- observedByYearStats$MAX_VALUE + observedByYearHist$INTERVAL_SIZE <- observedByYearStats$INTERVAL_SIZE + observedByYearHist$INTERVALS <- (observedByYearStats$MAX_VALUE - observedByYearStats$MIN_VALUE) / observedByYearStats$INTERVAL_SIZE renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/observedbyyear_data.sql", @@ -504,9 +500,9 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - observedByYearData <- DatabaseConnector::querySql(connection, renderedSql) + observedByYearData <- querySqlWithUpperCaseColumns(connection, renderedSql) observedByYearHist$DATA <- observedByYearData - output$OBSERVED_BY_YEAR_HISTOGRAM = observedByYearHist + output$OBSERVED_BY_YEAR_HISTOGRAM <- observedByYearHist observedByMonth <- { } renderedSql <- SqlRender::loadRenderTranslateSql( @@ -515,8 +511,8 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - observedByMonth <- DatabaseConnector::querySql(connection, renderedSql) - output$OBSERVED_BY_MONTH = observedByMonth + observedByMonth <- querySqlWithUpperCaseColumns(connection, renderedSql) + output$OBSERVED_BY_MONTH <- observedByMonth renderedSql <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/observationperiod/periodsperperson.sql", @@ -524,14 +520,12 @@ generateAOObservationPeriodReport <- function(connection, cdmDatabaseSchema, res dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - personPeriodsData <- DatabaseConnector::querySql(connection, renderedSql) - output$PERSON_PERIODS_DATA = personPeriodsData + personPeriodsData <- querySqlWithUpperCaseColumns(connection, renderedSql) + output$PERSON_PERIODS_DATA <- personPeriodsData return(output) } -generateAOVisitReports <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ - +generateAOVisitReports <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { queryVisits <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/visit/sqlVisitTreemap.sql", packageName = "Achilles", @@ -574,31 +568,31 @@ generateAOVisitReports <- function(connectionDetails, cdmDatabaseSchema, results conn <- DatabaseConnector::connect(connectionDetails) dataVisits <- - DatabaseConnector::querySql(conn, queryVisits) %>% - dplyr::rename(dplyr::all_of(c("CONCEPT_NAME" = "CONCEPT_PATH"))) %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) + querySqlWithUpperCaseColumns(conn, queryVisits) %>% + dplyr::rename(dplyr::all_of(c("CONCEPT_NAME" = "CONCEPT_PATH"))) %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) if (nrow(dataVisits) == 0) { return(NULL) } dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataVisitDurationByType <- - DatabaseConnector::querySql(conn, queryVisitDurationByType) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryVisitDurationByType) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataAgeAtFirstOccurrence <- - DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstOccurrence) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataVisits$CONCEPT_ID), @@ -606,10 +600,10 @@ generateAOVisitReports <- function(connectionDetails, cdmDatabaseSchema, results ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - dataVisits, - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + dataVisits, + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( concept_metadata = conceptMetadata, @@ -621,41 +615,40 @@ generateAOVisitReports <- function(connectionDetails, cdmDatabaseSchema, results } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataVisitDurationByType %>% - tidyr::nest(VISIT_DURATION_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstOccurrence %>% - tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataVisitDurationByType %>% + tidyr::nest(VISIT_DURATION_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstOccurrence %>% + tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOVisitDetailReports <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOVisitDetailReports <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { queryVisitDetails <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/visitdetail/sqlVisitDetailTreemap.sql", packageName = "Achilles", @@ -707,32 +700,32 @@ generateAOVisitDetailReports <- function(connectionDetails, cdmDatabaseSchema, r conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataVisitDetails <- - DatabaseConnector::querySql(conn, queryVisitDetails) %>% - dplyr::rename(dplyr::all_of(c("CONCEPT_NAME" = "CONCEPT_PATH"))) %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) + querySqlWithUpperCaseColumns(conn, queryVisitDetails) %>% + dplyr::rename(dplyr::all_of(c("CONCEPT_NAME" = "CONCEPT_PATH"))) %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) if (nrow(dataVisitDetails) == 0) { return(NULL) } dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataVisitDetailDurationByType <- - DatabaseConnector::querySql(conn, queryVisitDetailDurationByType) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryVisitDetailDurationByType) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataAgeAtFirstOccurrence <- - DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstOccurrence) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataVisitDetails$CONCEPT_ID), @@ -740,10 +733,10 @@ generateAOVisitDetailReports <- function(connectionDetails, cdmDatabaseSchema, r ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - dataVisitDetails, - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + dataVisitDetails, + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -756,56 +749,53 @@ generateAOVisitDetailReports <- function(connectionDetails, cdmDatabaseSchema, r } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataVisitDetailDurationByType %>% - tidyr::nest(VISIT_DETAIL_DURATION_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstOccurrence %>% - tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataVisitDetailDurationByType %>% + tidyr::nest(VISIT_DETAIL_DURATION_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstOccurrence %>% + tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOMetadataReport <- function(connection, cdmDatabaseSchema, outputPath) -{ - if (DatabaseConnector::existsTable(connection = connection, databaseSchema = cdmDatabaseSchema, tableName = "METADATA")) - { +generateAOMetadataReport <- function(connection, cdmDatabaseSchema, outputPath) { + if (DatabaseConnector::existsTable(connection = connection, databaseSchema = cdmDatabaseSchema, tableName = "METADATA")) { queryMetadata <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/metadata/sqlMetadata.sql", packageName = "Achilles", dbms = connection@dbms, cdm_database_schema = cdmDatabaseSchema ) - dataMetadata <- DatabaseConnector::querySql(connection, queryMetadata) + dataMetadata <- querySqlWithUpperCaseColumns(connection, queryMetadata) return(dataMetadata) } } -generateAOObservationReports <- function(connectionDetails, observationsData, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOObservationReports <- function(connectionDetails, observationsData, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { if (nrow(observationsData) == 0) { return(NULL) } @@ -853,20 +843,20 @@ generateAOObservationReports <- function(connectionDetails, observationsData, cd conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataObservationsByType <- - DatabaseConnector::querySql(conn, queryObservationsByType) %>% - dplyr::select(c("CONCEPT_ID" = "OBSERVATION_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryObservationsByType) %>% + dplyr::select(c("CONCEPT_ID" = "OBSERVATION_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataAgeAtFirstOccurrence <- - DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstOccurrence) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataObsFrequencyDistribution <- - DatabaseConnector::querySql(conn, queryObsFrequencyDistribution) %>% - dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) + querySqlWithUpperCaseColumns(conn, queryObsFrequencyDistribution) %>% + dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(observationsData$CONCEPT_ID), @@ -874,18 +864,18 @@ generateAOObservationReports <- function(connectionDetails, observationsData, cd ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - observationsData, - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "CDM_TABLE_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) + dplyr::left_join( + observationsData, + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "CDM_TABLE_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) if (outputFormat == "duckdb") { reports <- list( @@ -899,50 +889,48 @@ generateAOObservationReports <- function(connectionDetails, observationsData, cd } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataObsFrequencyDistribution %>% - tidyr::nest(OBS_FREQUENCY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataObservationsByType %>% - tidyr::nest(OBSERVATIONS_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstOccurrence %>% - tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataObsFrequencyDistribution %>% + tidyr::nest(OBS_FREQUENCY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataObservationsByType %>% + tidyr::nest(OBSERVATIONS_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstOccurrence %>% + tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOCdmSourceReport <- function(connection, cdmDatabaseSchema, outputPath) -{ - if (DatabaseConnector::existsTable(connection = connection, databaseSchema = cdmDatabaseSchema, tableName = "CDM_SOURCE")) - { +generateAOCdmSourceReport <- function(connection, cdmDatabaseSchema, outputPath) { + if (DatabaseConnector::existsTable(connection = connection, databaseSchema = cdmDatabaseSchema, tableName = "CDM_SOURCE")) { queryCdmSource <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/metadata/sqlCdmSource.sql", packageName = "Achilles", @@ -950,13 +938,12 @@ generateAOCdmSourceReport <- function(connection, cdmDatabaseSchema, outputPath) cdm_database_schema = cdmDatabaseSchema ) - dataCdmSource <- DatabaseConnector::querySql(connection, queryCdmSource) + dataCdmSource <- querySqlWithUpperCaseColumns(connection, queryCdmSource) return(dataCdmSource) } } -generateAODashboardReport <- function(outputPath) -{ +generateAODashboardReport <- function(outputPath) { output <- { } personReport <- jsonlite::fromJSON(file = paste(outputPath, "/person.json", sep = "")) output$SUMMARY <- personReport$SUMMARY @@ -971,8 +958,7 @@ generateAODashboardReport <- function(outputPath) write(jsonOutput, file = paste(outputPath, "/dashboard.json", sep = "")) } -generateAOMeasurementReports <- function(connectionDetails, dataMeasurements, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOMeasurementReports <- function(connectionDetails, dataMeasurements, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/measurement/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", @@ -1056,39 +1042,39 @@ generateAOMeasurementReports <- function(connectionDetails, dataMeasurements, cd conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) if (nrow(dataPrevalenceByMonth) == 0) { return(NULL) } dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataMeasurementsByType <- - DatabaseConnector::querySql(conn, queryMeasurementsByType) %>% - dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryMeasurementsByType) %>% + dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataAgeAtFirstOccurrence <- - DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstOccurrence) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataRecordsByUnit <- - DatabaseConnector::querySql(conn, queryRecordsByUnit) %>% - dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE", "UNIT_CONCEPT_ID")) + querySqlWithUpperCaseColumns(conn, queryRecordsByUnit) %>% + dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE", "UNIT_CONCEPT_ID")) dataMeasurementValueDistribution <- - DatabaseConnector::querySql(conn, queryMeasurementValueDistribution) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE", "UNIT_CONCEPT_ID")) + querySqlWithUpperCaseColumns(conn, queryMeasurementValueDistribution) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE", "UNIT_CONCEPT_ID")) dataLowerLimitDistribution <- - DatabaseConnector::querySql(conn, queryLowerLimitDistribution) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryLowerLimitDistribution) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataUpperLimitDistribution <- - DatabaseConnector::querySql(conn, queryUpperLimitDistribution) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryUpperLimitDistribution) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataValuesRelativeToNorm <- - DatabaseConnector::querySql(conn, queryValuesRelativeToNorm) %>% - dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryValuesRelativeToNorm) %>% + dplyr::select(c("CONCEPT_ID" = "MEASUREMENT_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataFrequencyDistribution <- - DatabaseConnector::querySql(conn, queryFrequencyDistribution) %>% - dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) + querySqlWithUpperCaseColumns(conn, queryFrequencyDistribution) %>% + dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataPrevalenceByMonth$CONCEPT_ID), @@ -1096,19 +1082,19 @@ generateAOMeasurementReports <- function(connectionDetails, dataMeasurements, cd ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataMeasurements %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataMeasurements %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1127,83 +1113,82 @@ generateAOMeasurementReports <- function(connectionDetails, dataMeasurements, cd } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataFrequencyDistribution %>% - tidyr::nest(FREQUENCY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataMeasurementsByType %>% - tidyr::nest(MEASUREMENTS_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstOccurrence %>% - tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataRecordsByUnit %>% - tidyr::nest(RECORDS_BY_UNIT = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataMeasurementValueDistribution %>% - tidyr::nest(MEASUREMENT_VALUE_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataLowerLimitDistribution %>% - tidyr::nest(LOWER_LIMIT_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataUpperLimitDistribution %>% - tidyr::nest(UPPER_LIMIT_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataValuesRelativeToNorm %>% - tidyr::nest(VALUES_RELATIVE_TO_NORM = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataFrequencyDistribution %>% + tidyr::nest(FREQUENCY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataMeasurementsByType %>% + tidyr::nest(MEASUREMENTS_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstOccurrence %>% + tidyr::nest(AGE_AT_FIRST_OCCURRENCE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataRecordsByUnit %>% + tidyr::nest(RECORDS_BY_UNIT = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataMeasurementValueDistribution %>% + tidyr::nest(MEASUREMENT_VALUE_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataLowerLimitDistribution %>% + tidyr::nest(LOWER_LIMIT_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataUpperLimitDistribution %>% + tidyr::nest(UPPER_LIMIT_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataValuesRelativeToNorm %>% + tidyr::nest(VALUES_RELATIVE_TO_NORM = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAODrugEraReports <- function(connectionDetails, dataDrugEra, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAODrugEraReports <- function(connectionDetails, dataDrugEra, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { if (nrow(dataDrugEra) == 0) { return(NULL) } @@ -1243,17 +1228,17 @@ generateAODrugEraReports <- function(connectionDetails, dataDrugEra, cdmDatabase conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataAgeAtFirstExposure <- - DatabaseConnector::querySql(conn, queryAgeAtFirstExposure) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstExposure) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataLengthOfEra <- - DatabaseConnector::querySql(conn, queryLengthOfEra) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryLengthOfEra) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataDrugEra$CONCEPT_ID), @@ -1261,19 +1246,19 @@ generateAODrugEraReports <- function(connectionDetails, dataDrugEra, cdmDatabase ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataDrugEra %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataDrugEra %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1286,42 +1271,40 @@ generateAODrugEraReports <- function(connectionDetails, dataDrugEra, cdmDatabase } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataAgeAtFirstExposure %>% - tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataLengthOfEra %>% - tidyr::nest(LENGTH_OF_ERA = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataAgeAtFirstExposure %>% + tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataLengthOfEra %>% + tidyr::nest(LENGTH_OF_ERA = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAODrugReports <- function(connectionDetails, dataDrugs, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ - +generateAODrugReports <- function(connectionDetails, dataDrugs, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { queryAgeAtFirstExposure <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/drug/sqlAgeAtFirstExposure.sql", packageName = "Achilles", @@ -1389,33 +1372,33 @@ generateAODrugReports <- function(connectionDetails, dataDrugs, cdmDatabaseSchem conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) if (nrow(dataPrevalenceByMonth) == 0) { return(NULL) } dataAgeAtFirstExposure <- - DatabaseConnector::querySql(conn, queryAgeAtFirstExposure) %>% - dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstExposure) %>% + dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataDaysSupplyDistribution <- - DatabaseConnector::querySql(conn, queryDaysSupplyDistribution) %>% - dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryDaysSupplyDistribution) %>% + dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataDrugsByType <- - DatabaseConnector::querySql(conn, queryDrugsByType) %>% - dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryDrugsByType) %>% + dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataQuantityDistribution <- - DatabaseConnector::querySql(conn, queryQuantityDistribution) %>% - dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryQuantityDistribution) %>% + dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataRefillsDistribution <- - DatabaseConnector::querySql(conn, queryRefillsDistribution) %>% - dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryRefillsDistribution) %>% + dplyr::select(c("CONCEPT_ID" = "DRUG_CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataDrugFrequencyDistribution <- - DatabaseConnector::querySql(conn, queryDrugFrequencyDistribution) %>% - dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) + querySqlWithUpperCaseColumns(conn, queryDrugFrequencyDistribution) %>% + dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataPrevalenceByMonth$CONCEPT_ID), @@ -1423,19 +1406,19 @@ generateAODrugReports <- function(connectionDetails, dataDrugs, cdmDatabaseSchem ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataDrugs %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataDrugs %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1452,69 +1435,68 @@ generateAODrugReports <- function(connectionDetails, dataDrugs, cdmDatabaseSchem } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataAgeAtFirstExposure %>% - tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataDaysSupplyDistribution %>% - tidyr::nest(DAYS_SUPPLY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataDrugsByType %>% - tidyr::nest(DRUGS_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataDrugFrequencyDistribution %>% - tidyr::nest(DRUG_FREQUENCY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataQuantityDistribution %>% - tidyr::nest(QUANTITY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataRefillsDistribution %>% - tidyr::nest(REFILLS_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataAgeAtFirstExposure %>% + tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataDaysSupplyDistribution %>% + tidyr::nest(DAYS_SUPPLY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataDrugsByType %>% + tidyr::nest(DRUGS_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataDrugFrequencyDistribution %>% + tidyr::nest(DRUG_FREQUENCY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataQuantityDistribution %>% + tidyr::nest(QUANTITY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataRefillsDistribution %>% + tidyr::nest(REFILLS_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAODeviceReports <- function(connectionDetails, dataDevices, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAODeviceReports <- function(connectionDetails, dataDevices, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { if (nrow(dataDevices) == 0) { return(NULL) } @@ -1562,20 +1544,20 @@ generateAODeviceReports <- function(connectionDetails, dataDevices, cdmDatabaseS conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataAgeAtFirstExposure <- - DatabaseConnector::querySql(conn, queryAgeAtFirstExposure) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstExposure) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataDevicesByType <- - DatabaseConnector::querySql(conn, queryDevicesByType) %>% - dplyr::select(c("CONCEPT_ID" = "DEVICE_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryDevicesByType) %>% + dplyr::select(c("CONCEPT_ID" = "DEVICE_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataDeviceFrequencyDistribution <- - DatabaseConnector::querySql(conn, queryDeviceFrequencyDistribution) %>% - dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) + querySqlWithUpperCaseColumns(conn, queryDeviceFrequencyDistribution) %>% + dplyr::select(c("CONCEPT_ID", "Y_NUM_PERSONS", "X_COUNT")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataDevices$CONCEPT_ID), @@ -1583,19 +1565,19 @@ generateAODeviceReports <- function(connectionDetails, dataDevices, cdmDatabaseS ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataDevices %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataDevices %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1609,48 +1591,47 @@ generateAODeviceReports <- function(connectionDetails, dataDevices, cdmDatabaseS } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataAgeAtFirstExposure %>% - tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataDevicesByType %>% - tidyr::nest(DEVICES_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataDeviceFrequencyDistribution %>% - tidyr::nest(DEVICE_FREQUENCY_DISTRIBUTION = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataAgeAtFirstExposure %>% + tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataDevicesByType %>% + tidyr::nest(DEVICES_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataDeviceFrequencyDistribution %>% + tidyr::nest(DEVICE_FREQUENCY_DISTRIBUTION = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOConditionReports <- function(connectionDetails, dataConditions, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOConditionReports <- function(connectionDetails, dataConditions, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( sqlFilename = "export/condition/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", @@ -1694,21 +1675,21 @@ generateAOConditionReports <- function(connectionDetails, dataConditions, cdmDat conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) if (nrow(dataPrevalenceByMonth) == 0) { return(NULL) } dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataConditionsByType <- - DatabaseConnector::querySql(conn, queryConditionsByType) %>% - dplyr::select(c("CONCEPT_ID" = "CONDITION_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) + querySqlWithUpperCaseColumns(conn, queryConditionsByType) %>% + dplyr::select(c("CONCEPT_ID" = "CONDITION_CONCEPT_ID", "CONCEPT_NAME", "COUNT_VALUE")) dataAgeAtFirstDiagnosis <- - DatabaseConnector::querySql(conn, queryAgeAtFirstDiagnosis) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstDiagnosis) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataPrevalenceByMonth$CONCEPT_ID), @@ -1716,19 +1697,19 @@ generateAOConditionReports <- function(connectionDetails, dataConditions, cdmDat ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataConditions %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataConditions %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1741,41 +1722,40 @@ generateAOConditionReports <- function(connectionDetails, dataConditions, cdmDat } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataConditionsByType %>% - tidyr::nest(CONDITIONS_BY_TYPE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataAgeAtFirstDiagnosis %>% - tidyr::nest(AGE_AT_FIRST_DIAGNOSIS = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataConditionsByType %>% + tidyr::nest(CONDITIONS_BY_TYPE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataAgeAtFirstDiagnosis %>% + tidyr::nest(AGE_AT_FIRST_DIAGNOSIS = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } -generateAOConditionEraReports <- function(connectionDetails, dataConditionEra, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) -{ +generateAOConditionEraReports <- function(connectionDetails, dataConditionEra, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, outputFormat) { if (nrow(dataConditionEra) == 0) { return(NULL) } @@ -1823,17 +1803,17 @@ generateAOConditionEraReports <- function(connectionDetails, dataConditionEra, c conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) dataPrevalenceByGenderAgeYear <- - DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) %>% - dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByGenderAgeYear) %>% + dplyr::select(c("CONCEPT_ID", "TRELLIS_NAME", "SERIES_NAME", "X_CALENDAR_YEAR", "Y_PREVALENCE_1000PP")) dataPrevalenceByMonth <- - DatabaseConnector::querySql(conn, queryPrevalenceByMonth) %>% - dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) + querySqlWithUpperCaseColumns(conn, queryPrevalenceByMonth) %>% + dplyr::select(c("CONCEPT_ID", "X_CALENDAR_MONTH", "Y_PREVALENCE_1000PP")) dataLengthOfEra <- - DatabaseConnector::querySql(conn, queryLengthOfEra) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryLengthOfEra) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) dataAgeAtFirstDiagnosis <- - DatabaseConnector::querySql(conn, queryAgeAtFirstDiagnosis) %>% - dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) + querySqlWithUpperCaseColumns(conn, queryAgeAtFirstDiagnosis) %>% + dplyr::select(c("CONCEPT_ID", "CATEGORY", "MIN_VALUE", "P10_VALUE", "P25_VALUE", "MEDIAN_VALUE", "P75_VALUE", "P90_VALUE", "MAX_VALUE")) uniqueConcepts <- data.frame( CONCEPT_ID = unique(dataConditionEra$CONCEPT_ID), @@ -1841,19 +1821,19 @@ generateAOConditionEraReports <- function(connectionDetails, dataConditionEra, c ) conceptMetadata <- uniqueConcepts %>% - dplyr::left_join( - ( - dataConditionEra %>% - dplyr::select( - "CONCEPT_ID", - "CONCEPT_NAME", - "NUM_PERSONS", - "PERCENT_PERSONS", - "RECORDS_PER_PERSON" - ) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) + dplyr::left_join( + ( + dataConditionEra %>% + dplyr::select( + "CONCEPT_ID", + "CONCEPT_NAME", + "NUM_PERSONS", + "PERCENT_PERSONS", + "RECORDS_PER_PERSON" + ) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) if (outputFormat == "duckdb") { reports <- list( @@ -1866,35 +1846,35 @@ generateAOConditionEraReports <- function(connectionDetails, dataConditionEra, c } else { reports <- conceptMetadata %>% - dplyr::left_join( - ( - dataAgeAtFirstDiagnosis %>% - tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByGenderAgeYear %>% - tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataPrevalenceByMonth %>% - tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::left_join( - ( - dataLengthOfEra %>% - tidyr::nest(LENGTH_OF_ERA = c(-1)) - ), - by = c("CONCEPT_ID" = "CONCEPT_ID") - ) %>% - dplyr::collect() + dplyr::left_join( + ( + dataAgeAtFirstDiagnosis %>% + tidyr::nest(AGE_AT_FIRST_EXPOSURE = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByGenderAgeYear %>% + tidyr::nest(PREVALENCE_BY_GENDER_AGE_YEAR = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataPrevalenceByMonth %>% + tidyr::nest(PREVALENCE_BY_MONTH = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::left_join( + ( + dataLengthOfEra %>% + tidyr::nest(LENGTH_OF_ERA = c(-1)) + ), + by = c("CONCEPT_ID" = "CONCEPT_ID") + ) %>% + dplyr::collect() } return(list("reports" = reports, "uniqueConcepts" = uniqueConcepts)) } @@ -1908,7 +1888,7 @@ generateDataDensityTotal <- function(connection, resultsDatabaseSchema) { results_database_schema = resultsDatabaseSchema ) - totalRecordsData <- DatabaseConnector::querySql(connection, renderedSql) + totalRecordsData <- querySqlWithUpperCaseColumns(connection, renderedSql) colnames(totalRecordsData) <- c("domain", "date", "records") totalRecordsData$date <- lubridate::parse_date_time(totalRecordsData$date, "ym") @@ -1926,7 +1906,7 @@ generateLocationData <- function(connection, resultsDatabaseSchema) { results_database_schema = resultsDatabaseSchema ) - locationData <- DatabaseConnector::querySql(connection, renderedSql) + locationData <- querySqlWithUpperCaseColumns(connection, renderedSql) return(locationData) } @@ -1938,7 +1918,7 @@ generateDataDensityRecordsPerPerson <- function(connection, resultsDatabaseSchem results_database_schema = resultsDatabaseSchema ) - recordsPerPerson <- DatabaseConnector::querySql(connection, renderedSql) + recordsPerPerson <- querySqlWithUpperCaseColumns(connection, renderedSql) colnames(recordsPerPerson) <- c("domain", "date", "records") recordsPerPerson$date <- lubridate::parse_date_time(recordsPerPerson$date, "ym") recordsPerPerson$records <- round(recordsPerPerson$records, 2) @@ -1952,10 +1932,10 @@ generateDataDensityConceptsPerPerson <- function(connection, resultsDatabaseSche dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - conceptsPerPerson <- DatabaseConnector::querySql(connection, renderedSql) + conceptsPerPerson <- querySqlWithUpperCaseColumns(connection, renderedSql) return(conceptsPerPerson) - #data.table::fwrite(conceptsPerPerson, file=paste0(sourceOutputPath, "/datadensity-concepts-per-person.csv")) - #dbWriteTable(duckdbCon, "concepts_per_person", conceptsPerPerson) + # data.table::fwrite(conceptsPerPerson, file=paste0(sourceOutputPath, "/datadensity-concepts-per-person.csv")) + # dbWriteTable(duckdbCon, "concepts_per_person", conceptsPerPerson) } generateDataDensityDomainsPerPerson <- function(connection, resultsDatabaseSchema) { @@ -1965,11 +1945,11 @@ generateDataDensityDomainsPerPerson <- function(connection, resultsDatabaseSchem dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - domainsPerPerson <- DatabaseConnector::querySql(connection, renderedSql) + domainsPerPerson <- querySqlWithUpperCaseColumns(connection, renderedSql) domainsPerPerson$PERCENT_VALUE <- round(as.numeric(domainsPerPerson$PERCENT_VALUE), 2) return(domainsPerPerson) - #data.table::fwrite(domainsPerPerson, file=paste0(sourceOutputPath, "/datadensity-domains-per-person.csv")) - #dbWriteTable(duckdbCon, "domains_per_person", domainsPerPerson) + # data.table::fwrite(domainsPerPerson, file=paste0(sourceOutputPath, "/datadensity-domains-per-person.csv")) + # dbWriteTable(duckdbCon, "domains_per_person", domainsPerPerson) } generateDomainSummaryConditions <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -1980,14 +1960,14 @@ generateDomainSummaryConditions <- function(connection, resultsDatabaseSchema, v results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataConditions <- DatabaseConnector::querySql(connection, queryConditions) + dataConditions <- querySqlWithUpperCaseColumns(connection, queryConditions) dataConditions$PERCENT_PERSONS <- format(round(dataConditions$PERCENT_PERSONS, 4), nsmall = 4) dataConditions$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataConditions$PERCENT_PERSONS), 10) dataConditions$RECORDS_PER_PERSON <- format(round(dataConditions$RECORDS_PER_PERSON, 1), nsmall = 1) dataConditions$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataConditions$RECORDS_PER_PERSON), 10) return(dataConditions) - #data.table::fwrite(dataConditions, file=paste0(sourceOutputPath, "/domain-summary-condition_occurrence.csv")) - #dbWriteTable(duckdbCon, "domain_summary", dataConditions, append = TRUE) + # data.table::fwrite(dataConditions, file=paste0(sourceOutputPath, "/domain-summary-condition_occurrence.csv")) + # dbWriteTable(duckdbCon, "domain_summary", dataConditions, append = TRUE) } generateDomainSummaryConditionEras <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -1998,13 +1978,13 @@ generateDomainSummaryConditionEras <- function(connection, resultsDatabaseSchema results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataConditionEra <- DatabaseConnector::querySql(connection, queryConditionEra) + dataConditionEra <- querySqlWithUpperCaseColumns(connection, queryConditionEra) dataConditionEra$PERCENT_PERSONS <- format(round(dataConditionEra$PERCENT_PERSONS, 4), nsmall = 4) dataConditionEra$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataConditionEra$PERCENT_PERSONS), 10) dataConditionEra$RECORDS_PER_PERSON <- format(round(dataConditionEra$RECORDS_PER_PERSON, 1), nsmall = 1) dataConditionEra$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataConditionEra$RECORDS_PER_PERSON), 10) return(dataConditionEra) - #data.table::fwrite(dataConditionEra, file=paste0(sourceOutputPath, "/domain-summary-condition_era.csv")) + # data.table::fwrite(dataConditionEra, file=paste0(sourceOutputPath, "/domain-summary-condition_era.csv")) } generateDomainSummaryDrugs <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2015,13 +1995,13 @@ generateDomainSummaryDrugs <- function(connection, resultsDatabaseSchema, vocabD results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataDrugs <- DatabaseConnector::querySql(connection, queryDrugs) + dataDrugs <- querySqlWithUpperCaseColumns(connection, queryDrugs) dataDrugs$PERCENT_PERSONS <- format(round(dataDrugs$PERCENT_PERSONS, 4), nsmall = 4) dataDrugs$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataDrugs$PERCENT_PERSONS), 10) dataDrugs$RECORDS_PER_PERSON <- format(round(dataDrugs$RECORDS_PER_PERSON, 1), nsmall = 1) dataDrugs$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataDrugs$RECORDS_PER_PERSON), 10) return(dataDrugs) - #data.table::fwrite(dataDrugs, file=paste0(sourceOutputPath, "/domain-summary-drug_exposure.csv")) + # data.table::fwrite(dataDrugs, file=paste0(sourceOutputPath, "/domain-summary-drug_exposure.csv")) } generateDomainDrugStratification <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2032,9 +2012,9 @@ generateDomainDrugStratification <- function(connection, resultsDatabaseSchema, results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataDrugType <- DatabaseConnector::querySql(connection, queryDrugType) + dataDrugType <- querySqlWithUpperCaseColumns(connection, queryDrugType) return(dataDrugType) - #data.table::fwrite(dataDrugType, file=paste0(sourceOutputPath, "/domain-drug-stratification.csv")) + # data.table::fwrite(dataDrugType, file=paste0(sourceOutputPath, "/domain-drug-stratification.csv")) } generateDomainSummaryDrugEra <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2045,13 +2025,13 @@ generateDomainSummaryDrugEra <- function(connection, resultsDatabaseSchema, voca results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataDrugEra <- DatabaseConnector::querySql(connection, queryDrugEra) + dataDrugEra <- querySqlWithUpperCaseColumns(connection, queryDrugEra) dataDrugEra$PERCENT_PERSONS <- format(round(dataDrugEra$PERCENT_PERSONS, 4), nsmall = 4) dataDrugEra$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataDrugEra$PERCENT_PERSONS), 10) dataDrugEra$RECORDS_PER_PERSON <- format(round(dataDrugEra$RECORDS_PER_PERSON, 1), nsmall = 1) dataDrugEra$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataDrugEra$RECORDS_PER_PERSON), 10) return(dataDrugEra) - #data.table::fwrite(dataDrugEra, file=paste0(sourceOutputPath, "/domain-summary-drug_era.csv")) + # data.table::fwrite(dataDrugEra, file=paste0(sourceOutputPath, "/domain-summary-drug_era.csv")) } generateDomainSummaryMeasurements <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2062,13 +2042,13 @@ generateDomainSummaryMeasurements <- function(connection, resultsDatabaseSchema, results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataMeasurements <- DatabaseConnector::querySql(connection, queryMeasurements) + dataMeasurements <- querySqlWithUpperCaseColumns(connection, queryMeasurements) dataMeasurements$PERCENT_PERSONS <- format(round(dataMeasurements$PERCENT_PERSONS, 4), nsmall = 4) dataMeasurements$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataMeasurements$PERCENT_PERSONS), 10) dataMeasurements$RECORDS_PER_PERSON <- format(round(dataMeasurements$RECORDS_PER_PERSON, 1), nsmall = 1) dataMeasurements$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataMeasurements$RECORDS_PER_PERSON), 10) return(dataMeasurements) - #data.table::fwrite(dataMeasurements, file=paste0(sourceOutputPath, "/domain-summary-measurement.csv")) + # data.table::fwrite(dataMeasurements, file=paste0(sourceOutputPath, "/domain-summary-measurement.csv")) } generateDomainSummaryObservations <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2079,13 +2059,13 @@ generateDomainSummaryObservations <- function(connection, resultsDatabaseSchema, results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataObservations <- DatabaseConnector::querySql(connection, queryObservations) + dataObservations <- querySqlWithUpperCaseColumns(connection, queryObservations) dataObservations$PERCENT_PERSONS <- format(round(dataObservations$PERCENT_PERSONS, 4), nsmall = 4) dataObservations$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataObservations$PERCENT_PERSONS), 10) dataObservations$RECORDS_PER_PERSON <- format(round(dataObservations$RECORDS_PER_PERSON, 1), nsmall = 1) dataObservations$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataObservations$RECORDS_PER_PERSON), 10) return(dataObservations) - #data.table::fwrite(dataObservations, file=paste0(sourceOutputPath, "/domain-summary-observation.csv")) + # data.table::fwrite(dataObservations, file=paste0(sourceOutputPath, "/domain-summary-observation.csv")) } generateDomainSummaryVisitDetails <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2096,14 +2076,14 @@ generateDomainSummaryVisitDetails <- function(connection, resultsDatabaseSchema, results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataVisitDetails <- DatabaseConnector::querySql(connection, queryVisitDetails) + dataVisitDetails <- querySqlWithUpperCaseColumns(connection, queryVisitDetails) dataVisitDetails$PERCENT_PERSONS <- format(round(dataVisitDetails$PERCENT_PERSONS, 4), nsmall = 4) dataVisitDetails$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataVisitDetails$PERCENT_PERSONS), 10) dataVisitDetails$RECORDS_PER_PERSON <- format(round(dataVisitDetails$RECORDS_PER_PERSON, 1), nsmall = 1) dataVisitDetails$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataVisitDetails$RECORDS_PER_PERSON), 10) - names(dataVisitDetails)[names(dataVisitDetails) == 'CONCEPT_PATH'] <- 'CONCEPT_NAME' + names(dataVisitDetails)[names(dataVisitDetails) == "CONCEPT_PATH"] <- "CONCEPT_NAME" return(dataVisitDetails) - #data.table::fwrite(dataVisitDetails, file=paste0(sourceOutputPath, "/domain-summary-visit_detail.csv")) + # data.table::fwrite(dataVisitDetails, file=paste0(sourceOutputPath, "/domain-summary-visit_detail.csv")) } generateDomainSummaryVisits <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2114,14 +2094,14 @@ generateDomainSummaryVisits <- function(connection, resultsDatabaseSchema, vocab results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataVisits <- DatabaseConnector::querySql(connection, queryVisits) + dataVisits <- querySqlWithUpperCaseColumns(connection, queryVisits) dataVisits$PERCENT_PERSONS <- format(round(dataVisits$PERCENT_PERSONS, 4), nsmall = 4) dataVisits$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataVisits$PERCENT_PERSONS), 10) dataVisits$RECORDS_PER_PERSON <- format(round(dataVisits$RECORDS_PER_PERSON, 1), nsmall = 1) dataVisits$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataVisits$RECORDS_PER_PERSON), 10) - names(dataVisits)[names(dataVisits) == 'CONCEPT_PATH'] <- 'CONCEPT_NAME' + names(dataVisits)[names(dataVisits) == "CONCEPT_PATH"] <- "CONCEPT_NAME" return(dataVisits) - #data.table::fwrite(dataVisits, file=paste0(sourceOutputPath, "/domain-summary-visit_occurrence.csv")) + # data.table::fwrite(dataVisits, file=paste0(sourceOutputPath, "/domain-summary-visit_occurrence.csv")) } generateDomainVisitStratification <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2132,9 +2112,9 @@ generateDomainVisitStratification <- function(connection, resultsDatabaseSchema, results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataVisits <- DatabaseConnector::querySql(connection, queryVisits) + dataVisits <- querySqlWithUpperCaseColumns(connection, queryVisits) return(dataVisits) - #data.table::fwrite(dataVisits, file=paste0(sourceOutputPath, "/domain-visit-stratification.csv")) + # data.table::fwrite(dataVisits, file=paste0(sourceOutputPath, "/domain-visit-stratification.csv")) } generateDomainSummaryProcedures <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2145,13 +2125,13 @@ generateDomainSummaryProcedures <- function(connection, resultsDatabaseSchema, v results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataProcedures <- DatabaseConnector::querySql(connection, queryProcedures) + dataProcedures <- querySqlWithUpperCaseColumns(connection, queryProcedures) dataProcedures$PERCENT_PERSONS <- format(round(dataProcedures$PERCENT_PERSONS, 4), nsmall = 4) dataProcedures$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataProcedures$PERCENT_PERSONS), 10) dataProcedures$RECORDS_PER_PERSON <- format(round(dataProcedures$RECORDS_PER_PERSON, 1), nsmall = 1) dataProcedures$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataProcedures$RECORDS_PER_PERSON), 10) return(dataProcedures) - #data.table::fwrite(dataProcedures, file=paste0(sourceOutputPath, "/domain-summary-procedure_occurrence.csv")) + # data.table::fwrite(dataProcedures, file=paste0(sourceOutputPath, "/domain-summary-procedure_occurrence.csv")) } generateDomainSummaryDevices <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2162,13 +2142,13 @@ generateDomainSummaryDevices <- function(connection, resultsDatabaseSchema, voca results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema ) - dataDevices <- DatabaseConnector::querySql(connection, queryDevices) + dataDevices <- querySqlWithUpperCaseColumns(connection, queryDevices) dataDevices$PERCENT_PERSONS <- format(round(dataDevices$PERCENT_PERSONS, 4), nsmall = 4) dataDevices$PERCENT_PERSONS_NTILE <- dplyr::ntile(dplyr::desc(dataDevices$PERCENT_PERSONS), 10) dataDevices$RECORDS_PER_PERSON <- format(round(dataDevices$RECORDS_PER_PERSON, 1), nsmall = 1) dataDevices$RECORDS_PER_PERSON_NTILE <- dplyr::ntile(dplyr::desc(dataDevices$RECORDS_PER_PERSON), 10) return(dataDevices) - #data.table::fwrite(dataDevices, file=paste0(sourceOutputPath, "/domain-summary-device_exposure.csv")) + # data.table::fwrite(dataDevices, file=paste0(sourceOutputPath, "/domain-summary-device_exposure.csv")) } generateDomainSummaryProvider <- function(connection, resultsDatabaseSchema, vocabDatabaseSchema) { @@ -2180,11 +2160,11 @@ generateDomainSummaryProvider <- function(connection, resultsDatabaseSchema, voc vocab_database_schema = vocabDatabaseSchema ) writeLines("Generating provider reports") - dataProviders <- DatabaseConnector::querySql(connection, queryProviders) + dataProviders <- querySqlWithUpperCaseColumns(connection, queryProviders) dataProviders$PERCENT_PERSONS <- format(round(dataProviders$PERCENT_PERSONS, 4), nsmall = 4) return(dataProviders) - #data.table::fwrite(dataProviders, file=paste0(sourceOutputPath, "/domain-summary-provider.csv")) - #dbWriteTable(duckdbCon, "domain_summary", dataProviders, append = TRUE) + # data.table::fwrite(dataProviders, file=paste0(sourceOutputPath, "/domain-summary-provider.csv")) + # dbWriteTable(duckdbCon, "domain_summary", dataProviders, append = TRUE) } generateQualityCompleteness <- function(connection, resultsDatabaseSchema) { @@ -2194,13 +2174,13 @@ generateQualityCompleteness <- function(connection, resultsDatabaseSchema) { dbms = connection@dbms, results_database_schema = resultsDatabaseSchema ) - dataCompleteness <- DatabaseConnector::querySql(connection, queryCompleteness) - dataCompleteness <- dataCompleteness[order(-dataCompleteness$RECORD_COUNT),] + dataCompleteness <- querySqlWithUpperCaseColumns(connection, queryCompleteness) + dataCompleteness <- dataCompleteness[order(-dataCompleteness$RECORD_COUNT), ] # prevent downstream crashes with large files if (nrow(dataCompleteness) > 100000) { - dataCompleteness <- dataCompleteness[1:100000,] + dataCompleteness <- dataCompleteness[1:100000, ] } - #data.table::fwrite(dataCompleteness, file=paste0(sourceOutputPath, "/quality-completeness.csv")) + # data.table::fwrite(dataCompleteness, file=paste0(sourceOutputPath, "/quality-completeness.csv")) return(dataCompleteness) } @@ -2224,26 +2204,24 @@ generateQualityCompleteness <- function(connection, resultsDatabaseSchema) { #' #' @return none #' -#'@import DBI -#'@importFrom data.table fwrite -#'@importFrom dplyr ntile desc -#'@export +#' @import DBI +#' @importFrom data.table fwrite +#' @export exportToAres <- function( - connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - vocabDatabaseSchema, - outputPath, - outputFormat = "default", - reports = c()) -{ + connectionDetails, + cdmDatabaseSchema, + resultsDatabaseSchema, + vocabDatabaseSchema, + outputPath, + outputFormat = "default", + reports = c()) { conn <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection = conn)) # generate a folder name for this release of the cdm characterization sql <- SqlRender::render(sql = "select * from @cdmDatabaseSchema.cdm_source;", cdmDatabaseSchema = cdmDatabaseSchema) sql <- SqlRender::translate(sql = sql, targetDialect = connectionDetails$dbms) - metadata <- DatabaseConnector::querySql(conn, sql) + metadata <- querySqlWithUpperCaseColumns(conn, sql) sourceKey <- gsub(" ", "_", metadata$CDM_SOURCE_ABBREVIATION) releaseDateKey <- format(lubridate::ymd(metadata$CDM_RELEASE_DATE), "%Y%m%d") sourceOutputPath <- file.path(outputPath, sourceKey, releaseDateKey) @@ -2253,7 +2231,7 @@ exportToAres <- function( conceptsFolder <- file.path(sourceOutputPath, "concepts") dir.create(conceptsFolder, showWarnings = F) if (outputFormat == "duckdb") { - conceptsDatabasePath <- file.path(conceptsFolder, 'data.duckdb') + conceptsDatabasePath <- file.path(conceptsFolder, "data.duckdb") if (file.exists(conceptsDatabasePath)) { unlink(conceptsDatabasePath) } @@ -2373,7 +2351,6 @@ exportToAres <- function( # quality - completeness currentTable <- generateQualityCompleteness(conn, resultsDatabaseSchema) data.table::fwrite(currentTable, file = paste0(sourceOutputPath, "/quality-completeness.csv")) - } if (length(reports) == 0 || (length(reports) > 0 && "performance" %in% reports)) { @@ -2652,7 +2629,7 @@ exportToAres <- function( if (length(reports) == 0 || (length(reports) > 0 && "person" %in% reports)) { writeLines("Generating person report") currentTable <- generateAOPersonReport(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, vocabDatabaseSchema, sourceOutputPath) - jsonOutput = jsonlite::toJSON(currentTable) + jsonOutput <- jsonlite::toJSON(currentTable) write(jsonOutput, file = paste0(sourceOutputPath, "/person.json")) } -} \ No newline at end of file +} diff --git a/R/exportToCSV.R b/R/exportToCSV.R index 1a1a5f63..9987e52c 100644 --- a/R/exportToCSV.R +++ b/R/exportToCSV.R @@ -22,7 +22,7 @@ #' minCellCount) are deleted. Set to 0 for complete summary without #' small cell count restrictions. #' @param exportFolder Path to store results -#' @returns +#' @returns #' No return value. Called to export CSV file to the file system. #' @export @@ -30,8 +30,7 @@ exportResultsToCSV <- function(connectionDetails, resultsDatabaseSchema, analysisIds = c(), minCellCount = 5, - - exportFolder) { + exportFolder) { # Ensure the export folder exists if (!file.exists(exportFolder)) { dir.create(exportFolder, recursive = TRUE) @@ -42,13 +41,14 @@ exportResultsToCSV <- function(connectionDetails, on.exit(DatabaseConnector::disconnect(connection)) # Obtain the data from the achilles tables - sql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/raw/export_raw_achilles_results.sql", + sql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/raw/export_raw_achilles_results.sql", packageName = "Achilles", dbms = connectionDetails$dbms, warnOnMissingParameters = FALSE, results_database_schema = resultsDatabaseSchema, - min_cell_count = minCellCount, analysis_ids = analysisIds) + min_cell_count = minCellCount, analysis_ids = analysisIds + ) ParallelLogger::logInfo("Querying achilles_results") results <- DatabaseConnector::querySql(connection = connection, sql = sql) # Save the data to the export folder readr::write_csv(results, file.path(exportFolder, "achilles_results.csv")) } - diff --git a/R/exportToJson.R b/R/exportToJson.R index d463780e..d6193c71 100755 --- a/R/exportToJson.R +++ b/R/exportToJson.R @@ -1,15 +1,15 @@ # @file exportToJson # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # https://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -28,8 +28,10 @@ # When adding a new report, append it to inst/csv/export/all_reports.csv getAllReports <- function() { - reports <- read.csv(file = system.file("csv", "export", "all_reports.csv", package = "Achilles"), - stringsAsFactors = FALSE, header = TRUE)$REPORT + reports <- read.csv( + file = system.file("csv", "export", "all_reports.csv", package = "Achilles"), + stringsAsFactors = FALSE, header = TRUE + )$REPORT return(reports) } @@ -99,8 +101,10 @@ showReportTypes <- function() { #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportToJson(connectionDetails, cdmDatabaseSchema = "cdm4_sim", outputPath = "your/output/path") #' } #' @export @@ -108,12 +112,11 @@ exportToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - reports = getAllReports(), vocabDatabaseSchema = cdmDatabaseSchema, compressIntoOneFile = FALSE) { - + reports = getAllReports(), vocabDatabaseSchema = cdmDatabaseSchema, compressIntoOneFile = FALSE) { start <- Sys.time() - if (missing(resultsDatabaseSchema)) + if (missing(resultsDatabaseSchema)) { resultsDatabaseSchema <- cdmDatabaseSchema + } initOutputPath(outputPath) @@ -124,150 +127,190 @@ exportToJson <- function(connectionDetails, # generate reports if ("CONDITION" %in% reports) { - generateConditionTreemap(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) - generateConditionReports(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateConditionTreemap( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateConditionReports( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("CONDITION_ERA" %in% reports) { - generateConditionEraTreemap(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) - generateConditionEraReports(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + generateConditionEraTreemap( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateConditionEraReports( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } - if ("DATA_DENSITY" %in% reports) - generateDataDensityReport(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + if ("DATA_DENSITY" %in% reports) { + generateDataDensityReport( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + } if ("DEATH" %in% reports) { - generateDeathReports(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateDeathReports( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("DRUG_ERA" %in% reports) { - generateDrugEraTreemap(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) - generateDrugEraReports(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateDrugEraTreemap( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateDrugEraReports( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("DRUG" %in% reports) { - generateDrugTreemap(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - - vocabDatabaseSchema) - generateDrugReports(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - - vocabDatabaseSchema) + generateDrugTreemap( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + vocabDatabaseSchema + ) + generateDrugReports( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + vocabDatabaseSchema + ) } if (("META" %in% reports)) { - generateMetadataReport(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) - generateCdmSourceReport(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateMetadataReport( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateCdmSourceReport( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if (("MEASUREMENT" %in% reports)) { - generateMeasurementTreemap(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) - generateMeasurementReports(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + generateMeasurementTreemap( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateMeasurementReports( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("OBSERVATION" %in% reports) { - generateObservationTreemap(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) - generateObservationReports(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + generateObservationTreemap( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateObservationReports( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } - if ("OBSERVATION_PERIOD" %in% reports) - generateObservationPeriodReport(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + if ("OBSERVATION_PERIOD" %in% reports) { + generateObservationPeriodReport( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + } - if ("PERSON" %in% reports) - generatePersonReport(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + if ("PERSON" %in% reports) { + generatePersonReport( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + } if ("PROCEDURE" %in% reports) { - generateProcedureTreemap(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) - generateProcedureReports(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateProcedureTreemap( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateProcedureReports( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("VISIT" %in% reports) { - generateVisitTreemap(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) - generateVisitReports(conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, - outputPath, vocabDatabaseSchema) + generateVisitTreemap( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateVisitReports( + conn, connectionDetails$dbms, cdmDatabaseSchema, resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("VISIT_DETAIL" %in% reports) { - generateVisitDetailTreemap(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) - generateVisitDetailReports(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + generateVisitDetailTreemap( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) + generateVisitDetailReports( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } if ("PERFORMANCE" %in% reports) { - generateAchillesPerformanceReport(conn, - connectionDetails$dbms, - cdmDatabaseSchema, - resultsDatabaseSchema, - - outputPath, vocabDatabaseSchema) + generateAchillesPerformanceReport( + conn, + connectionDetails$dbms, + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, vocabDatabaseSchema + ) } # dashboard is always last @@ -278,9 +321,13 @@ exportToJson <- function(connectionDetails, DatabaseConnector::disconnect(conn) if (compressIntoOneFile) { - zip(zipfile = file.path(outputPath, - sprintf("%s.zip", cdmDatabaseSchema)), files = c(outputPath), - flags = c("-r")) + zip( + zipfile = file.path( + outputPath, + sprintf("%s.zip", cdmDatabaseSchema) + ), files = c(outputPath), + flags = c("-r") + ) } delta <- Sys.time() - start @@ -314,26 +361,28 @@ exportToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportConditionToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportConditionToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("CONDITION"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("CONDITION"), + vocabDatabaseSchema + ) } #' @title @@ -362,26 +411,28 @@ exportConditionToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportConditionEraToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportConditionEraToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("CONDITION_ERA"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("CONDITION_ERA"), + vocabDatabaseSchema + ) } #' @title @@ -413,26 +464,28 @@ exportConditionEraToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportDashboardToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportDashboardToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("DASHBOARD"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("DASHBOARD"), + vocabDatabaseSchema + ) } #' @title @@ -462,26 +515,28 @@ exportDashboardToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportDataDensityToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportDataDensityToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("DATA_DENSITY"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("DATA_DENSITY"), + vocabDatabaseSchema + ) } #' @title @@ -511,26 +566,28 @@ exportDataDensityToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportDeathToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportDeathToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("DEATH"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("DEATH"), + vocabDatabaseSchema + ) } #' @title @@ -560,26 +617,28 @@ exportDeathToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportDrugToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportDrugToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("DRUG"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("DRUG"), + vocabDatabaseSchema + ) } #' @title @@ -609,26 +668,28 @@ exportDrugToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportDrugEraToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportDrugEraToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("DRUG_ERA"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("DRUG_ERA"), + vocabDatabaseSchema + ) } @@ -659,26 +720,28 @@ exportDrugEraToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportMetaToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportMetaToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("META"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("META"), + vocabDatabaseSchema + ) } #' @title @@ -708,26 +771,28 @@ exportMetaToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportMeasurementToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportMeasurementToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("MEASUREMENT"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("MEASUREMENT"), + vocabDatabaseSchema + ) } #' @title @@ -757,26 +822,28 @@ exportMeasurementToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportObservationToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportObservationToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("OBSERVATION"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("OBSERVATION"), + vocabDatabaseSchema + ) } #' @title @@ -807,11 +874,14 @@ exportObservationToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportObservationPeriodToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportObservationPeriodToJson <- function(connectionDetails, @@ -856,26 +926,28 @@ exportObservationPeriodToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportPersonToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportPersonToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("PERSON"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("PERSON"), + vocabDatabaseSchema + ) } #' @title @@ -905,26 +977,28 @@ exportPersonToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportProcedureToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportProcedureToJson <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { exportToJson(connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - outputPath, - reports = c("PROCEDURE"), - - vocabDatabaseSchema) + cdmDatabaseSchema, + resultsDatabaseSchema, + outputPath, + reports = c("PROCEDURE"), + vocabDatabaseSchema + ) } #' @title @@ -953,11 +1027,14 @@ exportProcedureToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportVisitToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportVisitToJson <- function(connectionDetails, @@ -971,7 +1048,6 @@ exportVisitToJson <- function(connectionDetails, resultsDatabaseSchema, outputPath, reports = c("VISIT"), - vocabDatabaseSchema ) } @@ -1003,11 +1079,14 @@ exportVisitToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportVisitDetailToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportVisitDetailToJson <- function(connectionDetails, @@ -1052,11 +1131,14 @@ exportVisitDetailToJson <- function(connectionDetails, #' none #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' exportPerformanceToJson(connectionDetails, -#' cdmDatabaseSchema = "cdm4_sim", -#' outputPath = "your/output/path") +#' cdmDatabaseSchema = "cdm4_sim", +#' outputPath = "your/output/path" +#' ) #' } #' @export exportPerformanceToJson <- function(connectionDetails, @@ -1070,7 +1152,6 @@ exportPerformanceToJson <- function(connectionDetails, resultsDatabaseSchema, outputPath, reports = c("PERFORMANCE"), - vocabDatabaseSchema ) } @@ -1081,29 +1162,33 @@ generateAchillesPerformanceReport <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating achilles performance report") output <- { } - queryAchillesPerformance <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/performance/sqlAchillesPerformance.sql", + queryAchillesPerformance <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/performance/sqlAchillesPerformance.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) output$MESSAGES <- DatabaseConnector::querySql(conn, queryAchillesPerformance) jsonOutput <- jsonlite::toJSON(output) write(jsonOutput, file = paste(outputPath, "/achillesperformance.json", sep = "")) } -generateMetadataReport <- function(conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - vocabDatabaseSchema = cdmDatabaseSchema) { +generateMetadataReport <- function( + conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating metadata report") output <- { } - queryMetadata <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/metadata/sqlMetadata.sql", - packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema) + queryMetadata <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/metadata/sqlMetadata.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema + ) if ("METADATA" %in% DatabaseConnector::getTableNames(connection = conn, databaseSchema = cdmDatabaseSchema)) { output$MESSAGES <- DatabaseConnector::querySql(conn, queryMetadata) @@ -1119,14 +1204,15 @@ generateCdmSourceReport <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating cdm source report") output <- { } - queryCdmSource <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/metadata/sqlCdmSource.sql", - packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema) + queryCdmSource <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/metadata/sqlCdmSource.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema + ) if ("CDM_SOURCE" %in% DatabaseConnector::getTableNames(connection = conn, databaseSchema = cdmDatabaseSchema)) { output$MESSAGES <- DatabaseConnector::querySql(conn, queryCdmSource) @@ -1137,20 +1223,24 @@ generateCdmSourceReport <- function(conn, } } -generateDrugEraTreemap <- function(conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - vocabDatabaseSchema = cdmDatabaseSchema) { +generateDrugEraTreemap <- function( + conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating drug era treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryDrugEraTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drugera/sqlDrugEraTreemap.sql", + queryDrugEraTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drugera/sqlDrugEraTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataDrugEraTreemap <- DatabaseConnector::querySql(conn, queryDrugEraTreemap) write(jsonlite::toJSON(dataDrugEraTreemap, method = "C"), paste(outputPath, "/drugera_treemap.json", - sep = "")) + sep = "" + )) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1167,14 +1257,18 @@ generateDrugTreemap <- function(conn, progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryDrugTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlDrugTreemap.sql", + queryDrugTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlDrugTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataDrugTreemap <- DatabaseConnector::querySql(conn, queryDrugTreemap) - write(jsonlite::toJSON(dataDrugTreemap, method = "C"), - paste(outputPath, "/drug_treemap.json", sep = "")) + write( + jsonlite::toJSON(dataDrugTreemap, method = "C"), + paste(outputPath, "/drug_treemap.json", sep = "") + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1186,21 +1280,25 @@ generateConditionTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating condition treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryConditionTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/condition/sqlConditionTreemap.sql", + queryConditionTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/condition/sqlConditionTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataConditionTreemap <- DatabaseConnector::querySql(conn, queryConditionTreemap) - write(jsonlite::toJSON(dataConditionTreemap, method = "C"), - paste(outputPath, "/condition_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataConditionTreemap, method = "C"), + paste(outputPath, "/condition_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1212,21 +1310,25 @@ generateConditionEraTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating condition era treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryConditionEraTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/conditionera/sqlConditionEraTreemap.sql", + queryConditionEraTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/conditionera/sqlConditionEraTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataConditionEraTreemap <- DatabaseConnector::querySql(conn, queryConditionEraTreemap) - write(jsonlite::toJSON(dataConditionEraTreemap, method = "C"), - paste(outputPath, "/conditionera_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataConditionEraTreemap, method = "C"), + paste(outputPath, "/conditionera_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1238,15 +1340,16 @@ generateConditionReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating condition reports") treemapFile <- file.path(outputPath, "condition_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -1260,27 +1363,34 @@ generateConditionReports <- function(conn, writeLines(paste("Warning: folder ", conditionsFolder, " already exists")) } else { dir.create(paste(conditionsFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/condition/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/condition/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/condition/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/condition/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryConditionsByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/condition/sqlConditionsByType.sql", + queryConditionsByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/condition/sqlConditionsByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstDiagnosis <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/condition/sqlAgeAtFirstDiagnosis.sql", + queryAgeAtFirstDiagnosis <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/condition/sqlAgeAtFirstDiagnosis.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) @@ -1293,8 +1403,10 @@ generateConditionReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$CONDITIONS_BY_TYPE <- dataConditionsByType[dataConditionsByType$CONDITION_CONCEPT_ID == concept_id, c(2, 3)] report$AGE_AT_FIRST_DIAGNOSIS <- dataAgeAtFirstDiagnosis[dataAgeAtFirstDiagnosis$CONCEPT_ID == @@ -1307,8 +1419,10 @@ generateConditionReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildConditionReport) @@ -1322,15 +1436,16 @@ generateConditionEraReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating condition era reports") treemapFile <- file.path(outputPath, "conditionera_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -1343,27 +1458,34 @@ generateConditionEraReports <- function(conn, writeLines(paste("Warning: folder ", conditionsFolder, " already exists")) } else { dir.create(paste(conditionsFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/conditionera/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/conditionera/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/conditionera/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/conditionera/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstDiagnosis <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/conditionera/sqlAgeAtFirstDiagnosis.sql", + queryAgeAtFirstDiagnosis <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/conditionera/sqlAgeAtFirstDiagnosis.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryLengthOfEra <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/conditionera/sqlLengthOfEra.sql", + queryLengthOfEra <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/conditionera/sqlLengthOfEra.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) @@ -1376,10 +1498,14 @@ generateConditionEraReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(2, 3, 4, 5)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(2, 3)] - report$LENGTH_OF_ERA <- dataLengthOfEra[dataLengthOfEra$CONCEPT_ID == concept_id, c(2, 3, 4, - 5, 6, 7, 8, 9)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(2, 3) + ] + report$LENGTH_OF_ERA <- dataLengthOfEra[dataLengthOfEra$CONCEPT_ID == concept_id, c( + 2, 3, 4, + 5, 6, 7, 8, 9 + )] report$AGE_AT_FIRST_DIAGNOSIS <- dataAgeAtFirstDiagnosis[dataAgeAtFirstDiagnosis$CONCEPT_ID == concept_id, c(2, 3, 4, 5, 6, 7, 8, 9)] filename <- paste(outputPath, "/conditioneras/condition_", concept_id, ".json", sep = "") @@ -1390,8 +1516,10 @@ generateConditionEraReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildConditionEraReport) @@ -1400,16 +1528,19 @@ generateConditionEraReports <- function(conn, close(progressBar) } -generateDrugEraReports <- function(conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - vocabDatabaseSchema = cdmDatabaseSchema) { +generateDrugEraReports <- function( + conn, dbms, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating drug era reports") treemapFile <- file.path(outputPath, "drugera_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -1428,21 +1559,29 @@ generateDrugEraReports <- function(conn, dbms, cdmDatabaseSchema, resultsDatabas progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryAgeAtFirstExposure <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drugera/sqlAgeAtFirstExposure.sql", + queryAgeAtFirstExposure <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drugera/sqlAgeAtFirstExposure.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drugera/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drugera/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drugera/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drugera/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryLengthOfEra <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drugera/sqlLengthOfEra.sql", + queryLengthOfEra <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drugera/sqlLengthOfEra.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataAgeAtFirstExposure <- DatabaseConnector::querySql(conn, queryAgeAtFirstExposure) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) @@ -1452,14 +1591,20 @@ generateDrugEraReports <- function(conn, dbms, cdmDatabaseSchema, resultsDatabas buildDrugEraReport <- function(concept_id) { report <- { } - report$AGE_AT_FIRST_EXPOSURE <- dataAgeAtFirstExposure[dataAgeAtFirstExposure$CONCEPT_ID == concept_id, - c(2, 3, 4, 5, 6, 7, 8, 9)] + report$AGE_AT_FIRST_EXPOSURE <- dataAgeAtFirstExposure[ + dataAgeAtFirstExposure$CONCEPT_ID == concept_id, + c(2, 3, 4, 5, 6, 7, 8, 9) + ] report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(2, 3, 4, 5)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(2, 3)] - report$LENGTH_OF_ERA <- dataLengthOfEra[dataLengthOfEra$CONCEPT_ID == concept_id, c(2, 3, 4, - 5, 6, 7, 8, 9)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(2, 3) + ] + report$LENGTH_OF_ERA <- dataLengthOfEra[dataLengthOfEra$CONCEPT_ID == concept_id, c( + 2, 3, 4, + 5, 6, 7, 8, 9 + )] filename <- paste(outputPath, "/drugeras/drug_", concept_id, ".json", sep = "") @@ -1469,8 +1614,10 @@ generateDrugEraReports <- function(conn, dbms, cdmDatabaseSchema, resultsDatabas env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildDrugEraReport) @@ -1489,9 +1636,11 @@ generateDrugReports <- function(conn, treemapFile <- file.path(outputPath, "drug_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -1509,37 +1658,53 @@ generateDrugReports <- function(conn, progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryAgeAtFirstExposure <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlAgeAtFirstExposure.sql", + queryAgeAtFirstExposure <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlAgeAtFirstExposure.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryDaysSupplyDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlDaysSupplyDistribution.sql", + queryDaysSupplyDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlDaysSupplyDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryDrugsByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlDrugsByType.sql", + queryDrugsByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlDrugsByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryDrugFrequencyDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlFrequencyDistribution.sql", + queryDrugFrequencyDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlFrequencyDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryQuantityDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlQuantityDistribution.sql", + queryQuantityDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlQuantityDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryRefillsDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/drug/sqlRefillsDistribution.sql", + queryRefillsDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/drug/sqlRefillsDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataAgeAtFirstExposure <- DatabaseConnector::querySql(conn, queryAgeAtFirstExposure) dataDaysSupplyDistribution <- DatabaseConnector::querySql(conn, queryDaysSupplyDistribution) @@ -1560,8 +1725,10 @@ generateDrugReports <- function(conn, report$DRUGS_BY_TYPE <- dataDrugsByType[dataDrugsByType$DRUG_CONCEPT_ID == concept_id, c(3, 4)] report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$DRUG_FREQUENCY_DISTRIBUTION <- dataDrugFrequencyDistribution[dataDrugFrequencyDistribution$CONCEPT_ID == concept_id, c(3, 4)] report$QUANTITY_DISTRIBUTION <- dataQuantityDistribution[dataQuantityDistribution$DRUG_CONCEPT_ID == @@ -1577,8 +1744,10 @@ generateDrugReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildDrugReport) @@ -1592,21 +1761,25 @@ generateProcedureTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating procedure treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryProcedureTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlProcedureTreemap.sql", + queryProcedureTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlProcedureTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataProcedureTreemap <- DatabaseConnector::querySql(conn, queryProcedureTreemap) - write(jsonlite::toJSON(dataProcedureTreemap, method = "C"), - paste(outputPath, "/procedure_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataProcedureTreemap, method = "C"), + paste(outputPath, "/procedure_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1618,15 +1791,16 @@ generateProcedureReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating procedure reports") treemapFile <- file.path(outputPath, "procedure_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -1639,46 +1813,59 @@ generateProcedureReports <- function(conn, writeLines(paste("Warning: folder ", proceduresFolder, " already exists")) } else { dir.create(paste(proceduresFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryProcedureFrequencyDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlFrequencyDistribution.sql", + queryProcedureFrequencyDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlFrequencyDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryProceduresByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlProceduresByType.sql", + queryProceduresByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlProceduresByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/procedure/sqlAgeAtFirstOccurrence.sql", + queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/procedure/sqlAgeAtFirstOccurrence.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) dataProceduresByType <- DatabaseConnector::querySql(conn, queryProceduresByType) dataAgeAtFirstOccurrence <- DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) - dataProcedureFrequencyDistribution <- DatabaseConnector::querySql(conn, - queryProcedureFrequencyDistribution) + dataProcedureFrequencyDistribution <- DatabaseConnector::querySql( + conn, + queryProcedureFrequencyDistribution + ) buildProcedureReport <- function(concept_id) { report <- { } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$PROCEDURE_FREQUENCY_DISTRIBUTION <- dataProcedureFrequencyDistribution[dataProcedureFrequencyDistribution$CONCEPT_ID == concept_id, c(3, 4)] report$PROCEDURES_BY_TYPE <- dataProceduresByType[dataProceduresByType$PROCEDURE_CONCEPT_ID == @@ -1693,8 +1880,10 @@ generateProcedureReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildProcedureReport) @@ -1717,11 +1906,12 @@ generatePersonReport <- function(conn, # 1. Title: Population a. Visualization: Table b.Row #1: CDM source name c.Row #2: # of persons - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/population.sql", - packageName = "Achilles", - + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/population.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, results_database_schema = resultsDatabaseSchema, - vocab_database_schema = vocabDatabaseSchema) + vocab_database_schema = vocabDatabaseSchema + ) personSummaryData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -1731,11 +1921,12 @@ generatePersonReport <- function(conn, # 2. Title: Gender distribution a. Visualization: Pie b.Category: Gender c.Value: % of persons - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/gender.sql", - packageName = "Achilles", - + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/gender.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, results_database_schema = resultsDatabaseSchema, - vocab_database_schema = vocabDatabaseSchema) + vocab_database_schema = vocabDatabaseSchema + ) genderData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1744,11 +1935,12 @@ generatePersonReport <- function(conn, # 3. Title: Race distribution a. Visualization: Pie b.Category: Race c.Value: % of persons - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/race.sql", - packageName = "Achilles", - + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/race.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, results_database_schema = resultsDatabaseSchema, - vocab_database_schema = vocabDatabaseSchema) + vocab_database_schema = vocabDatabaseSchema + ) raceData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1758,11 +1950,12 @@ generatePersonReport <- function(conn, # 4. Title: Ethnicity distribution a. Visualization: Pie b.Category: Ethnicity c.Value: % of # persons - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/ethnicity.sql", - packageName = "Achilles", - + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/ethnicity.sql", + packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, results_database_schema = resultsDatabaseSchema, - vocab_database_schema = vocabDatabaseSchema) + vocab_database_schema = vocabDatabaseSchema + ) ethnicityData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1774,9 +1967,11 @@ generatePersonReport <- function(conn, birthYearHist <- { } - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/yearofbirth_stats.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/yearofbirth_stats.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) birthYearStats <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1784,11 +1979,13 @@ generatePersonReport <- function(conn, birthYearHist$MIN <- birthYearStats$MIN_VALUE birthYearHist$MAX <- birthYearStats$MAX_VALUE birthYearHist$INTERVAL_SIZE <- birthYearStats$INTERVAL_SIZE - birthYearHist$INTERVALS <- (birthYearStats$MAX_VALUE - birthYearStats$MIN_VALUE)/birthYearStats$INTERVAL_SIZE + birthYearHist$INTERVALS <- (birthYearStats$MAX_VALUE - birthYearStats$MIN_VALUE) / birthYearStats$INTERVAL_SIZE - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/person/yearofbirth_data.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/person/yearofbirth_data.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) birthYearData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1811,8 +2008,7 @@ generateObservationPeriodReport <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating observation period reports") progressBar <- utils::txtProgressBar(max = 11, style = 3) progress <- 0 @@ -1831,9 +2027,11 @@ generateObservationPeriodReport <- function(conn, ageAtFirstObservationHist$INTERVAL_SIZE <- 1 ageAtFirstObservationHist$INTERVALS <- 100 - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/ageatfirst.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/ageatfirst.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) ageAtFirstObservationData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1843,9 +2041,11 @@ generateObservationPeriodReport <- function(conn, # 2. Title: Age by gender a.Visualization: Side-by-side boxplot b.Category: Gender c.Values: # Min/25%/Median/95%/Max - age at time of first observation - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/agebygender.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/agebygender.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) ageByGenderData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1857,9 +2057,11 @@ generateObservationPeriodReport <- function(conn, observationLengthHist <- { } - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observationlength_stats.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observationlength_stats.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) observationLengthStats <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -1867,11 +2069,13 @@ generateObservationPeriodReport <- function(conn, observationLengthHist$MIN <- observationLengthStats$MIN_VALUE observationLengthHist$MAX <- observationLengthStats$MAX_VALUE observationLengthHist$INTERVAL_SIZE <- observationLengthStats$INTERVAL_SIZE - observationLengthHist$INTERVALS <- (observationLengthStats$MAX_VALUE - observationLengthStats$MIN_VALUE)/observationLengthStats$INTERVAL_SIZE + observationLengthHist$INTERVALS <- (observationLengthStats$MAX_VALUE - observationLengthStats$MIN_VALUE) / observationLengthStats$INTERVAL_SIZE - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observationlength_data.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observationlength_data.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) observationLengthData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1884,9 +2088,11 @@ generateObservationPeriodReport <- function(conn, # survival plot, but information is the same as shown in a length of observation barchart, just # plotted as cumulative - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/cumulativeduration.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/cumulativeduration.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) cumulativeDurationData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -1896,9 +2102,11 @@ generateObservationPeriodReport <- function(conn, # 5. Title: Observation period length distribution, by gender a.Visualization: side-by-side # boxplot b.Category: Gender c.Values: Min/25%/Median/95%/Max length of observation period - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observationlengthbygender.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observationlengthbygender.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) opLengthByGenderData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1907,9 +2115,11 @@ generateObservationPeriodReport <- function(conn, # 6. Title: Observation period length distribution, by age a.Visualization: side-by-side boxplot # b.Category: Age decile c.Values: Min/25%/Median/95%/Max length of observation period - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observationlengthbyage.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observationlengthbyage.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) opLengthByAgeData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1920,20 +2130,24 @@ generateObservationPeriodReport <- function(conn, observedByYearHist <- { } - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observedbyyear_stats.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observedbyyear_stats.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) observedByYearStats <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) observedByYearHist$MIN <- observedByYearStats$MIN_VALUE observedByYearHist$MAX <- observedByYearStats$MAX_VALUE observedByYearHist$INTERVAL_SIZE <- observedByYearStats$INTERVAL_SIZE - observedByYearHist$INTERVALS <- (observedByYearStats$MAX_VALUE - observedByYearStats$MIN_VALUE)/observedByYearStats$INTERVAL_SIZE + observedByYearHist$INTERVALS <- (observedByYearStats$MAX_VALUE - observedByYearStats$MIN_VALUE) / observedByYearStats$INTERVAL_SIZE - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observedbyyear_data.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observedbyyear_data.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) observedByYearData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -1948,9 +2162,11 @@ generateObservationPeriodReport <- function(conn, observedByMonth <- { } - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/observedbymonth.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/observedbymonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) observedByMonth <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -1960,9 +2176,11 @@ generateObservationPeriodReport <- function(conn, # 9. Title: Number of observation periods per person a.Visualization: Pie b.Category: Number of # observation periods c.Values: # of persons - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observationperiod/periodsperperson.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observationperiod/periodsperperson.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) personPeriodsData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -2014,8 +2232,7 @@ generateDataDensityReport <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating data density reports") progressBar <- utils::txtProgressBar(max = 3, style = 3) progress <- 0 @@ -2025,9 +2242,11 @@ generateDataDensityReport <- function(conn, # 1. Title: Total records a.Visualization: scatterplot b.X-axis: month/year c.y-axis: records # d.series: person, visit, condition, drug, procedure, observation - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/datadensity/totalrecords.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/datadensity/totalrecords.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) totalRecordsData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2037,9 +2256,11 @@ generateDataDensityReport <- function(conn, # 2. Title: Records per person a.Visualization: scatterplot b.X-axis: month/year c.y-axis: # records/person d.series: person, visit, condition, drug, procedure, observation - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/datadensity/recordsperperson.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/datadensity/recordsperperson.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) recordsPerPerson <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2050,9 +2271,11 @@ generateDataDensityReport <- function(conn, # Condition/Drug/Procedure/Observation c.Values: Min/25%/Median/95%/Max number of distinct # concepts per person - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/datadensity/conceptsperperson.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/datadensity/conceptsperperson.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) conceptsPerPerson <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2063,7 +2286,6 @@ generateDataDensityReport <- function(conn, jsonOutput <- jsonlite::toJSON(output) write(jsonOutput, file = paste(outputPath, "/datadensity.json", sep = "")) close(progressBar) - } generateMeasurementTreemap <- function(conn, @@ -2071,26 +2293,29 @@ generateMeasurementTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating measurement treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryMeasurementTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlMeasurementTreemap.sql", + queryMeasurementTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlMeasurementTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataMeasurementTreemap <- DatabaseConnector::querySql(conn, queryMeasurementTreemap) - write(jsonlite::toJSON(dataMeasurementTreemap, method = "C"), - paste(outputPath, "/measurement_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataMeasurementTreemap, method = "C"), + paste(outputPath, "/measurement_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) close(progressBar) - } generateMeasurementReports <- function(conn, @@ -2098,15 +2323,16 @@ generateMeasurementReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating Measurement reports") treemapFile <- file.path(outputPath, "measurement_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -2119,59 +2345,80 @@ generateMeasurementReports <- function(conn, writeLines(paste("Warning: folder ", measurementsFolder, " already exists")) } else { dir.create(paste(measurementsFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryFrequencyDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlFrequencyDistribution.sql", + queryFrequencyDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlFrequencyDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryMeasurementsByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlMeasurementsByType.sql", + queryMeasurementsByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlMeasurementsByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlAgeAtFirstOccurrence.sql", + queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlAgeAtFirstOccurrence.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryRecordsByUnit <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlRecordsByUnit.sql", + queryRecordsByUnit <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlRecordsByUnit.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryMeasurementValueDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlMeasurementValueDistribution.sql", + queryMeasurementValueDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlMeasurementValueDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryLowerLimitDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlLowerLimitDistribution.sql", + queryLowerLimitDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlLowerLimitDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryUpperLimitDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlUpperLimitDistribution.sql", + queryUpperLimitDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlUpperLimitDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryValuesRelativeToNorm <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/measurement/sqlValuesRelativeToNorm.sql", + queryValuesRelativeToNorm <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/measurement/sqlValuesRelativeToNorm.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) dataMeasurementsByType <- DatabaseConnector::querySql(conn, queryMeasurementsByType) dataAgeAtFirstOccurrence <- DatabaseConnector::querySql(conn, queryAgeAtFirstOccurrence) dataRecordsByUnit <- DatabaseConnector::querySql(conn, queryRecordsByUnit) - dataMeasurementValueDistribution <- DatabaseConnector::querySql(conn, - queryMeasurementValueDistribution) + dataMeasurementValueDistribution <- DatabaseConnector::querySql( + conn, + queryMeasurementValueDistribution + ) dataLowerLimitDistribution <- DatabaseConnector::querySql(conn, queryLowerLimitDistribution) dataUpperLimitDistribution <- DatabaseConnector::querySql(conn, queryUpperLimitDistribution) dataValuesRelativeToNorm <- DatabaseConnector::querySql(conn, queryValuesRelativeToNorm) @@ -2182,8 +2429,10 @@ generateMeasurementReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$FREQUENCY_DISTRIBUTION <- dataFrequencyDistribution[dataFrequencyDistribution$CONCEPT_ID == concept_id, c(3, 4)] report$MEASUREMENTS_BY_TYPE <- dataMeasurementsByType[dataMeasurementsByType$MEASUREMENT_CONCEPT_ID == @@ -2191,8 +2440,10 @@ generateMeasurementReports <- function(conn, report$AGE_AT_FIRST_OCCURRENCE <- dataAgeAtFirstOccurrence[dataAgeAtFirstOccurrence$CONCEPT_ID == concept_id, c(2, 3, 4, 5, 6, 7, 8, 9)] - report$RECORDS_BY_UNIT <- dataRecordsByUnit[dataRecordsByUnit$MEASUREMENT_CONCEPT_ID == concept_id, - c(4, 5)] + report$RECORDS_BY_UNIT <- dataRecordsByUnit[ + dataRecordsByUnit$MEASUREMENT_CONCEPT_ID == concept_id, + c(4, 5) + ] report$MEASUREMENT_VALUE_DISTRIBUTION <- dataMeasurementValueDistribution[dataMeasurementValueDistribution$CONCEPT_ID == concept_id, c(2, 3, 4, 5, 6, 7, 8, 9)] report$LOWER_LIMIT_DISTRIBUTION <- dataLowerLimitDistribution[dataLowerLimitDistribution$CONCEPT_ID == @@ -2210,15 +2461,16 @@ generateMeasurementReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildMeasurementReport) utils::setTxtProgressBar(progressBar, 1) close(progressBar) - } generateObservationTreemap <- function(conn, @@ -2226,26 +2478,29 @@ generateObservationTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating observation treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryObservationTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlObservationTreemap.sql", + queryObservationTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlObservationTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataObservationTreemap <- DatabaseConnector::querySql(conn, queryObservationTreemap) - write(jsonlite::toJSON(dataObservationTreemap, method = "C"), - paste(outputPath, "/observation_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataObservationTreemap, method = "C"), + paste(outputPath, "/observation_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) close(progressBar) - } generateObservationReports <- function(conn, @@ -2253,15 +2508,16 @@ generateObservationReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating Observation reports") treemapFile <- file.path(outputPath, "observation_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -2274,31 +2530,40 @@ generateObservationReports <- function(conn, writeLines(paste("Warning: folder ", observationsFolder, " already exists")) } else { dir.create(paste(observationsFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryObsFrequencyDistribution <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlFrequencyDistribution.sql", + queryObsFrequencyDistribution <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlFrequencyDistribution.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryObservationsByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlObservationsByType.sql", + queryObservationsByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlObservationsByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/observation/sqlAgeAtFirstOccurrence.sql", + queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/observation/sqlAgeAtFirstOccurrence.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) @@ -2313,8 +2578,10 @@ generateObservationReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$OBS_FREQUENCY_DISTRIBUTION <- dataObsFrequencyDistribution[dataObsFrequencyDistribution$CONCEPT_ID == concept_id, c(3, 4)] report$OBSERVATIONS_BY_TYPE <- dataObservationsByType[dataObservationsByType$OBSERVATION_CONCEPT_ID == @@ -2331,15 +2598,16 @@ generateObservationReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildObservationReport) utils::setTxtProgressBar(progressBar, 1) close(progressBar) - } generateVisitTreemap <- function(conn, @@ -2352,14 +2620,18 @@ generateVisitTreemap <- function(conn, progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryVisitTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visit/sqlVisitTreemap.sql", + queryVisitTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visit/sqlVisitTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataVisitTreemap <- DatabaseConnector::querySql(conn, queryVisitTreemap) - write(jsonlite::toJSON(dataVisitTreemap, method = "C"), - paste(outputPath, "/visit_treemap.json", sep = "")) + write( + jsonlite::toJSON(dataVisitTreemap, method = "C"), + paste(outputPath, "/visit_treemap.json", sep = "") + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -2376,9 +2648,11 @@ generateVisitReports <- function(conn, treemapFile <- file.path(outputPath, "visit_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -2391,27 +2665,34 @@ generateVisitReports <- function(conn, writeLines(paste("Warning: folder ", visitsFolder, " already exists")) } else { dir.create(paste(visitsFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visit/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visit/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visit/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visit/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryVisitDurationByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visit/sqlVisitDurationByType.sql", + queryVisitDurationByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visit/sqlVisitDurationByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visit/sqlAgeAtFirstOccurrence.sql", + queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visit/sqlAgeAtFirstOccurrence.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) @@ -2423,8 +2704,10 @@ generateVisitReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$VISIT_DURATION_BY_TYPE <- dataVisitDurationByType[dataVisitDurationByType$CONCEPT_ID == concept_id, c(2, 3, 4, 5, 6, 7, 8, 9)] report$AGE_AT_FIRST_OCCURRENCE <- dataAgeAtFirstOccurrence[dataAgeAtFirstOccurrence$CONCEPT_ID == @@ -2437,8 +2720,10 @@ generateVisitReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildVisitReport) @@ -2463,9 +2748,11 @@ generateDeathReports <- function(conn, # lineplot b.Trellis category: age decile c.X-axis: year d.y-axis: condition prevalence (% # persons) e.series: male, female - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/death/sqlPrevalenceByGenderAgeYear.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/death/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) prevalenceByGenderAgeYearData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2475,9 +2762,11 @@ generateDeathReports <- function(conn, # 2. Title: Prevalence by month a.Visualization: scatterplot b.X-axis: month/year c.y-axis: % of # persons d.Comment: plot to show seasonality - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/death/sqlPrevalenceByMonth.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/death/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) prevalenceByMonthData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2487,9 +2776,11 @@ generateDeathReports <- function(conn, # 3. Title: Death records by type a.Visualization: pie b.Category: death type c.value: % of # records - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/death/sqlDeathByType.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/death/sqlDeathByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) deathByTypeData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2499,9 +2790,11 @@ generateDeathReports <- function(conn, # 4. Title: Age at death a.Visualization: side-by-side boxplot b.Category: gender c.Values: # Min/25%/Median/95%/Max as age at death - renderedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/death/sqlAgeAtDeath.sql", + renderedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/death/sqlAgeAtDeath.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) ageAtDeathData <- DatabaseConnector::querySql(conn, renderedSql) progress <- progress + 1 @@ -2519,21 +2812,25 @@ generateVisitDetailTreemap <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating visit_detail treemap") progressBar <- utils::txtProgressBar(max = 1, style = 3) progress <- 0 - queryVisitDetailTreemap <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visitdetail/sqlVisitDetailTreemap.sql", + queryVisitDetailTreemap <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visitdetail/sqlVisitDetailTreemap.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataVisitDetailTreemap <- DatabaseConnector::querySql(conn, queryVisitDetailTreemap) - write(jsonlite::toJSON(dataVisitDetailTreemap, method = "C"), - paste(outputPath, "/visitdetail_treemap.json", - sep = "")) + write( + jsonlite::toJSON(dataVisitDetailTreemap, method = "C"), + paste(outputPath, "/visitdetail_treemap.json", + sep = "" + ) + ) progress <- progress + 1 utils::setTxtProgressBar(progressBar, progress) @@ -2545,15 +2842,16 @@ generateVisitDetailReports <- function(conn, cdmDatabaseSchema, resultsDatabaseSchema, outputPath, - - vocabDatabaseSchema = cdmDatabaseSchema) { + vocabDatabaseSchema = cdmDatabaseSchema) { writeLines("Generating visit_detail reports") treemapFile <- file.path(outputPath, "visitdetail_treemap.json") if (!file.exists(treemapFile)) { - writeLines(paste("Warning: treemap file", - treemapFile, - "does not exist. Skipping detail report generation.")) + writeLines(paste( + "Warning: treemap file", + treemapFile, + "does not exist. Skipping detail report generation." + )) return() } @@ -2566,27 +2864,34 @@ generateVisitDetailReports <- function(conn, writeLines(paste("Warning: folder ", visitdetailFolder, " already exists")) } else { dir.create(paste(visitdetailFolder, "/", sep = "")) - } progressBar <- utils::txtProgressBar(style = 3) progress <- 0 - queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visitdetail/sqlPrevalenceByGenderAgeYear.sql", + queryPrevalenceByGenderAgeYear <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visitdetail/sqlPrevalenceByGenderAgeYear.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visitdetail/sqlPrevalenceByMonth.sql", + queryPrevalenceByMonth <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visitdetail/sqlPrevalenceByMonth.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryVisitDetailDurationByType <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visitdetail/sqlVisitDetailDurationByType.sql", + queryVisitDetailDurationByType <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visitdetail/sqlVisitDetailDurationByType.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) - queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql(sqlFilename = "export/visitdetail/sqlAgeAtFirstOccurrence.sql", + queryAgeAtFirstOccurrence <- SqlRender::loadRenderTranslateSql( + sqlFilename = "export/visitdetail/sqlAgeAtFirstOccurrence.sql", packageName = "Achilles", dbms = dbms, warnOnMissingParameters = FALSE, cdm_database_schema = cdmDatabaseSchema, - results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema) + results_database_schema = resultsDatabaseSchema, vocab_database_schema = vocabDatabaseSchema + ) dataPrevalenceByGenderAgeYear <- DatabaseConnector::querySql(conn, queryPrevalenceByGenderAgeYear) dataPrevalenceByMonth <- DatabaseConnector::querySql(conn, queryPrevalenceByMonth) @@ -2598,8 +2903,10 @@ generateVisitDetailReports <- function(conn, } report$PREVALENCE_BY_GENDER_AGE_YEAR <- dataPrevalenceByGenderAgeYear[dataPrevalenceByGenderAgeYear$CONCEPT_ID == concept_id, c(3, 4, 5, 6)] - report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[dataPrevalenceByMonth$CONCEPT_ID == concept_id, - c(3, 4)] + report$PREVALENCE_BY_MONTH <- dataPrevalenceByMonth[ + dataPrevalenceByMonth$CONCEPT_ID == concept_id, + c(3, 4) + ] report$VISIT_DETAIL_DURATION_BY_TYPE <- dataVisitDetailDurationByType[dataVisitDetailDurationByType$CONCEPT_ID == concept_id, c(2, 3, 4, 5, 6, 7, 8, 9)] report$AGE_AT_FIRST_OCCURRENCE <- dataAgeAtFirstOccurrence[dataAgeAtFirstOccurrence$CONCEPT_ID == @@ -2612,8 +2919,10 @@ generateVisitDetailReports <- function(conn, env <- parent.env(environment()) curVal <- get("progress", envir = env) assign("progress", curVal + 1, envir = env) - utils::setTxtProgressBar(get("progressBar", envir = env), - (curVal + 1)/get("totalCount", envir = env)) + utils::setTxtProgressBar( + get("progressBar", envir = env), + (curVal + 1) / get("totalCount", envir = env) + ) } dummy <- lapply(uniqueConcepts, buildVisitDetailReport) @@ -2621,4 +2930,3 @@ generateVisitDetailReports <- function(conn, utils::setTxtProgressBar(progressBar, 1) close(progressBar) } - diff --git a/R/generateDbSummary.R b/R/generateDbSummary.R index 859b4d35..4da824a1 100644 --- a/R/generateDbSummary.R +++ b/R/generateDbSummary.R @@ -1,15 +1,15 @@ # @file generateDbSummary # -# Copyright 2021 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # https://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,7 +21,7 @@ #' generateDbSummary #' #' @description -#' \code{generateDbSummary} can be run after the Achilles analyses are complete +#' \code{generateDbSummary} can be run after the Achilles analyses are complete #' to create a high-level database summary. #' #' @details @@ -48,24 +48,26 @@ #' #' @examples #' \dontrun{ -#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server", -#' server = "yourserver") +#' connectionDetails <- DatabaseConnector::createConnectionDetails( +#' dbms = "sql server", +#' server = "yourserver" +#' ) #' dbSummary <- generateDbSummary(connectionDetails, -#' cdmDatabaseSchema = "cdm_schema", -#' resultsDatabaseSchema = "results_schema", -#' country = "Country of Origin", -#' provenance = "Provenance of data") +#' cdmDatabaseSchema = "cdm_schema", +#' resultsDatabaseSchema = "results_schema", +#' country = "Country of Origin", +#' provenance = "Provenance of data" +#' ) #' } #' @export -generateDbSummary <- function (connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - country, - provenance){ - +generateDbSummary <- function(connectionDetails, + cdmDatabaseSchema, + resultsDatabaseSchema, + country, + provenance) { conn <- DatabaseConnector::connect(connectionDetails) - + sql <- SqlRender::loadRenderTranslateSql( sqlFilename = "summary/generateDbSummary.sql", @@ -77,9 +79,9 @@ generateDbSummary <- function (connectionDetails, country = country, provenance = provenance ) - + dbSummary <- DatabaseConnector::querySql(conn, sql) - + sql <- SqlRender::loadRenderTranslateSql( sqlFilename = "summary/dbSourceVocabs.sql", @@ -91,9 +93,9 @@ generateDbSummary <- function (connectionDetails, country = country, provenance = provenance ) - + dbSourceVocabs <- DatabaseConnector::querySql(conn, sql) - + sql <- SqlRender::loadRenderTranslateSql( sqlFilename = "summary/dbVisitDist.sql", @@ -105,20 +107,20 @@ generateDbSummary <- function (connectionDetails, country = country, provenance = provenance ) - + dbVisitDist <- DatabaseConnector::querySql(conn, sql) - + DatabaseConnector::dbDisconnect(conn) # extract columns and pivot - dbInfo <- dbSummary[1,c(1,2,3,4)] + dbInfo <- dbSummary[1, c(1, 2, 3, 4)] row.names(dbSummary) <- dbSummary$ATTRIBUTE_NAME - df <- dbSummary[,c('ATTRIBUTE_VALUE')] + df <- dbSummary[, c("ATTRIBUTE_VALUE")] df_t <- t(df) colnames(df_t) <- rownames(dbSummary) dbSummaryFinal <- cbind(dbInfo, df_t) - + colnames(dbSummaryFinal)[1:4] <- c("Data Source Name", "Data Source Abbreviation", "Source Country", "Data Provenance") - - return(list(summary=dbSummaryFinal, visitDist=dbVisitDist, sourceVocabs = dbSourceVocabs)) -} \ No newline at end of file + + return(list(summary = dbSummaryFinal, visitDist = dbVisitDist, sourceVocabs = dbSourceVocabs)) +} diff --git a/R/generateDomainOverlapSql.R b/R/generateDomainOverlapSql.R index 23e1f43f..2b690f63 100644 --- a/R/generateDomainOverlapSql.R +++ b/R/generateDomainOverlapSql.R @@ -7,21 +7,26 @@ generateDomainOverlapSql <- function() { # creates a matrix of domain overlap possibilities. If you want to add a domain, you would add # to the list directly below. - domainMatrix <- tidyr::crossing(condition_occurrence = 0:1, - drug_exposure = 0:1, - device_exposure = 0:1, - - measurement = 0:1, death = 0:1, procedure_occurrence = 0:1, observation = 0:1) + domainMatrix <- tidyr::crossing( + condition_occurrence = 0:1, + drug_exposure = 0:1, + device_exposure = 0:1, + measurement = 0:1, death = 0:1, procedure_occurrence = 0:1, observation = 0:1 + ) domainMatrixResults <- domainMatrix domainMatrixResults <- domainMatrixResults %>% mutate(count = 0, proportion = 0, dataSource = "") # Creates notes - write(x = "-- Analysis 2004: Number of distinct patients that overlap between specific domains", - sqlFile, append = TRUE) - write(x = "-- Bit String Breakdown: 1) Condition Occurrence 2) Drug Exposure 3) Device Exposure 4) Measurement 5) Death 6) Procedure Occurrence 7) Observation", - sqlFile, append = TRUE) + write( + x = "-- Analysis 2004: Number of distinct patients that overlap between specific domains", + sqlFile, append = TRUE + ) + write( + x = "-- Bit String Breakdown: 1) Condition Occurrence 2) Drug Exposure 3) Device Exposure 4) Measurement 5) Death 6) Procedure Occurrence 7) Observation", + sqlFile, append = TRUE + ) write(x = "", sqlFile, append = TRUE) # Creates temp tables for each specific domain @@ -29,7 +34,7 @@ generateDomainOverlapSql <- function() { write(x = "select distinct person_id into #drexp from @cdmDatabaseSchema.drug_exposure;", sqlFile, append = TRUE) write(x = "select distinct person_id into #dvexp from @cdmDatabaseSchema.device_exposure;", sqlFile, append = TRUE) write(x = "select distinct person_id into #msmt from @cdmDatabaseSchema.measurement;", sqlFile, append = TRUE) - write(x = "select distinct person_id into #death from @cdmDatabaseSchema.death;", sqlFile,append = TRUE) + write(x = "select distinct person_id into #death from @cdmDatabaseSchema.death;", sqlFile, append = TRUE) write(x = "select distinct person_id into #prococ from @cdmDatabaseSchema.procedure_occurrence;", sqlFile, append = TRUE) write(x = "select distinct person_id into #obs from @cdmDatabaseSchema.observation;", sqlFile, append = TRUE) write(x = "", sqlFile, append = TRUE) @@ -121,8 +126,7 @@ generateDomainOverlapSql <- function() { sql <- paste0(sql, " intersect select person_id from #obs") } } - - } # End for loop for domainMatrix by column + } # End for loop for domainMatrix by column sql <- paste0(sql, ")") @@ -155,10 +159,9 @@ generateDomainOverlapSql <- function() { } else { write(x = sql, sqlFile, append = TRUE) } + } # End for loop for domainMatrix by row + - } # End for loop for domainMatrix by row - - # clean up temp tables # Creates temp tables for each specific domain write(x = "drop table #conoc;", sqlFile, append = TRUE) @@ -168,5 +171,5 @@ generateDomainOverlapSql <- function() { write(x = "drop table #death;", sqlFile, append = TRUE) write(x = "drop table #prococ;", sqlFile, append = TRUE) write(x = "drop table #obs;", sqlFile, append = TRUE) - write(x = "", sqlFile, append = TRUE) -} # End function + write(x = "", sqlFile, append = TRUE) +} # End function diff --git a/R/getSeasonalityScore.r b/R/getSeasonalityScore.r index 37cb9461..ed1d2f9c 100644 --- a/R/getSeasonalityScore.r +++ b/R/getSeasonalityScore.r @@ -1,41 +1,40 @@ -#'@title Get the seasonality score for a given monthly time series +#' @title Get the seasonality score for a given monthly time series #' -#'@description The seasonality score of a monthly time series is computed as its departure from a uniform distribution. +#' @description The seasonality score of a monthly time series is computed as its departure from a uniform distribution. #' -#'@details +#' @details #' The degree of seasonality of a monthly time series is based on its departure from a uniform distribution. -#' If the number of cases for a given concept is uniformly distributed across all time periods (in this case, all months), +#' If the number of cases for a given concept is uniformly distributed across all time periods (in this case, all months), #' then its monthly proportion would be approximately constant. In this case, the time series would be #' considered "strictly non-seasonal" and its "seasonality score" would be zero. #' Similarly, if all cases recur at a single point in time (that is, in a single month), such a time series would be considered #' "strictly seasonal" and its seasonality score would be 1. All other time series would have #' a seasonality score between 0 and 1. Currently, only monthly time series are supported. -#' -#'@param tsData A time series object. #' -#'@return A numeric value between 0 and 1 (inclusive) representing the seasonality of a time series. +#' @param tsData A time series object. #' -#'@export +#' @return A numeric value between 0 and 1 (inclusive) representing the seasonality of a time series. +#' +#' @export + +getSeasonalityScore <- function(tsData) { + unifDist <- 1 / 12 + a <- c(1, rep(0, 11)) + maxDist <- sum(abs(a - unifDist)) + + tsObj <- tsData + tsObj <- Achilles::tsCompleteYears(tsObj) -getSeasonalityScore <- function(tsData) -{ - unifDist <- 1/12 - a <- c(1,rep(0,11)) - maxDist <- sum(abs(a-unifDist)) - - tsObj <- tsData - tsObj <- Achilles::tsCompleteYears(tsObj) - - # Matrix version: switch to and update this version once the rare-events issue is corrected - # NB: Remember to avoid dividing by zero with the matrix approach - # M <- matrix(data=tsObj, ncol=12, byrow=TRUE) - # ss <- sum(abs(t((rep(1,dim(M)[1]) %*% M)/as.integer(rep(1,dim(M)[1]) %*% M %*% rep(1,12))) - unifDist))/maxDist + # Matrix version: switch to and update this version once the rare-events issue is corrected + # NB: Remember to avoid dividing by zero with the matrix approach + # M <- matrix(data=tsObj, ncol=12, byrow=TRUE) + # ss <- sum(abs(t((rep(1,dim(M)[1]) %*% M)/as.integer(rep(1,dim(M)[1]) %*% M %*% rep(1,12))) - unifDist))/maxDist - # Original version using sum across years - tsObj.yrProp <- Achilles::sumAcrossYears(tsObj)$PROP - tsObj.ss <- round(sum(abs(tsObj.yrProp-unifDist))/maxDist,2) + # Original version using sum across years + tsObj.yrProp <- Achilles::sumAcrossYears(tsObj)$PROP + tsObj.ss <- round(sum(abs(tsObj.yrProp - unifDist)) / maxDist, 2) - tsObj.ss <- round(tsObj.ss,2) + tsObj.ss <- round(tsObj.ss, 2) - return (tsObj.ss) -} \ No newline at end of file + return(tsObj.ss) +} diff --git a/R/getTemporalData.r b/R/getTemporalData.r index 6bf418c2..95e802c2 100644 --- a/R/getTemporalData.r +++ b/R/getTemporalData.r @@ -1,6 +1,6 @@ # @file getTemporalData # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -61,12 +61,14 @@ #' @examples #' \dontrun{ #' pneumonia <- 255848 -#' monthlyResults <- getTemporalData(connectionDetails = connectionDetails, -#' cdmDatabaseSchema = "cdm", -#' -#' resultsDatabaseSchema = "results", conceptId = pneumonia) +#' monthlyResults <- getTemporalData( +#' connectionDetails = connectionDetails, +#' cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", conceptId = pneumonia +#' ) #' } #' +#' @importFrom dplyr rename_with #' @export @@ -74,17 +76,21 @@ getTemporalData <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, analysisIds = NULL, - - conceptId = NULL) { + conceptId = NULL) { if (!is.null(conceptId)) { - print(paste0("Retrieving Achilles monthly data for temporal support for concept_id: ", - conceptId)) + print(paste0( + "Retrieving Achilles monthly data for temporal support for concept_id: ", + conceptId + )) conceptIdGiven <- TRUE analysisIdGiven <- FALSE } else if (!is.null(analysisIds)) { - print(paste0("Retrieving Achilles monthly data for temporal support for analyses: ", - paste(analysisIds, - collapse = ", "))) + print(paste0( + "Retrieving Achilles monthly data for temporal support for analyses: ", + paste(analysisIds, + collapse = ", " + ) + )) conceptIdGiven <- FALSE analysisIdGiven <- TRUE } else { @@ -101,17 +107,18 @@ getTemporalData <- function(connectionDetails, dbName <- NA } - translatedSql <- SqlRender::loadRenderTranslateSql(sqlFilename = "temporal/achilles_temporal_data.sql", + translatedSql <- SqlRender::loadRenderTranslateSql( + sqlFilename = "temporal/achilles_temporal_data.sql", packageName = "Achilles", dbms = connectionDetails$dbms, db_name = dbName, cdm_schema = cdmDatabaseSchema, results_schema = resultsDatabaseSchema, concept_id = conceptId, analysis_ids = analysisIds, concept_id_given = conceptIdGiven, - analysis_id_given = analysisIdGiven) + analysis_id_given = analysisIdGiven + ) conn <- DatabaseConnector::connect(connectionDetails) - queryResults <- DatabaseConnector::querySql(conn, translatedSql) + queryResults <- DatabaseConnector::querySql(conn, translatedSql) |> dplyr::rename_with(toupper) on.exit(DatabaseConnector::disconnect(conn)) return(queryResults) - } diff --git a/R/isStationary.r b/R/isStationary.r index 240609e4..0afe5c48 100644 --- a/R/isStationary.r +++ b/R/isStationary.r @@ -1,29 +1,29 @@ -#'@title Determine whether or not a time series is stationary in the mean +#' @title Determine whether or not a time series is stationary in the mean #' -#'@description Uses the Augmented Dickey-Fuller test to determine when the time series has a unit root. +#' @description Uses the Augmented Dickey-Fuller test to determine when the time series has a unit root. #' -#'@details +#' @details #' A time series must have a minimum of three complete years of data. -#' For details on the implementation of the Augmented Dickey-Fuller test, +#' For details on the implementation of the Augmented Dickey-Fuller test, #' see the tseries package on cran. #' -#'@param tsData A time series object. +#' @param tsData A time series object. #' -#'@return A boolean indicating whether or not the given time series is stationary. +#' @return A boolean indicating whether or not the given time series is stationary. #' -#'@export +#' @export -isStationary <- function(tsData) -{ - tsObj <- tsData - minMonths <- 36 +isStationary <- function(tsData) { + tsObj <- tsData + minMonths <- 36 - tsObj <- Achilles::tsCompleteYears(tsObj) + tsObj <- Achilles::tsCompleteYears(tsObj) - if (length(tsObj) < minMonths) - stop("ERROR: Time series must have a minimum of three complete years of data") - - ADF_IS_STATIONARY <- suppressWarnings(tseries::adf.test(tsObj, alternative="stationary")$p.value <= .05) + if (length(tsObj) < minMonths) { + stop("ERROR: Time series must have a minimum of three complete years of data") + } - return (ADF_IS_STATIONARY) -} \ No newline at end of file + ADF_IS_STATIONARY <- suppressWarnings(tseries::adf.test(tsObj, alternative = "stationary")$p.value <= .05) + + return(ADF_IS_STATIONARY) +} diff --git a/R/listMissingAnalyses.r b/R/listMissingAnalyses.r index becbb39f..b1156dce 100644 --- a/R/listMissingAnalyses.r +++ b/R/listMissingAnalyses.r @@ -1,6 +1,6 @@ # @file listMissingAnalyses # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -44,14 +44,15 @@ #' #' @examples #' \dontrun{ -#' Achilles::listMissingAnalyses(connectionDetails = connectionDetails, -#' resultsDatabaseSchema = "results") +#' Achilles::listMissingAnalyses( +#' connectionDetails = connectionDetails, +#' resultsDatabaseSchema = "results" +#' ) #' } #' #' @export listMissingAnalyses <- function(connectionDetails, resultsDatabaseSchema) { - # Determine which analyses are missing by comparing analysisDetails with achilles_results and # achilles_results_dist analysisDetails <- getAnalysisDetails() @@ -73,7 +74,7 @@ listMissingAnalyses <- function(connectionDetails, resultsDatabaseSchema) { missingAnalysisIds <- setdiff(allAnalysisIds, existingAnalysisIds) - colsToDisplay <- c("analysis_id","distribution","category","is_default","analysis_name") + colsToDisplay <- c("analysis_id", "distribution", "category", "is_default", "analysis_name") retVal <- analysisDetails[analysisDetails$analysis_id %in% missingAnalysisIds, colsToDisplay] retVal <- retVal[order(retVal$analysis_id), ] diff --git a/R/performTemporalCharacterization.r b/R/performTemporalCharacterization.r index 026ca568..4859c85f 100644 --- a/R/performTemporalCharacterization.r +++ b/R/performTemporalCharacterization.r @@ -1,6 +1,6 @@ # @file performTemporalCharacterization # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -26,7 +26,7 @@ # @author Taha Abdul-Basser # @author Anthony Molinaro -#'@title performTemporalCharacterization +#' @title performTemporalCharacterization #' #' @description #' \code{performTemporalCharacterization} Perform temporal characterization on a concept or family of concepts belonging to a supported Achilles analysis. @@ -54,7 +54,7 @@ #' Server, 'cdm_results.dbo'. #' @param analysisIds (OPTIONAL) A vector containing the set of Achilles analysisIds for #' which results will be returned. The following are supported: \code{202,402,602,702,802,1802,2102}. -#' If not specified, data for all analysis will be returned. Ignored if \code{conceptId} is given. +#' If not specified, data for all analysis will be returned. Ignored if \code{conceptId} is given. #' @param conceptId (OPTIONAL) A SNOMED concept_id from the \code{CONCEPT} table for which a monthly Achilles analysis exists. #' If not specified, all concepts for a given analysis will be returned. #' @param outputFile CSV file where temporal characterization will be written. Default is temporal-characterization.csv. @@ -67,95 +67,95 @@ #' # Example 1: #' pneumonia <- 255848 #' performTemporalCharacterization( -#' connectionDetails = connectionDetails, -#' cdmDatabaseSchema = "cdm", -#' resultsDatabaseSchema = "results", -#' conceptId = pneumonia, -#' outputFolder = "output/pneumoniaTemporalChar.csv") +#' connectionDetails = connectionDetails, +#' cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", +#' conceptId = pneumonia, +#' outputFolder = "output/pneumoniaTemporalChar.csv" +#' ) #' #' # Example 2: #' performTemporalCharacterization( -#' connectionDetails = connectionDetails, -#' cdmDatabaseSchema = "cdm", -#' resultsDatabaseSchema = "results", -#' analysisIds = c(402,702), -#' outputFolder = "output/conditionAndDrugTemporalChar.csv") +#' connectionDetails = connectionDetails, +#' cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", +#' analysisIds = c(402, 702), +#' outputFolder = "output/conditionAndDrugTemporalChar.csv" +#' ) #' #' # Example 3: #' performTemporalCharacterization( -#' connectionDetails = connectionDetails, -#' cdmDatabaseSchema = "cdm", -#' resultsDatabaseSchema = "results", -#' outputFolder = "output/CompleteTemporalChar.csv") +#' connectionDetails = connectionDetails, +#' cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", +#' outputFolder = "output/CompleteTemporalChar.csv" +#' ) #' } #' -#'@export +#' @export -performTemporalCharacterization <- function( - connectionDetails, - cdmDatabaseSchema, - resultsDatabaseSchema, - analysisIds = NULL, - conceptId = NULL, - outputFile = "temporal-characterization.csv") -{ +performTemporalCharacterization <- function(connectionDetails, + cdmDatabaseSchema, + resultsDatabaseSchema, + analysisIds = NULL, + conceptId = NULL, + outputFile = "temporal-characterization.csv") { + # Minimum number of months of data to perform temporal characterization + minMonths <- 36 - # Minimum number of months of data to perform temporal characterization - minMonths <- 36 - - # Pull temporal data from Achilles and get list of unique concept_ids - temporalData <- Achilles::getTemporalData(connectionDetails,cdmDatabaseSchema,resultsDatabaseSchema,analysisIds,conceptId) - - if (nrow(temporalData) == 0) { - stop("CANNOT PERFORM TEMPORAL CHARACTERIZATION: NO ACHILLES DATA FOUND") - } - - allConceptIds <- unique(temporalData$CONCEPT_ID) - print(paste0("Attempting temporal characterization on ", length(allConceptIds), " individual concepts")) - - # Loop through temporal data, perform temporal characterization, and write out results - rowData <- - temporalData %>% - tidyr::nest( - tempData = c( - "START_DATE", - "COUNT_VALUE", - "PREVALENCE", - "PROPORTION_WITHIN_YEAR" - ) - ) %>% - ## rowwise allows to work with nested list vars as with usual ones - dplyr::rowwise() %>% - dplyr::mutate( - tempData.ts = list( - Achilles::createTimeSeries(.data$tempData) - ), - tempData.ts = list( - .data$tempData.ts[, "PREVALENCE"] - ), - tempData.ts = list( - Achilles::tsCompleteYears(.data$tempData.ts) - ) - ) %>% - dplyr::filter( - length(.data$tempData.ts) >= minMonths - ) %>% - dplyr::mutate( - tempData.ts.ss = Achilles::getSeasonalityScore(.data$tempData.ts), - tempData.ts.is = Achilles::isStationary(.data$tempData.ts) - ) %>% - ## now we don't need to handle variables row wise - dplyr::ungroup() %>% - dplyr::select( - DB_NAME = .data$DB_NAME, - CDM_TABLE_NAME = .data$CDM_TABLE_NAME, - CONCEPT_ID = .data$CONCEPT_ID, - CONCEPT_NAME = .data$CONCEPT_NAME, - SEASONALITY_SCORE = .data$tempData.ts.ss, - IS_STATIONARY = .data$tempData.ts.is, - ) %>% - dplyr::collect() - write.csv(rowData,outputFile,row.names = FALSE) - print(paste0("Temporal characterization complete. Results can be found in ", outputFile)) - invisible(rowData) + # Pull temporal data from Achilles and get list of unique concept_ids + temporalData <- Achilles::getTemporalData(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema, analysisIds, conceptId) + + if (nrow(temporalData) == 0) { + stop("CANNOT PERFORM TEMPORAL CHARACTERIZATION: NO ACHILLES DATA FOUND") + } + + allConceptIds <- unique(temporalData$CONCEPT_ID) + print(paste0("Attempting temporal characterization on ", length(allConceptIds), " individual concepts")) + + # Loop through temporal data, perform temporal characterization, and write out results + rowData <- + temporalData %>% + tidyr::nest( + tempData = c( + "START_DATE", + "COUNT_VALUE", + "PREVALENCE", + "PROPORTION_WITHIN_YEAR" + ) + ) %>% + ## rowwise allows to work with nested list vars as with usual ones + dplyr::rowwise() %>% + dplyr::mutate( + tempData.ts = list( + Achilles::createTimeSeries(.data$tempData) + ), + tempData.ts = list( + .data$tempData.ts[, "PREVALENCE"] + ), + tempData.ts = list( + Achilles::tsCompleteYears(.data$tempData.ts) + ) + ) %>% + dplyr::filter( + length(.data$tempData.ts) >= minMonths + ) %>% + dplyr::mutate( + tempData.ts.ss = Achilles::getSeasonalityScore(.data$tempData.ts), + tempData.ts.is = Achilles::isStationary(.data$tempData.ts) + ) %>% + ## now we don't need to handle variables row wise + dplyr::ungroup() %>% + dplyr::select( + DB_NAME = .data$DB_NAME, + CDM_TABLE_NAME = .data$CDM_TABLE_NAME, + CONCEPT_ID = .data$CONCEPT_ID, + CONCEPT_NAME = .data$CONCEPT_NAME, + SEASONALITY_SCORE = .data$tempData.ts.ss, + IS_STATIONARY = .data$tempData.ts.is, + ) %>% + dplyr::collect() + write.csv(rowData, outputFile, row.names = FALSE) + print(paste0("Temporal characterization complete. Results can be found in ", outputFile)) + invisible(rowData) } diff --git a/R/runMissingAnalyses.r b/R/runMissingAnalyses.r index 2b3f0d87..9ee66090 100644 --- a/R/runMissingAnalyses.r +++ b/R/runMissingAnalyses.r @@ -1,6 +1,6 @@ # @file runMissingAnalyses # -# Copyright 2023 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of Achilles # @@ -59,16 +59,17 @@ #' @param defaultAnalysesOnly Boolean to determine if only default analyses should be run. #' Including non-default analyses is substantially more resource #' intensive. Default = TRUE -#' @returns +#' @returns #' No return value. Run to execute analyses currently missing from results. -#' +#' #' @examples #' \dontrun{ -#' Achilles::runMissingAnalyses(connectionDetails = connectionDetails, -#' cdmDatabaseSchema = "cdm", -#' resultsDatabaseSchema = "results", -#' -#' outputFolder = "/tmp") +#' Achilles::runMissingAnalyses( +#' connectionDetails = connectionDetails, +#' cdmDatabaseSchema = "cdm", +#' resultsDatabaseSchema = "results", +#' outputFolder = "/tmp" +#' ) #' } #' #' @export @@ -76,38 +77,38 @@ runMissingAnalyses <- function(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema = cdmDatabaseSchema, - scratchDatabaseSchema = resultsDatabaseSchema, - vocabDatabaseSchema = cdmDatabaseSchema, - tempEmulationSchema = resultsDatabaseSchema, - outputFolder = "output", - defaultAnalysesOnly = TRUE) -{ - - missingAnalyses <- Achilles::listMissingAnalyses(connectionDetails,resultsDatabaseSchema) + scratchDatabaseSchema = resultsDatabaseSchema, + vocabDatabaseSchema = cdmDatabaseSchema, + tempEmulationSchema = resultsDatabaseSchema, + outputFolder = "output", + defaultAnalysesOnly = TRUE) { + missingAnalyses <- Achilles::listMissingAnalyses(connectionDetails, resultsDatabaseSchema) if (nrow(missingAnalyses) == 0) { stop("NO MISSING ANALYSES FOUND") } - + if (defaultAnalysesOnly) { - missingAnalyses <- missingAnalyses[missingAnalyses$is_default == 1,] - } + missingAnalyses <- missingAnalyses[missingAnalyses$is_default == 1, ] + } if (nrow(missingAnalyses) == 0) { stop("NO DEFAULT MISSING ANALYSES FOUND") } - + # By supplying analysisIds along with specifying createTable=F and updateGivenAnalysesOnly=T, # we add the missing analysis_ids without removing existing data - achilles(connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - resultsDatabaseSchema = resultsDatabaseSchema, - scratchDatabaseSchema = scratchDatabaseSchema, - vocabDatabaseSchema = cdmDatabaseSchema, - tempEmulationSchema = tempEmulationSchema, - analysisIds = missingAnalyses$analysis_id, - defaultAnalysesOnly = defaultAnalysesOnly, - outputFolder = outputFolder, - createTable = FALSE, - updateGivenAnalysesOnly = TRUE) + achilles( + connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + resultsDatabaseSchema = resultsDatabaseSchema, + scratchDatabaseSchema = scratchDatabaseSchema, + vocabDatabaseSchema = cdmDatabaseSchema, + tempEmulationSchema = tempEmulationSchema, + analysisIds = missingAnalyses$analysis_id, + defaultAnalysesOnly = defaultAnalysesOnly, + outputFolder = outputFolder, + createTable = FALSE, + updateGivenAnalysesOnly = TRUE + ) } diff --git a/R/sumAcrossYears.r b/R/sumAcrossYears.r index b98bc744..5ec698f7 100644 --- a/R/sumAcrossYears.r +++ b/R/sumAcrossYears.r @@ -1,22 +1,22 @@ -#'@title For a monhtly time series, compute sum and proportion by month across all years +#' @title For a monhtly time series, compute sum and proportion by month across all years #' -#'@param tsData A time series object +#' @param tsData A time series object #' -#'@return A data frame reporting the monthly sum across all years and the proportion this sum contributes to the total. +#' @return A data frame reporting the monthly sum across all years and the proportion this sum contributes to the total. #' -#'@export +#' @export -sumAcrossYears <- function(tsData) -{ - - # Read the time series into a data frame with character string months - tsAsDf <- data.frame(MONTH_NUM=cycle(tsData), TS_VALUE=tsData, stringsAsFactors = F) - # Summarize by month across all years - tsAggregatedByMonth <- tsAsDf %>% dplyr::group_by(.data$MONTH_NUM) %>% dplyr::summarize(SUM=sum(.data$TS_VALUE)) - # Compute proportion for each month - tsPropByMonth <- data.frame(MONTH_NUM=tsAggregatedByMonth$MONTH_NUM, PROP=tsAggregatedByMonth$SUM/sum(tsData), stringsAsFactors = F) - # Get sum and proportion in a single data frame - tsSummary <- merge(tsAggregatedByMonth, tsPropByMonth, by.x = "MONTH_NUM", by.y="MONTH_NUM") +sumAcrossYears <- function(tsData) { + # Read the time series into a data frame with character string months + tsAsDf <- data.frame(MONTH_NUM = cycle(tsData), TS_VALUE = tsData, stringsAsFactors = F) + # Summarize by month across all years + tsAggregatedByMonth <- tsAsDf %>% + dplyr::group_by(.data$MONTH_NUM) %>% + dplyr::summarize(SUM = sum(.data$TS_VALUE)) + # Compute proportion for each month + tsPropByMonth <- data.frame(MONTH_NUM = tsAggregatedByMonth$MONTH_NUM, PROP = tsAggregatedByMonth$SUM / sum(tsData), stringsAsFactors = F) + # Get sum and proportion in a single data frame + tsSummary <- merge(tsAggregatedByMonth, tsPropByMonth, by.x = "MONTH_NUM", by.y = "MONTH_NUM") - return (tsSummary[order(tsSummary$PROP, decreasing = T),]) -} \ No newline at end of file + return(tsSummary[order(tsSummary$PROP, decreasing = T), ]) +} diff --git a/R/tsCompleteYears.r b/R/tsCompleteYears.r index 099db174..ff63146d 100644 --- a/R/tsCompleteYears.r +++ b/R/tsCompleteYears.r @@ -1,32 +1,30 @@ -#'@title Trim a monthly time series object to so that partial years are removed +#' @title Trim a monthly time series object to so that partial years are removed #' -#'@details This function is only supported for monthly time series +#' @details This function is only supported for monthly time series #' -#'@param tsData A time series object +#' @param tsData A time series object #' -#'@return A time series with partial years removed. +#' @return A time series with partial years removed. #' -#'@export +#' @export -tsCompleteYears <- function(tsData) -{ +tsCompleteYears <- function(tsData) { + if (frequency(tsData) != 12) { + stop("This function is only supported for monthly time series.") + } - if (frequency(tsData) != 12) { - stop("This function is only supported for monthly time series.") - } + origStartMonth <- start(tsData)[2] + origStartYear <- start(tsData)[1] + origEndMonth <- end(tsData)[2] + origEndYear <- end(tsData)[1] - origStartMonth <- start(tsData)[2] - origStartYear <- start(tsData)[1] - origEndMonth <- end(tsData)[2] - origEndYear <- end(tsData)[1] + newStartMonth <- 1 + newEndMonth <- 12 - newStartMonth <- 1 - newEndMonth <- 12 + tsObj <- tsData - tsObj <- tsData + if (origStartMonth > 1) tsObj <- window(tsObj, start = c(origStartYear + 1, newStartMonth)) + if (origEndMonth < 12) tsObj <- window(tsObj, end = c(origEndYear - 1, newEndMonth)) - if (origStartMonth > 1) tsObj <- window(tsObj, start=c(origStartYear+1,newStartMonth)) - if (origEndMonth < 12) tsObj <- window(tsObj, end=c(origEndYear-1,newEndMonth)) - - return (tsObj) + return(tsObj) } diff --git a/docs/404.html b/docs/404.html index 4a370b97..7bc9e8e5 100644 --- a/docs/404.html +++ b/docs/404.html @@ -6,7 +6,7 @@