user007
user007

Reputation: 347

How to use NIH API inside R

I am trying to get some data from a project using API. In this case I need to set NIH API to get information. I need to use findDrugInteractions rest point but I do not have idea on how to set the extraction. I only know that I need httr to get the data and jsonlite to format it. This is the link of the API:

https://lhncbc.nlm.nih.gov/RxNav/APIs/api-Interaction.findDrugInteractions.html

In the help options there is an example:

https://rxnav.nlm.nih.gov/REST/interaction/interaction.json?rxcui=88014&sources=ONCHigh

This API uses rxcui (a code) to get the data. In the case of previous example is 88014. But I do not know how to replicate this example using R.

Can anybody please help me? Many thanks!

Update

With the answer provided I have obtained the data but I am not sure how I can process the long json object. I tried using rrapply but it returns a very large dataframe. Is there any way I can format the json objet properly to have columns that identify interactions?

library(httr)
library(rrapply)
#Code
#Code
v1 <- httr::GET("https://rxnav.nlm.nih.gov/REST/interaction/interaction.json",
                query=list(rxcui=88014))
#Format
cont <- content(v1, as = "parsed", type = "application/json")
#explicit convertion to data frame
o1 <- rrapply(cont$interactionTypeGroup, f = function(x) replace(x, is.null(x), NA), how = 'bind')

Upvotes: 1

Views: 91

Answers (2)

Joris C.
Joris C.

Reputation: 6234

The option how = "bind" in rrapply() returns a very wide data.frame because the list contains different levels of nesting (and it is unnesting the list at the level of the least nested leafs).

Instead, we can manually set the list level at which to unnest with the coldepth option. This allows us to bind all repeated interactionPair sublists into a wide data.frame:

library(httr)
library(rrapply)

v1 <- httr::GET("https://rxnav.nlm.nih.gov/REST/interaction/interaction.json",
                query=list(rxcui=88014))
cont <- content(v1, as = "parsed", type = "application/json")

## interactionPairs only
o1 <- rrapply(
  cont$interactionTypeGroup, 
  how = "bind", 
  options = list(coldepth = 6, namecols = TRUE)
)

head(o1)
#>   L1              L2 L3              L4 L5 interactionConcept.1.minConceptItem.rxcui interactionConcept.1.minConceptItem.name interactionConcept.1.minConceptItem.tty interactionConcept.1.sourceConceptItem.id interactionConcept.1.sourceConceptItem.name         interactionConcept.1.sourceConceptItem.url interactionConcept.2.minConceptItem.rxcui interactionConcept.2.minConceptItem.name interactionConcept.2.minConceptItem.tty interactionConcept.2.sourceConceptItem.id interactionConcept.2.sourceConceptItem.name         interactionConcept.2.sourceConceptItem.url severity                                                                                                       description
#> 1  1 interactionType  1 interactionPair  1                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                      1001                               antipyrine                                      IN                                   DB01435                                  Antipyrine https://go.drugbank.com/drugs/DB01435#interactions      N/A               The risk or severity of hypertension can be increased when Rizatriptan is combined with Antipyrine.
#> 2  1 interactionType  1 interactionPair  2                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                    221062                  antithrombin III, human                                     PIN                                   DB11598                      Antithrombin III human https://go.drugbank.com/drugs/DB11598#interactions      N/A Rizatriptan may decrease the excretion rate of Antithrombin III human which could result in a higher serum level.
#> 3  1 interactionType  1 interactionPair  3                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10109                             streptomycin                                      IN                                   DB01082                                Streptomycin https://go.drugbank.com/drugs/DB01082#interactions      N/A           Rizatriptan may decrease the excretion rate of Streptomycin which could result in a higher serum level.
#> 4  1 interactionType  1 interactionPair  4                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10154                          succinylcholine                                      IN                                   DB00202                             Succinylcholine https://go.drugbank.com/drugs/DB00202#interactions      N/A       The risk or severity of adverse effects can be increased when Succinylcholine is combined with Rizatriptan.
#> 5  1 interactionType  1 interactionPair  5                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10156                               sucralfate                                      IN                                   DB00364                                  Sucralfate https://go.drugbank.com/drugs/DB00364#interactions      N/A             Sucralfate may decrease the excretion rate of Rizatriptan which could result in a higher serum level.
#> 6  1 interactionType  1 interactionPair  6                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10167                                sulbactam                                      IN                                   DB09324                                   Sulbactam https://go.drugbank.com/drugs/DB09324#interactions      N/A              Rizatriptan may decrease the excretion rate of Sulbactam which could result in a higher serum level.

The remaining metadata (not listed under the interactionPair nodes) can be parsed into a wide data.frame in a similar fashion:

## complement of interactionPairs
o2 <- rrapply(
  cont$interactionTypeGroup, 
  condition = \(x, .xparents) !"interactionPair" %in% .xparents, 
  how = "bind", 
  options = list(coldepth = 4, namecols = TRUE)
)

head(o2)
#>   L1              L2 L3                                                comment minConceptItem.rxcui minConceptItem.name minConceptItem.tty
#> 1  1 interactionType  1 rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 2  2 interactionType  1 rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN

If needed, we can still merge everything together to have all data contained in a single wide data.frame:

## all data 
o <- merge(o1, o2, by = c("L1", "L2", "L3"))

head(o)
#>   L1              L2 L3              L4 L5 interactionConcept.1.minConceptItem.rxcui interactionConcept.1.minConceptItem.name interactionConcept.1.minConceptItem.tty interactionConcept.1.sourceConceptItem.id interactionConcept.1.sourceConceptItem.name         interactionConcept.1.sourceConceptItem.url interactionConcept.2.minConceptItem.rxcui interactionConcept.2.minConceptItem.name interactionConcept.2.minConceptItem.tty interactionConcept.2.sourceConceptItem.id interactionConcept.2.sourceConceptItem.name         interactionConcept.2.sourceConceptItem.url severity                                                                                                       description                                                comment minConceptItem.rxcui minConceptItem.name minConceptItem.tty
#> 1  1 interactionType  1 interactionPair  1                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                      1001                               antipyrine                                      IN                                   DB01435                                  Antipyrine https://go.drugbank.com/drugs/DB01435#interactions      N/A               The risk or severity of hypertension can be increased when Rizatriptan is combined with Antipyrine. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 2  1 interactionType  1 interactionPair  2                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                    221062                  antithrombin III, human                                     PIN                                   DB11598                      Antithrombin III human https://go.drugbank.com/drugs/DB11598#interactions      N/A Rizatriptan may decrease the excretion rate of Antithrombin III human which could result in a higher serum level. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 3  1 interactionType  1 interactionPair  3                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10109                             streptomycin                                      IN                                   DB01082                                Streptomycin https://go.drugbank.com/drugs/DB01082#interactions      N/A           Rizatriptan may decrease the excretion rate of Streptomycin which could result in a higher serum level. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 4  1 interactionType  1 interactionPair  4                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10154                          succinylcholine                                      IN                                   DB00202                             Succinylcholine https://go.drugbank.com/drugs/DB00202#interactions      N/A       The risk or severity of adverse effects can be increased when Succinylcholine is combined with Rizatriptan. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 5  1 interactionType  1 interactionPair  5                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10156                               sucralfate                                      IN                                   DB00364                                  Sucralfate https://go.drugbank.com/drugs/DB00364#interactions      N/A             Sucralfate may decrease the excretion rate of Rizatriptan which could result in a higher serum level. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN
#> 6  1 interactionType  1 interactionPair  6                                     88014                              rizatriptan                                      IN                                   DB00953                                 Rizatriptan https://go.drugbank.com/drugs/DB00953#interactions                                     10167                                sulbactam                                      IN                                   DB09324                                   Sulbactam https://go.drugbank.com/drugs/DB09324#interactions      N/A              Rizatriptan may decrease the excretion rate of Sulbactam which could result in a higher serum level. rizatriptan (88014) is resolved to rizatriptan (88014)                88014         rizatriptan                 IN

Upvotes: 2

MrFlick
MrFlick

Reputation: 206242

You can use httr to create your GET request

httr::GET("https://rxnav.nlm.nih.gov/REST/interaction/interaction.json",
          query=list(rxcui=88014)) |> httr::content()

This will parse the data as a named list.

Upvotes: 2

Related Questions