output | ||||||||
---|---|---|---|---|---|---|---|---|
|
R interface to the Chemical Translation Service (CTS)
CTSgetR
provides a consitent interface to translation of chemical names and over 200 database identifiers including InChIKey
, HMDB
, KEGG
and PubChem
. Translation of chemical names is hard. Use CTSgetR
to robustly translate chemical names to other identifiers through 1) conversion to InChIKey
2) biological
or popularity
scoring and 3) translation to over 200 biological database identifiers. CTSgetR
uses a sqlite database to cache and speed all of your routine translations.
-
from a shiny UI using asynchronous local or API calls
install_github("dgrapov/CTSgetR")
library(CTSgetR)
GET('https://cts.fiehnlab.ucdavis.edu/services') %>%
http_status(.) %>%
{if( .$category != 'Success'){stop('Oops looks like https://cts.fiehnlab.ucdavis.edu/services is down!') }}
trans<-unlist(valid_from())
head(trans,10)
## [1] "BioCyc" "CAS"
## [3] "ChEBI" "Chemical Name"
## [5] "Human Metabolome Database" "InChIKey"
## [7] "KEGG" "LMSD"
## [9] "LipidMAPS" "PubChem CID"
want<-'CID'
trans[grepl(want,trans,ignore.case=TRUE)]
## [1] "PubChem CID"
db_name<-'ctsgetr.sqlite'
init_CTSgetR_db(db_name)
db_stats()
db_name<-'ctsgetr.sqlite' # local cache
id<-c("alanine",'lactic acid')
from<-"Chemical Name"
to<-"InChIKey"
CTSgetR(id,from,to,db_name=db_name)
## id InChIKey
## 1 alanine QNAYBMKLOCPYGJ-REOHCLBHSA-N
## 2 lactic acid JVTAAEKCZFNVCJ-UHFFFAOYSA-N
id<-c("alanine",'lactic acid')
from<-"Chemical Name"
to<- c( "PubChem CID", "KEGG","Human Metabolome Database")
CTSgetR(id,from,to,db_name=db_name)
## id Human Metabolome Database KEGG PubChem CID
## 1 alanine HMDB0000161 C00041 5950
## 2 lactic acid HMDB0144295 C01432 19789253
#from many to many
args <-structure(list(id = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 4L, 4L), .Label = c("alanine", "foo", "lactic acid", "HMDB0000161"
), class = "factor"), from = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L), .Label = c("Chemical Name", "Human Metabolome Database"
), class = "factor"), to = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 2L, 1L), .Label = c("PubChem CID", "KEGG", "Human Metabolome Database"
), class = "factor")), class = "data.frame", row.names = c(NA,
-11L))
args
## id from to
## 1 alanine Chemical Name PubChem CID
## 2 foo Chemical Name PubChem CID
## 3 lactic acid Chemical Name PubChem CID
## 4 alanine Chemical Name KEGG
## 5 foo Chemical Name KEGG
## 6 lactic acid Chemical Name KEGG
## 7 alanine Chemical Name Human Metabolome Database
## 8 foo Chemical Name Human Metabolome Database
## 9 lactic acid Chemical Name Human Metabolome Database
## 10 HMDB0000161 Human Metabolome Database KEGG
## 11 HMDB0000161 Human Metabolome Database PubChem CID
args %>%
split(.,.$from) %>%
map(~CTSgetR(.$id,.$from,.$to,db_name=db_name)) %>%
bind_rows(.)
## id Human Metabolome Database KEGG PubChem CID
## 1 alanine HMDB0000161 C00041 5950
## 2 foo <NA> <NA> <NA>
## 3 lactic acid HMDB0144295 C01432 19789253
## 4 HMDB0000161 <NA> C00041 5950
The following docker image and docker-compose commands can be used to build
and run the CTSgetR
package as an opencpu based API
.
-
ocpuclient: client library for accessign
CTSgetR
API
-
localhost/ocpu/
: opencpu-server -
localhost/rstudio/
: rstudio server (use user: opencpu and password: )
export rstudio_pass=mypassword # rstudio server password for user opencpu
docker-compose -f docker-compose.yml build --force-rm
#mount to persist internal sqlite DB between updates
export ctsgetr_db_mount=<local path to save database e.g. /mypath>
docker-compose -f docker-compose.yml up -d
curl http://localhost/ocpu/library/CTSgetR/R/heartbeat
library(ocpuclient)
base_url<-'http://localhost/ocpu/'
endpoint<-'library/CTSgetR/R/heartbeat'
url<-paste0(base_url,endpoint)
post_ocpu(url=url)
#translate
endpoint<-'library/CTSgetR/R/CTSgetR'
url<-paste0(base_url,endpoint)
id <-
c("C15973",
"C00026")
from <- "KEGG"
to <- "PubChem CID"
body<-list(id=id,from=from,to=to,db_name=db_name)
post_ocpu(url=url,body=body)
The following example shows a how to use a shiny
module combined with futures
and promises
R
packages to connect to an opencpu
API uisng async calls.
library(shiny)
library(tippy)
library(CTSgetR) # local calls
library(ocpuclient) # CTSgetR opencpu API calls
#one of local
Sys.setenv('ctsgetr_DB'='inst/ctsgetr.sqlite') #see section `in R` showing how to initialize a local databse
#or API
Sys.setenv('ctsgetr_DB'='/ctsgetr/inst/ctsgetr.sqlite') # in API docker for mount
Sys.setenv('CTSgetR_API'='http://localhost/ocpu/library/CTSgetR/R/CTSgetR') # url of API endpoint
library(promises)
library(future)
plan(multisession)
#module
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(tagList(mod_CTSgetR_ui("translate"))),
mainPanel(verbatimTextOutput("main_out")))
)
server <- function(input, output, session) {
translation <- mod_CTSgetR_server('translate')
output$main_out <- renderPrint({
translation() %...>% print(.)
})
}
shinyApp(ui, server)
library(promises)
library(future)
plan(multisession)
#make `example` a reactive returning a data frame to update dynamically
example<-data.frame('chemical_name' = c('alanine','Pyruvic acid'))
#module
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(tagList(mod_CTSgetR_ui("translate"))),
mainPanel(verbatimTextOutput("main_out")))
)
server <- function(input, output, session) {
translation <- mod_CTSgetR_server('translate',data=example)
output$main_out <- renderPrint({
translation() %...>% print(.)
})
}
shinyApp(ui, server)