Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inclusion rule insert function #158

Open
anthonysena opened this issue Jun 5, 2024 · 0 comments
Open

Remove inclusion rule insert function #158

anthonysena opened this issue Jun 5, 2024 · 0 comments
Milestone

Comments

@anthonysena
Copy link
Collaborator

This function is not necessary as we can extract the inclusion rules names from a cohort definition set and we do not need to push this information to the database.

insertInclusionRuleNames <- function(connectionDetails = NULL,
connection = NULL,
cohortDefinitionSet,
cohortDatabaseSchema,
cohortInclusionTable = getCohortTableNames()$cohortInclusionTable) {
# Parameter validation
if (is.null(connection) && is.null(connectionDetails)) {
stop("You must provide either a database connection or the connection details.")
}
checkmate::assertDataFrame(cohortDefinitionSet, min.rows = 1, col.names = "named")
checkmate::assertNames(colnames(cohortDefinitionSet),
must.include = c(
"cohortId",
"cohortName",
"json"
)
)
if (is.null(connection)) {
connection <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))
}
tableList <- DatabaseConnector::getTableNames(connection, cohortDatabaseSchema)
if (!toupper(cohortInclusionTable) %in% toupper(tableList)) {
stop(paste0(cohortInclusionTable, " table not found in schema: ", cohortDatabaseSchema, ". Please make sure the table is created using the createCohortTables() function before calling this function."))
}
# Assemble the cohort inclusion rules
# NOTE: This data frame must match the @cohort_inclusion_table
# structure as defined in inst/sql/sql_server/CreateCohortTables.sql
inclusionRules <- data.frame(
cohortDefinitionId = bit64::integer64(),
ruleSequence = integer(),
name = character(),
description = character()
)
# Remove any cohort definitions that do not include the JSON property
cohortDefinitionSet <- cohortDefinitionSet[!(is.null(cohortDefinitionSet$json) | is.na(cohortDefinitionSet$json)), ]
for (i in 1:nrow(cohortDefinitionSet)) {
cohortDefinition <- RJSONIO::fromJSON(content = cohortDefinitionSet$json[i], digits = 23)
if (!is.null(cohortDefinition$InclusionRules)) {
nrOfRules <- length(cohortDefinition$InclusionRules)
if (nrOfRules > 0) {
for (j in 1:nrOfRules) {
ruleName <- cohortDefinition$InclusionRules[[j]]$name
ruleDescription <- cohortDefinition$InclusionRules[[j]]$description
if (is.na(ruleName) || ruleName == "") {
ruleName <- paste0("Unamed rule (Sequence ", j - 1, ")")
}
if (is.null(ruleDescription)) {
ruleDescription <- ""
}
inclusionRules <- rbind(
inclusionRules,
data.frame(
cohortDefinitionId = bit64::as.integer64(cohortDefinitionSet$cohortId[i]),
ruleSequence = as.integer(j - 1),
name = ruleName,
description = ruleDescription
)
)
}
}
}
}
# Remove any existing data to prevent duplication
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = "TRUNCATE TABLE @cohort_database_schema.@table;",
progressBar = FALSE,
reportOverallTime = FALSE,
cohort_database_schema = cohortDatabaseSchema,
table = cohortInclusionTable
)
# Insert the inclusion rules
if (nrow(inclusionRules) > 0) {
ParallelLogger::logInfo("Inserting inclusion rule names")
DatabaseConnector::insertTable(
connection = connection,
databaseSchema = cohortDatabaseSchema,
tableName = cohortInclusionTable,
data = inclusionRules,
dropTableIfExists = FALSE,
createTable = FALSE,
camelCaseToSnakeCase = TRUE
)
} else {
warning("No inclusion rules found in the cohortDefinitionSet")
}
invisible(inclusionRules)
}

@anthonysena anthonysena added this to the v1.0 milestone Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant