-
Notifications
You must be signed in to change notification settings - Fork 9
/
README.Rmd
270 lines (191 loc) · 7.62 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---
output:
html_document:
highlight: tango
keep_md: yes
theme: readable
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, message=FALSE,warning=FALSE)
```
# ![CTSgetR](https://github.com/dgrapov/CTSgetR/blob/master/inst/ctsgetR_logo.png?raw=true)
<hr>
## R interface to the [Chemical Translation Service (CTS)](http://cts.fiehnlab.ucdavis.edu/)
<hr>
#### `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.
`CTSgetR` adds robust support for translation `to` and `from` `Chemical names` based intermediate translation to `InChIKey`
first.
### This package supports metabolite identifier translation:
* ### [in R](#in-R)
* ### deployed as an [opencpu](https://www.opencpu.org/) [API](#API)
* ### from a [shiny UI](#shiny) using asynchronous local or API calls
<a name="in-R"></a>
> ### `using R`
#### Installation
```{r, message=FALSE,warning=FALSE,eval=FALSE}
install_github("dgrapov/CTSgetR")
```
#### Make sure CTS API is available
```{r}
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!') }}
```
#### View some of the possible translation options between > 200 databases
```{r}
trans<-unlist(valid_from())
head(trans,10)
```
#### Find a database of interest
```{r}
want<-'CID'
trans[grepl(want,trans,ignore.case=TRUE)]
```
#### Initialize a local database to speed up routine queries
```{r,eval=FALSE}
db_name<-'ctsgetr.sqlite'
init_CTSgetR_db(db_name)
db_stats()
```
#### Translation examples
#### `Chemical Name` to `InChIKey`
```{r,message=FALSE}
db_name<-'ctsgetr.sqlite' # local cache
id<-c("alanine",'lactic acid')
from<-"Chemical Name"
to<-"InChIKey"
CTSgetR(id,from,to,db_name=db_name)
```
#### One identifier to many
##### The example below shows the alternative `data.frame` input format for more complex queries.
```{r}
id<-c("alanine",'lactic acid')
from<-"Chemical Name"
to<- c( "PubChem CID", "KEGG","Human Metabolome Database")
CTSgetR(id,from,to,db_name=db_name)
```
#### Many identifiers to one
##### Build up complex queries by combining data frames of `id`, `from` to `to` values.
```{r}
#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
args %>%
split(.,.$from) %>%
map(~CTSgetR(.$id,.$from,.$to,db_name=db_name)) %>%
bind_rows(.)
```
<hr>
<a name="API"></a>
> ## Deploy `CTSgetR` as a `docker`ized `API`
<hr>
### The following [docker](https://hub.docker.com/r/dgrapov/ctsgetr/dockerfile) image and [docker-compose](https://github.com/dgrapov/CTSgetR/blob/master/docker-compose.yml) commands can be used to `build` and run the `CTSgetR` package as an [opencpu](https://hub.docker.com/r/opencpu/ubuntu-18.04) based `API`.
* #### [CTSgetR image](https://hub.docker.com/r/dgrapov/ctsgetr)
* #### [ocpuclient](https://github.com/dgrapov/ocpuclient): client library for accessign `CTSgetR` `API`
### The `CTSgetR` image contains an opencpu and Rstudio server
* #### `localhost/ocpu/`: [opencpu-server](https://www.opencpu.org/)
* #### `localhost/rstudio/` : [rstudio server](https://hub.docker.com/r/opencpu/rstudio) (use user: opencpu and password:<mypassword> )
## Build docker `image`
#### `build`
```
export rstudio_pass=mypassword # rstudio server password for user opencpu
docker-compose -f docker-compose.yml build --force-rm
```
### Launch API
```
#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
```
### Test API endpoints
#### `bash`
```bash
curl http://localhost/ocpu/library/CTSgetR/R/heartbeat
```
#### `R`
##### heartbeat
```r
library(ocpuclient)
base_url<-'http://localhost/ocpu/'
endpoint<-'library/CTSgetR/R/heartbeat'
url<-paste0(base_url,endpoint)
post_ocpu(url=url)
```
##### translation
```r
#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)
```
<a name="shiny"></a>
> ## Launch `shiny` UI using asynchronous `opencpu` API
#### 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.
```r
library(shiny)
library(tippy)
library(CTSgetR) # local calls
library(ocpuclient) # CTSgetR opencpu API calls
```
#### Specify local database or API options
```r
#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
````
#### User input translations
```r
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)
```
#### Connect to other shiny components
```r
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)
```