Skip to content

Instantly share code, notes, and snippets.

@allaway
Last active November 20, 2024 17:39
Show Gist options
  • Save allaway/71bb73ff629f3e0f9498f5c867623a0b to your computer and use it in GitHub Desktop.
Save allaway/71bb73ff629f3e0f9498f5c867623a0b to your computer and use it in GitHub Desktop.
Get drug prediction values for a disease from TxGNN.org server
library(httr2)
library(dplyr)
library(purrr)
library(tibble)
library(dplyr)
##use this table to find your bert_grouped_disease_id
get_disease_map <- function(url = "http://txgnn.org/txgnn_data_v2/node_name_dict.json"){
id_map <- jsonlite::read_json(url)
dis_map <- id_map$disease %>%
map_df(~ as_tibble(.x), .id = "id") %>%
rename(disease_name = value)
return(dis_map)
}
get_drug_map <- function(url = "http://txgnn.org/txgnn_data_v2/node_name_dict.json"){
id_map <- jsonlite::read_json(url)
drug_map <- id_map$drug %>%
map_df(~ as_tibble(.x), .id = "id") %>%
rename(drug_name = value)
return(drug_map)
}
get_disease_id <- function(disease, disease_map = NULL){
if(is.null(disease_map)){
disease_map <- get_disease_map()
}
disease_id <- disease_map |>
filter(stringr::str_detect(disease_name, stringr::coll(disease))) |>
pull(id) |>
unique()
if(length(disease_id) == 0){
stop("Disease id not found")
} else if(length(disease_id) > 1){
stop("Multiple disease ids found")
} else {
return(disease_id)
}
}
get_txgnn_drug_predictions <- function(bert_grouped_disease_id, drug_map){
url <- glue::glue("http://txgnn.org/api/drug_predictions")
resps <- request(url) |>
req_url_query(disease_id = bert_grouped_disease_id) |>
req_perform()
if(resps$status_code == 200){
content <- resp_body_json(resps)
df <- content |>
map_dfr(~ as_tibble(.x)) %>%
left_join(drug_map)
} else {
stop("Error in API call")
}
}
dm <- get_drug_map()
dism <- get_disease_map()
#neurofibromatosis
nfid <- get_disease_id("neurofibromatosis", dism)
nf <- get_txgnn_drug_predictions(nfid, dm)
write_csv(nf, './data/txgnn_nf.csv')
#malignant peripheral nerve sheath tumor
mpnstid <- get_disease_id("malignant peripheral nerve sheath tumor", dism)
mpnst <- get_txgnn_drug_predictions(mpnstid, dm)
write_csv(mpnst, './data/txgnn_mpnst.csv')
#plexiform neurofibroma (disease)
pnfid <- get_disease_id("plexiform neurofibroma (disease)", dism)
pnf <- get_txgnn_drug_predictions(pnfid, dm) %>%
filter(score != 0)
write_csv(pnf, './data/txgnn_pnf.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment