/
beIDsToAllScopes.R
186 lines (184 loc) · 5.93 KB
/
beIDsToAllScopes.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
#' Find all BEID and ProbeID corresponding to a BE
#'
#' @param beids a character vector of gene identifiers
#' @param be one BE. **Guessed if not provided**
#' @param source the source of gene identifiers. **Guessed if not provided**
#' @param organism the gene organism. **Guessed if not provided**
#' @param entities a numeric vector of gene entity. If NULL (default),
#' beids, source and organism arguments are used to identify BEs.
#' Be carefull when using entities as these identifiers are not stable.
#' @param canonical_symbols return only canonical symbols (default: TRUE).
#'
#' @return A data.frame with the following fields:
#'
#' - **value**: the identifier
#' - **be**: the type of BE
#' - **source**: the source of the identifier
#' - **organism**: the BE organism
#' - **symbol**: canonical symbol of the identifier
#' - **BE_entity**: the BE entity input
#' - **BEID** (optional): the BE ID input
#' - **BE_source** (optional): the BE source input
#'
#' @export
#'
beIDsToAllScopes <- function(
beids, be, source, organism, entities=NULL, canonical_symbols=TRUE
){
if(is.null(entities)){
stopifnot(
is.character(beids), all(!is.na(beids)), length(beids)>0
)
##
if(missing(be) || missing(source) || missing(organism)){
toWarn <- TRUE
}else{
toWarn <- FALSE
}
guess <- guessIdScope(ids=beids, be=be, source=source, organism=organism)
if(is.null(guess)){
warning("Could not find the provided ids")
if(missing(be) || missing(source) || missing(organism)){
stop("Missing be, source or organism information")
}
}else{
if(is.na(guess$be)){
warning(
"The provided ids does not match the provided scope",
" (be, source or organism)"
)
if(missing(be) || missing(source) || missing(organism)){
stop("Missing be, source or organism information")
}
}else{
be <- guess$be
source <- guess$source
organism <- guess$organism
}
}
if(toWarn){
warning(
"Guessing ID scope:",
sprintf("\n - be: %s", be),
sprintf("\n - source: %s", source),
sprintf("\n - organism: %s", organism)
)
}
##
if(source=="Symbol"){
stop('source cannot be "Symbol"')
}
query <- sprintf(
paste(
'MATCH (bid:%s {%s:"%s"})',
'-[:is_associated_to|is_replaced_by|targets*0..]->()',
'-[:identifies]->(be)',
'<-[:is_expressed_as|is_translated_in|codes_for*0..2]-(:Gene)',
'-[:belongs_to]->(tid:TaxID)',
'-[:is_named]->(o:OrganismName {value_up:"%s"})',
'WHERE bid.value IN $ids'
),
paste0(be, "ID"), ifelse(be=="Probe", "platform", "database"),
source,
toupper(organism)
)
ids <- beids
}else{
stopifnot(
is.numeric(entities), all(!is.na(entities)), length(entities)>0
)
warning(
'Be carefull when using entities as these identifiers are ',
'not stable.'
)
query <- paste(
'MATCH (be)',
'<-[:is_expressed_as|is_translated_in|codes_for*0..2]-(:Gene)',
'-[:belongs_to]->(tid:TaxID)',
'WHERE id(be) IN $ids'
)
ids <- unique(entities)
}
query <- paste(
query,
'MATCH (be)',
'<-[:identifies]-()<-[:is_associated_to|is_replaced_by|targets*0..]-',
'(beid)',
'MATCH (tid)',
'-[:is_named {nameClass:"scientific name"}]->(beo:OrganismName)',
sprintf(
'OPTIONAL MATCH (beid)-[k:is_known_as%s]->(bes)',
ifelse(canonical_symbols, " {canonical:true}", "")
),
'RETURN DISTINCT',
'beid.value as value, labels(beid) as be,',
'beid.database as db, beid.platform as pl,',
'beid.preferred as preferred,',
'bes.value as bes,',
'k.canonical as canBes,',
'beo.value as organism,',
'id(be) as BE_entity'
)
if(is.null(entities)){
query <- paste(
query,
', bid.value as BEID,',
sprintf(
'bid.%s as BE_source',
ifelse(be=="Probe", "platform", "database"), be
)
)
}
toRet <- bedCall(
neo2R::cypher, query=query, parameters=list(ids=as.list(ids))
)
if(!is.null(toRet)){
toRet <- dplyr::mutate(
toRet,
"be"=stringr::str_remove(
stringr::str_remove(toRet$be, "BEID [|][|] "),
"ID$"),
"source"=ifelse(is.na(toRet$db), toRet$pl, toRet$db)
)
toRet <- dplyr::select(toRet, -"db", -"pl")
##
toRet1 <- dplyr::arrange(toRet, desc(.data$canBes))
toRet1 <- dplyr::group_by(
toRet1,
.data$value, .data$preferred, .data$be,
.data$source, .data$organism, .data$BE_entity
)
toRet1 <- dplyr::summarise_all(toRet1, function(x)x[1])
toRet1 <- dplyr::select(toRet1, -"canBes")
toRet1 <- dplyr::ungroup(toRet1)
# toRet1 <- dplyr::distinct(dplyr::select(toRet, "bes"))
toRet2 <- dplyr::select(toRet, -"value", -"preferred")
toRet2 <- dplyr::filter(toRet2, !is.na(toRet2$bes))
##
toRet2 <- dplyr::mutate(
toRet2,
"value"=.data$bes,
source="Symbol"
)
toRet2 <- dplyr::rename(toRet2, "preferred"="canBes")
##
toRet2 <- dplyr::distinct(toRet2)
toRet <- dplyr::bind_rows(toRet1, toRet2)
toRet <- dplyr::rename(toRet, "symbol"="bes")
if(is.null(entities)){
toRet <- dplyr::select(
toRet, "value", "preferred", "be", "source", "organism",
"symbol",
"BE_entity",
"BEID", "BE_source"
)
}else{
toRet <- dplyr::select(
toRet, "value", "preferred", "be", "source", "organism",
"symbol",
"BE_entity"
)
}
}
return(toRet)
}