--- title: "Case Study: Building route-specific RxCUI and NDC lists" author: "Steven Smith" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Case Study: Building route-specific RxCUI and NDC lists} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(rxref) library(dplyr) read_rxref_example <- function(file) { path <- system.file("extdata", file, package = "rxref") if (!nzchar(path)) { stop( "The example data file '", file, "' was not found. ", "You may need to reinstall rxref to restore the bundled vignette data." ) } readRDS(path) } ``` ## Case A common use case for `rxref` is to develop a list of RxNorm product concepts (RxCUIs) and National Drug Codes (NDCs) for a medication class. However, sometimes the ingredient alone is not sufficient to define the exposure. Suppose we want to identify **oral beta-blocker products** for an outpatient pharmacoepidemiologic study. Several beta-blockers are available via multiple routes of administration. For example: - metoprolol, atenolol, labetalol, propranolol, and sotalol have oral and injectable products; - esmolol is primarily used as an injectable product; and - timolol, betaxolol, and related drugs may have ophthalmic products. If we simply expand beta-blocker ingredients to all products and map them to NDCs, we would capture products outside the intended exposure definition. This case study builds an oral beta-blocker medication list in five steps: 1. define the ingredients of interest; 2. resolve ingredient names to RxNorm ingredient RxCUIs; 3. expand the ingredients to product-level RxNorm concepts; 4. inspect and filter products by route; and 5. map the final oral products to active NDCs. The displayed results use precomputed data bundled with `rxref`, so the vignette can be built without requiring access to the live RxNorm API. The code shown below is the code users can run interactively to generate the results from the current API. ## Define the ingredient list We begin with a curated list of beta-blocker ingredients. ```{r define} beta_blocker_names <- c( "acebutolol", "atenolol", "betaxolol", "bisoprolol", "carvedilol", "labetalol", "metoprolol", "nadolol", "nebivolol", "penbutolol", "pindolol", "propranolol", "sotalol", "timolol" ) beta_blocker_names ``` A curated ingredient list is often appropriate when the study definition is known in advance. `rxref` also provides RxClass helpers such as `find_classes()` and `get_class_members()` when users want to begin from a source vocabulary class rather than a prespecified list. ## Step 1: Identify ingredient RxCUIs Use `find_ingredients()` to resolve each name to an RxNorm ingredient concept. Here we retain TTY = `"IN"`, representing the base ingredient concept. ```{r resolve, eval = FALSE} bb_ingredients <- find_ingredients(beta_blocker_names) |> filter(tty == "IN") |> distinct( input, ingredient_rxcui = rxcui, ingredient_name = name, ingredient_tty = tty ) bb_ingredients ``` ```{r resolve-data, include = FALSE} bb_ingredients <- read_rxref_example("bb_ingredients.rds") ``` ```{r resolve-output, echo = FALSE} bb_ingredients ``` This is a useful checkpoint: before expanding to hundreds of product concepts, verify that each study-defined name resolved to the intended ingredient. ## Step 2: Expand ingredients to product RxCUIs Next, use `products_for_ingredients()` to identify product-level RxNorm concepts containing the selected ingredients. For this example, we use the default product-focused TTYs, retain fixed-dose combination products, and initially keep products from all routes. ```{r expand, eval = FALSE} bb_products <- products_for_ingredients( bb_ingredients$ingredient_rxcui, ttys = product_ttys("default"), include_combos = TRUE, concept_status = "active" ) |> # rejoin the ingredients list so you know what maps to what left_join(bb_ingredients, by = "ingredient_rxcui") bb_products ``` ```{r expand-data, include = FALSE} bb_products <- read_rxref_example("bb_products.rds") ``` ```{r expand-output, echo = FALSE} bb_products |> arrange(ingredient_name, tty, name) |> head(30) ``` At this stage, the product table intentionally contains products from all routes. Keeping route filtering separate for the moment makes it easier to inspect what would otherwise be included in the medication definition. Because `include_combos = TRUE`, fixed-dose combination products may also be present. We'll return to that later. ## Step 3: Inspect route and dose-form information `get_clinical_attributes()` derives clinically useful information from the product concepts, including route, dose form, dose-form group, ingredient count, and brand/generic indicators. ```{r inspect-routes, eval = FALSE} bb_attributes <- get_clinical_attributes( unique(bb_products$product_rxcui) ) |> rename(product_rxcui = rxcui) bb_attributes ``` ```{r inspect-routes-data, include = FALSE} bb_attributes <- read_rxref_example("bb_attrs.rds") ``` A quick summary shows why route filtering is needed: ```{r inspect-routes-output} bb_attributes |> count(route, dose_form_group, sort = TRUE) ``` The exact mix of routes depends on the current RxNorm concepts returned for the selected ingredients, but the important point is that an ingredient-based search can identify products that do not correspond to the intended route of exposure. It can also be useful to inspect individual products associated with non-oral routes: ```{r inspect-nonoral} bb_products |> left_join( bb_attributes |> select(product_rxcui, route, dose_form, dose_form_group), by = "product_rxcui" ) |> filter(is.na(route) | route != "ORAL") |> select( ingredient_name, product_rxcui, name, tty, route, dose_form, dose_form_group ) |> arrange(ingredient_name, route, name) |> head(30) ``` This inspection step is especially useful when working with a medication class for which route heterogeneity is clinically important. ## Step 4: Filter to oral products Once we have confirmed that the unfiltered product set contains multiple routes, use `filter_products_by_route()` to retain only oral products. ```{r filter, eval = FALSE} bb_oral_products <- bb_products |> filter_products_by_route(route = "ORAL") bb_oral_products ``` ```{r filter-data, include = FALSE} bb_oral_products <- read_rxref_example("bb_oral_products.rds") ``` ```{r filter-output, echo = FALSE} bb_oral_products |> arrange(ingredient_name, tty, name) |> head(30) ``` The returned table preserves the product-level information and appends summarized route, dose-form, and dose-form-group fields. ```{r filter-summary} bb_oral_products |> count(route, dose_form_group, sort = TRUE) ``` Route filtering depends on clinical attribute information available for a product RxCUI. A product without a route match is not retained, so route-specific medication lists should always be reviewed before they are used in an analysis. ## Step 5: Map oral product RxCUIs to active NDCs After restricting the RxCUI definition to oral products, map those products to NDCs. For a current medication list, we use current NDC associations and retain NDCs whose reported status is `"ACTIVE"`. ```{r map, eval = FALSE} bb_oral_ndc_map <- map_rxcui_to_ndc( unique(bb_oral_products$product_rxcui), history = "active", status = "ACTIVE" ) bb_oral_ndcs <- bb_oral_ndc_map |> left_join( bb_oral_products, by = c("rxcui" = "product_rxcui") ) |> rename(product_rxcui = rxcui) |> distinct() bb_oral_ndcs ``` ```{r map-data, include = FALSE} bb_oral_ndcs <- read_rxref_example("bb_oral_ndcs.rds") ``` ```{r map-output, echo = FALSE} bb_oral_ndcs |> select( ingredient_name, product_rxcui, name, tty, route, dose_form, ndc11, ndc_status ) |> arrange(ingredient_name, product_rxcui, ndc11) |> head(30) ``` At this point, we have two useful study resources: - `bb_oral_products`, containing the route-restricted RxCUI definition; and - `bb_oral_ndcs`, containing the corresponding active NDC mappings. Keeping both can be useful when the medication definition will be applied to data sources that use different drug identifiers. ## Quality-control checks Route-specific medication definitions benefit from several simple quality checks before they are finalized. ### Confirm the observed routes ```{r qc-routes} bb_oral_products |> count(route, dose_form_group, sort = TRUE) ``` For this definition, every retained product should have an oral route assignment. ### Look for non-oral terminology in product names A name-based check is not a substitute for structured route filtering, but it is a useful secondary screen. ```{r qc-nonoral-names} bb_oral_products |> filter(grepl( "Injection|Injectable|Ophthalmic|Topical|Transdermal|Nasal|Inhalation", name, ignore.case = TRUE )) |> select( ingredient_name, product_rxcui, name, tty, route, dose_form ) ``` Unexpected rows should be reviewed rather than automatically removed, because product naming conventions do not always map perfectly to route. And, again, sometimes RxNorm has mistakes in the source data that will eventually get corrected. ### Review combination products Because this example retained combination products, inspect them explicitly: ```{r qc-combinations} bb_oral_products |> filter(n_ingredients > 1) |> select( ingredient_name, product_rxcui, name, tty, n_ingredients, route, dose_form ) |> arrange(ingredient_name, name) ``` For example, beta-blocker/thiazide combination products may be appropriate for a broad antihypertensive exposure definition but inappropriate for a study specifically evaluating beta-blocker monotherapy. ### Check products without active NDCs Some valid RxNorm product concepts may not map to a current active NDC. ```{r qc-without-ndcs} bb_oral_products |> anti_join( bb_oral_ndcs |> filter(!is.na(ndc11)) |> distinct(product_rxcui), by = "product_rxcui" ) |> select( ingredient_name, product_rxcui, name, tty, route, dose_form ) |> arrange(ingredient_name, name) ``` Whether products without active NDCs matter depends on the identifiers available in the study data. ### Summarize the final definition Finally, summarize the number of unique products and NDCs associated with each ingredient: ```{r qc-counts} bb_oral_ndcs |> group_by(ingredient_name) |> summarise( n_products = n_distinct(product_rxcui), n_ndcs = n_distinct(ndc11, na.rm = TRUE), .groups = "drop" ) |> arrange(ingredient_name) ``` ## A compact alternative with `search_drug()` Once the explicit workflow is understood, `search_drug()` can combine ingredient resolution, product expansion, route filtering, and NDC mapping in a single call. Using `return = "both"` is particularly useful here because the product table and the NDC table have different grains. ```{r shortcut, eval = FALSE} bb_oral <- search_drug( beta_blocker_names, return = "both", route = "ORAL", ndc_status = "ACTIVE", include_combos = TRUE, concept_status = "active" ) bb_oral$products bb_oral$ndcs ``` ```{r shortcut-data, include = FALSE} bb_oral <- read_rxref_example("bb_oral_both_search.rds") ``` The result is a list with separate product and NDC tables: ```{r shortcut-names} names(bb_oral) ``` ```{r shortcut-products} bb_oral$products |> arrange(ingredient_rxcui, tty, name) |> head(20) ``` ```{r shortcut-ndcs} bb_oral$ndcs |> arrange(ingredient_rxcui, product_rxcui, ndc11) |> head(20) ``` The shortcut is useful when the standard workflow matches the study definition. The explicit approach remains preferable when users need to inspect route heterogeneity before filtering, modify the product set, or document study-specific inclusion and exclusion decisions. ## Adapting the workflow The example above represents one particular route-specific medication definition. Several common modifications are possible. ### Filter by route during product expansion When users already know the intended route and do not need to inspect the unfiltered product universe first, `products_for_ingredients()` can apply the route restriction directly: ```{r direct-route-filter, eval = FALSE} bb_oral_products_direct <- products_for_ingredients( bb_ingredients$ingredient_rxcui, ttys = product_ttys("default"), route = "ORAL", include_combos = TRUE, concept_status = "active" ) ``` This uses the same route-filtering infrastructure but provides a more compact workflow. ### Exclude fixed-dose combination products If the study should include beta-blocker single drug entities (i.e., not combination products) only, set `include_combos = FALSE`: ```{r exclude-combinations, eval = FALSE} bb_oral_single_products <- products_for_ingredients( bb_ingredients$ingredient_rxcui, ttys = product_ttys("default"), route = "ORAL", include_combos = FALSE, concept_status = "active" ) ``` Alternatively, retain combination products initially, review them, and apply study-specific exclusions after product expansion. ### Include historical RxNorm concepts and NDC associations For older study periods, users may need concepts that are no longer active in the current RxNorm release. Historical RxNorm product concepts can be included with: ```{r historical-products, eval = FALSE} bb_products_historical <- products_for_ingredients( bb_ingredients$ingredient_rxcui, ttys = product_ttys("default"), include_combos = TRUE, concept_status = "active_and_historical" ) bb_oral_products_historical <- bb_products_historical |> filter_products_by_route(route = "ORAL") ``` `filter_products_by_route()` can use historical RxCUI metadata as a fallback when the product table contains non-active concepts. Even so, historical route assignments deserve careful review because clinical attribute information may be less complete for older concepts. Historical NDC associations are a separate choice. Retrieve them with the `history` argument in `map_rxcui_to_ndc()`: ```{r historical-ndcs, eval = FALSE} bb_oral_ndcs_historical <- map_rxcui_to_ndc( unique(bb_oral_products_historical$product_rxcui), history = "all" ) ``` The distinction is important: - `concept_status` determines whether product expansion includes historical RxNorm concepts; - `history` determines whether RxCUI-to-NDC mapping uses current or historical NDC associations; and - `status` filters NDCs after those associations have been retrieved. `search_drug()` is convenient for current route-specific medication lists, but it does not currently expose the NDC `history` argument. For studies requiring historical NDC associations, use the explicit `products_for_ingredients()` plus `map_rxcui_to_ndc()` workflow. ## Finalizing a route-specific medication definition Before using a route-specific medication list in an analysis, consider: - whether the ingredient list matches the intended clinical definition; - whether the selected route is represented correctly across products; - whether combination products should be included; - whether the default product TTYs are sufficient; - whether products without route information require manual review; - whether products without active NDCs matter for the source data; and - whether the study period requires historical RxNorm concepts or NDC associations. For reproducible research, save the final product and NDC tables used in the analysis rather than rebuilding them each time. It is also useful to record the `rxref` version and the date on which the medication definition was generated.