-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTop100HPO.R
316 lines (233 loc) · 11.7 KB
/
Top100HPO.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
library(shiny)
library(ontologySimilarity)
library(ontologyIndex)
library(ontologyPlot)
library(data.table)
library(tidyverse)
ui <- fluidPage(titlePanel("HPO Based Gene Priority"),
sidebarLayout(
sidebarPanel(
helpText(
"Note: Files should be in .csv format
and should not contain headers. The
first column should include proband IDs
and the second column should include HPO
terms with a ; between subsequent terms,
no space. EG: HP:0000001;HP:0000002"
),
fileInput("file", h3("File input")),
actionButton(inputId = "submit_loc",
label = "Submit"),
downloadButton("downloadData", "Download Gene Matches"),
downloadButton("downloadData1", "Download Disease Matches")
),
mainPanel(
DT::dataTableOutput("matches"),
DT::dataTableOutput("matches1"))
))
server <- function(input, output) {
values <- reactiveValues()
observeEvent(eventExpr = input[["submit_loc"]],
handlerExpr = {
print("PRESSED")
#getting the HPO ontology dynamically; always the most updated ontology (every 2weeks)
#future look at other ontologies
ontology <-
get_ontology("http://purl.obolibrary.org/obo/hp.obo", extract_tags = "everything")
information_content <-
descendants_IC(ontology)
#reference using method change to lin or resnick
term_sim_mat <- get_term_sim_mat(
ontology,
information_content = information_content,
method = "lin",
row_terms = names(information_content),
col_terms = names(information_content)
)
gene2pheno <-
read.delim(
"http://purl.obolibrary.org/obo/hp/hpoa/genes_to_phenotype.txt",
header = T,
stringsAsFactors = F
)
#loading the disease database
#header columns are good here
db2pheno <-
read.delim(
"http://purl.obolibrary.org/obo/hp/hpoa/phenotype.hpoa",
skip = 4,
header = T,
stringsAsFactors = F
)
#p terms are phenotype terms
# c terms are clinical terms
# i terms are inheritance terms
# letter designations used by database
# only want phenotype terms because other terms have hpo terms that will be included in analysis if you don't take them
# this is a research analysis; agnostic of what the inheritance mode is or just not assuming anything known about clinical onset kind of terms
nonp_terms <- db2pheno[db2pheno$aspect != "P", ]
# getting HPO Ids of non phenotype terms to remove
nonp_terms1 <- unique(nonp_terms$hpo_id)
#removing from gene database
gene2pheno <-
gene2pheno[!gene2pheno$hpo_id %in% nonp_terms1, ]
#cleaning up disease database
#databases don't have same structure; disease db has more info and is easier to clean
#Qualifier column is not associations and makes clinical input a lot harder
#remove NOTs and only keep P terms for phenotypes
db2pheno <- db2pheno %>% filter(qualifier != "NOT",
aspect == "P")
## O&T Combined
gn_ids <-
unique(gene2pheno$gene_symbol)
#unique ensure no hpo term duplicates
gene_sets <-
lapply(gn_ids, function(x) {
unique(gene2pheno[x == gene2pheno$gene_symbol,][["hpo_id"]])
})
names(gene_sets) <-
gn_ids
og_gene_sets <-
gene_sets
#
db2pheno <-
db2pheno %>% filter(!db2pheno$database_id %like% "DECIPHER")
dz_id <-
unique(db2pheno$database_id)
dz_sets <- lapply(dz_id, function(x) {
unique(db2pheno[x == db2pheno$database_id, ][["hpo_id"]])
})
names(dz_sets) <-
dz_id
og_dz_sets <-
dz_sets
## O&T Patient
#here put a reference
# preprocess file to match our input or change code to fit input file
#want a list of hpo terms
#importing ptdata
file1 <- input$file
ptdata <-
read.csv(file1$datapath,
stringsAsFactors = F,
header = F)
#split hpo column based on semicolon
pt_term_sets <-
lapply(strsplit(ptdata$V2, ";"), unique)
#use names from column 1 in original pheno input to name pt_term_sets
names(pt_term_sets) <-
ptdata$V1
#save original
og_pt_term_sets <- pt_term_sets
# O&T Patient Combined
for (a in 1:length(pt_term_sets)) {
gene_sets[length(gene_sets) + 1] <- pt_term_sets[a]
}
names(gene_sets)[(length(og_gene_sets) + 1):(length(og_gene_sets) + length(pt_term_sets))] <-
names(pt_term_sets)
for (a in 1:length(pt_term_sets)) {
dz_sets[length(dz_sets) + 1] <- pt_term_sets[a]
}
names(dz_sets)[(length(og_dz_sets) + 1):(length(og_dz_sets) + length(pt_term_sets))] <-
names(pt_term_sets)
## Gene Analysis Combined
# Make changes to pt_term_sets
pt_term_set3 <- pt_term_sets
sim_mat_gn <- get_sim_grid(
term_sim_mat = term_sim_mat,
term_sets = gene_sets,
term_sets2 = pt_term_set3,
term_sim_method = "lin",
combine = "average"
)
dist_mat_gn <-
max(sim_mat_gn) - sim_mat_gn
sim_df_gn <-
as.data.frame(
sim_mat_gn,
row.names = rownames(sim_mat_gn),
col.names = colnames(sim_mat_gn)
)
#
sim_score_pt_gn <-
sim_df_gn[colnames(sim_df_gn) %in% names(pt_term_set3), ]
sim_df_gn <-
sim_df_gn[!rownames(sim_df_gn) %in% names(pt_term_set3), ]
sim_score_gn <-
sim_df_gn[colnames(sim_df_gn) %in% names(pt_term_set3)]
#
ord_sim_gn <- list()
ord_sim_gn <- lapply(sim_score_gn, function(x) {
ord_sim_gn <-
x[order(x, decreasing = T)[1:100]]
names(ord_sim_gn) <-
rownames(sim_score_gn)[order(x, decreasing = T)[1:100]]
ord_sim_gn
})
ptest_grps <- lapply(ord_sim_gn, names)
names(ptest_grps) <-
NULL
query <- names(pt_term_set3)
#
test1 <- ptest_grps
names(test1) <- query
values$test2 <- as.data.frame(test1)
## Disease Analysis Combined
# Make changes to pt_term_sets
sim_mat_dz <- get_sim_grid(
term_sim_mat = term_sim_mat,
term_sets = dz_sets,
term_sets2 = pt_term_set3,
term_sim_method = "lin",
combine = "average"
)
dist_mat_dz <-
max(sim_mat_dz) - sim_mat_dz
sim_df_dz <-
as.data.frame(sim_mat_dz,
row.names = rownames(sim_mat_dz),
col.names = colnames(sim_mat_dz))
#
sim_score_pt_dz <-
sim_df_dz[colnames(sim_df_dz) %in% names(pt_term_set3), ]
sim_df_dz <-
sim_df_dz[!rownames(sim_df_dz) %in% names(pt_term_set3), ]
sim_score_dz <-
sim_df_dz[colnames(sim_df_dz) %in% names(pt_term_set3)]
#
ord_sim_dz <- lapply(sim_score_dz, function(x) {
ord_sim_dz <-
x[order(x, decreasing = T)[1:100]]
names(ord_sim_dz) <-
rownames(sim_score_dz)[order(x, decreasing = T)[1:100]]
ord_sim_dz
})
ptest_grps_dz <- lapply(ord_sim_dz, names)
names(ptest_grps_dz) <-
NULL
#
test3 <- ptest_grps_dz
names(test3) <- query
values$test4 <- as.data.frame(test3)
#
output$matches <- DT::renderDataTable({
values$test2
})
output$matches1 <- DT::renderDataTable({
values$test4
})
output$downloadData <- downloadHandler(
filename = "Gene_Matches.csv",
content = function(file) {
write.csv(values$test2, file, row.names = F)
}
)
output$downloadData1 <- downloadHandler(
filename = "Disease_Matches.csv",
content = function(file1) {
write.csv(values$test4, file1, row.names = F)
}
)
})
}
shinyApp(ui, server)