- PROCESS -

RxNorm GET

i=1
library(httr)
API<-  GET("https://rxnav.nlm.nih.gov/REST/allconcepts.json?tty=BN")
data =jsonlite::fromJSON(rawToChar(API$content))
data_frame<- as.data.frame(do.call(rbind, data$minConceptGroup))
data_frame$length<-str_length(data_frame$name)
BN<-data_frame
i=31
new.df<-as.data.frame("")

BN=> MIN | PIN/IN.

for (i in 1:nrow(BN)) {
  print(i)
  row<-BN[i,]
  query1=list(expand="genCard")
    call<-paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/allrelated.json?")
    API<-GET(call,
             query=query1,
            add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
    data =jsonlite::fromJSON(rawToChar(API$content)) 
    if (is.null(data$allRelatedGroup$conceptGroup)) {
      BN$genCard[i]<-0
      row$genCard<-paste0("0")
      new.df<-rbind.fill(row,new.df)
    } else {
            row$genCard<-  paste0("1")
            BN$genCard[i]<-1
            data_frame1<- try(do.call(rbind, data$allRelatedGroup$conceptGroup$conceptProperties), silent=T)
          MIN<-  data_frame1 %>% filter(tty=="MIN") 
          names(MIN)<-paste0("conceptProperties_",names(MIN))

          if (nrow(MIN)==0) {
            IN_PIN<-  data_frame1 %>% filter(tty=="PIN"|tty=="IN") 
            names(IN_PIN)<-paste0("conceptProperties_",names(IN_PIN))

            DF <- cbind(row,IN_PIN )
            rownames(DF)<-NULL

            rm(data_frame1)
          } else { 
            DF <- cbind(row,MIN ) 
            rownames(DF)<-NULL
                        rm(data_frame1)
                        }
          new.df<-plyr::rbind.fill(DF,new.df)
   new.df<-new.df[complete.cases(new.df$rxcui),]
}
}

BN=> tradename_of.

Trade_name<-as.data.frame("")
for (i in 1:nrow(BN)) {
  print(i)
  row<-BN[i,]
  query1=list(rela="tradename_of")
    call<-paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/related.json?")
    API<-GET(call,
             query=query1,
            add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
    data =jsonlite::fromJSON(rawToChar(API$content)) 
    if (is.null(data$relatedGroup$conceptGroup$conceptProperties)) {
      BN$tradename_of[i]<-0
      row$tradename_of<-paste0("0")
      Trade_name<-rbind.fill(Trade_name,row)
    } else {
            DF<-as.data.frame(data$relatedGroup$conceptGroup$conceptProperties)

      row$tradename_of<-  paste0("1")
      BN$tradename_of[i]<-1
       union<-  cbind(row,DF)
    Trade_name<-plyr::rbind.fill(Trade_name,union)
   
}
}

Trade_name<-Trade_name[complete.cases(Trade_name$rxcui),]
Trade_name[,1:length(Trade_name)]<-na_if(Trade_name[,1:length(Trade_name)],"")
Trade_name<-janitor::remove_empty(Trade_name,"cols")
write_csv(Trade_name,"/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/PROC_BRANDS/BN_tradename_of.csv")

BN=>

JSON: https://rxnav.nlm.nih.gov/REST/rxcui/174742/related.json?tty=SBD+SBDF
Trade_name<-as.data.frame("")
for (i in 1:nrow(BN)) {
  print(i)
  row<-BN[i,]
  query1=list(tty="MIN")
    call<-paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/related.json?")
    API<-GET(call,
             query=query1,
            add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
    data =jsonlite::fromJSON(rawToChar(API$content)) 
    if (is.null(data$relatedGroup$rxcui)) {
      BN$has_tradename[i]<-0
      row$has_tradename<-paste0("0")
      Trade_name<-plyr::rbind.fill(Trade_name,row)
    } else {
            DF<-as.data.frame(data$relatedGroup$conceptGroup$conceptProperties)

      row$has_tradename<-  paste0("1")
      BN$has_tradename[i]<-1
       union<-  cbind(row,DF)
    Trade_name<-plyr::rbind.fill(Trade_name,union)
   
}
}

Trade_name<-Trade_name[complete.cases(Trade_name$rxcui),]
Trade_name[,1:length(Trade_name)]<-na_if(Trade_name[,1:length(Trade_name)],"")
Trade_name<-janitor::remove_empty(Trade_name,"cols")
write_csv(Trade_name,"/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/PROC_BRANDS/BN_tradename_of.csv")
library(httr)
pre_proc<-read_csv( "/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/IGNITE_BS_BRANDS.csv")
data_frame1<-read_csv("/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/PROC_BRANDS/BRAND_SURF.csv")

pre_proc<-pre_proc %>% filter(complete.cases(Entry))
API<-  GET("https://rxnav.nlm.nih.gov/REST/allconcepts.json?tty=BN")
data =jsonlite::fromJSON(rawToChar(API$content))
data_frame1<- as.data.frame(do.call(rbind, data$minConceptGroup))
data_frame1$length<-str_length(data_frame1$name)
data_frame1<-data_frame1 %>% arrange(desc(length))
new<-data_frame1
data_frame1$RxNormBrand = sub(".*\\[([^][]+)].*", "\\1",data_frame1$name)
data_frame1$factor<-if_else(str_detect(data_frame1$RxNormBrand, regex(paste(pre_proc$RxNormBrand, collapse = "|"), ignore_case=T)),1,0)

TMP<-data_frame1 %>% filter(factor==1)

rm(data_frame1)
TMP<-TMP %>% select(rxcui, RxNormBrand)


ci_str_detect <- function(x, y){str_detect(x, regex(y, ignore_case = TRUE)) & substring(toupper(x),1,3)==substring(toupper(y),1,3) }
i=1
pre_proc0  <-fuzzyjoin::fuzzy_left_join(pre_proc,TMP, by = c("RxNormBrand" = "RxNormBrand"), match_fun = ci_str_detect)
new.df<-as.data.frame("")
BRANDCUIs<-pre_proc0 %>% filter(complete.cases(rxcui)) %>% select(`Record ID`,Medication.factor,variable, RxNormBrand.x,rxcui)
 for (i in 1:nrow(BRANDCUIs)) {
  print(i)
  row<-BRANDCUIs[i,]
    call<-paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/historystatus.json")
    API<-GET(call,
            add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
    data =jsonlite::fromJSON(rawToChar(API$content)) 
  
    if (length(data)==0) {
      BRANDCUIs$history_status[i]<-0
      row$history_status<-paste0("0")
      new.df<-rbind.fill(new.df,row)
       next } else {
      row$history_status<-  paste0("1")
      BRANDCUIs$history_status[i]<-1
      definitionalFeatures<-as.data.frame(data$rxcuiStatusHistory$definitionalFeatures$ingredientAndStrength)
      
      metaData<-as.data.frame(data$rxcuiStatusHistory$metaData)
      attributes<- as.data.frame(data$rxcuiStatusHistory$attributes)
      derivedConcepts<-as.data.frame(data$rxcuiStatusHistory$derivedConcepts)
      
      if (nrow(derivedConcepts)>0) {
       rxcuis <-paste0("RxCUI_",seq(1:nrow(derivedConcepts)))
       names <-paste0("derivedConcept_",seq(1:nrow(derivedConcepts)))
       derivedConcepts.df<- c(derivedConcepts$ingredientConcept.ingredientRxcui, derivedConcepts$ingredientConcept.ingredientName)
       derivedConcepts.df<-as.data.frame(t(derivedConcepts.df))
       colnames1<-cbind(t(rxcuis), t(names))
       names(derivedConcepts.df)<-colnames1
      }
      if (nrow(attributes)>0) { row<- cbind(row, attributes) }
      if (nrow(metaData)>0) { row<- cbind(row, metaData)  }
      if (nrow(derivedConcepts.df)>0) {row<- cbind(row, derivedConcepts.df)}
      if (nrow(definitionalFeatures)>0) {row<- cbind(row, definitionalFeatures)}
           
    new.df<-plyr::rbind.fill(new.df,row)
   
}
}
new.df<-new.df[complete.cases(new.df$Entry),]
new.df[,1:length(new.df)]<-na_if(new.df[,1:length(new.df)], "")
new.df<-janitor::remove_empty(new.df,"cols")
write_csv(new.df,"/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/PROC_BRANDS/IGNITE_BS_BRAND_derivedConcepts.csv")
i=1
ATC<-as.data.frame("")
for (i in 1:nrow(new.df)) {
  print(i)
  row<-new.df[i,] 
  RXCUI<-as.character(row$rxcui)
  API=try(httr::GET("https://rxnav.nlm.nih.gov/REST/rxclass/class/byRxcui.json" ,
                    query = list(rxcui = RXCUI, relaSource="ATC", format="json")), silent=T)
  data =jsonlite::fromJSON(rawToChar(API$content))
  if (is.null(data$rxclassDrugInfoList$rxclassDrugInfo)) {
    row$ATC<-"0"
    ATC<-plyr::rbind.fill(row, ATC)
    } else {
    row$ATC<-"1"
    df<-as.data.frame(data$rxclassDrugInfoList$rxclassDrugInfo)
    df<-flatten(df)
    api_union<-cbind(row, df)
    ATC<-plyr::rbind.fill(api_union, ATC)
  }
}
write_csv(ATC,"/Volumes/IGNITE_Admin/R_IGNITE/MedicationData/PROC_BRANDS/IGNITE_BS_BRAND_derivedConcepts_ATC14.csv")


cui.codebook<-read_csv("/Volumes/IGNITE_Admin/R_IGNITE/MedicationCodebook/RxTerms_AllTermInfo_SCD_SBD_BPCK_GPCK.csv")
table(pre_proc0$rxcui %in% cui.codebook$rxcui)

pre_proc0

MIN/PIN/IN

API<-  GET("https://rxnav.nlm.nih.gov/REST/allconcepts.json?tty=MIN+PIN+IN")
data =jsonlite::fromJSON(rawToChar(API$content))
data_frame1<- as.data.frame(do.call(rbind, data$minConceptGroup))
data_frame1$length<-str_length(data_frame1$name)
data_frame1<-data_frame1 %>% arrange(desc(length))

pre_proc$MIN_PIN_IN.factor<-if_else(pre_proc$Drug %in%data_frame1$name |
                              pre_proc$Drug_parentheses %in%data_frame1$name |
                              pre_proc$value %in%data_frame1$name ,1,0)
pre_proc$MIN_PIN_IN<-if_else(pre_proc$MIN_PIN_IN.factor==1,
  str_extract(pre_proc$Drug, regex(paste(unique(str_escape(data_frame1$name)), collapse ="|"), ignore_case = T)), "")
pre_proc$MIN_PIN_IN<-if_else(is.na(pre_proc$MIN_PIN_IN), "", pre_proc$MIN_PIN_IN)
pre_proc$MIN_PIN_IN<-if_else(pre_proc$MIN_PIN_IN=="" & !is.na(pre_proc$Drug_parentheses),
                     str_extract(pre_proc$Drug_parentheses, regex(paste(unique(str_escape(data_frame1$name)), collapse ="|"), ignore_case = T)), pre_proc$MIN_PIN_IN)
pre_proc$MIN_PIN_IN<-if_else(is.na(pre_proc$MIN_PIN_IN), "", pre_proc$MIN_PIN_IN)
pre_proc$MIN_PIN_IN<-if_else(pre_proc$MIN_PIN_IN=="",
                     str_extract(pre_proc$value, regex(paste(unique(str_escape(data_frame1$name)), collapse ="|"), ignore_case = T)), pre_proc$MIN_PIN_IN)

getAllConcepts

require(plyr)
API<-  GET("https://rxnav.nlm.nih.gov/REST/allconcepts.json?tty=BN")
data =jsonlite::fromJSON(rawToChar(API$content))
data_frame1<- as.data.frame(do.call(rbind, data$minConceptGroup))
data_frame1$length<-str_length(data_frame1$name)
data_frame1<-data_frame1 %>% arrange(desc(length))
related.df<-as.data.frame("")
i=2


i=1


for (i in 1:nrow(data_frame1)) {
  print(i)
  row<-data_frame1[i,] 
  query1=list(rela="tradename_of")
       call=paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/related.json?")
       API=httr::GET( call,
                      query=query1,
                       add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
       data =jsonlite::fromJSON(rawToChar(API$content))
       if (is.null(data$relatedGroup$conceptGroup$conceptProperties)) {
         row$Trade_Name<-"0"
        related.df<- plyr::rbind.fill(row,related.df)

        print(paste0("NO PROPRIETARY MATCH ", row$Medication.factor," DRUG NAME: ", row$Drug))
        next } else {
          row$Trade_Name<-"1"

          df<-as.data.frame(data$relatedGroup$conceptGroup$conceptProperties)
        union<- cbind(row,df)   
       related.df<- plyr::rbind.fill(union,related.df)

        }
}


for (i in 1:nrow(data_frame1)) {
  print(i)
  row<-data_frame1[i,] 
  query1=list(tty="GPCK")
       call=paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/related.json?")
       API=httr::GET( call,
                      query=query1,
                       add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
       data =jsonlite::fromJSON(rawToChar(API$content))
       if (is.null(data$relatedGroup$conceptGroup$conceptProperties)) {
         row$Trade_Name<-"0"
        related.df<- plyr::rbind.fill(row,related.df)

        print(paste0("NO PROPRIETARY MATCH ", row$Medication.factor," DRUG NAME: ", row$Drug))
        next } else {
          row$Trade_Name<-"1"

          df<-as.data.frame(data$relatedGroup$conceptGroup$conceptProperties)
        union<- cbind(row,df)   
       related.df<- plyr::rbind.fill(union,related.df)

        }
}

new.df<-as.data.frame("")

 for (i in 1:nrow(data_frame1)) {
  print(i)
  row<-data_frame1[i,]
    call<-paste0("https://rxnav.nlm.nih.gov/REST/rxcui/",row$rxcui,"/historystatus.json")
    API<-GET(call,
            add_headers( `Authorization`="Bearer 1927dcc0-1f79-48eb-a6e7-74f357a84b1b",
                                           `content-encoding`="UTF-8"),
                                           `content_type`="application/json")
    data =jsonlite::fromJSON(rawToChar(API$content)) 
  
    if (length(data)==0) {
      data_frame1$history_status[i]<-0
      row$history_status<-paste0("0")
      new.df<-rbind.fill(new.df,row)
       next } else {
      row$history_status<-  paste0("1")
      data_frame1$history_status[i]<-1
      definitionalFeatures<-as.data.frame(data$rxcuiStatusHistory$definitionalFeatures$ingredientAndStrength)
      metaData<-as.data.frame(data$rxcuiStatusHistory$metaData)
            attributes<- as.data.frame(data$rxcuiStatusHistory$attributes)
             if (nrow(attributes)>0) {
              row<- cbind(row, attributes)
              }
            if (nrow(metaData)>0) {
              row<- cbind(row, metaData)
              }
            if (nrow(definitionalFeatures)>0) {
              row<- cbind(row, definitionalFeatures)
              }
           
    new.df<-plyr::rbind.fill(new.df,row)
   
}
}
new.df<-new.df[complete.cases(new.df$name),]
new.df<-janitor::remove_empty(new.df,"cols")
write_csv(new.df,"/Volumes/IGNITE_Admin/R_IGNITE/MedicationCodebook/RxTerms_AllTermInfo_SCD_SBD_BPCK_GPCK.csv")
library(httr)
API<-  GET("https://rxnav.nlm.nih.gov/REST/rxclass/allClasses.json")
data =jsonlite::fromJSON(rawToChar(API$content))
data_frame1<- as.data.frame(do.call(rbind, data$rxclassMinConceptList))

 classTypes=TC+SCHEDULE
pre_proc %>% filter(BN=="" &is.na(MIN_PIN_IN))