-
Notifications
You must be signed in to change notification settings - Fork 10
/
CohortStats.R
266 lines (248 loc) · 9.57 KB
/
CohortStats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2024 Observational Health Data Sciences and Informatics
#
# This file is part of CohortGenerator
#
# 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
#
# http://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.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Used to insert the inclusion rule names from a cohort definition set
#' when generating cohorts that include cohort statistics
#'
#' @description
#' This function will take a cohortDefinitionSet that inclusions the Circe JSON
#' representation of each cohort, parse the InclusionRule property to obtain
#' the inclusion rule name and sequence number and insert the values into the
#' cohortInclusionTable. This function is only required when generating cohorts
#' that include cohort statistics.
#'
#' @template Connection
#'
#' @template CohortDefinitionSet
#'
#' @template CohortDatabaseSchema
#'
#' @param cohortInclusionTable Name of the inclusion table, one of the tables for storing
#' inclusion rule statistics.
#'
#' @returns
#' A data frame containing the inclusion rules by cohort and sequence ID
#'
#' @export
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.")
}
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."))
}
inclusionRules <- getCohortInclusionRules(cohortDefinitionSet)
# 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) {
rlang::inform("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)
}
# Get stats data
getStatsTable <- function(connectionDetails,
connection = NULL,
cohortDatabaseSchema,
table,
snakeCaseToCamelCase = FALSE,
databaseId = NULL,
includeDatabaseId = TRUE) {
if (is.null(connection)) {
# Establish the connection and ensure the cleanup is performed
connection <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))
}
# Force databaseId to NULL when includeDatabaseId is FALSE
if (!includeDatabaseId) {
databaseId <- NULL
}
rlang::inform(paste0("- Fetching data from ", table))
sql <- "SELECT {@database_id != ''}?{CAST('@database_id' as VARCHAR(255)) as database_id,} t.* FROM @cohort_database_schema.@table t"
data <- DatabaseConnector::renderTranslateQuerySql(
sql = sql,
connection = connection,
snakeCaseToCamelCase = snakeCaseToCamelCase,
table = table,
cohort_database_schema = cohortDatabaseSchema,
database_id = ifelse(test = is.null(databaseId),
yes = "",
no = databaseId
)
)
if (!snakeCaseToCamelCase) {
colnames(data) <- tolower(colnames(data))
}
return(data)
}
#' Get Cohort Inclusion Stats Table Data
#'
#' @description
#' This function returns a data frame of the data in the Cohort Inclusion Tables.
#' Results are organized in to a list with 5 different data frames:
#' * cohortInclusionTable
#' * cohortInclusionResultTable
#' * cohortInclusionStatsTable
#' * cohortSummaryStatsTable
#' * cohortCensorStatsTable
#'
#'
#' These can be optionally specified with the `outputTables`.
#' See `exportCohortStatsTables` function for saving data to csv.
#'
#' @md
#' @inheritParams exportCohortStatsTables
#'
#' @param snakeCaseToCamelCase Convert column names from snake case to camel case.
#' @param outputTables Character vector. One or more of "cohortInclusionTable", "cohortInclusionResultTable",
#' "cohortInclusionStatsTable", "cohortInclusionStatsTable", "cohortSummaryStatsTable"
#' or "cohortCensorStatsTable". Output is limited to these tables. Cannot export, for,
#' example, the cohort table. Defaults to all stats tables.
#' @export
getCohortStats <- function(connectionDetails,
connection = NULL,
cohortDatabaseSchema,
databaseId = NULL,
snakeCaseToCamelCase = TRUE,
outputTables = c(
"cohortInclusionTable",
"cohortInclusionResultTable",
"cohortInclusionStatsTable",
"cohortInclusionStatsTable",
"cohortSummaryStatsTable",
"cohortCensorStatsTable"
),
cohortTableNames = getCohortTableNames()) {
# Names of cohort table names must include output tables
checkmate::assertNames(names(cohortTableNames), must.include = outputTables)
# ouput tables strictly the set of allowed tables
checkmate::assertNames(outputTables,
subset.of = c(
"cohortInclusionTable",
"cohortInclusionResultTable",
"cohortInclusionStatsTable",
"cohortInclusionStatsTable",
"cohortSummaryStatsTable",
"cohortCensorStatsTable"
)
)
results <- list()
for (table in outputTables) {
# The cohortInclusionTable does not hold database
# specific information so the databaseId
# should NOT be included.
includeDatabaseId <- ifelse(test = table != "cohortInclusionTable",
yes = TRUE,
no = FALSE
)
results[[table]] <- getStatsTable(
connectionDetails = connectionDetails,
connection = connection,
cohortDatabaseSchema = cohortDatabaseSchema,
table = cohortTableNames[[table]],
snakeCaseToCamelCase = snakeCaseToCamelCase,
includeDatabaseId = includeDatabaseId,
databaseId = databaseId
)
}
return(results)
}
#' Get Cohort Inclusion Rules from a cohort definition set
#'
#' @description
#' This function returns a data frame of the inclusion rules defined
#' in a cohort definition set.
#'
#' @md
#' @template CohortDefinitionSet
#'
#' @export
getCohortInclusionRules <- function(cohortDefinitionSet) {
checkmate::assertDataFrame(cohortDefinitionSet, min.rows = 1, col.names = "named")
checkmate::assertNames(colnames(cohortDefinitionSet),
must.include = c(
"cohortId",
"cohortName",
"json"
)
)
# 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 = numeric(),
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 = as.numeric(cohortDefinitionSet$cohortId[i]),
ruleSequence = as.integer(j - 1),
name = ruleName,
description = ruleDescription
)
)
}
}
}
}
invisible(inclusionRules)
}