gramene-search 2.0.1 → 2.0.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramene-search",
3
- "version": "2.0.1",
3
+ "version": "2.0.4",
4
4
  "description": "search wrapper for gramene",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -27,6 +27,7 @@
27
27
  "ag-grid-community": "^31.1.1",
28
28
  "ag-grid-react": "^31.0.3",
29
29
  "axios": "^1.6.8",
30
+ "d3": "^7.9.0",
30
31
  "firebase": "^11.0.1",
31
32
  "flat-to-nested": "^1.1.1",
32
33
  "gramene-bins-client": "^2.3.3",
@@ -0,0 +1,422 @@
1
+ import { createSelector } from 'redux-bundler';
2
+
3
+ // State shape:
4
+ // {
5
+ // pivot: { status, signature, data: { [taxon_id]: <gene count> }, error, requestId },
6
+ // activeTaxon: <taxon_id|null>,
7
+ // byTaxon: {
8
+ // [taxon_id]: {
9
+ // selectedFields: [<solr field name>...],
10
+ // fieldsModalOpen: <bool>,
11
+ // fetch: { status, offset, total, signature, requestId, error },
12
+ // rows: [<doc>...]
13
+ // }
14
+ // }
15
+ // }
16
+ // Per-taxon study lists are derived in the view layer from expressionStudies.
17
+
18
+ const PAGE_SIZE = 200;
19
+
20
+ let pivotPendingId = 0;
21
+ const fetchPending = {}; // per-taxon request id
22
+ const attrsPending = {}; // per-taxon request id for available attrs
23
+ const existencePending = {}; // per-taxon request id for field-existence check
24
+
25
+ function pivotSignature(store) {
26
+ const q = store.selectGrameneFiltersQueryString();
27
+ const g = store.selectGrameneGenomes();
28
+ const m = store.selectGrameneMaps() || {};
29
+ const taxa = Object.keys(g.active || {}).filter(tid => m[tid] && !m[tid].hidden);
30
+ return `${q}|${taxa.sort().join(',')}`;
31
+ }
32
+
33
+ function fetchSignature(store, taxon) {
34
+ const q = store.selectGrameneFiltersQueryString();
35
+ const ev = store.selectExprViz();
36
+ const sel = (ev.byTaxon[taxon] && ev.byTaxon[taxon].selectedFields) || [];
37
+ return `${q}|${taxon}|${sel.slice().sort().join(',')}`;
38
+ }
39
+
40
+ const ALWAYS_FL = ['id', 'name', 'system_name', 'taxon_id'];
41
+
42
+ const exprViz = {
43
+ name: 'exprViz',
44
+
45
+ getReducer: () => {
46
+ const initialState = {
47
+ pivot: { status: 'idle', signature: null, data: {}, error: null, requestId: 0 },
48
+ activeTaxon: null,
49
+ byTaxon: {}
50
+ };
51
+
52
+ function ensureTaxon(state, taxon) {
53
+ if (state.byTaxon[taxon]) return state.byTaxon[taxon];
54
+ return {
55
+ selectedFields: [],
56
+ fieldsModalOpen: false,
57
+ fetch: { status: 'idle', offset: 0, total: 0, signature: null, requestId: 0, error: null },
58
+ rows: [],
59
+ availableAttrs: null,
60
+ attrsSignature: null,
61
+ attrsRequestId: 0,
62
+ fieldExistence: null,
63
+ existenceStatus: 'idle',
64
+ existenceSignature: null,
65
+ existenceRequestId: 0
66
+ };
67
+ }
68
+
69
+ return (state = initialState, { type, payload }) => {
70
+ switch (type) {
71
+ case 'EXPRVIZ_PIVOT_STARTED':
72
+ return { ...state, pivot: { status: 'loading', signature: payload.signature, data: state.pivot.data, error: null, requestId: payload.requestId } };
73
+ case 'EXPRVIZ_PIVOT_SUCCEEDED':
74
+ if (payload.requestId !== state.pivot.requestId) return state;
75
+ return { ...state, pivot: { status: 'ready', signature: state.pivot.signature, data: payload.data, error: null, requestId: payload.requestId } };
76
+ case 'EXPRVIZ_PIVOT_FAILED':
77
+ if (payload.requestId !== state.pivot.requestId) return state;
78
+ return { ...state, pivot: { ...state.pivot, status: 'error', error: payload.error } };
79
+
80
+ case 'EXPRVIZ_ACTIVE_TAXON_SET':
81
+ return { ...state, activeTaxon: payload };
82
+
83
+ case 'EXPRVIZ_FIELDS_MODAL_TOGGLED': {
84
+ const t = ensureTaxon(state, payload.taxon);
85
+ return {
86
+ ...state,
87
+ byTaxon: { ...state.byTaxon, [payload.taxon]: { ...t, fieldsModalOpen: payload.open } }
88
+ };
89
+ }
90
+ case 'EXPRVIZ_FIELDS_SET': {
91
+ const t = ensureTaxon(state, payload.taxon);
92
+ return {
93
+ ...state,
94
+ byTaxon: {
95
+ ...state.byTaxon,
96
+ [payload.taxon]: {
97
+ ...t,
98
+ selectedFields: payload.fields,
99
+ rows: [],
100
+ fetch: { status: 'idle', offset: 0, total: 0, signature: null, requestId: 0, error: null }
101
+ }
102
+ }
103
+ };
104
+ }
105
+ case 'EXPRVIZ_FIELDS_REORDERED': {
106
+ const t = state.byTaxon[payload.taxon];
107
+ if (!t) return state;
108
+ // Same set of fields — preserve rows; only the column/axis order changes.
109
+ const setA = new Set(t.selectedFields);
110
+ const setB = new Set(payload.fields);
111
+ if (setA.size !== setB.size || ![...setA].every(f => setB.has(f))) {
112
+ return state;
113
+ }
114
+ return {
115
+ ...state,
116
+ byTaxon: {
117
+ ...state.byTaxon,
118
+ [payload.taxon]: { ...t, selectedFields: payload.fields }
119
+ }
120
+ };
121
+ }
122
+
123
+ case 'EXPRVIZ_FETCH_STARTED': {
124
+ const t = ensureTaxon(state, payload.taxon);
125
+ return {
126
+ ...state,
127
+ byTaxon: {
128
+ ...state.byTaxon,
129
+ [payload.taxon]: {
130
+ ...t,
131
+ fetch: { ...t.fetch, status: 'loading', signature: payload.signature, requestId: payload.requestId, error: null }
132
+ }
133
+ }
134
+ };
135
+ }
136
+ case 'EXPRVIZ_FETCH_BATCH': {
137
+ const t = state.byTaxon[payload.taxon];
138
+ if (!t || payload.requestId !== t.fetch.requestId) return state;
139
+ const rows = t.rows.concat(payload.docs);
140
+ return {
141
+ ...state,
142
+ byTaxon: {
143
+ ...state.byTaxon,
144
+ [payload.taxon]: {
145
+ ...t,
146
+ rows,
147
+ fetch: { ...t.fetch, offset: rows.length, total: payload.total, status: rows.length >= payload.total ? 'done' : 'loading' }
148
+ }
149
+ }
150
+ };
151
+ }
152
+ case 'EXPRVIZ_FETCH_FAILED': {
153
+ const t = state.byTaxon[payload.taxon];
154
+ if (!t || payload.requestId !== t.fetch.requestId) return state;
155
+ return {
156
+ ...state,
157
+ byTaxon: {
158
+ ...state.byTaxon,
159
+ [payload.taxon]: { ...t, fetch: { ...t.fetch, status: 'error', error: payload.error } }
160
+ }
161
+ };
162
+ }
163
+
164
+ case 'EXPRVIZ_ATTRS_STARTED': {
165
+ const t = ensureTaxon(state, payload.taxon);
166
+ return {
167
+ ...state,
168
+ byTaxon: {
169
+ ...state.byTaxon,
170
+ [payload.taxon]: { ...t, attrsRequestId: payload.requestId, attrsSignature: payload.signature }
171
+ }
172
+ };
173
+ }
174
+ case 'EXPRVIZ_ATTRS_SUCCEEDED': {
175
+ const t = state.byTaxon[payload.taxon];
176
+ if (!t || payload.requestId !== t.attrsRequestId) return state;
177
+ return {
178
+ ...state,
179
+ byTaxon: {
180
+ ...state.byTaxon,
181
+ [payload.taxon]: { ...t, availableAttrs: payload.attrs }
182
+ }
183
+ };
184
+ }
185
+
186
+ case 'EXPRVIZ_EXISTENCE_STARTED': {
187
+ const t = ensureTaxon(state, payload.taxon);
188
+ return {
189
+ ...state,
190
+ byTaxon: {
191
+ ...state.byTaxon,
192
+ [payload.taxon]: {
193
+ ...t,
194
+ existenceStatus: 'loading',
195
+ existenceRequestId: payload.requestId,
196
+ existenceSignature: payload.signature
197
+ }
198
+ }
199
+ };
200
+ }
201
+ case 'EXPRVIZ_EXISTENCE_SUCCEEDED': {
202
+ const t = state.byTaxon[payload.taxon];
203
+ if (!t || payload.requestId !== t.existenceRequestId) return state;
204
+ return {
205
+ ...state,
206
+ byTaxon: {
207
+ ...state.byTaxon,
208
+ [payload.taxon]: { ...t, existenceStatus: 'ready', fieldExistence: payload.counts }
209
+ }
210
+ };
211
+ }
212
+
213
+ case 'GRAMENE_SEARCH_CLEARED': {
214
+ // Search context changed — invalidate pivot and any loaded rows.
215
+ // Selected fields are kept so the user can re-run the load.
216
+ const newByTaxon = {};
217
+ for (const tid of Object.keys(state.byTaxon)) {
218
+ newByTaxon[tid] = {
219
+ ...state.byTaxon[tid],
220
+ rows: [],
221
+ fetch: { status: 'idle', offset: 0, total: 0, signature: null, requestId: 0, error: null },
222
+ availableAttrs: null,
223
+ attrsSignature: null,
224
+ fieldExistence: null,
225
+ existenceStatus: 'idle',
226
+ existenceSignature: null
227
+ };
228
+ }
229
+ return {
230
+ ...state,
231
+ pivot: { status: 'idle', signature: null, data: {}, error: null, requestId: 0 },
232
+ byTaxon: newByTaxon
233
+ };
234
+ }
235
+
236
+ default:
237
+ return state;
238
+ }
239
+ };
240
+ },
241
+
242
+ doFetchExprVizPivot: () => ({ dispatch, store }) => {
243
+ const requestId = ++pivotPendingId;
244
+ const signature = pivotSignature(store);
245
+ dispatch({ type: 'EXPRVIZ_PIVOT_STARTED', payload: { requestId, signature } });
246
+
247
+ const api = store.selectGrameneAPI();
248
+ const q = store.selectGrameneFiltersQueryString();
249
+ const g = store.selectGrameneGenomes();
250
+ const m = store.selectGrameneMaps() || {};
251
+ const taxa = Object.keys(g.active || {}).filter(tid => m[tid] && !m[tid].hidden);
252
+ const fq = taxa.length ? `&fq=taxon_id:(${taxa.join(' OR ')})` : '';
253
+ const facetField = "{!facet.limit='300' facet.mincount='1' key='taxon_id'}taxon_id";
254
+ const url = `${api}/search?q=${q}${fq}&fq=expressed_in_gxa_attr_ss:*&rows=0&facet=true&facet.field=${facetField}`;
255
+
256
+ fetch(url)
257
+ .then(r => r.json())
258
+ .then(json => {
259
+ const arr = (json && json.facet_counts && json.facet_counts.facet_fields && json.facet_counts.facet_fields.taxon_id) || [];
260
+ const data = {};
261
+ for (let i = 0; i < arr.length; i += 2) {
262
+ data[arr[i]] = arr[i + 1];
263
+ }
264
+ if (Object.keys(data).length === 0) {
265
+ console.warn('[exprViz] no taxon_ids found with expression', { url, json });
266
+ }
267
+ dispatch({ type: 'EXPRVIZ_PIVOT_SUCCEEDED', payload: { requestId, data } });
268
+ })
269
+ .catch(err => {
270
+ dispatch({ type: 'EXPRVIZ_PIVOT_FAILED', payload: { requestId, error: String(err) } });
271
+ });
272
+ },
273
+
274
+ doSetExprVizActiveTaxon: tid => ({ dispatch }) =>
275
+ dispatch({ type: 'EXPRVIZ_ACTIVE_TAXON_SET', payload: tid }),
276
+
277
+ doToggleExprVizFieldsModal: (taxon, open) => ({ dispatch, store }) => {
278
+ dispatch({ type: 'EXPRVIZ_FIELDS_MODAL_TOGGLED', payload: { taxon, open: !!open } });
279
+ if (open) store.doFetchExprVizAvailableAttrs(taxon);
280
+ },
281
+
282
+ doFetchExprVizAvailableAttrs: taxon => ({ dispatch, store }) => {
283
+ const q = store.selectGrameneFiltersQueryString();
284
+ const signature = `${q}|${taxon}`;
285
+ const ev = store.selectExprViz();
286
+ const t = ev.byTaxon[taxon];
287
+ if (t && t.availableAttrs && t.attrsSignature === signature) return;
288
+ const requestId = (attrsPending[taxon] = (attrsPending[taxon] || 0) + 1);
289
+ dispatch({ type: 'EXPRVIZ_ATTRS_STARTED', payload: { taxon, requestId, signature } });
290
+
291
+ const api = store.selectGrameneAPI();
292
+ const facetField = "{!facet.limit='2000' facet.mincount='1' key='attrs'}expressed_in_gxa_attr_ss";
293
+ const url = `${api}/search?q=${q}&fq=taxon_id:${taxon}&fq=expressed_in_gxa_attr_ss:*&rows=0&facet=true&facet.field=${facetField}`;
294
+ fetch(url)
295
+ .then(r => r.json())
296
+ .then(json => {
297
+ if (requestId !== attrsPending[taxon]) return;
298
+ const arr = (json && json.facet_counts && json.facet_counts.facet_fields && json.facet_counts.facet_fields.attrs) || [];
299
+ const attrs = [];
300
+ for (let i = 0; i < arr.length; i += 2) attrs.push(arr[i]);
301
+ dispatch({ type: 'EXPRVIZ_ATTRS_SUCCEEDED', payload: { taxon, requestId, attrs } });
302
+ })
303
+ .catch(() => { /* swallow — non-fatal; modal still works without filtering */ });
304
+ },
305
+
306
+ doSetExprVizFields: (taxon, fields) => ({ dispatch }) =>
307
+ dispatch({ type: 'EXPRVIZ_FIELDS_SET', payload: { taxon, fields } }),
308
+
309
+ doReorderExprVizFields: (taxon, fields) => ({ dispatch }) =>
310
+ dispatch({ type: 'EXPRVIZ_FIELDS_REORDERED', payload: { taxon, fields } }),
311
+
312
+ doFetchExprVizData: taxon => ({ dispatch, store }) => {
313
+ const ev = store.selectExprViz();
314
+ const t = ev.byTaxon[taxon];
315
+ if (!t || t.selectedFields.length === 0) return;
316
+ const requestId = (fetchPending[taxon] = (fetchPending[taxon] || 0) + 1);
317
+ const signature = fetchSignature(store, taxon);
318
+ dispatch({ type: 'EXPRVIZ_FETCH_STARTED', payload: { taxon, requestId, signature } });
319
+
320
+ const api = store.selectGrameneAPI();
321
+ const q = store.selectGrameneFiltersQueryString();
322
+ const fl = [...new Set([...ALWAYS_FL, ...t.selectedFields])].join(',');
323
+
324
+ const fetchPage = (offset) => {
325
+ if (requestId !== fetchPending[taxon]) return; // superseded
326
+ const url = `${api}/search?q=${q}&fq=taxon_id:${taxon}&fl=${fl}&rows=${PAGE_SIZE}&start=${offset}`;
327
+ fetch(url)
328
+ .then(r => r.json())
329
+ .then(json => {
330
+ if (requestId !== fetchPending[taxon]) return;
331
+ const docs = (json.response && json.response.docs) || [];
332
+ const total = (json.response && json.response.numFound) || 0;
333
+ dispatch({ type: 'EXPRVIZ_FETCH_BATCH', payload: { taxon, requestId, docs, total } });
334
+ const next = offset + docs.length;
335
+ if (docs.length > 0 && next < total) {
336
+ fetchPage(next);
337
+ }
338
+ })
339
+ .catch(err => {
340
+ dispatch({ type: 'EXPRVIZ_FETCH_FAILED', payload: { taxon, requestId, error: String(err) } });
341
+ });
342
+ };
343
+ fetchPage(0);
344
+ },
345
+
346
+ // Per-candidate-field non-null counts under the current q + taxon. Used by
347
+ // FieldsModal to hide fields that have no data in the current result set
348
+ // before the user picks. Uses stats.field (the swagger whitelist drops
349
+ // facet.query) batched across multiple GETs to stay under URL limits.
350
+ doFetchExprVizFieldExistence: (taxon, fields) => ({ dispatch, store }) => {
351
+ if (!fields || fields.length === 0) return;
352
+ const q = store.selectGrameneFiltersQueryString();
353
+ const fieldsKey = fields.slice().sort().join(',');
354
+ const signature = `${q}|${taxon}|${fieldsKey}`;
355
+ const ev = store.selectExprViz();
356
+ const t = ev.byTaxon[taxon];
357
+ if (t && t.fieldExistence && t.existenceSignature === signature) return;
358
+
359
+ const requestId = (existencePending[taxon] = (existencePending[taxon] || 0) + 1);
360
+ dispatch({ type: 'EXPRVIZ_EXISTENCE_STARTED', payload: { taxon, requestId, signature } });
361
+
362
+ const api = store.selectGrameneAPI();
363
+ const BATCH_SIZE = 40;
364
+ const batches = [];
365
+ for (let i = 0; i < fields.length; i += BATCH_SIZE) {
366
+ batches.push(fields.slice(i, i + BATCH_SIZE));
367
+ }
368
+
369
+ Promise.all(batches.map(batch => {
370
+ const params = new URLSearchParams();
371
+ params.append('q', q);
372
+ params.append('fq', `taxon_id:${taxon}`);
373
+ params.append('rows', '0');
374
+ params.append('stats', 'true');
375
+ batch.forEach(f => params.append('stats.field', f));
376
+ return fetch(`${api}/search?${params.toString()}`)
377
+ .then(r => r.json())
378
+ .then(json => {
379
+ const sf = (json && json.stats && json.stats.stats_fields) || {};
380
+ const out = {};
381
+ for (const f of Object.keys(sf)) {
382
+ const s = sf[f];
383
+ out[f] = s && typeof s.count === 'number' ? s.count : 0;
384
+ }
385
+ return out;
386
+ });
387
+ }))
388
+ .then(results => {
389
+ if (requestId !== existencePending[taxon]) return;
390
+ const counts = Object.assign({}, ...results);
391
+ dispatch({ type: 'EXPRVIZ_EXISTENCE_SUCCEEDED', payload: { taxon, requestId, counts } });
392
+ })
393
+ .catch(() => { /* swallow — non-fatal; modal falls back to showing all */ });
394
+ },
395
+
396
+ reactExprVizPivot: createSelector(
397
+ 'selectExprViz',
398
+ 'selectGrameneFiltersStatus',
399
+ 'selectGrameneViews',
400
+ 'selectGrameneFiltersQueryString',
401
+ 'selectGrameneGenomes',
402
+ 'selectGrameneMaps',
403
+ (ev, filtersStatus, views, q, g, m) => {
404
+ if (!ev || filtersStatus === 'init') return;
405
+ const onView = views && views.options && views.options.find(v => v.id === 'exprViz');
406
+ if (!onView || onView.show !== 'on') return;
407
+ const maps = m || {};
408
+ const taxa = Object.keys((g && g.active) || {}).filter(tid => maps[tid] && !maps[tid].hidden);
409
+ const sig = `${q}|${taxa.sort().join(',')}`;
410
+ if (ev.pivot.status === 'loading') return;
411
+ if (ev.pivot.signature === sig && ev.pivot.status === 'ready') return;
412
+ return { actionCreator: 'doFetchExprVizPivot' };
413
+ }
414
+ ),
415
+
416
+ selectExprViz: state => state.exprViz,
417
+ selectExprVizPivot: state => state.exprViz.pivot,
418
+ selectExprVizActiveTaxon: state => state.exprViz.activeTaxon,
419
+ selectExprVizByTaxon: state => state.exprViz.byTaxon
420
+ };
421
+
422
+ export default exprViz;
@@ -6,5 +6,6 @@ import genomesBundle from './genomes'
6
6
  import fieldCatalogBundle from './swaggerFields'
7
7
  import exporterBundle from './exporter'
8
8
  import ontologiesBundle from './ontologies'
9
+ import exprVizBundle from './exprViz'
9
10
 
10
- export default [...apiBundles, docsBundle, filterBundle, viewsBundle, genomesBundle, fieldCatalogBundle, exporterBundle, ontologiesBundle];
11
+ export default [...apiBundles, docsBundle, filterBundle, viewsBundle, genomesBundle, fieldCatalogBundle, exporterBundle, ontologiesBundle, exprVizBundle];
@@ -11,12 +11,6 @@ const grameneViews = {
11
11
  show: 'off',
12
12
  shouldScroll: false
13
13
  },
14
- {
15
- id: 'userLists',
16
- name: 'User Gene Lists',
17
- show: 'off',
18
- shouldScroll: false
19
- },
20
14
  {
21
15
  id: 'taxonomy',
22
16
  name: 'Taxonomic distribution',
@@ -30,15 +24,14 @@ const grameneViews = {
30
24
  shouldScroll: false
31
25
  },
32
26
  {
33
- id: 'expression',
34
- name: 'Gene expression',
27
+ id: 'exprViz',
28
+ name: 'Expression visualization',
35
29
  show: 'off',
36
- shouldScroll: false,
37
- desiredSamples: {}
30
+ shouldScroll: false
38
31
  },
39
32
  {
40
- id: 'attribs',
41
- name: 'Gene attributes',
33
+ id: 'userLists',
34
+ name: 'User Gene Lists',
42
35
  show: 'off',
43
36
  shouldScroll: false
44
37
  },
@@ -47,6 +40,19 @@ const grameneViews = {
47
40
  name: 'Data exporter',
48
41
  show: 'off',
49
42
  shouldScroll: false
43
+ },
44
+ {
45
+ id: 'expression',
46
+ name: 'Gene expression',
47
+ show: 'off',
48
+ shouldScroll: false,
49
+ desiredSamples: {}
50
+ },
51
+ {
52
+ id: 'attribs',
53
+ name: 'Gene attributes',
54
+ show: 'off',
55
+ shouldScroll: false
50
56
  }
51
57
  // {
52
58
  // id: 'domains',
@@ -115,7 +121,7 @@ const grameneViews = {
115
121
  const touched = raw.touched || {};
116
122
  const numFound = (search && search.response && search.response.numFound) || 0;
117
123
  const hasFilters = !!(filters && filters.rightIdx > 1);
118
- const resultDependentIds = new Set(['taxonomy', 'list']);
124
+ const resultDependentIds = new Set(['taxonomy', 'list', 'export']);
119
125
  const autoDisable = (numFound === 0) || !hasFilters;
120
126
  const hasFirebase = !!(config && config.firebaseConfig);
121
127
 
@@ -137,6 +143,10 @@ const grameneViews = {
137
143
  if (touched[v.id]) return v;
138
144
  return { ...v, show: o };
139
145
  });
146
+ const anyOtherOn = options.some(v => v.id !== 'help' && v.show === 'on');
147
+ if (!anyOtherOn) {
148
+ options = options.map(v => v.id === 'help' ? { ...v, show: 'on' } : v);
149
+ }
140
150
  return { ...raw, options };
141
151
  }
142
152
  )