# Make Lookup Table

    library(conflicted)
    suppressMessages(conflict_prefer("filter", "dplyr"))

    library(DOPE)
    library(tibble)
    library(dplyr)
    library(stringr)
    library(usethis)

The lookup table has information on drug category, class and synonyms. It is made from the DEA and “No Slang” tables. The core of the table is the dea_factsheets table. It is tweaked to simplify complex names

## Fix dea_brands

The dea brands file has:

- mixed case
- registered trademark symbols
- The drugs kadian is together with ms contin as a single record

    dea_brands <- DOPE::dea_brands %>%
      rename("brand" = brands) %>%
      mutate(across(where(is.character), tolower)) %>%
      mutate(brand = str_remove_all(brand, "®")) %>%
      mutate(brand = case_when(brand == "kadianms-contin" ~ "kadian", # two names
                               TRUE ~ brand)) %>%
      bind_rows(c(category = "morphine", brand = "ms contin"))

## Fix DEA category and class

The fact sheet file has:

- mixed case
- ghb listed with a complex name
  - gamma-hydroxybutyric acid should appear as ghb in all “class” variables
- Mdma listed with 3 names
  - ecstacy, molly and mdma should be appear as mdma in all “class” variables
- peyote and mescalin grouped together
  - peyote and mescalin should appear as mescalin in all “class” variables
- flakka is listed with a complex name
  - flakka should appear as flakka in all “class” variables

These two do not fit well

- spice/ k2, synthetic marijuana
- acetaminophen and oxycodone

    dea_class_cat <- DOPE::dea_factsheets %>%
      mutate(across(where(is.character), tolower)) %>%
      mutate(category =
               case_when(category == "peyote and mescaline" ~ "mescaline",
                         category == "ghb - gamma-hydroxybutyric acid" ~ "ghb",
                         category == "ecstasy or mdma (also known as molly)" ~ "mdma",
                         category == "flakka (alpha-pvp)" ~ "flakka",

                         TRUE ~ category))

## The DEA street name file has:

- mixed case
- fentanyl has a complex name
  - fentanyl should appear as fentanyl in all “class” variables

    dea_street_names <- DOPE::dea_street_names %>%
      mutate(across(where(is.character), tolower)) %>%
      mutate(category =
               case_when(category == "fentanyl and fentanyl derivatives"    ~ "fentanyl",
                         TRUE ~ category))

    # the these drugs do not have fact sheets but they appear on the DEA

    # DEA website (in slang)
    missing <- tribble(
      ~category, ~class, ~fact_path,
      "acetaminophen and oxycodone", "narcotics (opioids)", "DEA slang",
      "alprazolam", "benzodiazepine", "DEA slang" ,
      "clonazepam", "benzodiazepine", "DEA slang"  ,
      "crack cocaine", "stimulants", "DEA slang",
      "hydrocodone", "narcotics (opioids)", "DEA slang",
      "marijuana concentrates", "cannabis", "DEA slang",
      "mushrooms", "hallucinogen", "DEA slang",
      "PCP", "hallucinogen", "DEA slang",
      "promethazine with codeine", "narcotics (opioids)", "DEA slang",
      "ritalin", "stimulants", "DEA slang",
      "synthetic cannabinoids", "designer drugs", "DEA slang",
      "synthetic cathinones", "stimulants", "DEA slang")

    # extras from NoSlang
    extraNewSlang <- tribble(
      ~category,  ~class, ~fact_path,
      "2cb", "hallucinogen", "noSlang Term",
      "alpha-ethyltryptamine", "hallucinogen", "noSlang Term",
      "alpha-methyltryptamine", "hallucinogen", "noSlang Term",
      "amobarbital", "depressant", "noSlang Term",
      "amyl nitrite", "inhalant", "noSlang Term",
      "dextromethorphan", "depressant", "noSlang Term",
      "diazepam", "benzodiazepine", "noSlang Term",
      "dimethyltryptamine", "hallucinogen", "noSlang Term",
      "gbl", "depressant", "noSlang Term",
      "isobutyl nitrite", "inhalant", "noSlang Term",
      "methcathinone", "stimulant", "noSlang Term",
      "methaqualone", "depressant", "noSlang Term",
      "nitrous oxide", "inhalant", "noSlang Term"
    )

    categories <- data.frame(allCat = c(dea_brands$category,
                                       dea_street_names$category,
                                       dea_class_cat$category,
                                       missing$category,
                                       extraNewSlang$category)) %>%
      distinct() %>%
      arrange()

    library(sqldf)
    #> Loading required package: gsubfn
    #> Loading required package: proto
    #> Loading required package: RSQLite
    lookup_df <- sqldf("select cc.class, a.category, a.syn synonym from
                    (select b.category, b.brand as syn from dea_brands as b
                     union
                     select s1.category, s1.slang as syn from dea_street_names as s1
                     union
                     select s2.category, s2.brand as syn from dea_street_names as s2 where s2.brand <> NULL
                     union
                     select ns.drug, ns.street_name as syn from noslang_street_names as ns) as a
                  left join dea_class_cat as cc on a.category = cc.category")

    #use_data(lookup_df, overwrite = TRUE)

Several records do not have a class.

    #> [1] 30

Add data from IQVIA

    iqvia <- DOPE::iqvia %>%
      mutate(across(where(is.character), tolower))

    lookup_df <- lookup_df %>%
      bind_rows(iqvia)

Fix pluralization

    # get classes & categories
    unique(lookup_df$class)
    #>  [1] "hallucinogen"        "narcotics (opioids)" "depressants"
    #>  [4] "stimulants"          "antitussives"        "heroin"
    #>  [7] "inhalant"            "cannabis"            "drugs of concern"
    #> [10] "steroids"            "analgesic"           "diuretic"
    #> [13] "treatment drug"      "reversal agent"
    unique(lookup_df$category)
    #>  [1] "2cb"
    #>  [2] "acetaminophen and oxycodone"
    #>  [3] "alpha-ethyltryptamine"
    #>  [4] "alprazolam"
    #>  [5] "amobarbital"
    #>  [6] "amphetamine"
    #>  [7] "amphetamines"
    #>  [8] "amt"
    #>  [9] "amyl nitrite"
    #> [10] "barbiturates"
    #> [11] "benzodiazepines"
    #> [12] "clonazepam"
    #> [13] "cocaine"
    #> [14] "codeine"
    #> [15] "crack"
    #> [16] "crack cocaine"
    #> [17] "dextromethorphan"
    #> [18] "diazepam"
    #> [19] "dmt"
    #> [20] "fentanyl"
    #> [21] "gbl"
    #> [22] "ghb"
    #> [23] "heroin"
    #> [24] "hydrocodone"
    #> [25] "hydromorphone"
    #> [26] "inhalants"
    #> [27] "isobutyl nitrite"
    #> [28] "ketamine"
    #> [29] "khat"
    #> [30] "lsd"
    #> [31] "marijuana"
    #> [32] "marijuana concentrates"
    #> [33] "mdma"
    #> [34] "mescaline"
    #> [35] "methadone"
    #> [36] "methamphetamine"
    #> [37] "methaqualone"
    #> [38] "methcathinone"
    #> [39] "methylphenidate"
    #> [40] "morphine"
    #> [41] "mushrooms"
    #> [42] "nitrous oxide"
    #> [43] "opium"
    #> [44] "oxycodone"
    #> [45] "pcp"
    #> [46] "peyote"
    #> [47] "promethazine with codeine"
    #> [48] "psilocybin"
    #> [49] "ritalin"
    #> [50] "rohypnol"
    #> [51] "salvia divinorum"
    #> [52] "steroids"
    #> [53] "synthetic cannabinoids"
    #> [54] "synthetic cathinones"
    #> [55] "pain relief"
    #> [56] "codeine combinations, non-injectable"
    #> [57] "caffeine"
    #> [58] "treatment drug"
    #> [59] "morphine-opium, injectable"
    #> [60] "morphine-opium, non-injectable"
    #> [61] "reversal agent"
    #> [62] "synthetic narcotic, injectable"
    #> [63] "synthetic narcotic, non-injectable"

    lookup_df3 <- lookup_df %>%
      mutate(class = if_else(class != "cannabis", str_replace(class, "s$", ""), class),
             class = case_when(class == "narcotics (opioids)" ~ "narcotic (opioid)",
                               TRUE ~ class),
             category = if_else(category != "mushrooms", str_replace(category, "s$", ""), category)
             )

    orange_book <-
      tibble(synonym = c(dea_controlled$substance, dea_controlled$synonym)) %>%
      mutate(synonym = tolower(synonym)) %>%
      distinct() %>%
      mutate(
        difficult = str_count(synonym, "[(]") > 0 |
          str_detect(synonym, ",(?=\\S)") |
          str_detect(synonym, '[-|/|&|,|"| ]')
      ) %>%
      filter(!difficult) %>%
      anti_join(lookup_df, by = "synonym") %>%
      anti_join(lookup_df, by = c("synonym" = "class")) %>%
      anti_join(lookup_df, by = c("synonym" = "category")) %>%
      mutate(category = "Unknown",  class = "Unknown") %>%
      select(class, category, synonym)

    lookup_df <- bind_rows(lookup_df3 , orange_book)

    use_data(lookup_df, overwrite = TRUE)
    #> v Setting active project to 'C:/Users/godom/AppData/Local/Temp/RtmpWA2jey/Rbuild1540c1cf043da/DOPE'
    #> v Saving 'lookup_df' to 'data/lookup_df.rda'
    #> * Document your data (see 'https://r-pkgs.org/data.html')