|
| 1 | +# Copyright 2019 Observational Health Data Sciences and Informatics |
| 2 | +# |
| 3 | +# This file is part of CdmDdlBase |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +#' Create the OHDSI-SQL Common Data Model DDL code |
| 18 | +#' |
| 19 | +#' The createDdl, createForeignKeys, and createPrimaryKeys functions each return a character string |
| 20 | +#' containing their respective DDL SQL code in OHDSQL dialect for a specific CDM version. |
| 21 | +#' The SQL they generate needs to be rendered and translated before it can be executed. |
| 22 | +#' |
| 23 | +#' The DDL SQL code is created from a two csv files that detail the OMOP CDM Specifications. |
| 24 | +#' These files also form the basis of the CDM documentation and the Data Quality |
| 25 | +#' Dashboard. |
| 26 | +#' |
| 27 | +#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4 |
| 28 | +#' @return A character string containing the OHDSQL DDL |
| 29 | +#' @importFrom utils read.csv |
| 30 | +#' @export |
| 31 | +#' @examples |
| 32 | +#'\dontrun{ |
| 33 | +#' ddl <- createDdl("5.4") |
| 34 | +#' pk <- createPrimaryKeys("5.4") |
| 35 | +#' fk <- createForeignKeys("5.4") |
| 36 | +#'} |
| 37 | +createDdl <- function(cdmVersion){ |
| 38 | + # prevent check NOTE from occurring due to non-standard evaluation of variables |
| 39 | + cdmTableName <- cdmFieldName <- isRequired <- cdmDatatype <- NULL |
| 40 | + |
| 41 | + # argument checks |
| 42 | + stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions()) |
| 43 | + |
| 44 | + # cdmTableCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Table_Level.csv")), package = "CommonDataModel", mustWork = TRUE) |
| 45 | + # cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE) |
| 46 | + cdmTableCsvLoc <- "./inst/csv/Gaia_Table_Level.csv" |
| 47 | + cdmFieldCsvLoc <- "./inst/csv/Gaia_Field_Level.csv" |
| 48 | + |
| 49 | + tableSpecs <- read.csv(cdmTableCsvLoc, stringsAsFactors = FALSE) |
| 50 | + cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE) |
| 51 | + |
| 52 | + tableList <- tableSpecs$cdmTableName |
| 53 | + |
| 54 | + sql_result <- c() |
| 55 | + sql_result <- c(paste0("--@targetDialect CDM DDL Specification for OMOP Common Data Model ", cdmVersion)) |
| 56 | + for (tableName in tableList){ |
| 57 | + fields <- subset(cdmSpecs, cdmTableName == tableName) |
| 58 | + fieldNames <- fields$cdmFieldName |
| 59 | + |
| 60 | + if ('person_id' %in% fieldNames){ |
| 61 | + query <- "\n\n--HINT DISTRIBUTE ON KEY (person_id)\n" |
| 62 | + } else { |
| 63 | + query <- "\n\n--HINT DISTRIBUTE ON RANDOM\n" |
| 64 | + } |
| 65 | + |
| 66 | + sql_result <- c(sql_result, query, paste0("CREATE TABLE @cdmDatabaseSchema.", tableName, " (")) |
| 67 | + |
| 68 | + n_fields <- length(fieldNames) |
| 69 | + for(fieldName in fieldNames) { |
| 70 | + |
| 71 | + if (subset(fields, cdmFieldName == fieldName, isRequired) == "Yes") { |
| 72 | + nullable_sql <- (" NOT NULL") |
| 73 | + } else { |
| 74 | + nullable_sql <- (" NULL") |
| 75 | + } |
| 76 | + |
| 77 | + if (fieldName == fieldNames[[n_fields]]) { |
| 78 | + closing_sql <- (" );") |
| 79 | + } else { |
| 80 | + closing_sql <- (",") |
| 81 | + } |
| 82 | + |
| 83 | + if (fieldName=="offset") { |
| 84 | + field <- paste0('"',fieldName,'"') |
| 85 | + } else { |
| 86 | + field <- fieldName |
| 87 | + } |
| 88 | + fieldSql <- paste0("\n\t\t\t", |
| 89 | + field," ", |
| 90 | + subset(fields, cdmFieldName == fieldName, cdmDatatype), |
| 91 | + nullable_sql, |
| 92 | + closing_sql) |
| 93 | + sql_result <- c(sql_result, fieldSql) |
| 94 | + } |
| 95 | + sql_result <- c(sql_result, "") |
| 96 | + } |
| 97 | + return(paste0(sql_result, collapse = "")) |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +#' @describeIn createDdl createPrimaryKeys Returns a string containing the OHDSQL for creation of primary keys in the OMOP CDM. |
| 102 | +#' @return A string containing the OHDSQL for creation of primary keys in the OMOP CDM. |
| 103 | +#' @export |
| 104 | +createPrimaryKeys <- function(cdmVersion){ |
| 105 | + # prevent check NOTE from occurring due to non-standard evaluation of variables |
| 106 | + isPrimaryKey <- cdmFieldName <- NULL |
| 107 | + |
| 108 | + # argument checks |
| 109 | + stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions()) |
| 110 | + |
| 111 | + # cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE) |
| 112 | + cdmFieldCsvLoc <- "./inst/csv/Gaia_Field_Level.csv" |
| 113 | + cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE) |
| 114 | + |
| 115 | + primaryKeys <- subset(cdmSpecs, isPrimaryKey == "Yes") |
| 116 | + pkFields <- primaryKeys$cdmFieldName |
| 117 | + |
| 118 | + sql_result <- c(paste0("--@targetDialect CDM Primary Key Constraints for OMOP Common Data Model ", cdmVersion, "\n")) |
| 119 | + for (pkField in pkFields){ |
| 120 | + |
| 121 | + subquery <- subset(primaryKeys, cdmFieldName==pkField) |
| 122 | + |
| 123 | + sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT xpk_", subquery$cdmTableName, " PRIMARY KEY NONCLUSTERED (", subquery$cdmFieldName , ");\n")) |
| 124 | + |
| 125 | + } |
| 126 | + return(paste0(sql_result, collapse = "")) |
| 127 | +} |
| 128 | + |
| 129 | +#' @describeIn createDdl createForeignKeys Returns a string containing the OHDSQL for creation of foreign keys in the OMOP CDM. |
| 130 | +#' @return A string containing the OHDSQL for creation of foreign keys in the OMOP CDM. |
| 131 | +#' @export |
| 132 | +createForeignKeys <- function(cdmVersion){ |
| 133 | + # prevent check NOTE from occurring due to non-standard evaluation of variables |
| 134 | + isForeignKey <- NULL |
| 135 | + |
| 136 | + # argument checks |
| 137 | + stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions()) |
| 138 | + |
| 139 | + # cdmFieldCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Field_Level.csv")), package = "CommonDataModel", mustWork = TRUE) |
| 140 | + cdmFieldCsvLoc <- "./inst/csv/Gaia_Field_Level.csv" |
| 141 | + cdmSpecs <- read.csv(cdmFieldCsvLoc, stringsAsFactors = FALSE) |
| 142 | + |
| 143 | + foreignKeys <- subset(cdmSpecs, isForeignKey == "Yes") |
| 144 | + foreignKeys$key <- paste0(foreignKeys$cdmTableName, "_", foreignKeys$cdmFieldName) |
| 145 | + |
| 146 | + sql_result <- c(paste0("--@targetDialect CDM Foreign Key Constraints for OMOP Common Data Model ", cdmVersion, "\n")) |
| 147 | + for (foreignKey in foreignKeys$key){ |
| 148 | + |
| 149 | + subquery <- subset(foreignKeys, foreignKeys$key==foreignKey) |
| 150 | + |
| 151 | + sql_result <- c(sql_result, paste0("\nALTER TABLE @cdmDatabaseSchema.", subquery$cdmTableName, " ADD CONSTRAINT fpk_", subquery$cdmTableName, "_", subquery$cdmFieldName, " FOREIGN KEY (", subquery$cdmFieldName , ") REFERENCES @cdmDatabaseSchema.", subquery$fkTableName, " (", subquery$fkFieldName, ");\n")) |
| 152 | + |
| 153 | + } |
| 154 | + return(paste0(sql_result, collapse = "")) |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +# A helper function that will return a character string with the omop ascii art given a major and minor cdm version |
| 160 | +# example: cat(createAsciiHeader(5, 3)) |
| 161 | +createAsciiHeader <- function(major, minor) { |
| 162 | + |
| 163 | + stopifnot(is.numeric(major), is.numeric(minor), length(major) == 1, length(minor) == 1) |
| 164 | + stopifnot(major %in% 0:99, minor %in% 0:99) |
| 165 | + |
| 166 | + # An inner function that returns an ascii art matrix for any number between 0 and 99 |
| 167 | + numberMatrix <- function(num){ |
| 168 | + stopifnot(is.numeric(num), num %in% 0:99) |
| 169 | + |
| 170 | + # An inner function that returns a 7x7 matrix of number ascii art for the number 0 through 9 |
| 171 | + # for the number 1 a 7x5 matrix is returned because 1 is narrower than other numbers. |
| 172 | + singleDigit <- function(num) { |
| 173 | + nums <- c(' ### # ##### ##### # ####### ##### ####### ##### ##### # # ## # ## ## # # # ## # # ## ## # # # # ## # # # # # ## ## # # ##### ##### # # ###### ###### # ##### ####### # # # ######## ## # # # # # # # # # # # # # ## # # # ## # ### ##### ####### ##### # ##### ##### # ##### ##### ') |
| 174 | + numsMatrix <- matrix(data = strsplit(nums, character(0))[[1]], nrow = 7, byrow = T) |
| 175 | + cols <- seq(num*7+1, num*7+7, by = 1) |
| 176 | + out <- numsMatrix[1:7, cols] |
| 177 | + # the number 1 is narrower than the other numbers |
| 178 | + if(num == 1) out<- out[1:7, 2:6] |
| 179 | + out |
| 180 | + } |
| 181 | + |
| 182 | + if(num < 10){ |
| 183 | + return(singleDigit(num)) |
| 184 | + } else { |
| 185 | + space <- matrix(rep(" ", 7), nrow = 7) |
| 186 | + return(cbind(singleDigit(floor(num/10)), space, singleDigit(num %% 10))) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + omop <- c('. |
| 191 | + ####### # # ####### ###### ##### ###### # # . |
| 192 | + # # ## ## # # # # # # # # ## ## # #. |
| 193 | + # # # # # # # # # # # # # # # # # # #. |
| 194 | + # # # # # # # ###### # # # # # # # #. |
| 195 | + # # # # # # # # # # # # # #. |
| 196 | + # # # # # # # # # # # # # # # . |
| 197 | + ####### # # ####### # ##### ###### # # ## ') |
| 198 | + |
| 199 | + # convert to matrix and remove first column |
| 200 | + omop <- matrix(strsplit(omop, character(0))[[1]], nrow = 7, byrow = TRUE) |
| 201 | + omop <- omop[,c(-1, -2)] |
| 202 | + |
| 203 | + dot <- matrix(c(rep(" ", 3*4), rep("#", 3*3)), nrow = 7, byrow = TRUE) |
| 204 | + space <- matrix(rep(" ", 7), nrow = 7) |
| 205 | + newline <- matrix(rep("\n", 7, nrow = 7)) |
| 206 | + |
| 207 | + |
| 208 | + header <- character(0) |
| 209 | + headerMatrix <- cbind(omop, space, numberMatrix(major), space, dot, space, numberMatrix(minor), newline) |
| 210 | + for(i in 1:7) { |
| 211 | + header <- c(header, as.character(headerMatrix[i,])) |
| 212 | + } |
| 213 | + header <- paste(header, collapse = "") |
| 214 | + return(header) |
| 215 | +} |
0 commit comments