forked from UW-GAC/anvil-util-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_model_report.R
43 lines (34 loc) · 1.54 KB
/
data_model_report.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
library(argparser)
library(AnvilDataModels)
argp <- arg_parser("report")
argp <- add_argument(argp, "--table_files", help="2-column tsv file with (table name, table tsv file)")
argp <- add_argument(argp, "--model_file", help="json file with data model")
argp <- add_argument(argp, "--out_prefix", help="output prefix")
argv <- parse_args(argp)
# argv <- list(table_files="testdata/table_files.tsv",
# model_file="testdata/data_model.json",
# out_prefix="test")
# read data model
model <- json_to_dm(argv$model_file)
# read tables
table_files <- readr::read_tsv(argv$table_files, col_names=c("names", "files"), col_types = readr::cols())
# check if we need to add any columns to files
if (length(attr(model, "auto_id")) > 0) {
tables <- read_data_tables(table_files$files, table_names=table_files$names)
# add auto columns
tables2 <- lapply(names(tables), function(t) {
add_auto_columns(tables[[t]], table_name=t, model=model)
})
names(tables2) <- names(tables)
# write new tables
new_files <- paste(argv$out_prefix, names(tables), "table.tsv", sep="_")
names(new_files) <- names(tables)
for (t in names(tables2)) {
readr::write_tsv(tables2[[t]], new_files[t])
}
params <- list(tables=new_files, model=argv$model_file)
} else {
params <- list(tables=setNames(table_files$files, table_files$names), model=argv$model_file)
}
pass <- custom_render_markdown("data_model_report", argv$out_prefix, parameters=params)
writeLines(tolower(as.character(pass)), "pass.txt")