Repository: HHS/uts-rest-api Branch: master Commit: 51d78ef84abe Files: 36 Total size: 816.6 KB Directory structure: gitextract_ygghmsng/ ├── README.md └── samples/ ├── R/ │ ├── README.md │ └── UTS REST API - R starting kit - version 0.1.html ├── java/ │ ├── pom.xml │ ├── resources/ │ │ ├── findings.txt │ │ └── hpo-codes.txt │ └── src/ │ └── test/ │ └── java/ │ └── uts/ │ └── rest/ │ └── samples/ │ ├── classes/ │ │ ├── AtomLite.java │ │ ├── ConceptLite.java │ │ ├── SearchResult.java │ │ └── SourceAtomClusterLite.java │ ├── content/ │ │ ├── RetrieveAtomsTestCase.java │ │ ├── RetrieveCodeTestCase.java │ │ ├── RetrieveCuiTestCase.java │ │ └── WalkHierarchyTestCase.java │ ├── cookbook/ │ │ └── CodeCrosswalk.java │ ├── search/ │ │ ├── BatchTermToCuiOrCodeTestCase.java │ │ └── SearchTermsTestCase.java │ └── util/ │ └── RestTicketClient.java ├── perl/ │ ├── crosswalk.pl │ ├── lib/ │ │ └── Authentication/ │ │ └── TicketClient.pm │ ├── retrieve-atoms.pl │ ├── retrieve-cui-or-code.pl │ ├── search-terms.pl │ └── walk-hierarchy.pl └── python/ ├── Authentication.py ├── crosswalk.py ├── get-content-view-members.py ├── get-rxcui-ingredients.py ├── get-rxcui-status.py ├── get-rxcuis-by-tty.py ├── hpo-codes.txt ├── retrieve-cui-or-code.py ├── retrieve-usp-atoms-from-umls.py ├── retrieve-value-set-info.py ├── search-terms.py └── walk-hierarchy.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # uts-rest-api (archived) This repository is no longer maintained. The scripts in this repository may no longer work as expected. For additional help with scripting against the UMLS API, see the following resources: [Python script examples](https://documentation.uts.nlm.nih.gov/rest/rest-api-cookbook/python-scripts.html) - A few sample python scripts that can help you to perform basic batch requests against the UMLS API. [Postman examples](https://documentation.uts.nlm.nih.gov/rest/rest-api-cookbook/postman.html) - API request examples that can be loaded in the Postman application. [UMLS Community](https://www.nlm.nih.gov/research/umls/implementation_resources/community/index.html) - A collection of user-contributed applications and scripts that extend the functionality of the UMLS. ================================================ FILE: samples/R/README.md ================================================ #R examples ================================================ FILE: samples/R/UTS REST API - R starting kit - version 0.1.html ================================================
library('httr')
library('xml2')
library("knitr")
library("stringr")
MY_USERNAME <- "xxxxxxxxxxxxx";
MY_PASSWORD <- "xxxxxxxxxxxxx";
Getting a Ticket Granting Ticket (TGT) using UMLS credentials. The TGT is valid for 8 hours. You do not need a new TGT for each call to the REST API.
getTGT <- function(username, password) {
AUTH_URI <- "https://utslogin.nlm.nih.gov/cas/v1/tickets"
body <- list(username = username, password = password)
TGT_RESPONSE <- POST(AUTH_URI, body = body, encode = "form")
warn_for_status(TGT_RESPONSE)
TGT_uri <- headers(TGT_RESPONSE)$location
TGT_timestamp <- Sys.time()
return(list(TGT_uri,TGT_timestamp) )
}
tgt <- getTGT(MY_USERNAME,MY_PASSWORD)
paste("TGT=", str_split(tgt, "/")[[1]][7])
## [1] "TGT= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
Getting a Service Ticket (ST), good for one call. A ST will expire after 5 minutes if not used.
getST <-function(TGT,VERBOSE) {
timeout(15)
AUTH_URI <- paste(tgt[[1]])
ST_RESPONSE <- POST(AUTH_URI,
body = list(service = "http://umlsks.nlm.nih.gov"),
encode = "form")
warn_for_status(ST_RESPONSE)
ST <- content(ST_RESPONSE)
if(VERBOSE){
print(paste("getST: ", http_status(ST_RESPONSE)$message, sep = ""))
print(paste(" st= ", ST, sep = ""))
print(paste(" tgt= ", str_split(tgt, "/")[[1]][7], sep = ""))
}
# no sleeping time neccessary of run at LHC
# random sleep of 5-10ms for each ST
# sleep_time <- round(runif(1, 0.005, 0.010), digits = 3)
# Sys.sleep(sleep_time)
return(ST)
}
paste("st=[", getST(tgt, TRUE),"]",sep="")
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442113-nPt1AMRHxsObj3n0kLc3-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "st=[ST-1442113-nPt1AMRHxsObj3n0kLc3-cas]"
getCUI <-function(STRING,SEARCH_TYP,VERSION,VERBOSE){
if(! SEARCH_TYP %in% c("exact","words","leftTruncation","rightTruncation","approximate","normalizedString")){
warning("Invalid search type")
break
}
if(VERBOSE){
print(paste("START getCUI for [", STRING, "][",SEARCH_TYP,"]", sep = ""))
}
timeout(5)
SEARCH_URI <- paste("https://uts-ws.nlm.nih.gov/rest/search",VERSION, sep = "/")
concepts <- list()
page <- 0
attempt <- 0
MAX_attempt <- 6 # maximum number of tries if a http error occurs [6 is good]
SNOOZE_time <- 10 # time (seconds) of the pause when error occurs [10 is good]
Res <- NULL
sleep_time <- 0 # no sleeping time neccessary of run at LHC
repeat{
page <- (page+1)
concepts_cnt <- length(concepts)
QUERY = list(string = STRING,
ticket = getST(tgt, VERBOSE),
searchType=SEARCH_TYP,
pageNumber=page)
response <- GET(url=SEARCH_URI, query=QUERY)
# no sleeping time neccessary of run at LHC
# sleep_time <- round(runif(1, 0.015, 0.025), digits = 3) # random sleep of 10-25ms
# Sys.sleep(sleep_time)
if(VERBOSE){
print(paste("getCUI: random sleep (sec)= ", sleep_time, sep = ""))
}
if(http_error(response)){
page <- 0
if(attempt<MAX_attempt){
attempt <- (attempt + 1)
sleep_time <- SNOOZE_time*attempt*runif(1,0.5,1)
message_for_status(response, paste("mapping term [", STRING, "]",
"[Attempt #", attempt, "]",
sep = ""))
print(paste("[Now taking a ", sleep_time, " sec break]", sep = ""))
Sys.sleep(sleep_time)
next
}
stop("Too many failures, unable tor perform mapping.")
}
else
{
Res <- content(response)
for(i in 1:length(Res$result$results)){
concepts[[concepts_cnt+i]] <- c(Res$result$results[[i]]$ui,
Res$result$results[[i]]$name,
Res$result$results[[i]]$rootSource,
Res$result$results[[i]]$uri)
}
}
if(Res$result$results[[1]]$ui == "NONE"){
break
}
}
if(length(concepts) > 1){
if(VERBOSE){
print(paste("END getCUI for [", STRING, "][",(length(concepts)-1)," CUI found]", sep = ""))
}
concepts <- concepts[1:(length(concepts)-1)]
}
else
{
if(VERBOSE){
print(paste("END getCUI for [", STRING, "][0 CUI found]", sep = ""))
}
concepts[[1]] <- c("NO_CONCEPT_MAPPED_TO","","","")
}
return(concepts)
}
#
getCUI("fracture of carpal bone","exact","2015AB", TRUE)
## [1] "START getCUI for [fracture of carpal bone][exact]"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442114-zqywcCRkMnD2YSG93Ld5-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442115-S0QKlt9SfE5eTnQoSeHZ-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "END getCUI for [fracture of carpal bone][1 CUI found]"
## [[1]]
## [1] "C0016644"
## [2] "Fracture of carpal bone"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C0016644"
getCUI("fracture of carpal bone","exact","2015AB", FALSE)
## [[1]]
## [1] "C0016644"
## [2] "Fracture of carpal bone"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C0016644"
#
getCUI("fracture of the carpal bone","exact","2015AB", FALSE)
## [[1]]
## [1] "NO_CONCEPT_MAPPED_TO" "" ""
## [4] ""
getCUI("fracture of the carpal bone","normalizedString","2015AB", FALSE)
## [[1]]
## [1] "C0016644"
## [2] "Fracture of carpal bone"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C0016644"
This was tested on 15,162 terms, done 44.12 mins (over NIH Guest wifi)
map_to_umls <-function(STRING,VERSION, VERBOSE){
if(VERBOSE){
print(paste("###==[MAP TO UMLS]==>"))
print(paste("START map_to_umls for [", STRING, "]", sep = ""))
print(paste("--> LOOKING FOR EM FOR [", STRING, "]", sep = ""))
}
MAPPING_list <- list()
MAPPING_typ <- "NO_CONCEPT_MAPPED_TO"
MAPPING_count <- 0
EM_list <- getCUI(STRING,"exact",VERSION, VERBOSE)
if(EM_list[[1]][1] == MAPPING_typ){
if(VERBOSE){
print(paste("----> LOOKING FOR NSI FOR [", STRING, "]", sep = ""))
}
NSI_list <- getCUI(STRING,"normalizedString",VERSION, VERBOSE)
if(NSI_list[[1]][1] != MAPPING_typ){
MAPPING_list <- NSI_list
MAPPING_typ <- "NSI"
MAPPING_count <- length(MAPPING_list)
}
else
{
if(VERBOSE){
print(paste("--X-X-> NO EM NOR NSI FOR [", STRING, "]", sep = ""))
}
MAPPING_list[[1]] <- c("","","","")
}
}
else
{
MAPPING_list <- EM_list
MAPPING_typ <- "EM"
MAPPING_count <- length(MAPPING_list)
}
i<-0
while(i<length(MAPPING_list)){
i<-i+1
MAPPING_list[[i]][5] <- paste(STRING, MAPPING_typ, MAPPING_count,
MAPPING_list[[i]][1], MAPPING_list[[i]][2],
MAPPING_list[[i]][3], sep = "|")
}
if(VERBOSE){
print(paste("END map_to_umls for [", STRING, "]", sep = ""))
}
return(MAPPING_list)
}
# to retrive a list with 1 mapping per line
getMappingSplitList <- function(MAPPINGS){
splitList <- list()
for(i in 1:length(MAPPINGS)){
splitList[i] <- MAPPINGS[[i]][5]
}
return(splitList)
}
# to retrive a list with all mappings on 1 line
getMappingNoSplitList <- function(MAPPINGS){
STRING <- strsplit(MAPPINGS[[1]][5], "|",fixed = TRUE)[[1]][1]
MAPPING_typ <- strsplit(MAPPINGS[[1]][5], "|",fixed = TRUE)[[1]][2]
MAPPING_count <- strsplit(MAPPINGS[[1]][5], "|",fixed = TRUE)[[1]][3]
noSplitList <- paste(STRING,MAPPING_typ,MAPPING_count, sep="|")
if(MAPPING_count == 0){
return(noSplitList)
}
else{
cuis <- character()
names <- character()
for(i in 1:length(MAPPINGS)){
cuis <- paste(cuis, list(MAPPINGS[[i]][1]), sep = "|")
names <- paste(names, list(MAPPINGS[[i]][2]), sep = "|")
}
noSplitList <- paste(noSplitList, cuis, names, sep = "")
return(noSplitList)
}
}
map_to_umls("fetus","2015AB", TRUE)
## [1] "###==[MAP TO UMLS]==>"
## [1] "START map_to_umls for [fetus]"
## [1] "--> LOOKING FOR EM FOR [fetus]"
## [1] "START getCUI for [fetus][exact]"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442121-qop9Mistx63ZJPpf9W5b-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442122-1Y4s6jmSjgPPy44GVBds-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "END getCUI for [fetus][3 CUI found]"
## [1] "END map_to_umls for [fetus]"
## [[1]]
## [1] "C0015965"
## [2] "Fetus"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C0015965"
## [5] "fetus|EM|3|C0015965|Fetus|MTH"
##
## [[2]]
## [1] "C0242291"
## [2] "Fetal Tissue"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C0242291"
## [5] "fetus|EM|3|C0242291|Fetal Tissue|MTH"
##
## [[3]]
## [1] "C1305737"
## [2] "Entire fetus"
## [3] "MTH"
## [4] "https://uts-ws.nlm.nih.gov/rest/content/2015AB/CUI/C1305737"
## [5] "fetus|EM|3|C1305737|Entire fetus|MTH"
map_to_umls("fetal medicine","2015AB", TRUE)
## [1] "###==[MAP TO UMLS]==>"
## [1] "START map_to_umls for [fetal medicine]"
## [1] "--> LOOKING FOR EM FOR [fetal medicine]"
## [1] "START getCUI for [fetal medicine][exact]"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442123-B5pOLyeaVLZ4RFYE9lD1-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "END getCUI for [fetal medicine][0 CUI found]"
## [1] "----> LOOKING FOR NSI FOR [fetal medicine]"
## [1] "START getCUI for [fetal medicine][normalizedString]"
## [1] "getST: Success: (200) OK"
## [1] " st= ST-1442124-TD1JmJKCwzTsKHwj1Poj-cas"
## [1] " tgt= TGT-200264-Il9XrohIp6DKkRcEzYjwLtIbdzO3V6ce31VcF7bUe7d4q2uqNb-cas"
## [1] "getCUI: random sleep (sec)= 0"
## [1] "END getCUI for [fetal medicine][0 CUI found]"
## [1] "--X-X-> NO EM NOR NSI FOR [fetal medicine]"
## [1] "END map_to_umls for [fetal medicine]"
## [[1]]
## [1] ""
## [2] ""
## [3] ""
## [4] ""
## [5] "fetal medicine|NO_CONCEPT_MAPPED_TO|0|||"
s <- "fetus"
mappings <- map_to_umls(s,"2015AB", FALSE)
getMappingSplitList(mappings)
## [[1]]
## [1] "fetus|EM|3|C0015965|Fetus|MTH"
##
## [[2]]
## [1] "fetus|EM|3|C0242291|Fetal Tissue|MTH"
##
## [[3]]
## [1] "fetus|EM|3|C1305737|Entire fetus|MTH"
getMappingNoSplitList(mappings)
## [1] "fetus|EM|3|C0015965|C0242291|C1305737|Fetus|Fetal Tissue|Entire fetus"
sessionInfo()
## R version 3.2.3 (2015-12-10)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.11.3 (El Capitan)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] stringr_1.0.0 knitr_1.12.3 xml2_0.1.2 httr_1.1.0
##
## loaded via a namespace (and not attached):
## [1] R6_2.1.1 magrittr_1.5 formatR_1.2.1 tools_3.2.3
## [5] htmltools_0.2.6 curl_0.9.6 yaml_2.1.13 Rcpp_0.12.1
## [9] stringi_0.5-5 rmarkdown_0.9.5 jsonlite_0.9.19 digest_0.6.8
## [13] evaluate_0.8