forked from webyrd/mediKanren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.rkt
2472 lines (2171 loc) · 136 KB
/
gui.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#|
WEB: mediKanren 2 Explorer TODO:
* Fix problem with 'Find in X's':
when you enter a string in 'Find in X's' text box, and a row is hilighted,
the Paths and Pubmed info isn't updated, and *clicking* on the
hilighted row doesn't update the Paths and Pubmed info.
* Fix sorting by CURIE, so that DOID:26 appears before DOID:2531
* fully implement concept normalization checkbox
* fully implement lightweight reasoning checkbox
* implement smarter copy/paste
* make the interface faster/more responsive
|#
#lang racket
(require
"common.rkt"
"synonyms.rkt"
"string-search.rkt"
;; (prefix-in semmed: "db/semmed.rkt")
(prefix-in rtx: "db/rtx2-biolink_2_1_2021_07_28.rkt")
(prefix-in kgx: "db/kgx-synonym.rkt")
json
racket/sandbox
racket/gui/base
framework
racket/engine
racket/date
racket/string
net/sendurl
(except-in racket/match ==)
(only-in srfi/1 iota))
#|
(require "base.rkt"
(prefix-in semmed: "db/semmed.rkt")
(prefix-in rtx: "db/rtx2-20210204.rkt")
(prefix-in kgx: "db/kgx-synonym.rkt"))
|#
#|
(require
"common.rkt"
"synonyms.rkt"
racket/sandbox
racket/gui/base
framework
racket/engine
racket/date
racket/string
net/sendurl
(except-in racket/match ==)
(only-in srfi/1 iota))
|#
#|
Choice 1:
;;; find-ids-named
;; Consult the string index associated with rel to find the ids
;; in rel with a (name) string matching every substr in substrs,
;; according to options stsopt. If the associated string index
;; has not been previously prepared, fail.
(define (find-ids-named rel substrs (stsopt stsopt-default))
(edited)
Choice 2:
;; Consult the string index associated with rel to find the concepts
;; in rel with a (name) string matching every substr in substrs,
;; according to options stsopt. If the associated string index
;; has not been previously prepared, fail.
(define (find-concepts-named rel substrs (stsopt stsopt-default))
|#
(provide
launch-gui)
(define MEDIKANREN_VERSION_STRING "mediKanren 2 Explorer 0.1.0")
(displayln "Starting mediKanren 2 Explorer...")
(newline)
(displayln "**************************************************")
(displayln "*** mediKanren 2 is for research purposes only ***")
(displayln "**************************************************")
(newline)
(displayln MEDIKANREN_VERSION_STRING)
;;; Query save file settings
(define WRITE_QUERY_RESULTS_TO_FILE #f)
(define QUERY_RESULTS_FILE_NAME "last.sx")
(define HUMAN_FRIENDLY_QUERY_RESULTS_FILE_NAME "last.txt")
(define SPREADSHEET_FRIENDLY_QUERY_RESULTS_FILE_NAME "last.tsv")
(define QUERY_RESULTS_FILE_MODE 'replace)
;;; Initial window size
(define HORIZ-SIZE 800)
(define VERT-SIZE 400)
;;; Decreases/increases predicate names
(define DECREASES_PREDICATE_NAMES
(list
"biolink:ameliorates"
"biolink:approved_to_treat"
"biolink:decreases_abundance_of"
"biolink:decreases_activity_of"
"biolink:decreases_expression_of"
"biolink:decreases_synthesis_of"
"biolink:decreases_transport_of"
"biolink:decreases_uptake_of"
"biolink:disrupts"
"biolink:entity_negatively_regulates_entity"
"biolink:prevents"
"biolink:process_negatively_regulates_process"
"biolink:treats"))
(define INCREASES_PREDICATE_NAMES
(list
"biolink:causes"
"biolink:causes_adverse_event"
"biolink:condition_associated_with_gene"
"biolink:contributes_to"
"biolink:decreases_degradation_of"
"biolink:enables"
"biolink:entity_positively_regulates_entity"
"biolink:exacerbates"
"biolink:gene_associated_with_condition"
"biolink:increases_abundance_of"
"biolink:increases_activity_of"
"biolink:increases_expression_of"
"biolink:increases_synthesis_of"
"biolink:increases_transport_of"
"biolink:process_positively_regulates_process"
"biolink:produces"))
(define (curie-string str)
(let ((cs (regexp-match* #px"^[\\s]*(([^\\s]+:[^\\s]*)|(:[^\\s]+))[\\s]*$" str #:match-select cadr)))
(if (null? cs)
#f
(car cs))))
(define (sort-paths paths)
(printf "sort-paths -- IMPLEMENT ME!\n")
paths)
(define (path-confidence . args)
;(printf "path-confidence -- IMPLEMENT ME!\n")
'todo)
(define (path-confidence<? . args)
;(printf "path-confidence<? -- IMPLEMENT ME!\n")
'todo)
(define (get-pred-names e*)
(let loop ([e* e*]
[pred-names '()])
(cond
[(null? e*) pred-names]
[else
(let ((edge (car e*))
(rest (cdr e*)))
(match edge
['path-separator
(loop rest pred-names)]
[`(,dbname ,eid ,subj ,obj (,pid . ,p-name) ,eprops)
(loop rest (if (member p-name pred-names)
pred-names
(cons p-name pred-names)))]
[else (error 'get-pred-names (format "unmatched edge ~s\n" edge))]))])))
(define (pubmed-count e)
(length (pubmed-ids-from-edge e)))
(define (pubmed-ids-from-edge-props eprops)
(cond
[(assoc "publications" eprops)
=> (lambda (pr)
(define pubs (cdr pr))
(let ((pubmed-ids (if (not (string? pubs))
'()
(regexp-match* #rx"PMID:([0-9]+)" pubs #:match-select cadr))))
pubmed-ids))]
[else '()])
;; Old mediKanren 1 common.rkt code:
#;(cond
[(assoc "pmids" eprops) ;; WEB the 'pmids' property is only used by semmed, I believe
=> (lambda (pr) (regexp-split #rx";" (cdr pr)))]
[(assoc "publications" eprops)
=> (lambda (pr)
(define pubs (cdr pr))
(if (not (string? pubs))
'()
(regexp-match* #rx"([0-9]+)" pubs #:match-select cadr)))]
[else '()])
)
(define (python->json py)
(define len (string-length py))
(let loop ((i 0) (start 0))
(cond ((= i len) (if (= start 0) py (substring py start)))
((eqv? (string-ref py i) #\')
(string-append
(substring py start i) "\""
(let requote ((i (+ i 1)) (start (+ i 1)))
(cond ((eqv? (string-ref py i) #\')
(string-append (substring py start i) "\""
(loop (+ i 1) (+ i 1))))
((eqv? (string-ref py i) #\\)
(if (eqv? (string-ref py (+ i 1)) #\")
(requote (+ i 2) start)
(string-append (substring py start i)
(requote (+ i 2) (+ i 1)))))
((eqv? (string-ref py i) #\")
(string-append (substring py start i) "\\\""
(requote (+ i 1) (+ i 1))))
(else (requote (+ i 1) start))))))
((eqv? (string-ref py i) #\")
(let skip ((i (+ i 1)) (start start))
(cond ((eqv? (string-ref py i) #\") (loop (+ i 1) start))
((eqv? (string-ref py i) #\\)
(if (eqv? (string-ref py (+ i 1)) #\")
(skip (+ i 2) start)
(string-append (substring py start i)
(skip (+ i 2) (+ i 1)))))
(else (skip (+ i 1) start)))))
(else (loop (+ i 1) start)))))
(define PUBMED_URL_PREFIX "https://www.ncbi.nlm.nih.gov/pubmed/")
(define (pubmed-URLs-from-edge edge)
(map (lambda (pubmed-id) (string-append PUBMED_URL_PREFIX (~a pubmed-id)))
(pubmed-ids-from-edge edge)))
(define (pubmed-ids-from-edge edge)
(remove-duplicates
(match edge
['path-separator '()]
[`(,dbname ,eid ,subj ,obj ,p ,eprops)
(pubmed-ids-from-edge-props eprops)])))
(define (publications-info-alist-from-edge-props eprops)
(cond
[(assoc "publications_info" eprops)
=> (lambda (pr)
(with-handlers ([exn:fail?
(lambda (v)
((error-display-handler) (exn-message v) v)
'())])
(define pubs (cdr pr))
(define jason-ht (string->jsexpr (python->json pubs)))
(hash-map jason-ht (lambda (k v)
(cons (string-append
PUBMED_URL_PREFIX
(car (regexp-match* #rx"([0-9]+)" (symbol->string k) #:match-select cadr)))
(list (hash-ref v '|publication date| #f)
(hash-ref v '|subject score| #f)
(hash-ref v '|object score| #f)
(regexp-replace*
#rx"([ ]+)"
(hash-ref v 'sentence #f)
" ")))))))]
[else '()]))
(define (publications-info-alist-from-edge edge)
;; ((pubmed-URL . (publication-date subject-score object-score sentence)) ...)
(remove-duplicates
(match edge
['path-separator '()]
[`(,dbname ,eid ,subj ,obj ,p ,eprops)
(publications-info-alist-from-edge-props eprops)])))
(define (print-short-concept-description concepts)
(displayln
(map
(lambda (c)
(match c
[`(,dbname ,curie ,name . ,rest)
`(,curie ,name)]))
concepts)))
(define (find-predicates/concepts subject? object? concepts)
; (printf "find-predicates/concepts subject?: ~s\nobject?: ~s\nconcepts: ~s\n\n" subject? object? concepts)
(let ((ans (map
(lambda (c)
(match c
[`(,dbname ,curie ,name ,cat)
(define subject-predicates
(and subject?
(set->list
(run*/set pred
(fresh (eid o)
(edge `(,dbname . ,eid) curie o)
(eprop `(,dbname . ,eid) "predicate" pred))))))
(define object-predicates
(and object?
(set->list
(run*/set pred
(fresh (eid s)
(edge `(,dbname . ,eid) s curie)
(eprop `(,dbname . ,eid) "predicate" pred))))))
; (printf "subject-predicates: ~s\n" subject-predicates)
; (printf "object-predicates: ~s\n" object-predicates)
(list c subject-predicates object-predicates)]))
concepts)))
ans))
;; from medikanren/common.rkt:
;;
#;(define (find-predicates/concepts subject? object? concepts)
(map (lambda (c)
(define subject-predicates
(and subject? (run* (p) (subject-predicateo c p))))
(define object-predicates
(and object? (run* (p) (object-predicateo c p))))
(list c subject-predicates object-predicates))
concepts))
;; TODO implement or remove
(define (find-concepts/options/curie-infer subject? object? strings)
'())
;; from medikanren/common.rkt:
;;
#;(define (find-concepts/options/cui-infer subject? object? isa-count strings)
(define yes-cui
(map (lambda (s) (run* (c) (~cui*-concepto (list s) c))) strings))
(define no-cui (filter-not not (map (lambda (s rs) (and (null? rs) s))
strings yes-cui)))
(define all (append* (cons (run* (c) (~name*-concepto no-cui c)) yes-cui)))
(concepts/options subject? object? isa-count all))
(define chars:ignore-typical "-")
(define chars:split-typical "\t\n\v\f\r !\"#$%&'()*+,./:;<=>?@\\[\\\\\\]\\^_`{|}~")
(define (smart-string-matches? case-sensitive? chars:ignore chars:split str* hay)
(define re:ignore (and (non-empty-string? chars:ignore)
(pregexp (string-append "[" chars:ignore "]"))))
(define re:split (and (non-empty-string? chars:split)
(pregexp (string-append "[" chars:split "]"))))
(define (normalize s case-sensitive?)
(define pruned (if re:ignore (string-replace s re:ignore "") s))
(if case-sensitive? pruned (string-downcase pruned)))
(define (contains-upcase? s) (not (string=? s (string-downcase s))))
(define case-sensitive?*
(map (lambda (s) (or case-sensitive? (contains-upcase? s))) str*))
(define needles
(map (lambda (v case-sensitive?) (normalize v case-sensitive?))
str* case-sensitive?*))
(and hay
(andmap
(if re:split
(lambda (n case-sensitive?)
(ormap (lambda (s) (string=? s n))
(string-split (normalize hay case-sensitive?) re:split)))
(lambda (n case-sensitive?)
(string-contains? (normalize hay case-sensitive?) n)))
needles case-sensitive?*)))
;; from medikanren/db.rkt:
;;
#;(define (smart-string-matches? case-sensitive? chars:ignore chars:split str* hay)
(define re:ignore (and (non-empty-string? chars:ignore)
(pregexp (string-append "[" chars:ignore "]"))))
(define re:split (and (non-empty-string? chars:split)
(pregexp (string-append "[" chars:split "]"))))
(define (normalize s case-sensitive?)
(define pruned (if re:ignore (string-replace s re:ignore "") s))
(if case-sensitive? pruned (string-downcase pruned)))
(define (contains-upcase? s) (not (string=? s (string-downcase s))))
(define case-sensitive?*
(map (lambda (s) (or case-sensitive? (contains-upcase? s))) str*))
(define needles
(map (lambda (v case-sensitive?) (normalize v case-sensitive?))
str* case-sensitive?*))
(and hay
(andmap
(if re:split
(lambda (n case-sensitive?)
(ormap (lambda (s) (string=? s n))
(string-split (normalize hay case-sensitive?) re:split)))
(lambda (n case-sensitive?)
(string-contains? (normalize hay case-sensitive?) n)))
needles case-sensitive?*)))
(define (split-name-string name)
(string-split name #px"\\s+"))
(define (empty-string? str)
(not (not (regexp-match #px"^[\\s]*$" str))))
(define *verbose* #f)
(define input-response-latency 50)
(define MAX-CHAR-WIDTH 150)
(define smart-column-width-list-box%
(class list-box%
(super-new)
(define (on-size width height)
(super on-size width height)
(set-default-column-widths this))
(override on-size)))
(define (set-default-column-widths list-box)
(define label* (send list-box get-column-labels))
(define num-cols (length label*))
(define window-width (send list-box get-width))
(define min-width 5)
(define max-width 1000)
(define fudge-factor 4) ;; column divider width
(define width (min (max (- (floor (/ window-width num-cols)) fudge-factor)
min-width)
max-width))
(let loop ((col-num (sub1 num-cols)))
(cond
[(zero? col-num) (void)]
[else
(send list-box
set-column-width
col-num
width
min-width
max-width)
(loop (sub1 col-num))])))
(define construct-predicate-label-string
(lambda (pred-string pred-name-list)
(~a
(string-append pred-string
" ("
(foldr (lambda (str1 str2)
(if (equal? "" str2)
(string-append str1 "" str2)
(string-append str1 ", " str2)))
""
pred-name-list)
")")
#:max-width MAX-CHAR-WIDTH #:limit-marker "...")))
(define DECREASES_PREDICATE_PREFIX_STRING "decreases [synthetic]")
(define DECREASES_PREDICATE_STRING
(construct-predicate-label-string DECREASES_PREDICATE_PREFIX_STRING DECREASES_PREDICATE_NAMES))
(define INCREASES_PREDICATE_PREFIX_STRING "increases [synthetic]")
(define INCREASES_PREDICATE_STRING
(construct-predicate-label-string INCREASES_PREDICATE_PREFIX_STRING INCREASES_PREDICATE_NAMES))
(define SYNTHETIC_PREDICATE_PREFIXES (list DECREASES_PREDICATE_PREFIX_STRING
INCREASES_PREDICATE_PREFIX_STRING
))
(define SORT_COLUMN_INCREASING 'sort-column-increasing)
(define SORT_COLUMN_DECREASING 'sort-column-decreasing)
(define *concept-1-column-sort-order*
(vector SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING))
(define *last-concept-1-column-clicked-for-sorting* (box -1))
(define *concept-2-column-sort-order*
(vector SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING))
(define *last-concept-2-column-clicked-for-sorting* (box -1))
(define *concept-X-column-sort-order*
(vector SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_INCREASING
SORT_COLUMN_DECREASING
SORT_COLUMN_DECREASING
SORT_COLUMN_DECREASING
SORT_COLUMN_DECREASING
SORT_COLUMN_DECREASING))
(define *last-concept-X-column-clicked-for-sorting* (box -1))
(define *concept-1-name-string* (box ""))
(define *concept-1-node-normalization-flag* (box #t))
(define *concept-1-lightweight-reasoning-flag* (box #f))
(define *concept-1-choices* (box '()))
(define *predicate-1-choices* (box '()))
(define *concept-2-name-string* (box ""))
(define *concept-2-node-normalization-flag* (box #t))
(define *concept-2-lightweight-reasoning-flag* (box #f))
(define *concept-2-choices* (box '()))
(define *predicate-2-choices* (box '()))
(define *concept-X-choices* (box '()))
(define *full-path-choices* (box '()))
(define *pubmed-choices* (box '()))
;; saved choices used to generate
;; paths when clicking on a concept in the X list box.
(define *solution-concept-1-name-string* (box ""))
(define *solution-concept-2-name-string* (box ""))
(define *solution-concept-1-node-normalization-flag* (box #t))
(define *solution-concept-2-node-normalization-flag* (box #t))
(define *solution-concept-1-lightweight-reasoning-flag* (box #f))
(define *solution-concept-2-lightweight-reasoning-flag* (box #f))
(define *solution-concept-1-choices* (box '()))
(define *solution-concept-2-choices* (box '()))
(define *solution-predicate-1-choices* (box '()))
(define *solution-predicate-2-choices* (box '()))
;; ((pubmed-URL . (publication-date subject-score object-score sentence)) ...)
(define *publications-info-alist* (box '()))
(define *populate-publication-fields*
(lambda args
(error '*populate-publication-fields* "*populate-publication-fields* function not initialized")))
(define (scheduler dependents)
(define mk-thread #f)
(define (kill-and-run p)
(kill-current-thread)
(set! mk-thread (thread p)))
(define (kill-current-thread)
(and mk-thread (begin (kill-thread mk-thread)
(set! mk-thread #f)))
(for-each (lambda (s) (s 'kill)) dependents))
(lambda (op . args)
(case op
((run) (apply kill-and-run args))
((kill) (kill-current-thread))
(else (error "invalid scheduler operation:" op args)))))
(define S (scheduler '()))
(define S:edges S)
(define S:X S)
(define S:C1P S)
(define S:C2P S)
(define S:C1 S)
(define S:C2 S)
(define handle-search-in-Xs
(lambda (search-in-Xs-field
concept-X-list-box
search-in-Xs-previous-button
search-in-Xs-next-button
. rest)
(define direction (if (and (list? rest) (= (length rest) 1)) (car rest) #f))
(define search-str (send search-in-Xs-field get-value))
(define current-selection (send concept-X-list-box get-selection))
(cond
[direction
(define count (send concept-X-list-box get-number))
(define add1/sub1 (case direction
[(previous) sub1]
[(next) add1]
[else (error 'add1/sub1 "unknown direction in inc/dec")]))
(define found-selection
(and (> count 0)
(let loop ((i (add1/sub1 current-selection)))
(cond
[(>= i count) (loop 0)]
[(< i 0) (loop (- count 1))]
[else
(define data (send concept-X-list-box get-data i))
(define name-str (list-ref data 2))
(define matches?
(smart-string-matches? #f
chars:ignore-typical
""
(string-split search-str " ")
name-str))
(cond
[matches? i]
[(= i current-selection)
;; wrapped around without a match
#f]
[else (loop (add1/sub1 i))])]))))
(if found-selection
(when (not (equal? found-selection current-selection))
(when current-selection
(send concept-X-list-box select current-selection #f))
(send concept-X-list-box select found-selection #t)
(send concept-X-list-box set-first-visible-item found-selection))
(begin
(when current-selection
(send concept-X-list-box select current-selection #f))))]
[(empty-string? search-str)
(when current-selection
(send concept-X-list-box select current-selection #f))
(send search-in-Xs-previous-button enable #f)
(send search-in-Xs-next-button enable #f)]
[else
(define count (send concept-X-list-box get-number))
(define found-selection
(and (> count 0)
(let loop ((i 0))
(cond
[(>= i count) #f]
[else
(define data (send concept-X-list-box get-data i))
(define name-str (list-ref data 2))
(define matches?
(smart-string-matches? #f
chars:ignore-typical
""
(string-split search-str " ")
name-str))
(if matches?
i
(loop (add1 i)))]))))
(if found-selection
(begin
(send search-in-Xs-previous-button enable #t)
(send search-in-Xs-next-button enable #t))
(begin
(send search-in-Xs-previous-button enable #f)
(send search-in-Xs-next-button enable #f)))
(if found-selection
(when (not (equal? found-selection current-selection))
(when current-selection
(send concept-X-list-box select current-selection #f))
(send concept-X-list-box select found-selection #t)
(send concept-X-list-box set-first-visible-item found-selection))
(begin
(when current-selection
(send concept-X-list-box select current-selection #f))))])))
(define (convert-concept-1/2-to-list-box-format concept)
(match concept
[`(,dbname ,curie ,name ,cat)
(list (format "~a" dbname)
(~a curie #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(format "~a" cat)
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "..."))]))
(define (convert-X-concept-to-list-box-format concept)
(match concept
[`(,dbname ,curie ,name ,cat ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-length ,confidence)
(list (format "~a" dbname)
(~a curie #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a cat #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(format "~a" max-pubmed-count)
(format "~a" min-pubmed-count)
(string-join pred-names ", ")
(format "~a" path-length)
(format "~a" confidence))]))
(define (convert-concept-1/2-to-column-sorting-format concept)
(match concept
[`(,dbname ,curie ,name ,cat)
(list (format "~a" dbname)
(~a curie #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a cat #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "..."))]))
(define (convert-X-concept-to-column-sorting-format concept)
(match concept
[`(,dbname ,curie ,name ,cat ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-length ,confidence)
(list (format "~a" dbname)
(~a curie #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a cat #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
max-pubmed-count
min-pubmed-count
(string-join pred-names ", ")
path-length
confidence)]))
(define (make-send-concepts-to-concept-1/2-list-box concept-1/2-list-box-thunk)
(lambda (concepts)
(define concept-1/2-list-box (concept-1/2-list-box-thunk))
(define formatted-concepts (map convert-concept-1/2-to-list-box-format concepts))
(send concept-1/2-list-box
set
(map (lambda (e) (list-ref e 0)) formatted-concepts)
(map (lambda (e) (list-ref e 1)) formatted-concepts)
(map (lambda (e) (list-ref e 2)) formatted-concepts)
(map (lambda (e) (list-ref e 3)) formatted-concepts))))
(define (make-send-concepts-to-concept-X-list-box concept-X-list-box)
(lambda (concepts)
(define formatted-concepts (map convert-X-concept-to-list-box-format concepts))
(send concept-X-list-box
set
(map (lambda (e) (list-ref e 0)) formatted-concepts)
(map (lambda (e) (list-ref e 1)) formatted-concepts)
(map (lambda (e) (list-ref e 2)) formatted-concepts)
(map (lambda (e) (list-ref e 3)) formatted-concepts)
(map (lambda (e) (list-ref e 4)) formatted-concepts)
(map (lambda (e) (list-ref e 5)) formatted-concepts)
(map (lambda (e) (list-ref e 6)) formatted-concepts)
(map (lambda (e) (list-ref e 7)) formatted-concepts)
(map (lambda (e) (list-ref e 8)) formatted-concepts))))
(define (handle-sort-by-column-header-click event
list-box
last-column-clicked-for-sorting-box
column-sort-order-vector
choices-box
convert-values-to-column-sorting-format
send-values-to-list-box)
(printf "handle-sort-by-column-header-click called\n")
;; get previously selected choice's data, if any
(define current-selection (send list-box get-selection))
(printf "current-selection: ~s\n" current-selection)
(define current-selection-data (and current-selection
(send list-box get-data current-selection)))
(printf "current-selection-data: ~s\n" current-selection-data)
(when current-selection
(send list-box select current-selection #f))
;; sort by column
(define column-clicked (send event get-column))
(define last-column-clicked (unbox last-column-clicked-for-sorting-box))
(define sort-order (vector-ref column-sort-order-vector column-clicked))
;; swap sort order if user clicks on same column twice in a row
(when (= column-clicked last-column-clicked)
(set! sort-order
(if (eqv? sort-order SORT_COLUMN_INCREASING)
SORT_COLUMN_DECREASING
SORT_COLUMN_INCREASING))
(vector-set! column-sort-order-vector
column-clicked
sort-order))
(printf "sorting by column ~s in ~s order\n" column-clicked sort-order)
(define choices (unbox choices-box))
(define sorted-choices (sort choices
(lambda (c1 c2)
(let ((fc1 (convert-values-to-column-sorting-format c1))
(fc2 (convert-values-to-column-sorting-format c2)))
(let ((v1 (list-ref fc1 column-clicked))
(v2 (list-ref fc2 column-clicked)))
(let ((num-compare
(if (eqv? sort-order SORT_COLUMN_INCREASING)
<
>))
(string-compare
(if (eqv? sort-order SORT_COLUMN_INCREASING)
string<?
string>?)))
(if (and (number? v1) (number? v2))
(num-compare v1 v2)
(string-compare (string-downcase v1)
(string-downcase v2)))))))))
(set-box! last-column-clicked-for-sorting-box column-clicked)
(set-box! choices-box sorted-choices)
(send-values-to-list-box sorted-choices)
;; add choice data to each list-box entry
(define len (length sorted-choices))
(let loop ((i 0)
(c* sorted-choices))
(cond
[(= len i) (void)]
[else
(send list-box set-data i (car c*))
(loop (add1 i)
(cdr c*))]))
;; select previously selected choice in its new location, if any
(when (and current-selection current-selection-data)
(define count (send list-box get-number))
(printf "count: ~s\n" count)
(define new-selection
(let loop ((i 0))
(cond
[(>= i count) #f]
[else
(let ((d (send list-box get-data i)))
(printf "--------\n")
(printf "d: ~s\n" d)
(printf "(equal? d current-selection-data): ~s\n" (equal? d current-selection-data))
(if (equal? d current-selection-data)
i
(loop (add1 i))))])))
(printf "new-selection: ~s\n" new-selection)
(when new-selection
(send list-box select new-selection #t)
(send list-box set-first-visible-item new-selection)))
(void))
(define (concept-list parent
parent-search/normalize/lw-panel
parent-list-boxes-panel
label
name-string
node-normalization-flag
lightweight-reasoning-flag
choices
predicate-list-box-thunk
predicate-choices
edge-type
last-column-clicked-for-sorting-box
column-sort-order-vector
choices-box
convert-values-to-column-sorting-format
send-values-to-list-box
S:C S:CP)
(define name-field (new text-field%
(label label)
(parent parent-search/normalize/lw-panel)
(init-value "")
(callback (lambda (self event)
(define name (send self get-value))
(set-box! name-string name)
(set-box! predicate-choices '())
(send (predicate-list-box-thunk) set '())
(handle)))))
(define node-normalization-field (new check-box%
(parent parent-search/normalize/lw-panel)
(label "Show concept synonyms for CURIE searches")
(value #t)
(callback (lambda (self event) (handle)))))
(define lightweight-reasoning-field (new check-box%
(parent parent-search/normalize/lw-panel)
(label "Use lightweight reasoning")
(value #f)
;; TODO remove the 'deleted style to show the checkbox
(style '(deleted))
(callback (lambda (self event) (handle)))))
(define concept-listbox (new smart-column-width-list-box%
(label label)
(choices '())
(columns '("KG" "CURIE" "Category" "Name"))
(parent parent-list-boxes-panel)
(style '(column-headers clickable-headers reorderable-headers extended))
(callback (lambda (self event)
(define event-type (send event get-event-type))
(cond
[(eqv? event-type 'list-box-column)
(handle-sort-by-column-header-click
event
concept-listbox
last-column-clicked-for-sorting-box
column-sort-order-vector
choices-box
convert-values-to-column-sorting-format
send-values-to-list-box)]
[else
(define selections (send self get-selections))
(define selected-concepts
(foldr (lambda (i l) (cons (list-ref (unbox choices) i) l)) '() selections))
(when *verbose*
(printf "selected concepts:\n")
(print-short-concept-description selected-concepts)
;; Old mediKanren 1 GUI code:
;; (printf "selected concepts:\n~s\n" selected-concepts)
)
(S:CP 'run
(thunk
(define preds-by-concept
(time (case edge-type
[(in-edge) (map caddr (find-predicates/concepts #f #t selected-concepts))]
[(out-edge) (map cadr (find-predicates/concepts #t #f selected-concepts))]
[else (error 'concept-listbox/predicates)])))
(define predicates
(begin
(printf "preds-by-concept:\n~s\n" preds-by-concept)
(sort (remove-duplicates (apply append preds-by-concept)) string<?)
;; Old mediKanren 1 GUI code:
;; (sort (remove-duplicates (map cddr (append* preds-by-concept))) string<?)
))
(define (create-increase/decrease-syn-pred-list
syn-pred-prefix predicate-names selected-predicates)
(let ((inter (sort (set-intersect predicate-names selected-predicates)
string<?)))
(if (not (null? inter))
(let ((str (string-append syn-pred-prefix " (" (string-join inter ", ") ")")))
(let ((safe-string (~a str #:max-width MAX-CHAR-WIDTH #:limit-marker "...")))
(list safe-string)))
'())))
(define decreases-synthetic-predicate-string-list
(create-increase/decrease-syn-pred-list
DECREASES_PREDICATE_PREFIX_STRING DECREASES_PREDICATE_NAMES predicates))
(define increases-synthetic-predicate-string-list
(create-increase/decrease-syn-pred-list
INCREASES_PREDICATE_PREFIX_STRING INCREASES_PREDICATE_NAMES predicates))
(set! predicates (append
decreases-synthetic-predicate-string-list
increases-synthetic-predicate-string-list
predicates))
(printf "predicates: ~s\n" predicates)
(set-box! predicate-choices predicates)
(send (predicate-list-box-thunk) set predicates)
;; unselect all items
(for ([i (length predicates)])
(send (predicate-list-box-thunk) select i #f))))])))))
(define (mk-run)
(let* ((subject? (case edge-type
[(out-edge) #t]
[(in-edge) #f]))
(object? (case edge-type
[(out-edge) #f]
[(in-edge) #t]))
(string-parts (split-name-string current-name))
(ans (cond
((null? string-parts) '())
((curie-string current-name) =>
(lambda (cs)
(printf "treating '~s' as a single CURIE\n" cs)
(printf "performing CURIE search for: ~s\n" cs)
(let ((synonyms (if (unbox node-normalization-flag)
(set->list
(set-union
(set cs)
(list->set
(run* x (kgx-synonym cs x)))))
(list cs))))
(printf "found synonyms:\n~s\n" synonyms)
(time (let ((result
(map (lambda (curie)
(run*/set ans
(fresh (dbname eid s o name cat)
(== `(,dbname ,curie ,name ,cat) ans)
(if subject?
(== curie s)
(== curie o))
(edge `(,dbname . ,eid) s o)
(cprop curie "name" name)
(cprop curie "category" cat))))
synonyms)))
(if (null? result)
'()
(set->list (apply set-union result))))))))
(else
(printf "treating '~s' as a non-CURIE search\n" string-parts)
(printf "performing search for: ~s\n" string-parts)
(let ((string-search-curies (find-ids-named rtx:cprop string-parts)))
(time (let ((result
(map (lambda (curie)
(run*/set ans
(fresh (dbname eid s o name cat)
(== `(,dbname ,curie ,name ,cat) ans)
(if subject?
(== curie s)
(== curie o))
(edge `(,dbname . ,eid) s o)
(cprop curie "name" name)
(cprop curie "category" cat))))
string-search-curies)))
(if (null? result)
'()
(set->list (apply set-union result))))))
;; Old mediKanren 1 GUI code:
;; (time (find-concepts/options/cui-infer subject? object? isa-count string-parts))
))))
;; (printf "ans:\n~s\n" ans)
(set-box! choices ans)