|
| 1 | +#' @export |
| 2 | +format_edge_type <- function(edge_types) { |
| 3 | + et <- dplyr::bind_rows(lapply(edge_types, function(x) data.frame(value = x[[2]], schema_name = x[[1]]))) |
| 4 | + components <- et |> |
| 5 | + dplyr::filter(tolower(value) == "component") |> |
| 6 | + dplyr::pull(schema_name) |
| 7 | + et |> |
| 8 | + dplyr::filter(value %in% c("Component", "Filename")) |> |
| 9 | + dplyr::group_by(schema_name) |> |
| 10 | + dplyr::summarise(file_based = "Filename" %in% value) |> |
| 11 | + dplyr::filter(schema_name %in% components) |
| 12 | +} |
| 13 | + |
| 14 | +#' @export |
| 15 | +get_display_names <- function(qlist) { |
| 16 | + if (!"schema_url" %in% names(qlist)) stop("qlist needs element named `schema_url`") |
| 17 | + if (!"node_list" %in% names(qlist)) stop("qlist needs at least one element named `node_list`") |
| 18 | + httr::GET( |
| 19 | + url = "https://schematic-dev.api.sagebionetworks.org/v1/schemas/get_nodes_display_names", |
| 20 | + query = qlist |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +#' @export |
| 25 | +create_template_config <- function(data_model, include_schemas = NULL, exclude_schemas = NULL) { |
| 26 | + if (!is.null(include_schemas) && !is.null(exclude_schemas)) stop("include_schemas and exclude_schemas cannot both have values") |
| 27 | + edges <- graph_by_edge_type(schema_url = data_model) |
| 28 | + schema_names <- format_edge_type(edges) |
| 29 | + nl <- setNames(as.list(schema_names$schema_name), rep("node_list", length(schema_names$schema_name))) |
| 30 | + dnames <- get_display_names(c(schema_url = data_model, nl)) |> httr::content() |
| 31 | + config <- data.frame(display_name = unlist(dnames), schema_name = unlist(nl)) |> |
| 32 | + dplyr::left_join(schema_names, by = "schema_name") |> |
| 33 | + dplyr::mutate(type = ifelse(file_based, "file", "record")) |> |
| 34 | + dplyr::select(-file_based) |
| 35 | + if (!is.null(include_schemas)) { |
| 36 | + if (any(length(x <- setdiff(include_schemas, config$schema_name)))) stop(sprintf("%s is not a schema name in the data model", x)) |
| 37 | + config <- dplyr::filter(config, schema_name %in% include_schemas) |
| 38 | + } |
| 39 | + if (!is.null(exclude_schemas)) { |
| 40 | + if (any(length(y <- setdiff(exclude_schemas, config$schema_name)))) stop(sprintf("%s is not a schema name in the data model", y)) |
| 41 | + config <- dplyr::filter(config, !schema_name %in% exclude_schemas) |
| 42 | + } |
| 43 | + config |
| 44 | +} |
| 45 | + |
| 46 | +#' @export |
| 47 | +create_dca_template_config <- function(data_model, include_schemas = NULL, exclude_schemas = NULL) { |
| 48 | + df <- create_template_config(data_model, include_schemas, exclude_schemas) |
| 49 | + schematic_version <- httr::GET("https://schematic-dev.api.sagebionetworks.org/v1/version") |> |
| 50 | + httr::content() |
| 51 | + list( |
| 52 | + manifest_schemas = df, |
| 53 | + service_version = schematic_version, |
| 54 | + schema_version = "" |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +#' @export |
| 59 | +#' @description Create a DCA-specific template generation function |
| 60 | +write_dca_template_config <- function(data_model, file, include_schemas = NULL, exclude_schemas = NULL) { |
| 61 | + df <- create_dca_template_config(data_model, include_schemas, exclude_schemas) |
| 62 | + jsonlite::write_json(df, file, pretty = TRUE, auto_unbox = TRUE) |
| 63 | +} |
0 commit comments