-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTerms.R
249 lines (197 loc) · 6.75 KB
/
goTerms.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
#'
#' Functions regarding GO terms
#'
library(shiny)
library(GO.db)
library(AnnotationDbi)
# Importers for condition visuals
supportedSelectedGOTermsImporters <- list(
ImporterEntry(name = "csv", label = "PCAGO Selected GO Terms CSV (*.csv)", parameters = list(ImporterParameter.csv))
)
supportedSelectedGOTermsGenerators <- list()
availableSelectedGOTermsSamples <- list()
importSelectedGOTerms.CSV <- function(filehandle, parameters, available.terms) {
data <- read.csv(filehandle, sep = parameters$separator, row.names = NULL, stringsAsFactors = F, check.names = F)
return(data)
}
#' Imports visual definitions from filehandle with importer defined by datatype
#'
#' @param filehandle Either a filename or a connection
#' @param datatype One value in supportedReadcountDataTypes
#' @param conditions Vector of condition names
#'
#' @return Data frame containing the visual parameters for each condition
#' @export
#'
#' @examples
importSelectedGOTerms <- function(filehandle, importer, parameters, available.terms) {
if(length(available.terms) == 0) {
stop("There are no available GO terms!")
}
data <- NULL
if(importer == "csv") {
data <- importSelectedGOTerms.CSV(filehandle, parameters, available.terms)
}
else {
stop(paste("Unknown importer", importer))
}
if(nrow(data) == 0) {
stop("Imported data has no rows!")
}
if(ncol(data) == 0) {
stop("Imported data has no columns!")
}
if(!("goid" %in% names(data)) ) {
stop("Imported data must contain a column 'goid'!")
}
matching.indices <- match(data$goid, available.terms)
if(length(matching.indices) == 0) {
stop("The file does not contain any of the available GO IDs!")
}
return(unique(data$goid[matching.indices]))
}
#' Imports sample with given sample id
#'
#' @param sample
#'
#' @return Data frame containing the read data
#' @export
#'
#' @examples
importSelectedGOTermsSample <- function(sample, parameters, available.terms) {
if(!is.character(sample)) {
stop("Invalid arguments!")
}
con <- file(paste0("sampledata/", sample), "r")
on.exit({ close(con) })
parameters$separator <- ","
data <- importSelectedGOTerms(filehandle = con,
importer = "csv",
parameters = parameters,
available.terms = available.terms)
return(data)
}
exportSelectedGOTerms.CSV <- function(filename, goids) {
if(length(goids) > 0) {
frame <- AnnotationDbi::select(GO.db, goids, columns = c("TERM", "DEFINITION"))
names(frame) <- c("goid", "goterm", "definition")
write.csv(frame, file = filename, sep = ",", row.names = F)
}
}
#' Builds UI that describes GO terms of given IDs
#'
#' @param goids
#'
#' @return
#' @export
#'
#' @examples
goTermsBuildInfoUI <- function(goids) {
output <- tagList()
for(goid in goids) {
info <- GO.db::GOTERM[[goid]]
if(is.null(info)) {
output <- tagAppendChild(output, tags$div(
class = "go-term-details",
tags$h2("Unknown GO ID"),
vSkip(5),
tags$a(href=paste0("http://amigo.geneontology.org/amigo/medial_search?q=", info@GOID), target = "_blank", "Search on AmiGO 2"),
hDivider()
))
}
else {
ontology <- "Unknown"
if(info@Ontology == "BP") {
ontology <- "Biological Process (BP)"
}
else if(info@Ontology == "CC") {
ontology <- "Cellular Component (CC)"
}
else if(info@Ontology == "MF") {
ontology <- "Molecular Function (MF)"
}
alternative.ids <- if(length(info@Secondary) > 0) paste(info@Secondary, collapse = ", ") else "None"
synonyms <- if(length(info@Synonym) > 0) paste(info@Synonym, collapse = ", ") else "None"
output <- tagAppendChild(output, tags$div(
class = "go-term-details",
tags$h2(info@GOID),
tags$h3(info@Term),
tags$div(class = "goterm-info-entry", tags$span(class = "key", "Ontology"), tags$span(class = "value", ontology)),
tags$div(class = "goterm-info-entry", tags$span(class = "key", "Alternate IDs"), tags$span(class = "value", alternative.ids)),
tags$div(class = "goterm-info-entry", tags$span(class = "key", "Synonyms"), tags$span(class = "value", synonyms)),
vSkip(10),
tags$div(class = "definition", info@Definition),
vSkip(5),
tags$a(href=paste0("http://amigo.geneontology.org/amigo/term/", info@GOID), target = "_blank", "View on AmiGO 2"),
hDivider()
))
}
}
return(output)
}
#' Selects the GO ids that are the root of the drilldown menu
#'
#' @param goids
#'
#' @return
#' @export
#'
#' @examples
goTermsFindRootGOIds <- function(goids) {
# We assume that all parent GO terms are also existing
# Just search for the GO ids that have parent is_a "all"
roots <- c()
info <- AnnotationDbi::select(GO.db, goids, columns = c("ONTOLOGY"))
# Biological Process (BP)
goids.bp <- goids[info$ONTOLOGY == "BP"]
if(length(goids.bp) > 0) {
roots <- c(roots, goids.bp[sapply(as.list(GOBPPARENTS[goids.bp]), function(x) "is_a" %in% names(x) && x["is_a"] == "all")])
}
# Cellular Component (CC)
goids.cc <- goids[info$ONTOLOGY == "CC"]
if(length(goids.cc) > 0) {
roots <- c(roots, goids.cc[sapply(as.list(GOCCPARENTS[goids.cc]), function(x) "is_a" %in% names(x) && x["is_a"] == "all")])
}
# Molecular Function (MF)
goids.mf <- goids[info$ONTOLOGY == "MF"]
if(length(goids.mf) > 0) {
roots <- c(roots, goids.mf[sapply(as.list(GOMFPARENTS[goids.mf]), function(x) "is_a" %in% names(x) && x["is_a"] == "all")])
}
return(roots)
}
#' Finds the child GO ids of selection
#'
#' @param goids
#'
#' @return
#' @export
#'
#' @examples
goTermsFindChildGOIds <- function(goids, selection) {
children <- c()
goids.full <- goids
goids <- goids[goids == selection]
info <- AnnotationDbi::select(GO.db, selection, columns = c("ONTOLOGY"))
# Biological Process (BP)
goids.bp <- goids[info$ONTOLOGY == "BP"]
if(length(goids.bp) > 0) {
children <- c(children, sapply(as.list(GOBPCHILDREN[goids.bp]), function(x) x[names(x) == "is_a"]))
}
# Cellular Component (CC)
goids.cc <- goids[info$ONTOLOGY == "CC"]
if(length(goids.cc) > 0) {
children <- c(children, sapply(as.list(GOCCCHILDREN[goids.cc]), function(x) x[names(x) == "is_a"]))
}
# Molecular Function (MF)
goids.mf <- goids[info$ONTOLOGY == "MF"]
if(length(goids.mf) > 0) {
children <- c(children, sapply(as.list(GOMFCHILDREN[goids.mf]), function(x) x[names(x) == "is_a"]))
}
found.terms <- as.vector(na.omit(match(children, goids.full)))
if(length(found.terms) == 0) {
return(c("No terms found" = "none"))
}
else {
return(goids.full[found.terms])
}
}