/
getBeIds.R
349 lines (340 loc) · 9.18 KB
/
getBeIds.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#' Get biological entities identifiers
#'
#' @param be one BE or "Probe"
#' @param source the BE ID database or "Symbol" if BE or
#' the probe platform if Probe
#' @param organism organism name
#' @param restricted boolean indicating if the results should be restricted to
#' current version of to BEID db. If FALSE former BEID are also returned.
#' @param entity boolean indicating if the technical ID of BE should be
#' returned
#' @param attributes a character vector listing attributes that should be
#' returned.
#' @param verbose boolean indicating if the CQL query should be displayed
#' @param recache boolean indicating if the CQL query should be run even if
#' the table is already in cache
#' @param filter character vector on which to filter id. If NULL (default),
#' the result is not filtered: all IDs are taken into account.
#' @param caseSensitive if TRUE the case of provided symbols
#' is taken into account.
#' This option will only affect "Symbol" source
#' (default: caseSensitive=FALSE).
#' @param limForCache if there are more filter than limForCache results are
#' collected for all IDs (beyond provided ids) and cached for futur queries.
#' If not, results are collected only for provided ids and not cached.
#' @param bef For internal use only
#'
#' @return a data.frame mapping BE IDs with the
#' following fields:
#'
#' - **id**: the BE ID
#' - **BE**: IF entity is TRUE the technical ID of BE
#' - **db.version**: IF be is not "Probe" and source not "Symbol"
#' the version of the DB
#' - **db.deprecated**: IF be is not "Probe" and source not "Symbol"
#' a value if the BE ID is deprecated or FALSE if it's not
#' - **canonical**: IF source is "Symbol" TRUE if the symbol is canonical
#' - **organism**: IF be is "Probe" the organism of the targeted BE
#'
#' If attributes are part of the query, additional columns for each of them.
#' Scope ("be", "source" and "organism") is provided as a named list
#' in the "scope" attributes: `attr(x, "scope")`
#'
#' @examples \dontrun{
#' beids <- getBeIds(be="Gene", source="EntrezGene", organism="human", restricted=TRUE)
#' }
#'
#' @seealso [listPlatforms], [listBeIdSources]
#'
#' @export
#'
getBeIds <- function(
be=c(listBe(), "Probe"),
source,
organism=NA,
restricted,
entity=TRUE,
attributes=NULL,
verbose=FALSE,
recache=FALSE,
filter=NULL,
caseSensitive=FALSE,
limForCache=100,
bef=NULL
){
fn <- sub(
sprintf("^%s[:][::]", utils::packageName()), "",
sub("[(].*$", "", deparse(sys.call(), nlines=1, width.cutoff=500L))
)
## Other verifications
bentity <- match.arg(be)
bedSources <- c(
getAllBeIdSources(recache=recache)$database,
listPlatforms()$name,
"Symbol"
)
# source <- match.arg(source, bedSources)
if(!is.atomic(source) || length(source)!=1){
stop("source should be a character vector of length one")
}
if(!is.logical(restricted) || length(restricted)!=1){
stop("restricted should be a logical vector of length one")
}
if(!is.logical(entity) || length(entity)!=1){
stop("entity should be a logical vector of length one")
}
if(!is.logical(verbose) || length(verbose)!=1){
stop("verbose should be a logical vector of length one")
}
if(!is.logical(recache) || length(recache)!=1){
stop("recache should be a logical vector of length one")
}
if(be=="Probe" || source=="Symbol"){
attributes <- NULL
}else{
attributes <- intersect(attributes, listDBAttributes(source))
}
## Filter
noCache <- FALSE
if(length(filter)>0){
if(length(bef)>0){
stop("Cannot set filter and bef together")
}
if(!inherits(filter, "character")){
stop("filter should be a character vector")
}
if(source=="Symbol" & !caseSensitive){
filter <- toupper(filter)
}
FILT <- "beid"
if(length(filter)<=limForCache){
noCache <- TRUE
}
}
## Filter
if(length(bef)>0){
FILT <- "be"
if(length(bef)<=limForCache){
noCache <- TRUE
}
}
## Organism
if(!is.atomic(organism) || length(organism)!=1){
stop("organism should be a character vector of length one")
}
# if(is.na(organism) && bentity!="Probe"){
# stop("organism should be given for non 'Probe' entities")
# }
# if(!is.na(organism) && bentity=="Probe"){
# warning("organism won't be taken into account to retrieve probes")
# organism <- NA
# }
if(is.na(organism)){
taxId <- NA
}else{
taxId <- getTaxId(name=organism)
if(length(taxId)==0){
stop("organism not found")
}
if(length(taxId)>1){
print(getOrgNames(taxId))
stop("Multiple TaxIDs match organism")
}
}
parameters <- list(
taxId=taxId,
beSource=source
)
if(noCache){
if(FILT=="beid"){
parameters$filter <- as.list(as.character(filter))
}else{
parameters$filter <- as.list(bef)
}
}
if(bentity != "Probe"){
be <- bentity
beid <- paste0(be, "ID")
if(source=="Symbol"){
cql <- c(
sprintf(
'MATCH (n:BESymbol)<-[ika:is_known_as]-(:%s)',
beid
)
)
}else{
cql <- sprintf('MATCH (n:%s {database:$beSource})', beid)
}
cql <- c(
cql,
ifelse(
restricted,
'-[:is_associated_to*0..]->',
'-[:is_replaced_by|is_associated_to*0..]->'
),
'(ni)',
'-[:identifies]->(be)'
)
}else{
qs <- genProbePath(platform=source)
be <- attr(qs, "be")
cql <- paste0(
'MATCH (n:ProbeID {platform:$beSource})',
qs,
sprintf('(be:%s)', be)
)
}
cql <- c(
cql,
ifelse(
be=="Gene",
'',
ifelse(
!is.na(organism) || bentity=="Probe",
'<-[cr:codes_for|is_expressed_as|is_translated_in*1..2]-(g:Gene)',
''
)
),
# ifelse(
# bentity!="Probe",
# ifelse(
# !is.na(organism),
# '-[:belongs_to]->(:TaxID {value:$taxId})',
# ''
# ),
# '-[:belongs_to]->()-[:is_named {nameClass:"scientific name"}]->(o)'
# ),
ifelse(
!is.na(organism),
'-[:belongs_to]->(:TaxID {value:$taxId})',
''
),
ifelse(
noCache,
ifelse(
FILT=="beid",
sprintf(
'WHERE n.%s IN $filter',
ifelse(
source=="Symbol" & !caseSensitive,
"value_up",
"value"
)
),
'WHERE id(be) IN $filter'
),
""
),
ifelse(
bentity!="Probe" && source!="Symbol",
'OPTIONAL MATCH (n)-[db:is_recorded_in]->()',
''
)
)
##
for(at in attributes){
cql <- c(
cql,
sprintf(
'OPTIONAL MATCH (n)-[%s:has]->(:Attribute {name:"%s"})',
gsub("[^[:alnum:] ]", "_", at), at
)
)
}
##
cql <- c(
cql,
'RETURN DISTINCT n.value as id, n.preferred as preferred',
sprintf(
', id(be) as %s',
be
),
ifelse(
bentity!="Probe" && source!="Symbol",
', db.version, db.deprecated',
''
)#,
# ifelse(
# bentity=="Probe",
# # is.na(organism),
# ', o.value as organism',
# ''
# )
)
if(source=="Symbol"){
cql <- c(
cql,
", ika.canonical as canonical"
)
}
##
for(at in attributes){
cql <- c(
cql,
sprintf(
', %s.value as %s',
gsub("[^[:alnum:] ]", "_", at), gsub("[^[:alnum:] ]", "_", at)
)
)
}
if(verbose){
message(neo2R::prepCql(cql))
}
##
if(noCache){
toRet <- bedCall(
f=neo2R::cypher,
query=neo2R::prepCql(cql),
parameters=parameters
)
}else{
tn <- gsub(
"[^[:alnum:]]", "_",
paste(
fn,
bentity, source,
taxId,
ifelse(restricted, "restricted", "full"),
paste(attributes, collapse="-"),
sep="_"
)
)
toRet <- cacheBedCall(
f=neo2R::cypher,
query=neo2R::prepCql(cql),
tn=tn,
recache=recache,
parameters=parameters
)
}
toRet <- unique(toRet)
if(length(filter)>0 & !is.null(toRet)){
.data <- NULL
if(source=="Symbol" & !caseSensitive){
toRet <- dplyr::filter(
toRet,
toupper(.data$id) %in% filter
)
}else{
toRet <- dplyr::filter(
toRet,
.data$id %in% filter
)
}
}
if(length(bef)>0 & !is.null(toRet)){
toRet <- dplyr::filter(toRet, get(!!be) %in% !!bef)
}
##
if(!is.null(toRet)){
toRet$preferred <- as.logical(toRet$preferred)
toRet <- dplyr::arrange(toRet, get(!!be))
##
if(!entity){
toRet <- dplyr::distinct(dplyr::select(toRet, -!!be, -"preferred"))
}
attr(toRet, "scope") <- list(be=be, source=source, organism=organism)
}
##
return(toRet)
}