-
Notifications
You must be signed in to change notification settings - Fork 40
/
chemid.R
221 lines (203 loc) · 7.87 KB
/
chemid.R
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
#' Retrieve information from ChemIDPlus \url{http://chem.sis.nlm.nih.gov/chemidplus}
#'
#' Retrieve information from ChemIDPlus \url{http://chem.sis.nlm.nih.gov/chemidplus}
#'
#' @import xml2
#' @importFrom rvest html_table
#' @importFrom stats rgamma
#' @importFrom utils URLencode URLdecode
#'
#' @param query character; query string
#' @param type character; type of query string.
#' 'rn' for regeistry number or 'name' for common name or 'inchikey' for inchikey as input.
#' @param match character; How should multiple hits be handeled?
#' 'first' returns only the first match, 'best' the best matching (by name) ID, 'ask' is a interactive mode and the user is asked for input,
#' 'na' returns NA if multiple hits are found.
#' @param verbose logical; should a verbose output be printed on the console?
#' @return A list of 8 entries: name (vector), synonyms (vector), cas (vector),
#' inchi (vector), inchikey (vector), smiles(vector), toxicity (data.frame),
#' physprop (data.frame) and source_url.
#'
#' @note The data of the entry \code{pp_query} is identical to the result returned
#' by \code{\link{pp_query}}.
#'
#' @export
#' @examples
#' \dontrun{
#' # might fail if API is not available
#' # query common name
#' y1 <- ci_query(c('Formaldehyde', 'Triclosan'), type = 'name')
#' names(y1)
#' str(y1[['Triclosan']]) # lots of information inside
#' y1[['Triclosan']]$inchikey
#'
#' # Query by CAS
#' y2 <- ci_query('50-00-0', type = 'rn', match = 'first')
#' y2[['50-00-0']]$inchikey
#'
#' # query by inchikey
#' y3 <- ci_query('WSFSSNUMVMOOMR-UHFFFAOYSA-N', type = 'inchikey')
#' y3[[1]]$name
#'
#' # extract lop-P
#'sapply(y1, function(y){
#' if (length(y) == 1 && is.na(y))
#' return(NA)
#' y$physprop$Value[y$physprop$`Physical Property` == 'log P (octanol-water)']
#' })
#' }
ci_query <- function(query, type = c('name', 'rn', 'inchikey'),
match = c('best', 'first', 'ask', 'na'),
verbose = TRUE){
type <- match.arg(type)
match <- match.arg(match)
foo <- function(query, type, match, verbose){
if (is.na(query)) {
message('query is NA! Returning NA.\n')
return(NA)
}
query <- URLencode(query)
baseurl <- switch(type,
rn = 'https://chem.sis.nlm.nih.gov/chemidplus/rn/',
name = 'https://chem.sis.nlm.nih.gov/chemidplus/name/startswith/',
inchikey = 'https://chem.sis.nlm.nih.gov/chemidplus/inchikey/')
# return max 50 hits
qurl <- paste0(baseurl, query, '?DT_START_ROW=0&DT_ROWS_PER_PAGE=50')
if (verbose)
message(qurl)
Sys.sleep( rgamma(1, shape = 15, scale = 1/10))
ttt <- try(read_html(qurl), silent = TRUE)
if (inherits(ttt, 'try-error')) {
message('Not found! Returning NA.\n')
return(NA)
}
tit <- xml_text(xml_find_all(ttt, "//head/title"))
no <- xml_text(xml_find_all(ttt, "//h3"))
if (length(no) != 0 && no == 'The following query produced no records:') {
message('Not found! Returning NA.\n')
return(NA)
}
# handle multiple inputs
if (grepl('^ChemIDplus Results - Chemical information', x = tit)) {
if (verbose)
message("More then one Link found. \n")
hit_names <- xml_text(xml_find_all(ttt, "//a[@title='Open record details']"))
hit_cas <- xml_text(xml_find_all(ttt, "//a[@title='Open record details']/following-sibling::text()[1]"))
# exclude missing cas
trm <- nchar(hit_cas) < 5
hit_cas <- hit_cas[!trm]
hit_names <- hit_names[!trm]
if (match == 'first') {
if (verbose)
message("Returning first match. \n")
hit_cas <- hit_cas[1]
matched_sub <- hit_names[1]
d <- 'first'
}
if (match == 'best') {
if (verbose)
message("Returning best match. \n")
hit_names <- gsub(' \\[.*\\]', '', hit_names)
dd <- adist(URLdecode(query), hit_names) / nchar(hit_names)
hit_cas <- hit_cas[which.min(dd)]
matched_sub <- hit_names[which.min(dd)]
d <- dd[which.min(dd)]
}
if (match == 'na') {
if (verbose)
message("Returning NA. \n")
return(NA)
}
if (match == 'ask') {
tochoose <- data.frame(name = hit_names, cas = hit_cas)
print(tochoose)
message("\nEnter rownumber of compounds (other inputs will return 'NA'):\n") # prompt
take <- as.numeric(scan(n = 1, quiet = TRUE))
if (length(take) == 0) {
return(NA)
}
if (take %in% seq_len(nrow(tochoose))) {
hit_cas <- hit_cas[take]
matched_sub <- hit_names[take]
d <- 'interactive'
}
}
# check hit
if (is.na(hit_cas)) {
if (verbose)
message('CAS not found! Returning NA.\n')
return(NA)
}
# retry with CAS-API
qurl <- paste0('http://chem.sis.nlm.nih.gov/chemidplus/rn/', hit_cas)
if (verbose)
message(qurl)
Sys.sleep( rgamma(1, shape = 15, scale = 1/10))
ttt <- try(read_html(qurl), silent = TRUE)
source_url <- qurl
} else {
d <- 'direct match'
matched_sub <- xml_text(xml_find_all(ttt, "//h3[contains(., 'Name of Substance')]/following-sibling::div[1]//li"))[1]
source_url <- gsub('^(.*)\\?.*', '\\1', qurl)
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'Name of Substance')]/following-sibling::div[1]//li"))){
name <- NA
}else{
name <- xml_text(xml_find_all(ttt, "//h3[contains(., 'Name of Substance')]/following-sibling::div[1]//li"))
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'Synonyms')]/following-sibling::div[1]//li"))){
synonyms <- NA
}else{
synonyms <- xml_text(xml_find_all(ttt, "//h3[contains(., 'Synonyms')]/following-sibling::div[1]//li"))
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'CAS Registry')]/following-sibling::ul[1]//li"))){
cas <- NA
} else {
cas <- xml_text(xml_find_all(ttt, "//h3[contains(., 'CAS Registry')]/following-sibling::ul[1]//li"))
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'InChI')]/following-sibling::text()[1]"))){
inchi <- NA
} else {
inchi <- gsub('\\n|\\t', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'InChI')]/following-sibling::text()[1]"))[1]
)
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'InChIKey')]/following-sibling::text()[1]"))){
inchikey <- NA
} else {
inchikey <- gsub('\\n|\\t|\\r', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'InChIKey')]/following-sibling::text()[1]"))
)
}
if(is.na(xml_find_first(ttt, "//h3[contains(., 'Smiles')]/following-sibling::text()[1]"))){
smiles <- NA
} else {
smiles <- gsub('\\n|\\t|\\r', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'Smiles')]/following-sibling::text()[1]"))
)
}
if(is.na(xml_find_first(ttt, "//h2[contains(., 'Toxicity')]/following-sibling::div//table"))){
toxicity <- NA
} else {
toxicity <- html_table(xml_find_all(ttt, "//h2[contains(., 'Toxicity')]/following-sibling::div//table"))[[1]]
}
if(is.na(xml_find_first(ttt, "//h2[contains(., 'Physical Prop')]/following-sibling::div//table"))){
physprop <- NA
} else {
physprop <- html_table(xml_find_all(ttt, "//h2[contains(., 'Physical Prop')]/following-sibling::div//table"))[[1]]
physprop[ , 'Value'] <- as.numeric(physprop[ , 'Value'])
#= same as physprop
}
out <- list(name = name, synonyms = synonyms, cas = cas, inchi = inchi,
inchikey = inchikey, smiles = smiles, toxicity = toxicity,
physprop = physprop, source_url = source_url)
attr(out, "matched") <- matched_sub
attr(out, "distance") <- d
class(out) <- 'chemid'
return(out)
}
out <- lapply(query, foo, type = type, match = match, verbose = verbose)
out <- setNames(out, query)
class(out) <- c('list', 'ci_query')
return(out)
}