/
extractors.R
200 lines (177 loc) · 4.13 KB
/
extractors.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
#' Extract parts from webchem objects
#' @name extractors
#' @rdname extractors
#' @param x object
#' @param ... currently not used.
#' @return a vector.
#' @references Eduard Szöcs, Tamás Stirling, Eric R. Scott, Andreas Scharmüller,
#' Ralf B. Schäfer (2020). webchem: An R Package to Retrieve Chemical
#' Information from the Web. Journal of Statistical Software, 93(13).
#' \doi{10.18637/jss.v093.i13}.
#' @export
cas <- function(x, ...){
UseMethod("cas")
}
# CAS ---------------------------------------------------------------------
#' @export
cas.default <- function(x, ...) {
stop(paste("No cas method for class", class(x)))
}
#' @export
cas.chebi_comp_entity <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y)) return(NA)
unique(y$regnumbers$data[y$regnumbers$type == "CAS Registry Number"])
})
}
#' @export
cas.opsin_query <- function(x, ...) {
stop("CAS is not returned by this datasource!")
}
#' @export
cas.pan_query <- function(x, ...) {
sapply(x, function(y) y$`CAS Number`)
}
#' @export
cas.aw_query <- function(x, ...) {
sapply(x, function(y) y$cas)
}
#' @export
cas.wd_ident <- function(x, ...) {
x$cas
}
#' @export
cas.cts_compinfo <- function(x, ...) {
stop("CAS is not returned by this data source")
}
#' @export
cas.etox_basic <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y))
return(NA)
unique(y$cas)
})
}
#' @export
cas.ci_query <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y))
return(NA)
unique(y$cas)
})
}
# InChIKey ----------------------------------------------------------------
#' @rdname extractors
#' @export
inchikey <- function(x, ...){
UseMethod("inchikey")
}
#' @export
inchikey.default <- function(x, ...) {
stop(paste(" No inchikey method for class", class(x)))
}
#' @export
inchikey.aw_query <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y)) return(NA)
y$inchikey
})
}
#' @export
inchikey.chebi_comp_entity <- function(x, ...) {
sapply(x, function (y) {
if (length(y) == 1 && is.na(y)) return(NA)
y$properties$inchikey
})
}
#' @export
inchikey.etox_basic <- function(x, ...) {
stop("InChIkey is not returned by this datasource!")
}
#' @export
inchikey.pan_query <- function(x, ...) {
stop("InChIkey is not returned by this datasource!")
}
#' @export
inchikey.opsin_query <- function(x, ...) {
x$stdinchikey
}
#' @export
inchikey.pc_prop <- function(x, ...) {
if (!"InChIKey" %in% names(x)) {
stop("InChIKey not queried!")
}
x$InChIKey
}
#' @export
inchikey.wd_ident <- function(x, ...) {
x$inchikey
}
#' @export
inchikey.cts_compinfo <- function(x, ...) {
sapply(x, function(x) x$inchikey)
}
#' @export
inchikey.ci_query <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y))
return(NA)
unique(y$inchikey)
})
}
# SMILES ------------------------------------------------------------------
#' @rdname extractors
#' @export
smiles <- function(x, ...){
UseMethod("smiles")
}
#' @export
smiles.default <- function(x, ...) {
stop(paste("no smiles method for class", class(x)))
}
#' @export
smiles.chebi_comp_entity <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y)) return(NA)
y$properties$smiles
})
}
#' @export
smiles.cts_compinfo <- function(x, ...) {
stop("SMILES is not returned by this datasource!")
}
#' @export
smiles.etox_basic <- function(x, ...) {
stop("InChIkey is not returned by this datasource!")
}
#' @export
smiles.pan_query <- function(x, ...) {
stop("SMILES is not returned by this datasource!")
}
#' @export
smiles.opsin_query <- function(x, ...) {
x$smiles
}
#' @export
smiles.aw_query <- function(x, ...) {
stop("SMILES is not returned by this datasource!")
}
#' @export
smiles.pc_prop <- function(x, ...) {
if (!"CanonicalSMILES" %in% names(x)) {
stop("CanonicalSMILES not queried!")
}
x$CanonicalSMILES
}
#' @export
smiles.wd_ident <- function(x, ...) {
x$smiles
}
#' @export
smiles.ci_query <- function(x, ...) {
sapply(x, function(y) {
if (length(y) == 1 && is.na(y))
return(NA)
unique(y$smiles)
})
}