andrewallenbruce / provider

Public Healthcare Provider APIs :stethoscope:
https://andrewallenbruce.github.io/provider/
Other
18 stars 2 forks source link

Open Payments Helpers: NDC Information Retrieval #19

Closed andrewallenbruce closed 11 months ago

andrewallenbruce commented 11 months ago

Possible helper functions to augment National Drug Code information from open_payments() results:

# Example NDC codes
ndcs <- c(
  "0002-1433-80", "0173-0869-06", "0003-0894-31", 
  "50242-828-02", "61874-115-31", "0023-6501-10", 
  "0169-4132-12", "0003-0893-21", "12496-0300-1", 
  "64597-301-60", "0310-1730-30", "0310-6205-30", 
  "0310-4616-12", "65649-303-02", "72618-3000-2", 
  "66220-729-30", "12496-0100-1", "65757-300-01", 
  "59467-679-01", "0002-1436-11", "0002-1506-80", 
  "78206-145-01", "55513-112-01", "59148-018-71", 
  "59148-037-13", "55513-100-01", "50419-540-01")

# MedlinePlus
medline <- function(ndc) {
  http <- "https://connect.medlineplus.gov/service?"
  code <- paste0("mainSearchCriteria.v.c=", ndc)
  sys  <- "mainSearchCriteria.v.cs=2.16.840.1.113883.6.69"
  fmt  <- "knowledgeResponseType=application/json"
  url  <- paste0(http, code, "&", sys, "&", fmt)
  resp <- httr2::request(url) |> httr2::req_perform()
  res  <- httr2::resp_body_json(resp, 
                                check_type = FALSE, 
                                simplifyVector = TRUE)

  if (isTRUE(vctrs::vec_is_empty(res$feed$entry))) {
    cli_args <- dplyr::tribble(
      ~x,                  ~y,
      "NDC",               ndc) |>
      tidyr::unnest(cols = c(y))

    provider:::format_cli(cli_args)
    return(invisible(NULL))
  }

  results <- res$feed$entry |>
    tidyr::unnest(c(title, summary), names_sep = ".") |>
    dplyr::select(subject = title._value, 
                  summary = summary._value) |>
    dplyr::mutate(ndc = ndc, .before = 1) |> 
    dplyr::mutate(summary = stringr::str_squish(summary))

  return(results)
}

# Retrieve NDC data from MedlinePlus
medline_results <- ndcs |> 
  purrr::map(\(x) medline(ndc = x)) |> 
  purrr::list_rbind()
#> ✖ No results for NDC = 78206-145-01

#> # A tibble: 36 × 3
#>    ndc          subject                                     summary             
#>    <chr>        <chr>                                       <chr>               
#>  1 0002-1433-80 Dulaglutide Injection                       "Dulaglutide inject…
#>  2 0002-1433-80 Diabetes Medicines                          "<h3>What is diabet…
#>  3 0173-0869-06 Umeclidinium and Vilanterol Oral Inhalation "The combination of…
#>  4 0003-0894-31 Apixaban                                    "Apixaban is used h…
#>  5 0003-0894-31 Blood Thinners                              "<h3>What are blood…
#>  6 50242-828-02 Baloxavir Marboxil                          "Baloxavir marboxil…
#>  7 61874-115-31 Cariprazine                                 "Cariprazine is use…
#>  8 0023-6501-10 Ubrogepant                                  "Ubrogepant is used…
#>  9 0023-6501-10 Pain Relievers                              "<p>Pain relievers …
#> 10 0169-4132-12 Semaglutide Injection                       "Semaglutide inject…
#> # ℹ 26 more rows

# Retrieve NDC data from RxNorm
rxnorm <- function(ndc) {
  rxcui      <- rxnorm::from_ndc(ndc)
  ndc_status <- rxnorm::get_ndc_status(ndc)
  brand_name <- rxnorm::get_bn(rxcui)
  atc        <- rxnorm::get_atc(rxcui)
  atc_first  <- rxnorm::get_atc(rxcui, "first")
  atc_second <- rxnorm::get_atc(rxcui, "second")
  atc_third  <- rxnorm::get_atc(rxcui, "third")
  atc_fourth <- rxnorm::get_atc(rxcui, "fourth")

  dplyr::tibble(ndc        = ndc,
                status     = ndc_status,
                rxcui      = rxcui,
                brand_name = brand_name,
                atc        = atc,
                atc_first  = atc_first,
                atc_second = atc_second,
                atc_third  = atc_third,
                atc_fourth = atc_fourth)
}

rxnorm("0002-1433-80")
#> # A tibble: 1 × 9
#>   ndc    status rxcui brand_name atc   atc_first atc_second atc_third atc_fourth
#>   <chr>  <chr>  <chr> <chr>      <chr> <chr>     <chr>      <chr>     <chr>     
#> 1 0002-… ACTIVE 1551… TRULICITY  A10BJ alimenta… drugs use… blood gl… glucagon-…

rxnorm_results <- ndcs |> 
  purrr::map(\(x) rxnorm(ndc = x)) |> 
  purrr::list_rbind()

#> # A tibble: 27 × 9
#>    ndc   status rxcui brand_name atc   atc_first atc_second atc_third atc_fourth
#>    <chr> <chr>  <chr> <chr>      <chr> <chr>     <chr>      <chr>     <chr>     
#>  1 0002… ACTIVE 1551… TRULICITY  A10BJ alimenta… drugs use… blood gl… glucagon-…
#>  2 0173… ACTIVE 1487… ANORO      R03AL respirat… drugs for… adrenerg… adrenergi…
#>  3 0003… ACTIVE 1364… ELIQUIS    B01AF blood an… antithrom… antithro… direct fa…
#>  4 5024… ACTIVE 2100… XOFLUZA 4… J05AX antiinfe… antiviral… direct a… other ant…
#>  5 6187… ACTIVE 1667… VRAYLAR    N05AX nervous … psycholep… antipsyc… other ant…
#>  6 0023… ACTIVE 2268… UBRELVY    N02CD nervous … analgesics antimigr… calcitoni…
#>  7 0169… ACTIVE 1991… OZEMPIC    A10BJ alimenta… drugs use… blood gl… glucagon-…
#>  8 0003… ACTIVE 1364… ELIQUIS    B01AF blood an… antithrom… antithro… direct fa…
#>  9 1249… ACTIVE 1996… SUBLOCADE  N07BC nervous … other ner… drugs us… drugs use…
#> 10 6459… ACTIVE 1040… NUEDEXTA   N07XX nervous … other ner… other ne… other ner…
#> # ℹ 17 more rows

Created on 2023-10-12 with reprex v2.0.2

andrewallenbruce commented 11 months ago

Initial functionality implemented