forked from OHDSI/CohortDiagnostics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runBreakdownIndexEvents.R
326 lines (281 loc) · 9.93 KB
/
runBreakdownIndexEvents.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Copyright 2024 Observational Health Data Sciences and Informatics
#
# This file is part of CohortDiagnostics
#
# 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.
getBreakdownIndexEvents <- function(connection,
cohort,
conceptSets,
tempEmulationSchema,
cdmDatabaseSchema,
vocabularyDatabaseSchema,
cohortDatabaseSchema,
cohortTable) {
if (!CohortGenerator::isCohortDefinitionSet(cohort) || nrow(cohort) != 1) {
stop("cohortDefinitionSet must have one row")
}
if(!tempTableExists(connection, "inst_concept_sets")) {
stop("Execute the function runResolvedConceptSets() first.")
}
domains <-
readr::read_csv(
system.file("csv", "domains.csv", package = "CohortDiagnostics", mustWork = T),
show_col_types = FALSE
)
emptyResult <- dplyr::tibble(
domainTable = character(),
domainField = character(),
conceptId = double(),
conceptCount = double(),
subjectCount = double(),
cohortId = double()
)
if (isTRUE(cohort$isSubset)) {
checkmate::assert_character(cohort$parentJson, len = 1, any.missing = F)
jsonDef <- cohort$parentJson
} else {
jsonDef <- cohort$json
}
cohortDefinition <- RJSONIO::fromJSON(jsonDef, digits = 23)
primaryCodesetIds <-
lapply(
cohortDefinition$PrimaryCriteria$CriteriaList,
getCodeSetIds
)
if (length(primaryCodesetIds)) {
primaryCodesetIds <- dplyr::bind_rows(primaryCodesetIds)
} else {
primaryCodesetIds <- data.frame()
}
if (nrow(primaryCodesetIds) == 0) {
warning(
"No primary event criteria concept sets found for cohort id: ",
cohort$cohortId
)
return(emptyResult)
}
primaryCodesetIds <- primaryCodesetIds %>%
dplyr::filter(.data$domain %in% c(domains$domain %>% unique()))
if (nrow(primaryCodesetIds) == 0) {
warning(
"Primary event criteria concept sets found for cohort id: ",
cohort$cohortId, " but,",
"\nnone of the concept sets belong to the supported domains.",
"\nThe supported domains are:\n",
paste(domains$domain, collapse = ", ")
)
return(emptyResult)
}
primaryCodesetIds <- conceptSets %>%
dplyr::filter(.data$cohortId %in% cohort$cohortId) %>%
dplyr::select(
codeSetIds = "conceptSetId",
"uniqueConceptSetId"
) %>%
dplyr::distinct() %>%
dplyr::inner_join(primaryCodesetIds %>% dplyr::distinct(), by = "codeSetIds")
pasteIds <- function(row) {
return(dplyr::tibble(
domain = row$domain[1],
uniqueConceptSetId = paste(row$uniqueConceptSetId, collapse = ", ")
))
}
primaryCodesetIds <-
lapply(
split(primaryCodesetIds, primaryCodesetIds$domain),
pasteIds
)
if (length(primaryCodesetIds) == 0) {
return(emptyResult)
} else {
primaryCodesetIds <- dplyr::bind_rows(primaryCodesetIds)
}
getCounts <- function(row) {
domain <- domains %>% dplyr::filter(.data$domain == row$domain)
sql <-
SqlRender::loadRenderTranslateSql(
"CohortEntryBreakdown.sql",
packageName = utils::packageName(),
dbms = connection@dbms,
tempEmulationSchema = tempEmulationSchema,
cdm_database_schema = cdmDatabaseSchema,
vocabulary_database_schema = vocabularyDatabaseSchema,
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
cohort_id = cohort$cohortId,
domain_table = domain$domainTable,
domain_start_date = domain$domainStartDate,
domain_concept_id = domain$domainConceptId,
domain_source_concept_id = domain$domainSourceConceptId,
use_source_concept_id = !(is.na(domain$domainSourceConceptId) | is.null(domain$domainSourceConceptId)),
primary_codeset_ids = row$uniqueConceptSetId,
concept_set_table = "#inst_concept_sets",
store = TRUE,
store_table = "#breakdown"
)
DatabaseConnector::executeSql(
connection = connection,
sql = sql,
progressBar = FALSE,
reportOverallTime = FALSE
)
counts <-
DatabaseConnector::renderTranslateQuerySql(
connection = connection,
sql = "SELECT * FROM #breakdown;",
tempEmulationSchema = tempEmulationSchema,
snakeCaseToCamelCase = TRUE
) %>%
tidyr::tibble()
sql <- "INSERT INTO #concept_ids (concept_id)
SELECT DISTINCT a.concept_id
FROM #breakdown a
LEFT JOIN #concept_ids b ON a.concept_id = b.concept_id
WHERE a.concept_id is not NULL AND b.concept_id is NULL;"
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = sql,
tempEmulationSchema = tempEmulationSchema,
progressBar = FALSE,
reportOverallTime = FALSE
)
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = "TRUNCATE TABLE #breakdown;\nDROP TABLE #breakdown;",
tempEmulationSchema = tempEmulationSchema,
progressBar = FALSE,
reportOverallTime = FALSE
)
return(counts)
}
# optimization idea - can this loop be removed and done all in one sql statement?
counts <-
lapply(split(primaryCodesetIds, 1:nrow(primaryCodesetIds)), getCounts) %>%
dplyr::bind_rows() %>%
dplyr::arrange(.data$conceptCount)
if (nrow(counts) > 0) {
counts$cohortId <- cohort$cohortId
} else {
ParallelLogger::logInfo(
"Index event breakdown results were not returned for: ",
cohort$cohortId
)
return(emptyResult)
}
return(counts)
}
#' runBreakdownIndexEvents
#'
#' @template connection
#' @template cohortDefinitionSet
#' @template tempEmulationSchema
#' @template cdmDatabaseSchema
#' @template vocabularyDatabaseSchema
#' @template cohortDatabaseSchema
#' @template databaseId
#' @template exportFolder
#' @template minCellCount
#' @template cohortTable
#' @template Incremental
#'
#' @return NULL
#' @export
runBreakdownIndexEvents <- function(connection,
cohortDefinitionSet,
tempEmulationSchema,
cdmDatabaseSchema,
vocabularyDatabaseSchema,
cohortDatabaseSchema,
databaseId,
exportFolder,
minCellCount,
cohortTable,
incremental = FALSE,
incrementalFolder) {
ParallelLogger::logInfo("Breaking down index events")
start <- Sys.time()
cohortDefinitionSet$checksum <- computeChecksum(cohortDefinitionSet$sql)
# for each row in the cohort definition set add a column with parent json if it is a subset
# this is so that all data needed for running one cohort is in one row of the cohort definition set
cohortDefinitionSet$parentJson <- vapply(
split(cohortDefinitionSet, cohortDefinitionSet$cohortId),
FUN = function(.) getParentCohort(., cohortDefinitionSet)$json,
FUN.VALUE = character(1L)
)
subset <- subsetToRequiredCohorts(
cohorts = cohortDefinitionSet,
task = "runBreakdownIndexEvents",
incremental = incremental,
recordKeepingFile = file.path(incrementalFolder, "CreatedDiagnostics.csv")
) %>% dplyr::distinct()
if (nrow(subset) == 0) {
return(NULL)
}
if (incremental && (nrow(cohorts) - nrow(subset)) > 0) {
ParallelLogger::logInfo(sprintf(
"Skipping %s cohorts in incremental mode.",
nrow(cohorts) - nrow(subset)
))
}
conceptSets <- combineConceptSetsFromCohorts(cohortDefinitionSet) %>%
dplyr::filter(.data$cohortId %in% subset$cohortId)
data <- lapply(split(subset, subset$cohortId),
function(cohort) {
ParallelLogger::logInfo(
"- Breaking down index events for cohort '", cohort$cohortName, "'"
)
timeExecution(
exportFolder,
taskName = "getBreakdownIndexEvents",
cohortIds = cohort$cohortId,
parent = "runConceptSetDiagnostics",
expr = {
result <- getBreakdownIndexEvents(
connection = connection,
cohort = cohort,
conceptSets = conceptSets,
tempEmulationSchema = tempEmulationSchema,
cdmDatabaseSchema = cdmDatabaseSchema,
vocabularyDatabaseSchema = vocabularyDatabaseSchema,
cohortDatabaseSchema = cohortDatabaseSchema,
cohortTable = cohortTable
)
}
)
return(result)
}
)
data <- dplyr::bind_rows(data)
exportDataToCsv(
data = data,
tableName = "index_event_breakdown",
fileName = file.path(exportFolder, "index_event_breakdown.csv"),
minCellCount = minCellCount,
databaseId = databaseId,
incremental = incremental,
cohortId = subset$cohortId
)
recordTasksDone(
cohortId = subset$cohortId,
task = "runBreakdownIndexEvents",
checksum = subset$checksum,
recordKeepingFile = file.path(incrementalFolder, "CreatedDiagnostics.csv"),
incremental = incremental
)
delta <- Sys.time() - start
ParallelLogger::logInfo(paste(
"Breaking down index event took",
signif(delta, 3),
attr(delta, "units")
))
}