forked from webyrd/mediKanren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui-simple-v2.rkt
1808 lines (1575 loc) · 92.8 KB
/
gui-simple-v2.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
#lang racket
(require
"../common.rkt"
racket/sandbox
racket/gui/base
framework
racket/engine
racket/date
racket/string
net/sendurl
"../db.rkt"
(except-in racket/match ==)
(only-in srfi/1 iota))
(provide
launch-gui)
(define MEDIKANREN_VERSION_STRING "mediKanren Explorer 0.2.30")
(define argv (current-command-line-arguments))
(define argv-optional '#(CONFIG_FILE))
(when (not (<= (vector-length argv) (vector-length argv-optional)))
(error "optional arguments ~s; given ~s" argv-optional argv))
(displayln "Starting mediKanren Explorer...")
(newline)
(displayln "************************************************")
(displayln "*** mediKanren is for research purposes only ***")
(displayln "************************************************")
(newline)
(displayln MEDIKANREN_VERSION_STRING)
;; Loading will occur at first use if not explicitly forced like this.
(load-config #t (and (<= 1 (vector-length argv)) (vector-ref argv 0)))
(load-databases #t)
;;; Query save file settings
(define WRITE_QUERY_RESULTS_TO_FILE (config-ref 'query-results.write-to-file?))
(define QUERY_RESULTS_FILE_NAME (config-ref 'query-results.file-name))
(define HUMAN_FRIENDLY_QUERY_RESULTS_FILE_NAME (config-ref 'query-results.file-name-human))
(define SPREADSHEET_FRIENDLY_QUERY_RESULTS_FILE_NAME (config-ref 'query-results.file-name-spreadsheet))
(define QUERY_RESULTS_FILE_MODE (config-ref 'query-results.file-mode))
;;; Initial window size
(define HORIZ-SIZE (config-ref 'initial-window-size.horizontal))
(define VERT-SIZE (config-ref 'initial-window-size.vertical))
;;; Decreases/increases predicate names
(define DECREASES_PREDICATE_NAMES (config-ref 'decreases-predicate-names))
(define INCREASES_PREDICATE_NAMES (config-ref 'increases-predicate-names))
#|
concept format (subject or object), without dbname at front:
`(,cid ,cui ,name (,catid . ,cat) ,props)
concept format (subject or object), with dbname at front:
`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
edge format, without dbname at front:
`(,eid (,scid ,scui ,sname (,scatid . ,scat) ,sprops)
(,ocid ,ocui ,oname (,ocatid . ,ocat) ,oprops)
(,pid . ,pred) ,eprops)
edge format, with dbname at front (as used in edgeo):
`(,dbname ,eid (,scid ,scui ,sname (,scatid . ,scat) ,sprops)
(,ocid ,ocui ,oname (,ocatid . ,ocat) ,oprops)
(,pid . ,pred) ,eprops)
|#
(define (split-name-string name)
(string-split name #px"\\s+"))
(define (empty-string? str)
(not (not (regexp-match #px"^[\\s]*$" str))))
(define *verbose* #t)
(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-isa-flag* (box #f))
(define *concept-1-choices* (box '()))
(define *predicate-1-choices* (box '()))
(define *concept-2-name-string* (box ""))
(define *concept-2-isa-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-isa-flag* (box #f))
(define *solution-concept-2-isa-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)
;; TODO: ideally we would tier schedulers so that independent processes could
;; be run concurrently. Unfortunately, the underlying database does not
;; currently use its ports in a thread-safe manner. Fix this.
;(define S:edges (scheduler '()))
;(define S:X (scheduler (list S:edges)))
;(define S:C1P (scheduler (list S:X)))
;(define S:C2P (scheduler (list S:X)))
;(define S:C1 (scheduler (list S:C1P)))
;(define S:C2 (scheduler (list S:C2P)))
(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 3))
(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 3))
(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 ,cid ,cui ,name (,catid . ,cat) ,props)
(list (format "~a" dbname)
(format "~a" cid)
(~a cui #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(format "~a" `(,catid . ,cat))
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "..."))]))
(define (convert-X-concept-to-list-box-format concept)
(match concept
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-length ,confidence)
(list (format "~a" dbname)
(format "~a" cid)
(~a cui #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
(format "~a" `(,catid . ,cat))
(~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 ,cid ,cui ,name (,catid . ,cat) ,props)
(list (format "~a" dbname)
cid
(~a cui #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
catid
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "..."))]))
(define (convert-X-concept-to-column-sorting-format concept)
(match concept
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-length ,confidence)
(list (format "~a" dbname)
cid
(~a cui #:max-width MAX-CHAR-WIDTH #:limit-marker "...")
catid
(~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)
(map (lambda (e) (list-ref e 4)) 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)
(map (lambda (e) (list-ref e 9)) 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/isa-panel
parent-list-boxes-panel
label
name-string
isa-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/isa-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 isa-field (new check-box%
(parent parent-search/isa-panel)
(label "Include ISA-related concepts")
(value #f)
(callback (lambda (self event) (handle)))))
(define concept-listbox (new smart-column-width-list-box%
(label label)
(choices '())
(columns '("KG" "CID" "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~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
(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* ((isa-count (if current-isa 50 0)) ;; Only grab the first 50. 50 should probably be a parameter.
(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 (if (null? string-parts) '()
(begin (printf "searching for: ~s\n" current-name)
(time (find-concepts/options/cui-infer subject? object? isa-count string-parts))))))
(set-box! choices ans)
(send concept-listbox
set
(map (lambda (x)
(match x
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
(~a dbname #:max-width MAX-CHAR-WIDTH #:limit-marker "...")]))
ans)
(map (lambda (x)
(match x
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
(format "~a" cid)]))
ans)
(map (lambda (x)
(match x
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
(~a cui #:max-width MAX-CHAR-WIDTH #:limit-marker "...")]))
ans)
(map (lambda (x)
(match x
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
(~a `(,catid . ,cat) #:max-width MAX-CHAR-WIDTH #:limit-marker "...")]))
ans)
(map (lambda (x)
(match x
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)
(~a name #:max-width MAX-CHAR-WIDTH #:limit-marker "...")]))
ans))
;; add choice data to each list-box entry
(define len (length (unbox choices)))
(let loop ((i 0)
(c* (unbox choices)))
(cond
[(= len i) (void)]
[else
(send concept-listbox set-data i (car c*))
(loop (add1 i)
(cdr c*))]))
;; unselect all items
(for ([i (length ans)])
(send concept-listbox select i #f))))
(define current-name "")
(define current-isa #f)
(define pending-name current-name)
(define timer (new timer% (notify-callback (thunk (S:C 'run mk-run)))))
(define (handle)
(define new-name (send name-field get-value))
(define new-isa (send isa-field get-value))
(when (not (and (equal? current-name new-name)
(equal? current-isa new-isa)))
(set! current-name new-name)
(set! current-isa new-isa)
(set-box! isa-flag current-isa)
(S:C 'kill)
(send timer stop)
(send timer start input-response-latency #t)))
concept-listbox)
(define (launch-gui)
;; (launch-gene-window)
(launch-main-window))
(define (launch-main-window)
(let ((frame (new frame%
(label MEDIKANREN_VERSION_STRING)
(width HORIZ-SIZE)
(height VERT-SIZE))))
(define outer-vert-draggable-panel (new panel:vertical-dragable%
(parent frame)
(alignment '(left center))))
(define upper-pane (new panel:vertical-dragable%
(parent outer-vert-draggable-panel)
(alignment '(left center))))
(define lower-pane (new panel:vertical-dragable%
(parent outer-vert-draggable-panel)
(alignment '(left center))))
(define go-callback
(lambda (button event)
(send running-status-description set-label "Running...")
(define concept-1-selections (send concept-1-list-box get-selections))
(define concept-2-selections (send concept-2-list-box get-selections))
(define concept-1-selected-concepts
(foldr (lambda (i l) (cons (list-ref (unbox *concept-1-choices*) i) l))
'()
concept-1-selections))
(define concept-2-selected-concepts
(foldr (lambda (i l) (cons (list-ref (unbox *concept-2-choices*) i) l))
'()
concept-2-selections))
(printf "concept-1-selections: ~s\n" concept-1-selections)
(displayln concept-1-selected-concepts)
(printf "---------------------------------\n")
(printf "concept-2-selections: ~s\n" concept-2-selections)
(displayln concept-2-selected-concepts)
(printf "---------------------------------\n")
(define predicate-1-selections (send predicate-1-list-box get-selections))
(define predicate-2-selections (send predicate-2-list-box get-selections))
(define predicate-1-selected-predicates
(foldr (lambda (i l) (cons (list-ref (unbox *predicate-1-choices*) i) l))
'()
predicate-1-selections))
(define predicate-2-selected-predicates
(foldr (lambda (i l) (cons (list-ref (unbox *predicate-2-choices*) i) l))
'()
predicate-2-selections))
(printf "predicate-1-selections: ~s\n" predicate-1-selections)
(displayln predicate-1-selected-predicates)
(printf "---------------------------------\n")
(printf "predicate-2-selections: ~s\n" predicate-2-selections)
(displayln predicate-2-selected-predicates)
(printf "---------------------------------\n")
(S:X 'run (thunk (find-X-concepts concept-1-selected-concepts
concept-2-selected-concepts
predicate-1-selected-predicates
predicate-2-selected-predicates
(unbox *predicate-1-choices*)
(unbox *predicate-2-choices*)
concept-X-list-box
running-status-description
full-path-list-box
subject-properties-list-box
edge-properties-list-box
object-properties-list-box
pubmed-list-box
search-in-Xs-field
search-in-Xs-previous-button
search-in-Xs-next-button)))))
(define concept-1-overall-pane (new vertical-pane%
(parent upper-pane)
(alignment '(left center))))
(define concept-1-search/isa-panel (new panel:horizontal-dragable%
(parent concept-1-overall-pane)
(alignment '(left center))
(stretchable-height #f)))
(define concept-1-list-boxes-panel (new panel:horizontal-dragable%
(parent concept-1-overall-pane)
(alignment '(left center))))
(define concept-1-list-box (concept-list concept-1-overall-pane
concept-1-search/isa-panel
concept-1-list-boxes-panel
"Concept 1"
*concept-1-name-string*
*concept-1-isa-flag*
*concept-1-choices*
(lambda () predicate-1-list-box)
*predicate-1-choices*
'out-edge
*last-concept-1-column-clicked-for-sorting*
*concept-1-column-sort-order*
*concept-1-choices*
convert-concept-1/2-to-column-sorting-format
(make-send-concepts-to-concept-1/2-list-box (lambda () concept-1-list-box))
S:C1 S:C1P))
(define predicate-1-list-box (new list-box%
(label "Predicate 1")
(choices (unbox *predicate-1-choices*))
(columns '("Name"))
(parent concept-1-list-boxes-panel)
(style '(extended))
(callback go-callback)))
(define edge-description (new message%
(parent concept-1-overall-pane)
(label "Concept 1 -> Predicate 1 -> [X] -> Predicate 2 -> Concept 2")))
(define concept-2-overall-pane (new vertical-pane%
(parent upper-pane)
(alignment '(left center))))
(define concept-2-search/isa-panel (new panel:horizontal-dragable%
(parent concept-2-overall-pane)
(alignment '(left center))
(stretchable-height #f)))
(define concept-2-list-boxes-panel (new panel:horizontal-dragable%
(parent concept-2-overall-pane)
(alignment '(left center))))
(define predicate-2-list-box (new list-box%
(label "Predicate 2")
(choices (unbox *predicate-2-choices*))
(columns '("Name"))
(parent concept-2-list-boxes-panel)
(style '(extended))
(callback go-callback)))
(define concept-2-list-box (concept-list concept-2-overall-pane
concept-2-search/isa-panel
concept-2-list-boxes-panel
"Concept 2"
*concept-2-name-string*
*concept-2-isa-flag*
*concept-2-choices*
(lambda () predicate-2-list-box)
*predicate-2-choices*
'in-edge
*last-concept-2-column-clicked-for-sorting*
*concept-2-column-sort-order*
*concept-2-choices*
convert-concept-1/2-to-column-sorting-format
(make-send-concepts-to-concept-1/2-list-box (lambda () concept-2-list-box))
S:C2 S:C2P))
(define running-status-description/search-in-Xs-panel
(new
horizontal-panel%
(parent concept-2-overall-pane)
(alignment '(left center))
(stretchable-height #f)))
(define running-status-description (new message%
(parent running-status-description/search-in-Xs-panel)
(label " ")))
(define search-in-Xs-field (new text-field%
(label "Find in X's")
(parent running-status-description/search-in-Xs-panel)
(init-value "")
(callback (lambda (self event)
(handle-search-in-Xs self
concept-X-list-box
search-in-Xs-previous-button
search-in-Xs-next-button
)))))
(define search-in-Xs-previous-button (new button%
(parent running-status-description/search-in-Xs-panel)
(label "Previous")
(callback (lambda (self event)
(handle-search-in-Xs search-in-Xs-field
concept-X-list-box
search-in-Xs-previous-button
search-in-Xs-next-button
'previous)))))
(define search-in-Xs-next-button (new button%
(parent running-status-description/search-in-Xs-panel)
(label "Next")
(callback (lambda (self event)
(handle-search-in-Xs search-in-Xs-field
concept-X-list-box
search-in-Xs-previous-button
search-in-Xs-next-button
'next)))))
(define concept-X-list-box (new smart-column-width-list-box%
(label "X")
(choices (unbox *concept-X-choices*))
(columns '("KG" "CID" "CURIE" "Category" "Name" "Max PubMed #" "Min PubMed #" "Predicates" "Path Length" "Path Confidence"))
(parent lower-pane)
(style '(column-headers clickable-headers reorderable-headers single))
(callback (lambda (self event)
(define event-type (send event get-event-type))
(S:edges 'run
(thunk
(cond
[(eqv? event-type 'list-box-column)
(handle-sort-by-column-header-click
event
concept-X-list-box
*last-concept-X-column-clicked-for-sorting*
*concept-X-column-sort-order*
*concept-X-choices*
convert-X-concept-to-column-sorting-format
(make-send-concepts-to-concept-X-list-box self))]
[(eqv? event-type 'list-box-dclick)
(printf "double-click!! copy name of the concept to the clipboard\n")
(define time-stamp (send event get-time-stamp))
(printf "time stamp: ~s\n" time-stamp)
(define concept-name
(let ((sel* (send concept-X-list-box get-selections)))
(if (= (length sel*) 1)
(let ((selected-X (list-ref (unbox *concept-X-choices*) (car sel*))))
(match selected-X
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-count ,confidence)
name]
[else ""]))
"")))
(printf "concept name: ~s\n" concept-name)
(send the-clipboard set-clipboard-string concept-name time-stamp)]
[else
;; empty the entries in the full-path-list-box
(send full-path-list-box set '() '() '() '() '() '() '() '())
;; empty the entries in the properties list-boxes
(send subject-properties-list-box set '() '())
(send edge-properties-list-box set '() '())
(send object-properties-list-box set '() '())
;; empty the entries in the pubmed-list-box
(send pubmed-list-box set '())
(let ((sel* (send concept-X-list-box get-selections)))
(when (= (length sel*) 1)
(let ((selected-X (list-ref (unbox *concept-X-choices*) (car sel*))))
(let ((selected-X
(match selected-X
[`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props ,max-pubmed-count ,min-pubmed-count ,pred-names ,path-count ,confidence)
`(,dbname ,cid ,cui ,name (,catid . ,cat) ,props)])))
(printf "selected ~s\n" selected-X)
(define concept-1* (unbox *solution-concept-1-choices*))
(define concept-2* (unbox *solution-concept-2-choices*))
(printf "concept-1* ~s\n" concept-1*)
(printf "concept-2* ~s\n" concept-2*)
(define predicate-1* (unbox *solution-predicate-1-choices*))
(define predicate-2* (unbox *solution-predicate-2-choices*))
(printf "predicate-1* ~s\n" predicate-1*)
(printf "predicate-2* ~s\n" predicate-2*)
(define atomic/synthetic-predicate-1* (split-atomic/synthetic-predicates (unbox *predicate-1-choices*) predicate-1*))
(define atomic/synthetic-predicate-2* (split-atomic/synthetic-predicates (unbox *predicate-2-choices*) predicate-2*))
(define atomic-predicate-1* (car atomic/synthetic-predicate-1*))
(define atomic-predicate-2* (car atomic/synthetic-predicate-2*))
(define synthetic-predicate-1* (cadr atomic/synthetic-predicate-1*))
(define synthetic-predicate-2* (cadr atomic/synthetic-predicate-2*))
(define paths '())
(cond
[(and
(null?
(split-name-string
(unbox *solution-concept-1-name-string*)))
(null?
(split-name-string
(unbox *solution-concept-2-name-string*))))
(set! paths '())]
[(null? (split-name-string (unbox *solution-concept-1-name-string*)))
(set! paths '())
;; run synthetic queries here
(set! paths
(remove-duplicates
(append paths
(run* (q)
(fresh (e dbname eid x o pid pred eprops)
(== (list `(,dbname ,eid ,x ,o (,pid . ,pred) ,eprops)) q)
(== `(,dbname . ,x) selected-X)
(== `(,dbname ,eid ,x ,o (,pid . ,pred) ,eprops) e)
(membero `(,dbname . ,o) concept-2*)
(membero pred atomic-predicate-2*)
(edgeo e))))))]
[(null? (split-name-string (unbox *solution-concept-2-name-string*)))
(set! paths '())
;; run synthetic queries here
(set! paths
(remove-duplicates
(append paths
(run* (q)
(fresh (e dbname eid s x pid pred eprops)
(== (list `(,dbname ,eid ,s ,x (,pid . ,pred) ,eprops)) q)
(== `(,dbname . ,x) selected-X)
(== `(,dbname ,eid ,s ,x (,pid . ,pred) ,eprops) e)
(membero `(,dbname . ,s) concept-1*)
(membero pred atomic-predicate-1*)
(edgeo e))))))]
[else
(set! paths '())
;; run synthetic queries here
(set! paths
(remove-duplicates
(append paths
(run* (q)
(fresh (e1 e2 dbname eid1 eid2 s x o pid1 pid2 p1 p2 eprops1 eprops2)
(== `(,dbname . ,x) selected-X)
(== (list
`(,dbname ,eid1 ,s ,x (,pid1 . ,p1) ,eprops1)
`(,dbname ,eid2 ,x ,o (,pid2 . ,p2) ,eprops2))