@abi-software/flatmapvuer 0.5.7-alpha → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +399 -385
  2. package/LICENSE +201 -201
  3. package/README.md +105 -105
  4. package/babel.config.js +14 -14
  5. package/dist/flatmapvuer.common.js +190 -173
  6. package/dist/flatmapvuer.common.js.map +1 -1
  7. package/dist/flatmapvuer.css +1 -1
  8. package/dist/flatmapvuer.umd.js +190 -173
  9. package/dist/flatmapvuer.umd.js.map +1 -1
  10. package/dist/flatmapvuer.umd.min.js +2 -2
  11. package/dist/flatmapvuer.umd.min.js.map +1 -1
  12. package/package-lock.json +14399 -14390
  13. package/package.json +78 -78
  14. package/public/index.html +17 -17
  15. package/src/App.vue +226 -226
  16. package/src/assets/_variables.scss +43 -43
  17. package/src/assets/styles.scss +7 -7
  18. package/src/components/EventBus.js +2 -2
  19. package/src/components/ExternalResourceCard.vue +98 -98
  20. package/src/components/FlatmapVuer.vue +1841 -1837
  21. package/src/components/MultiFlatmapVuer.vue +529 -523
  22. package/src/components/SelectionsGroup.vue +249 -249
  23. package/src/components/Tooltip.vue +417 -422
  24. package/src/components/TreeControls.vue +231 -231
  25. package/src/components/index.js +9 -9
  26. package/src/components/legends/DynamicLegends.vue +112 -112
  27. package/src/components/legends/SvgLegends.vue +66 -66
  28. package/src/icons/fonts/mapicon-species.eot +0 -0
  29. package/src/icons/fonts/mapicon-species.svg +14 -14
  30. package/src/icons/fonts/mapicon-species.ttf +0 -0
  31. package/src/icons/fonts/mapicon-species.woff +0 -0
  32. package/src/icons/mapicon-species-style.css +42 -42
  33. package/src/legends/legend.svg +25 -25
  34. package/src/main.js +8 -8
  35. package/src/services/flatmapQueries.js +415 -420
  36. package/vue.config.js +31 -31
  37. package/src/nerve-map.js +0 -99
@@ -1,420 +1,415 @@
1
- /* eslint-disable no-alert, no-console */
2
- // remove duplicates by stringifying the objects
3
- const removeDuplicates = function(arrayOfAnything){
4
- return [...new Set(arrayOfAnything.map(e => JSON.stringify(e)))].map(e => JSON.parse(e))
5
- }
6
-
7
- const cachedLabels = {};
8
-
9
- const findTaxonomyLabel = async function(flatmapAPI, taxonomy){
10
- if (cachedLabels && cachedLabels.hasOwnProperty(taxonomy)) {
11
- return cachedLabels[taxonomy];
12
- }
13
-
14
- return new Promise(resolve=>{
15
- fetch(`${flatmapAPI}knowledge/label/${taxonomy}`, {
16
- method: 'GET',
17
- })
18
- .then(response => response.json())
19
- .then(data => {
20
- let label = data.label;
21
- if (label === "Mammalia") {
22
- label = "Mammalia not otherwise specified";
23
- }
24
- cachedLabels[taxonomy] = label;
25
- resolve(label);
26
- })
27
- .catch((error) => {
28
- console.error('Error:', error);
29
- cachedLabels[taxonomy] = taxonomy;
30
- resolve(taxonomy);
31
- })
32
- });
33
- }
34
-
35
- const inArray = function(ar1, ar2){
36
- let as1 = JSON.stringify(ar1)
37
- let as2 = JSON.stringify(ar2)
38
- return as1.indexOf(as2) !== -1
39
- }
40
-
41
- let FlatmapQueries = function(){
42
-
43
- this.initialise = function(flatmapApi){
44
- this.flatmapApi = flatmapApi
45
- this.destinations = []
46
- this.origins = []
47
- this.components = []
48
- this.uberons = []
49
- this.urls = []
50
- this.controller = undefined
51
- this.uberons = []
52
- this.lookUp = []
53
- }
54
-
55
- this.createTooltipData = async function (eventData) {
56
- let hyperlinks = []
57
- if (eventData.feature.hyperlinks && eventData.feature.hyperlinks.length > 0) {
58
- hyperlinks = eventData.feature.hyperlinks
59
- } else {
60
- hyperlinks = this.urls.map(url=>({url: url, id: "pubmed"}))
61
- }
62
- let taxonomyLabel = undefined;
63
- if (eventData.provenanceTaxonomy) {
64
- taxonomyLabel = [];
65
- for (let i = 0; eventData.provenanceTaxonomy.length > i; i++) {
66
- taxonomyLabel.push(await findTaxonomyLabel(this.flatmapAPI, eventData.provenanceTaxonomy[i]))
67
- }
68
- }
69
-
70
- let tooltipData = {
71
- destinations: this.destinations,
72
- origins: this.origins,
73
- components: this.components,
74
- destinationsWithDatasets: this.destinationsWithDatasets,
75
- originsWithDatasets: this.originsWithDatasets,
76
- componentsWithDatasets: this.componentsWithDatasets,
77
- title: eventData.label,
78
- featureId: eventData.resource,
79
- hyperlinks: hyperlinks,
80
- provenanceTaxonomy: eventData.provenanceTaxonomy,
81
- provenanceTaxonomyLabel: taxonomyLabel
82
- }
83
- return tooltipData
84
- }
85
-
86
- this.createComponentsLabelList = function(components, lookUp){
87
- let labelList = []
88
- components.forEach(n=>{
89
- labelList.push(this.createLabelFromNeuralNode(n[0]), lookUp)
90
- if (n.length === 2){
91
- labelList.push(this.createLabelFromNeuralNode(n[1]), lookUp)
92
- }
93
- })
94
- return labelList
95
- }
96
-
97
- this.createLabelLookup = function(uberons) {
98
- return new Promise(resolve=> {
99
- let uberonMap = {}
100
- const data = { sql: this.buildLabelSqlStatement(uberons)}
101
- fetch(`${this.flatmapApi}knowledge/query/`, {
102
- method: 'POST',
103
- headers: {
104
- 'Content-Type': 'application/json',
105
- },
106
- body: JSON.stringify(data),
107
- })
108
- .then(response => response.json())
109
- .then(payload => {
110
- const entity = payload.keys.indexOf("entity");
111
- const label = payload.keys.indexOf("label");
112
- if (entity > -1 && label > -1) {
113
- payload.values.forEach(pair => {
114
- uberonMap[pair[entity]] = pair[label];
115
- });
116
- }
117
- resolve(uberonMap)
118
- })
119
- })
120
- }
121
-
122
- this.buildConnectivitySqlStatement = function (keastIds) {
123
- let sql = 'select knowledge from knowledge where entity in ('
124
- if (keastIds.length === 1) {
125
- sql += `'${keastIds[0]}')`
126
- } else if (keastIds.length > 1) {
127
- for (let i in keastIds) {
128
- sql += `'${keastIds[i]}'${i >= keastIds.length - 1 ? ')' : ','} `
129
- }
130
- }
131
- return sql
132
- }
133
-
134
- this.buildLabelSqlStatement = function (uberons) {
135
- let sql = 'select entity, label from labels where entity in ('
136
- if (uberons.length === 1) {
137
- sql += `'${uberons[0]}')`
138
- } else if (uberons.length > 1) {
139
- for (let i in uberons) {
140
- sql += `'${uberons[i]}'${i >= uberons.length - 1 ? ')' : ','} `
141
- }
142
- }
143
- return sql
144
- }
145
-
146
- this.findAllIdsFromConnectivity = function (connectivity) {
147
- let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
148
- let nodes = [...new Set(dnodes)] // remove duplicates
149
- let found = []
150
- nodes.forEach(n => {
151
- if (Array.isArray(n)) {
152
- found.push(n.flat())
153
- } else {
154
- found.push(n)
155
- }
156
- })
157
- this.uberons = [... new Set(found.flat())]
158
- return this.uberons
159
- }
160
-
161
- this.flattenConntectivity = function (connectivity) {
162
- let dnodes = connectivity.flat() // get nodes from edgelist
163
- let nodes = [...new Set(dnodes)] // remove duplicates
164
- let found = []
165
- nodes.forEach(n => {
166
- if (Array.isArray(n)) {
167
- found.push(n.flat())
168
- } else {
169
- found.push(n)
170
- }
171
- })
172
- return found.flat()
173
- }
174
-
175
- this.findComponents = function (connectivity) {
176
- let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
177
- let nodes = removeDuplicates(dnodes)
178
-
179
- let found = []
180
- let terminal = false
181
- nodes.forEach(node => {
182
- terminal = false
183
- // Check if the node is an destination or origin (note that they are labelled dendrite and axon as opposed to origin and destination)
184
- if (inArray(connectivity.axons, node)) {
185
- terminal = true
186
- }
187
- if (inArray(connectivity.dendrites, node)) {
188
- terminal = true
189
- }
190
- if (!terminal) {
191
- found.push(node)
192
- }
193
- })
194
-
195
- return found
196
- }
197
-
198
- this.retrieveFlatmapKnowledgeForEvent = async function(eventData){
199
- // check if there is an existing query
200
- if (this.controller) this.controller.abort();
201
-
202
- // set up the abort controller
203
- this.controller = new AbortController();
204
- const signal = this.controller.signal;
205
-
206
- const keastIds = eventData.resource
207
- this.destinations = []
208
- this.origins = []
209
- this.components = []
210
- if (!keastIds || keastIds.length == 0) return
211
- const data = { sql: this.buildConnectivitySqlStatement(keastIds)};
212
- let prom1 = new Promise(resolve=>{
213
- fetch(`${this.flatmapApi}knowledge/query/`, {
214
- method: 'POST',
215
- headers: {
216
- 'Content-Type': 'application/json',
217
- },
218
- body: JSON.stringify(data),
219
- signal: signal
220
- })
221
- .then(response => response.json())
222
- .then(data => {
223
- if(this.connectivityExists(data)){
224
- let connectivity = JSON.parse(data.values[0][0])
225
- this.processConnectivity(connectivity).then(()=>{
226
- resolve(true)
227
- })
228
- } else {
229
- resolve(false)
230
- }
231
- })
232
- .catch((error) => {
233
- console.error('Error:', error);
234
- resolve(false)
235
- })
236
- })
237
- let prom2 = await this.pubmedQueryOnIds(eventData)
238
- let results = await Promise.all([prom1, prom2])
239
- return results
240
- }
241
-
242
- this.connectivityExists = function(data){
243
- if (data.values && data.values.length > 0 && JSON.parse(data.values[0][0]).connectivity && JSON.parse(data.values[0][0]).connectivity.length > 0) {
244
- return true
245
- } else {
246
- return false
247
- }
248
- }
249
-
250
- this.createLabelFromNeuralNode = function(node, lookUp){
251
- let label = lookUp[node[0]]
252
- if (node.length === 2 && node[1].length > 0){
253
- node[1].forEach(n=>{
254
- if (lookUp[n] == undefined){
255
- label += `, ${n}`
256
- } else {
257
- label += `, ${lookUp[n]}`
258
- }
259
- })
260
- }
261
- return label
262
- }
263
-
264
- this.flattenAndFindDatasets = function(components, axons, dendrites){
265
-
266
- // process the nodes for finding datasets (Note this is not critical to the tooltip, only for the 'search on components' button)
267
- let componentsFlat = this.flattenConntectivity(components)
268
- let axonsFlat = this.flattenConntectivity(axons)
269
- let dendritesFlat = this.flattenConntectivity(dendrites)
270
-
271
- console.log("uber in flattehn find datasets", this.uberons)
272
-
273
- window.uberons = this.uberons
274
- window.componentsFlat = componentsFlat
275
-
276
- // Filter for the anatomy which is annotated on datasets
277
- this.destinationsWithDatasets = this.uberons.filter(ub => axonsFlat.indexOf(ub) !== -1)
278
- this.originsWithDatasets = this.uberons.filter(ub => dendritesFlat.indexOf(ub) !== -1)
279
- this.componentsWithDatasets = this.uberons.filter(ub => componentsFlat.indexOf(ub) !== -1)
280
- }
281
-
282
- this.processConnectivity = function(connectivity){
283
- return new Promise (resolve=>{
284
- console.log(connectivity)
285
- // Filter the origin and destinations from components
286
- let components = this.findComponents(connectivity)
287
-
288
- // Remove duplicates
289
- let axons = removeDuplicates(connectivity.axons)
290
- let dendrites = removeDuplicates(connectivity.dendrites)
291
-
292
- // Create list of ids to get labels for
293
- let conIds = this.findAllIdsFromConnectivity(connectivity)
294
- console.log(conIds)
295
-
296
- // Create readable labels from the nodes. Setting this to 'this.origins' updates the display
297
- this.createLabelLookup(conIds).then(lookUp=>{
298
- console.log("after label lookup", this.uberons)
299
- this.destinations = axons.map(a=>this.createLabelFromNeuralNode(a,lookUp))
300
- this.origins = dendrites.map(d=>this.createLabelFromNeuralNode(d,lookUp))
301
- this.components = components.map(c=>this.createLabelFromNeuralNode(c, lookUp))
302
- this.flattenAndFindDatasets(components, axons, dendrites)
303
- resolve(true)
304
- })
305
- })
306
- }
307
-
308
- this.flattenConntectivity = function(connectivity){
309
- let dnodes = connectivity.flat() // get nodes from edgelist
310
- let nodes = [...new Set(dnodes)] // remove duplicates
311
- let found = []
312
- nodes.forEach(n=>{
313
- if (Array.isArray(n)){
314
- found.push(n.flat())
315
- } else {
316
- found.push(n)
317
- }
318
- })
319
- return found.flat()
320
- }
321
-
322
- this.findComponents = function(connectivity){
323
- let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
324
- let nodes = removeDuplicates(dnodes)
325
-
326
- let found = []
327
- let terminal = false
328
- nodes.forEach(node=>{
329
- terminal = false
330
- // Check if the node is an destination or origin (note that they are labelled dendrite and axon as opposed to origin and destination)
331
- if(inArray(connectivity.axons,node)){
332
- terminal = true
333
- }
334
- if(inArray(connectivity.dendrites, node)){
335
- terminal = true
336
- }
337
- if (!terminal){
338
- found.push(node)
339
- }
340
- })
341
-
342
- return found
343
- }
344
-
345
- this.stripPMIDPrefix = function (pubmedId){
346
- return pubmedId.split(':')[1]
347
- }
348
-
349
- this.buildPubmedSqlStatement = function(keastIds) {
350
- let sql = 'select distinct publication from publications where entity in ('
351
- if (keastIds.length === 1) {
352
- sql += `'${keastIds[0]}')`
353
- } else if (keastIds.length > 1) {
354
- for (let i in keastIds) {
355
- sql += `'${keastIds[i]}'${i >= keastIds.length - 1 ? ')' : ','} `
356
- }
357
- }
358
- return sql
359
- }
360
-
361
- this.buildPubmedSqlStatementForModels = function(model) {
362
- return `select distinct publication from publications where entity = '${model}'`
363
- }
364
-
365
- this.flatmapQuery = function(sql){
366
- const data = { sql: sql}
367
- return fetch(`${this.flatmapApi}knowledge/query/`, {
368
- method: 'POST',
369
- headers: {
370
- 'Content-Type': 'application/json',
371
- },
372
- body: JSON.stringify(data),
373
- })
374
- .then(response => response.json())
375
- .catch((error) => {
376
- console.error('Error:', error)
377
- })
378
- }
379
- // Note that this functin WILL run to the end, as it doesn not catch the second level of promises
380
- this.pubmedQueryOnIds = function(eventData){
381
- return new Promise(resolve=>{
382
- const keastIds = eventData.resource
383
- const source = eventData.feature.source
384
- if(!keastIds || keastIds.length === 0) return
385
- const sql = this.buildPubmedSqlStatement(keastIds)
386
- this.flatmapQuery(sql).then(data=>{
387
- // Create pubmed url on paths if we have them
388
- if (data.values.length > 0){
389
- this.urls = [this.pubmedSearchUrl(data.values.map(id=>this.stripPMIDPrefix(id[0])))]
390
- resolve(true)
391
- } else { // Create pubmed url on models
392
- this.pubmedQueryOnModels(source).then(result=>{
393
- resolve(result)
394
- })
395
- }
396
- })
397
- })
398
- }
399
-
400
- this.pubmedQueryOnModels = function(source){
401
- return this.flatmapQuery(this.buildPubmedSqlStatementForModels(source)).then(data=>{
402
- if (Array.isArray(data.values) && data.values.length > 0){
403
- this.urls = [this.pubmedSearchUrl(data.values.map(id=>this.stripPMIDPrefix(id[0])))]
404
- return true
405
- } else {
406
- this.urls = [] // Clears the pubmed search button
407
- }
408
- return false
409
- })
410
- }
411
-
412
- this.pubmedSearchUrl = function(ids) {
413
- let url = 'https://pubmed.ncbi.nlm.nih.gov/?'
414
- let params = new URLSearchParams()
415
- params.append('term', ids)
416
- return url + params.toString()
417
- }
418
- }
419
-
420
- export {FlatmapQueries, findTaxonomyLabel}
1
+ /* eslint-disable no-alert, no-console */
2
+ // remove duplicates by stringifying the objects
3
+ const removeDuplicates = function(arrayOfAnything){
4
+ return [...new Set(arrayOfAnything.map(e => JSON.stringify(e)))].map(e => JSON.parse(e))
5
+ }
6
+
7
+ const cachedLabels = {};
8
+
9
+ const findTaxonomyLabel = async function(flatmapAPI, taxonomy){
10
+ if (cachedLabels && cachedLabels.hasOwnProperty(taxonomy)) {
11
+ return cachedLabels[taxonomy];
12
+ }
13
+
14
+ return new Promise(resolve=>{
15
+ fetch(`${flatmapAPI}knowledge/label/${taxonomy}`, {
16
+ method: 'GET',
17
+ })
18
+ .then(response => response.json())
19
+ .then(data => {
20
+ let label = data.label;
21
+ if (label === "Mammalia") {
22
+ label = "Mammalia not otherwise specified";
23
+ }
24
+ cachedLabels[taxonomy] = label;
25
+ resolve(label);
26
+ })
27
+ .catch((error) => {
28
+ console.error('Error:', error);
29
+ cachedLabels[taxonomy] = taxonomy;
30
+ resolve(taxonomy);
31
+ })
32
+ });
33
+ }
34
+
35
+ const inArray = function(ar1, ar2){
36
+ let as1 = JSON.stringify(ar1)
37
+ let as2 = JSON.stringify(ar2)
38
+ return as1.indexOf(as2) !== -1
39
+ }
40
+
41
+ let FlatmapQueries = function(){
42
+
43
+ this.initialise = function(flatmapApi){
44
+ this.flatmapApi = flatmapApi
45
+ this.destinations = []
46
+ this.origins = []
47
+ this.components = []
48
+ this.urls = []
49
+ this.controller = undefined
50
+ this.uberons = []
51
+ this.lookUp = []
52
+ }
53
+
54
+ this.createTooltipData = async function (eventData) {
55
+ let hyperlinks = []
56
+ if (eventData.feature.hyperlinks && eventData.feature.hyperlinks.length > 0) {
57
+ hyperlinks = eventData.feature.hyperlinks
58
+ } else {
59
+ hyperlinks = this.urls.map(url=>({url: url, id: "pubmed"}))
60
+ }
61
+ let taxonomyLabel = undefined;
62
+ if (eventData.provenanceTaxonomy) {
63
+ taxonomyLabel = [];
64
+ for (let i = 0; eventData.provenanceTaxonomy.length > i; i++) {
65
+ taxonomyLabel.push(await findTaxonomyLabel(this.flatmapAPI, eventData.provenanceTaxonomy[i]))
66
+ }
67
+ }
68
+
69
+ let tooltipData = {
70
+ destinations: this.destinations,
71
+ origins: this.origins,
72
+ components: this.components,
73
+ destinationsWithDatasets: this.destinationsWithDatasets,
74
+ originsWithDatasets: this.originsWithDatasets,
75
+ componentsWithDatasets: this.componentsWithDatasets,
76
+ title: eventData.label,
77
+ featureId: eventData.resource,
78
+ hyperlinks: hyperlinks,
79
+ provenanceTaxonomy: eventData.provenanceTaxonomy,
80
+ provenanceTaxonomyLabel: taxonomyLabel
81
+ }
82
+ return tooltipData
83
+ }
84
+
85
+ this.createComponentsLabelList = function(components, lookUp){
86
+ let labelList = []
87
+ components.forEach(n=>{
88
+ labelList.push(this.createLabelFromNeuralNode(n[0]), lookUp)
89
+ if (n.length === 2){
90
+ labelList.push(this.createLabelFromNeuralNode(n[1]), lookUp)
91
+ }
92
+ })
93
+ return labelList
94
+ }
95
+
96
+ this.createLabelLookup = function(uberons) {
97
+ return new Promise(resolve=> {
98
+ let uberonMap = {}
99
+ this.uberons = []
100
+ const data = { sql: this.buildLabelSqlStatement(uberons)}
101
+ fetch(`${this.flatmapApi}knowledge/query/`, {
102
+ method: 'POST',
103
+ headers: {
104
+ 'Content-Type': 'application/json',
105
+ },
106
+ body: JSON.stringify(data),
107
+ })
108
+ .then(response => response.json())
109
+ .then(payload => {
110
+ const entity = payload.keys.indexOf("entity");
111
+ const label = payload.keys.indexOf("label");
112
+ if (entity > -1 && label > -1) {
113
+ payload.values.forEach(pair => {
114
+ uberonMap[pair[entity]] = pair[label];
115
+ this.uberons.push({
116
+ id: pair[entity],
117
+ name: pair[label]
118
+ })
119
+ });
120
+ }
121
+ resolve(uberonMap)
122
+ })
123
+ })
124
+ }
125
+
126
+ this.buildConnectivitySqlStatement = function (keastIds) {
127
+ let sql = 'select knowledge from knowledge where entity in ('
128
+ if (keastIds.length === 1) {
129
+ sql += `'${keastIds[0]}')`
130
+ } else if (keastIds.length > 1) {
131
+ for (let i in keastIds) {
132
+ sql += `'${keastIds[i]}'${i >= keastIds.length - 1 ? ')' : ','} `
133
+ }
134
+ }
135
+ return sql
136
+ }
137
+
138
+ this.buildLabelSqlStatement = function (uberons) {
139
+ let sql = 'select entity, label from labels where entity in ('
140
+ if (uberons.length === 1) {
141
+ sql += `'${uberons[0]}')`
142
+ } else if (uberons.length > 1) {
143
+ for (let i in uberons) {
144
+ sql += `'${uberons[i]}'${i >= uberons.length - 1 ? ')' : ','} `
145
+ }
146
+ }
147
+ return sql
148
+ }
149
+
150
+ this.findAllIdsFromConnectivity = function (connectivity) {
151
+ let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
152
+ let nodes = [...new Set(dnodes)] // remove duplicates
153
+ let found = []
154
+ nodes.forEach(n => {
155
+ if (Array.isArray(n)) {
156
+ found.push(n.flat())
157
+ } else {
158
+ found.push(n)
159
+ }
160
+ })
161
+ return [... new Set(found.flat())]
162
+ }
163
+
164
+ this.flattenConntectivity = function (connectivity) {
165
+ let dnodes = connectivity.flat() // get nodes from edgelist
166
+ let nodes = [...new Set(dnodes)] // remove duplicates
167
+ let found = []
168
+ nodes.forEach(n => {
169
+ if (Array.isArray(n)) {
170
+ found.push(n.flat())
171
+ } else {
172
+ found.push(n)
173
+ }
174
+ })
175
+ return found.flat()
176
+ }
177
+
178
+ this.findComponents = function (connectivity) {
179
+ let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
180
+ let nodes = removeDuplicates(dnodes)
181
+
182
+ let found = []
183
+ let terminal = false
184
+ nodes.forEach(node => {
185
+ terminal = false
186
+ // Check if the node is an destination or origin (note that they are labelled dendrite and axon as opposed to origin and destination)
187
+ if (inArray(connectivity.axons, node)) {
188
+ terminal = true
189
+ }
190
+ if (inArray(connectivity.dendrites, node)) {
191
+ terminal = true
192
+ }
193
+ if (!terminal) {
194
+ found.push(node)
195
+ }
196
+ })
197
+
198
+ return found
199
+ }
200
+
201
+ this.retrieveFlatmapKnowledgeForEvent = async function(eventData){
202
+ // check if there is an existing query
203
+ if (this.controller) this.controller.abort();
204
+
205
+ // set up the abort controller
206
+ this.controller = new AbortController();
207
+ const signal = this.controller.signal;
208
+
209
+ const keastIds = eventData.resource
210
+ this.destinations = []
211
+ this.origins = []
212
+ this.components = []
213
+ if (!keastIds || keastIds.length == 0) return
214
+ const data = { sql: this.buildConnectivitySqlStatement(keastIds)};
215
+ let prom1 = new Promise(resolve=>{
216
+ fetch(`${this.flatmapApi}knowledge/query/`, {
217
+ method: 'POST',
218
+ headers: {
219
+ 'Content-Type': 'application/json',
220
+ },
221
+ body: JSON.stringify(data),
222
+ signal: signal
223
+ })
224
+ .then(response => response.json())
225
+ .then(data => {
226
+ if(this.connectivityExists(data)){
227
+ let connectivity = JSON.parse(data.values[0][0])
228
+ this.processConnectivity(connectivity).then(()=>{
229
+ resolve(true)
230
+ })
231
+ } else {
232
+ resolve(false)
233
+ }
234
+ })
235
+ .catch((error) => {
236
+ console.error('Error:', error);
237
+ resolve(false)
238
+ })
239
+ })
240
+ let prom2 = await this.pubmedQueryOnIds(eventData)
241
+ let results = await Promise.all([prom1, prom2])
242
+ return results
243
+ }
244
+
245
+ this.connectivityExists = function(data){
246
+ if (data.values && data.values.length > 0 && JSON.parse(data.values[0][0]).connectivity && JSON.parse(data.values[0][0]).connectivity.length > 0) {
247
+ return true
248
+ } else {
249
+ return false
250
+ }
251
+ }
252
+
253
+ this.createLabelFromNeuralNode = function(node, lookUp){
254
+ let label = lookUp[node[0]]
255
+ if (node.length === 2 && node[1].length > 0){
256
+ node[1].forEach(n=>{
257
+ if (lookUp[n] == undefined){
258
+ label += `, ${n}`
259
+ } else {
260
+ label += `, ${lookUp[n]}`
261
+ }
262
+ })
263
+ }
264
+ return label
265
+ }
266
+
267
+ this.flattenAndFindDatasets = function(components, axons, dendrites){
268
+
269
+ // process the nodes for finding datasets (Note this is not critical to the tooltip, only for the 'search on components' button)
270
+ let componentsFlat = this.flattenConntectivity(components)
271
+ let axonsFlat = this.flattenConntectivity(axons)
272
+ let dendritesFlat = this.flattenConntectivity(dendrites)
273
+
274
+ // Filter for the anatomy which is annotated on datasets
275
+ this.destinationsWithDatasets = this.uberons.filter(ub => axonsFlat.indexOf(ub.id) !== -1)
276
+ this.originsWithDatasets = this.uberons.filter(ub => dendritesFlat.indexOf(ub.id) !== -1)
277
+ this.componentsWithDatasets = this.uberons.filter(ub => componentsFlat.indexOf(ub.id) !== -1)
278
+ }
279
+
280
+ this.processConnectivity = function(connectivity){
281
+ return new Promise (resolve=>{
282
+ // Filter the origin and destinations from components
283
+ let components = this.findComponents(connectivity)
284
+
285
+ // Remove duplicates
286
+ let axons = removeDuplicates(connectivity.axons)
287
+ let dendrites = removeDuplicates(connectivity.dendrites)
288
+
289
+ // Create list of ids to get labels for
290
+ let conIds = this.findAllIdsFromConnectivity(connectivity)
291
+
292
+ // Create readable labels from the nodes. Setting this to 'this.origins' updates the display
293
+ this.createLabelLookup(conIds).then(lookUp=>{
294
+ this.destinations = axons.map(a=>this.createLabelFromNeuralNode(a,lookUp))
295
+ this.origins = dendrites.map(d=>this.createLabelFromNeuralNode(d,lookUp))
296
+ this.components = components.map(c=>this.createLabelFromNeuralNode(c, lookUp))
297
+ this.flattenAndFindDatasets(components, axons, dendrites)
298
+ resolve(true)
299
+ })
300
+ })
301
+ }
302
+
303
+ this.flattenConntectivity = function(connectivity){
304
+ let dnodes = connectivity.flat() // get nodes from edgelist
305
+ let nodes = [...new Set(dnodes)] // remove duplicates
306
+ let found = []
307
+ nodes.forEach(n=>{
308
+ if (Array.isArray(n)){
309
+ found.push(n.flat())
310
+ } else {
311
+ found.push(n)
312
+ }
313
+ })
314
+ return found.flat()
315
+ }
316
+
317
+ this.findComponents = function(connectivity){
318
+ let dnodes = connectivity.connectivity.flat() // get nodes from edgelist
319
+ let nodes = removeDuplicates(dnodes)
320
+
321
+ let found = []
322
+ let terminal = false
323
+ nodes.forEach(node=>{
324
+ terminal = false
325
+ // Check if the node is an destination or origin (note that they are labelled dendrite and axon as opposed to origin and destination)
326
+ if(inArray(connectivity.axons,node)){
327
+ terminal = true
328
+ }
329
+ if(inArray(connectivity.dendrites, node)){
330
+ terminal = true
331
+ }
332
+ if (!terminal){
333
+ found.push(node)
334
+ }
335
+ })
336
+
337
+ return found
338
+ }
339
+
340
+ this.stripPMIDPrefix = function (pubmedId){
341
+ return pubmedId.split(':')[1]
342
+ }
343
+
344
+ this.buildPubmedSqlStatement = function(keastIds) {
345
+ let sql = 'select distinct publication from publications where entity in ('
346
+ if (keastIds.length === 1) {
347
+ sql += `'${keastIds[0]}')`
348
+ } else if (keastIds.length > 1) {
349
+ for (let i in keastIds) {
350
+ sql += `'${keastIds[i]}'${i >= keastIds.length - 1 ? ')' : ','} `
351
+ }
352
+ }
353
+ return sql
354
+ }
355
+
356
+ this.buildPubmedSqlStatementForModels = function(model) {
357
+ return `select distinct publication from publications where entity = '${model}'`
358
+ }
359
+
360
+ this.flatmapQuery = function(sql){
361
+ const data = { sql: sql}
362
+ return fetch(`${this.flatmapApi}knowledge/query/`, {
363
+ method: 'POST',
364
+ headers: {
365
+ 'Content-Type': 'application/json',
366
+ },
367
+ body: JSON.stringify(data),
368
+ })
369
+ .then(response => response.json())
370
+ .catch((error) => {
371
+ console.error('Error:', error)
372
+ })
373
+ }
374
+ // Note that this functin WILL run to the end, as it doesn not catch the second level of promises
375
+ this.pubmedQueryOnIds = function(eventData){
376
+ return new Promise(resolve=>{
377
+ const keastIds = eventData.resource
378
+ const source = eventData.feature.source
379
+ if(!keastIds || keastIds.length === 0) return
380
+ const sql = this.buildPubmedSqlStatement(keastIds)
381
+ this.flatmapQuery(sql).then(data=>{
382
+ // Create pubmed url on paths if we have them
383
+ if (data.values.length > 0){
384
+ this.urls = [this.pubmedSearchUrl(data.values.map(id=>this.stripPMIDPrefix(id[0])))]
385
+ resolve(true)
386
+ } else { // Create pubmed url on models
387
+ this.pubmedQueryOnModels(source).then(result=>{
388
+ resolve(result)
389
+ })
390
+ }
391
+ })
392
+ })
393
+ }
394
+
395
+ this.pubmedQueryOnModels = function(source){
396
+ return this.flatmapQuery(this.buildPubmedSqlStatementForModels(source)).then(data=>{
397
+ if (Array.isArray(data.values) && data.values.length > 0){
398
+ this.urls = [this.pubmedSearchUrl(data.values.map(id=>this.stripPMIDPrefix(id[0])))]
399
+ return true
400
+ } else {
401
+ this.urls = [] // Clears the pubmed search button
402
+ }
403
+ return false
404
+ })
405
+ }
406
+
407
+ this.pubmedSearchUrl = function(ids) {
408
+ let url = 'https://pubmed.ncbi.nlm.nih.gov/?'
409
+ let params = new URLSearchParams()
410
+ params.append('term', ids)
411
+ return url + params.toString()
412
+ }
413
+ }
414
+
415
+ export {FlatmapQueries, findTaxonomyLabel}