-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathLoadEventTables.r
318 lines (288 loc) · 11 KB
/
LoadEventTables.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
#' @title Load CDM Non-Vocabulary Tables.
#'
#' @description This function loads the CDM Event tables with Synthea data.
#'
#' @details This function assumes \cr\code{createCDMTables()}, \cr\code{createSyntheaTables()}, \cr\code{LoadSyntheaTables()},
#' \cr\code{LoadVocabTables()}, and \cr\code{CreateMapAndRollupTables()} have all been run.
#'
#' @param connectionDetails An R object of type\cr\code{connectionDetails} created using the
#' function \code{createConnectionDetails} in the
#' \code{DatabaseConnector} package.
#' @param cdmSchema The name of the database schema that will contain the CDM.
#' Requires read and write permissions to this database. On SQL
#' Server, this should specify both the database and the schema,
#' so for example 'cdm_instance.dbo'.
#' @param syntheaSchema The name of the database schema that contain the Synthea
#' instance. Requires read and write permissions to this database. On SQL
#' Server, this should specify both the database and the schema,
#' so for example 'cdm_instance.dbo'.
#' @param cdmVersion The version of your CDM. Currently "5.3" and "5.4".
#' @param syntheaVersion The version of Synthea used to generate the csv files.
#' Currently "2.7.0","3.0.0","3.1.0","3.2.0" and "3.3.0" are supported.
#' @param cdmSourceName The source name to insert into the CDM_SOURCE table. Default is Synthea synthetic health database.
#' @param cdmSourceAbbreviation The source abbreviation to insert into the CDM_SOURCE table. Default is Synthea.
#' @param cdmHolder The holder to insert into the CDM_SOURCE table. Default is OHDSI
#' @param cdmSourceDescription The description of the source data. Default is generic Synthea description.
#' @param createIndices A boolean that determines whether or not to create indices on CDM tables before the ETL.
#' @param sqlOnly A boolean that determines whether or not to perform the load or generate SQL scripts. Default is FALSE.
#'
#'@export
LoadEventTables <- function(connectionDetails,
cdmSchema,
syntheaSchema,
cdmVersion,
syntheaVersion = "2.7.0",
cdmSourceName = "Synthea synthetic health database",
cdmSourceAbbreviation = "Synthea",
cdmHolder = "OHDSI",
cdmSourceDescription = "SyntheaTM is a Synthetic Patient Population Simulator. The goal is to output synthetic, realistic (but not real), patient data and associated health records in a variety of formats.",
createIndices = FALSE,
sqlOnly = FALSE)
{
# Determine which sql scripts to run based on the given version.
# The path is relative to inst/sql/sql_server.
if (cdmVersion == "5.3") {
sqlFilePath <- "cdm_version/v531"
} else if (cdmVersion == "5.4") {
sqlFilePath <- "cdm_version/v540"
} else {
stop("Unsupported CDM specified. Supported CDM versions are \"5.3\" and \"5.4\".")
}
supportedSyntheaVersions <- c("2.7.0", "3.0.0", "3.1.0", "3.2.0", "3.3.0")
if (!(syntheaVersion %in% supportedSyntheaVersions))
stop(
"Invalid Synthea version specified. Currently \"2.7.0\", \"3.0.0\",\"3.1.0\",\"3.2.0\", and \"3.3.0\" are supported."
)
if (createIndices) {
print("Creating Indices on CDM Tables....")
indexSQLFile <- CommonDataModel::writeIndex(
targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmSchema,
outputfolder = tempdir()
)
indexDDL <- SqlRender::readSql(paste0(tempdir(), "/", indexSQLFile))
conn <- DatabaseConnector::connect(connectionDetails)
DatabaseConnector::executeSql(conn, indexDDL)
DatabaseConnector::disconnect(conn)
print("Index Creation Complete.")
}
if (!sqlOnly) {
conn <- DatabaseConnector::connect(connectionDetails)
} else {
if (!dir.exists("output")) {
dir.create("output")
}
}
runStep <- function(sql, fileQuery) {
if (sqlOnly) {
writeLines(paste0("Saving to output/", sql))
SqlRender::writeSql(sql, paste0("output/", fileQuery))
} else {
writeLines(paste0("Running: ", fileQuery))
DatabaseConnector::executeSql(conn, sql)
}
}
# location
fileQuery <- "insert_location.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# care_site
fileQuery <- "insert_care_site.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# person
fileQuery <- "insert_person.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# observation period
fileQuery <- "insert_observation_period.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# provider
fileQuery <- "insert_provider.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# visit occurrence
fileQuery <- "insert_visit_occurrence.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# visit detail
fileQuery <- "insert_visit_detail.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# condition occurrence
fileQuery <- "insert_condition_occurrence.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema,
synthea_version = syntheaVersion
)
runStep(sql, fileQuery)
# observation
fileQuery <- "insert_observation.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# measurement
fileQuery <- "insert_measurement.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema,
synthea_version = syntheaVersion
)
runStep(sql, fileQuery)
# procedure occurrence
fileQuery <- "insert_procedure_occurrence.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema,
synthea_version = syntheaVersion
)
runStep(sql, fileQuery)
# drug exposure
fileQuery <- "insert_drug_exposure.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# condition era
fileQuery <- "insert_condition_era.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema
)
runStep(sql, fileQuery)
# drug era
fileQuery <- "insert_drug_era.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema
)
runStep(sql, fileQuery)
# cdm source
fileQuery <- "insert_cdm_source.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
cdm_version = cdmVersion,
cdm_source_name = cdmSourceName,
cdm_source_abbreviation = cdmSourceAbbreviation,
cdm_holder = cdmHolder,
source_description = paste("Synthea version: ", syntheaVersion, " ", cdmSourceDescription)
)
runStep(sql, fileQuery)
# device exposure
fileQuery <- "insert_device_exposure.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# death
fileQuery <- "insert_death.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
# payer_plan_period
fileQuery <- "insert_payer_plan_period.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema,
synthea_version = syntheaVersion
)
runStep(sql, fileQuery)
# cost
if (syntheaVersion == "2.7.0")
fileQuery <- "insert_cost_v270.sql"
else if (syntheaVersion %in% c("3.0.0", "3.1.0", "3.2.0", "3.3.0"))
fileQuery <- "insert_cost_v300.sql"
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = file.path(sqlFilePath, fileQuery),
packageName = "ETLSyntheaBuilder",
dbms = connectionDetails$dbms,
cdm_schema = cdmSchema,
synthea_schema = syntheaSchema
)
runStep(sql, fileQuery)
if (!sqlOnly) {
DatabaseConnector::disconnect(conn)
}
}