@abi-software/map-side-bar 2.2.1-alpha-2 → 2.2.1-beta.0

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.
@@ -1,242 +1,242 @@
1
- /* eslint-disable no-alert, no-console */
2
- import algoliasearch from 'algoliasearch'
3
-
4
- // export `createAlgoliaClient` to use it in page components
5
- export class AlgoliaClient {
6
- constructor(algoliaId, algoliaKey, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
7
- this.client = algoliasearch(
8
- algoliaId,
9
- algoliaKey
10
- )
11
- this.PENNSIEVE_API_LOCATION = PENNSIEVE_API_LOCATION
12
- this.anatomyFacetLabels = []
13
- }
14
- initIndex(ALGOLIA_INDEX) {
15
- this.index = this.client.initIndex(ALGOLIA_INDEX);
16
- }
17
-
18
- getAlgoliaFacets(propPathMapping) {
19
- const facetPropPaths = propPathMapping.map(facet => facet.facetPropPath)
20
- const facetSubpropPaths = propPathMapping.map(item => item.facetSubpropPath)
21
- let facetData = []
22
- let facetId = 0
23
- return this.index
24
- .search('', {
25
- sortFacetValuesBy: 'alpha',
26
- facets: facetPropPaths.concat(facetSubpropPaths),
27
- })
28
- .then(response => {
29
- facetPropPaths.map((facetPropPath) => {
30
- const parentFacet = propPathMapping.find(item => item.facetPropPath == facetPropPath)
31
- var children = []
32
- const responseFacets = response.facets
33
- if (responseFacets === undefined) {return}
34
- const responseFacetChildren =
35
- responseFacets[facetPropPath] == undefined // if no facets, return empty object
36
- ? {}
37
- : responseFacets[facetPropPath]
38
- const allPossibleChildrenSubfacets = parentFacet && responseFacets[parentFacet.facetSubpropPath] ? Object.keys(responseFacets[parentFacet.facetSubpropPath]) : []
39
- // Loop through all subfacets and find the ones that are children of the current facet
40
- Object.keys(responseFacetChildren).map(facet => {
41
- const childrenSubfacets = allPossibleChildrenSubfacets.reduce((filtered, childFacetInfo) => {
42
- const info = childFacetInfo.split('.');
43
- if (info.length !== 2) {
44
- return filtered;
45
- }
46
- if (facet === info[0]) {
47
- filtered.push({
48
- label: info[1],
49
- id: facetId++,
50
- facetPropPath: `${parentFacet ? parentFacet.facetSubpropPath : undefined}`
51
- });
52
- }
53
- return filtered;
54
- }, []); // Provide an empty array as the initial value
55
- let newChild = {
56
- label: facet,
57
- id: facetId++,
58
- facetPropPath: facetPropPath
59
- }
60
- if (childrenSubfacets.length > 0) {
61
- newChild.children = childrenSubfacets
62
- }
63
- children.push(newChild)
64
- })
65
- if (children.length > 0) {
66
- facetData.push({
67
- label: parentFacet ? parentFacet.label : '',
68
- id: facetId++,
69
- children: children,
70
- key: facetPropPath
71
- })
72
- }
73
- })
74
- return facetData
75
- })
76
- }
77
-
78
- // Returns all DOIs of all versions for a given discover dataset
79
- _discoverAllDois(discoverId, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
80
- return new Promise(resolve => {
81
- fetch(`${PENNSIEVE_API_LOCATION}/discover/datasets/${discoverId}/versions`).then(r => r.json()).then(dataset => {
82
- resolve(dataset.map(version => version.doi))
83
- })
84
- })
85
- }
86
-
87
- // Get all dois given a list of discoverIds
88
- _expandDois(discoverIds, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
89
- return new Promise(resolve => {
90
- let promiseList = discoverIds.map(discoverId => this._discoverAllDois(discoverId, PENNSIEVE_API_LOCATION))
91
- Promise.all(promiseList).then((values) => {
92
- resolve(values.flat())
93
- });
94
- })
95
- }
96
-
97
- _processResultsForCards(results) {
98
- let newResults = []
99
- let newResult = {}
100
- for (let res of results) {
101
- newResult = { ...res }
102
- newResult = {
103
- anatomy: res.anatomy ? res.anatomy.organ.map((organ => organ.curie)) : undefined,
104
- doi: res.item.curie.split(':')[1],
105
- name: res.item.name,
106
- description: res.item.description,
107
- updated: res.pennsieve ? res.pennsieve.updatedAt : undefined,
108
- publishDate: res.pennsieve ? res.pennsieve.publishDate : undefined,
109
- datasetId: res.objectID,
110
- detailsReady: false
111
- }
112
- newResults.push(newResult)
113
- }
114
- return newResults
115
- }
116
-
117
- _processAnatomy(hits) {
118
- let foundKeyWords = []
119
- let foundLabels = []
120
- let uniqueLabels = []
121
- let uniqueKeywords = []
122
- hits.forEach(hit => {
123
- if (hit.item && hit.item.keywords) {
124
- hit.item.keywords.forEach(keywordObj => {
125
- let keyword = keywordObj.keyword.toUpperCase()
126
- if (keyword.includes('UBERON') || keyword.includes('ILX')) {
127
- foundKeyWords.push(this._processUberonURL(keyword))
128
- }
129
- })
130
- }
131
- if (hit.anatomy && hit.anatomy.organ ) {
132
- hit.anatomy.organ.forEach(anatomy => {
133
- if (anatomy.curie) {
134
- foundKeyWords.push(anatomy.curie)
135
- foundLabels.push(anatomy.name)
136
- }
137
- })
138
- }
139
- })
140
- uniqueKeywords = [...new Set(foundKeyWords) ]
141
- uniqueLabels = [...new Set(foundLabels) ]
142
- this.anatomyFacetLabels = uniqueLabels
143
- return uniqueKeywords
144
- }
145
-
146
- _processUberonURL(url) {
147
- let ub = url.split('/').pop()
148
- return ub.replace('_', ':')
149
- }
150
-
151
- /**
152
- * Get Search results
153
- * This is using fetch from the Algolia API
154
- */
155
- search(filter, query = '', hitsperPage = 10, page = 1) {
156
- return new Promise(resolve => {
157
- this.index
158
- .search(query, {
159
- facets: ['*'],
160
- hitsPerPage: hitsperPage,
161
- page: page - 1,
162
- filters: filter,
163
- attributesToHighlight: [],
164
- attributesToRetrieve: [
165
- 'pennsieve.publishDate',
166
- 'pennsieve.updatedAt',
167
- 'item.curie',
168
- 'item.name',
169
- 'item.description',
170
- 'objectID',
171
- 'anatomy.organ.curie'
172
- ],
173
- })
174
- .then(response => {
175
- let searchData = {
176
- items: this._processResultsForCards(response.hits),
177
- total: response.nbHits,
178
- discoverIds: response.hits.map(r => r.pennsieve ? r.pennsieve.identifier : r.objectID),
179
- dois: response.hits.map(r => r.item.curie.split(':')[1])
180
- }
181
- resolve(searchData)
182
- })
183
- })
184
- }
185
-
186
- /**
187
- * Get key words
188
- * This is used to return all keywords for a given search. Note that you often want the hits per page to be maxed out
189
- */
190
- anatomyInSearch(filter, query = '', hitsperPage = 999999, page = 1) {
191
- return new Promise(resolve => {
192
- this.index
193
- .search(query, {
194
- facets: ['*'],
195
- hitsPerPage: hitsperPage,
196
- page: page - 1,
197
- filters: filter,
198
- attributesToHighlight: [],
199
- attributesToRetrieve: [
200
- 'objectID',
201
- 'item.keywords.keyword',
202
- 'anatomy.organ.name',
203
- 'anatomy.organ.curie'
204
- ],
205
- })
206
- .then(response => {
207
- // Saving the line below incase we want to starty using keywords again
208
- // let anatomyAsUberons = this._processAnatomy(response.hits)
209
-
210
- resolve({
211
- forFlamap: this.processResultsForFlatmap(response.hits),
212
- forScaffold: this.processResultsForScaffold(response.hits)
213
- })
214
- })
215
- })
216
- }
217
- processResultsForFlatmap(hits) {
218
- let curieForDatsets = hits.map(h=>({
219
- id: h.objectID,
220
- terms: h.anatomy? h.anatomy.organ.map(o=>o.curie) : []
221
- }))
222
- return curieForDatsets
223
- }
224
- processResultsForScaffold(hits) {
225
- let numberOfDatasetsForAnatomy = {}
226
- hits.forEach(hit => {
227
- if (hit.anatomy && hit.anatomy.organ ) {
228
- hit.anatomy.organ.forEach(anatomy => {
229
- if (anatomy.name) {
230
- if (numberOfDatasetsForAnatomy[anatomy.name]) {
231
- numberOfDatasetsForAnatomy[anatomy.name]++
232
- } else {
233
- numberOfDatasetsForAnatomy[anatomy.name] = 1
234
- }
235
- }
236
- })
237
- }
238
- })
239
- return numberOfDatasetsForAnatomy
240
- }
241
-
242
- }
1
+ /* eslint-disable no-alert, no-console */
2
+ import algoliasearch from 'algoliasearch'
3
+
4
+ // export `createAlgoliaClient` to use it in page components
5
+ export class AlgoliaClient {
6
+ constructor(algoliaId, algoliaKey, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
7
+ this.client = algoliasearch(
8
+ algoliaId,
9
+ algoliaKey
10
+ )
11
+ this.PENNSIEVE_API_LOCATION = PENNSIEVE_API_LOCATION
12
+ this.anatomyFacetLabels = []
13
+ }
14
+ initIndex(ALGOLIA_INDEX) {
15
+ this.index = this.client.initIndex(ALGOLIA_INDEX);
16
+ }
17
+
18
+ getAlgoliaFacets(propPathMapping) {
19
+ const facetPropPaths = propPathMapping.map(facet => facet.facetPropPath)
20
+ const facetSubpropPaths = propPathMapping.map(item => item.facetSubpropPath)
21
+ let facetData = []
22
+ let facetId = 0
23
+ return this.index
24
+ .search('', {
25
+ sortFacetValuesBy: 'alpha',
26
+ facets: facetPropPaths.concat(facetSubpropPaths),
27
+ })
28
+ .then(response => {
29
+ facetPropPaths.map((facetPropPath) => {
30
+ const parentFacet = propPathMapping.find(item => item.facetPropPath == facetPropPath)
31
+ var children = []
32
+ const responseFacets = response.facets
33
+ if (responseFacets === undefined) {return}
34
+ const responseFacetChildren =
35
+ responseFacets[facetPropPath] == undefined // if no facets, return empty object
36
+ ? {}
37
+ : responseFacets[facetPropPath]
38
+ const allPossibleChildrenSubfacets = parentFacet && responseFacets[parentFacet.facetSubpropPath] ? Object.keys(responseFacets[parentFacet.facetSubpropPath]) : []
39
+ // Loop through all subfacets and find the ones that are children of the current facet
40
+ Object.keys(responseFacetChildren).map(facet => {
41
+ const childrenSubfacets = allPossibleChildrenSubfacets.reduce((filtered, childFacetInfo) => {
42
+ const info = childFacetInfo.split('.');
43
+ if (info.length !== 2) {
44
+ return filtered;
45
+ }
46
+ if (facet === info[0]) {
47
+ filtered.push({
48
+ label: info[1],
49
+ id: facetId++,
50
+ facetPropPath: `${parentFacet ? parentFacet.facetSubpropPath : undefined}`
51
+ });
52
+ }
53
+ return filtered;
54
+ }, []); // Provide an empty array as the initial value
55
+ let newChild = {
56
+ label: facet,
57
+ id: facetId++,
58
+ facetPropPath: facetPropPath
59
+ }
60
+ if (childrenSubfacets.length > 0) {
61
+ newChild.children = childrenSubfacets
62
+ }
63
+ children.push(newChild)
64
+ })
65
+ if (children.length > 0) {
66
+ facetData.push({
67
+ label: parentFacet ? parentFacet.label : '',
68
+ id: facetId++,
69
+ children: children,
70
+ key: facetPropPath
71
+ })
72
+ }
73
+ })
74
+ return facetData
75
+ })
76
+ }
77
+
78
+ // Returns all DOIs of all versions for a given discover dataset
79
+ _discoverAllDois(discoverId, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
80
+ return new Promise(resolve => {
81
+ fetch(`${PENNSIEVE_API_LOCATION}/discover/datasets/${discoverId}/versions`).then(r => r.json()).then(dataset => {
82
+ resolve(dataset.map(version => version.doi))
83
+ })
84
+ })
85
+ }
86
+
87
+ // Get all dois given a list of discoverIds
88
+ _expandDois(discoverIds, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
89
+ return new Promise(resolve => {
90
+ let promiseList = discoverIds.map(discoverId => this._discoverAllDois(discoverId, PENNSIEVE_API_LOCATION))
91
+ Promise.all(promiseList).then((values) => {
92
+ resolve(values.flat())
93
+ });
94
+ })
95
+ }
96
+
97
+ _processResultsForCards(results) {
98
+ let newResults = []
99
+ let newResult = {}
100
+ for (let res of results) {
101
+ newResult = { ...res }
102
+ newResult = {
103
+ anatomy: res.anatomy ? res.anatomy.organ.map((organ => organ.curie)) : undefined,
104
+ doi: res.item.curie.split(':')[1],
105
+ name: res.item.name,
106
+ description: res.item.description,
107
+ updated: res.pennsieve ? res.pennsieve.updatedAt : undefined,
108
+ publishDate: res.pennsieve ? res.pennsieve.publishDate : undefined,
109
+ datasetId: res.objectID,
110
+ detailsReady: false
111
+ }
112
+ newResults.push(newResult)
113
+ }
114
+ return newResults
115
+ }
116
+
117
+ _processAnatomy(hits) {
118
+ let foundKeyWords = []
119
+ let foundLabels = []
120
+ let uniqueLabels = []
121
+ let uniqueKeywords = []
122
+ hits.forEach(hit => {
123
+ if (hit.item && hit.item.keywords) {
124
+ hit.item.keywords.forEach(keywordObj => {
125
+ let keyword = keywordObj.keyword.toUpperCase()
126
+ if (keyword.includes('UBERON') || keyword.includes('ILX')) {
127
+ foundKeyWords.push(this._processUberonURL(keyword))
128
+ }
129
+ })
130
+ }
131
+ if (hit.anatomy && hit.anatomy.organ ) {
132
+ hit.anatomy.organ.forEach(anatomy => {
133
+ if (anatomy.curie) {
134
+ foundKeyWords.push(anatomy.curie)
135
+ foundLabels.push(anatomy.name)
136
+ }
137
+ })
138
+ }
139
+ })
140
+ uniqueKeywords = [...new Set(foundKeyWords) ]
141
+ uniqueLabels = [...new Set(foundLabels) ]
142
+ this.anatomyFacetLabels = uniqueLabels
143
+ return uniqueKeywords
144
+ }
145
+
146
+ _processUberonURL(url) {
147
+ let ub = url.split('/').pop()
148
+ return ub.replace('_', ':')
149
+ }
150
+
151
+ /**
152
+ * Get Search results
153
+ * This is using fetch from the Algolia API
154
+ */
155
+ search(filter, query = '', hitsperPage = 10, page = 1) {
156
+ return new Promise(resolve => {
157
+ this.index
158
+ .search(query, {
159
+ facets: ['*'],
160
+ hitsPerPage: hitsperPage,
161
+ page: page - 1,
162
+ filters: filter,
163
+ attributesToHighlight: [],
164
+ attributesToRetrieve: [
165
+ 'pennsieve.publishDate',
166
+ 'pennsieve.updatedAt',
167
+ 'item.curie',
168
+ 'item.name',
169
+ 'item.description',
170
+ 'objectID',
171
+ 'anatomy.organ.curie'
172
+ ],
173
+ })
174
+ .then(response => {
175
+ let searchData = {
176
+ items: this._processResultsForCards(response.hits),
177
+ total: response.nbHits,
178
+ discoverIds: response.hits.map(r => r.pennsieve ? r.pennsieve.identifier : r.objectID),
179
+ dois: response.hits.map(r => r.item.curie.split(':')[1])
180
+ }
181
+ resolve(searchData)
182
+ })
183
+ })
184
+ }
185
+
186
+ /**
187
+ * Get key words
188
+ * This is used to return all keywords for a given search. Note that you often want the hits per page to be maxed out
189
+ */
190
+ anatomyInSearch(filter, query = '', hitsperPage = 999999, page = 1) {
191
+ return new Promise(resolve => {
192
+ this.index
193
+ .search(query, {
194
+ facets: ['*'],
195
+ hitsPerPage: hitsperPage,
196
+ page: page - 1,
197
+ filters: filter,
198
+ attributesToHighlight: [],
199
+ attributesToRetrieve: [
200
+ 'objectID',
201
+ 'item.keywords.keyword',
202
+ 'anatomy.organ.name',
203
+ 'anatomy.organ.curie'
204
+ ],
205
+ })
206
+ .then(response => {
207
+ // Saving the line below incase we want to starty using keywords again
208
+ // let anatomyAsUberons = this._processAnatomy(response.hits)
209
+
210
+ resolve({
211
+ forFlatmap: this.processResultsForFlatmap(response.hits),
212
+ forScaffold: this.processResultsForScaffold(response.hits)
213
+ })
214
+ })
215
+ })
216
+ }
217
+ processResultsForFlatmap(hits) {
218
+ let curieForDatsets = hits.map(h=>({
219
+ id: h.objectID,
220
+ terms: h.anatomy? h.anatomy.organ.map(o=>o.curie) : []
221
+ }))
222
+ return curieForDatsets
223
+ }
224
+ processResultsForScaffold(hits) {
225
+ let numberOfDatasetsForAnatomy = {}
226
+ hits.forEach(hit => {
227
+ if (hit.anatomy && hit.anatomy.organ ) {
228
+ hit.anatomy.organ.forEach(anatomy => {
229
+ if (anatomy.name) {
230
+ if (numberOfDatasetsForAnatomy[anatomy.name]) {
231
+ numberOfDatasetsForAnatomy[anatomy.name]++
232
+ } else {
233
+ numberOfDatasetsForAnatomy[anatomy.name] = 1
234
+ }
235
+ }
236
+ })
237
+ }
238
+ })
239
+ return numberOfDatasetsForAnatomy
240
+ }
241
+
242
+ }