[73f552]: / R / enrichment_functions.R

Download this file

261 lines (242 with data), 7.0 kB

  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
#' Get interaction from ORA enrichment analysis
#'
#' Returns results of an ORA analysis as an interaction graph
#'
#' @param query a vector (or a list) of character with the ID to perform
#' the ORA analysis
#' @param sources (optional) a character in
#' (GO, KEGG, REAC, TF, MIRNA, CORUM, HP, HPA, WP)
#' @param organism (optional) a character (default = 'hsapiens')
#' @param signif.value (optional) a logical, default = ''
#'
#' @return
#' a graph object (or list of graph) containing the interaction between
#' the query and the target terms.
#'
#' @seealso \code{\link[gprofiler2]{gost}} \code{\link[gprofiler2]{gconvert}}
#'
#' @examples
#' query <- c('IL15', 'CDHR5', 'TGFA', 'C4B')
#' get_interaction_from_ORA(query,
#' sources = 'GO')
#'
#' query <- list('All' = c('IL15', 'CDHR5', 'TGFA', 'C4B'),
#' 'c1' = c('IL15', 'CDHR5', 'TGFA'))
#' get_interaction_from_ORA(query,
#' sources = 'GO')
#'
#' @importFrom gprofiler2 gost gconvert
#' @importFrom dplyr pull select filter
#' @export
get_interaction_from_ORA <- function(query,
sources = "GO",
organism = "hsapiens",
signif.value = TRUE) {
# validate query (char)
if (is(query, "list")) {
query <- lapply(query,
function(x)
check_vector_char(x,
var.name = "'query '"))
if (is.null(names(query))) {
names(query) <- seq_along(query)
}
} else {
query <- check_vector_char(query)
}
# check organism
organism = check_vector_char(
X = organism,
X.length = 1,
default = "hsapiens",
var.name = "'organism' "
)
# check source
sources <- match.arg(
arg = sources,
choices = c("GO", "KEGG", "REAC", "TF", "MIRNA", "CORUM", "HP",
"HPA", "WP"),
several.ok = FALSE
)
sources <-
check_vector_char(sources, default = "GO") # default value
# check signif
signif.value <- return_true_false(signif.value, default = TRUE)
if (is(query, "list")) {
res.ora <- list()
term_map <- list()
res.graph <- list()
for (i in names(query)) {
res.ora[[i]] <- get_ORA(query = query[[i]],
sources = sources,
organism = organism)
term_map_tmp <- gprofiler2::gconvert(query = query[[i]],
organism = organism,
target = sources)
target_id <- (
res.ora[[i]] %>%
dplyr::filter(significant == signif.value) %>%
dplyr::pull(term_id)
)
term_map[[i]] <- term_map_tmp %>%
dplyr::filter(target %in% target_id) %>%
dplyr::select(input, target) %>%
unique %>%
na.omit()
res.graph[[i]] <-
igraph::graph_from_data_frame(term_map[[i]],
directed = FALSE)
res.graph[[i]] <- set_vertex_attr(
graph = res.graph[[i]],
name = "mode",
index = term_map[[i]]$input,
value = "core"
)
res.graph[[i]] <- set_vertex_attr(
graph = res.graph[[i]],
name = "mode",
index = term_map[[i]]$target,
value = "extended"
)
class(res.graph) <-
c("list.interaction.igraph", "list.igraph")
}
} else {
# query is not a list
res.ora <- get_ORA(query = query,
sources = sources,
organism = organism)
term_map_tmp <- gprofiler2::gconvert(query = query,
organism = organism,
target = sources)
target_id <- (res.ora %>%
dplyr::filter(significant == signif.value) %>%
dplyr::pull(term_id))
if (is.null(term_map_tmp)) {
return(NULL)
}
term_map <- term_map_tmp %>%
dplyr::filter(target %in% target_id) %>%
dplyr::select(input, target) %>%
unique %>%
na.omit()
res.graph <-
igraph::graph_from_data_frame(term_map, directed = FALSE)
res.graph <- set_vertex_attr(
graph = res.graph,
name = "mode",
index = term_map$input,
value = "core"
)
res.graph <- set_vertex_attr(
graph = res.graph,
name = "mode",
index = term_map$target,
value = "extended"
)
class(res.graph) <- c("interaction.igraph", "igraph")
}
return(res.graph)
}
#' ORA enrichment analysis
#'
#' Returns results of an ORA analysis
#'
#' @param query a vector of character, a lit of ID
#' @param sources a character or list of character
#' @param organism a character (default = 'hsapiens')
#'
#' @return
#' a data.frame containing the enrichment result
#'
#' @seealso \code{\link[gprofiler2]{gost}}
#'
#' @importFrom gprofiler2 gost
get_ORA <- function(query,
sources = NULL,
organism = "hsapiens") {
if (is(query, "list")) {
res <- list()
for (i in names(query)) {
ORA <- gprofiler2::gost(
query = query[[i]],
organism = organism,
significant = FALSE,
sources = sources,
multi_query = FALSE
)
ORA.res <- ORA$result
if (!is.null(ORA.res)) {
ORA.res <- ORA.res %>%
mutate(cluster = i) %>%
dplyr::select(
"cluster",
"term_id",
"source",
"term_name",
"p_value",
"significant",
"term_size",
"query_size",
"intersection_size",
"precision",
"recall"
)
res[[i]] <- ORA.res
}
}
RES <- purrr::map_dfr(res, ~ .x)
} else {
ORA <- gprofiler2::gost(
query = query,
organism = organism,
significant = FALSE,
sources = sources,
multi_query = FALSE
)
ORA.res <- ORA$result
if (!is.null(ORA.res)) {
ORA.res <- ORA.res %>%
mutate(cluster = "All") %>%
dplyr::select(
"cluster",
"term_id",
"source",
"term_name",
"p_value",
"significant",
"term_size",
"query_size",
"intersection_size",
"precision",
"recall"
)
RES <- ORA.res
} else {
RES <- NULL
}
}
return(RES)
}
#' Get GO info
#'
#' From a GO terms (GOID), return definition, ontology and term values
#' from GO.db
#'
#' @param go a character, GO term
#'
#' @return
#' a data.frame with the following columns: 'GOID', 'DEFINITION',
#' 'ONTOLOGY', 'TERM'
#'
#' @import GO.db
#' @importFrom AnnotationDbi keytypes
get_go_info <- function(go) {
res <- AnnotationDbi::select(
x = GO.db,
keys = go,
keytype = "GOID",
columns = keytypes(GO.db)
)
return(res)
}