-
Notifications
You must be signed in to change notification settings - Fork 53
/
auto_query_symptom.rkt
515 lines (402 loc) · 15.5 KB
/
auto_query_symptom.rkt
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#lang racket
(provide (all-defined-out)
(all-from-out "../../medikanren/common.rkt" "../../medikanren/pieces-parts/mk-db.rkt"))
(require "../../medikanren/pieces-parts/common.rkt" "../../medikanren/pieces-parts/mk-db.rkt")
(require racket/date)
(require csv-reading)
(require csv-writing)
(require "../../medikanren/pieces-parts/csv.rkt"
"../../medikanren/pieces-parts/repr.rkt")
(require racket/list racket/port racket/set racket/stream racket/string)
(provide set-field-separator!
csv-records)
(define extract-name/curie/category-from-concept-ls
(lambda (query-ls els)
(cond
((null? query-ls) els)
((or (void? (car query-ls))
(boolean? (car query-ls)))
(extract-name/curie/category-from-concept-ls
(cdr query-ls) els))
(else
(match (car query-ls)
[`(,db ,cui ,id ,name ,category ,properties-list)
(extract-name/curie/category-from-concept-ls
(cdr query-ls)
(cons
(list db id name category) els))])))))
(define extract-curie-from-concept-ls
(lambda (query-ls els)
(cond
((null? query-ls) els)
((or (void? (car query-ls))
(boolean? (car query-ls)))
(extract-curie-from-concept-ls
(cdr query-ls) els))
(else
(match (car query-ls)
[`(,db ,cui ,id ,name ,category ,properties-list)
(extract-curie-from-concept-ls
(cdr query-ls)
(cons
(list id) els))])))))
;; no rtx2 categories
(define molecular_entity
'((semmed 2 . "gene")
(orange 6 . "(\"gene\")")
(orange 19 . "(\"genomic entity\" \"gene\")")
(orange 23 . "(\"gene\" \"genomic entity\")")
(robokop 0 . "(\"named_thing\" \"gene\")")
(robokop 11 . "(\"named_thing\" \"gene_family\")")
(semmed 6 . "protein")
(rtx 1 . "protein")
(orange 28 . "(\"genome\")")
(orange 22 . "(\"genomic entity\")")
(semmed 9 . "genomic_entity")
(rtx 2 . "microRNA")
(orange 15 . "(\"transcript\")")
(rtx 3 . "metabolite")
(semmed 0 ."biological_entity")
(orange 12 . "(\"food material\")")))
(define member?
(lambda (a ls)
(cond
((null? ls) #f)
(else
(or (equal? a (car ls))
(member? a (cdr ls)))))))
(define filter-concept-by-f
(lambda (ls els curie f)
(cond
((null? ls) els)
((f curie (car ls))
(filter-concept-by-f
(cdr ls)
(cons (car ls) els) curie f))
(else
(filter-concept-by-f
(cdr ls) els curie f)))))
(define HP-symptoms-from-input/concept-ls
(filter-concept-by-f
(find-concepts
#t
(list
"HP:0001531"
"HP:0003550"
"HP:0001004"
"HP:0008513"
"HP:0002144"
"HP:0001790")) '() 'orange member?))
#|
(define HP-symptoms-from-input/concept-ls
(find-concepts
#t
(list
"HP:0001531"
"HP:0003550"
"HP:0001004"
"HP:0008513"
"HP:0002144"
"HP:0001790")))
|#
(newline)
(displayln (format "CONCEPTS FOUND RETURNED FROM INITIAL INPUT CURIE:"))
(newline)
(pretty-print (extract-name/curie/category-from-concept-ls HP-symptoms-from-input/concept-ls '()))
(define subclass_of (find-exact-predicates (list "subclass_of")))
(define contributes_to (find-exact-predicates (list "contributes_to")))
(define causally_related_to (find-exact-predicates (list "causually_related_to")))
(define has_phenotype (find-exact-predicates (list "has_phenotype")))
;;subclass of doesn't give any more concepts
;;will attempt to get disease assoc next
#|
(match-define
(list A-->subclass_of-->HP-input=>concepts
A-->subclass_of-->HP-input=>edges)
(time
(run/graph
((A #f)
(HP-input HP-symptoms-from-input/concept-ls))
((--subclass_of--> subclass_of))
(A --subclass_of--> HP-input))))
|#
;;orange only
(match-define
(list A-->subclass_of-->HP-input=>concepts
A-->subclass_of-->HP-input=>edges)
(time
(run/graph
((A #f)
(HP-input HP-symptoms-from-input/concept-ls))
((--subclass_of--> '((orange 0 . "subclass_of"))))
(A --subclass_of--> HP-input))))
(define A-->subclass_of-->HP-input/concepts
(hash-ref A-->subclass_of-->HP-input=>concepts 'A))
(define HP-concept-ls
(set-union A-->subclass_of-->HP-input/concepts
HP-symptoms-from-input/concept-ls))
(newline)
(displayln (format "~a CONCEPTS FROM:\nA --subclass_of--> HP-concept-ls" (length HP-concept-ls)))
(newline)
(pretty-print (extract-name/curie/category-from-concept-ls HP-concept-ls '()))
(newline)
#|
(match-define
(list A--has_phenotype-->HP-ls=>concepts
A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((A #f)
(HP-ls HP-concept-ls))
((--has_phenotype--> has_phenotype))
(A --has_phenotype--> HP-ls))))
|#
;;orange only
(match-define
(list A--has_phenotype-->HP-ls=>concepts
A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((A #f)
(HP-ls HP-concept-ls))
((--has_phenotype--> '((orange 2 . "has_phenotype"))))
(A --has_phenotype--> HP-ls))))
(define A-->has_phenotype-->HP-input/concepts
(hash-ref A--has_phenotype-->HP-ls=>concepts 'A))
(define A-->has_phenotype-->HP-input/edges
(hash-ref A--has_phenotype-->HP-ls=>edges '--has_phenotype-->))
(newline)
(displayln (format "~a CONCEPTS FROM:\nA --has_phenotype--> HP-concept-ls" (length A-->has_phenotype-->HP-input/concepts)))
(newline)
(pretty-print (extract-name/curie/category-from-concept-ls A-->has_phenotype-->HP-input/concepts '()))
(newline)
#|
(match-define
(list B--contributes_to-->A--has_phenotype-->HP-ls=>concepts
B--contributes_to-->A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((A #f)
(B molecular_entity)
(HP-ls HP-concept-ls))
((--has_phenotype--> has_phenotype)
(--contributes_to--> contributes_to))
(B --contributes_to--> A --has_phenotype--> HP-ls))))
|#
(match-define
(list B--contributes_to-->A--has_phenotype-->HP-ls=>concepts
B--contributes_to-->A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((A #f)
(B #f)
(HP-ls HP-concept-ls))
((--has_phenotype--> '((orange 2 . "has_phenotype")))
(--contributes_to--> '((orange 11 . "contributes_to"))))
(B --contributes_to--> A --has_phenotype--> HP-ls))))
(define B--contributes_to-->A--has_phenotype-->HP-ls/concepts
(hash-ref B--contributes_to-->A--has_phenotype-->HP-ls=>concepts 'B))
(define B--contributes_to-->A--has_phenotype-->HP-ls/edges
(hash-ref B--contributes_to-->A--has_phenotype-->HP-ls=>edges '--contributes_to-->))
(newline)
(displayln (format "~a CONCEPTS FROM:\nB--contributes_to-->A--has_phenotype-->HP-ls/concepts" (length B--contributes_to-->A--has_phenotype-->HP-ls/concepts)))
(newline)
(pretty-print (extract-name/curie/category-from-concept-ls B--contributes_to-->A--has_phenotype-->HP-ls/concepts '()))
(newline)
(define NCBIGenes-from-symptoms/orange
(flatten (extract-curie-from-concept-ls B--contributes_to-->A--has_phenotype-->HP-ls/concepts '())))
(define NCBIGenes-from-symptoms/orange/concept-ls
(find-concepts #t NCBIGenes-from-symptoms/orange))
;; NCBIGene from orange --> HGNC gene in robokop/rtx2
(match-define
(list NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB=>concepts
NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB=>edges)
(time
(run/graph
((A #f)
(B #f)
(NCBIGene_input NCBIGenes-from-symptoms/orange/concept-ls))
((--equivalent_to--> '((rtx2 57 . "equivalent_to")))
(--encodes--> '((rtx2 775 . "encodes"))))
(NCBIGene_input --equivalent_to--> A --encodes--> B))))
(define NCBIGene_input--equivalent_to-->HGNC/concepts
(hash-ref NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB=>concepts 'A))
(define NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB/concepts
(hash-ref NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB=>concepts 'B))
(define HGNC-from-symptoms
(flatten (extract-curie-from-concept-ls NCBIGene_input--equivalent_to-->HGNC/concepts '())))
(define HGNC-from-symptoms/concept-ls
(find-concepts #t HGNC-from-symptoms))
(define UniProtKB-from-symptoms
(flatten (extract-curie-from-concept-ls NCBIGene_input--equivalent_to-->HGNC--encodes-->UniProtKB/concepts '())))
(define UniProtKB-from-symptoms/concept-ls
(find-concepts #t UniProtKB-from-symptoms))
;; rtx2 uniprotkb --> GO
;; GO is 3 separate ontologies: Cellular Component, Biological Process, Molecular Function
;; isa predicate operates within one ontology and will not cross
;; part_of , regulates cross between the ontologies
;; isa --> biological_process GO ontology
;; biological process is broken into 3 nodes = cellular process, development, physiologic process
;; regulates repdicate not really useful
#|
(match-define
(list UniProtKB_input--involved_in-->GO--regulates-->GOparent=>concepts
UniProtKB_input--involved_in-->GO--regulates-->GOparent=>edges)
(time
(run/graph
((A #f)
(B #f)
(UniProtKB_input UniProtKB-from-symptoms/concept-ls))
((--involved_in--> '((rtx2 182 . "involved_in")))
(--regulates--> '((rtx2 185 . "regulates"))))
(UniProtKB_input --involved_in--> A --regulates--> B))))
(define UniProtKB_input--involved_in-->GO/concepts
(hash-ref UniProtKB_input--involved_in-->GO--regulates-->GOparent=>concepts 'A))
(define UniProtKB_input--involved_in-->GO--regulates-->GOparent/concepts
(hash-ref UniProtKB_input--involved_in-->GO--regulates-->GOparent=>concepts 'B))
(define UniProtKB_input--involved_in-->GO/edges
(hash-ref UniProtKB_input--involved_in-->GO--regulates-->GOparent=>edges '--involved_in-->))
(define UniProtKB_input--involved_in-->GO--regulates-->GOparent/edges
(hash-ref UniProtKB_input--involved_in-->GO--regulates-->GOparent=>edges '--regulates-->))
;;rtx2 involved_in gives regulation/positive/negative terms back
(pretty-print (extract-name/curie/category-from-concept-ls UniProtKB_input--involved_in-->GO/concepts '()))
;; isa--> "biological process"
(pretty-print (extract-name/curie/category-from-concept-ls UniProtKB_input--involved_in-->GO--regulates-->GOparent/concepts '()))
|#
;; use to get
(rtx2 188 . "positively_regulates")
(rtx2 189 . "negatively_regulates")
(match-define
(list UniProtKB_input--involved_in-->GO--part_of-->GOparent=>concepts
UniProtKB_input--involved_in-->GO--part_of-->GOparent=>edges)
(time
(run/graph
((A #f)
(B #f)
(UniProtKB_input UniProtKB-from-symptoms/concept-ls))
((--involved_in--> '((rtx2 182 . "involved_in")))
(--part_of--> '((rtx2 14 . "part_of")))
(--subclass_of--> '((rtx2 15 . "subclass_of"))))
(UniProtKB_input --involved_in--> A --subclass_of--> B))))
(define UniProtKB_input--involved_in-->GO/concepts
(hash-ref UniProtKB_input--involved_in-->GO--part_of-->GOparent=>concepts 'A))
(define UniProtKB_input--involved_in-->GO--part_of-->GOparent/concepts
(hash-ref UniProtKB_input--involved_in-->GO--part_of-->GOparent=>concepts 'B))
(define UniProtKB_input--involved_in-->GO/edges
(hash-ref UniProtKB_input--involved_in-->GO--part_of-->GOparent=>edges '--involved_in-->))
(define UniProtKB_input--involved_in-->GO--part_of-->GOparent/edges
(hash-ref UniProtKB_input--involved_in-->GO--part_of-->GOparent=>edges '--part_of-->))
;;rtx2 involved_in gives 21
(pretty-print (extract-name/curie/category-from-concept-ls UniProtKB_input--involved_in-->GO/concepts '()))
;; part_of--> gives 25
(pretty-print (extract-name/curie/category-from-concept-ls UniProtKB_input--involved_in-->GO--part_of-->GOparent/concepts '()))
(define UniProtKB_input--involved_in-->GO/edges/edges-for-print
(edge-matcher UniProtKB_input--involved_in-->GO/edges '()))
(define UniProtKB_input--involved_in-->GO--part_of-->GOparent/edges-for-print
(edge-matcher UniProtKB_input--involved_in-->GO--part_of-->GOparent/edges '()))
(define UniProtKB_input--involved_in-->GO/edges
(hash-ref UniProtKB_input--involved_in-->GO--involved_in-->GOparent=>edges '--involved_in-->))
;;gives most relevant GO pathways
(define UniProtKB_input--involved_in-->GO--involved_in-->GOparent/edges
(hash-ref UniProtKB_input--involved_in-->GO--involved_in-->GOparent=>edges '--involved_in-->))
(define edge-matcher
(lambda (ls els)
(cond
((null? ls) (set-union els))
(else
(match (car ls)
[`(,db ,edge-cui
(,subject-cui ,subject-id ,subject-name (,_ . ,subject-category) ,subject-props-assoc)
(,object-cui ,object-id ,object-name (,_ . ,object-category) ,object-props-assoc)
(,_ . ,pred)
,pred-props-assoc)
(edge-matcher
(cdr ls)
(set-union
(list `((,subject-name . ,subject-id) ,pred (,object-name . ,object-id)) `(,db))
els))])))))
(define UniProtKB_input--involved_in-->GO/edges-for-print
(edge-matcher UniProtKB_input--involved_in-->GO/edges '()))
(define UniProtKB_input--involved_in-->GO--involved_in-->GOparent/edges-for-print
(edge-matcher UniProtKB_input--involved_in-->GO--involved_in-->GOparent/edges '()))
#|
(define encodes (find-exact-predicates (list "encodes")))
(define gene_associated_with_condition (find-exact-predicates (list "gene_associated_with_condition")))
(match-define
(list hgnc--encodes-->B--gene_associated_with_condition-->D=>concepts
B--contributes_to-->A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((B #f)
(D #f)
(hgnc gene-concept-ls))
((--encodes--> encodes)
(--gene_associated_with_condition--> '((rtx2 717 . "gene_associated_with_condition"))))
(hgnc --encodes--> B --gene_associated_with_condition--> D))))
(define hgnc--encodes-->B--gene_associated_with_condition-->D/concepts
(hash-ref hgnc--encodes-->B--gene_associated_with_condition-->D=>concepts 'D))
(filter-concept-by-f
(find-concepts
#t
(list
"HP:0001531"
"HP:0003550"
"HP:0001004"
"HP:0008513"
"HP:0002144"
"HP:0001790")) '() 'orange member?)
(match-define
(list B--contributes_to-->A--has_phenotype-->HP-ls=>concepts
B--contributes_to-->A--has_phenotype-->HP-ls=>edges)
(time
(run/graph
((A #f)
(B molecular_entity)
(HP-ls HP-concept-ls))
((--has_phenotype--> has_phenotype)
(--contributes_to--> contributes_to))
(B --contributes_to--> A --has_phenotype--> HP-ls))))
|#
#|
;; need an exact predicate function
(define (find-exact-predicates names)
(append* (map (lambda (name) (run* (x) (~predicateo name x))) names)))
(define (find-categories names)
(append* (map (lambda (name) (run* (x) (~categoryo name x))) names)))
(define (find-exact-categories names)
(run* (cat) (fresh (db catid name)
(membero name names)
(== cat `(,db ,catid . ,name))
(categoryo cat))))
(define (find-exact-predicates names)
(run* (cat) (fresh (db catid name)
(membero name names)
(== cat `(,db ,catid . ,name))
(categoryo cat))))
|#
;;TODO, load with only orange db to prevent has-phenotype from using other dbs
#|
(match-define
(list A-->HP-ls=>concepts
A-->HP-ls=>edges)
(time
(run/graph
((A #f)
(HP-ls HP-concept-ls))
((--has_phenotype--> has_phenotype)
(--subclass_of--> subclass_of))
(A --subclass_of--> HP-ls)
(A --has_phenotype--> HP-ls))))
(define A-->has_phenotype-->HP-input/concepts
(hash-ref A-->HP-ls=>concepts 'A))
(define A-->subclass_of-->HP-input/concepts
(hash-ref A-->HP-ls=>concepts 'A))
(define A-->has_phenotype-->HP-input/edges
(hash-ref A-->HP-ls=>edges '--has_phenotype-->))
(define A-->subclass_of-->HP-input/edges
(hash-ref A-->HP-ls=>concepts 'subclass_of))
;; missing a DOID --has_phenotype--> HP in my orange KG
;; DOID concepts appear to have "disease" only names in Orange
;; that are clearly not disease in RTX2 robokop etc DOID:3457 example
;; perhaps filter by mondo concepts
|#