@abi-software/flatmapvuer 0.5.7 → 0.5.8

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 -399
  2. package/LICENSE +201 -201
  3. package/README.md +105 -105
  4. package/babel.config.js +14 -14
  5. package/dist/flatmapvuer.common.js +139 -98
  6. package/dist/flatmapvuer.common.js.map +1 -1
  7. package/dist/flatmapvuer.css +1 -1
  8. package/dist/flatmapvuer.umd.js +139 -98
  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 -14399
  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 -1841
  21. package/src/components/MultiFlatmapVuer.vue +529 -529
  22. package/src/components/SelectionsGroup.vue +249 -249
  23. package/src/components/Tooltip.vue +447 -417
  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/nerve-map.js +99 -0
  36. package/src/services/flatmapQueries.js +415 -415
  37. package/vue.config.js +31 -31
@@ -1,415 +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.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}
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}