json_scorefiles <- read_json (params$ log_scorefiles, simplifyVector= TRUE )
link_traits <- function (trait_efo, mapped) {
if (length (trait_efo) == 0 ) {
return ("" )
} else {
return (purrr:: map2_chr (trait_efo, mapped, ~ stringr:: str_glue ('<a href="http://www.ebi.ac.uk/efo/{.x}">{.y}</a>' )))
}
}
extract_traits <- function (x) {
trait_efo <- purrr:: map (json_scorefiles, ~ extract_chr_handle_null (.x, "trait_efo" ))
mapped <- purrr:: map (json_scorefiles, ~ extract_chr_handle_null (.x, "trait_mapped" ))
trait_display <- purrr:: map2 (trait_efo, mapped, link_traits)
mapped_trait_links <- purrr:: map_chr (trait_display, ~ paste (.x, collapse = "<br />" ))
reported_traits <- purrr:: map (json_scorefiles, ~ extract_chr_handle_null (.x, "trait_reported" ))
purrr:: map2 (reported_traits, mapped_trait_links, ~ {
stringr:: str_glue ("<u>Reported trait:</u> {.x} <br /> <u>Mapped trait(s):</u> {.y}" )
})
}
extract_chr_handle_null <- function (x, field) {
return (replace (x[[field]], is.null (x[[field]]), "" ))
}
link_pgscatalog <- function (id, link_type) {
if (id != "" ) {
return (stringr:: str_glue ('<a href="https://www.pgscatalog.org/{link_type}/{id}">{id}</a>' ))
} else {
return (id)
}
}
add_note <- function (id, note) {
if (id != "" ) {
return (stringr:: str_glue ("{id} <br /> <small>{note}</small>" ))
} else {
return (id)
}
}
annotate_genome_build <- function (original_build, harmonised_build) {
return (stringr:: str_glue ("<u>Original build:</u> {original_build} <br /> <u>Harmonised build:</u> {harmonised_build}" ))
}
tibble:: tibble (json = json_scorefiles) %>%
# extract fields from json list
mutate (pgs_id = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "pgs_id" )),
pgs_name = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "pgs_name" )),
pgp_id = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "pgp_id" )),
citation = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "citation" )),
# trait_efo = purrr::map_chr(json, ~ extract_chr_handle_null(.x, "trait_efo")),
# trait_reported = purrr::map_chr(json, ~ extract_chr_handle_null(.x, "trait_reported")),
# trait_mapped = purrr::map_chr(json, ~ extract_chr_handle_null(.x, "trait_mapped")),
trait_display = extract_traits (.),
genome_build = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "genome_build" )),
harmonised_build = purrr:: map_chr (json, ~ extract_chr_handle_null (.x, "HmPOS_build" )),
n_variants = purrr:: map_chr (json, ~ .x$ variants_number),
accession = stringr:: str_replace_all (names (json), "_" , " " )
) %>%
# add links to pgs catalog identifiers
mutate (pgs_id = purrr:: map_chr (pgs_id, ~ link_pgscatalog (.x, "score" )),
pgp_id = purrr:: map_chr (pgp_id, ~ link_pgscatalog (.x, "publication" ))) %>%
# add notes
mutate (pgp_id = purrr:: map2_chr (pgp_id, citation, ~ add_note (.x, .y)),
pgs_id = purrr:: map2_chr (pgs_id, pgs_name, ~ add_note (.x, .y)),
genome_build = purrr:: map2_chr (genome_build, harmonised_build, ~ annotate_genome_build (.x, .y))) %>%
# pick columns
select (accession, pgs_id, pgp_id, trait_display, n_variants, genome_build) -> scorefile_metadata