Drug Interactions

        

I was thinking about drug interactions the other day: the relevant, and/or dangerous interactions that can occure in patients with multiple comorbidities. And I realised then, how few of these interactions I’d learnt by-heart. If I’d wanted to check, I would have looked up a patient’s drugs up in the BNF individually.

To try to learn them, I want to visualise them - although I’ll likely never learn them all whilst new obscure drugs come out, or drug interactions are discovered over time.

As such - I’ve been trying to learn 2 new skills - webscraping to collect the data (in this first post), and the use of D3 to present it. D3 being one of the standards for web graphics, including many notable newspapers.

Luckily all the drug interactions are listed online through the BNF, so I can import and update all this information easily.

For example - Abacavir

library(rvest)
library(magrittr)
library(stringr)
library(stringi)
library(tidyverse)

Creating the drugs look-up.

#Get a list of links to test
drugs_list <- readLines("https://bnf.nice.org.uk/interaction/") %>%
  str_match_all("<a href=\"(.*?)\"><span>(.*?)</span>") %>%
  unlist() %>%
  data.frame()

#Create a dataframe from it.
drugs_list <-  drugs_list %>%
  data.frame(cbind(observation = rep(1:(nrow(drugs_list)/3), each=3))) %>%
  data.frame(cbind(class = c("String", "Link", "Title"))) %>%
  rename(value = '.') %>%
  spread(key=class, value=value)

#Remove non-drug links
drugs_list <- drugs_list %>%
  filter(stri_detect_fixed(drugs_list$Link, "title=") == FALSE)

#Remove defunct columns
drugs_list <- drugs_list %>%
    select(-observation) %>%
    select(-String)

#Remove some leftover tags
drugs_list$Title <- str_replace(drugs_list$Title, "<sub>", replacement = "")
drugs_list$Title <- str_replace(drugs_list$Title, "</sub>", replacement = "")

#Add a string column
drugs_list <- cbind(drugs_list,
                    string = str_replace(drugs_list$Link, ".html", replacement = ""))

#Add CSS tag and URL columns
drugs_list <- cbind(drugs_list,
                    url = str_c("https://bnf.nice.org.uk/interaction/",
                                drugs_list$string, ".html"))
drugs_list <- cbind(drugs_list, css_string = str_c("#", drugs_list$string, " .interactant span"))

#Convert to character classes
drugs_list[] <- lapply(drugs_list, as.character)
##               Link       Title      string
## 1  abacavir-2.html    Abacavir  abacavir-2
## 2 abatacept-2.html   Abatacept abatacept-2
## 3 abciximab-2.html   Abciximab abciximab-2
## 4 abiraterone.html Abiraterone abiraterone
## 5  acarbose-2.html    Acarbose  acarbose-2
##                                                    url
## 1  https://bnf.nice.org.uk/interaction/abacavir-2.html
## 2 https://bnf.nice.org.uk/interaction/abatacept-2.html
## 3 https://bnf.nice.org.uk/interaction/abciximab-2.html
## 4 https://bnf.nice.org.uk/interaction/abiraterone.html
## 5  https://bnf.nice.org.uk/interaction/acarbose-2.html
##                       css_string
## 1  #abacavir-2 .interactant span
## 2 #abatacept-2 .interactant span
## 3 #abciximab-2 .interactant span
## 4 #abiraterone .interactant span
## 5  #acarbose-2 .interactant span

Constructing the interactions database.

#Data
data <- sapply(drugs_list$url, function(x)
               read_html(x) %>%
               list())

#Title
example_dataframe <- data.frame(Title = drugs_list$Title)

#Class
class <- lapply(data,
                function(url){
                    url %>%
                    html_nodes(css = ".unstyled .interactant span") %>%
                    html_text()
                })
example_dataframe <- cbind(example_dataframe, data_frame(class))

#Dangerous Interactions
dangerousi <- lapply(data,
                function(url){
                    url %>%
                    html_nodes(css = ".High .interactant span") %>%
                    html_text()
                })
example_dataframe <- cbind(example_dataframe, data_frame(dangerousi))

#Interactions
interactions <- mapply(url = data, css = drugs_list$css_string,
                       function(url, css){
                          url %>%
                          html_nodes(css) %>%
                          html_text()
                        })
example_dataframe <- cbind(example_dataframe, data_frame(interactions))

#Rename columns
example_dataframe <-  example_dataframe %>%
  rename(Class = 'class',
         'Dangerous Interactions' = 'dangerousi',
         Interactions = 'interactions')

Currently zero (interactions/ dangerous interactions/ class) listing is represented by character(0). And individual drugs and general drug classes are listed together. The latter lists its drug within the ‘Class’ column.

E.g

##         Title               Class
## 1    Abacavir          Antivirals
## 2   Abatacept
## 3   Abciximab
## 4 Abiraterone Hormone Antagonists
## 5    Acarbose       Antidiabetics
##                                                                                    Dangerous Interactions
## 1                                                                         Orlistat, Ribavirin, Tipranavir
## 2                                         Certolizumab pegol, Etanercept, Golimumab, Infliximab, Vaccines
## 3
## 4 Carbamazepine, Fosphenytoin, Phenobarbital, Phenytoin, Primidone, Rifabutin, Rifampicin, St John's Wort
## 5
##                                                                                                                                Interactions
## 1                                 Fosphenytoin, Methadone, Orlistat, Phenobarbital, Phenytoin, Primidone, Ribavirin, Rifampicin, Tipranavir
## 2                                                               Adalimumab, Certolizumab pegol, Etanercept, Golimumab, Infliximab, Vaccines
## 3
## 4 Carbamazepine, Dextromethorphan, Fosphenytoin, Phenobarbital, Phenytoin, Primidone, Rifabutin, Rifampicin, Spironolactone, St John's Wort
## 5                                                                                    Colestyramine, Digoxin, Neomycin, Orlistat, Pancreatin

I’ll add formal BNF classes next and try to apply this data to a D3 model, such as a circular Sankey diagram.

Last Modified:
Next: 'Interactions Continued'
Prev: 'openprescribingR Plots3'