-
Notifications
You must be signed in to change notification settings - Fork 5
/
Database.R
436 lines (396 loc) · 12.7 KB
/
Database.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# @file Database.R
#
# Copyright 2024 Observational Health Data Sciences and Informatics
#
# This file is part of Characterization
#
# 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.
#' Create an sqlite database connection
#' @description
#' This function creates a connection to an sqlite database
#'
#' @details
#' This function creates a sqlite database and connection
#'
#' @param sqliteLocation The location of the sqlite database
#'
#' @return
#' Returns the connection to the sqlite database
#'
#' @export
createSqliteDatabase <- function(
sqliteLocation = tempdir()) {
sqliteLocation <- file.path(
sqliteLocation,
"sqliteCharacterization"
)
if (!dir.exists(sqliteLocation)) {
dir.create(
path = sqliteLocation,
recursive = T
)
}
connectionDetails <- DatabaseConnector::createConnectionDetails(
dbms = "sqlite",
server = file.path(sqliteLocation, "sqlite.sqlite")
)
connection <- DatabaseConnector::connect(
connectionDetails = connectionDetails
)
return(connection)
}
# move Andromeda to sqlite database
insertAndromedaToDatabase <- function(
connection,
databaseSchema,
tableName,
andromedaObject,
tempEmulationSchema,
bulkLoad = T,
tablePrefix = "c_",
minCellCount = 0,
minCellCountColumns = list()) {
errorMessages <- checkmate::makeAssertCollection()
.checkTablePrefix(
tablePrefix = tablePrefix,
errorMessages = errorMessages
)
checkmate::reportAssertions(errorMessages)
message("Inserting Andromeda table into database table ", tablePrefix, tableName)
Andromeda::batchApply(
tbl = andromedaObject,
fun = function(x) {
data <- as.data.frame(x %>% dplyr::collect()) # apply minCellCount
data <- removeMinCell(
data = data,
minCellCount = minCellCount,
minCellCountColumns = minCellCountColumns
)
DatabaseConnector::insertTable(
connection = connection,
databaseSchema = databaseSchema,
tableName = paste0(tablePrefix, tableName),
data = data,
dropTableIfExists = F,
createTable = F,
tempEmulationSchema = tempEmulationSchema,
bulkLoad = bulkLoad,
camelCaseToSnakeCase = T
)
}
)
return(TRUE)
}
removeMinCell <- function(
data,
minCellCount = 0,
minCellCountColumns = list()) {
for (columns in minCellCountColumns) {
ind <- apply(
X = data[, columns, drop = FALSE],
MARGIN = 1,
FUN = function(x) sum(x < minCellCount) > 0
)
if (sum(ind) > 0) {
ParallelLogger::logInfo(
paste0(
"Removing values less than ",
minCellCount,
" from ",
paste(columns, collapse = " and ")
)
)
data[ind, columns] <- -1
}
}
return(data)
}
#' Create the results tables to store characterization results into a database
#' @description
#' This function executes a large set of SQL statements to create tables that can store results
#'
#' @details
#' This function can be used to create (or delete) Characterization result tables
#'
#' @param conn A connection to a database created by using the
#' function \code{connect} in the
#' \code{DatabaseConnector} package.
#' @param resultSchema The name of the database schema that the result tables will be created.
#' @param targetDialect The database management system being used
#' @param deleteExistingTables If true any existing tables matching the Characterization result tables names will be deleted
#' @param createTables If true the Characterization result tables will be created
#' @param tablePrefix A string appended to the Characterization result tables
#' @param tempEmulationSchema The temp schema used when the database management system is oracle
#'
#' @return
#' Returns NULL but creates the required tables into the specified database schema.
#'
#' @export
createCharacterizationTables <- function(
conn,
resultSchema,
targetDialect = "postgresql",
deleteExistingTables = T,
createTables = T,
tablePrefix = "c_",
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema")) {
errorMessages <- checkmate::makeAssertCollection()
.checkTablePrefix(
tablePrefix = tablePrefix,
errorMessages = errorMessages
)
checkmate::reportAssertions(errorMessages)
if (deleteExistingTables) {
message("Deleting existing tables")
tables <- getResultTables()
tables <- paste0(tablePrefix, tables)
alltables <- tolower(
DatabaseConnector::getTableNames(
connection = conn,
databaseSchema = resultSchema
)
)
for (tb in tables) {
if (tb %in% alltables) {
sql <- "DELETE FROM @my_schema.@table"
sql <- SqlRender::render(
sql = sql,
my_schema = resultSchema,
table = tb
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = targetDialect,
tempEmulationSchema = tempEmulationSchema
)
DatabaseConnector::executeSql(
connection = conn,
sql = sql
)
sql <- "DROP TABLE @my_schema.@table"
sql <- SqlRender::render(
sql = sql,
my_schema = resultSchema,
table = tb
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = targetDialect,
tempEmulationSchema = tempEmulationSchema
)
DatabaseConnector::executeSql(
connection = conn,
sql = sql
)
}
}
}
if (createTables) {
ParallelLogger::logInfo("Creating characterization results tables")
renderedSql <- SqlRender::loadRenderTranslateSql(
sqlFilename = "ResultTables.sql",
packageName = "Characterization",
dbms = targetDialect,
tempEmulationSchema = tempEmulationSchema,
my_schema = resultSchema,
table_prefix = tablePrefix
)
DatabaseConnector::executeSql(
connection = conn,
sql = renderedSql
)
# add database migration here in the future
}
}
#' Exports all tables in the result database to csv files
#' @description
#' This function extracts the database tables into csv files
#'
#' @details
#' This function extracts the database tables into csv files
#'
#' @param connectionDetails The connection details to input into the
#' function \code{connect} in the
#' \code{DatabaseConnector} package.
#' @param resultSchema The name of the database schema that the result tables will be created.
#' @param targetDialect DEPRECATED: derived from \code{connectionDetails}.
#' @param tablePrefix The table prefix to apply to the characterization result tables
#' @param filePrefix The prefix to apply to the files
#' @param tempEmulationSchema The temp schema used when the database management system is oracle
#' @param saveDirectory The directory to save the csv results
#' @param minMeanCovariateValue The minimum mean covariate value (i.e. the minimum proportion for
#' binary covariates) for a covariate to be included in covariate table.
#' Other covariates are removed to save space.
#'
#' @return
#' csv file per table into the saveDirectory
#'
#' @export
exportDatabaseToCsv <- function(
connectionDetails,
resultSchema,
targetDialect = NULL,
tablePrefix = "c_",
filePrefix = NULL,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
saveDirectory,
minMeanCovariateValue = 0.001) {
errorMessages <- checkmate::makeAssertCollection()
.checkConnectionDetails(connectionDetails, errorMessages)
.checkTablePrefix(
tablePrefix = tablePrefix,
errorMessages = errorMessages
)
checkmate::reportAssertions(errorMessages)
if (!is.null(targetDialect)) {
warning("The targetDialect argument is deprecated")
}
if (is.null(filePrefix)) {
filePrefix <- ""
}
# connect to result database
connection <- DatabaseConnector::connect(
connectionDetails = connectionDetails
)
on.exit(
DatabaseConnector::disconnect(connection)
)
# create the folder to save the csv files
if (!dir.exists(saveDirectory)) {
dir.create(
path = saveDirectory,
recursive = T
)
}
# max number of rows extracted at a time
maxRowCount <- 1e6
# get the table names using the function in uploadToDatabase.R
tables <- getResultTables()
# extract result per table
for (table in tables) {
ParallelLogger::logInfo(paste0("Exporting rows from ", table, " to csv file"))
# get row count and figure out number of loops
sql <- "select count(*) as N from @resultSchema.@appendtotable@tablename;"
sql <- SqlRender::render(
sql = sql,
resultSchema = resultSchema,
appendtotable = tablePrefix,
tablename = table
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema
)
countN <- DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = F
)$N
# get column names
sql <- "select * from @resultSchema.@appendtotable@tablename where 1=0;"
sql <- SqlRender::render(
sql = sql,
resultSchema = resultSchema,
appendtotable = tablePrefix,
tablename = table
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema
)
cnames <- colnames(DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = F
))
inds <- floor(countN / maxRowCount)
tableAppend <- F
# NOTE: If the table has 0 rows (countN == 0),
# then setting the txtProgressBar will fail since
# min < max. So, setting max = countN+1 for this
# reason.
pb <- utils::txtProgressBar(min = 0, max = countN + 1, initial = 0)
for (i in 1:length(inds)) {
startRow <- (i - 1) * maxRowCount + 1
endRow <- min(i * maxRowCount, countN)
sql <- "select @cnames from
(select *,
ROW_NUMBER() OVER(ORDER BY @cnames) AS row
from @resultSchema.@appendtotable@tablename
) temp
where
temp.row >= @start_row and
temp.row <= @end_row;"
sql <- SqlRender::render(
sql = sql,
resultSchema = resultSchema,
appendtotable = tablePrefix,
tablename = table,
cnames = paste(cnames, collapse = ","),
start_row = startRow,
end_row = endRow
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema
)
result <- DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = F
)
result <- formatDouble(result)
# save the results as a csv
readr::write_csv(
x = result,
file = file.path(saveDirectory, paste0(tolower(filePrefix), table, ".csv")),
append = tableAppend
)
tableAppend <- T
# NOTE: Handling progresss bar per note on txtProgressBar
# above. Otherwise the progress bar doesn't show that it completed.
if (endRow == countN) {
utils::setTxtProgressBar(pb, countN + 1)
} else {
utils::setTxtProgressBar(pb, endRow)
}
}
close(pb)
}
invisible(saveDirectory)
}
getResultTables <- function() {
return(
unique(
readr::read_csv(
file = system.file(
"settings",
"resultsDataModelSpecification.csv",
package = "Characterization"
),
show_col_types = FALSE
)$table_name
)
)
}
# Removes scientific notation for any columns that are
# formatted as doubles. Based on this GitHub issue:
# https://github.com/tidyverse/readr/issues/671#issuecomment-300567232
formatDouble <- function(x, scientific = F, ...) {
doubleCols <- vapply(x, is.double, logical(1))
x[doubleCols] <- lapply(x[doubleCols], format, scientific = scientific, ...)
return(x)
}