-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsheets_to_JSON_gsr.R
98 lines (77 loc) · 2.9 KB
/
sheets_to_JSON_gsr.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
library(googlesheets4)
library(dplyr)
library(tidyr)
library(stringr)
library(jsonlite)
# link to the data
url <- "https://docs.google.com/spreadsheets/d/1xfSQqRQIq6pGkJ5jzzv2QhetmX5boaEZoNECpDwXe5I"
model_name <- "PRIMED GSR Data Model"
model_description <- "Data model for Genomic Summary Results in the PRIMED consortium"
model_version <- "2.0"
# table metadata
meta <- read_sheet(url, sheet="Tables", skip=1, col_types="c") %>%
select(table=Table, required=Required)
table_names <- meta$table
tables <- lapply(table_names, function(x) read_sheet(url, sheet=x, skip=1, col_types="c"))
names(tables) <- table_names
# rename and reorder columns
for (i in 1:length(tables)) {
tmp <- tables[[i]] %>%
filter(!is.na(`Data type`)) %>% # keep only valid rows
mutate(primary_key = ifelse(paste0(names(tables)[i], "_id") == Column, TRUE, NA)) %>%
mutate(is_bucket_path = ifelse(Column == "file_path", TRUE, NA)) %>%
mutate(Description=gsub('"', "'", Description), # replace double with single quote
Description=gsub('\n', ' ', Description), # replace newline with space
`Notes/comments`=gsub('"', "'", `Notes/comments`), # replace double with single quote
`Notes/comments`=gsub('\n', ' ', `Notes/comments`), # replace newline with space
References=ifelse(grepl("omop_concept", References), NA, References)) # remove external table reference
lookup <- c(
data_type = "Data type",
multi_value_delimiter = "Multi-value delimiter",
notes = "Notes/comments"
)
tmp <- tmp %>%
rename(any_of(lookup)) %>%
rename_with(tolower)
keep_cols <- c(
"column",
"primary_key",
"required",
"description",
"data_type",
"references",
"enumerations",
"is_bucket_path",
"multi_value_delimiter",
"examples",
"notes"
)
tables[[i]] <- tmp %>%
select(any_of(keep_cols))
}
# call in the sheets_to_list function that accepts two arguments:
# 1) the list describing which tables are in the Google Sheets file
# 2) the list of data tables corresponding to the first argument
source("sheets_to_list.R")
tab_list <- sheets_to_list(apply(meta, 1, as.list), tables)
# initialize leading text
master <- list(
# Overall File Details
name = model_name,
description = model_description,
version = model_version,
# Data Table Details
tables = tab_list
)
# compile master file in JSON format
out <- toJSON(x = master,
pretty = TRUE,
auto_unbox = TRUE,
unbox = TRUE)
# unquote the logical parameters TRUE and FALSE
out <- gsub(pattern = ': \"TRUE\"', replacement = ': true', x = out)
out <- gsub(pattern = ': \"FALSE\"', replacement = ': false', x = out)
# view the final version
out
# save the final version
write(out, "PRIMED_GSR_data_model.json")