forked from OHDSI/Legend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadDataToDb.R
314 lines (288 loc) · 14.4 KB
/
UploadDataToDb.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
# Copyright 2018 Observational Health Data Sciences and Informatics
#
# This file is part of Legend
#
# 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.library(DatabaseConnector)
#' Upload results to the database server
#'
#' @description
#' Upload the exported results of a database-indication run into the database. Assumes the data conforms to
#' the LEGEND data model.
#'
#' @param connectionDetails An object of type \code{connectionDetails} as created
#' using the
#' \code{\link[DatabaseConnector]{createConnectionDetails}}
#' function in the DatabaseConnector package.
#' @param exportFolder Name of local folder where the export results are stored; make sure to use
#' forward slashes (/). Do not use a folder on a network
#' drive since this greatly impacts performance.
#' @param createTables Create the tables on the server? This will drop the tables
#' if they already exist prior to creating them.
#' @param staging If TRUE, the table names on the server will have the postfix "_staging" to
#' allow new tables to be created while old ones are in use.
#' @param deletePriorData Should prior data (with the same keys) be deleted prior to uploading the
#' data to avoid duplicates?
#' @param skipBigTables Skip the covariate balance and KM tables?
#'
#' @export
uploadResultsToDatabase <- function(connectionDetails,
exportFolder,
createTables = FALSE,
staging = FALSE,
deletePriorData = TRUE,
skipBigTables = TRUE) {
conn <- connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(conn))
batchSize <- 1e+07
# ParallelLogger::clearLoggers()
# ParallelLogger::addDefaultConsoleLogger()
ParallelLogger::addDefaultFileLogger(file.path(exportFolder, "uploadLog.txt"))
files <- list.files(exportFolder, pattern = ".*csv")
# Dropping cm_interaction_result and chronograph tables:
files <- files[files != "cm_interaction_result.csv"]
files <- files[files != "chronograph.csv"]
if (skipBigTables) {
files <- files[files != "kaplan_meier_dist.csv"]
files <- files[files != "covariate_balance.csv"]
}
infinityToNa <- function(data) {
# Replace infinity with NA, because OHDSI Postgres server doesn't like infinity:
if (nrow(data) > 0) {
for (j in 1:ncol(data)) {
if (is.numeric(data[, j])) {
idx <- is.infinite(data[, j])
if (any(idx)) {
data[idx, j] <- NA
}
}
}
}
return(data)
}
keys <- list(indication = c("indication_id"),
cohort_method_analysis = c("analysis_id"),
covariate_analysis = c("covariate_analysis_id"),
incidence_analysis = c("incidence_analysis_id"),
single_exposure_of_interest = c("exposure_id"),
combi_exposure_of_interest = c("exposure_id"),
exposure_group = c("exposure_id", "exposure_group"),
outcome_of_interest = c("outcome_id"),
negative_control_outcome = c("outcome_id"),
positive_control_outcome = c("outcome_id"),
database = c("database_id"),
exposure_summary = c("database_id"),
comparison_summary = c("database_id"),
attrition = c("database_id"),
cm_follow_up_dist = c("database_id"),
covariate = c("database_id"),
cohort_method_result = c("database_id"),
cm_interaction_result = c("database_id"),
chronograph = c("database_id"),
incidence = c("database_id"),
covariate_balance = c("database_id"),
preference_score_dist = c("database_id"),
kaplan_meier_dist = c("database_id"),
propensity_model = c("database_id"))
deleteExistingData <- function(data, tableName, sqlTableName, dropped) {
key <- keys[[tableName]]
toDrop <- unique(data[, key, drop = FALSE])
if (!is.null(toDrop$database_id)) {
toDrop$database_id <- as.character(toDrop$database_id)
}
if (!is.null(toDrop$exposure_group)) {
toDrop$exposure_group <- as.character(toDrop$exposure_group)
}
if (!is.null(dropped)) {
dropped$alreadyDropped <- TRUE
toDrop <- merge(toDrop, dropped, all.x = TRUE)
toDrop <- toDrop[is.na(toDrop$alreadyDropped), ]
toDrop$alreadyDropped <- NULL
dropped$alreadyDropped <- NULL
}
if (nrow(toDrop) > 0) {
createSqlStatement <- function(i) {
sql <- paste0("DELETE FROM ",
sqlTableName,
" WHERE ",
paste(paste0(key, " = '", toDrop[i, ], "'"), collapse = " AND "),
";")
}
sql <- sapply(1:nrow(toDrop), createSqlStatement)
sql <- paste(sql, collapse = "\n")
sql <- SqlRender::translate(sql, targetDialect = connectionDetails$dbms)
executeSql(conn, sql, progressBar = FALSE, reportOverallTime = FALSE)
}
dropped <- unique(rbind(dropped, toDrop))
return(dropped)
}
dropInteractionCovariateId <- function(data) {
if (any(names(data) == "interaction_covariate_id")) {
data <- data[is.na(data$interaction_covariate_id), -which(names(data) == "interaction_covariate_id")]
}
return(data)
}
for (i in 1:length(files)) {
file <- files[i]
tableName <- gsub(".csv$", "", file)
if (staging) {
sqlTableName <- paste0(tableName, "_staging")
} else {
sqlTableName <- tableName
}
ParallelLogger::logInfo("Uploading ", file, " to ", sqlTableName)
start <- Sys.time()
fileCon <- file(file.path(exportFolder, file), "r")
data <- read.csv(fileCon, nrows = batchSize)
columnNames <- colnames(data)
data <- dropInteractionCovariateId(data)
data <- infinityToNa(data)
first <- createTables
done <- FALSE
insertedCount <- 0
dropped <- NULL
while (!done) {
if (!createTables && deletePriorData) {
ParallelLogger::logInfo("- Deleting existing data with same keys")
dropped <- deleteExistingData(data = data,
tableName = tableName,
sqlTableName = sqlTableName,
dropped = dropped)
}
ParallelLogger::logInfo("- Inserting data")
DatabaseConnector::insertTable(connection = conn,
tableName = sqlTableName,
data = data,
dropTableIfExists = first,
createTable = first,
tempTable = FALSE)
insertedCount <- insertedCount + nrow(data)
ParallelLogger::logInfo(paste("- Inserted ", insertedCount, "rows"))
data <- read.csv(fileCon, nrows = batchSize, header = FALSE, col.names = columnNames)
done <- (nrow(data) == 0)
data <- dropInteractionCovariateId(data)
data <- infinityToNa(data)
first <- FALSE
}
close(fileCon)
delta <- Sys.time() - start
ParallelLogger::logInfo(paste("- Uploading took", signif(delta, 3), attr(delta, "units")))
}
}
#' Create indices in the LEGEND result database
#'
#' @description
#' Create indices on the large tables in the LEGEND evidence model for faster searching.
#'
#' @param connectionDetails An object of type \code{connectionDetails} as created
#' using the
#' \code{\link[DatabaseConnector]{createConnectionDetails}}
#' function in the DatabaseConnector package.
#' @param staging If TRUE, the table names on the server will have the postfix "_staging" to
#' allow new tables to be created while old ones are in use.
#'
#' @export
createIndicesOnDatabase <- function(connectionDetails,
staging = FALSE) {
conn <- connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(conn))
sql <- "CREATE INDEX idx_attrition ON attrition (database_id, exposure_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_covariate ON covariate (database_id, covariate_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_cm_follow_up_dist ON cm_follow_up_dist (database_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_cohort_method_result ON cohort_method_result (database_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_t_cohort_method_result ON cohort_method_result (target_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_c_cohort_method_result ON cohort_method_result (comparator_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_cm_interaction_result ON cm_interaction_result (database_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_covariate_balance ON covariate_balance (database_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_covariate_balance_2 ON covariate_balance (target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_preference_score_dist ON preference_score_dist (database_id, target_id, comparator_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_kaplan_meier_dist ON kaplan_meier_dist (database_id, target_id, comparator_id, outcome_id, analysis_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
sql <- "CREATE INDEX idx_propensity_model ON propensity_model (database_id, target_id, comparator_id);"
if (staging) {
sql <- gsub(" \\(", "_staging (", gsub(" ON ", "_staging ON ", sql))
}
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
}
#' Drop the old tables, and remove _staging postfix from staging tables.
#'
#' @param connectionDetails An object of type \code{connectionDetails} as created
#' using the
#' \code{\link[DatabaseConnector]{createConnectionDetails}}
#' function in the DatabaseConnector package.
#' @param schema The schema holding the LEGEND results.
#'
#'
#' @export
unstageDb <- function(connectionDetails, schema = "legend") {
conn <- connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(conn))
tables <- DatabaseConnector::getTableNames(conn, schema)
stagingTables <- tables[grepl("_STAGING$", tables)]
toDrop <- gsub("_STAGING$", "", stagingTables)
for (i in 1:length(toDrop)) {
tableName <- toDrop[i]
ParallelLogger::logInfo("Dropping table ", tableName)
sql <- paste("DROP TABLE", tableName)
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
}
for (i in 1:length(stagingTables)) {
tableName <- stagingTables[i]
newTableName <- gsub("_STAGING$", "", tableName)
ParallelLogger::logInfo("Renaming table ", tableName, " to ", newTableName)
sql <- sprintf("ALTER TABLE %s RENAME TO %s;", tableName, newTableName)
DatabaseConnector::executeSql(conn, sql, progressBar = FALSE)
}
}