Last active
November 9, 2023 21:03
-
-
Save allaway/6f401a1e81a982950365c9733caaaf57 to your computer and use it in GitHub Desktop.
submit manifests in a google drive folder to validation by manifest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googlesheets4) | |
library(nfportalutils) | |
submit_gs_to_api <- function(sheet_id, | |
schema_url) { | |
# Retrieve the Google Sheets document | |
sheet <- googlesheets4::gs4_get(sheet_id) | |
# Extract the workbook name and remove '.manifest' and split it on the underscore ("_") | |
workbook_name <- sheet$name | |
workbook_name <- sub("\\.manifest", "", workbook_name) | |
component <- strsplit(workbook_name, "_")[[1]][2] | |
# Read the data from the Google Sheets document | |
data <- googlesheets4::read_sheet(sheet_id, col_types = "c") | |
# Convert the data to a CSV file | |
temp_file <- tempfile(fileext = ".csv") | |
readr::write_csv(data, temp_file) | |
# POST manifest to schematic API with nfportalutils helper function | |
content <- nfportalutils::manifest_validate( | |
data_type = component, | |
file_name = temp_file, | |
schema_url = schema_url | |
) | |
# sleep to prevent hitting google API rate limits. | |
Sys.sleep(5) | |
return(content) | |
} | |
#' Terse error messages please | |
#' | |
#' @param error An error object from schematic. | |
#' @keywords internal | |
tersely <- function(error) { | |
row <- error[[1]] | |
column <- error[[2]] | |
message <- error[[3]] | |
value <- error[[4]] | |
wording <- if(grepl("is not one of", message)) paste(shQuote(value), "is not a valid value for", column) else message | |
wording | |
} | |
manifest_passed <- function(result) { | |
errors <- length(result$errors) | |
if(errors) { | |
messages <- unique(sapply(result$errors, tersely)) | |
notes <- paste(messages, collapse = ", ") | |
return(list(result = FALSE, notes = notes)) | |
} else { | |
return(list(result = TRUE, notes = "No errors")) | |
} | |
} | |
bar <- googledrive::drive_ls( | |
"https://drive.google.com/drive/u/1/folders/1yblPMk-kgMj5KJi7P0AFOobZ9vkl6Nkk", | |
type = 'spreadsheet', | |
recursive = T) | |
validation_responses <- lapply(bar$id, submit_gs_to_api, schema_url = "https://raw.githubusercontent.com/nf-osi/nf-research-tools-schema/update_schema/nf-research-tools.jsonld") | |
sapply(validation_responses, manifest_passed) |
Updated to use nf-osi/nfportalutils functions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some light documentation here https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/3045621809/Validating+multiple+google+sheets+manifests+against+different+components