This is an R code session using RxNorm as example.
library(pander);panderOptions('table.split.table', Inf);options(knitr.table.format = 'pandoc');panderOptions("table.alignment.default", "left")
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
#library()
rm(conso)
## Warning in rm(conso): object 'conso' not found
conso<-read.delim(file = 'RXNCONSO.RRF', sep='|',header = F, stringsAsFactors=F)
#conso$V3
conso[3:7] <- list(NULL) #not used in this distribution, only in full UMLS
table(conso$V2)
##
## ENG
## 220728
#all english
# column names are taken from http://www.nlm.nih.gov/research/umls/rxnorm/docs/2014/rxnorm_doco_full_2014-2.html#s12_4
#assigning nice column names
names(conso)[1] <- 'rxcui'
names(conso)[3] <- 'rxaui'
names(conso)[7] <- 'sab' #12
names(conso)[8] <- 'tty' #13
names(conso)[10] <- 'str' #15
#looking at the names
names(conso)
## [1] "rxcui" "V2" "rxaui" "V9" "V10" "V11" "sab" "tty"
## [9] "V14" "str" "V16" "V17" "V18" "V19"
#what concept types there are
table(conso$tty)
##
## BN BPCK DF DFG DP GPCK
## 4719 358 107 41 71738 287
## IN MIN MTH_RXN_DP PIN PSN PT
## 5299 1502 393 1427 15331 8
## SBD SBDC SBDF SBDG SCD SCDC
## 8246 7674 5613 7982 14355 12535
## SCDF SCDG SU SY TMSY
## 6873 8004 10932 27719 9585
#nicer output
as.matrix(table(conso$tty))
## [,1]
## BN 4719
## BPCK 358
## DF 107
## DFG 41
## DP 71738
## GPCK 287
## IN 5299
## MIN 1502
## MTH_RXN_DP 393
## PIN 1427
## PSN 15331
## PT 8
## SBD 8246
## SBDC 7674
## SBDF 5613
## SBDG 7982
## SCD 14355
## SCDC 12535
## SCDF 6873
## SCDG 8004
## SU 10932
## SY 27719
## TMSY 9585
#help URL and table
# https://rxnav.nlm.nih.gov/RxNavViews.html
# Abbreviations:
# BN - brand name SBD - branded drug
# BPCK - branded pack SBDC - branded drug component
# DF - dose form SBDF - branded dose form
# DFG - dose form group SBDG - branded dose form group
# GPCK - generic pack SCD - clinical drug
# IN - ingredient SCDC - clinical drug component
# MIN - multiple ingredients SCDF - clinical dose form
# PIN - precise ingredient SCDG - clinical dose form group
more experiments
#get only true rxnorm terms
sconso<-subset(conso,sab=='RXNORM')
#types - what types are present
as.matrix(table(sconso$tty))
## [,1]
## BN 4719
## BPCK 358
## DF 107
## DFG 41
## GPCK 287
## IN 5299
## MIN 1502
## PIN 1427
## PSN 15331
## SBD 8246
## SBDC 7674
## SBDF 5613
## SBDG 7982
## SCD 14355
## SCDC 12535
## SCDF 6873
## SCDG 8004
## SY 27719
## TMSY 9585
#get only ingredients and CUI and name for each intredient
ingr<-subset(conso,tty=='IN',select=c('rxcui','str'))
#only brand names
bn<-subset(conso,tty=='BN',select=c('rxcui','str'))
#nrow(unique(bn)) same
#head(bn,5)
#lets create a simple lookup file for RxCUIs
codes<-subset(sconso,select=c('rxcui','str'))
codes$str<-tolower(codes$str)
codes<-unique(codes)
nrow(unique(codes))
## [1] 119066
Relationship file
rel<-read.delim(file = 'RXNREL.RRF', sep='|',header = F, stringsAsFactors=F)
#rm(rel)
#table(conso$V2)
#all english
# column names are taken from http://www.nlm.nih.gov/research/umls/rxnorm/docs/2014/rxnorm_doco_full_2014-2.html#s12_4
#http://www.nlm.nih.gov/research/umls/rxnorm/docs/index.html
#names(rel)
names(rel)[1] <- 'rxcui1'
names(rel)[5] <- 'rxcui2'
names(rel)[4] <- 'rel'
names(rel)[3] <- 'stype1'
names(rel)[7] <- 'stype2'
names(rel)[8] <- 'rela'
names(rel)[11] <- 'source_abbrev'
rel$V12 <- NULL #not used in this distribution, only in full UMLS
rel$V10 <- NULL
table(rel$rel)
##
## RB RN RO SY
## 126678 126678 1763974 52635
table(rel$stype1)
##
## AUI CUI
## 1473231 596734
table(rel$source_abbrev)
##
## MTHSPL RXNORM
## 1420596 649369
#either rxnrom or mthspl
names(rel)
## [1] "rxcui1" "V2" "stype1" "rel"
## [5] "rxcui2" "V6" "stype2" "rela"
## [9] "V9" "source_abbrev" "V13" "V14"
## [13] "V15" "V16" "V17"
#names(conso)[3] <- 'rxaui'
#smaller relationship table
#srel<-subset(rel,stype1=='CUI')
#table(srel$rela)
table(rel$source_abbrev)
##
## MTHSPL RXNORM
## 1420596 649369
as.data.frame(table(rel$rela))
## Var1 Freq
## 1 52635
## 2 active_ingredient_of 89333
## 3 active_moiety_of 88891
## 4 consists_of 42455
## 5 constitutes 42455
## 6 contained_in 1283
## 7 contains 1283
## 8 dose_form_of 35732
## 9 doseformgroup_of 15986
## 10 form_of 1204
## 11 has_active_ingredient 89333
## 12 has_active_moiety 88891
## 13 has_dose_form 35732
## 14 has_doseformgroup 15986
## 15 has_form 1204
## 16 has_inactive_ingredient 532074
## 17 has_ingredient 66473
## 18 has_ingredients 3883
## 19 has_part 4751
## 20 has_precise_ingredient 4935
## 21 has_quantified_form 32
## 22 has_tradename 42261
## 23 inactive_ingredient_of 532074
## 24 ingredient_of 66473
## 25 ingredients_of 3883
## 26 inverse_isa 79365
## 27 isa 79365
## 28 part_of 4751
## 29 precise_ingredient_of 4935
## 30 quantified_form_of 32
## 31 reformulated_to 7
## 32 reformulation_of 7
## 33 tradename_of 42261
map<-subset(rel,rela=='tradename_of',select=c('rxcui2','rela','rxcui1'))
#note that we switched the order (to account for RxNorm semantics)
head(map)
## rxcui2 rela rxcui1
## 1473242 218330 tradename_of 44
## 1473251 219831 tradename_of 73
## 1473253 352790 tradename_of 73
## 1473274 884749 tradename_of 73
## 1473275 999533 tradename_of 73
## 1473288 1043301 tradename_of 73
#lets use this map on our brand names
head(bn)
## rxcui str
## 1 38 Parlodel
## 77 332 Adipex-P
## 113 479 Alfenta
## 217 756 Anafranil
## 218 769 Anaspaz
## 266 1204 ATGAM
#base R offers merge function, but dplyr or data.table is better
#left inner join
#mapbn<-merge(map,bn,by.x='rxcui1',by.y='rxcui',sort=T)
#head(mapbn)
# left outer join
#mapbn2<-merge(map,bn,by.x='rxcui1',by.y='rxcui',sort=T,all.x=T)
nrow(bn)
## [1] 4719
library(dplyr)
#left_join() disadvantage was that that it has to be same column name, new version has a by parameter
#head(rename(bn,rxcui1=rxcui))
names(map)
## [1] "rxcui2" "rela" "rxcui1"
mapBn<- bn %>% left_join(map,by=c('rxcui' = 'rxcui2'))
head(bn)
## rxcui str
## 1 38 Parlodel
## 77 332 Adipex-P
## 113 479 Alfenta
## 217 756 Anafranil
## 218 769 Anaspaz
## 266 1204 ATGAM
head(mapBn)
## rxcui str rela rxcui1
## 1 38 Parlodel tradename_of 1760
## 2 332 Adipex-P tradename_of 8152
## 3 479 Alfenta tradename_of 480
## 4 756 Anafranil tradename_of 2597
## 5 769 Anaspaz tradename_of 153970
## 6 1204 ATGAM tradename_of 91601
#let's slap on the conso file
names(codes)
## [1] "rxcui" "str"
mapBnIn<-left_join(mapBn,codes,by.x='rxcui1',by.y='rxcui')
## Joining by: c("rxcui", "str")
head(bn)
## rxcui str
## 1 38 Parlodel
## 77 332 Adipex-P
## 113 479 Alfenta
## 217 756 Anafranil
## 218 769 Anaspaz
## 266 1204 ATGAM
head(mapBn)
## rxcui str rela rxcui1
## 1 38 Parlodel tradename_of 1760
## 2 332 Adipex-P tradename_of 8152
## 3 479 Alfenta tradename_of 480
## 4 756 Anafranil tradename_of 2597
## 5 769 Anaspaz tradename_of 153970
## 6 1204 ATGAM tradename_of 91601
head(mapBnIn)
## rxcui str rela rxcui1
## 1 38 Parlodel tradename_of 1760
## 2 332 Adipex-P tradename_of 8152
## 3 479 Alfenta tradename_of 480
## 4 756 Anafranil tradename_of 2597
## 5 769 Anaspaz tradename_of 153970
## 6 1204 ATGAM tradename_of 91601
#Uh oh, totaly not what we wanted !
mapBnIn<-left_join(mapBn,rename(codes,rxcui1=rxcui,str1=str),by.x='rxcui1',by.y='rxcui')
## Joining by: "rxcui1"
head(bn)
## rxcui str
## 1 38 Parlodel
## 77 332 Adipex-P
## 113 479 Alfenta
## 217 756 Anafranil
## 218 769 Anaspaz
## 266 1204 ATGAM
head(mapBn)
## rxcui str rela rxcui1
## 1 38 Parlodel tradename_of 1760
## 2 332 Adipex-P tradename_of 8152
## 3 479 Alfenta tradename_of 480
## 4 756 Anafranil tradename_of 2597
## 5 769 Anaspaz tradename_of 153970
## 6 1204 ATGAM tradename_of 91601
head(mapBnIn)
## rxcui str rela rxcui1 str1
## 1 38 Parlodel tradename_of 1760 bromocriptine
## 2 332 Adipex-P tradename_of 8152 phentermine
## 3 479 Alfenta tradename_of 480 alfentanil
## 4 756 Anafranil tradename_of 2597 clomipramine
## 5 769 Anaspaz tradename_of 153970 hyoscyamine
## 6 1204 ATGAM tradename_of 91601 lymphocyte immune globulin
#library(sqldf)
#map<-sqldf("select * from rel where rela='has_tradename')
#mapbnin<-sqldf('select * from map)
#rename
write.csv(mapBnIn,file="mapBnIn-003.csv",row.names=F,quote=T,na='')
API use and JSON experiments
#API
#some demo codes
rxcui=308460
url<-'https://rxnav.nlm.nih.gov/REST/rxcui/308460/allrelated.json'
url<-sprintf('https://rxnav.nlm.nih.gov/REST/rxcui/%s/allrelated.json',rxcui)
url
## [1] "https://rxnav.nlm.nih.gov/REST/rxcui/308460/allrelated.json"
#there are 3 JSON libraries, after googling, we pick jsonlite
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:utils':
##
## View
library(dplyr)
j<-fromJSON(url)
j$allRelatedGroup$conceptGroup$tty
## [1] "BN" "BPCK" "DF" "GPCK" "IN" "MIN" "PIN" "SBD" "SBDC" "SBDF"
## [11] "SCD" "SCDC" "SCDF" "SCDG" "SBDG" "DFG"
tta<-j$allRelatedGroup$conceptGroup
pander(tta)
BN |
196474, Zithromax, , BN, ENG, N, C0678143 |
BPCK |
750149, {6 (Azithromycin 250 MG Oral Tablet [Zithromax]) } Pack [Z-PAK], Z-PAK, BPCK, ENG, N, C1878682 |
DF |
317541, Oral Tablet, , DF, ENG, N, C0993159 |
GPCK |
749783, {6 (Azithromycin 250 MG Oral Tablet) } Pack, azithromycin 250 MG Oral Tablet 6 Count Pack, GPCK, ENG, N, C1327690 |
IN |
18631, Azithromycin, , IN, ENG, N, C0052796 |
MIN |
NULL |
PIN |
NULL |
SBD |
212446, Azithromycin 250 MG Oral Tablet [Zithromax], Zithromax 250 MG Oral Tablet, SBD, ENG, N, C0715252 |
SBDC |
564001, Azithromycin 250 MG [Zithromax], , SBDC, ENG, N, C1596442 |
SBDF |
367697, Azithromycin Oral Tablet [Zithromax], , SBDF, ENG, N, C1241672 |
SCD |
308460, Azithromycin 250 MG Oral Tablet, , SCD, ENG, N, C0974670 |
SCDC |
315449, Azithromycin 250 MG, , SCDC, ENG, N, C0983950 |
SCDF |
370976, Azithromycin Oral Tablet, , SCDF, ENG, N, C1245010 |
SCDG |
1155011, 1155012, Azithromycin Oral Product, Azithromycin Pill, , , SCDG, SCDG, ENG, ENG, N, N, C1271250, C3209472 |
SBDG |
1187674, 1187675, Zithromax Oral Product, Zithromax Pill, , , SBDG, SBDG, ENG, ENG, N, N, C3241414, C3241415 |
DFG |
1151131, 1151133, Oral Product, Pill, , , DFG, DFG, ENG, ENG, N, N, C3205666, C0994475 |
#skip this part
# ttb<-filter(tta,tty=='DF')
# ttb
# ttb$conceptProperties
#
# ttc<-j$allRelatedGroup$conceptGroup$conceptProperties
# ttd<-do.call("rbind",ttc)
# tte<-plyr::rbind.fill(ttc)
#
# ttf<-plyr::rbind.fill(j$allRelatedGroup$conceptGroup$conceptProperties)
# ttf$rxcui0=rxcui
# defining a function
getAllRelated<-function(rxcui) {
url<-sprintf('https://rxnav.nlm.nih.gov/REST/rxcui/%s/allrelated.json',rxcui)
j<-fromJSON(url)
ttf<-plyr::rbind.fill(j$allRelatedGroup$conceptGroup$conceptProperties)
ttf$rxcui0=rxcui
#alsways end function with the ouput call (use list(a=) if multiple)
ttf
}
getAllRelated(308460)
## rxcui name
## 1 196474 Zithromax
## 2 750149 {6 (Azithromycin 250 MG Oral Tablet [Zithromax]) } Pack [Z-PAK]
## 3 317541 Oral Tablet
## 4 749783 {6 (Azithromycin 250 MG Oral Tablet) } Pack
## 5 18631 Azithromycin
## 6 212446 Azithromycin 250 MG Oral Tablet [Zithromax]
## 7 564001 Azithromycin 250 MG [Zithromax]
## 8 367697 Azithromycin Oral Tablet [Zithromax]
## 9 308460 Azithromycin 250 MG Oral Tablet
## 10 315449 Azithromycin 250 MG
## 11 370976 Azithromycin Oral Tablet
## 12 1155011 Azithromycin Oral Product
## 13 1155012 Azithromycin Pill
## 14 1187674 Zithromax Oral Product
## 15 1187675 Zithromax Pill
## 16 1151131 Oral Product
## 17 1151133 Pill
## synonym tty language suppress
## 1 BN ENG N
## 2 Z-PAK BPCK ENG N
## 3 DF ENG N
## 4 azithromycin 250 MG Oral Tablet 6 Count Pack GPCK ENG N
## 5 IN ENG N
## 6 Zithromax 250 MG Oral Tablet SBD ENG N
## 7 SBDC ENG N
## 8 SBDF ENG N
## 9 SCD ENG N
## 10 SCDC ENG N
## 11 SCDF ENG N
## 12 SCDG ENG N
## 13 SCDG ENG N
## 14 SBDG ENG N
## 15 SBDG ENG N
## 16 DFG ENG N
## 17 DFG ENG N
## umlscui rxcui0
## 1 C0678143 308460
## 2 C1878682 308460
## 3 C0993159 308460
## 4 C1327690 308460
## 5 C0052796 308460
## 6 C0715252 308460
## 7 C1596442 308460
## 8 C1241672 308460
## 9 C0974670 308460
## 10 C0983950 308460
## 11 C1245010 308460
## 12 C1271250 308460
## 13 C3209472 308460
## 14 C3241414 308460
## 15 C3241415 308460
## 16 C3205666 308460
## 17 C0994475 308460
using our function
#let's use our new function
#we will need some data
#how about all brand names again
head(bn$rxcui)
## [1] 38 332 479 756 769 1204
#key function here
ttg<-lapply(head(bn$rxcui),getAllRelated)
#this may take some time
#make it data.frame
tth<-plyr::rbind.fill(ttg)
#output of the combinging all properties
pander(tth)
38 |
Parlodel |
|
BN |
ENG |
N |
C0000266 |
38 |
316965 |
Oral Capsule |
|
DF |
ENG |
N |
C0991533 |
38 |
317541 |
Oral Tablet |
|
DF |
ENG |
N |
C0993159 |
38 |
1760 |
Bromocriptine |
|
IN |
ENG |
N |
C0006230 |
38 |
142426 |
2-Bromoergocryptine Mesylate |
|
PIN |
ENG |
N |
C0546852 |
38 |
105050 |
Bromocriptine 5 MG Oral Capsule [Parlodel] |
Parlodel 5 MG Oral Capsule |
SBD |
ENG |
N |
C0355759 |
38 |
105446 |
Bromocriptine 2.5 MG Oral Tablet [Parlodel] |
Parlodel 2.5 MG Oral Tablet |
SBD |
ENG |
N |
C0356595 |
38 |
564062 |
Bromocriptine 2.5 MG [Parlodel] |
|
SBDC |
ENG |
N |
C1596503 |
38 |
563894 |
Bromocriptine 5 MG [Parlodel] |
|
SBDC |
ENG |
N |
C1596335 |
38 |
368543 |
Bromocriptine Oral Tablet [Parlodel] |
|
SBDF |
ENG |
N |
C1242530 |
38 |
366490 |
Bromocriptine Oral Capsule [Parlodel] |
|
SBDF |
ENG |
N |
C1240459 |
38 |
197411 |
Bromocriptine 2.5 MG Oral Tablet |
bromocriptine (as bromocriptine mesylate) 2.5 MG Oral Tablet |
SCD |
ENG |
N |
C0688043 |
38 |
197412 |
Bromocriptine 5 MG Oral Capsule |
bromocriptine 5 MG (bromocriptine mesylate 5.74 MG) Oral Capsule |
SCD |
ENG |
N |
C0688044 |
38 |
329606 |
Bromocriptine 5 MG |
|
SCDC |
ENG |
N |
C1124895 |
38 |
329605 |
Bromocriptine 2.5 MG |
|
SCDC |
ENG |
N |
C1124894 |
38 |
371127 |
Bromocriptine Oral Capsule |
|
SCDF |
ENG |
N |
C1245164 |
38 |
371128 |
Bromocriptine Oral Tablet |
|
SCDF |
ENG |
N |
C1245165 |
38 |
1155381 |
Bromocriptine Oral Product |
|
SCDG |
ENG |
N |
C3209835 |
38 |
1155382 |
Bromocriptine Pill |
|
SCDG |
ENG |
N |
C3209836 |
38 |
1182382 |
Parlodel Pill |
|
SBDG |
ENG |
N |
C3236217 |
38 |
1182381 |
Parlodel Oral Product |
|
SBDG |
ENG |
N |
C3236216 |
38 |
1151131 |
Oral Product |
|
DFG |
ENG |
N |
C3205666 |
38 |
1151133 |
Pill |
|
DFG |
ENG |
N |
C0994475 |
38 |
332 |
Adipex-P |
|
BN |
ENG |
N |
C0001520 |
332 |
316965 |
Oral Capsule |
|
DF |
ENG |
N |
C0991533 |
332 |
317541 |
Oral Tablet |
|
DF |
ENG |
N |
C0993159 |
332 |
8152 |
Phentermine |
|
IN |
ENG |
N |
C0031447 |
332 |
82078 |
Phentermine Hydrochloride |
|
PIN |
ENG |
N |
C0282304 |
332 |
803354 |
Phentermine Hydrochloride 37.5 MG Oral Tablet [Adipex-P] |
Adipex-P 37.5 MG Oral Tablet |
SBD |
ENG |
N |
C1329300 |
332 |
803350 |
Phentermine Hydrochloride 37.5 MG Oral Capsule [Adipex-P] |
Adipex-P 37.5 MG Oral Capsule |
SBD |
ENG |
N |
C1329481 |
332 |
803349 |
Phentermine Hydrochloride 37.5 MG [Adipex-P] |
|
SBDC |
ENG |
N |
C2356294 |
332 |
405765 |
Phentermine Oral Tablet [Adipex-P] |
|
SBDF |
ENG |
N |
C1330982 |
332 |
405764 |
Phentermine Oral Capsule [Adipex-P] |
|
SBDF |
ENG |
N |
C1330981 |
332 |
803353 |
Phentermine Hydrochloride 37.5 MG Oral Tablet |
phentermine hydrochloride 37.5 MG (equivalent to phentermine 30 MG) Oral Tablet |
SCD |
ENG |
N |
C2356295 |
332 |
803348 |
Phentermine Hydrochloride 37.5 MG Oral Capsule |
phentermine hydrochloride 37.5 MG (equivalent to phentermine 30 MG) Oral Capsule |
SCD |
ENG |
N |
C2362843 |
332 |
803347 |
Phentermine Hydrochloride 37.5 MG |
|
SCDC |
ENG |
N |
C2356293 |
332 |
373349 |
Phentermine Oral Capsule |
|
SCDF |
ENG |
N |
C1247414 |
332 |
373351 |
Phentermine Oral Tablet |
|
SCDF |
ENG |
N |
C1247416 |
332 |
1157569 |
Phentermine Pill |
|
SCDG |
ENG |
N |
C3211988 |
332 |
1157568 |
Phentermine Oral Product |
|
SCDG |
ENG |
N |
C1270966 |
332 |
1170632 |
Adipex-P Pill |
|
SBDG |
ENG |
N |
C3224748 |
332 |
1170631 |
Adipex-P Oral Product |
|
SBDG |
ENG |
N |
C3224747 |
332 |
1151131 |
Oral Product |
|
DFG |
ENG |
N |
C3205666 |
332 |
1151133 |
Pill |
|
DFG |
ENG |
N |
C0994475 |
332 |
479 |
Alfenta |
|
BN |
ENG |
N |
C0002025 |
479 |
316949 |
Injectable Solution |
|
DF |
ENG |
N |
C0991511 |
479 |
480 |
Alfentanil |
|
IN |
ENG |
N |
C0002026 |
479 |
203203 |
Alfentanil hydrochloride |
|
PIN |
ENG |
N |
C0700562 |
479 |
106499 |
Alfentanil 0.5 MG/ML Injectable Solution [Alfenta] |
Alfenta 0.5 MG/ML Injectable Solution |
SBD |
ENG |
N |
C0710745 |
479 |
564334 |
Alfentanil 0.5 MG/ML [Alfenta] |
|
SBDC |
ENG |
N |
C1621185 |
479 |
91732 |
Alfentanil Injectable Solution [Alfenta] |
|
SBDF |
ENG |
N |
C1618599 |
479 |
307819 |
Alfentanil 0.5 MG/ML Injectable Solution |
alfentanil 500 MCG/ML Injectable Solution |
SCD |
ENG |
N |
C0973926 |
479 |
328828 |
Alfentanil 0.5 MG/ML |
|
SCDC |
ENG |
N |
C1123823 |
479 |
370546 |
Alfentanil Injectable Solution |
|
SCDF |
ENG |
N |
C1244567 |
479 |
1154625 |
Alfentanil Injectable Product |
|
SCDG |
ENG |
N |
C3209090 |
479 |
1175508 |
Alfenta Injectable Product |
|
SBDG |
ENG |
N |
C3229504 |
479 |
1151126 |
Injectable Product |
|
DFG |
ENG |
N |
C0086466 |
479 |
756 |
Anafranil |
|
BN |
ENG |
N |
C0002752 |
756 |
316965 |
Oral Capsule |
|
DF |
ENG |
N |
C0991533 |
756 |
2597 |
Clomipramine |
|
IN |
ENG |
N |
C0009010 |
756 |
81984 |
Clomipramine Hydrochloride |
|
PIN |
ENG |
N |
C0282107 |
756 |
857303 |
Clomipramine Hydrochloride 50 MG Oral Capsule [Anafranil] |
Anafranil 50 MG Oral Capsule |
SBD |
ENG |
N |
C0698289 |
756 |
857307 |
Clomipramine Hydrochloride 75 MG Oral Capsule [Anafranil] |
Anafranil 75 MG Oral Capsule |
SBD |
ENG |
N |
C0707125 |
756 |
857299 |
Clomipramine Hydrochloride 25 MG Oral Capsule [Anafranil] |
Anafranil 25 MG Oral Capsule |
SBD |
ENG |
N |
C0698288 |
756 |
857298 |
Clomipramine Hydrochloride 25 MG [Anafranil] |
|
SBDC |
ENG |
N |
C2710787 |
756 |
857302 |
Clomipramine Hydrochloride 50 MG [Anafranil] |
|
SBDC |
ENG |
N |
C2710789 |
756 |
857306 |
Clomipramine Hydrochloride 75 MG [Anafranil] |
|
SBDC |
ENG |
N |
C2710791 |
756 |
366755 |
Clomipramine Oral Capsule [Anafranil] |
|
SBDF |
ENG |
N |
C1240724 |
756 |
857301 |
Clomipramine Hydrochloride 50 MG Oral Capsule |
|
SCD |
ENG |
N |
C0688371 |
756 |
857305 |
Clomipramine Hydrochloride 75 MG Oral Capsule |
|
SCD |
ENG |
N |
C0688372 |
756 |
857297 |
Clomipramine Hydrochloride 25 MG Oral Capsule |
|
SCD |
ENG |
N |
C0688370 |
756 |
857304 |
Clomipramine Hydrochloride 75 MG |
|
SCDC |
ENG |
N |
C2710790 |
756 |
857295 |
Clomipramine Hydrochloride 25 MG |
|
SCDC |
ENG |
N |
C2710786 |
756 |
857300 |
Clomipramine Hydrochloride 50 MG |
|
SCDC |
ENG |
N |
C2710788 |
756 |
371572 |
Clomipramine Oral Capsule |
|
SCDF |
ENG |
N |
C1245613 |
756 |
1153375 |
Clomipramine Pill |
|
SCDG |
ENG |
N |
C3207860 |
756 |
1153374 |
Clomipramine Oral Product |
|
SCDG |
ENG |
N |
C3207859 |
756 |
1174907 |
Anafranil Pill |
|
SBDG |
ENG |
N |
C3228921 |
756 |
1174906 |
Anafranil Oral Product |
|
SBDG |
ENG |
N |
C3228920 |
756 |
1151131 |
Oral Product |
|
DFG |
ENG |
N |
C3205666 |
756 |
1151133 |
Pill |
|
DFG |
ENG |
N |
C0994475 |
756 |
769 |
Anaspaz |
|
BN |
ENG |
N |
C0002802 |
769 |
316942 |
Disintegrating Oral Tablet |
|
DF |
ENG |
N |
C0991504 |
769 |
153970 |
Hyoscyamine |
|
IN |
ENG |
N |
C0596004 |
769 |
1225 |
Hyoscyamine Sulfate |
|
PIN |
ENG |
N |
C0004261 |
769 |
1048056 |
Hyoscyamine Sulfate 0.125 MG Disintegrating Oral Tablet [Anaspaz] |
Anaspaz 0.125 MG Disintegrating Oral Tablet |
SBD |
ENG |
N |
C0713899 |
769 |
1048055 |
Hyoscyamine Sulfate 0.125 MG [Anaspaz] |
|
SBDC |
ENG |
N |
C2954504 |
769 |
730601 |
Hyoscyamine Disintegrating Oral Tablet [Anaspaz] |
|
SBDF |
ENG |
N |
C1965421 |
769 |
1046985 |
Hyoscyamine Sulfate 0.125 MG Disintegrating Oral Tablet |
Hyoscyamine Sulfate 0.125 MG Chewable Tablet |
SCD |
ENG |
N |
C0937296 |
769 |
1046981 |
Hyoscyamine Sulfate 0.125 MG |
|
SCDC |
ENG |
N |
C2949673 |
769 |
370665 |
Hyoscyamine Disintegrating Oral Tablet |
|
SCDF |
ENG |
N |
C1577746 |
769 |
1165278 |
Hyoscyamine Oral Product |
|
SCDG |
ENG |
N |
C1270932 |
769 |
1165279 |
Hyoscyamine Pill |
|
SCDG |
ENG |
N |
C3219534 |
769 |
1294826 |
Hyoscyamine Disintegrating Oral Product |
|
SCDG |
ENG |
N |
C3465491 |
769 |
1174935 |
Anaspaz Pill |
|
SBDG |
ENG |
N |
C3228949 |
769 |
1174934 |
Anaspaz Oral Product |
|
SBDG |
ENG |
N |
C3228948 |
769 |
1296711 |
Anaspaz Disintegrating Oral Product |
|
SBDG |
ENG |
N |
C3467315 |
769 |
1151131 |
Oral Product |
|
DFG |
ENG |
N |
C3205666 |
769 |
1151133 |
Pill |
|
DFG |
ENG |
N |
C0994475 |
769 |
1294713 |
Disintegrating Oral Product |
|
DFG |
ENG |
N |
C3465379 |
769 |
1204 |
ATGAM |
|
BN |
ENG |
N |
C0004149 |
1204 |
316949 |
Injectable Solution |
|
DF |
ENG |
N |
C0991511 |
1204 |
91601 |
Lymphocyte immune globulin |
|
IN |
ENG |
N |
C0305055 |
1204 |
206289 |
Lymphocyte immune globulin 50 MG/ML Injectable Solution [ATGAM] |
ATGAM 50 MG/ML Injectable Solution |
SBD |
ENG |
N |
C0707351 |
1204 |
567141 |
Lymphocyte immune globulin 50 MG/ML [ATGAM] |
|
SBDC |
ENG |
N |
C1599565 |
1204 |
362978 |
Lymphocyte immune globulin Injectable Solution [ATGAM] |
|
SBDF |
ENG |
N |
C1236907 |
1204 |
197906 |
Lymphocyte immune globulin 50 MG/ML Injectable Solution |
|
SCD |
ENG |
N |
C0689413 |
1204 |
332132 |
Lymphocyte immune globulin 50 MG/ML |
|
SCDC |
ENG |
N |
C1128296 |
1204 |
377108 |
Lymphocyte immune globulin Injectable Solution |
|
SCDF |
ENG |
N |
C1251320 |
1204 |
1156320 |
Lymphocyte immune globulin Injectable Product |
|
SCDG |
ENG |
N |
C3210756 |
1204 |
1165939 |
ATGAM Injectable Product |
|
SBDG |
ENG |
N |
C3220180 |
1204 |
1151126 |
Injectable Product |
|
DFG |
ENG |
N |
C0086466 |
1204 |
#embracing vectorized approach instead of loop is not trivial
#if all codes are proper concepts, or tth data frame should contain
#old code
#lapply(ttg,class)
#head(ttg[[1]])
#head(ttg[[3]])
#tt1<-plyr::ldply(ttg,class)
#table(tt1$V1)
#for (i in 1:200) print(head(ttg[[i]]))
# head(ttg[[9670]])
#from ttg select only the data.frames
#list.condition <- sapply(ttg, function(x) class(x)=="data.frame")
#ttg2<- ttg[list.condition]
#tth<-plyr::rbind.fill(ttg2)
code export
cat(readr::read_file('RxNormDemo02-extended.Rmd'))
---
title: "RxNorm"
author: "Vojtech Huser"
date: "October 6, 2015"
output:
html_document:
toc: yes
---
This is an R code session using RxNorm as example.
```{r}
library(pander);panderOptions('table.split.table', Inf);options(knitr.table.format = 'pandoc');panderOptions("table.alignment.default", "left")
library(dplyr)
library(tidyr)
#library()
rm(conso)
conso<-read.delim(file = 'RXNCONSO.RRF', sep='|',header = F, stringsAsFactors=F)
#conso$V3
conso[3:7] <- list(NULL) #not used in this distribution, only in full UMLS
table(conso$V2)
#all english
# column names are taken from http://www.nlm.nih.gov/research/umls/rxnorm/docs/2014/rxnorm_doco_full_2014-2.html#s12_4
#assigning nice column names
names(conso)[1] <- 'rxcui'
names(conso)[3] <- 'rxaui'
names(conso)[7] <- 'sab' #12
names(conso)[8] <- 'tty' #13
names(conso)[10] <- 'str' #15
#looking at the names
names(conso)
#what concept types there are
table(conso$tty)
#nicer output
as.matrix(table(conso$tty))
#help URL and table
# https://rxnav.nlm.nih.gov/RxNavViews.html
# Abbreviations:
# BN - brand name SBD - branded drug
# BPCK - branded pack SBDC - branded drug component
# DF - dose form SBDF - branded dose form
# DFG - dose form group SBDG - branded dose form group
# GPCK - generic pack SCD - clinical drug
# IN - ingredient SCDC - clinical drug component
# MIN - multiple ingredients SCDF - clinical dose form
# PIN - precise ingredient SCDG - clinical dose form group
```
#more experiments
```{r}
#get only true rxnorm terms
sconso<-subset(conso,sab=='RXNORM')
#types - what types are present
as.matrix(table(sconso$tty))
#get only ingredients and CUI and name for each intredient
ingr<-subset(conso,tty=='IN',select=c('rxcui','str'))
#only brand names
bn<-subset(conso,tty=='BN',select=c('rxcui','str'))
#nrow(unique(bn)) same
#head(bn,5)
#lets create a simple lookup file for RxCUIs
codes<-subset(sconso,select=c('rxcui','str'))
codes$str<-tolower(codes$str)
codes<-unique(codes)
nrow(unique(codes))
```
#Relationship file
```{r}
rel<-read.delim(file = 'RXNREL.RRF', sep='|',header = F, stringsAsFactors=F)
#rm(rel)
#table(conso$V2)
#all english
# column names are taken from http://www.nlm.nih.gov/research/umls/rxnorm/docs/2014/rxnorm_doco_full_2014-2.html#s12_4
#http://www.nlm.nih.gov/research/umls/rxnorm/docs/index.html
#names(rel)
names(rel)[1] <- 'rxcui1'
names(rel)[5] <- 'rxcui2'
names(rel)[4] <- 'rel'
names(rel)[3] <- 'stype1'
names(rel)[7] <- 'stype2'
names(rel)[8] <- 'rela'
names(rel)[11] <- 'source_abbrev'
rel$V12 <- NULL #not used in this distribution, only in full UMLS
rel$V10 <- NULL
table(rel$rel)
table(rel$stype1)
table(rel$source_abbrev)
#either rxnrom or mthspl
names(rel)
#names(conso)[3] <- 'rxaui'
#smaller relationship table
#srel<-subset(rel,stype1=='CUI')
#table(srel$rela)
table(rel$source_abbrev)
as.data.frame(table(rel$rela))
map<-subset(rel,rela=='tradename_of',select=c('rxcui2','rela','rxcui1'))
#note that we switched the order (to account for RxNorm semantics)
head(map)
#lets use this map on our brand names
head(bn)
#base R offers merge function, but dplyr or data.table is better
#left inner join
#mapbn<-merge(map,bn,by.x='rxcui1',by.y='rxcui',sort=T)
#head(mapbn)
# left outer join
#mapbn2<-merge(map,bn,by.x='rxcui1',by.y='rxcui',sort=T,all.x=T)
nrow(bn)
library(dplyr)
#left_join() disadvantage was that that it has to be same column name, new version has a by parameter
#head(rename(bn,rxcui1=rxcui))
names(map)
mapBn<- bn %>% left_join(map,by=c('rxcui' = 'rxcui2'))
head(bn)
head(mapBn)
#let's slap on the conso file
names(codes)
mapBnIn<-left_join(mapBn,codes,by.x='rxcui1',by.y='rxcui')
head(bn)
head(mapBn)
head(mapBnIn)
#Uh oh, totaly not what we wanted !
mapBnIn<-left_join(mapBn,rename(codes,rxcui1=rxcui,str1=str),by.x='rxcui1',by.y='rxcui')
head(bn)
head(mapBn)
head(mapBnIn)
#library(sqldf)
#map<-sqldf("select * from rel where rela='has_tradename')
#mapbnin<-sqldf('select * from map)
#rename
write.csv(mapBnIn,file="mapBnIn-003.csv",row.names=F,quote=T,na='')
```
#API use and JSON experiments
```{r}
#API
#some demo codes
rxcui=308460
url<-'https://rxnav.nlm.nih.gov/REST/rxcui/308460/allrelated.json'
url<-sprintf('https://rxnav.nlm.nih.gov/REST/rxcui/%s/allrelated.json',rxcui)
url
#there are 3 JSON libraries, after googling, we pick jsonlite
library(jsonlite)
library(dplyr)
j<-fromJSON(url)
j$allRelatedGroup$conceptGroup$tty
tta<-j$allRelatedGroup$conceptGroup
pander(tta)
#skip this part
# ttb<-filter(tta,tty=='DF')
# ttb
# ttb$conceptProperties
#
# ttc<-j$allRelatedGroup$conceptGroup$conceptProperties
# ttd<-do.call("rbind",ttc)
# tte<-plyr::rbind.fill(ttc)
#
# ttf<-plyr::rbind.fill(j$allRelatedGroup$conceptGroup$conceptProperties)
# ttf$rxcui0=rxcui
# defining a function
getAllRelated<-function(rxcui) {
url<-sprintf('https://rxnav.nlm.nih.gov/REST/rxcui/%s/allrelated.json',rxcui)
j<-fromJSON(url)
ttf<-plyr::rbind.fill(j$allRelatedGroup$conceptGroup$conceptProperties)
ttf$rxcui0=rxcui
#alsways end function with the ouput call (use list(a=) if multiple)
ttf
}
getAllRelated(308460)
```
#using our function
```{r}
#let's use our new function
#we will need some data
#how about all brand names again
head(bn$rxcui)
#key function here
ttg<-lapply(head(bn$rxcui),getAllRelated)
#this may take some time
#make it data.frame
tth<-plyr::rbind.fill(ttg)
#output of the combinging all properties
pander(tth)
#embracing vectorized approach instead of loop is not trivial
#if all codes are proper concepts, or tth data frame should contain
#old code
#lapply(ttg,class)
#head(ttg[[1]])
#head(ttg[[3]])
#tt1<-plyr::ldply(ttg,class)
#table(tt1$V1)
#for (i in 1:200) print(head(ttg[[i]]))
# head(ttg[[9670]])
#from ttg select only the data.frames
#list.condition <- sapply(ttg, function(x) class(x)=="data.frame")
#ttg2<- ttg[list.condition]
#tth<-plyr::rbind.fill(ttg2)
```
#code export
```{r comment=''}
cat(readr::read_file('RxNormDemo02-extended.Rmd'))
```
#end
end