forked from intermine/im-tables-3
/
subs.cljs
389 lines (332 loc) · 12.5 KB
/
subs.cljs
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
(ns im-tables.subs
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :as re-frame :refer [reg-sub subscribe]]
[clojure.string :as string]
[imcljs.internal.utils :refer [scrub-url]]
[imcljs.query :as im-query]))
(defn glue [path remainder-vec]
(reduce conj (or path []) remainder-vec))
(reg-sub
:main/query-response
(fn [db [_ prefix]]
(get-in db (glue prefix [:response]))))
(reg-sub
:main/query
(fn [db [_ prefix]]
(get-in db (glue prefix [:query]))))
(reg-sub
:main/query-constraints
(fn [[_ prefix]]
(subscribe [:main/query prefix]))
(fn [query [_ _prefix]]
(:where query)))
(reg-sub
:main/temp-query
(fn [db [_ prefix]]
(get-in db (glue prefix [:temp-query]))))
(reg-sub
:main/query-parts
(fn [db [_ prefix]]
(get-in db (glue prefix [:query-parts]))))
(reg-sub
:main/query-parts-counts
(fn [db [_ prefix]]
(get-in db (glue prefix [:query-parts-counts]))))
(reg-sub
:main/error
(fn [db [_ prefix]]
(get-in db (glue prefix [:error]))))
(reg-sub
:summary/item-details
(fn [db [_ loc id]]
(get-in db (glue loc [:cache :item-details id]))))
(reg-sub
:style/dragging-item
(fn [db [_ prefix]]
(get-in db (glue prefix [:cache :dragging-item]))))
(reg-sub
:style/dragging-over
(fn [db [_ prefix]]
(get-in db (glue prefix [:cache :dragging-over]))))
(reg-sub
:settings/pagination
(fn [db [_ prefix]]
(get-in db (glue prefix [:settings :pagination]))))
(reg-sub
:settings/data-out
(fn [db [_ prefix]]
(get-in db (glue prefix [:settings :data-out]))))
(reg-sub
:settings/settings
(fn [db [_ prefix]]
(get-in db (glue prefix [:settings]))))
(reg-sub
:settings/cdn
(fn [db [_ prefix]]
(get-in db (glue prefix [:settings :cdn]))))
(reg-sub
:settings/compact
(fn [db [_ prefix]]
(get-in db (glue prefix [:settings :compact]))))
(reg-sub
:summaries/column-summaries
(fn [db [_ prefix]]
(get-in db (glue prefix [:cache :column-summary]))))
(reg-sub
:selection/selections
(fn [db [_ prefix view]]
(get-in db (glue prefix [:cache :column-summary view :selections]))))
(reg-sub
:selection/response
(fn [db [_ prefix view]]
(get-in db (glue prefix [:cache :column-summary view :response]))))
(reg-sub
:selection/text-filter
(fn [db [_ prefix view]]
(get-in db (glue prefix [:cache :column-summary view :filters :text]))))
(reg-sub
:selection/possible-values
(fn [db [_ prefix view]]
(get-in db (glue prefix [:cache :possible-values view]))))
(reg-sub
:assets/service
(fn [db [_ prefix]]
(get-in db (glue prefix [:service]))))
;; We add `:type-constraints` to the model right here in case it is used to
;; traverse a subclass. In some places the constraints of `:temp-query` will be
;; used instead, in which case they assoc `:type-constraints` themselves.
(reg-sub
:assets/model
(fn [[_ prefix]]
[(subscribe [:assets/service prefix])
(subscribe [:main/query-constraints prefix])])
(fn [[service constraints] [_ _prefix]]
(assoc (:model service)
:type-constraints constraints)))
(reg-sub
:tree-view/selection
(fn [db [_ prefix]]
(get-in db (glue prefix [:cache :tree-view :selection]))))
(reg-sub
:modal
(fn [db [_ prefix]]
(get-in db (glue prefix [:cache :modal]))))
(defn head-contains?
"True if a collection's head contains all elements of another collection (sub-coll)
(coll-head-contains? [1 2] [1 2 3 4]) => true
Strings are collections of characters, so this function also mimics clojure.string/starts-with?
(coll-head-contains? apple applejuice) => true"
[sub-coll coll]
(every? true? (map = sub-coll coll)))
(def head-missing? (complement head-contains?))
(defn group-by-starts-with
"Given a substring and a collection of strings, shift all occurences
of strings beginning with that substring to immediately follow the first occurence
ex: (group-by-starts-with [orange apple banana applepie apricot applejuice] apple)
=> [orange apple applepie applejuice banana apricot]"
[string-coll starts-with]
(let [leading (take-while (partial head-missing? starts-with) string-coll)]
(concat leading
(filter (partial head-contains? starts-with) string-coll)
(filter (partial head-missing? starts-with) (drop (count leading) string-coll)))))
(defn replace-join-views
"Remove all occurances of strings in a collection that begin with a value while
replacing the first occurance of the match with the value
ex: (replace-join-views [orange apple applepie applejuice banana apricot] apple)
=> [orange apple banana apricot]"
[string-coll starts-with]
(let [leading (take-while (partial head-missing? starts-with) string-coll)]
(concat leading
[starts-with]
(filter (partial head-missing? starts-with) (drop (count leading) string-coll)))))
(reg-sub
:query-response/views
(fn [db [_ prefix]]
(get-in db (glue prefix [:response :views]))))
(reg-sub
:query/joins
(fn [db [_ prefix]]
(get-in db (glue prefix [:query :joins]))))
(reg-sub
:query/joined-path?
(fn [[_ loc]]
(subscribe [:query/joins loc]))
(fn [joins [_ _prefix path]]
(contains? (set joins) path)))
;; Map from outer joined paths in the query to views descendent of that path.
;; e.g. {"Gene.interactions" ["Gene.interactions.participant2.alleles.primaryIdentifier"
;; "Gene.interactions.participant2.alleles.symbol"
;; "Gene.interactions.participant2.alleles.alleleClass"
;; "Gene.interactions.participant2.alleles.organism.name"]}
(reg-sub
:query-response/joins->views
(fn [[_ loc]]
[(subscribe [:query/joins loc])
(subscribe [:query-response/views loc])])
(fn [[joins views]]
(reduce (fn [m view]
(if-let [matched-join (some #(when (string/starts-with? view (str % ".")) %) joins)]
(update m matched-join (fnil conj []) view)
m))
{}
views)))
(reg-sub
:query-response/joined-views
(fn [[_ loc]]
(subscribe [:query-response/joins->views loc]))
(fn [joins->views [_ _prefix path]]
(get joins->views path)))
; The following two subscriptions do two things to support outer joins, resulting in:
; Gene.secondaryIdentifier Gene.publications.year Gene.symbol Gene.publications.title
; ... with outer joins [Gene.publications]
; Becoming:
; Gene.secondaryIdentifier Gene.publications Gene.symbol
; This could have been done with a single subscription but having a reference
; to the grouped views [:query-response/views-sorted-by-joins] is useful elsewhere
; First move any views that are part of outer joins next to eachother:
(reg-sub
:query-response/views-sorted-by-joins
(fn [[_ loc]]
[(subscribe [:query-response/views loc])
(subscribe [:query/joins loc])])
(fn [[views joins]]
(reduce (fn [total next] (group-by-starts-with total next)) views joins)))
; ...then replace all views that are part of outer joins with the name of the outer joins:
(reg-sub
:query-response/views-collapsed-by-joins
(fn [[_ loc]]
[(subscribe [:query-response/views-sorted-by-joins loc])
(subscribe [:query/joins loc])])
(fn [[views joins]]
(reduce (fn [total next] (replace-join-views total next)) views joins)))
(reg-sub
:rel-manager/query
(fn [db [_ loc]]
(get-in db (glue loc [:cache :rel-manager]))))
;;;;;;;;;;;;;;;; Code generation
(defn generate-javascript [{:keys [cdn query service html? comments?]}]
(string/join "\n"
(-> []
(cond->
(and html? comments?) (conj "<!-- The Element we will target -->")
html? (conj "<div id=\"some-elem\"></div>\n")
(and html? comments?) (conj "<!-- The imtables source -->")
html? (conj (str "<script src=\"" cdn "/js/intermine/im-tables/latest/imtables.js\" charset=\"UTF8\"></script>"))
html? (conj (str "<link href=\"" cdn "/js/intermine/im-tables/latest/main.sandboxed.css\" rel=\"stylesheet\">\n"))
html? (conj "<script>")
(and (not html?) comments?) (conj "/* Install from npm: npm install imtables\n * This snippet assumes the presence on the page of an element like:\n * <div id=\"some-elem\"></div>\n */")
(not html?) (conj "var imtables = require(\"imtables\");\n"))
(conj "var selector = \"#some-elem\";\n")
(conj (str "var service = " (js/JSON.stringify
(clj->js (-> service
(select-keys [:root :token])
(update :root scrub-url))) nil 2) "\n"))
(conj (str "var query = " (js/JSON.stringify (clj->js query) nil 2) "\n"))
(conj (str "imtables.loadTable(\n selector, // Can also be an element, or a jQuery object.\n {\"start\":0,\"size\":25}, // May be null\n {service: service, query: query} // May be an imjs.Query\n).then(\n function (table) { console.log('Table loaded', table); },\n function (error) { console.error('Could not load table', error); }\n);"))
(cond->
html? (conj "</script>")))))
(defn remove-java-comments [s]
(clojure.string/replace s #"/\*([\S\s]*?)\*/" ""))
(defn octo-comment? [s]
(or
(clojure.string/starts-with? s "# ")
(every? true? (map (partial = "#") s))))
(def not-octo-comment? (complement octo-comment?))
(defn remove-octothorpe-comments [s]
(let [lines (clojure.string/split-lines s)]
(clojure.string/replace
(->> lines
(map (fn [line] (if (octo-comment? line) "\n" line)))
(clojure.string/join "\n"))
#"\n\n\n+"
"\n\n")))
(defn format-code [{:keys [lang code comments? html? query service cdn] :as options}]
(cond
(= "js" lang) (when (and query service) (generate-javascript options))
(= "java" lang) (cond-> code (not comments?) remove-java-comments)
(= "xml" lang) code
(or
(= "rb" lang)
(= "py" lang)
(= "pl" lang)) (cond-> code (not comments?) remove-octothorpe-comments)
:else ""))
(reg-sub
:codegen/code
(fn [db [_ loc]]
(get-in db (glue loc [:codegen :code]))))
(reg-sub
:codegen/options
(fn [db [_ loc]]
(get-in db (glue loc [:settings :codegen]))))
(reg-sub
:codegen/formatted-code
(fn [[_ loc]]
[(subscribe [:settings/cdn loc])
(subscribe [:codegen/code loc])
(subscribe [:codegen/options loc])
(subscribe [:main/query loc])
(subscribe [:assets/service loc])])
(fn [[cdn code options query service] [_ _loc]]
(let [{:keys [html? comments? lang]} options]
(when code
(format-code {:lang lang
:code code
:comments? comments?
:html? html?
:query query
:service service
:cdn cdn})))))
(reg-sub
:ui/column-sort-direction
(fn [db [_ loc view]]
(let [sortm (get-in db (glue loc [:query :sortOrder 0]))]
(when (and sortm (= (string/join "." (drop 1 (string/split view ".")))
(:path sortm)))
(:direction sortm)))))
(reg-sub
:filter-manager/filters
(fn [[_ loc]]
(subscribe [:main/temp-query loc]))
(fn [query [_ loc]]
(:where query)))
(reg-sub
:filter-manager/constraint-logic
(fn [[_ loc]]
(subscribe [:main/temp-query loc]))
(fn [query [_ loc]]
(:constraintLogic query)))
(reg-sub
:pick-items/picked
(fn [db [_ loc]]
(get-in db (glue loc [:pick-items :picked]))))
(reg-sub
:pick-items/is-picked?
(fn [[_ loc]]
(subscribe [:pick-items/picked loc]))
(fn [picked [_ _loc id]]
(contains? picked id)))
(reg-sub
:pick-items/class
(fn [db [_ loc]]
(get-in db (glue loc [:pick-items :class]))))
(reg-sub
:export/download-href
(fn [[_ loc]]
[(subscribe [:settings/data-out loc])
(subscribe [:assets/service loc])
(subscribe [:main/query loc])
(subscribe [:assets/model loc])])
(fn [[{:keys [selected-format export-data-package compression]} {:keys [root token]} query model]
[_ _loc]]
(let [fasta? (= selected-format :fasta)]
(str root "/service/query/results" (when fasta? "/fasta")
"?format=" (name selected-format)
"&filename=" "results"
"&query=" (js/encodeURIComponent
(im-query/->xml model (cond-> query
fasta? (assoc :select ["id"]))))
(if export-data-package
"&exportDataPackage=true&compress=zip"
(when compression
(str "&compress=" (name compression))))
"&token=" token))))